📄 schedule-widget.c
字号:
if (info) { g_free (info); info = NULL; } schedule_paint (widget); return TRUE;}static gintschedule_key_press_event (GtkWidget * widget, GdkEventKey * event){ return FALSE;}static voidschedule_paint (GtkWidget * widget){ int i; int x, y; int width, height; int start_row = 0, stop_row = 0; GList *seek; Schedule *schedule = SCHEDULE (widget); g_return_if_fail (widget != NULL); x = widget->allocation.x; y = widget->allocation.y; width = widget->allocation.width; height = widget->allocation.height; start_row = (y / SCHEDULE (widget)->row_height) - 1; /* stop_row = ((y + height) / SCHEDULE (widget)->row_height) + 1; */ switch (schedule->granularity) { case SCHEDULE_15M: stop_row = 96; break; case SCHEDULE_30M: stop_row = 48; break; case SCHEDULE_60M: stop_row = 24; break; } gtk_draw_shadow (widget->style, widget->window, GTK_STATE_NORMAL, GTK_SHADOW_OUT, 0, 0, SCHEDULE (widget)->width, SCHEDULE (widget)->height); for (i = start_row; i < stop_row; i++) { schedule_paint_row (widget, i); } seek = SCHEDULE (widget)->children; while (seek) { ScheduleChild *child = seek->data; schedule_child_handle_text (schedule, child); schedule_child_paint (child); gtk_widget_draw (child->widget, NULL); seek = g_list_next (seek); } seek = schedule_find_conflicts (schedule); while (seek) { schedule_show_conflict (schedule, (time_t) seek->data); seek = g_list_next (seek); }}GList *schedule_find_conflicts (Schedule * schedule){ GList *children = schedule->children; GList *seek; GList *list = NULL; ScheduleChild *child, *test; time_t time; while (children) { child = children->data; seek = schedule->children; while (seek) { test = seek->data; time = test->start; if (child->data != test->data) { if ((time >= child->start) && (time <= child->stop)) { if ((time != child->stop)) list = g_list_append (list, (gpointer) time); } } seek = seek->next; } children = children->next; } return list;}static voidschedule_show_conflict (Schedule * schedule, time_t time){ GtkWidget *widget = GTK_WIDGET (schedule); GdkWindow *window = widget->window; int row = schedule_time_to_row (1, schedule, time, time); GtkStyle *custom; GdkGC *gc; GdkColor cfcolor = {0, 0xff00, 0xff00, 0x5500}; GdkGC *dark_grey, *light_grey; int start = 0, stop = 0; if (schedule->dark) dark_grey = schedule->dark; else dark_grey = widget->style->base_gc[GTK_STATE_INSENSITIVE]; if (schedule->light) light_grey = schedule->light; else light_grey = widget->style->base_gc[GTK_STATE_NORMAL]; switch (schedule->granularity) { case SCHEDULE_15M: start = schedule->day_start_row * 2 + 1; stop = schedule->day_end_row * 2; break; case SCHEDULE_30M: start = schedule->day_start_row; stop = schedule->day_end_row; break; case SCHEDULE_60M: start = schedule->day_start_row / 2; stop = schedule->day_end_row / 2; break; } if ((SCHEDULE (widget)->selected_row_start) >= 0) { int start, stop, diff, paint; start = SCHEDULE (widget)->selected_row_start; stop = SCHEDULE (widget)->selected_row_stop; diff = stop - start; paint = 0; if (row == start) { paint = 1; } else { if (stop == -1) diff = 0; if (diff > 0) { if ((row > start) && (row <= (start + diff))) paint = 1; } else if (diff < 0) { if ((row < start) && (row >= (start + diff))) paint = 1; } } if (paint == 1) dark_grey = light_grey = widget->style->bg_gc[GTK_STATE_SELECTED]; } if ((row > start) && (row < stop)) { gdk_draw_rectangle (widget->window, light_grey, TRUE, 64, row * SCHEDULE (widget)->row_height + 1, SCHEDULE (widget)->width - 66, 19); } else { gdk_draw_rectangle (widget->window, dark_grey, TRUE, 64, row * SCHEDULE (widget)->row_height + 1, SCHEDULE (widget)->width - 66, 19); } custom = gtk_style_copy (widget->style); custom->font = gdk_font_load ("-*-helvetica-bold-o-*-*-*-180-*-*-*-*-*-1"); if (!custom->font) custom->font = widget->style->font; gc = gdk_gc_new (window); gdk_color_alloc (gtk_widget_get_colormap (widget), &cfcolor); gdk_gc_set_foreground (gc, &cfcolor); gdk_draw_string (window, custom->font, widget->style->black_gc, 64 + 3, row * SCHEDULE (widget)->row_height + 18, "!"); gdk_draw_string (window, custom->font, gc, 64 + 2, row * SCHEDULE (widget)->row_height + 17, "!");}static voidschedule_paint_row (GtkWidget * widget, int row){ GdkGC *white, *black, *dark_grey, *light_grey; Schedule *schedule = SCHEDULE (widget); int count = 0, min = 0, start = 0, stop = 0; char buf[40]; char tmp[40]; struct tm time; GtkStyle *style; g_return_if_fail (widget != NULL); g_return_if_fail (widget->style != NULL); switch (schedule->granularity) { case SCHEDULE_15M: count = 95; min = 15; start = schedule->day_start_row * 2 + 1; stop = schedule->day_end_row * 2; break; case SCHEDULE_30M: count = 47; min = 30; start = schedule->day_start_row; stop = schedule->day_end_row; break; case SCHEDULE_60M: count = 23; min = 60; start = schedule->day_start_row / 2; stop = schedule->day_end_row / 2; break; } if ((row < 0) || (row > count)) return; black = widget->style->bg_gc[GTK_STATE_SELECTED]; white = widget->style->dark_gc[GTK_STATE_NORMAL]; if (schedule->dark) dark_grey = schedule->dark; else dark_grey = widget->style->base_gc[GTK_STATE_INSENSITIVE]; if (schedule->light) light_grey = schedule->light; else light_grey = widget->style->base_gc[GTK_STATE_NORMAL]; memset (&time, 0, sizeof (struct tm)); time.tm_min = row * min; time.tm_hour = time.tm_min / 60; time.tm_min = time.tm_min % 60; time.tm_hour = time.tm_hour % 24; time.tm_isdst = -1; mktime (&time); gdk_draw_rectangle (widget->window, black, TRUE, 2, row * SCHEDULE (widget)->row_height + 1, 62, 19); if ((row > start) && (row < stop)) { gdk_draw_rectangle (widget->window, light_grey, TRUE, 64, row * SCHEDULE (widget)->row_height + 1, SCHEDULE (widget)->width - 66, 19); } else { gdk_draw_rectangle (widget->window, dark_grey, TRUE, 64, row * SCHEDULE (widget)->row_height + 1, SCHEDULE (widget)->width - 66, 19); } if ((SCHEDULE (widget)->selected_row_start) >= 0) { int start, stop, diff, paint; start = SCHEDULE (widget)->selected_row_start; stop = SCHEDULE (widget)->selected_row_stop; diff = stop - start; paint = 0; if (row == start) { paint = 1; } else { if (stop == -1) diff = 0; if (diff > 0) { if ((row > start) && (row <= (start + diff))) paint = 1; } else if (diff < 0) { if ((row < start) && (row >= (start + diff))) paint = 1; } } if (paint == 1) gdk_draw_rectangle (widget->window, black, TRUE, 64, row * SCHEDULE (widget)->row_height + 1, SCHEDULE (widget)->width - 66, 19); } gdk_draw_line (widget->window, black, 2, row * SCHEDULE (widget)->row_height + SCHEDULE (widget)->row_height, widget->allocation.width - 2, row * SCHEDULE (widget)->row_height + SCHEDULE (widget)->row_height); gdk_draw_line (widget->window, white, 64, row * SCHEDULE (widget)->row_height + SCHEDULE (widget)->row_height, widget->allocation.width - 3, row * SCHEDULE (widget)->row_height + SCHEDULE (widget)->row_height); if (SCHEDULE (widget)->time_style == SCHEDULE_AMPM) strftime (buf, sizeof (buf), "%I:%M %p", &time); else strftime (buf, sizeof (buf), " %H:%M", &time); black = widget->style->fg_gc[GTK_STATE_SELECTED]; strftime (tmp, sizeof (tmp), "%M", &time); style = gtk_style_copy (widget->style); if (strcmp (tmp, "00")) { style->font = gdk_font_load ("-*-helvetica-normal-r-*-*-*-120-*-*-*-*-*-1"); } else { style->font = gdk_font_load ("-*-helvetica-bold-r-*-*-*-120-*-*-*-*-*-1"); } if (!style->font) style->font = widget->style->font; gdk_draw_string (widget->window, style->font, black, 5, row * SCHEDULE (widget)->row_height + 15, buf); gdk_draw_line (widget->window, white, 63, row * SCHEDULE (widget)->row_height + 1, 63, row * SCHEDULE (widget)->row_height + SCHEDULE (widget)->row_height);}static voidschedule_size_request (GtkWidget * widget, GtkRequisition * requisition){ Schedule *schedule = SCHEDULE (widget); int count = 0; g_return_if_fail (widget != NULL); g_return_if_fail (SCHEDULE_IS_SCHEDULE (widget)); switch (schedule->granularity) { case SCHEDULE_15M: count = 96; break; case SCHEDULE_30M: count = 48; break; case SCHEDULE_60M: count = 24; break; } schedule->width = requisition->width = widget->parent->allocation.width - 4; schedule->height = requisition->height = (schedule->row_height * count) + 4;}static voidschedule_size_allocate (GtkWidget * widget, GtkAllocation * allocation){ g_return_if_fail (widget != NULL); g_return_if_fail (SCHEDULE_IS_SCHEDULE (widget)); g_return_if_fail (allocation != NULL); widget->allocation.x = allocation->x; widget->allocation.y = allocation->y; widget->allocation.width = allocation->width; widget->allocation.height = allocation->height; if (GTK_WIDGET_REALIZED (widget)) { gdk_window_move_resize (widget->window, allocation->x, allocation->y, allocation->width, allocation->height); schedule_send_configure (SCHEDULE (widget)); }}static voidschedule_send_configure (Schedule * schedule){ GtkWidget *widget; GdkEventConfigure event; widget = GTK_WIDGET (schedule); event.type = GDK_CONFIGURE; event.window = widget->window; event.x = widget->allocation.x; event.y = widget->allocation.y; event.width = widget->allocation.width; event.height = widget->allocation.height; gtk_widget_event (widget, (GdkEvent *) & event);}ScheduleChild *schedule_child_new (Schedule * schedule){ ScheduleChild *child = (ScheduleChild *) g_malloc0 (sizeof (ScheduleChild)); child->schedule = schedule; child->start = -1; child->stop = -1; child->widget = gtk_text_new (NULL, NULL); gtk_text_set_word_wrap (GTK_TEXT (child->widget), TRUE); gtk_text_set_editable (GTK_TEXT (child->widget), FALSE); schedule->children = g_list_append (schedule->children, child); gtk_signal_connect_after (GTK_OBJECT (child->widget), "focus_in_event", GTK_SIGNAL_FUNC (schedule_child_focus_in), child); gtk_signal_connect_after (GTK_OBJECT (child->widget), "key_press_event", GTK_SIGNAL_FUNC (schedule_child_key_press), child); child->focus_out_id = gtk_signal_connect_after (GTK_OBJECT (child->widget), "focus_out_event", GTK_SIGNAL_FUNC (schedule_child_focus_out), child); return child;}static intschedule_time_to_row (int first, Schedule * schedule, time_t start, time_t stop){ time_t time = (first ? start : stop); int start_year, stop_year, start_month, stop_month, start_day, stop_day; struct tm *tms; int row = 0; tms = localtime (&start); start_year = tms->tm_year; start_month = tms->tm_mon; start_day = tms->tm_mday; tms = localtime (&stop); stop_year = tms->tm_year; stop_month = tms->tm_mon; stop_day = tms->tm_mday; if (!first) { if (stop_year > start_year) return 48; if (stop_month > start_month) return 48; if (stop_day > start_day) return 48; } tms = localtime (&time); switch (schedule->granularity) { case SCHEDULE_15M: row = tms->tm_hour * 4; break; case SCHEDULE_30M: row = tms->tm_hour * 2; break; case SCHEDULE_60M: row = tms->tm_hour; break; } if (tms->tm_min >= 30) row++; return row;}voidschedule_add_child (Schedule * schedule, char *text, time_t start, time_t stop, gpointer data){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -