]> Pileus Git - ~andy/gtk/blob - examples/rulers/rulers.c
- new section on Events - change all delete_event callbacks to include a
[~andy/gtk] / examples / rulers / rulers.c
1 /* This file extracted from the GTK tutorial. */
2
3 /* rulers.c */
4
5 #include <gtk/gtk.h>
6
7 #define EVENT_METHOD(i, x) GTK_WIDGET_CLASS(GTK_OBJECT(i)->klass)->x
8
9 #define XSIZE  600
10 #define YSIZE  400
11
12 /* this routine gets control when the close button is clicked
13  */
14 void close_application( GtkWidget *widget, GdkEvent *event, gpointer *data ) {
15     gtk_main_quit();
16 }
17
18
19 /* the main routine
20  */
21 int main( int argc, char *argv[] ) {
22     GtkWidget *window, *table, *area, *hrule, *vrule;
23
24     /* initialize gtk and create the main window */
25     gtk_init( &argc, &argv );
26
27     window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
28     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
29             GTK_SIGNAL_FUNC( close_application ), NULL);
30     gtk_container_border_width (GTK_CONTAINER (window), 10);
31
32     /* create a table for placing the ruler and the drawing area */
33     table = gtk_table_new( 3, 2, FALSE );
34     gtk_container_add( GTK_CONTAINER(window), table );
35
36     area = gtk_drawing_area_new();
37     gtk_drawing_area_size( (GtkDrawingArea *)area, XSIZE, YSIZE );
38     gtk_table_attach( GTK_TABLE(table), area, 1, 2, 1, 2,
39                       GTK_EXPAND|GTK_FILL, GTK_FILL, 0, 0 );
40     gtk_widget_set_events( area, GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK );
41
42     /* The horizontal ruler goes on top.  As the mouse moves across the drawing area,
43        a motion_notify_event is passed to the appropriate event handler for the ruler. */
44     hrule = gtk_hruler_new();
45     gtk_ruler_set_metric( GTK_RULER(hrule), GTK_PIXELS );
46     gtk_ruler_set_range( GTK_RULER(hrule), 7, 13, 0, 20 );
47     gtk_signal_connect_object( GTK_OBJECT(area), "motion_notify_event",
48                                (GtkSignalFunc)EVENT_METHOD(hrule, motion_notify_event),
49                                GTK_OBJECT(hrule) );
50     /*  GTK_WIDGET_CLASS(GTK_OBJECT(hrule)->klass)->motion_notify_event, */
51     gtk_table_attach( GTK_TABLE(table), hrule, 1, 2, 0, 1,
52                       GTK_EXPAND|GTK_SHRINK|GTK_FILL, GTK_FILL, 0, 0 );
53     
54     /* The vertical ruler goes on the left.  As the mouse moves across the drawing area,
55        a motion_notify_event is passed to the appropriate event handler for the ruler. */
56     vrule = gtk_vruler_new();
57     gtk_ruler_set_metric( GTK_RULER(vrule), GTK_PIXELS );
58     gtk_ruler_set_range( GTK_RULER(vrule), 0, YSIZE, 10, YSIZE );
59     gtk_signal_connect_object( GTK_OBJECT(area), "motion_notify_event",
60                                (GtkSignalFunc)
61                                   GTK_WIDGET_CLASS(GTK_OBJECT(vrule)->klass)->motion_notify_event,
62                                GTK_OBJECT(vrule) );
63     gtk_table_attach( GTK_TABLE(table), vrule, 0, 1, 1, 2,
64                       GTK_FILL, GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0 );
65
66     /* now show everything */
67     gtk_widget_show( area );
68     gtk_widget_show( hrule );
69     gtk_widget_show( vrule );
70     gtk_widget_show( table );
71     gtk_widget_show( window );
72     gtk_main();
73
74     return 0;
75 }