summaryrefslogtreecommitdiff
path: root/docs/gdk.sgml
blob: 0b660bbee79c33bced77ac89a0061eeede76bcb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!doctype linuxdoc system>

<article>

<!-- Title information -->

<title>The GTK+ Drawing Kit Programming Manual
<author>Shawn T. Amundson, Peter Mattis
<date>July 26, 1998

<abstract>
This document aims at teaching user how to effectively program in
GDK, the GTK+ Drawing Kit, and to serve as a reference guide to 
more experienced GTK+ programmers.  It is a work in progress.

<!-- Table of contents -->
<toc>

<!-- Begin the document -->

<!-- ***************************************************************** -->
<sect>Introduction

<p>
GDK is designed as a wrapper library that lies on top of Xlib. It
performs many common and desired operations for a programmer instead
of the programmer having to explicitly ask for such functionality from
Xlib directly. For example, GDK provides a common interface to both
regular and shared memory XImage types. By doing so, an application
can nearly transparently use the fastest image type available. GDK
also provides routines for determining the best available color depth
and the best available visual which is not always the default visual
for a screen.

GDK is distributed and developed with GTK+, and is licensed under the 
GNU Library General Public Licence (LGPL).

<sect>Getting Started

<sect1>Initialization
<p>
Initialization of GDK is easy.  Simply call gdk_init() passing 
in the argc and argv parameters.

<tscreen><verb>
int main (int argc, char *argv[])
{
    /* Initialize GDK. */
    gdk_init (&amp;argc, &amp;argv);

    /* Cleanup of GDK is done automatically when the program exits. */
    return 0;
}
</verb></tscreen>

Generally, GDK initialization is done by gtk_init() in GTK+.  This means 
that when using GTK+, you do not need to directly call gdk_init().

<sect1>An Example using GDK with GTK+ 
<p>
This example demonstrates drawing a line using the foreground
color of the GtkDrawArea widget it is drawn inside.  The example 
will end when you click inside the window, which is filled by the
GtkDrawingArea widget.

The line is drawn during the expose event so that when the window 
drawing is done whenever it is needed.

<tscreen><verb>
#include <gtk/gtk.h>

/* The expose callback does the drawing of the line */
int
expose_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
    GdkGC *gc;

    printf("expose...\n");


    /* The GC is the Graphics Context.  Here it is borrowed from the widget */
    gc = widget->style->fg_gc[GTK_STATE_NORMAL];

    gdk_draw_line (widget->window,  /* GDK Window of GtkDrawingArea widget */
                   gc,              /* Graphics Context */
                   0,               /* x1, left */
                   0,               /* y1, top */
                   200,             /* x2, right */
                   200);            /* y2, bottom */
}

/* This quits GTK+ */
void destroy (GtkWidget *widget, gpointer data)
{
    gtk_main_quit ();
}

int main (int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *darea;
    int events;

    /* This initializes both GTK+ and GDK */
    gtk_init (&amp;argc, &amp;argv);

    /* Create a window */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_signal_connect (GTK_OBJECT (window), "destroy",
                        GTK_SIGNAL_FUNC (destroy), NULL);

    /* Create a drawing area widget. This widget actually is just a 
       simple widget which provides us an GDK window to draw on and 
       takes care of all the toolkit integration, like providing the
       ability to add it to the window with gtk_contianer_add() */
    darea = gtk_drawing_area_new ();
    gtk_container_add (GTK_CONTAINER (window), darea);

    /* Set the width and height (arguments are in that order) */
    gtk_drawing_area_size (GTK_DRAWING_AREA (darea), 200, 200);

    /* Drawing in the expose event is important to keep the 
       draw line always on the GDK window */
    gtk_signal_connect (GTK_OBJECT (darea), "expose_event",
                        GTK_SIGNAL_FUNC (expose_callback), NULL);

    /* We get the events, then add in button press.  If we did not
       do this, we would not be notified of button press events in
       the GtkDrawingArea widget */
    events = gtk_widget_get_events (darea);
    gtk_widget_set_events (darea, events | GDK_BUTTON_PRESS_MASK);

    /* If we click on the darea, the application will exit */
    gtk_signal_connect_object (GTK_OBJECT (darea), "button_press_event",
                               GTK_SIGNAL_FUNC (gtk_widget_destroy),
                               GTK_OBJECT (window));

    gtk_widget_show (darea);
    gtk_widget_show (window);

    /* The GTK+ main idle loop */
    gtk_main();

    /* Cleanup of GDK is done automatically when the program exits. */
    return 0;
}
</verb></tscreen>

<sect>The Graphics Context 
<p>
The Graphics Context, or GC, defines how things should be drawn, 
including color, font, fill, tile, stipple, clipping mask, line 
width, line style, and join style.

<sect1>Color
<p>
Changing color is done by changing the forground or background color 
of the GC.

<sect>Drawing Commands
<sect>Event Handling
<sect>Understanding and Using Visuals
<sect>Creating and Using New Windows
<sect>Pixmaps
<sect>Images
<sect>Fonts
<sect>

<sect>About this Document
<sect1>History
<P>
This document was originially written by Peter Mattis and entitled
"The General Drawing Kit".  It was meant as a reference guide.

This version of the document has been renamed and is meant as a general
programming guide.

<sect1>Copying
<p>
Copyright (c) 1996 Peter Mattis
<p>
Copyright (c) 1998 Shawn T. Amundson

Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.

Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided that the
entire resulting derived work is distributed under the terms of a
permission notice identical to this one.

Permission is granted to copy and distribute translations of this manual
into another language, under the above conditions for modified versions,
except that this permission notice may be stated in a translation
approved by Peter Mattis.

</article>