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_notebook).
21
22-include_lib("wx/include/wx.hrl").
23
24-behaviour(wx_object).
25
26-export([start/1, init/1, terminate/2,  code_change/3,
27	 handle_info/2, handle_call/3, handle_cast/2, handle_event/2]).
28
29-record(state,
30	{
31	  parent,
32	  config,
33	  notebook
34	 }).
35
36start(Config) ->
37    wx_object:start_link(?MODULE, Config, []).
38
39%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40init(Config) ->
41        wx:batch(fun() -> do_init(Config) end).
42do_init(Config) ->
43    Parent = proplists:get_value(parent, Config),
44    Panel = wxPanel:new(Parent, []),
45
46    %% Setup sizers
47    MainSizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel,
48				     [{label, "wxNotebook"}]),
49
50    Notebook = wxNotebook:new(Panel, 1, [{style, ?wxBK_DEFAULT%,
51					        %?wxBK_ALIGN_MASK,
52					        %?wxBK_TOP,
53					        %?wxBK_BOTTOM,
54					        %?wxBK_LEFT,
55					        %?wxBK_RIGHT,
56					        %?wxNB_MULTILINE % windows only
57					 }]),
58
59    %% Make a wxImageList to be able to display icons in the tab field
60    IL = wxImageList:new(16,16),
61    wxImageList:add(IL, wxArtProvider:getBitmap("wxART_INFORMATION", [{size, {16,16}}])),
62    wxImageList:add(IL, wxArtProvider:getBitmap("wxART_MISSING_IMAGE", [{size, {16,16}}])),
63    wxNotebook:assignImageList(Notebook, IL),
64
65
66
67    Win1 = wxPanel:new(Notebook, []),
68    wxPanel:setBackgroundColour(Win1, ?wxRED),
69    Win1Text = wxStaticText:new(Win1, ?wxID_ANY, "This is a red tab.",
70				[{pos, {50, 100}}]),
71    wxStaticText:setForegroundColour(Win1Text, ?wxGREEN),
72    Sizer1 = wxBoxSizer:new(?wxHORIZONTAL),
73    wxSizer:add(Sizer1, Win1Text),
74    wxPanel:setSizer(Win1, Sizer1),
75    wxNotebook:addPage(Notebook, Win1, "Red", []),
76
77    Win2 = wxPanel:new(Notebook, []),
78    wxPanel:setBackgroundColour(Win2, ?wxBLUE),
79    Win2Text = wxStaticText:new(Win2, ?wxID_ANY, "This is a blue tab.",
80				[{pos, {50, 100}}]),
81    wxStaticText:setForegroundColour(Win2Text, {255,255,0,255}),
82    Sizer2 = wxBoxSizer:new(?wxHORIZONTAL),
83    wxSizer:add(Sizer2, Win2Text),
84    wxPanel:setSizer(Win2, Sizer2),
85    wxNotebook:addPage(Notebook, Win2, "Blue", []),
86
87    Win3 = wxPanel:new(Notebook, []),
88    wxNotebook:addPage(Notebook, Win3, "No color", []),
89    wxNotebook:setPageImage(Notebook, 2, 1),
90
91    Win4 = wxPanel:new(Notebook, []),
92    wxPanel:setBackgroundColour(Win4, ?wxBLACK),
93    Win4Text = wxStaticText:new(Win4, ?wxID_ANY, "This is a black tab.",
94				[{pos, {50, 100}}]),
95    wxStaticText:setForegroundColour(Win4Text, ?wxWHITE),
96    Sizer4 = wxBoxSizer:new(?wxHORIZONTAL),
97    wxSizer:add(Sizer4, Win4Text),
98    wxPanel:setSizer(Win4, Sizer4),
99    wxNotebook:addPage(Notebook, Win4, "Black", []),
100
101    Win5 = wxPanel:new(Notebook, []),
102    wxNotebook:addPage(Notebook, Win5, "Tab with icon", []),
103    wxNotebook:setPageImage(Notebook, 4, 0),
104
105    %% Add to sizers
106    wxSizer:add(MainSizer, Notebook, [{proportion, 1},
107				      {flag, ?wxEXPAND}]),
108
109    wxNotebook:connect(Notebook, command_notebook_page_changed,
110		       [{skip, true}]), % {skip, true} has to be set on windows
111    wxPanel:setSizer(Panel, MainSizer),
112    {Panel, #state{parent=Panel, config=Config,
113		   notebook = Notebook}}.
114
115
116%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117%% Async Events are handled in handle_event as in handle_info
118handle_event(#wx{event = #wxBookCtrl{type = command_notebook_page_changed}},
119	     State = #state{notebook = Notebook}) ->
120    Selection = wxNotebook:getSelection(Notebook),
121    Title = wxNotebook:getPageText(Notebook, Selection),
122    demo:format(State#state.config,"You have selected the tab ~p\n",[Title]),
123    {noreply,State};
124handle_event(Ev = #wx{}, State = #state{}) ->
125    demo:format(State#state.config,"Got Event ~p\n",[Ev]),
126    {noreply,State}.
127
128%% Callbacks handled as normal gen_server callbacks
129handle_info(Msg, State) ->
130    demo:format(State#state.config, "Got Info ~p\n",[Msg]),
131    {noreply,State}.
132
133handle_call(shutdown, _From, State=#state{parent=Panel}) ->
134    wxPanel:destroy(Panel),
135    {stop, normal, ok, State};
136
137handle_call(Msg, _From, State) ->
138    demo:format(State#state.config,"Got Call ~p\n",[Msg]),
139    {reply,ok,State}.
140
141handle_cast(Msg, State) ->
142    io:format("Got cast ~p~n",[Msg]),
143    {noreply,State}.
144
145
146code_change(_, _, State) ->
147    {stop, ignore, State}.
148
149terminate(_Reason, _State) ->
150    ok.
151
152%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153%%%% Local functions
154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155
156
157
158