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-module(ex_dialogs). 21 22-behaviour(wx_object). 23 24%% Client API 25-export([start/1]). 26 27%% wx_object callbacks 28-export([init/1, terminate/2, code_change/3, 29 handle_info/2, handle_call/3, handle_cast/2, handle_event/2]). 30 31-include_lib("wx/include/wx.hrl"). 32 33-record(state, 34 { 35 parent, 36 config, 37 dialogs, 38 choices 39 }). 40 41 42start(Config) -> 43 wx_object:start_link(?MODULE, Config, []). 44 45%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 46init(Config) -> 47 wx:batch(fun() -> do_init(Config) end). 48 49do_init(Config) -> 50 Parent = proplists:get_value(parent, Config), 51 Panel = wxScrolledWindow:new(Parent, []), 52 wxScrolledWindow:setScrollRate(Panel, 5, 5), 53 54 %% Setup sizers 55 MainSizer = wxBoxSizer:new(?wxVERTICAL), 56 Sizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel, 57 [{label, "Dialogs"}]), 58 59 Buttons = 60 [wxButton:new(Panel, 1, [{label, "wxDirDialog"}]), 61 wxButton:new(Panel, 2, [{label, "wxFileDialog"}]), 62 wxButton:new(Panel, 3, [{label, "wxColourDialog"}]), 63 wxButton:new(Panel, 4, [{label, "wxMessageDialog"}]), 64 wxButton:new(Panel, 5, [{label, "wxTextEntryDialog"}]), 65 wxButton:new(Panel, 6, [{label, "wxSingleChoiceDialog"}]), 66 wxButton:new(Panel, 7, [{label, "wxMultiChoiceDialog"}]), 67 wxButton:new(Panel, 10, [{label, "wxFontDialog"}])], 68 69 Choices = ["Orange","Banana", "Apple", "Lemon", "Pear", 70 "Carrot", "Potato", "Peach", "Tomato", "Grape", 71 "Pineapple", "Blueberry"], 72 Dialogs = [{wxDirDialog, [Panel, []]}, 73 {wxFileDialog, [Panel, []]}, 74 {wxColourDialog, [Panel, []]}, 75 {wxSingleChoiceDialog, [Panel, "wxSingleChoiceDialog\n" 76 "Feel free to pick one of " 77 "these items !", "Caption", 78 Choices]}, 79 {wxMultiChoiceDialog, [Panel, "wxMultiChoiceDialog\n" 80 "Feel free to pick one of " 81 "these items !", "Caption", 82 Choices]}, 83 {wxMessageDialog, [Panel, "This is a wxMessageDialog !"]}, 84 {wxTextEntryDialog, [Panel, "This is a wxTextEntryDialog !", 85 [{value, "Erlang is the best !"}]]}, 86 {wxFontDialog, [get_parent(Parent), wxFontData:new()]}], 87 88 %% Add to sizers 89 Fun = fun(Button) -> 90 Label = list_to_atom(wxButton:getLabel(Button)), 91 wxSizer:add(Sizer, Button, [{border, 4}, {flag, ?wxALL bor ?wxEXPAND}]), 92 wxButton:connect(Button, command_button_clicked, [{userData, Label}]) 93 end, 94 wx:foreach(Fun, Buttons), 95 96 wxSizer:add(MainSizer, Sizer), 97 98 wxPanel:setSizer(Panel, MainSizer), 99 {Panel, #state{parent=Panel, config=Config, 100 dialogs = Dialogs, choices = Choices}}. 101 102%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 103%% Async Events are handled in handle_event as in handle_info 104handle_event(#wx{event = #wxCommand{type = command_button_clicked}, 105 userData = Module}, 106 State = #state{dialogs = Dialogs, choices = Choices}) -> 107 Args = proplists:get_value(Module, Dialogs), 108 Dialog = apply(Module, new, Args), 109 110 case Module:showModal(Dialog) of 111 ?wxID_OK -> 112 case Module of 113 wxColourDialog -> 114 Colour = wxColourData:getColour(Module:getColourData(Dialog)), 115 demo:format(State#state.config, "Colour: ~p\n", [Colour]); 116 wxSingleChoiceDialog -> 117 Selection = Module:getStringSelection(Dialog), 118 demo:format(State#state.config, "Selection: ~p\n", [Selection]); 119 wxTextEntryDialog -> 120 Value = Module:getValue(Dialog), 121 demo:format(State#state.config, "Value: ~p\n", [Value]); 122 wxFontDialog -> 123 Font = wxFontData:getChosenFont(Module:getFontData(Dialog)), 124 FontDesc = wxFont:getNativeFontInfoUserDesc(Font), 125 demo:format(State#state.config, 126 "Font: ~p\n", 127 [FontDesc]); 128 wxFileDialog -> 129 demo:format(State#state.config, "File path: ~p\n", [Module:getPath(Dialog)]); 130 wxDirDialog -> 131 demo:format(State#state.config, "Dir path: ~p\n", [Module:getPath(Dialog)]); 132 wxMultiChoiceDialog -> 133 Selected = [lists:nth(Num+1, Choices) || 134 Num <- Module:getSelections(Dialog)], 135 demo:format(State#state.config, "Selections: ~p\n", [Selected]); 136 wxMessageDialog -> 137 ok 138 end; 139 ?wxID_CANCEL -> cancel; 140 Any -> io:format("Any: ~p\n", [Any]) 141 end, 142 Module:destroy(Dialog), 143 {noreply, State}; 144handle_event(Ev = #wx{}, State = #state{}) -> 145 demo:format(State#state.config, "Got Event ~p\n", [Ev]), 146 {noreply, State}. 147 148%% Callbacks handled as normal gen_server callbacks 149handle_info(Msg, State) -> 150 demo:format(State#state.config, "Got Info ~p\n", [Msg]), 151 {noreply, State}. 152 153handle_call(shutdown, _From, State=#state{parent=Panel}) -> 154 wxPanel:destroy(Panel), 155 {stop, normal, ok, State}; 156 157handle_call(Msg, _From, State) -> 158 demo:format(State#state.config, "Got Call ~p\n", [Msg]), 159 {reply,{error, nyi}, State}. 160 161handle_cast(Msg, State) -> 162 io:format("Got cast ~p~n",[Msg]), 163 {noreply,State}. 164 165code_change(_, _, State) -> 166 {stop, ignore, State}. 167 168terminate(_Reason, _State) -> 169 ok. 170 171%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 172%% Local functions 173%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 174 175get_parent(Window) -> 176 Parent = wxWindow:getParent(Window), 177 case wx:is_null(Parent) of 178 false -> get_parent(Parent); 179 true -> Window 180 end. 181