4
%% Copyright Ericsson AB 2009. All Rights Reserved.
6
%% The contents of this file are subject to the Erlang Public License,
7
%% Version 1.1, (the "License"); you may not use this file except in
8
%% compliance with the License. You should have received a copy of the
9
%% Erlang Public License along with this software. If not, it can be
10
%% retrieved online at http://www.erlang.org/.
12
%% Software distributed under the License is distributed on an "AS IS"
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
14
%% the License for the specific language governing rights and limitations
19
-module(ex_popupMenu).
21
-behavoiur(wx_object).
26
%% wx_object callbacks
27
-export([init/1, terminate/2, code_change/3,
28
handle_info/2, handle_call/3, handle_event/2]).
30
-include_lib("wx/include/wx.hrl").
40
wx_object:start_link(?MODULE, Config, []).
42
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44
wx:batch(fun() -> do_init(Config) end).
47
Parent = proplists:get_value(parent, Config),
48
Panel = wxPanel:new(Parent, []),
51
MainSizer = wxBoxSizer:new(?wxVERTICAL),
52
Sizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel,
53
[{label, "Popup Menu"}]),
55
Text = wxStaticText:new(Panel, ?wxID_ANY, "Right click to open popup menu", []),
59
wxSizer:add(Sizer, Text, [{border, 20},
62
wxSizer:add(MainSizer, Sizer, [{flag, ?wxEXPAND},
65
wxPanel:connect(Panel, right_up),
66
wxPanel:setSizer(Panel, MainSizer),
67
{Panel, #state{parent=Panel, config=Config,
68
menu = create_menu()}}.
70
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71
%% Callbacks handled as normal gen_server callbacks
72
handle_info(Msg, State) ->
73
demo:format(State#state.config, "Got Info ~p\n", [Msg]),
76
handle_call(Msg, _From, State) ->
77
demo:format(State#state.config, "Got Call ~p\n", [Msg]),
78
{reply,{error, nyi}, State}.
80
%% Async Events are handled in handle_event as in handle_info
81
handle_event(#wx{obj = Panel,
82
event = #wxMouse{type = right_up}},
83
State = #state{menu = Menu}) ->
84
wxWindow:popupMenu(Panel, Menu),
86
handle_event(#wx{obj = Menu,
88
event = #wxCommand{type = command_menu_selected}},
90
Label = wxMenu:getLabel(Menu, Id),
91
demo:format(State#state.config, "wxMenu clicked ~p\n", [Label]),
93
handle_event(Ev = #wx{}, State = #state{}) ->
94
demo:format(State#state.config, "Got Event ~p\n", [Ev]),
97
code_change(_, _, State) ->
98
{stop, ignore, State}.
100
terminate(_Reason, _State) ->
103
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108
Menu = wxMenu:new([]),
109
SubMenu = wxMenu:new([]),
110
SubMenu2 = wxMenu:new([]),
112
wxMenu:append(Menu, ?wxID_UNDO, "Undo", []),
113
wxMenu:append(Menu, ?wxID_REDO, "Redo", []),
114
wxMenu:append(Menu, ?wxID_HELP, "Help", []),
115
wxMenu:appendSeparator(Menu),
116
wxMenu:appendCheckItem(Menu, ?wxID_ANY, "Check item", []),
117
wxMenu:appendSeparator(Menu),
118
wxMenu:appendRadioItem(Menu, ?wxID_ANY, "Radio item 1", []),
119
wxMenu:appendRadioItem(Menu, ?wxID_ANY, "Radio item 2", []),
120
wxMenu:appendRadioItem(Menu, ?wxID_ANY, "Radio item 3", []),
121
wxMenu:appendRadioItem(Menu, ?wxID_ANY, "Radio item 4", []),
123
wxMenu:appendSeparator(Menu),
124
wxMenuItem:enable(wxMenu:append(Menu, ?wxID_ANY, "Disabled", []), [{enable,false}]),
125
wxMenu:appendSeparator(Menu),
127
wxMenu:append(SubMenu, ?wxID_ABOUT, "About", []),
128
wxMenu:append(SubMenu, ?wxID_ANY, "Sub Item2", []),
129
wxMenu:append(SubMenu, ?wxID_SAVE, "Save", []),
130
wxMenu:break(SubMenu),
131
wxMenu:append(SubMenu, ?wxID_EXIT, "Exit", []),
132
wxMenu:append(SubMenu, ?wxID_OPEN, "Open", []),
133
wxMenu:append(SubMenu, ?wxID_NEW, "New", []),
134
wxMenu:append(Menu, ?wxID_ANY, "Sub menu", SubMenu, []),
136
wxMenu:appendCheckItem(SubMenu2, ?wxID_ANY, "Check Item", []),
137
wxMenu:appendSeparator(SubMenu2),
138
wxMenu:append(SubMenu2, ?wxID_CLEAR, "Clear", []),
139
wxMenu:append(SubMenu2, ?wxID_ANY, "Sub Item", []),
141
Bitmap = wxArtProvider:getBitmap("wxART_NEW"),
142
AnotherSubMenu = wxMenuItem:new([{parentMenu, Menu},
144
{text, "Another sub menu"},
146
{kind, ?wxITEM_NORMAL}]),
147
wxMenuItem:setBitmap(AnotherSubMenu, Bitmap),
148
wxMenu:append(Menu, AnotherSubMenu),
150
wxMenu:connect(Menu, command_menu_selected),
151
wxMenu:connect(SubMenu, command_menu_selected),
152
wxMenu:connect(SubMenu2, command_menu_selected),