4
%% Copyright Ericsson AB 2009. All Rights Reserved.
6
%% The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
19
%% This is example of the widgets and usage of wxErlang
20
%% Hopefully it will contain all implemented widgets, it's event handling
21
%% and some tutorials of how to use sizers and other stuff.
25
-include_lib("wx/include/wx.hrl").
27
-behavoiur(wx_object).
28
-export([start/1, init/1, terminate/2, code_change/3,
29
handle_info/2, handle_call/3, handle_event/2]).
38
wx_object:start_link(?MODULE, Config, []).
40
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42
wx:batch(fun() -> do_init(Config) end).
44
Parent = proplists:get_value(parent, Config),
45
Panel = wxScrolledWindow:new(Parent),
48
Sz = wxBoxSizer:new(?wxVERTICAL),
50
SzFlags = [{proportion, 0}, {border, 4}, {flag, ?wxALL}],
51
Expand = [{proportion, 0}, {border, 4}, {flag, ?wxALL bor ?wxEXPAND}],
53
ButtSz = wxStaticBoxSizer:new(?wxHORIZONTAL, Panel,
54
[{label, "wxButton"}]),
56
B10 = wxButton:new(Panel, 10, [{label,"Normal"}]),
57
wxButton:setToolTip(B10, "Normal button with (default) centered label"),
59
B11 = wxToggleButton:new(Panel, 11, "Toggle Button"),
60
wxToggleButton:connect(B11, command_togglebutton_clicked),
61
wxButton:setToolTip(B11, "A toggle button"),
63
B12 = wxButton:new(Panel, 12, [{label,"Default"}]),
64
wxButton:setDefault(B12),
65
wxButton:setToolTip(B12, "Normal button set to be the default button"),
67
B13 = wxButton:new(Panel, 13, [{label,"Disabled"}]),
68
wxButton:disable(B13),
69
wxButton:setToolTip(B13, "Disabled Normal button"),
71
B14 = wxBitmapButton:new(Panel, 14, create_bitmap("A bitmap button")),
72
wxButton:setToolTip(B14, "A Bitmap button"),
73
[wxSizer:add(ButtSz, Button, SzFlags) || Button <- [B10, B11, B12, B13, B14]],
75
%% Alignment and NO_BORDER only works on Win and GTK according to docs.
76
AlignSz = wxStaticBoxSizer:new(?wxHORIZONTAL, Panel,
77
[{label, "Alignment Style"}]),
79
B20 = wxButton:new(Panel, 20, [{label,"Left Aligned"}, {size, {100, -1}},
81
wxButton:setToolTip(B20, "Normal button with left aligned label"),
83
B21 = wxButton:new(Panel, 21, [{label,"Top Aligned"}, {size, {-1, 50}},
85
wxButton:setToolTip(B21, "Normal button with top aligned label"),
87
B22 = wxButton:new(Panel, 22, [{label,"Lower Right Aligned"}, {size, {150, 50}},
88
{style,?wxBU_BOTTOM bor ?wxBU_RIGHT}]),
89
wxButton:setToolTip(B22, "Normal button with top right aligned label"),
90
[wxSizer:add(AlignSz, Button, SzFlags) || Button <- [B20,B21,B22]],
93
OtherSz = wxStaticBoxSizer:new(?wxHORIZONTAL, Panel,
94
[{label, "Other Styles"}]),
96
B30 = wxButton:new(Panel, 30, [{label,"Flat Style"}, {style, ?wxNO_BORDER}]),
97
wxButton:setToolTip(B30, "Flat style button, on some OS'es"),
99
B31 = wxButton:new(Panel, 31, [{label,"Exact Fit"}, {style, ?wxBU_EXACTFIT}]),
100
wxButton:setToolTip(B31, "Minimal Size button"),
102
[wxSizer:add(OtherSz, Button, SzFlags) || Button <- [B30,B31]],
105
StockTopSz = wxStaticBoxSizer:new(?wxHORIZONTAL, Panel,
106
[{label, "Stock Buttons"}]),
108
StockButts = [wxButton:new(Panel, Id) || Id <- stock_buttons()],
109
StockSz = wxGridSizer:new(0,5,3,3),
110
[wxSizer:add(StockSz, Butt, SzFlags) || Butt <- StockButts],
111
wxSizer:add(StockTopSz, StockSz, SzFlags),
114
[wxSizer:add(Sz, Button, Flag) ||
115
{Button, Flag} <- [{ButtSz,SzFlags},{AlignSz,Expand},
116
{OtherSz,Expand}, {StockTopSz, Expand}]],
118
wxWindow:connect(Panel, command_button_clicked),
119
wxWindow:setSizer(Panel, Sz),
121
wxScrolledWindow:setScrollRate(Panel, 5, 5),
122
{Panel, #state{parent=Panel, config=Config}}.
148
?wxID_JUSTIFY_CENTER,
165
?wxID_REVERT_TO_SAVED,
182
%% Async Events are handled in handle_event as in handle_info
183
handle_event(#wx{id=Id, event=#wxCommand{type=command_button_clicked}},
184
State = #state{parent=Parent}) ->
185
B0 = wxWindow:findWindowById(Id, [{parent, Parent}]),
186
Butt = wx:typeCast(B0, wxButton),
187
Label = wxButton:getLabel(Butt),
188
demo:format(State#state.config,"Button: \'~ts\' clicked~n",[Label]),
191
handle_event(#wx{event=#wxCommand{type=command_togglebutton_clicked}},
193
demo:format(State#state.config,"Button: You toggled the 'Toggle button' ~n",[]),
196
handle_event(Ev = #wx{}, State = #state{}) ->
197
demo:format(State#state.config,"Got Event ~p~n",[Ev]),
200
%% Callbacks handled as normal gen_server callbacks
201
handle_info(Msg, State) ->
202
demo:format(State#state.config, "Got Info ~p~n",[Msg]),
205
handle_call(Msg, _From, State) ->
206
demo:format(State#state.config,"Got Call ~p~n",[Msg]),
209
code_change(_, _, State) ->
210
{stop, ignore, State}.
212
terminate(_Reason, _State) ->
215
%%%%% a copy from wxwidgets samples.
216
create_bitmap(Label) ->
217
Bmp = wxBitmap:new(140, 30),
218
DC = wxMemoryDC:new(),
219
wxMemoryDC:selectObject(DC, Bmp),
220
wxDC:setBackground(DC, ?wxWHITE_BRUSH),
222
wxDC:setTextForeground(DC, ?wxBLUE),
223
wxDC:drawLabel(DC, Label, {5,5,130,20}, [{alignment, ?wxALIGN_CENTER}]),
224
wxMemoryDC:destroy(DC),