File: toolbar.pl

package info (click to toggle)
swi-prolog-packages 5.0.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 50,688 kB
  • ctags: 25,904
  • sloc: ansic: 195,096; perl: 91,425; cpp: 7,660; sh: 3,046; makefile: 2,750; yacc: 843; awk: 14; sed: 12
file content (379 lines) | stat: -rw-r--r-- 10,051 bytes parent folder | download
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*  $Id: toolbar.pl,v 1.9 2002/02/01 15:04:49 jan Exp $

    Part of XPCE --- The SWI-Prolog GUI toolkit

    Author:        Jan Wielemaker and Anjo Anjewierden
    E-mail:        jan@swi.psy.uva.nl
    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
    Copyright (C): 1985-2002, University of Amsterdam

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

:- module(pce_tool_button, []).
:- use_module(library(pce)).
:- use_module(library(help_message)).
:- require([ default/3
	   ]).

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
This library module defines  three   classes:  tool_bar, tool_button and
tool_status_button. It is intended for defining   rows  of buttons, also
called tool-bars. Each button represents an   action  on the `client' of
the tool_bar.

Below is a typical example:

resource(printer,	image,	image('16x16/print.xpm')).
resource(floppy,	image,	image('16x16/save.xpm')).

:- pce_begin_class(myapp, frame).

initialise(MyApp) :->
	send_super(MyApp, initialise, 'My application'),
	send(F, append, new(D, tool_dialog(MyApp))),
	send_list(TB, append,
		  [ tool_button(load,
				resource(floppy),
				load),
		    gap,		% skip a little
		    tool_button(print,
				resource(printer),
				print)
		  ]),
	...
				
print(MyApp) :->
	<Print the current document>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

:- pce_begin_class(tool_bar, dialog_group,
		   "Row of buttons").

variable(orientation, 	{horizontal,vertical},	get,  "Stacking direction").
variable(client,	object*,		both, "Receiving object").

initialise(BG, Client:[object]*, Orientation:[{horizontal,vertical}]) :->
	default(C, @nil, Client),
	default(Orientation, horizontal, O),
	send(BG, send_super, initialise, @default, group),
	send(BG, slot, client, C),
	send(BG, slot, orientation, O),
	send(BG, gap, size(0,0)).
	
append(BG, B:'tool_button|{gap}') :->
	"Append button or gap"::
	(   get(BG, orientation, horizontal)
	->  Where = right
	;   Where = next_row
	),
	(   B == gap
	->  What = graphical(0, 0, 5, 5)
	;   What = B
	),
	send(BG, send_super, append, What, Where).

activate(BG) :->
	"Update activation of member buttons"::
	send(BG?graphicals, for_some, message(@arg1, activate)).

reference(BG, Ref:point) :<-
	"Reference is at the baseline"::
	get(BG, height, H),
	new(Ref, point(0, H)).

compute(BG) :->
	"Make all buttons the same size"::
	(   get(BG, request_compute, @nil)
	->  true
	;   new(S, size(0,0)),
	    send(BG?graphicals, for_all,
		 if(message(@arg1, instance_of, button),
		    message(S, union, @arg1?size))),
	    send(BG?graphicals, for_all,
		 if(message(@arg1, instance_of, button),
		    message(@arg1, size, S))),
	    send_super(BG, compute)
	).

:- pce_end_class.

:- pce_begin_class(tool_button, button,
		   "Button for a tool_bar").

variable(condition,	code*,	     get, "Condition for activation").

initialise(TB,
	   Action:action='name|code',
	   Label:label='name|image',
	   Balloon:balloon=[name|string],
	   Condition:condition=[code]*) :->
	default(Condition, @nil, Cond),
	make_label(Label, Lbl),
	make_name(Action, Name),
	make_message(Action, Msg),
	send(TB, send_super, initialise, Name, Msg),
	send(TB, label, Lbl),
	send(TB, slot, condition, Cond),
	(   Balloon == @default
	->  true
	;   atom(Balloon)
	->  get(Balloon, label_name, Text),
	    send(TB, help_message, tag, Text)
	;   send(TB, help_message, tag, Balloon)
	).
	     
client(TB, Client:object) :<-
	get(TB, device, Dev),
	get(Dev, client, Client).

make_message(Name, @default) :-
	atomic(Name), !.
make_message(Code, Code).


make_name(Name, Name) :-
	atomic(Name), !.
make_name(Code, Name) :-
	send(Code, instance_of, message),
	get(Code, selector, Name),
	atom(Name), !.
make_name(_, 'tool_button').


make_label(Image, Image) :-
	send(Image, instance_of, image), !.
make_label(Name, Image) :-
	pce_catch_error(cannot_find_file, new(Image, image(Name))), !.
make_label(Name, Image) :-
	new(T, text(Name, left, small)),
	get(T, size, size(W, H)),
	new(I0, image(@nil, W, H)),
	send(I0, draw_in, T),
	get(I0, scale, size(16,16), Image),
	free(T),
	free(I0).


forward(TB) :->
	"Send action to <-client"::
	get(TB, message, Msg),
	(   Msg == @default
	->  get(TB, name, Selector),
	    get(TB, client, Client),
	    send(Client, Selector)
	;   send(Msg, execute)
	).


activate(TB) :->
	"Update the activation using <-condition"::
	(   get(TB, condition, Cond),
	    Cond \== @nil
	->  (   get(TB, client, Client),
	        send(Cond, forward, Client)
	    ->	send(TB, active, @on)
	    ;	send(TB, active, @off)
	    )
	).

active(TB, Val:bool) :->
	"(de)activate the menu-item"::
	send(TB, send_super, active, Val),
	(   get(TB, label, Image),
	    send(Image, instance_of, image)
	->  get(Image, active, Val, Activated),
	    send(TB, label, Activated)
	;   true
	).

:- pce_end_class.

:- pce_begin_class(tool_status_button, tool_button,
		   "A tool button representing two states").

variable(value,		bool := @off,	get, "Current value").

execute(TB) :->
	"Switch status and send message"::
	get(TB, value, Value),
	get(Value, negate, NewValue),
	send(TB, value, NewValue),
	send(TB, flush),
	get(TB, message, Message),
	(   Message == @default
	->  get(TB, client, Client),
	    get(TB, name, Selector),
	    send(Client, Selector, NewValue)
	;   Message == @nil
	->  true
	;   send(Message, forward, NewValue)
	).
	

value(TB, Value:bool) :->
	(   get(TB, value, Value)
	->  true
	;   send(TB, slot, value, Value),
	    (	Value == @on
	    ->	send(TB, status, execute)
	    ;	send(TB, status, inactive)
	    )
	).


reset(TB) :->
	"Smooth handling of abort"::
	(   get(TB, value, @off)
	->  send(TB, status, inactive)
	;   send(TB, status, execute)
	).

:- pce_end_class(tool_status_button).


		 /*******************************
		 *	      DIALOG		*
		 *******************************/

:- pce_begin_class(tool_dialog, dialog,
		   "Dialog for menu-bar and toolbar").

variable(client,	[object],	get, "The client").

initialise(TD, Client:[object]) :->
	"Refine layout"::
	send_super(TD, initialise),
	send(TD, slot, client, Client),
	send(TD, pen, 0),
	send(TD, gap, size(0, 5)).


menu_bar(TD, Create:[bool], MB:menu_bar) :<-
	"Get (or create) the menu_bar"::
	(   get(TD, member, menu_bar, MB)
	->  true
	;   Create == @on
	->  (   get(TD, tool_bar, TB)
	    ->	send(new(MB, menu_bar), above, TB)
	    ;	send(TD, append, new(MB, menu_bar))
	    )
	).

tool_bar(TD, Create:[bool], TB:tool_bar) :<-
	"Get (or create) the tool_bar"::
	(   get(TD, member, tool_bar, TB)
	->  true
	;   Create == @on
	->  (   get(TD, client, Client),
	        Client \== @default
	    ->	true
	    ;	get(TD, frame, Client)
	    ),
	    (   get(TD, menu_bar, MB)
	    ->	send(new(TB, tool_bar(Client)), below, MB)
	    ;	send(TD, append, new(TB, tool_bar(Client)))
	    )
	).


popup(TD, Name:name, Create:[bool], Popup:popup) :<-
	"Find named popup or create it"::
	get(TD, menu_bar, Create, MB),
	(   get(MB, member, Name, Popup)
	->  true
	;   Create == @on
	->  send(MB, append, new(Popup, popup(Name)))
	).


append(TD, B:'popup|tool_button|graphical|{gap}', Where:[name]) :->
	"Append buttons or popup for manu-bar"::
	(   send(B, instance_of, popup)
	->  get(TD, menu_bar, @on, MB),
	    send(MB, append, B)
	;   send(B, instance_of, menu_item)
	->  get(TD, popup, Where, @on, Popup),
	    send(Popup, append, B)
	;   (   send(B, instance_of, tool_button)
	    ;	B == gap
	    )
	->  get(TD, tool_bar, @on, TB),
	    send(TB, append, B)
	;   send_super(TD, append, B, Where)
	).

:- pce_end_class(tool_dialog).


		 /*******************************
		 *         GRAYING ICONS	*
		 *******************************/

:- pce_extend_class(image).

active(Img, Active:bool, Img2:image) :<-
	"Return image with proper activation"::
	(   Active == @off
	->  (   get(Img, hypered, inactive, Img2)
	    ->	true
	    ;	get(Img, hypered, active, _)
	    ->	Img2 = Img
	    ;	get(Img, greyed, Img2)
	    )
	;   (   get(Img, hypered, active, Img2)
	    ->	true
	    ;	Img2 = Img
	    )
	).
	    

greyed(Img, Grey:image) :<-
	"Created a greyed version of a colour image"::
	(   get(Img, hypered, inactive, Grey)
	->  true
	;   get(Img, size, size(W, H)),
	    new(Grey, image(@nil, W, H, pixmap)),
	    (   get(Img, mask, Mask),
		send(Mask, instance_of, image)
	    ->  send(Grey, mask, new(M, image(@nil, W, H, bitmap))),
		new(MB, bitmap(Mask)),
		send(MB, transparent, @on),
		send(M, draw_in, MB),
		send(M, draw_in, MB, point(1,1))
	    ;   true
	    ),
	    get(Img, monochrome, I2),
	    send(Grey, background, black),
	    new(B2, bitmap(I2)),
	    send(B2, transparent, on),
	    send(B2, colour, white),
	    send(Grey, draw_in, B2, point(1,1)),
	    get(class(menu), class_variable, inactive_colour, ClassVar),
	    get(ClassVar, value, GreyColour),
	    send(B2, colour, GreyColour),
	    send(Grey, draw_in, B2),
	    new(_, hyper(Img, Grey, inactive, active))
	).

:- pce_end_class.