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_popupMenu).
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	  menu
38	}).
39
40start(Config) ->
41    wx_object:start_link(?MODULE, Config, []).
42
43%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44init(Config) ->
45    wx:batch(fun() -> do_init(Config) end).
46
47do_init(Config) ->
48    Root = proplists:get_value(parent, Config),
49    Parent = wxPanel:new(Root,[]),
50    MainSizer = wxBoxSizer:new(?wxVERTICAL),
51    Box = wxStaticBox:new(Parent, ?wxID_ANY, "Popup Menu"),
52    Sz = wxStaticBoxSizer:new(Box, ?wxVERTICAL),
53    Text = wxStaticText:new(Parent, ?wxID_ANY, "Right click to open popup menu", []),
54    Panel = wxPanel:new(Parent),
55    wxPanel:connect(Panel, right_up),
56    Sizer = wxBoxSizer:new(?wxVERTICAL),
57    wxSizer:add(Sizer, Text, [{border, 20}, {flag, ?wxALL}]),
58    wxPanel:setSizer(Panel, Sizer),
59    wxSizer:add(Sz, Panel, [{proportion,1}, {flag, ?wxEXPAND}]),
60    wxSizer:layout(Sz),
61    PopupMenu = create_menu(),
62    wxSizer:add(MainSizer, Sz, [{proportion,1}, {flag, ?wxEXPAND}]),
63    wxWindow:setSizer(Parent, MainSizer),
64    {Parent, #state{parent=Parent, config=Config, menu=PopupMenu}}.
65
66%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67%% Async Events are handled in handle_event as in handle_info
68handle_event(#wx{obj = Panel,
69		 event = #wxMouse{type = right_up}},
70	     State = #state{menu = Menu}) ->
71    %% Open the popup menu
72    wxWindow:popupMenu(Panel, Menu),
73    {noreply, State};
74handle_event(#wx{obj = Menu, id = Id,
75		 event = #wxCommand{type = command_menu_selected}},
76	     State = #state{}) ->
77    %% Get the selected item label
78    Label = wxMenu:getLabel(Menu, Id),
79    demo:format(State#state.config, "wxMenu clicked ~p\n", [Label]),
80    {noreply, State};
81handle_event(Ev, State) ->
82    demo:format(State#state.config, "Unexpected Event ~p\n", [Ev]),
83    {noreply, State}.
84
85%% Callbacks handled as normal gen_server callbacks
86handle_info(Msg, State) ->
87    demo:format(State#state.config, "Got Info ~p\n", [Msg]),
88    {noreply, State}.
89
90handle_call(shutdown, _From, State=#state{parent=Panel}) ->
91    wxPanel:destroy(Panel),
92    {stop, normal, ok, State};
93
94handle_call(Msg, _From, State) ->
95    demo:format(State#state.config, "Got Call ~p\n", [Msg]),
96    {reply,{error, nyi}, State}.
97
98handle_cast(Msg, State) ->
99    io:format("Got cast ~p~n",[Msg]),
100    {noreply,State}.
101
102
103code_change(_, _, State) ->
104    {stop, ignore, State}.
105
106terminate(_Reason, #state{menu=Popup}) ->
107    wxMenu:destroy(Popup),
108    ok.
109
110%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111%% Local functions
112%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113
114create_menu() ->
115    Menu = wxMenu:new([]),
116    SubMenu  = wxMenu:new([]),
117    SubMenu2 = wxMenu:new([]),
118
119    wxMenu:append(Menu, ?wxID_UNDO, "Undo", []),
120    wxMenu:append(Menu, ?wxID_REDO, "Redo", []),
121    wxMenu:append(Menu, ?wxID_HELP, "Help", []),
122    wxMenu:appendSeparator(Menu),
123    wxMenu:appendCheckItem(Menu, ?wxID_ANY, "Check item", []),
124    wxMenu:appendSeparator(Menu),
125    wxMenu:appendRadioItem(Menu, ?wxID_ANY, "Radio item 1", []),
126    wxMenu:appendRadioItem(Menu, ?wxID_ANY, "Radio item 2", []),
127    wxMenu:appendRadioItem(Menu, ?wxID_ANY, "Radio item 3", []),
128    wxMenu:appendRadioItem(Menu, ?wxID_ANY, "Radio item 4", []),
129
130    wxMenu:appendSeparator(Menu),
131    wxMenuItem:enable(wxMenu:append(Menu, ?wxID_ANY, "Disabled", []), [{enable,false}]),
132    wxMenu:appendSeparator(Menu),
133
134    wxMenu:append(SubMenu, ?wxID_ABOUT, "About", []),
135    wxMenu:append(SubMenu, ?wxID_ANY, "Sub Item2", []),
136    wxMenu:append(SubMenu, ?wxID_SAVE, "Save", []),
137    wxMenu:break(SubMenu),
138    wxMenu:append(SubMenu, ?wxID_EXIT, "Exit", []),
139    wxMenu:append(SubMenu, ?wxID_OPEN, "Open", []),
140    wxMenu:append(SubMenu, ?wxID_NEW, "New", []),
141    wxMenu:append(Menu, ?wxID_ANY, "Sub menu", SubMenu, []),
142
143    wxMenu:appendCheckItem(SubMenu2, ?wxID_ANY, "Check Item", []),
144    wxMenu:appendSeparator(SubMenu2),
145    wxMenu:append(SubMenu2, ?wxID_CLEAR, "Clear", []),
146    wxMenu:append(SubMenu2, ?wxID_ANY, "Sub Item", []),
147
148    Bitmap = wxArtProvider:getBitmap("wxART_NEW"),
149    AnotherSubMenu = wxMenuItem:new([{parentMenu, Menu},
150				     {id, ?wxID_ANY},
151				     {text, "Another sub menu"},
152				     {subMenu, SubMenu2},
153				     {kind, ?wxITEM_NORMAL}]),
154    wxMenuItem:setBitmap(AnotherSubMenu, Bitmap),
155    wxMenu:append(Menu, AnotherSubMenu),
156
157    wxMenu:connect(Menu, command_menu_selected),
158    wxMenu:connect(SubMenu, command_menu_selected),
159    wxMenu:connect(SubMenu2, command_menu_selected),
160    Menu.
161