Re: Drawing text into a drawing area with Pango



On Tue, 17 Feb 2004, Bryan Brown wrote:
Problem is, it's not clear to me what parts of the Pango context and
layout I need to initialize other than the text string.  Here's a snippet
of the code that I've tried so far:

=== begin code snippet ===

PangoContext *context;
PangoLayout  *layout;
gchar *text_buffer[] = "BLAH";
GdkEventButton *event;
GdkGC *gc;
gint x_start, y_start;

[snip]

   context = (PangoContext *) pango_context_new ();
   layout  = pango_layout_new (context);
   pango_layout_set_text (layout, text_buffer, -1);

[snip]

   x_start = event->x;
   y_start = event->y;

   gdk_draw_layout (drawing_area->window,
                    gc,
                    x_start,
                    y_start,
                    layout);

=== end code snippet ===

This causes an abend in the call to gdk_draw_layout when run, presumably
because I've not initialized some items in the Pango context and/or
layout.

What other items do I need to initialize?  I can't readily find some
"simple", sample code to illustrate the process, hence my appeal for help.

Hello Bryan,
at first you don't need to create a PangoContext yourself. You can create
the layout directly from your drawingarea widget:
PangoLayout *pLayout = gtk_widget_create_pango_layout(drawing_area, 0);

Then set the text and draw it like you did. After you printed your text
free the PangoLayout with g_object_unref(pLayout).
From your code snipped I guess you forgot to initialize the gc. You can
create your own graphics context with:
gc = gdk_gc_new(drawing_area->window);
and free it when you don't need it anymore with:
g_object_unref(gc);
Create the gc just before you need it to make sure that the widget is
already realized and drawing_area->window is valid.

The drawing area also contains a couple of graphics contexts inside its
GtkStyle. If you would like to use one of them instead of creating your
own try something like
gc = gtk_widget_get_style(drawing_area)->fg_gc[GTK_STATE_NORMAL]
and see if it fits your needs. In this case you don't have to free the
graphics context. But remember that you must not change any attributes
inside such a "borrowed" gc. GTK eventually shares graphics contexts
between different widgets and changing attributes of such a gc has weird
effects on your user interface.

Regards,
                   Peter
-- 
====================================================================
Peter Krüger

applied software solutions (appss) GmbH
Sandtorstr. 23
D-39106 Magdeburg
Germany

Phone:  +49-(0)391-54486-19388
Fax:    +49-(0)391-54486-19222
email:  krueger appss de
URL:    http://www.appss.de

Managing Director: Uwe Hess, Dietmar Schäfer
Register: HRB12386, AG Mageburg

"Virtual business becomes reality!"

This e-mail may contain confidential and/or privileged information. If
you are not the intended recipient (or have received this e-mail in
error) please notify the sender immediately and destroy this e-mail. Any
unauthorised copying, disclosure or distribution of the material in this
e-mail is strictly forbidden.
====================================================================




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