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_pickers).
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	}).
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 = wxPanel:new(Parent, []),
49
50    %% Setup sizers
51    MainSizer = wxBoxSizer:new(?wxVERTICAL),
52    DirPickerSizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel,
53					  [{label, "wxDirPickerCtrl"}]),
54    FilePickerSizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel,
55					  [{label, "wxFilePickerCtrl"}]),
56    FontPickerSizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel,
57					  [{label, "wxFontPickerCtrl"}]),
58    DatePickerSizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel,
59					   [{label, "wxDatePickerCtrl"}]),
60    ColourPickerSizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel,
61					   [{label, "wxColourPickerCtrl"}]),
62
63    DirPicker = wxDirPickerCtrl:new(Panel, 1, [{path, "/"}]),
64    FilePicker = wxFilePickerCtrl:new(Panel, 2, [{path, "/"}]),
65    FontPicker = wxFontPickerCtrl:new(Panel, 3, []),
66    DatePicker = wxDatePickerCtrl:new(Panel, 4, []),
67    ColourPicker = wxColourPickerCtrl:new(Panel, 5, []),
68
69    wxColourPickerCtrl:connect(ColourPicker, command_colourpicker_changed, []),
70    wxDirPickerCtrl:connect(DirPicker, command_dirpicker_changed, []),
71    wxFilePickerCtrl:connect(FilePicker, command_filepicker_changed, []),
72    wxFontPickerCtrl:connect(FontPicker, command_fontpicker_changed, []),
73    wxDatePickerCtrl:connect(DatePicker, date_changed, []),
74
75    %% Add to sizers
76    PickerOptions = [{border, 4},{flag, ?wxALL bor ?wxEXPAND}],
77    wxSizer:add(DirPickerSizer, DirPicker, PickerOptions),
78    wxSizer:add(FilePickerSizer, FilePicker, PickerOptions),
79    wxSizer:add(FontPickerSizer, FontPicker, PickerOptions),
80    wxSizer:add(DatePickerSizer, DatePicker, PickerOptions),
81    wxSizer:add(ColourPickerSizer, ColourPicker, PickerOptions),
82
83    SizerOptions  = [{flag, ?wxEXPAND}],
84    wxSizer:add(MainSizer, DirPickerSizer, SizerOptions),
85    wxSizer:add(MainSizer, FilePickerSizer, SizerOptions),
86    wxSizer:add(MainSizer, FontPickerSizer, SizerOptions),
87    wxSizer:add(MainSizer, DatePickerSizer, SizerOptions),
88    wxSizer:add(MainSizer, ColourPickerSizer, SizerOptions),
89
90    wxPanel:setSizer(Panel, MainSizer),
91    {Panel, #state{parent=Panel, config=Config}}.
92
93%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94%% Async Events are handled in handle_event as in handle_info
95handle_event(#wx{event = #wxFontPicker{font = Font}}, State = #state{}) ->
96    demo:format(State#state.config, "Font changed to ~p.\n", [Font]),
97    {noreply, State};
98handle_event(#wx{event = #wxColourPicker{colour = Colour}}, State = #state{}) ->
99    demo:format(State#state.config, "Colour changed to ~p.\n", [Colour]),
100    {noreply, State};
101handle_event(#wx{event = #wxFileDirPicker{type = command_filepicker_changed,
102					  path = Path}},
103	     State = #state{}) ->
104    demo:format(State#state.config, "Filepicker changed to ~p.\n", [Path]),
105    {noreply, State};
106handle_event(#wx{event = #wxFileDirPicker{type = command_dirpicker_changed,
107					  path = Path}},
108	     State = #state{}) ->
109    demo:format(State#state.config, "Dirpicker changed to ~p.\n", [Path]),
110    {noreply, State};
111handle_event(#wx{event = #wxDate{date = Date}},
112	     State = #state{}) ->
113    demo:format(State#state.config, "Datepicker changed to ~p.\n", [Date]),
114    {noreply, State};
115handle_event(Ev = #wx{}, State = #state{}) ->
116    demo:format(State#state.config, "Got Event ~p\n", [Ev]),
117    {noreply, State}.
118
119%% Callbacks handled as normal gen_server callbacks
120handle_info(Msg, State) ->
121    demo:format(State#state.config, "Got Info ~p\n", [Msg]),
122    {noreply, State}.
123
124handle_call(shutdown, _From, State=#state{parent=Panel}) ->
125    wxPanel:destroy(Panel),
126    {stop, normal, ok, State};
127
128handle_call(Msg, _From, State) ->
129    demo:format(State#state.config, "Got Call ~p\n", [Msg]),
130    {reply,{error, nyi}, State}.
131
132handle_cast(Msg, State) ->
133    io:format("Got cast ~p~n",[Msg]),
134    {noreply,State}.
135
136
137code_change(_, _, State) ->
138    {stop, ignore, State}.
139
140terminate(_Reason, _State) ->
141    ok.
142
143%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144%% Local functions
145%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146
147