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_choices).
21
22-behaviour(wx_object).
23
24-export([start/1, init/1, terminate/2,  code_change/3,
25	 handle_info/2, handle_call/3, handle_cast/2, handle_event/2]).
26
27-include_lib("wx/include/wx.hrl").
28
29-record(state,
30	{
31	  parent,
32	  config,
33	  list_box
34	 }).
35
36
37start(Config) ->
38    wx_object:start_link(?MODULE, Config, []).
39
40%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41init(Config) ->
42        wx:batch(fun() -> do_init(Config) end).
43
44do_init(Config) ->
45    Parent = proplists:get_value(parent, Config),
46    Panel = wxScrolledWindow:new(Parent, []),
47
48    %% Setup sizers
49    MainSizer = wxBoxSizer:new(?wxVERTICAL),
50    ListBoxSizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel,
51				 [{label, "wxListBox"}]),
52
53    ChoiceSizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel,
54				 [{label, "wxChoice"}]),
55    SpinSizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel,
56				     [{label, "wxSpinCtrl"}]),
57    ComboSizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel,
58				     [{label, "wxComboBox"}]),
59    Sizer = wxBoxSizer:new(?wxHORIZONTAL),
60    Sizer3  = wxBoxSizer:new(?wxHORIZONTAL),
61
62    Choices = ["one","two","three",
63	       "four","five","six",
64	       "seven","eight","nine",
65	       "ten", "eleven", "twelve"],
66
67    %% Create a wxListBox that uses multiple selection
68    ListBox = wxListBox:new(Panel, 1, [{size, {-1,100}},
69				       {choices, ["Multiple selection"|Choices]},
70				       {style, ?wxLB_MULTIPLE}]),
71    wxListBox:setToolTip(ListBox, "A wxListBox with multiple selection"),
72
73    %% Create a wxListBox that uses single selection
74    ListBox2 = wxListBox:new(Panel, 2, [{size, {-1,100}},
75					{choices, ["Single selection"|Choices]},
76					{style, ?wxLB_SINGLE}]),
77    wxListBox:setToolTip(ListBox2, "A wxListBox with single selection"),
78
79    %% Create a wxChoice
80    Choice = wxChoice:new(Panel, 4, [{choices, Choices}]),
81    wxChoice:setToolTip(Choice, "A wxChoice"),
82
83    %% Create a wxSpinCtrl with range between 0 and 100
84    SpinCtrl = wxSpinCtrl:new(Panel, []),
85    wxSpinCtrl:setRange(SpinCtrl, 0, 100),
86    wxSpinCtrl:setToolTip(SpinCtrl, "A wxSpinCtrl with range from 0 to 100"),
87
88    %% Create a wxComboBox and set the value to "Default value"
89    ComboBox = wxComboBox:new(Panel, 5, [{choices, Choices}]),
90    wxComboBox:setToolTip(ComboBox, "A wxComboBox"),
91
92
93    wxChoice:connect(Choice,command_choice_selected),
94    wxSpinCtrl:connect(SpinCtrl,command_spinctrl_updated),
95    wxComboBox:connect(ComboBox, command_combobox_selected),
96
97
98    %% Add to sizers
99    Options = [{border,4}, {flag, ?wxALL}],
100    wxSizer:add(Sizer, ListBox, Options),
101    wxSizer:add(Sizer, ListBox2, Options),
102
103    wxSizer:add(ChoiceSizer, Choice, Options),
104    wxSizer:add(SpinSizer, SpinCtrl, Options),
105    wxSizer:add(Sizer3, ChoiceSizer, []),
106    wxSizer:add(Sizer3, SpinSizer, [{border, 4}, {flag, ?wxLEFT}]),
107
108    wxSizer:add(ComboSizer, ComboBox, Options),
109
110    wxSizer:add(ListBoxSizer, Sizer, Options),
111    wxSizer:add(MainSizer, ListBoxSizer, Options),
112    wxSizer:add(MainSizer, Sizer3, Options),
113    wxSizer:add(MainSizer, ComboSizer, Options),
114
115    wxScrolledWindow:setScrollRate(Panel, 5, 5),
116    wxPanel:setSizer(Panel, MainSizer),
117    {Panel, #state{parent=Panel, config=Config,
118		   list_box = ListBox}}.
119
120%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121%% Async Events are handled in handle_event as in handle_info
122handle_event(#wx{obj = ComboBox,
123		 event = #wxCommand{type = command_combobox_selected}},
124	     State = #state{}) ->
125    Value = wxComboBox:getValue(ComboBox),
126    demo:format(State#state.config,"Selected wxComboBox ~p\n",[Value]),
127    {noreply, State};
128handle_event(#wx{event = #wxCommand{type = command_choice_selected,
129					cmdString = Value}},
130	     State = #state{}) ->
131    demo:format(State#state.config,"Selected wxChoice ~p\n",[Value]),
132    {noreply, State};
133handle_event(#wx{event = #wxSpin{type = command_spinctrl_updated,
134				 commandInt = Int}},
135	     State = #state{}) ->
136    demo:format(State#state.config,"wxSpinCtrl changed to ~p\n",[Int]),
137    {noreply, State};
138handle_event(Ev = #wx{}, State = #state{}) ->
139    demo:format(State#state.config,"Got Event ~p\n",[Ev]),
140    {noreply, State}.
141
142%% Callbacks handled as normal gen_server callbacks
143handle_info(Msg, State) ->
144    demo:format(State#state.config, "Got Info ~p\n",[Msg]),
145    {noreply, State}.
146
147handle_call(shutdown, _From, State=#state{parent=Panel}) ->
148    wxPanel:destroy(Panel),
149    {stop, normal, ok, State};
150
151handle_call(Msg, _From, State) ->
152    demo:format(State#state.config,"Got Call ~p\n",[Msg]),
153    {reply, {error,nyi}, State}.
154
155handle_cast(Msg, State) ->
156    io:format("Got cast ~p~n",[Msg]),
157    {noreply,State}.
158
159code_change(_, _, State) ->
160    {stop, ignore, State}.
161
162terminate(_Reason, _) ->
163    ok.
164
165%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166%% Local functions
167%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168
169