Re: Not understanding the principles of drawing areas and primitives etc... Colors?



you can follow this basic algorithm, (there are many ways to effect this, this is just one that works and is, hopefully, descriptive):

// set up colors and some GCs
GdkColor red, black;
GdkGC *gc, *inverse_gc;

gc = gdk_gc_new(window); // regular GC for the window
inverse_gc = gdk_gc_new(window); // inverse GC

// init the various aspects/qualities of the GCs
red.red = 65535; red.green = 0; red.blue = 0; // red
black.red = 0; black.green = 0; black.blue = 0; // black
gdk_gc_set_rgb_fg_color(gc, &red);
gdk_gc_set_rgb_bg_color(gc, &black);
gdk_gc_set_rgb_fg_color(inverse_gc, &black);
gdk_gc_set_rgb_bg_color(inverse_gc, &red);
// plus any other qualities of the line you would like to set, like dashes, thickness, etc.
// see gdk_gc_set_function(gc, FUNCTION) in the documentation

// then in your configure routine for your drawing area:

// make the pixmap you will be drawing on
GdkPixmap *pixmap = gdk_pixmap_new(da->window, da->allocation.width, da->allocation.height, -1);

// paint the background using the background color (foreground color of our inverse GC)
gdk_draw_rectangle(pixmap, inverse_gc, TRUE, 0, 0, da->allocation.width, da->allocation.height);

// draw a line from corner to corner, twice
gdk_draw_line(pixmap, gc, 0, 0, da->allocation.width, da->allocation.height);
gdk_draw_line(pixmap, gc, 0, da->allocation.height, da->allocation.width, 0);

// force the expose event for the drawing area to be called
gtk_widget_queue_draw_area(da, 0, 0, da->allocation.width, da->allocation.height);

// render the pixmap to the screen in your expose routine for the drawing area:
gdk_draw_drawable(widget->window,
widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
pixmap, 
event->area.x, event->area.y
event->area.x, event->area.y
event->area.width, event->area.height);


richard


On Mar 11, 2006, at 5:29 PM, Leo - wrote:

I think I can grasp the GC idea; it's just a way to reduce the number of parameters sent to certain functions, but where do I "get" the colors from?
When I check up on gdk_gc_set_foreground etc. they require a gdkColor parameter, and following that link (clicking gdkColor) gets me to a page
about colormaps and, my impression is that I need to set up a "palette", link it to the widget, add my colors etc. etc., before I can actually draw on my widget.

Is that really the case?

Whether it is or not, it would be nice if somebody could give me a (c language) source snippet on how to set up colors and colormaps for a widget
and then draw a red line on it...

2006/3/9, Paul Davis <paul linuxaudiosystems com >:
On Thu, 2006-03-09 at 19:39 +0100, Leo - wrote:
> I want to be able to draw lines on a gtkDrawingArea, with a color the
> user selects (r,g,b),
> however I don't understand how I would do.
> The whole business with gdkGC's and gdcColors seems very alien. If
> somebody could
> point me to a tutorial (the scribble one isn't enough since that one
> doesn't use color).

no tutorial here, but a brief lecture.

there are many different possible parameters for many drawing
primitives. in your case of line drawing, we have things like:

        * is the line dashed?
            * if dashed, what are the stroke & gap sizes?
        * the pen width
        * the color of the line

for other drawing primitives, there are many more.

rather than constantly pass all these parameters into every drawing
function, most drawing kits based on the X Window System instead using a
Graphics Context (or GC) which contains a set of values that can be used
by any drawing primitive. This is more efficient in other ways too, but
you probably don't need to worry about them. You generally work with a
fixed number of

> What I simply want to do is something like line(gtkDrawingArea* da,
> int x1, int y1, int x2, int y2, r, g, b)

just grab a suitable GC, such as

       Glib::RefPtr<GC> gc = get_style()->get_fg_gc();

change its foreground color, specifically for whatever state
the drawing area is in right now (widgets in GTK can be in one of
several possible states):

       gc.set_foreground_gdk (redColor, get_state());

and draw on the window:

       get_window()->draw_line (gc, x1, y1, x2, y2);

the precise details might be slightly off, i am writing from memory.

--p



_______________________________________________
gtk-list mailing list



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