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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327 | {
$Id: toolbar.pp,v 1.2 2002/05/31 11:54:33 marco Exp $
Converted from C to Pascal by Artur Bac
<arturbac@poczta.onet.pl> Reda Poland
}
{$MODE objfpc}
{$H+}
{$S+}{$HINTS ON}
{$ifdef win32}
{$define extdecl := stdcall;}
{$APPTYPE GUI}
{$endif}{$ifdef Unix}
{$define extdecl := cdecl;}
{$endif}
Program tool_bar;uses glib,gdk,gtk;
Const
//* XPM */
gtk_xpm : array[0..44]of pgchar = (
'32 39 5 1','. c none',
'+ c black',
'@ c #3070E0',
'# c #F05050',
'$ c #35E035','................+...............',
'..............+++++.............',
'............+++++@@++...........',
'..........+++++@@@@@@++.........',
'........++++@@@@@@@@@@++........','......++++@@++++++++@@@++.......',
'.....+++@@@+++++++++++@@@++.....',
'...+++@@@@+++@@@@@@++++@@@@+....',
'..+++@@@@+++@@@@@@@@+++@@@@@++..',
'.++@@@@@@+++@@@@@@@@@@@@@@@@@@++','.+#+@@@@@@++@@@@+++@@@@@@@@@@@@+',
'.+##++@@@@+++@@@+++++@@@@@@@@$@.',
'.+###++@@@@+++@@@+++@@@@@++$$$@.',
'.+####+++@@@+++++++@@@@@+@$$$$@.',
'.+#####+++@@@@+++@@@@++@$$$$$$+.','.+######++++@@@@@@@++@$$$$$$$$+.',
'.+#######+##+@@@@+++$$$$$$@@$$+.',
'.+###+++##+##+@@++@$$$$$$++$$$+.',
'.+###++++##+##+@@$$$$$$$@+@$$@+.',
'.+###++++++#+++@$$@+@$$@++$$$@+.','.+####+++++++#++$$@+@$$++$$$$+..',
'.++####++++++#++$$@+@$++@$$$$+..',
'.+#####+++++##++$$++@+++$$$$$+..',
'.++####+++##+#++$$+++++@$$$$$+..',
'.++####+++####++$$++++++@$$$@+..','.+#####++#####++$$+++@++++@$@+..',
'.+#####++#####++$$++@$$@+++$@@..',
'.++####++#####++$$++$$$$$+@$@++.',
'.++####++#####++$$++$$$$$$$$+++.',
'.+++####+#####++$$++$$$$$$$@+++.','..+++#########+@$$+@$$$$$$+++...',
'...+++########+@$$$$$$$$@+++....',
'.....+++######+@$$$$$$$+++......',
'......+++#####+@$$$$$@++........',
'.......+++####+@$$$$+++.........','.........++###+$$$@++...........',
'..........++##+$@+++............',
'...........+++++++..............',
'.............++++...............');
{ This function is connected to the Close button or closing the window from the WM }
function BOOL_TO_GINT(data : boolean) : gint;
Begin
if data then
BOOL_TO_GINT:=1 else
BOOL_TO_GINT:=0;
end;
function delete_event (widget : PGtkWidget ;
event : PGdkEvent; data : gpointer) : boolean ; cdecl;
Begin
gtk_main_quit ();
delete_event :=FALSE;
end;
{The above beginning seems for sure familiar to you if it's not your first GTK program.
There is one additional thing though,
we include a nice XPM picture to serve as an icon for all of the buttons.
}Var
close_button : PGtkWidget; { This button will emit signal to close application }
tooltips_button : PGtkWidget; { to enable/disable tooltips }
text_button : PGtkWidget;
icon_button : PGtkWidget; both_button : PGtkWidget; { radio buttons for toolbar style }
entry : PGtkWidget; { a text entry to show packing any widget into toolbar }
//In fact not all of the above widgets are needed here,
//but to make things clearer I put them all together.
{ that's easy... when one of the buttons is toggled, we just
* check which one is active and set the style of the toolbar
* accordingly
* ATTENTION: our toolbar is passed as data to callback ! }Procedure radio_event (widget : PGTkWidget; data : gpointer); cdecl;
Begin
if (gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON (text_button))) then
gtk_toolbar_set_style(GTK_TOOLBAR ( data ), GTK_TOOLBAR_TEXT) else begin
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON (icon_button))) then
gtk_toolbar_set_style(GTK_TOOLBAR ( data ), GTK_TOOLBAR_ICONS);
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON (both_button))) then
gtk_toolbar_set_style(GTK_TOOLBAR ( data ), GTK_TOOLBAR_BOTH); end;
End;
{ even easier, just check given toggle button and enable/disable
* tooltips }Procedure toggle_event (widget : PGtkWidget; data : gpointer); cdecl;
Begin
{Due to gtk specification
void gtk_toolbar_set_tooltips (GtkToolbar *toolbar, gboolean enable);
In Pasal unit this functioni is implemented as
procedure gtk_toolbar_set_tooltips (toolbar:PGtkToolbar;
enable:gint);
cdecl;external gtkdll name 'gtk_toolbar_set_tooltips';so we have to change boolean to gint with function BOOL_TO_GINT implemented
on the top of source
}
gtk_toolbar_set_tooltips (GTK_TOOLBAR ( data ),
BOOL_TO_GINT( gtk_toggle_button_get_active(
GTK_TOGGLE_BUTTON (widget))));
End;
{The above are just two callback functions that will be calledwhen one of the buttons on a toolbar is pressed.
You should already be familiar with things like this if you've already used toggle buttons
and radio buttons.
}
Var { Here is our main window (a dialog) and a handle for the handlebox }
dialog : PGtkWidget;
handlebox : PGtkWidget;
{ Ok, we need a toolbar, an icon with a mask (one for all of the buttons) and an icon widget to put this icon in (but
we'll create a separate widget for each button) }
toolbar : PGtkWidget;
icon : PGdkPixmap;
mask : PGdkBitmap; iconw : PGtkWidget;
style : PGtkStyle;
Begin
{ this is called in all GTK application. }
gtk_set_locale (); //It important for apps taht use local language specific characters gtk_init (@argc, @argv);
gtk_rc_init;
{ create a new window with a given title, and nice size }
dialog := gtk_dialog_new (); gtk_window_set_title ( GTK_WINDOW ( dialog ) , 'GTKToolbar Tutorial');
gtk_widget_set_usize( GTK_WIDGET ( dialog ) , 600 , 300 );
set_allow_shrink(PGtkWindow(dialog)^,BM_ALLOW_SHRINK);
{ typically we quit if someone tries to close us } gtk_signal_connect ( GTK_OBJECT ( dialog ), 'delete_event',
GTK_SIGNAL_FUNC ( @delete_event ), NULL);
{ we need to realize the window because we use pixmaps for
* items on the toolbar in the context of it } gtk_widget_realize ( dialog );
{ to make it nice we'll put the toolbar into the handle box,
* so that it can be detached from the main window }
handlebox := gtk_handle_box_new (); gtk_box_pack_start ( GTK_BOX ( GTK_DIALOG(dialog)^.vbox ),
handlebox, FALSE, FALSE, 5 );
{The above should be similar to any other GTK application.
Just initialization of GTK, creating the window, etc.There is only one thing that probably needs some explanation:
a handle box. A handle box is just another box that can be used to pack widgets in to.
The difference between it and typical boxes is that it can be detached from
a parent window (or, in fact, the handle box remains in the parent,
but it is reduced to a very small rectangle, while all of its contents are reparented to a new freely floating window). It is usually nice
to have a detachable toolbar, so these two widgets occur together quite often.
toolbar will be horizontal, with both icons and text, and
* with 5pxl spaces between items and finally,
* we'll also put it into our handlebox } toolbar := gtk_toolbar_new ( GTK_ORIENTATION_HORIZONTAL,
GTK_TOOLBAR_BOTH );
gtk_container_set_border_width ( GTK_CONTAINER ( toolbar ) , 5 );
gtk_toolbar_set_space_size ( GTK_TOOLBAR ( toolbar ), 5 );
gtk_container_add ( GTK_CONTAINER ( handlebox ) , toolbar );
{ now we create icon with mask: we'll reuse it to create
* icon widgets for toolbar items }
style := gtk_widget_get_style( dialog );
icon := gdk_pixmap_create_from_xpm_d ( dialog^.window, @mask, @style^.fg_gc, gtk_xpm );
{Well, what we do above is just a straightforward initialization of the toolbar widget
and creation of a GDK pixmap with its mask.
If you want to know something more about using pixmaps, refer to GDK documentation or to the Pixmaps section earlier in this tutorial.}
{ our first item is <close> button }
iconw := gtk_pixmap_new ( icon, mask ); { icon widget }
close_button := gtk_toolbar_append_item ( GTK_TOOLBAR (toolbar), { our toolbar }
'Close', { button label }
'Closes this app', { this button's tooltip }
'Private', { tooltip private info }
iconw, { icon widget } GTK_SIGNAL_FUNC (@delete_event), { a signal }
NULL );
gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) ); { space after item }
{In the above code you see the simplest case: adding a button to toolbar. Just before appending a new item, we have to construct a pixmap widget
to serve as an icon for this item; this step will have to be repeated for each new item.
Just after the item we also add a space, so the following items will not touch each other.
As you see gtk_toolbar_append_item returns a pointer to our newly created button widget,
so that we can work with it in the normal way.}
{ now, let's make our radio buttons group... }
iconw := gtk_pixmap_new ( icon, mask );
icon_button := gtk_toolbar_append_element(
GTK_TOOLBAR(toolbar), GTK_TOOLBAR_CHILD_RADIOBUTTON, { a type of element }
NULL, { pointer to widget }
'Icon', { label }
'Only icons in toolbar', { tooltip }
'Private', { tooltip private string } iconw, { icon }
GTK_SIGNAL_FUNC (@radio_event), { signal }
toolbar); { data for signal }
gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) );
{Here we begin creating a radio buttons group.
To do this we use gtk_toolbar_append_element.
In fact, using this function one can also +add simple items or even spaces
(type := GTK_TOOLBAR_CHILD_SPACE or +GTK_TOOLBAR_CHILD_BUTTON).
In the above case we start creating a radio group.In creating other radio buttons for this group a pointer to the previous button in the group
is required, so that a list of buttons can be easily constructed
(see the section on Radio Buttons earlier in this tutorial).
following radio buttons refer to previous ones } iconw := gtk_pixmap_new ( icon, mask );
text_button :=
gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
GTK_TOOLBAR_CHILD_RADIOBUTTON,
icon_button, 'Text',
'Only texts in toolbar',
'Private',
iconw,
GTK_SIGNAL_FUNC (@radio_event), toolbar);
gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) );
iconw := gtk_pixmap_new ( icon, mask );
both_button := gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
GTK_TOOLBAR_CHILD_RADIOBUTTON,
text_button,
'Both',
'Icons and text in toolbar', 'Private',
iconw,
GTK_SIGNAL_FUNC (@radio_event),
toolbar);
gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) ); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(both_button),TRUE);
{In the end we have to set the state of one of the buttons manually
(otherwise they all stay in active state, preventing us from switching between them).}
{ here we have just a simple toggle button }
iconw := gtk_pixmap_new ( icon, mask );
tooltips_button :=
gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
GTK_TOOLBAR_CHILD_TOGGLEBUTTON, NULL,
'Tooltips',
'Toolbar with or without tips',
'Private',
iconw, GTK_SIGNAL_FUNC (@toggle_event),
toolbar);
gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) );
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(tooltips_button),TRUE);
{A toggle button can be created in the obvious way
(if one knows how to create radio buttons already).}
{ to pack a widget into toolbar, we only have to
create it and append it with an appropriate tooltip } entry := gtk_entry_new ();
gtk_toolbar_append_widget( GTK_TOOLBAR (toolbar),
entry,
'This is just an entry',
'Private' );
{ well, it isn't created within thetoolbar, so we must still show it }
gtk_widget_show ( entry );
{As you see, adding any kind of widget to a toolbar is simple.The one thing you have to remember is that this widget must be shown manually
(contrary to other items which will be shown together with the toolbar).}
{ that's it ! let's show everything. }
gtk_widget_show ( toolbar ); gtk_widget_show (handlebox);
gtk_widget_show ( dialog );
{ rest in gtk_main and wait for the fun to begin! }
gtk_main ();
End. |