summaryrefslogtreecommitdiff
path: root/lib/gs/src/gstk_frame.erl
blob: a1ebb94407a4124d5057f4c25d3110ade7420299 (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
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
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1996-2009. 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 Frame Type.
%% ------------------------------------------------------------

-module(gstk_frame).

%%-----------------------------------------------------------------------------
%% 			    FRAME OPTIONS
%%
%%  Attributes:
%%	anchor			n,w,s,e,nw,se,ne,sw,center
%%	bg			Color
%%	bw			Int
%%	data			Data
%%	height			Int
%%	highlightbg		Color
%%	highlightbw		Int
%%	highlightfg		Color
%%	relief			Relief	[flat|raised|sunken|ridge|groove]
%%	width			Int
%%	x			Int
%%	y			Int
%%      cursor                  arrow|busy|cross|hand|help|resize|text
%%
%%  Commands:
%%	setfocus		Bool
%%
%%  Events:
%%	buttonpress		[Bool | {Bool, Data}]
%%	buttonrelease		[Bool | {Bool, Data}]
%%	configure		[Bool | {Bool, Data}]
%%	destroy			[Bool | {Bool, Data}]
%%	enter			[Bool | {Bool, Data}]
%%	focus			[Bool | {Bool, Data}]
%%	keypress		[Bool | {Bool, Data}]
%%	keyrelease		[Bool | {Bool, Data}]
%%	leave			[Bool | {Bool, Data}]
%%	motion			[Bool | {Bool, Data}]
%%
%%  Read Options:
%%	children
%%	id
%%	parent
%%	type
%%

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

-include("gstk.hrl").

%%-----------------------------------------------------------------------------
%%			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},
    PlacePreCmd = [";place ", TkW],
    case gstk_generic:make_command(Opts, NGstkid, TkW, "", PlacePreCmd, DB) of
	{error,Reason} -> {error,Reason};
	Cmd when is_list(Cmd) ->
	    gstk:exec(["frame ", TkW,
		      " -relief raised -bo 0",Cmd]),
	    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"],
    PlacePreCmd = [";place ", TkW],
    Opts2 = atomic_width_height(false,false,Opts),
    gstk_generic:mk_cmd_and_exec(Opts2,Gstkid,TkW,SimplePreCmd,PlacePreCmd,DB).

atomic_width_height(false,false,[]) ->
    [];
atomic_width_height(false,Width,[]) ->
    [{width,Width}];
atomic_width_height(Height,false,[]) ->
    [{height,Height}];
atomic_width_height(H,W,[]) ->
    [{width_height,{W,H}}];
atomic_width_height(_,W,[{height,H}|Opts]) ->
    atomic_width_height(H,W,Opts);
atomic_width_height(H,_,[{width,W}|Opts]) ->
    atomic_width_height(H,W,Opts);
atomic_width_height(H,W,[Opt|Opts]) ->
    [Opt|atomic_width_height(H,W,Opts)].

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 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.

event(DB, Gstkid, Etype, Edata, Args) ->
    gstk_generic:event(DB, Gstkid, Etype, Edata, Args).


%%-----------------------------------------------------------------------------
%%			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}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
option(Option, Gstkid, _TkW, DB,_) ->
    case Option of
	{bg,          Color} -> {s, [" -bg ", gstk:to_color(Color)]};
	{packer_x, _Pack} ->
            gstk_db:insert_opt(DB,Gstkid,Option),
	    none;
	{packer_y, _Pack} ->
	    gstk_db:insert_opt(DB,Gstkid,Option),
	    none;
	{width, W} ->
	    execute_pack_cmds(DB,xpack(W,DB,Gstkid)),
	    {s,[" -wi ", gstk:to_ascii(W)]};
	{height, H} ->
	    execute_pack_cmds(DB,ypack(H,DB,Gstkid)),
	    {s,[" -he ", gstk:to_ascii(H)]};
	{width_height,{W,H}} ->
	    execute_pack_cmds(DB, merge_pack_cmds(xpack(W,DB,Gstkid),
						  ypack(H,DB,Gstkid))),
	    {s,[" -he ", gstk:to_ascii(H)," -wi ", gstk:to_ascii(W)]};
	_  -> invalid_option
    end.

xpack(W,DB,Gstkid) ->
    gstk_db:insert_opt(DB,Gstkid,{width,W}),
    case gstk_db:opt_or_not(DB,Gstkid,packer_x) of
	{value,Pack} when is_list(Pack) ->
	    ColSiz = gs_packer:pack(W,Pack),
	    pack_children(pack_x,x,width,DB,
			  gstk_db:lookup_kids(DB,Gstkid#gstkid.id),
			  ColSiz);
	_Else -> []
    end.

ypack(H,DB,Gstkid) ->
    gstk_db:insert_opt(DB,Gstkid,{height,H}),
    case gstk_db:opt_or_not(DB,Gstkid,packer_y) of
	{value,Pack} when is_list(Pack) ->
	    ColSiz = gs_packer:pack(H,Pack),
	    pack_children(pack_y,y,height,DB,
			  gstk_db:lookup_kids(DB,Gstkid#gstkid.id),
			  ColSiz);
	_Else -> []
    end.

merge_pack_cmds([{Id,Opts1}|Cmds1],[{Id,Opts2}|Cmds2]) ->
    [{Id,Opts1++Opts2}|merge_pack_cmds(Cmds1,Cmds2)];
merge_pack_cmds(L1,L2) ->
    L1++L2.

execute_pack_cmds(DB,[{Id,Opts}|Cmds]) ->
    gstk:config_impl(DB,Id,Opts),
    execute_pack_cmds(DB,Cmds);
execute_pack_cmds(_,[]) ->
    ok.

%%----------------------------------------------------------------------
%% Returns: list of {Id,Opts} to be executed (or merged with other first)
%%----------------------------------------------------------------------
pack_children(PackOpt,PosOpt,SizOpt,DB,Kids,Sizes) ->
    Schildren = keep_packed(Kids,PackOpt,DB),
    pack_children2(PackOpt,PosOpt,SizOpt,Schildren,Sizes).

pack_children2(PackOpt,PosOpt,SizOpt,[{StartStop,Id}|Childs],Sizes) ->
    [pack_child(Id,StartStop,SizOpt,PosOpt,Sizes)
     | pack_children2(PackOpt,PosOpt,SizOpt,Childs,Sizes)];
pack_children2(_,_,_,[],_) ->
    [].

pack_child(Id,{StartPos,StopPos},SizOpt,PosOpt,Sizes) ->
    {Pos,Size} = find_pos(StartPos,StopPos,1,0,0,Sizes),
    {Id,[{PosOpt,Pos},{SizOpt,Size}]}.

%%----------------------------------------------------------------------
%% Returns: {PixelPos,PixelSize}
%%----------------------------------------------------------------------
find_pos(_StartPos,Pos,Pos,AccPixelPos,AccPixelSize,[Size|_]) ->
    {AccPixelPos,Size+AccPixelSize};
find_pos(StartPos,StopPos,Pos,AccPixelPos,0,[Size|Sizes])
  when Pos < StartPos ->
    find_pos(StartPos,StopPos,Pos+1,Size+AccPixelPos,0,Sizes);
find_pos(_StartPos,StopPos,Pos,AccPixelPos,AccPixelSize,[Size|Sizes])
  when Pos < StopPos ->
    find_pos(Pos,StopPos,Pos+1,AccPixelPos,Size+AccPixelSize,Sizes).

    

keep_packed([Id|Ids],PackOpt,DB) ->
    case gstk:read_impl(DB,Id,PackOpt) of
	undefined ->
	    keep_packed(Ids,PackOpt,DB);
	StartStop ->
	    [{StartStop,Id} | keep_packed(Ids,PackOpt,DB)]
    end;
keep_packed([],_,_) ->
    [].
    


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 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
	bg            -> tcl2erl:ret_color([TkW," cg -bg"]);
	_ -> {bad_result, {Gstkid#gstkid.objtype, invalid_option, Option}}
    end.

%% ----- Done -----