1 <!doctype linuxdoc system>
5 <!-- Title information -->
7 <title>The GTK+ Drawing Kit Programming Manual
8 <author>Shawn T. Amundson, Peter Mattis
12 This document aims at teaching user how to effectively program in
13 GDK, the GTK+ Drawing Kit, and to serve as a reference guide to
14 more experienced GTK+ programmers. It is a work in progress.
16 <!-- Table of contents -->
19 <!-- Begin the document -->
21 <!-- ***************************************************************** -->
25 GDK is designed as a wrapper library that lies on top of Xlib. It
26 performs many common and desired operations for a programmer instead
27 of the programmer having to explicitly ask for such functionality from
28 Xlib directly. For example, GDK provides a common interface to both
29 regular and shared memory XImage types. By doing so, an application
30 can nearly transparently use the fastest image type available. GDK
31 also provides routines for determining the best available color depth
32 and the best available visual which is not always the default visual
35 GDK is distributed and developed with GTK+, and is licensed under the
36 GNU Library General Public Licence (LGPL).
42 Initialization of GDK is easy. Simply call gdk_init() passing
43 in the argc and argv parameters.
46 int main (int argc, char *argv[])
49 gdk_init (&argc, &argv);
51 /* Cleanup of GDK is done automatically when the program exits. */
56 Generally, GDK initialization is done by gtk_init() in GTK+. This means
57 that when using GTK+, you do not need to directly call gdk_init().
59 <sect1>An Example using GDK with GTK+
61 This example demonstrates drawing a line using the foreground
62 color of the GtkDrawArea widget it is drawn inside. The example
63 will end when you click inside the window, which is filled by the
64 GtkDrawingArea widget.
66 The line is drawn during the expose event so that when the window
67 drawing is done whenever it is needed.
72 /* The expose callback does the drawing of the line */
74 expose_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)
78 printf("expose...\n");
81 /* The GC is the Graphics Context. Here it is borrowed from the widget */
82 gc = widget->style->fg_gc[GTK_STATE_NORMAL];
84 gdk_draw_line (widget->window, /* GDK Window of GtkDrawingArea widget */
85 gc, /* Graphics Context */
89 200); /* y2, bottom */
93 void destroy (GtkWidget *widget, gpointer data)
98 int main (int argc, char *argv[])
104 /* This initializes both GTK+ and GDK */
105 gtk_init (&argc, &argv);
107 /* Create a window */
108 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
109 gtk_signal_connect (GTK_OBJECT (window), "destroy",
110 GTK_SIGNAL_FUNC (destroy), NULL);
112 /* Create a drawing area widget. This widget actually is just a
113 simple widget which provides us an GDK window to draw on and
114 takes care of all the toolkit integration, like providing the
115 ability to add it to the window with gtk_contianer_add() */
116 darea = gtk_drawing_area_new ();
117 gtk_container_add (GTK_CONTAINER (window), darea);
119 /* Set the width and height (arguments are in that order) */
120 gtk_drawing_area_size (GTK_DRAWING_AREA (darea), 200, 200);
122 /* Drawing in the expose event is important to keep the
123 draw line always on the GDK window */
124 gtk_signal_connect (GTK_OBJECT (darea), "expose_event",
125 GTK_SIGNAL_FUNC (expose_callback), NULL);
127 /* We get the events, then add in button press. If we did not
128 do this, we would not be notified of button press events in
129 the GtkDrawingArea widget */
130 events = gtk_widget_get_events (darea);
131 gtk_widget_set_events (darea, events | GDK_BUTTON_PRESS_MASK);
133 /* If we click on the darea, the application will exit */
134 gtk_signal_connect_object (GTK_OBJECT (darea), "button_press_event",
135 GTK_SIGNAL_FUNC (gtk_widget_destroy),
136 GTK_OBJECT (window));
138 gtk_widget_show (darea);
139 gtk_widget_show (window);
141 /* The GTK+ main idle loop */
144 /* Cleanup of GDK is done automatically when the program exits. */
149 <sect>The Graphics Context
151 The Graphics Context, or GC, defines how things should be drawn,
152 including color, font, fill, tile, stipple, clipping mask, line
153 width, line style, and join style.
157 Changing color is done by changing the forground or background color
160 <sect>Drawing Commands
162 <sect>Understanding and Using Visuals
163 <sect>Creating and Using New Windows
169 <sect>About this Document
172 This document was originially written by Peter Mattis and entitled
173 "The General Drawing Kit". It was meant as a reference guide.
175 This version of the document has been renamed and is meant as a general
180 Copyright (c) 1996 Peter Mattis
182 Copyright (c) 1998 Shawn T. Amundson
184 Permission is granted to make and distribute verbatim copies of this
185 manual provided the copyright notice and this permission notice are
186 preserved on all copies.
188 Permission is granted to copy and distribute modified versions of this
189 manual under the conditions for verbatim copying, provided that the
190 entire resulting derived work is distributed under the terms of a
191 permission notice identical to this one.
193 Permission is granted to copy and distribute translations of this manual
194 into another language, under the above conditions for modified versions,
195 except that this permission notice may be stated in a translation
196 approved by Peter Mattis.