Re: GTK+2: gdk_draw_arc() -- changing arc colour



Chris Martin wrote:

In the example shown in GTK API/Miscellaneous/GtkDrawingArea it seems
impossible to change the colour the arc is drawn in.

Has anybody a solution to this?

Two examples of code tried -- the first is the "simple" method, the
second a more devious one: seeing that gdk_gc_new() called
gdk_gc_new_with_values() it seemed possible that supplying it with
values might work...

It will not work, cause you are not specifying the pixel's value. gdk_color_parse will fill the *red*,*green*,*blue* members of the GdkColor structure, not the
*pixel* field. You should call *gdk_colormap_alloc_color()* for this (and
*gdk_colormap_free_colors()* for freeing the color).


Example 1:

gboolean
expose_event_callback(GtkWidget * widget, GdkEventExpose * event,
                      gpointer data)
{
 GdkGC *gc;
 GdkColor colour;

 memset(&colour, 0, sizeof(colour));
 if (gdk_color_parse(colour_str, &colour)) {
   g_print("gdk_color_parse(%s) succeeds.\n", colour_str);
   g_print("colour: pixel %lu, red 0x%04x, green 0x%04x, blue 0x%04x.\n",
            colour.pixel, colour.red, colour.green, colour.blue);
   g_print("colour: pixel %lu, red %u, green %u, blue %u.\n",
            colour.pixel, colour.red>>8, colour.green>>8, colour.blue>>8);
 } else
   g_print("gdk_color_parse(%s) fails.\n", colour_str);

 gc = gdk_gc_new(widget->window);

gdk_colormap_color_alloc(gtk_widget_get_colormap(widget), &colour, FALSE,FALSE);


 gdk_gc_set_foreground(gc, &colour);

 gdk_draw_arc(widget->window, gc, TRUE,
               0, 0, widget->allocation.width, widget->allocation.height,
               0, 64 * 360);
 return TRUE;
}

Example 2:

gboolean
expose_event_callback(GtkWidget * widget, GdkEventExpose * event,
                      gpointer data)
{
 GdkGC *gc;
 GdkGCValues values;
 GdkColor colour;

 gdk_gc_get_values(widget->style->fg_gc[GTK_WIDGET_STATE(widget)], &values);

 memset(&colour, 0, sizeof(colour));
 if (gdk_color_parse(colour_str, &colour)) {
   g_print("gdk_color_parse(%s) succeeds.\n", colour_str);
   g_print("colour: pixel %lu, red 0x%04x, green 0x%04x, blue 0x%04x.\n",
            colour.pixel, colour.red, colour.green, colour.blue);
   g_print("colour: pixel %lu, red %u, green %u, blue %u.\n",
            colour.pixel, colour.red>>8, colour.green>>8, colour.blue>>8);
 } else
   fprintf(stderr, "gdk_color_parse(%s) fails.\n", colour_str);

gdk_colormap_color_alloc(gtk_widget_get_colormap(widget), &colour, FALSE,FALSE);


 values.foreground = colour;

 gc = gdk_gc_new_with_values(widget->window, &values, GDK_GC_FOREGROUND);

 gdk_draw_arc(widget->window, gc, TRUE,
               0, 0, widget->allocation.width, widget->allocation.height,
               0, 64 * 360);

 return TRUE;
}

In both cases the filled arc is drawn in black -- the output shows
that the colour was parsed correctly and the GdkColor was set
correctly:

Example 1:

$ ./try-colour1
gdk_color_parse(LightGrey) succeeds.
colour: pixel 0, red 0xd3d3, green 0xd3d3, blue 0xd3d3.
colour: pixel 0, red 211, green 211, blue 211.

Pixel is zero - you'll probably see the black color in the screen (in TrueColor mode at least)



Example 2:
$ ./try-colour2
gdk_color_parse(LightGrey) succeeds.
colour: pixel 0, red 0xd3d3, green 0xd3d3, blue 0xd3d3.
colour: pixel 0, red 211, green 211, blue 211.

The same situation

Olexiy





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]