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
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2009. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
%%
%% ------------------------------------------------------------
%% Basic CheckButton Type
%% ------------------------------------------------------------
-module(gstk_checkbutton).
%%------------------------------------------------------------------------------
%% CHECKBUTTON OPTIONS
%%
%% Attributes:
%% activebg Color
%% activefg Color
%% align n,w,s,e,nw,se,ne,sw,center
%% anchor n,w,s,e,nw,se,ne,sw,center
%% bg Color
%% bw Int
%% data Data
%% disabledfg Color
%% fg Color
%% group Atom
%% groupid Groupid
%% height Int
%% highlightbg Color
%% highlightbw Int
%% highlightfg Color
%% justify left|right|center
%% label {text, String} | {image, BitmapFile}
%% padx Int (Pixels)
%% pady Int (Pixels)
%% relief Relief [flat|raised|sunken|ridge|groove]
%% select Bool
%% selectbg Color
%% underline Int
%% width Int
%% wraplength Int
%% x Int
%% y Int
%%
%% Commands:
%% enable Bool
%% flash
%% invoke
%% setfocus Bool
%% toggle
%%
%% Events:
%% buttonpress [Bool | {Bool, Data}]
%% buttonrelease [Bool | {Bool, Data}]
%% click [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
%%
%% Not Implemented:
%% cursor ??????
%% focus ?????? (-takefocus)
%% font ??????
%%
-export([create/3,config/3,read/3,delete/2,event/5,option/5,read_option/5]).
-include("gstk.hrl").
%%-----------------------------------------------------------------------------
%% MANDATORY INTERFACE FUNCTIONS
%%-----------------------------------------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Function : create/7
%% Purpose : Create a widget of the type defined in this module.
%% Return : [Gsid_of_new_widget | {bad_result, Reason}]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
create(DB, GstkId, Opts) ->
TkW = gstk_generic:mk_tkw_child(DB,GstkId),
{G, GID, _NOpts} = fix_group(Opts, DB, GstkId#gstkid.owner),
NGstkId=GstkId#gstkid{widget=TkW,widget_data={G, GID}},
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(["checkbutton ", TkW," -bo 2 -indi true ",Cmd]),
NGstkId
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 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,
{NOpts, NGstkid} = fix_group(Opts, DB, Gstkid#gstkid.owner, Gstkid),
SimplePreCmd = [TkW, " conf"],
PlacePreCmd = [";place ", TkW],
gstk_generic:mk_cmd_and_exec(NOpts,NGstkid,TkW,SimplePreCmd,PlacePreCmd,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),
{_, Gid} = Gstkid#gstkid.widget_data,
gstk_db:delete_bgrp(DB, Gid),
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
event(DB, Gstkid, Etype, Edata, Args) ->
Arg2 = case Etype of
click ->
[Text, Bool | Rest] = Args,
RBool = case Bool of
1 -> true;
_Other2 -> false
end,
{G, _Gid} = Gstkid#gstkid.widget_data,
[Text, G, RBool | Rest];
_Other3 ->
Args
end,
gstk_generic:event(DB, Gstkid, Etype, Edata, Arg2).
%%------------------------------------------------------------------------------
%% 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
{disabledfg, Color} -> {s, [" -disabledforegr ", gstk:to_color(Color)]};
{group, Group} -> {s, [" -var ", gstk:to_ascii(Group)]};
{selectbg, Color} -> {s, [" -selectc ", gstk:to_color(Color)]};
{underline, Int} -> {s, [" -un ", gstk:to_ascii(Int)]};
{wraplength, Int} -> {s, [" -wr ", gstk:to_ascii(Int)]};
flash -> {c, [TkW, " f;"]};
invoke -> {c, [TkW, " i;"]};
toggle -> {c, [TkW, " to;"]};
{select, true} -> {c, [TkW, " se;"]};
{select, false} -> {c, [TkW, " de;"]};
{click, On} -> cbind(DB, Gstkid, click, On);
_ -> invalid_option
end.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 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
disabledfg -> tcl2erl:ret_color([TkW," cg -disabledforegr"]);
group -> {G, _} = Gstkid#gstkid.widget_data, G;
selectbg -> tcl2erl:ret_color([TkW," cg -selectc"]);
groupid -> {_, Gid} = Gstkid#gstkid.widget_data, Gid;
underline -> tcl2erl:ret_int([TkW," cg -un"]);
wraplength -> tcl2erl:ret_int([TkW," cg -wr"]);
select -> tcl2erl:ret_bool(["set x [", TkW,
" cg -va];global $x;set $x"]);
click -> gstk_db:is_inserted(DB, Gstkid, click);
_ -> {bad_result, {Gstkid#gstkid.objtype, invalid_option, Option}}
end.
%%------------------------------------------------------------------------------
%% PRIMITIVES
%%------------------------------------------------------------------------------
%% check button version
%% create version
fix_group(Opts, DB, Owner) ->
{G, GID, NOpts} = fg(Opts, erlNIL, erlNIL, []),
NG = case G of
erlNIL ->
Vref = gstk_db:counter(DB, variable),
list_to_atom(lists:flatten(["cb", gstk:to_ascii(Vref)]));
Other1 -> Other1
end,
RGID = case GID of
erlNIL -> {cbgrp, NG, Owner};
Other2 -> Other2
end,
RG = gstk_db:insert_bgrp(DB, RGID),
{NG, RGID, [{group, RG} | NOpts]}.
%% config version
fix_group(Opts, DB, Owner, Gstkid) ->
{RG, RGID} = Gstkid#gstkid.widget_data,
{G, GID, NOpts} = fg(Opts, RG, RGID, []),
case {G, GID} of
{RG, RGID} ->
{NOpts, Gstkid};
{NG, RGID} ->
NGID = {cbgrp, NG, Owner},
gstk_db:delete_bgrp(DB, RGID),
NRG = gstk_db:insert_bgrp(DB, NGID),
NGstkid = Gstkid#gstkid{widget_data={NG,NGID}},
gstk_db:insert_widget(DB, NGstkid),
{[{group, NRG} | NOpts], NGstkid};
{_, NGID} when NGID =/= RGID ->
gstk_db:delete_bgrp(DB, RGID),
NRG = gstk_db:insert_bgrp(DB, NGID),
NGstkid = Gstkid#gstkid{widget_data={RG,NGID}},
gstk_db:insert_widget(DB, NGstkid),
{[{group, NRG} | NOpts], NGstkid}
end.
fg([{group, G} | Opts], _, GID, Nopts) ->
fg(Opts, G, GID, Nopts);
fg([{groupid, GID} | Opts], G, _, Nopts) ->
fg(Opts, G, GID, Nopts);
fg([Opt | Opts], G, GID, Nopts) ->
fg(Opts, G, GID, [Opt | Nopts]);
fg([], Group, GID, Opts) ->
{Group, GID, Opts}.
%%
%% Config bind
%%
cbind(DB, Gstkid, Etype, On) ->
TkW = Gstkid#gstkid.widget,
Cmd = case On of
{true, Edata} ->
Eref = gstk_db:insert_event(DB, Gstkid, Etype, Edata),
[" -command {erlsend ", Eref, " \\\"[", TkW,
" cg -text]\\\" \[expr \$[", TkW, " cg -va]\]}"];
true ->
Eref = gstk_db:insert_event(DB, Gstkid, Etype, ""),
[" -command {erlsend ", Eref, " \\\"[", TkW,
" cg -text]\\\" \[expr \$[", TkW, " cg -va]\]}"];
_Other ->
gstk_db:delete_event(DB, Gstkid, Etype),
" -command {}"
end,
{s, Cmd}.
%% ----- Done -----
|