Help requested with drawing lines in a child window.



Hi,
Would somebody please help out a new Gtk programmer. Here's the problem -
The code below is part of my first Gtk program. I've taken everything out
of it that isn't directly Gtk related or is unnecessary
to reproduce the problem. What's left is a program that displays a window
with two buttons, Plot and Exit. Clicking on Plot should produce a second
window with a white line on it, instead I get a blank window and this error
message -

Gdk-CRITICAL **: file gdkdraw.c: line 65 (gdk_draw_line): assertion
'drawable != NULL' failed.

I understand that I'm calling gtk_draw_line with a NULL first parameter, but
why is it NULL. I've followed a similar example from the book 'Beginning
GTK+/GNOME Programming' by Peter Wright.

Mike

-------------------------
-------------------------

#include <gtk/gtk.h>

void CloseTheApp(GtkWidget *window, gpointer data)
{
    gtk_main_quit();
}
void ExitPressed(GtkButton *button, gpointer data)
{
    gtk_main_quit();
}
void PlotPressed(GtkButton *button, gpointer data)
{
    GtkWidget *window;
    GtkWidget *drawingarea;
    GdkDrawable *drawable;

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
    gtk_window_set_title(GTK_WINDOW(window), "Output window");
    drawingarea = gtk_drawing_area_new();
    gtk_container_add(GTK_CONTAINER(window), drawingarea);
    drawable = drawingarea->window;
    gdk_draw_line(drawable, drawingarea->style->white_gc,
                    0, 0, 100, 100);
    gtk_widget_show_all(window);
}

GtkWidget *buildwindow()
{
    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *table;

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Species plotter");
    gtk_container_set_border_width(GTK_CONTAINER(window), 10);
    gtk_signal_connect(GTK_OBJECT(window),
                                "destroy",
                                GTK_SIGNAL_FUNC(CloseTheApp),
                                NULL);

/* Set up the basic layout with a table */
    table = gtk_table_new(20, 26, FALSE);
    gtk_container_add(GTK_CONTAINER(window), table);

/* Add the two main buttons */
    button = gtk_button_new_with_label("Plot graphs");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 10, 18, 14, 16);
    gtk_signal_connect(GTK_OBJECT(button),
                                "clicked",
                                GTK_SIGNAL_FUNC(PlotPressed),
                                NULL);
    button = gtk_button_new_with_label("Exit");
    gtk_table_attach_defaults(GTK_TABLE(table), button, 18, 26, 14, 16);
    gtk_signal_connect(GTK_OBJECT(button),
                            "clicked",
                            GTK_SIGNAL_FUNC(ExitPressed),
                            NULL);

    return window;
}

gint main(gint argc, gchar *argv[])
{
    GtkWidget *window;

    gtk_init(&argc, &argv);
    window = buildwindow();
    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}






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