diff options
author | Burt P <pburt0@gmail.com> | 2017-07-25 06:24:47 -0500 |
---|---|---|
committer | Leandro Pereira <leandro@hardinfo.org> | 2017-07-30 10:16:42 -0700 |
commit | 03184de73de4adf8fcba09b8eac6b8c0a3f06aab (patch) | |
tree | 934ffbe93bd846ac95a1c57f2fed8871ee3b6e47 /shell/loadgraph.c | |
parent | 6f8cedca0671ab68514352c64579546547804475 (diff) |
GTK3 updates
* Use gtk_widget_get_window() instead of ->window
cmake:
* add option to build against gtk3 `-DHARDINFO_GTK3=1`
* combine hardinfo-shell static library and hardinfo
* disable guibench for gtk3
hardinfo/util.c:
* widget_set_cursor() gtk3 changes
* For now, the whole function tree_view_save_image() is disabled
for gtk3.
shell/shell.c:
* add gtk_notebook_set_page() compatibility macro if not defined
* shell_summary_add_item() fixes
* Disable RANGE_GET_VALUE() RANGE_SET_VALUE() macros for GTK3. This
is a nigtmare onion to try and peel.
shell/callbacks.c:
* gtk_about_dialog_set_name() -> gtk_about_dialog_set_program_name()
after GTK+2.12.
shell/loadgraph.c:
* builds, but not yet functioning under gtk3
Signed-off-by: Burt P <pburt0@gmail.com>
Diffstat (limited to 'shell/loadgraph.c')
-rwxr-xr-x | shell/loadgraph.c | 182 |
1 files changed, 104 insertions, 78 deletions
diff --git a/shell/loadgraph.c b/shell/loadgraph.c index b8720464..e78524ca 100755 --- a/shell/loadgraph.c +++ b/shell/loadgraph.c @@ -20,7 +20,7 @@ * You should have received a copy of the GNU Lesser General Public * License along with the Simple Load Graph; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - * 02111-1307 USA. + * 02111-1307 USA. */ #include "loadgraph.h" @@ -89,7 +89,7 @@ void load_graph_clear(LoadGraph * lg) gint i; for (i = 0; i < lg->size; i++) - lg->data[i] = 0; + lg->data[i] = 0; lg->scale = 1.0; lg->max_value = 1; @@ -101,39 +101,44 @@ void load_graph_clear(LoadGraph * lg) void load_graph_set_color(LoadGraph * lg, LoadGraphColor color) { lg->color = color; +#if GTK_CHECK_VERSION(3, 0, 0) +#define UNPACK_COLOR(C) (((C)>>16) & 0xff), (((C)>>8) & 0xff), ((C) & 0xff) + cairo_set_source_rgb(lg->trace, UNPACK_COLOR(lg->color) ); + cairo_set_source_rgb(lg->fill, UNPACK_COLOR(lg->color - 0x303030) ); + cairo_set_source_rgb(lg->grid, UNPACK_COLOR(lg->color - 0xcdcdcd) ); +#else gdk_rgb_gc_set_foreground(lg->trace, lg->color); gdk_rgb_gc_set_foreground(lg->fill, lg->color - 0x303030); gdk_rgb_gc_set_foreground(lg->grid, lg->color - 0xcdcdcd); +#endif } void load_graph_destroy(LoadGraph * lg) { - g_free(lg->data); - gtk_widget_destroy(lg->area); +#if GTK_CHECK_VERSION(3, 0, 0) + g_object_unref(lg->buf); +#else gdk_pixmap_unref(lg->buf); +#endif g_object_unref(lg->trace); g_object_unref(lg->grid); g_object_unref(lg->fill); g_object_unref(lg->layout); + gtk_widget_destroy(lg->area); + g_free(lg->data); g_free(lg); } -static gboolean _expose(GtkWidget * widget, GdkEventExpose * event, - gpointer user_data) +static gboolean _expose(GtkWidget * widget, GdkEventExpose * event, gpointer user_data) { LoadGraph *lg = (LoadGraph *) user_data; #if GTK_CHECK_VERSION(3, 0, 0) - cairo_t *draw = GDK_WINDOW(lg->buf); + /* TODO:GTK3 copy from lg->buf or lg->area? to widget? */ #else GdkDrawable *draw = GDK_DRAWABLE(lg->buf); -#endif - cairo_t *cr; -#if GTK_CHECK_VERSION(3, 0, 0) - gdk_cairo_set_source_window(cr, lg->area, 0, 0); -#else gdk_draw_drawable(lg->area->window, - lg->area->style->black_gc, - draw, 0, 0, 0, 0, lg->width, lg->height); + lg->area->style->black_gc, + draw, 0, 0, 0, 0, lg->width, lg->height); #endif return FALSE; } @@ -144,15 +149,18 @@ void load_graph_configure_expose(LoadGraph * lg) gtk_widget_realize(lg->area); #if GTK_CHECK_VERSION(3, 0, 0) cairo_content_t content; - lg->buf = gdk_window_create_similar_surface(lg->area, CAIRO_CONTENT_COLOR, lg->width, lg->height); + GdkWindow *gdk_window = gtk_widget_get_window(lg->area); + lg->buf = gdk_window_create_similar_surface(gdk_window, CAIRO_CONTENT_COLOR, lg->width, lg->height); + lg->grid = cairo_create(lg->buf); + lg->trace = cairo_create(lg->buf); + lg->fill = cairo_create(lg->buf); #else lg->buf = gdk_pixmap_new(lg->area->window, lg->width, lg->height, -1); -#endif - /* create the graphic contexts */ lg->grid = gdk_gc_new(GDK_DRAWABLE(lg->buf)); lg->trace = gdk_gc_new(GDK_DRAWABLE(lg->buf)); lg->fill = gdk_gc_new(GDK_DRAWABLE(lg->buf)); +#endif /* the default color is green */ load_graph_set_color(lg, LG_COLOR_GREEN); @@ -165,12 +173,12 @@ void load_graph_configure_expose(LoadGraph * lg) cairo_set_dash(lg->grid, 0, (gint8*)"\2\2", 2); #else gdk_gc_set_line_attributes(lg->grid, - 1, GDK_LINE_ON_OFF_DASH, - GDK_CAP_NOT_LAST, GDK_JOIN_ROUND); + 1, GDK_LINE_ON_OFF_DASH, + GDK_CAP_NOT_LAST, GDK_JOIN_ROUND); gdk_gc_set_dashes(lg->grid, 0, (gint8*)"\2\2", 2); #endif - -#if 0 /* old-style grid */ + +#if 0 /* old-style grid */ gdk_rgb_gc_set_foreground(lg->grid, 0x707070); #endif @@ -180,47 +188,61 @@ void load_graph_configure_expose(LoadGraph * lg) cairo_set_line_join(lg->trace, CAIRO_LINE_JOIN_MITER); #else gdk_gc_set_line_attributes(lg->trace, - 1, GDK_LINE_SOLID, - GDK_CAP_PROJECTING, GDK_JOIN_ROUND); + 1, GDK_LINE_SOLID, + GDK_CAP_PROJECTING, GDK_JOIN_ROUND); #endif -#if 0 /* old-style fill */ +#if 0 /* old-style fill */ gdk_gc_set_line_attributes(lg->fill, - 1, GDK_LINE_SOLID, - GDK_CAP_BUTT, GDK_JOIN_BEVEL); + 1, GDK_LINE_SOLID, + GDK_CAP_BUTT, GDK_JOIN_BEVEL); #endif +#if GTK_CHECK_VERSION(3, 0, 0) + /* configures the draw event */ + g_signal_connect(G_OBJECT(lg->area), "draw", + (GCallback) _expose, lg); +#else /* configures the expose event */ g_signal_connect(G_OBJECT(lg->area), "expose-event", - (GCallback) _expose, lg); + (GCallback) _expose, lg); +#endif } +#if GTK_CHECK_VERSION(3, 0, 0) +#define _draw_line(D, CR, X1, Y1, X2, Y2) \ + cairo_move_to(CR, X1, Y1); \ + cairo_line_to(CR, X2, Y2); +#else +#define _draw_line(D, GC, X1, Y1, X2, Y2) gdk_draw_line(D, GC, X1, Y1, X2, Y2) +#endif + static void _draw_label_and_line(LoadGraph * lg, gint position, gint value) { gchar *tmp; /* draw lines */ - if (position > 0) - gdk_draw_line(GDK_DRAWABLE(lg->buf), lg->grid, 0, position, - lg->width, position); - else - position = -1 * position; + if (position > 0) { + _draw_line(GDK_DRAWABLE(lg->buf), lg->grid, 0, position, + lg->width, position); + } else + position = -1 * position; /* draw label */ tmp = - g_strdup_printf("<span size=\"x-small\">%d%s</span>", value, - lg->suffix); + g_strdup_printf("<span size=\"x-small\">%d%s</span>", value, + lg->suffix); pango_layout_set_markup(lg->layout, tmp, -1); #if GTK_CHECK_VERSION(3, 0, 0) pango_layout_set_width(lg->layout, - lg->width * PANGO_SCALE); - gtk_widget_create_pango_layout(GDK_WINDOW(lg->buf), NULL); + lg->width * PANGO_SCALE); + gtk_widget_create_pango_layout(lg->area, NULL); #else pango_layout_set_width(lg->layout, - lg->area->allocation.width * PANGO_SCALE); + lg->area->allocation.width * PANGO_SCALE); gdk_draw_layout(GDK_DRAWABLE(lg->buf), lg->trace, 2, position, - lg->layout); + lg->layout); #endif g_free(tmp); @@ -229,7 +251,7 @@ static void _draw_label_and_line(LoadGraph * lg, gint position, gint value) static void _draw(LoadGraph * lg) { #if GTK_CHECK_VERSION(3, 0, 0) - cairo_t *draw = GDK_WINDOW(lg->buf); + void *draw = NULL; /* not used by cairo */ #else GdkDrawable *draw = GDK_DRAWABLE(lg->buf); #endif @@ -237,10 +259,11 @@ static void _draw(LoadGraph * lg) /* clears the drawing area */ #if GTK_CHECK_VERSION(3, 0, 0) - cairo_rectangle(draw, 0, 0, lg->width, lg->height); + cairo_rectangle(lg->fill, 0, 0, lg->width, lg->height); + cairo_fill (lg->fill); #else gdk_draw_rectangle(draw, lg->area->style->black_gc, - TRUE, 0, 0, lg->width, lg->height); + TRUE, 0, 0, lg->width, lg->height); #endif @@ -248,23 +271,28 @@ static void _draw(LoadGraph * lg) GdkPoint *points = g_new0(GdkPoint, lg->size + 1); for (i = 0; i < lg->size; i++) { - points[i].x = i * 4; - points[i].y = lg->height - lg->data[i] * lg->scale; + points[i].x = i * 4; + points[i].y = lg->height - lg->data[i] * lg->scale; } points[0].x = points[1].x = 0; points[0].y = points[i].y = lg->height; points[i].x = points[i - 1].x = lg->width; +#if GTK_CHECK_VERSION(3, 0, 0) + /* TODO:GTK3 draw using loop and _draw_line() */ +#else gdk_draw_polygon(draw, lg->fill, TRUE, points, lg->size + 1); gdk_draw_polygon(draw, lg->trace, FALSE, points, lg->size + 1); +#endif g_free(points); /* vertical bars */ for (i = lg->width, d = 0; i > 1; i--, d++) - if ((d % 45) == 0 && d) - gdk_draw_line(draw, lg->grid, i, 0, i, lg->height); + if ((d % 45) == 0 && d) { + _draw_line(draw, lg->grid, i, 0, i, lg->height); + } /* horizontal bars and labels; 25%, 50% and 75% */ _draw_label_and_line(lg, -1, lg->max_value); @@ -272,25 +300,25 @@ static void _draw(LoadGraph * lg) _draw_label_and_line(lg, lg->height / 2, lg->max_value / 2); _draw_label_and_line(lg, 3 * (lg->height / 4), lg->max_value / 4); -#if 0 /* old-style drawing */ +#if 0 /* old-style drawing */ for (i = 0; i < lg->size; i++) { - gint this = lg->height - lg->data[i] * lg->scale; - gint next = lg->height - lg->data[i + 1] * lg->scale; - gint i4 = i * 4; + gint this = lg->height - lg->data[i] * lg->scale; + gint next = lg->height - lg->data[i + 1] * lg->scale; + gint i4 = i * 4; - gdk_draw_line(draw, lg->fill, i4, this, i4, lg->height); - gdk_draw_line(draw, lg->fill, i4 + 2, this, i4 + 2, lg->height); + _draw_line(draw, lg->fill, i4, this, i4, lg->height); + _draw_line(draw, lg->fill, i4 + 2, this, i4 + 2, lg->height); } for (i = 0; i < lg->size; i++) { - gint this = lg->height - lg->data[i] * lg->scale; - gint next = lg->height - lg->data[i + 1] * lg->scale; - gint i4 = i * 4; - - gdk_draw_line(draw, lg->trace, i4, this, i4 + 2, - (this + next) / 2); - gdk_draw_line(draw, lg->trace, i4 + 2, (this + next) / 2, - i4 + 4, next); + gint this = lg->height - lg->data[i] * lg->scale; + gint next = lg->height - lg->data[i + 1] * lg->scale; + gint i4 = i * 4; + + _draw_line(draw, lg->trace, i4, this, i4 + 2, + (this + next) / 2); + _draw_line(draw, lg->trace, i4 + 2, (this + next) / 2, + i4 + 4, next); } #endif @@ -302,11 +330,11 @@ void load_graph_update(LoadGraph * lg, gint value) gint i; if (value < 0) - return; + return; /* shift-right our data */ for (i = 0; i < lg->size - 1; i++) { - lg->data[i] = lg->data[i + 1]; + lg->data[i] = lg->data[i + 1]; } /* insert the updated value */ @@ -314,20 +342,20 @@ void load_graph_update(LoadGraph * lg, gint value) /* calculates the maximum value */ if (lg->remax_count++ > 20) { - /* only finds the maximum amongst the data every 20 times */ - lg->remax_count = 0; + /* only finds the maximum amongst the data every 20 times */ + lg->remax_count = 0; - gint max = lg->data[0]; - for (i = 1; i < lg->size; i++) { - if (lg->data[i] > max) - max = lg->data[i]; - } + gint max = lg->data[0]; + for (i = 1; i < lg->size; i++) { + if (lg->data[i] > max) + max = lg->data[i]; + } - lg->max_value = max; + lg->max_value = max; } else { - /* otherwise, select the maximum between the current maximum - and the supplied value */ - lg->max_value = MAX(value, lg->max_value); + /* otherwise, select the maximum between the current maximum + and the supplied value */ + lg->max_value = MAX(value, lg->max_value); } /* recalculates the scale; always use 90% of it */ @@ -346,16 +374,16 @@ gboolean lg_update(gpointer d) static int j = 1; if (i > 150) { - j = -1; + j = -1; } else if (i < 0) { - j = 1; + j = 1; } i += j; if (rand() % 10 > 8) - i *= 2; + i *= 2; if (rand() % 10 < 2) - i /= 2; + i /= 2; load_graph_update(lg, i + rand() % 50); return TRUE; @@ -363,8 +391,6 @@ gboolean lg_update(gpointer d) int main(int argc, char **argv) { - - LoadGraph *lg; GtkWidget *window; |