📄 clock.c
字号:
/* GKrellM| Copyright (C) 1999-2006 Bill Wilson|| Author: Bill Wilson billw@gkrellm.net| Latest versions might be found at: http://gkrellm.net|| This program is free software which I release under the GNU General Public| License. You may redistribute and/or modify this program under the terms| of that license as published by the Free Software Foundation; either| version 2 of the License, or (at your option) any later version.|| This program is distributed in the hope that it will be useful,| but WITHOUT ANY WARRANTY; without even the implied warranty of| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the| GNU General Public License for more details. Version 2 is in the| COPYRIGHT file in the top level directory of this distribution.| | To get a copy of the GNU General Puplic License, write to the Free Software| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*/#include "gkrellm.h"#include "gkrellm-private.h"#if defined(WIN32)#define STRFTIME_24_HOUR "%#H:%M"#define STRFTIME_12_HOUR "%#I:%M"#else#define STRFTIME_24_HOUR "%k:%M"#define STRFTIME_12_HOUR "%l:%M"#endifstatic GkrellmMonitor *mon_clock, *mon_cal;static GkrellmPanel *pclock, *pcal;static GkrellmDecal *d_wday, *d_mday, *d_month, *d_clock, *d_seconds;static GkrellmLauncher clock_launch, cal_launch;typedef struct { GkrellmTextstyle ts; gint width, height, baseline, y_ink; } Extents;static gboolean cal_enable, clock_enable, clock_12hr_sec_enable, clock_24hr_sec_enable, clock_24hr_mode, loop_chime_enable;static gint x_clock_seconds, x_clock_no_seconds;static gchar *hour_chime_command, *quarter_chime_command;typedef struct { gchar *command; gint count; } ChimeData;static Extents wday_extents, mday_extents, month_extents, time_extents, sec_extents, ampm_extents;static gint clock_style_id, cal_style_id;static gboolean chime_block;struct tm gkrellm_current_tm;struct tm *gkrellm_get_current_time(void) { return &gkrellm_current_tm; }static voidstring_extents(gchar *text, Extents *ext) { gkrellm_text_extents(ext->ts.font, text, strlen(text), &ext->width, &ext->height, &ext->baseline, &ext->y_ink); }static gpointerchime_func(gpointer data) { ChimeData *chime = (ChimeData *)data; gint counter; if (strlen(chime->command)) { if (chime->count > 12) chime->count -= 12; for (counter = 0; counter < chime -> count; counter ++) g_spawn_command_line_sync(chime->command, NULL, NULL, NULL, NULL /* GError */); } g_free(chime->command); g_free(chime); return NULL; }static gchar *utf8_from_strftime(gchar *buf) { gchar *utf8 = NULL; if ( g_utf8_validate(buf, -1, NULL) || (utf8 = g_locale_to_utf8(buf, -1, NULL, NULL, NULL)) == NULL ) utf8 = g_strdup(buf); return utf8; }static voiddraw_cal(void) { struct tm *t; gchar *utf8, buf[32]; gint w; if (!cal_enable) return; if (_GK.client_mode) t = gkrellm_client_server_time(); else t = &gkrellm_current_tm; strftime(buf, sizeof(buf), "%a", t); /* Abbreviated weekday name */ utf8 = utf8_from_strftime(buf); w = gkrellm_gdk_string_width(d_wday->text_style.font, utf8); d_wday->x_off = (w < d_wday->w) ? (d_wday->w - w) / 2 : 0; gkrellm_draw_decal_text(pcal, d_wday, utf8, -1); g_free(utf8); /*strftime(buf, sizeof(buf), "%e", t);*/ /* Day in month 1-31 */ snprintf(buf, sizeof(buf), "%d", t->tm_mday); w = gkrellm_gdk_string_width(d_mday->text_style.font, buf); d_mday->x_off = (w < d_mday->w) ? (d_mday->w - w) / 2 : 0; gkrellm_draw_decal_text(pcal, d_mday, buf, -1); strftime(buf, sizeof(buf), "%b", t); /* Abbreviated month name */ utf8 = utf8_from_strftime(buf); gkrellm_draw_decal_text(pcal, d_month, utf8, -1); g_free(utf8); gkrellm_draw_panel_layers(pcal); }static voiddraw_clock(gboolean update_minutes) { struct tm *t; gchar buf[32], sec_ampm[32], *utf8; gint w; if (!clock_enable) return; if (_GK.client_mode) t = gkrellm_client_server_time(); else t = &gkrellm_current_tm; if (update_minutes) { if (clock_24hr_mode) strftime(buf, sizeof(buf), STRFTIME_24_HOUR, t); else strftime(buf, sizeof(buf), STRFTIME_12_HOUR, t); w = gkrellm_gdk_string_width(d_clock->text_style.font, buf); d_clock->x_off = (w < d_clock->w) ? (d_clock->w - w) / 2 : 0; gkrellm_draw_decal_text(pclock, d_clock, buf, -1); } if ( (!clock_24hr_mode && clock_12hr_sec_enable) || (clock_24hr_mode && clock_24hr_sec_enable) ) { strftime(sec_ampm, sizeof(sec_ampm), "%S", t); /* seconds 00-59 */ gkrellm_draw_decal_text(pclock, d_seconds, sec_ampm, -1); } else if (! clock_24hr_mode) { strftime(sec_ampm, sizeof(sec_ampm), "%p", t); /* AM/PM */ if (sec_ampm[0] == '\0') /* locale doesn't support AM/PM */ strcpy(sec_ampm, "--"); if ( g_utf8_validate(sec_ampm, -1, NULL) || (utf8 = g_locale_to_utf8(sec_ampm, -1, NULL, NULL,NULL)) == NULL ) utf8 = g_strdup(sec_ampm); gkrellm_draw_decal_text(pclock, d_seconds, utf8, -1); g_free(utf8); } gkrellm_draw_panel_layers(pclock); }static voidupdate_clock(void) { struct tm *ptm; gboolean update_minutes = FALSE; static gint min_prev, hour_prev = -1, sec_prev = -1; ChimeData *chime; if (_GK.client_mode) { ptm = gkrellm_client_server_time(); if (sec_prev == ptm->tm_sec) return; sec_prev = ptm->tm_sec; } else { if (!GK.second_tick) return; ptm = &gkrellm_current_tm; } if ( ptm->tm_min != min_prev || ptm->tm_hour != hour_prev ) { update_minutes = TRUE; draw_cal(); if (ptm->tm_hour != hour_prev && hour_prev != -1) { if (!chime_block && hour_chime_command && *hour_chime_command) { chime = g_new0(ChimeData, 1); chime -> command = strdup(hour_chime_command); chime -> count = loop_chime_enable ? ptm->tm_hour : 1; g_thread_create(chime_func, chime, FALSE, NULL); } } else { if ( !chime_block && (ptm->tm_min % 15) == 0 && quarter_chime_command && *quarter_chime_command ) { chime = g_new0(ChimeData, 1); chime -> command = strdup(quarter_chime_command); chime -> count = 1; g_thread_create(chime_func, chime, FALSE, NULL); } } } draw_clock(update_minutes); min_prev = ptm->tm_min; hour_prev = ptm->tm_hour; }static gintexpose_event(GtkWidget *widget, GdkEventExpose *ev) { GdkPixmap *pixmap = NULL; if (widget == pcal->drawing_area) pixmap = pcal->pixmap; else if (widget == pclock->drawing_area) pixmap = pclock->pixmap; if (pixmap) gdk_draw_drawable(widget->window, gkrellm_draw_GC(1), pixmap, ev->area.x, ev->area.y, ev->area.x, ev->area.y, ev->area.width, ev->area.height); return FALSE; }static gintcb_panel_press(GtkWidget *widget, GdkEventButton *ev) { if (ev->button == 3) gkrellm_open_config_window(mon_clock); return FALSE; }static voidcal_visibility(void) { if (cal_enable) { gkrellm_panel_show(pcal); gkrellm_spacers_show(mon_cal); } else { gkrellm_panel_hide(pcal); gkrellm_spacers_hide(mon_cal); } }static voidcreate_calendar(GtkWidget *vbox, gint first_create) { GkrellmStyle *style; Extents ext; gchar buf[32], *utf8, *str = NULL; gint m, d, w; struct tm tm = *gkrellm_get_current_time(); if (first_create) pcal = gkrellm_panel_new0(); style = gkrellm_meter_style(cal_style_id); ext.ts = *gkrellm_meter_textstyle(cal_style_id); /* Get max weekday name height for current locale */ wday_extents.width = wday_extents.height = 0; wday_extents.baseline = wday_extents.y_ink = 0; for (d = 0; d < 6; ++d) { tm.tm_wday = d; strftime(buf, sizeof(buf), "%a", &tm); /* Abbreviated weekday name */ utf8 = utf8_from_strftime(buf); string_extents(utf8, &ext); wday_extents.baseline = ext.baseline; /* not used */ if (ext.width > wday_extents.width) wday_extents.width = ext.width; if (ext.height > wday_extents.height) { str = utf8; wday_extents.height = ext.height; /* not used */ wday_extents.y_ink = ext.y_ink; /* not used */ } else g_free(utf8); } wday_extents.ts = *gkrellm_meter_textstyle(cal_style_id); d_wday = gkrellm_create_decal_text(pcal, str, &wday_extents.ts, style, 0, -1 /*top margin*/, wday_extents.width + 2); g_free(str); mday_extents.ts = *gkrellm_meter_alt_textstyle(cal_style_id); string_extents("28", &mday_extents); d_mday = gkrellm_create_decal_text(pcal, "28", &mday_extents.ts, style, 0, -1 /*top margin*/, mday_extents.width + 2); /* Get max month name height for current locale */ month_extents.width = month_extents.height = 0; month_extents.baseline = month_extents.y_ink = 0; for (m = 0; m < 11; ++m) { tm.tm_mon = m; strftime(buf, sizeof(buf), "%b", &tm); /* Abbreviated month name */ utf8 = utf8_from_strftime(buf); string_extents(utf8, &ext); month_extents.baseline = ext.baseline; /* not used */ if (ext.width > month_extents.width) month_extents.width = ext.width; if (ext.height > month_extents.height) { str = utf8; month_extents.height = ext.height; /* not used */ month_extents.y_ink = ext.y_ink; /* not used */ } else g_free(utf8); } month_extents.ts = *gkrellm_meter_textstyle(cal_style_id); d_month = gkrellm_create_decal_text(pcal, str, &month_extents.ts, style, 0, -1 /*top margin*/, month_extents.width + 2); g_free(str); w = d_wday->w + d_mday->w + d_month->w; d_wday->x = (gkrellm_chart_width() - w) / 2; d_mday->x = d_wday->x + d_wday->w; d_month->x = d_mday->x + d_mday->w; if (d_wday->h < d_mday->h) { d_wday->y = d_mday->y + d_mday->h - d_wday->h - 1; d_month->y = d_wday->y; } else if (d_mday->h < d_wday->h) d_mday->y = d_wday->y + d_wday->h - d_mday->h - 1; gkrellm_panel_configure(pcal, NULL, style); gkrellm_panel_create(vbox, mon_cal, pcal); if (first_create)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -