aboutsummaryrefslogtreecommitdiffstats
path: root/lib/gs/src/gstk_window.erl
blob: c14cf2fd812eb92a3dc1066278cecf6f02cf84ef (plain) (blame)
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
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1996-2016. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%

%%
%% ------------------------------------------------------------
%% Basic Window Type.
%% ------------------------------------------------------------

-module(gstk_window).
-compile([{nowarn_deprecated_function,{gs,destroy,1}}]).

%%------------------------------------------------------------------------------
%% 			    WINDOW OPTIONS
%%
%%  Attributes:
%%	x			Int
%%	y			Int
%%	width			Int
%%	height			Int
%%	bg			Color
%%	bw			Int
%%	relief			Relief  [flat|raised|sunken|ridge|groove]
%%	highlightbw		Int
%%	highlightbg		Color
%%	highlightfg		Color
%%	map			Bool
%%	iconify 		Bool
%%	title			String
%%	iconname		String	
%%      iconbitmap      	Bitmap
%%      iconmask        	Bitmap
%%	data			Data
%%      cursor                  arrow|busy|cross|hand|help|resize|text
%%
%%  Commands:
%%      raise			
%%      lower			
%%	setfocus		Bool
%%
%%  Events:
%%      configure		[Bool | {Bool, Data}]
%%	enter			[Bool | {Bool, Data}]
%%	leave			[Bool | {Bool, Data}]
%%	motion			[Bool | {Bool, Data}]
%%	keypress		[Bool | {Bool, Data}]
%%	keyrelease		[Bool | {Bool, Data}]
%%	buttonpress		[Bool | {Bool, Data}]
%%	buttonrelease		[Bool | {Bool, Data}]
%%	focus			[Bool | {Bool, Data}]
%%	destroy			[Bool | {Bool, Data}]
%%
%%  Read options:
%%	children
%%	id
%%	parent
%%	type
%%
%%  Not Implemented:
%%	screen			?????????
%%	map			
%%	unmap			
%%	iconify
%%	deiconify
%%	focusmodel		[active|passive] (wm focusmodel)
%%

-export([create/3, config/3, read/3, delete/2, event/5,destroy_win/1]).
-export([option/5,read_option/5,mk_create_opts_for_child/4]).

-include("gstk.hrl").
% bind . <1> {puts "x: [expr %X - [winfo rootx .]] y: [expr %Y - [wi rooty .]]"}

%%-----------------------------------------------------------------------------
%%			MANDATORY INTERFACE FUNCTIONS
%%-----------------------------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function   	: create/3
%% Purpose    	: Create a widget of the type defined in this module.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
create(DB, Gstkid, Opts) ->
    TkW = gstk_generic:mk_tkw_child(DB,Gstkid),
    NGstkid=Gstkid#gstkid{widget=TkW},
    case gstk_generic:make_command(transform_geometry_opts(Opts),
				  NGstkid, TkW, "", ";", DB) of
	{error,Reason} -> {error,Reason};
	Cmd when is_list(Cmd) ->
	    BindCmd = gstk_generic:bind(DB, Gstkid, TkW, configure, true),
%	    io:format("\nWINDOW1: ~p\n",[TkW]),
%	    io:format("\nWINDOW1: ~p\n",[Cmd]),
%	    io:format("\nWINDOW1: ~p\n",[BindCmd]),
	    gstk:exec(["toplevel ", TkW,Cmd,$;,BindCmd]),
	    NGstkid
    end.

mk_create_opts_for_child(DB,Cgstkid, Pgstkid, Opts) ->
    gstk_generic:mk_create_opts_for_child(DB,Cgstkid,Pgstkid,Opts).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function   	: config/3
%% Purpose    	: Configure a widget of the type defined in this module.
%% Args        	: DB	  - The Database
%%		  Gstkid   - The gstkid of the widget
%%		  Opts    - A list of options for configuring the widget
%%
%% Return 	: [true | {bad_result, Reason}]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
config(DB, Gstkid, Opts) ->
    TkW = Gstkid#gstkid.widget,
    SimplePreCmd = [TkW, " conf"],
    gstk_generic:mk_cmd_and_exec(transform_geometry_opts(Opts),
				Gstkid,TkW,SimplePreCmd,"",DB).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function   	: read/3
%% Purpose    	: Read one option from a widget
%% Args        	: DB	  - The Database
%%		  Gstkid   - The gstkid of the widget
%%		  Opt     - An option to read
%%
%% Return 	: [OptionValue | {bad_result, Reason}]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
read(DB, Gstkid, Opt) ->
    gstk_generic:read_option(DB, Gstkid, Opt).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function   	: delete/2
%% Purpose    	: Delete widget from databas and return tkwidget to destroy
%% Args        	: DB	  - The Database
%%		  Gstkid   - The gstkid of the widget
%%
%% Return 	: TkWidget to destroy
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
delete(DB, Gstkid) ->
    gstk_db:delete_widget(DB, Gstkid),
    Gstkid#gstkid.widget.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function   	: event/5
%% Purpose    	: Construct the event and send it to the owner of the widget
%% Args        	: DB	  - The Database
%%		  Gstkid   - The gstkid of the widget
%%		  Etype   - The event type
%%		  Edata   - The event data
%%		  Args    - The data from tcl/tk
%%		
%% Return 	: [true | {bad_result, Reason}]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
event(DB, Gstkid, configure, Edata, Args) ->
    [W,H|_] = Args,
    gstk_db:insert_opt(DB,Gstkid,{width,W}),
    gstk_db:insert_opt(DB,Gstkid,{height,H}),
    case gstk_db:opt(DB,Gstkid,configure) of
	true ->
	    apply(gstk_generic,event,[DB,Gstkid,configure,Edata,Args]);
	false ->
	    ok
    end;
event(DB, Gstkid, destroy, Edata, Args) ->
    spawn(gstk_window,destroy_win,[gstk:make_extern_id(Gstkid#gstkid.id,DB)]),
    gstk_generic:event(DB, Gstkid, destroy, Edata, Args);
event(DB, Gstkid, Etype, Edata, Args) ->
    gstk_generic:event(DB, Gstkid, Etype, Edata, Args).

destroy_win(ID) ->
    gs:destroy(ID).
%%------------------------------------------------------------------------------
%%			MANDATORY FUNCTIONS
%%------------------------------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function   	: option/4
%% Purpose    	: Take care of options
%% Args        	: Option  - An option tuple
%%		  Gstkid   - The gstkid of the widget
%%		  TkW     - The  tk-widget
%%		  DB	  - The Database
%%
%% Return 	: A tuple {OptionType, OptionCmd}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%-define(REGEXP,"regexp {(\\d+)x(\\d+)\\+?(-?\\d+)\\+?(-?\\d+)} ").
% FIXME: Is this ok? Always positive?
-define(REGEXP,"regexp {(\\d+)x(\\d+)\\+(\\d+)\\+(\\d+)} ").

option(Option, Gstkid, TkW, DB,_) ->
    case Option of
%% Bug in tcl/tk complicates setting of a single x,y,width,height.
	{x,               X} -> 
	    {c, 
	    [?REGEXP,"[wm ge ",TkW, "] g w h x y;wm ge ", TkW,
	     " ${w}x$h",signed(X),"+$y;update idletasks"]};
	{y,               Y} -> 
	    {c,[?REGEXP,"[wm ge ",TkW, "] g w h x y;wm ge ", TkW,
		" ${w}x$h+$x",signed(Y),"; update idletasks"]};
	{width,       Width} when Width >= 0 ->	% FIXME: Needed test?
	    case gstk_db:opt_or_not(DB,Gstkid,width) of
		{value,Width} -> none;
		_Q ->
		    gstk_db:insert_opt(DB,Gstkid,{width,Width}),
		    {c,[?REGEXP,"[wm ge ",TkW, "] g w h x y;wm ge ", TkW," ",
			gstk:to_ascii(Width),"x$h+$x+$y;update idletasks"]}
	    end;
    	{height,     Height} when Height >= 0 -> % FIXME: Needed test?
	    case gstk_db:opt_or_not(DB,Gstkid,height) of
		{value,Height} -> none;
		_Q ->				% FIXME: Why different?
		    gstk_db:insert_opt(DB,Gstkid,{height,Height}),
		    {c,
		     ["wm ge ",TkW,
		      " [winfo w ", TkW, "]x",gstk:to_ascii(Height),
		      ";update idletasks"]}
	    end;
	{width_height, {W,H}} when W >= 0, H >= 0 ->
	    case {gstk_db:opt_or_not(DB,Gstkid,width),
		  gstk_db:opt_or_not(DB,Gstkid,height)} of
		{{value,W},{value,H}} ->
		    none;
		_OtherSize -> 
		    gstk_db:insert_opt(DB,Gstkid,{height,H}),
		    gstk_db:insert_opt(DB,Gstkid,{width,W}),
		    {c, ["update idletasks;wm ge ", TkW, " ",
			 gstk:to_ascii(W),"x",gstk:to_ascii(H),
			 ";update idletasks"]}
	    end;
	{xy,             {X,Y}} -> 
	    {c, [?REGEXP,"[wm ge ",TkW, "] g w h x y;wm ge ", TkW,
		 " ${w}x$h", signed(X),signed(Y),
		 ";update idletasks"]};
	{bg,          Color} -> {s, [" -bg ", gstk:to_color(Color)]};
	{map,          true} -> {c, ["wm deiconify ", TkW]};
	{map,         false} -> {c, ["wm withdraw ", TkW]};
	{configure,      On} ->
	    gstk_db:insert_opt(DB,Gstkid,{configure,On}),
	    none;
	{iconify,      true} -> {c, ["wm iconify ", TkW]};
	{iconify,     false} -> {c, ["wm deiconify ", TkW]};
	{title,       Title} -> {c, ["wm title ", TkW, " " , 
					   gstk:to_ascii(Title)]};
	{iconname,     Name} -> {c, ["wm iconn ",TkW, " ",
					   gstk:to_ascii(Name)]};
	{iconbitmap, Bitmap} -> {c, ["wm iconb ",TkW, " ",
					   gstk:to_ascii(Bitmap)]};
	{iconmask,   Bitmap} -> {c, ["wm iconm ",TkW, " ",
					   gstk:to_ascii(Bitmap)]};
	raise		     -> {c, ["raise ", TkW]};
	lower		     -> {c, ["lower ", TkW]};
	{setfocus,     true} -> {c, ["focus ", TkW]};
	{setfocus,    false} -> {c, ["focus {}"]};
	{buttonpress,    On} ->
	    Eref = mk_eref(On, DB, Gstkid, buttonpress),
	    {c,["bind ",TkW," <ButtonPress> ",
	       event_onoff(["{erlsend ",Eref," %b ",xy_abs_str(TkW),"};"],On)]};
	{buttonrelease,  On} ->
	    Eref = mk_eref(On, DB, Gstkid, buttonrelease),
	    {c,["bind ",TkW," <ButtonRelease> ",
	       event_onoff(["{erlsend ",Eref," %b ",xy_abs_str(TkW),"};"],On)]};
	{motion,         On} ->
	    Eref = mk_eref(On, DB, Gstkid, motion),
	    {c,["bind ",TkW," <Motion> ",
	       event_onoff(["{erlsend ",Eref," ",xy_abs_str(TkW),"};"],On)]};
	_                    -> invalid_option
    end.

xy_abs_str(TkW) ->
    ["[expr %X-[winfo rootx ",TkW,"]] [expr %Y-[winfo rooty ",TkW,"]]"].

event_onoff(Str, true) -> Str;
event_onoff(_,false) -> "{}".

mk_eref(false, DB, Gstkid, Etype) ->
    gstk_db:delete_event(DB, Gstkid, Etype),
    dummy;
mk_eref(true,DB,Gstkid,Etype) ->
    gstk_db:insert_event(DB, Gstkid, Etype, []).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function   	: read_option/3
%% Purpose    	: Take care of a read option
%% Args        	: DB	  - The Database
%%		  Gstkid   - The gstkid of the widget
%%		  Option  - An option
%%
%% Return 	: The value of the option or invalid_option
%%		  [OptionValue | {bad_result, Reason}]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
read_option(Option, Gstkid, TkW, DB,_) ->
    case Option of
	x             -> tcl2erl:ret_x(geo_str(TkW));
	y             -> tcl2erl:ret_y(geo_str(TkW));
	width         -> tcl2erl:ret_width(geo_str(TkW));
	height        -> tcl2erl:ret_height(geo_str(TkW));
	configure     -> gstk_db:opt(DB,Gstkid,configure);
	bg            -> tcl2erl:ret_color([TkW," cg -bg"]);
	map           -> tcl2erl:ret_mapped(["winfo is ", TkW]);
	iconify       -> tcl2erl:ret_iconified(["wm st ", TkW]);
	title         -> tcl2erl:ret_str(["wm ti ", TkW]);
	iconname      -> tcl2erl:ret_str(["wm iconn ", TkW]);
	iconbitmap    -> tcl2erl:ret_str(["wm iconb ", TkW]);
	iconmask      -> tcl2erl:ret_str(["wm iconm ", TkW]);
	setfocus      -> tcl2erl:ret_focus(TkW, "focus");
	_ -> {bad_result, {Gstkid#gstkid.objtype, invalid_option, Option}}
    end.

geo_str(TkW) ->
    ["update idletasks;",?REGEXP,"[wm geometry ", TkW,
     "] g w h x y;set tmp \"$w $h $x $y\""].



%%----------------------------------------------------------------------
%%			       PRIMITIVES
%%----------------------------------------------------------------------

%% Return {+,-}Int  to be used in a geometry option
signed(X) when X>=0 ->
    [$+,integer_to_list(X)];
signed(X) when X<0 ->
    integer_to_list(X).

%%----------------------------------------------------------------------
%% Purpose: tcl/tk: wm .window geo sets WxH+x+y at one time.
%%          flushing every time is expensive. Do (almost) as much as
%%          possible in one operation.
%%----------------------------------------------------------------------
transform_geometry_opts(Opts) ->
    {Geo,RestOpts} = collect_geo_opts(Opts,[],[]),
    Geo2 = make_atomic(lists:sort(Geo)),
    lists:append(Geo2,RestOpts).

make_atomic([{height,H},{width,W},{x,X},{y,Y}]) ->
    [{width_height,{W,H}},{xy,{X,Y}}];
make_atomic([{height,H},{width,W}|XY]) ->
    [{width_height,{W,H}}|XY];
make_atomic([WH,{x,X},{y,Y}]) ->
    [WH,{xy,{X,Y}}];
make_atomic(L) -> L.

%%----------------------------------------------------------------------
%% Returns: {(list of x,y,width,height options),list of other opts}
%%----------------------------------------------------------------------
collect_geo_opts([{x,X}|Opts],Geo,Rest) ->
    collect_geo_opts(Opts,[{x,X}|Geo],Rest);
collect_geo_opts([{y,Y}|Opts],Geo,Rest) ->
    collect_geo_opts(Opts,[{y,Y}|Geo],Rest);
collect_geo_opts([{height,H}|Opts],Geo,Rest) ->
    collect_geo_opts(Opts,[{height,H}|Geo],Rest);
collect_geo_opts([{width,W}|Opts],Geo,Rest) ->
    collect_geo_opts(Opts,[{width,W}|Geo],Rest);
collect_geo_opts([Opt|Opts],Geo,Rest) ->
    collect_geo_opts(Opts,Geo,[Opt|Rest]);
collect_geo_opts([],Geo,Rest) -> {Geo,Rest}.
    
%%% ----- Done -----