From 3a99ca67912282fbbc46839a5794db72d8c2eda7 Mon Sep 17 00:00:00 2001 From: Geert Janssens Date: Thu, 3 Sep 2015 15:45:46 +0200 Subject: [PATCH] Gtk code cleanups: Convert dense-cal from gdk_gc_* to cairo --- src/gnome-utils/gnc-dense-cal.c | 172 ++++++++++++---------------- src/gnome-utils/gnc-dense-cal.h | 2 +- src/gnome/gnc-plugin-page-sx-list.c | 3 - 3 files changed, 77 insertions(+), 100 deletions(-) diff --git a/src/gnome-utils/gnc-dense-cal.c b/src/gnome-utils/gnc-dense-cal.c index 88d894ce99..abd6cd1997 100644 --- a/src/gnome-utils/gnc-dense-cal.c +++ b/src/gnome-utils/gnc-dense-cal.c @@ -657,10 +657,10 @@ gnc_dense_cal_dispose (GObject *object) dcal->transPopup = NULL; } - if (dcal->drawbuf) + if (dcal->surface) { - g_object_unref(dcal->drawbuf); - dcal->drawbuf = NULL; + cairo_surface_destroy (dcal->surface); + dcal->surface = NULL; } /* FIXME: we have a bunch of cleanup to do, here. */ @@ -720,13 +720,15 @@ gdc_reconfig(GncDenseCal *dcal) GdkWindow *window; GtkAllocation alloc; - if (dcal->drawbuf) - g_object_unref(dcal->drawbuf); + if (dcal->surface) + cairo_surface_destroy (dcal->surface); widget = GTK_WIDGET(dcal->cal_drawing_area); window = gtk_widget_get_window (widget); gtk_widget_get_allocation (widget, &alloc); - dcal->drawbuf = gdk_pixmap_new(window, alloc.width, alloc.height, -1); + dcal->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, + alloc.width, + alloc.height); gnc_dense_cal_draw_to_buffer(dcal); } @@ -868,7 +870,7 @@ gnc_dense_cal_expose(GtkWidget *widget, gpointer user_data) { GncDenseCal *dcal; - GdkGC *gc; + cairo_t *cr; g_return_val_if_fail(widget != NULL, FALSE); g_return_val_if_fail(GNC_IS_DENSE_CAL(user_data), FALSE); @@ -878,10 +880,10 @@ gnc_dense_cal_expose(GtkWidget *widget, return FALSE; dcal = GNC_DENSE_CAL(user_data); - gc = gtk_widget_get_style (widget)->fg_gc[gtk_widget_get_state(widget)]; - gdk_draw_drawable(GDK_DRAWABLE(gtk_widget_get_window (GTK_WIDGET(dcal->cal_drawing_area))), - gc, GDK_DRAWABLE(dcal->drawbuf), - 0, 0, 0, 0, -1, -1); + cr = gdk_cairo_create (gtk_widget_get_window (GTK_WIDGET(dcal->cal_drawing_area))); + cairo_set_source_surface (cr, dcal->surface, 0, 0); + cairo_paint (cr); + cairo_destroy (cr); return TRUE; } @@ -893,40 +895,41 @@ gnc_dense_cal_draw_to_buffer(GncDenseCal *dcal) { GtkWidget *widget; GtkAllocation alloc; + GdkColor color; gint i; int maxWidth; PangoLayout *layout; GTimer *timer; + cairo_t *cr; timer = g_timer_new(); g_debug("drawing"); widget = GTK_WIDGET(dcal); - if (!dcal->drawbuf) + if (!dcal->surface) return; g_timer_start(timer); + cr = cairo_create (dcal->surface); layout = gtk_widget_create_pango_layout(GTK_WIDGET(dcal), NULL); LOG_AND_RESET(timer, "create_pango_layout"); gtk_widget_get_allocation (GTK_WIDGET(dcal->cal_drawing_area), &alloc); - gdk_draw_rectangle(dcal->drawbuf, - gtk_widget_get_style (widget)->white_gc, - TRUE, - 0, 0, - alloc.width, - alloc.height); + color = gtk_widget_get_style (widget)->white; + cairo_set_source_rgb (cr, color.red / 65535.0, + color.green / 65535.0, + color.blue / 65535.0); + cairo_rectangle (cr, 0, 0, + cairo_image_surface_get_width (dcal->surface), + cairo_image_surface_get_height (dcal->surface)); + cairo_fill (cr); /* Fill in alternating month colors. */ { gint i; - GdkGC *gc; GdkRectangle *rect; GList *mcList, *mcListIter; - gc = gdk_gc_new(gtk_widget_get_window (GTK_WIDGET(dcal))); - gdk_gc_copy(gc, gtk_widget_get_style (widget)->fg_gc[gtk_widget_get_state(widget)]); - /* reset all of the month position offsets. */ for (i = 0; i < 12; i++) { @@ -936,26 +939,26 @@ gnc_dense_cal_draw_to_buffer(GncDenseCal *dcal) /* Paint the weeks for the upcoming N months. */ for (i = 0; i < dcal->numMonths; i++) { - gdk_gc_set_foreground(gc, &dcal->weekColors[ i % 2 ]); - mcList = NULL; month_coords(dcal, i, &mcList); dcal->monthPositions[i].x = floor(i / dcal->monthsPerCol) * (col_width(dcal) + COL_BORDER_SIZE); - dcal->monthPositions[i].y = ((GdkRectangle*)mcList->next->next->data)->y; + dcal->monthPositions[i].y = ((GdkRectangle*)mcList->next->next->next->data)->y; for (mcListIter = mcList; mcListIter != NULL; mcListIter = mcListIter->next) { rect = (GdkRectangle*)mcListIter->data; - gdk_draw_rectangle(dcal->drawbuf, gc, - TRUE, rect->x, rect->y, - rect->width, rect->height); + color = dcal->weekColors[ i % 2 ]; + cairo_set_source_rgb (cr, color.red / 65535.0, + color.green / 65535.0, + color.blue / 65535.0); + cairo_rectangle (cr, rect->x, rect->y, + rect->width, rect->height); + cairo_fill (cr); } g_list_foreach(mcList, free_rect, NULL); g_list_free(mcList); } - - g_object_unref(gc); } LOG_AND_RESET(timer, "alternating month colors"); @@ -963,49 +966,28 @@ gnc_dense_cal_draw_to_buffer(GncDenseCal *dcal) { int i; int x1, x2, y1, y2; - GdkColor markColor; - GdkGCValues current_values; - gdk_gc_get_values(gtk_widget_get_style (widget)->fg_gc[gtk_widget_get_state (widget)], ¤t_values); + gdk_color_parse(MARK_COLOR, &color); + gdk_colormap_alloc_color(gdk_colormap_get_system(), &color, TRUE, TRUE); + cairo_set_source_rgb (cr, color.red / 65535.0, + color.green / 65535.0, + color.blue / 65535.0); - gdk_color_parse(MARK_COLOR, &markColor); - gdk_colormap_alloc_color(gdk_colormap_get_system(), &markColor, TRUE, TRUE); - - gdk_gc_set_foreground(gtk_widget_get_style (widget)->fg_gc[gtk_widget_get_state (widget)], &markColor); for (i = 0; i < dcal->numMarks; i++) { if (dcal->marks[i] != NULL) { - int w, h, x_offset, y_offset, circle_delta; + int center_x, center_y, radius; doc_coords(dcal, i, &x1, &y1, &x2, &y2); - w = x2 - x1; - h = y2 - y1; + center_x = (x1 + x2 ) / 2; + center_y = (y1 + y2 ) / 2; + radius = MIN((x2 - x1), (y2 - y1)) * .75; - x_offset = x1; - y_offset = y1; - - circle_delta = ABS(w - h) / 2; - if (w < h) - { - y_offset += circle_delta; - } - else - { - x_offset += circle_delta; - } - - gdk_draw_arc(dcal->drawbuf, - gtk_widget_get_style (widget)->fg_gc[gtk_widget_get_state (widget)], - TRUE, - x_offset, y_offset, MIN(w, h), MIN(w, h), - 0 * 64, - 360 * 64); + cairo_arc (cr, center_x, center_y, radius, 0.0, 2 * M_PI); + cairo_fill (cr); } } - - // reset to the previous foreground color. - gdk_gc_set_foreground(gtk_widget_get_style (widget)->fg_gc[gtk_widget_get_state (widget)], ¤t_values.foreground); } LOG_AND_RESET(timer, "marked days"); @@ -1025,27 +1007,30 @@ gnc_dense_cal_draw_to_buffer(GncDenseCal *dcal) h = col_height(dcal); /* draw the outside border [inside the month labels] */ - gdk_draw_rectangle(dcal->drawbuf, - gtk_widget_get_style (widget)->fg_gc[gtk_widget_get_state (widget)], - FALSE, x, y, w, h); + color = gtk_widget_get_style (widget)->fg[gtk_widget_get_state (widget)]; + cairo_set_line_width (cr, 1); + cairo_set_source_rgb (cr, color.red / 65535.0, + color.green / 65535.0, + color.blue / 65535.0); + cairo_rectangle (cr, x + 0.5, y + 0.5, w, h); + cairo_stroke (cr); + /* draw the week seperations */ for (j = 0; j < num_weeks_per_col(dcal); j++) { gint wy = y + (j * week_height(dcal)); - gdk_draw_line(dcal->drawbuf, - gtk_widget_get_style (widget)->fg_gc[gtk_widget_get_state (widget)], - x, wy, - x + w, wy); + cairo_move_to (cr, x, wy + 0.5); + cairo_line_to (cr, x + w, wy + 0.5); + cairo_stroke (cr); } /* draw the day seperations */ for (j = 1; j < 7; j++) { gint dx = x + (j * day_width(dcal)); - gdk_draw_line(dcal->drawbuf, - gtk_widget_get_style (widget)->fg_gc[gtk_widget_get_state (widget)], - dx, y, - dx, y + col_height(dcal)); + cairo_move_to (cr, dx + 0.5, y); + cairo_line_to (cr, dx + 0.5, y + col_height(dcal)); + cairo_stroke (cr); } /* draw the day labels */ @@ -1070,10 +1055,12 @@ gnc_dense_cal_draw_to_buffer(GncDenseCal *dcal) - (day_label_width / 2); label_y_offset = y - dcal->dayLabelHeight; pango_layout_set_text(layout, day_label_str, -1); - gdk_draw_layout(GDK_DRAWABLE(dcal->drawbuf), gtk_widget_get_style (widget)->fg_gc[gtk_widget_get_state (widget)], - label_x_offset, label_y_offset, - layout); - + color = gtk_widget_get_style (widget)->text[gtk_widget_get_state (widget)]; + cairo_set_source_rgb (cr, color.red / 65535.0, + color.green / 65535.0, + color.blue / 65535.0); + cairo_move_to (cr, label_x_offset, label_y_offset); + pango_cairo_show_layout (cr, layout); } } } @@ -1082,11 +1069,6 @@ gnc_dense_cal_draw_to_buffer(GncDenseCal *dcal) /* Month labels. */ { gint i; - PangoMatrix matrix = PANGO_MATRIX_INIT; - - pango_matrix_rotate(&matrix, 90.); - pango_context_set_matrix(gtk_widget_get_pango_context(GTK_WIDGET(dcal)), &matrix); - for (i = 0; i < 12; i++) { guint idx; @@ -1095,16 +1077,15 @@ gnc_dense_cal_draw_to_buffer(GncDenseCal *dcal) break; idx = (dcal->month - 1 + i) % 12; pango_layout_set_text(layout, month_name(idx), -1); - gdk_draw_layout(GDK_DRAWABLE(dcal->drawbuf), - gtk_widget_get_style (widget)->fg_gc[gtk_widget_get_state (widget)], - dcal->leftPadding + dcal->monthPositions[i].x, - dcal->monthPositions[i].y, - layout); - } - // reset rotation - pango_matrix_rotate(&matrix, -90.); - pango_context_set_matrix(gtk_widget_get_pango_context(GTK_WIDGET(dcal)), &matrix); + cairo_save (cr); + cairo_translate (cr, dcal->leftPadding + dcal->monthPositions[i].x, + dcal->monthPositions[i].y); + cairo_move_to (cr, 0, 0); + cairo_rotate (cr, -G_PI / 2.); + pango_cairo_show_layout (cr, layout); + cairo_restore (cr); + } } LOG_AND_RESET(timer, "month labels"); @@ -1128,11 +1109,9 @@ gnc_dense_cal_draw_to_buffer(GncDenseCal *dcal) pango_layout_get_pixel_size(layout, &numW, &numH); w = (x2 - x1) + 1; h = (y2 - y1) + 1; - gdk_draw_layout(GDK_DRAWABLE(dcal->drawbuf), - gtk_widget_get_style (widget)->fg_gc[gtk_widget_get_state (widget)], - x1 + (w / 2) - (numW / 2), - y1 + (h / 2) - (numH / 2), - layout); + cairo_move_to (cr, x1 + (w / 2) - (numW / 2), + y1 + (h / 2) - (numH / 2)); + pango_cairo_show_layout (cr, layout); } } LOG_AND_RESET(timer, "dates"); @@ -1147,6 +1126,7 @@ gnc_dense_cal_draw_to_buffer(GncDenseCal *dcal) LOG_AND_RESET(timer, "queue draw"); g_object_unref(layout); + cairo_destroy (cr); g_timer_destroy(timer); } diff --git a/src/gnome-utils/gnc-dense-cal.h b/src/gnome-utils/gnc-dense-cal.h index d13c53c5b7..c193327fcc 100644 --- a/src/gnome-utils/gnc-dense-cal.h +++ b/src/gnome-utils/gnc-dense-cal.h @@ -58,7 +58,7 @@ struct _GncDenseCal GtkComboBox *view_options; GtkDrawingArea *cal_drawing_area; - GdkPixmap *drawbuf; + cairo_surface_t *surface; gboolean initialized; diff --git a/src/gnome/gnc-plugin-page-sx-list.c b/src/gnome/gnc-plugin-page-sx-list.c index 3d547b9184..c97da4430e 100644 --- a/src/gnome/gnc-plugin-page-sx-list.c +++ b/src/gnome/gnc-plugin-page-sx-list.c @@ -261,8 +261,6 @@ gnc_plugin_page_sx_list_init (GncPluginPageSxList *plugin_page) gnc_plugin_page_sx_list_n_actions, plugin_page); /* gnc_plugin_init_short_names (action_group, toolbar_labels); */ - gnc_gobject_tracking_remember (G_OBJECT (plugin_page), - G_OBJECT_CLASS (klass)); } @@ -301,7 +299,6 @@ gnc_plugin_page_sx_list_finalize (GObject *object) g_return_if_fail(GNC_IS_PLUGIN_PAGE_SX_LIST (page)); priv = GNC_PLUGIN_PAGE_SX_LIST_GET_PRIVATE(page); g_return_if_fail(priv != NULL); - gnc_gobject_tracking_forget (G_OBJECT (page)); G_OBJECT_CLASS (parent_class)->finalize (object); }