Re: Why "GtkWidget->window" is "NULL" when "gtk_window_new()"



On Tue, 2008-08-12 at 18:24 +0800, Wu Yin wrote:
> Thanks very much. But I have Another question:
> I want to draw line on my own initiative, don't use "expose-event" and callback function.

why not? that's how you do what you want to do.

> Like the following code, but the code can't do what I want.

because it's not how you do what you want to do.

> Please tell me how to do this?
> 
> 
> Code:
> +-------------------------------------------------------------------------------------
> |	int main(int argc, char *argv[])
> |	{
> |		GtkWidget *w;
> |
> |		gtk_init(&argc, &argv);
> |
> |		w = gtk_window_new(GTK_WINDOW_TOPLEVEL);
> |		g_signal_connect(GTK_OBJECT(w), "destroy", G_CALLBACK(gtk_main_quit), NULL);

don't cast to GTK_OBJECT: it's a useless type check, and
g_signal_connect() takes a gpointer as the first argument.

> |		gtk_widget_realize(w);
> |		gdk_draw_line(w->window, w->style->fg_gc[GTK_WIDGET_STATE(w)],
> |				0, 0, 120, 120);

don't use gdk_draw_* API: it's long deprecated (gtk+ 2.8 deprecated that
family of functions).

connect to the ::expose-event signal, get the cairo context from the
GdkWindow and use the Cairo API to draw:

  static gboolean
  my_expose_event (GtkWidget *widget, GdkEvent *event, gpointer data)
  {
    cairo_t *cr = gdk_cairo_create (GDK_DRAWABLE (widget->window));
    GdkColor color;

    color = w->style->fg[GTK_WIDGET_STATE (widget)]

    cairo_move_to (cr, 0, 0);
    cairo_line_to (cr, 120, 120);
    gdk_cairo_set_source_color (cr, &color);

    cairo_stroke (cr);

    cairo_destroy (cr);

    return FALSE;
  }

[WARNING: untested code, written from memory - use the Cairo API
reference to check the functions]

ciao,
 Emmanuele.

-- 
Emmanuele Bassi,
W: http://www.emmanuelebassi.net
B: http://log.emmanuelebassi.net



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