Re: [gtk-list] Colors, key_presses, and PNGs



katre wrote:
> 
> Okay, I got a lot of coding done this weekend, and ran into a few problems I
> can't make heads or tails of.  So it's time for a lot of dumb questions to be
> sent to the list. :)
> 
> Background: I'm writing a frontend for an interpreter.  The front end needs to
> open a window, write text into that window at any point, in any of 9 fonts and
> in any of 12 colors.  I need to be able to erase parts of the screen to the
> background color, display a PNG image, and accept input.  After looking at
> what's out there (GtkText, GtkConsole), and realizing none did what I want,
> I've gone on to write my own widget, which will do some things, and not do
> others.  My problems are :
> 
> 1) Colors - Here's what I've been doing.
> GdkColor *fg, *bg;
> GdkGC *gc;
> GdkPixmap *pm;
> 
> fg = malloc(sizeof(GdkColor));
> bg = malloc(sizeof(GdkColor));
> 
> gdk_color_parse("red", &fg);
> gdk_color_parse("blue", &bg);
> 
> gc = gdk_gc_new (widget->window);
> gdk_gc_set_foreground (gc, fg);
> gdk_gc_set_background (gc, bg);
> 
> gdk_draw_text (pm, font, gc, 0, 0, "Hello world!", strlen("Hello world"));
> 
> gdk_draw_pixmap (widget->window, gc, pm, 0, 0, 0, 0, width, height);
> 
> This appears to be what is done in the scribble example, but instead of
> drawing in the colors I want, this will give me an entirely black window.  Can
> anyone tell what I'm doing wrong?

Your gdk_color_parse() calls are incorrect. They should be:

  gdk_color_parse("red", fg);

since your fg is already a GdkColor*.


You haven't allocated the colors. gdk_color_parse() only gets the red, green
and blue values corresponding to the given color string - it doesn't actually
allocate the color. Use something like:

      GdkColormap *colormap;

      colormap = gtk_widget_get_colormap (widget);
      if (!gdk_color_alloc (colormap, fg))
	g_warning ("Couldn't allocate foreground color");
      if (!gdk_color_alloc (colormap, bg))
	g_warning ("Couldn't allocate background color");


Also, the y coord passed to gdk_draw_text() is for the baseline of the
text, so if you use 0 you may not see the string.
 
(And you probably don't need to malloc the GdkColors if you don't need them
anywhere else. Just use 'GdkColor fg, bg' - then use &fg to pass to functions.)


> 2) key_press - I have given my widget a widget_class->key_press entry, and set
> the event mask in widget_realize to be gtk_get_events() | GDK_EXPOSURE_MASK |
> GDK_KEY_PRESS_MASK.  What else do I need to do to receive key presses?

You also need to tell the widget to accept the keyboard focus, by setting the
flag:
   GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);

The widget should also show that it has the focus somehow, so you know where
you are when you tab through the widgets.

If you want the keys to be usable anywhere in the window, it may be better
to add the handler to the window's key_press event. Then you don't have to worry
about the keyboard focus.

 
> 3) PNG - The png files that this interpreter will use are packaged in a
> resource file with the interpreted program and some other data.  I can read in
> the PNG data, but the only way I've found to draw it is to write it to a
> temporary file, then let gdk_imlib open the file.  Can I get gdk_imlib to work
> with a chunk of data in memory?  Or is this not possible?

>From gdk_imlib.h this may be useful:

  GdkImlibImage      *gdk_imlib_inlined_png_to_image(unsigned char *data, 
                                                     int data_size);


Damon



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