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_cursor).
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	  cursors,
38	  win
39	}).
40
41start(Config) ->
42    wx_object:start_link(?MODULE, Config, []).
43
44%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45init(Config) ->
46    wx:batch(fun() -> do_init(Config) end).
47
48do_init(Config) ->
49    Parent = proplists:get_value(parent, Config),
50    Panel = wxScrolledWindow:new(Parent, []),
51
52    %% Setup sizers
53    MainSizer = wxBoxSizer:new(?wxVERTICAL),
54    MiscSizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel,
55				     [{label, "Misc"}]),
56    StaticBoxSizer = wxStaticBoxSizer:new(?wxHORIZONTAL, Panel,
57					  [{label, "Test the cursor here"}]),
58    CursorSizer = wxBoxSizer:new(?wxHORIZONTAL),
59
60    CursorLabels = [Cursor || {Cursor, _} <- cursors()],
61    StockCursors = wxRadioBox:new(Panel, ?wxID_ANY, "Stock cursors",
62			      ?wxDefaultPosition,
63			      ?wxDefaultSize, CursorLabels,
64			      [{majorDim, 2},
65			       {style, ?wxHORIZONTAL}]),
66
67    Fun = fun(Item, Int) ->
68		  CursorId = proplists:get_value(Item, cursors()),
69		  Cursor = wxCursor:new(CursorId),
70		  case wxCursor:ok(Cursor) of
71		      true ->
72			  ok;
73		      false ->
74			  wxRadioBox:enable(StockCursors, Int, [{enable, false}])
75		  end,
76		  Int+1
77	  end,
78    wx:foldl(Fun,0, CursorLabels),
79
80
81
82
83    Win = wxWindow:new(Panel, ?wxID_ANY, [{size, {300,300}}]),
84
85    ToggleButton = wxToggleButton:new(Panel, ?wxID_ANY, "Begin busy cursor", []),
86
87    %% Add to sizers
88    wxSizer:add(CursorSizer, StockCursors),
89    wxSizer:add(StaticBoxSizer, Win),
90    wxSizer:add(CursorSizer, StaticBoxSizer),
91    wxSizer:add(MiscSizer, ToggleButton),
92
93    wxSizer:add(MainSizer, CursorSizer),
94    wxSizer:add(MainSizer, MiscSizer),
95
96    wxToggleButton:connect(ToggleButton, command_togglebutton_clicked, []),
97    wxRadioBox:connect(StockCursors, command_radiobox_selected, []),
98    wxScrolledWindow:setScrollRate(Panel, 5,5),
99    wxPanel:setSizer(Panel, MainSizer),
100    {Panel, #state{parent=Panel, config=Config,
101		   win = Win}}.
102
103%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104%% Async Events are handled in handle_event as in handle_info
105handle_event(#wx{event = #wxCommand{type = command_radiobox_selected,
106				    cmdString = String}},
107	     State = #state{}) ->
108    wxWindow:refresh(State#state.parent),
109    CursorId = proplists:get_value(String, cursors()),
110    Cursor = wxCursor:new(CursorId),
111    wxWindow:setCursor(State#state.win,  Cursor),
112    {noreply, State#state{}};
113handle_event(#wx{obj = ToggleButton,
114		 event = #wxCommand{type = command_togglebutton_clicked,
115				    commandInt = Int}},
116	     State = #state{}) ->
117    case Int of
118	1 ->
119	    wx_misc:beginBusyCursor(),
120	    wxToggleButton:setLabel(ToggleButton, "End busy cursor");
121	0 ->
122	    wx_misc:endBusyCursor(),
123	    wxToggleButton:setLabel(ToggleButton, "Begin busy cursor")
124    end,
125    {noreply, State};
126handle_event(Ev = #wx{}, State = #state{}) ->
127    demo:format(State#state.config, "Got Event ~p\n", [Ev]),
128    {noreply, State}.
129
130%% Callbacks handled as normal gen_server callbacks
131handle_info(Msg, State) ->
132    demo:format(State#state.config, "Got Info ~p\n", [Msg]),
133    {noreply, State}.
134
135handle_call(shutdown, _From, State=#state{parent=Panel}) ->
136    wxPanel:destroy(Panel),
137    {stop, normal, ok, State};
138
139handle_call(Msg, _From, State) ->
140    demo:format(State#state.config, "Got Call ~p\n", [Msg]),
141    {reply,{error, nyi}, State}.
142
143handle_cast(Msg, State) ->
144    io:format("Got cast ~p~n",[Msg]),
145    {noreply,State}.
146
147code_change(_, _, State) ->
148    {stop, ignore, State}.
149
150terminate(_Reason, _State) ->
151    case wx_misc:isBusy() of
152	true ->
153	    wx_misc:endBusyCursor();
154	false ->
155	    ignore
156    end.
157
158%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159%% Local functions
160%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
161
162cursors() ->
163    [{"Arrow",          ?wxCURSOR_ARROW},
164     {"Right arrow",    ?wxCURSOR_RIGHT_ARROW},
165     {"Blank",          ?wxCURSOR_BLANK},
166     {"Bullseye",       ?wxCURSOR_BULLSEYE},
167     {"Char",           ?wxCURSOR_CHAR},
168     {"Cross",          ?wxCURSOR_CROSS},
169     {"Hand",           ?wxCURSOR_HAND},
170     {"I-beam",         ?wxCURSOR_IBEAM},
171     {"Left button",    ?wxCURSOR_LEFT_BUTTON},
172     {"Magnifier",      ?wxCURSOR_MAGNIFIER},
173     {"Middle button",  ?wxCURSOR_MIDDLE_BUTTON},
174     {"No entry",       ?wxCURSOR_NO_ENTRY},
175     {"Paint brush",    ?wxCURSOR_PAINT_BRUSH},
176     {"Pencil",         ?wxCURSOR_PENCIL},
177     {"Point left",     ?wxCURSOR_POINT_LEFT},
178     {"Point right",    ?wxCURSOR_POINT_RIGHT},
179     {"Question arrow", ?wxCURSOR_QUESTION_ARROW},
180     {"Right button",   ?wxCURSOR_RIGHT_BUTTON},
181     {"Size NE-SW",     ?wxCURSOR_SIZENESW},
182     {"Size N-S",       ?wxCURSOR_SIZENS},
183     {"Size NW-SE",     ?wxCURSOR_SIZENWSE},
184     {"Size W-E",       ?wxCURSOR_SIZEWE},
185     {"Sizing",         ?wxCURSOR_SIZING},
186     {"Spraycan",       ?wxCURSOR_SPRAYCAN},
187     {"Wait",           ?wxCURSOR_WAIT},
188     {"Watch",          ?wxCURSOR_WATCH},
189     {"Arrow wait",     ?wxCURSOR_ARROWWAIT}].
190