1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2009-2018. 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_aui).
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("../../include/wx.hrl").
32
33-record(state,
34	{
35	  parent,
36	  config,
37	  aui
38	}).
39
40start(Config) ->
41    wx_object:start_link(?MODULE, Config, []).
42
43%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44init(Config) ->
45    wx:batch(fun() -> do_init(Config) end).
46
47-define(pi, wxAuiPaneInfo).
48
49do_init(Config) ->
50    Parent = proplists:get_value(parent, Config),
51    Panel = wxPanel:new(Parent, []),
52    %% Setup sizers
53    MainSizer = wxBoxSizer:new(?wxVERTICAL),
54
55    Manager = wxAuiManager:new([{managed_wnd, Panel}]),
56    try
57	Art = wxAuiManager:getArtProvider(Manager),
58	wxAuiDockArt:setColour(Art, ?wxAUI_DOCKART_BACKGROUND_COLOUR, {200, 100, 100}),
59	wxAuiDockArt:setColour(Art, ?wxAUI_DOCKART_ACTIVE_CAPTION_COLOUR, {200, 100, 100}),
60	wxAuiDockArt:setColour(Art, ?wxAUI_DOCKART_INACTIVE_CAPTION_COLOUR, {100, 200, 100}),
61
62
63	Pane = ?pi:new(),
64	?pi:closeButton(Pane),
65	?pi:right(Pane),
66	?pi:dockable(Pane, [{b, true}]),
67	?pi:floatingSize(Pane, 300,200),
68	?pi:minSize(Pane, {50,50}),
69	?pi:paneBorder(Pane),
70	?pi:floatable(Pane, [{b, true}]),
71
72	create_pane(Panel, Manager, Pane),
73	create_pane(Panel, Manager,
74		    ?pi:caption(?pi:top(?pi:new(Pane)), "One")),
75	create_pane(Panel, Manager,
76		    ?pi:caption(?pi:left(?pi:new(Pane)), "two")),
77	create_pane(Panel, Manager,
78		    ?pi:caption(?pi:bottom(?pi:new(Pane)), "Three")),
79	Pane2 = wxAuiPaneInfo:new(Pane),
80	?pi:centrePane(Pane2),
81	create_notebook(Panel, Manager, ?pi:new(Pane2)),
82
83	wxPanel:setSizer(Panel, MainSizer),
84
85	wxAuiManager:connect(Manager, aui_pane_button, [{skip,true}]),
86	wxAuiManager:connect(Manager, aui_pane_maximize, [{skip,true}]),
87	wxAuiManager:update(Manager),
88	process_flag(trap_exit, true),
89	{Panel, #state{parent=Panel, config=Config, aui=Manager}}
90    catch Class:Reason:ST ->
91	    io:format("AUI Crashed ~p ~p~n",[Reason, ST]),
92	    wxAuiManager:unInit(Manager),
93	    wxAuiManager:destroy(Manager),
94	    wxPanel:destroy(Panel),
95	    erlang:raise(Class, Reason, ST)
96    end.
97
98%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99%% Callbacks handled as normal gen_server callbacks
100handle_info(Msg, State) ->
101    demo:format(State#state.config, "Got Info ~p\n", [Msg]),
102    {noreply, State}.
103
104handle_call(shutdown, _From, State=#state{parent=Panel, aui=Manager}) ->
105    wxAuiManager:unInit(Manager),
106    wxAuiManager:destroy(Manager),
107    wxPanel:destroy(Panel),
108    {stop, normal, ok, State};
109
110handle_call(Msg, _From, State) ->
111    demo:format(State#state.config, "Got Call ~p\n", [Msg]),
112    {reply,{error, nyi}, State}.
113
114handle_cast(Msg, State) ->
115    io:format("Got cast ~p~n",[Msg]),
116    {noreply,State}.
117
118%% Async Events are handled in handle_event as in handle_info
119handle_event(#wx{obj = Notebook,
120		 event = #wxCommand{type = command_button_clicked}},
121	     State) ->
122    Tab = wxPanel:new(Notebook, []),
123    wxButton:new(Tab, ?wxID_ANY, [{label,"New tab"}]),
124    wxAuiNotebook:insertPage(Notebook, 1, Tab, "OMG TAB!! ", [{select, false}]),
125    {noreply, State};
126handle_event(#wx{obj = Notebook,
127		 event = #wxAuiNotebook{type = command_auinotebook_page_changed,
128					selection = Sel}}, State) ->
129    demo:format(State#state.config, "You have changed page to ~p.\n",
130		[wxAuiNotebook:getPageText(Notebook, Sel)]),
131    {noreply, State};
132handle_event(#wx{event = #wxAuiNotebook{type = command_auinotebook_page_close}}, State) ->
133    demo:format(State#state.config, "You have closed a page.\n",[]),
134    {noreply, State};
135handle_event(#wx{event = #wxAuiManager{type = aui_pane_button,
136				       button = Button}}, State) ->
137    case Button of
138	?wxAUI_BUTTON_CLOSE ->
139	    demo:format(State#state.config, "You have closed a pane.\n",[]);
140	?wxAUI_BUTTON_MAXIMIZE_RESTORE ->
141	    ok;
142	?wxAUI_BUTTON_PIN ->
143	    demo:format(State#state.config, "You have pinned a pane.\n",[])
144    end,
145    {noreply, State};
146handle_event(#wx{event = #wxAuiManager{type = aui_pane_maximize}}, State) ->
147    demo:format(State#state.config, "You have maximized a pane.\n",[]),
148    {noreply, State};
149handle_event(#wx{event = #wxAuiManager{type = aui_pane_restore}}, State) ->
150    demo:format(State#state.config, "You have restored a pane.\n",[]),
151    {noreply, State};
152handle_event(Ev = #wx{}, State) ->
153    io:format("~p\n", [Ev]),
154    {noreply, State}.
155
156code_change(_, _, State) ->
157    {stop, ignore, State}.
158
159terminate(_Reason, _) ->
160    ok.
161
162%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163%% Local functions
164%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165
166
167create_notebook(Parent, Manager, Pane) ->
168    Style = (0
169	     bor ?wxAUI_NB_DEFAULT_STYLE
170	     bor ?wxAUI_NB_TOP
171	     bor ?wxAUI_NB_WINDOWLIST_BUTTON
172	     bor ?wxAUI_NB_CLOSE_ON_ACTIVE_TAB
173	     bor ?wxAUI_NB_TAB_MOVE
174	     bor ?wxAUI_NB_SCROLL_BUTTONS
175	    ),
176
177    Notebook = wxAuiNotebook:new(Parent, [{style, Style}]),
178
179    Art = wxAuiSimpleTabArt:new(),
180    case ?wxMAJOR_VERSION > 2 of
181	true ->
182	    wxAuiSimpleTabArt:setColour(Art, {200, 0, 0}),
183	    wxAuiSimpleTabArt:setActiveColour(Art, {0, 0, 200});
184	false -> ignore
185    end,
186    ok = wxAuiNotebook:setArtProvider(Notebook, Art),
187
188    Tab1 = wxPanel:new(Notebook, []),
189    wxPanel:setBackgroundColour(Tab1, ?wxBLACK),
190    wxButton:new(Tab1, ?wxID_ANY, [{label,"New tab"}]),
191    wxAuiNotebook:addPage(Notebook, Tab1, "You can", []),
192
193    Tab2 = wxPanel:new(Notebook, []),
194    wxPanel:setBackgroundColour(Tab2, ?wxRED),
195    wxButton:new(Tab2, ?wxID_ANY, [{label,"New tab"}]),
196    wxAuiNotebook:addPage(Notebook, Tab2, "rearrange", []),
197
198    Tab3 = wxPanel:new(Notebook, []),
199    wxPanel:setBackgroundColour(Tab3, ?wxGREEN),
200    wxButton:new(Tab3, ?wxID_ANY, [{label,"New tab"}]),
201    wxAuiNotebook:addPage(Notebook, Tab3, "these tabs", []),
202
203    wxAuiManager:addPane(Manager, Notebook, Pane),
204
205    wxAuiNotebook:connect(Notebook, command_button_clicked),
206    wxAuiNotebook:connect(Notebook, command_auinotebook_page_close, [{skip, false}]),
207    wxAuiNotebook:connect(Notebook, command_auinotebook_page_changed),
208    Notebook.
209
210
211create_pane(Parent, Manager, Pane) ->
212    TextCtrl = wxTextCtrl:new(Parent, ?wxID_ANY, [{size, {300,200}},
213						  {value, "An empty pane"},
214						  {style, 0
215						   bor ?wxDEFAULT
216						   bor ?wxTE_MULTILINE}]),
217    wxAuiManager:addPane(Manager, TextCtrl, Pane),
218    TextCtrl.
219
220
221