1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 2009-2016. All Rights Reserved. 5%% 6%% Licensed under the Apache License, Version 2.0 (the "License"); 7%% you may not use this file except in compliance with the License. 8%% You may obtain a copy of the License at 9%% 10%% http://www.apache.org/licenses/LICENSE-2.0 11%% 12%% Unless required by applicable law or agreed to in writing, software 13%% distributed under the License is distributed on an "AS IS" BASIS, 14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15%% See the License for the specific language governing permissions and 16%% limitations under the License. 17%% 18%% %CopyrightEnd% 19 20%% This is example of the widgets and usage of wxErlang 21%% Hopefully it will contain all implemented widgets, it's event handling 22%% and some tutorials of how to use sizers and other stuff. 23 24-module(ex_button). 25 26-include_lib("wx/include/wx.hrl"). 27 28-behaviour(wx_object). 29-export([start/1, init/1, 30 terminate/2, code_change/3, 31 handle_info/2, handle_call/3, handle_cast/2, handle_event/2]). 32 33-record(state, 34 { 35 parent, 36 config 37 }). 38 39start(Config) -> 40 wx_object:start_link(?MODULE, Config, []). 41 42%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 43init(Config) -> 44 wx:batch(fun() -> do_init(Config) end). 45 46do_init(Config) -> 47 Parent = proplists:get_value(parent, Config), 48 Panel = wxScrolledWindow:new(Parent), 49 50 %% Setup sizers 51 Sz = wxBoxSizer:new(?wxVERTICAL), 52 ButtSz = wxStaticBoxSizer:new(?wxHORIZONTAL, Panel, 53 [{label, "wxButton"}]), 54 AlignSz = wxStaticBoxSizer:new(?wxHORIZONTAL, Panel, 55 [{label, "Alignment Style"}]), 56 OtherSz = wxStaticBoxSizer:new(?wxHORIZONTAL, Panel, 57 [{label, "Other Styles"}]), 58 StockTopSz = wxStaticBoxSizer:new(?wxHORIZONTAL, Panel, 59 [{label, "Stock Buttons"}]), 60 61 B10 = wxButton:new(Panel, 10, [{label,"Normal"}]), 62 wxButton:setToolTip(B10, "Normal button with (default) centered label"), 63 64 B11 = wxToggleButton:new(Panel, 11, "Toggle Button"), 65 wxToggleButton:connect(B11, command_togglebutton_clicked), 66 wxButton:setToolTip(B11, "A toggle button"), 67 68 B12 = wxButton:new(Panel, 12, [{label,"Default"}]), 69 wxButton:setDefault(B12), 70 wxButton:setToolTip(B12, "Normal button set to be the default button"), 71 72 B13 = wxButton:new(Panel, 13, [{label,"Disabled"}]), 73 wxButton:disable(B13), 74 wxButton:setToolTip(B13, "Disabled Normal button"), 75 76 B14 = wxBitmapButton:new(Panel, 14, create_bitmap("A bitmap button")), 77 wxButton:setToolTip(B14, "A Bitmap button"), 78 79 %% Alignment and NO_BORDER only works on Win and GTK 80 %% according to docs. 81 B20 = wxButton:new(Panel, 20, [{label,"Left Aligned"}, 82 {size, {100, -1}}, 83 {style,?wxBU_LEFT}]), 84 wxButton:setToolTip(B20, "Normal button with left aligned label"), 85 86 B21 = wxButton:new(Panel, 21, [{label,"Top Aligned"}, 87 {size, {-1, 50}}, 88 {style,?wxBU_TOP}]), 89 wxButton:setToolTip(B21, "Normal button with top aligned label"), 90 91 B22 = wxButton:new(Panel, 22, [{label,"Lower Right Aligned"}, 92 {size, {150, 50}}, 93 {style,?wxBU_BOTTOM bor ?wxBU_RIGHT}]), 94 wxButton:setToolTip(B22, "Normal button with top right aligned label"), 95 96 %% Other types 97 B30 = wxButton:new(Panel, 30, [{label,"Flat Style"}, 98 {style, ?wxNO_BORDER}]), 99 wxButton:setToolTip(B30, "Flat style button, on some OS'es"), 100 101 B31 = wxButton:new(Panel, 31, [{label,"Exact Fit"}, 102 {style, ?wxBU_EXACTFIT}]), 103 wxButton:setToolTip(B31, "Minimal Size button"), 104 105 %% Stock Buttons 106 StockButts = [wxButton:new(Panel, Id) || Id <- stock_buttons()], 107 108 StockSz = wxGridSizer:new(0,5,3,3), 109 110 SzFlags = [{proportion, 0}, {border, 4}, {flag, ?wxALL}], 111 Expand = [{proportion, 0}, {border, 4}, {flag, ?wxALL bor 112 ?wxEXPAND}], 113 [wxSizer:add(ButtSz, Button, SzFlags) || 114 Button <- [B10, B11, B12, B13, B14]], 115 [wxSizer:add(AlignSz, Button, SzFlags) || Button <- [B20,B21,B22]], 116 [wxSizer:add(OtherSz, Button, SzFlags) || Button <- [B30,B31]], 117 [wxSizer:add(StockSz, Butt, SzFlags) || Butt <- StockButts], 118 wxSizer:add(StockTopSz, StockSz, SzFlags), 119 [wxSizer:add(Sz, Button, Flag) || 120 {Button, Flag} <- [{ButtSz,SzFlags},{AlignSz,Expand}, 121 {OtherSz,Expand}, {StockTopSz, Expand}]], 122 wxWindow:connect(Panel, command_button_clicked), 123 wxWindow:setSizer(Panel, Sz), 124 wxSizer:layout(Sz), 125 wxWindow:refresh(Panel), 126 wxScrolledWindow:setScrollRate(Panel, 5, 5), 127 {Panel, #state{parent=Panel, config=Config}}. 128 129 130%%%%%%%%%%%% 131%% Async Events are handled in handle_event as in handle_info 132handle_event(#wx{id=Id, event=#wxCommand{type=command_button_clicked}}, 133 State = #state{parent=Parent}) -> 134 B0 = wxWindow:findWindowById(Id, [{parent, Parent}]), 135 Butt = wx:typeCast(B0, wxButton), 136 Label = wxButton:getLabel(Butt), 137 demo:format(State#state.config,"Button: \'~ts\' clicked~n",[Label]), 138 {noreply,State}; 139 140handle_event(#wx{event=#wxCommand{type=command_togglebutton_clicked}}, 141 State = #state{}) -> 142 demo:format(State#state.config, 143 "Button: You toggled the 'Toggle button' ~n",[]), 144 {noreply,State}; 145 146handle_event(Ev = #wx{}, State = #state{}) -> 147 demo:format(State#state.config,"Got Event ~p~n",[Ev]), 148 {noreply,State}. 149 150%% Callbacks handled as normal gen_server callbacks 151handle_info(Msg, State) -> 152 demo:format(State#state.config, "Got Info ~p~n",[Msg]), 153 {noreply,State}. 154 155handle_call(shutdown, _From, State=#state{parent=Panel}) -> 156 wxPanel:destroy(Panel), 157 {stop, normal, ok, State}; 158 159handle_call(Msg, _From, State) -> 160 demo:format(State#state.config,"Got Call ~p~n",[Msg]), 161 {reply,ok,State}. 162 163handle_cast(Msg, State) -> 164 io:format("Got cast ~p~n",[Msg]), 165 {noreply,State}. 166 167code_change(_, _, State) -> 168 {stop, ignore, State}. 169 170terminate(_Reason, _) -> 171 ok. 172 173%%%%% a copy from wxwidgets samples. 174create_bitmap(Label) -> 175 Bmp = wxBitmap:new(140, 30), 176 DC = wxMemoryDC:new(), 177 wxMemoryDC:selectObject(DC, Bmp), 178 wxDC:setBackground(DC, ?wxWHITE_BRUSH), 179 wxDC:clear(DC), 180 wxDC:setTextForeground(DC, ?wxBLUE), 181 wxDC:drawLabel(DC, Label, {5,5,130,20}, [{alignment, ?wxALIGN_CENTER}]), 182 wxMemoryDC:destroy(DC), 183 Bmp. 184 185 186stock_buttons() -> 187 [?wxID_ABOUT, 188 ?wxID_ADD, 189 ?wxID_APPLY, 190 ?wxID_BOLD, 191 ?wxID_CANCEL, 192 ?wxID_CLEAR, 193 ?wxID_CLOSE, 194 ?wxID_COPY, 195 ?wxID_CUT, 196 ?wxID_DELETE, 197 ?wxID_EDIT, 198 ?wxID_FIND, 199 ?wxID_FILE, 200 ?wxID_REPLACE, 201 ?wxID_BACKWARD, 202 ?wxID_DOWN, 203 ?wxID_FORWARD, 204 ?wxID_UP, 205 ?wxID_HELP, 206 ?wxID_HOME, 207 ?wxID_INDENT, 208 ?wxID_INDEX, 209 ?wxID_ITALIC, 210 ?wxID_JUSTIFY_CENTER, 211 ?wxID_JUSTIFY_FILL, 212 ?wxID_JUSTIFY_LEFT, 213 ?wxID_JUSTIFY_RIGHT, 214 ?wxID_NEW, 215 ?wxID_NO, 216 ?wxID_OK, 217 ?wxID_OPEN, 218 ?wxID_PASTE, 219 ?wxID_PREFERENCES, 220 ?wxID_PRINT, 221 ?wxID_PREVIEW, 222 ?wxID_PROPERTIES, 223 ?wxID_EXIT, 224 ?wxID_REDO, 225 ?wxID_REFRESH, 226 ?wxID_REMOVE, 227 ?wxID_REVERT_TO_SAVED, 228 ?wxID_SAVE, 229 ?wxID_SAVEAS, 230 ?wxID_SELECTALL, 231 ?wxID_STOP, 232 ?wxID_UNDELETE, 233 ?wxID_UNDERLINE, 234 ?wxID_UNDO, 235 ?wxID_UNINDENT, 236 ?wxID_YES, 237 ?wxID_ZOOM_100, 238 ?wxID_ZOOM_FIT, 239 ?wxID_ZOOM_IN, 240 ?wxID_ZOOM_OUT]. 241 242