%%%-------------------------------------------------------------------
%%% @author Charu Palkar <cspalkar@gmail.com>
%%% @copyright (C) 2016, Charu Palkar
%%% @doc
%%%
%%% @end
%%% Created : 22 Jul 2016 by Charu Palkar <cspalkar@gmail.com>
%%%-------------------------------------------------------------------
-module(tstx_ui).
-behaviour(wx_object).
-compile([{parse_transform, lager_transform}]).
-include_lib("wx/include/wx.hrl").
%% API
-export([start_link/0]).
%% wx_object callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
handle_event/2, terminate/2, code_change/3]).
-record(state, { parent }).
-define(TstX_SBar,1000).
-define(TstX_MEnv,2000). %% Environment
-define(TstX_MSvc,2010). %% Services
-define(TstX_MPkg,2020). %% Pkg
-define(TstX_MGrp,2030). %% Grp
-define(TstX_MFld,2040). %% Fields
-define(TstX_MDty,2050). %% Derived Types
-define(TstX_MBty,2050). %% Base Types
-define(TstX_MQit,9999). %% Quit
menu_list() -> [
{ append , ?TstX_MEnv,"&Environment" },
{ append , ?TstX_MSvc,"&Services" },
{ append , ?TstX_MPkg,"&Package" },
{ append , ?TstX_MGrp,"&Groups" },
{ append , ?TstX_MFld,"&Fields" },
{ appendSeparator , ?TstX_MFld,"Fields" },
{ append , ?TstX_MDty,"&Derived Types" },
{ append , ?TstX_MBty,"&Base Types" },
{ appendSeparator , ?TstX_MFld,"Fields" },
{ append , ?TstX_MQit,"&Quit" }
].
sbar_list() -> [
{ number, 3 },
{ style, ?wxSB_RAISED },
{ id, ?TstX_SBar }
].
%%%===================================================================
%%% API
%%%===================================================================
%%--------------------------------------------------------------------
%% @doc
%% Starts the server
%%
%% @spec start_link() -> wxWindow()
%% @end
%%--------------------------------------------------------------------
start_link() ->
wx_object:start(?MODULE, [], []).
%%%===================================================================
%%% wx_object callbacks
%%%===================================================================
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Initializes the server
%%
%% @spec init(Args) -> {wxWindow(), State} |
%% {wxWindow(), State, Timeout} |
%% ignore |
%% {stop, Reason}
%% @end
%%--------------------------------------------------------------------
init([]) ->
WX = wx:new(),
wx:debug(verbose),
Frame = wxFrame:new(wx:null(),-1,"TstX Root"),
SBar = wxFrame:createStatusBar(Frame,sbar_list()),
wxStatusBar:setStatusText(SBar,"Status -> ",[{number, 0 }]),
MBar = wxMenuBar:new(),
DBMenu = wxMenu:new(),
%% fun to add menu items
AddMenu = fun(Op,Menu,Id,Name) when Op == append -> wxMenu:Op(Menu,Id,Name);
(Op,Menu,_Id,_Name) -> wxMenu:Op(Menu)
end,
[ AddMenu(Op,DBMenu,Id,Name) || { Op, Id, Name } <- menu_list() ], %% Add menu items
wxMenuBar:append(MBar, DBMenu, "DataModel"), %% Add menu to menu bar
wxFrame:setMenuBar(Frame,MBar), %% Add menu bar to frame
SzBox = wxBoxSizer:new(?wxVERTICAL),
ok = wxFrame:setSizer(Frame,SzBox),
wxFrame:connect(Frame,command_menu_selected,[Id || { Op, Id, _Name } <- menu_list(), Op == append ]),
wxFrame:show(Frame),
{Frame, #state{ parent = Frame }}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling events
%%
%% @spec handle_event(wx{}, State) ->
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_event(#wx{id = ?TstX_MSvc, obj = Obj, userData = UD, event = Evt}, State) ->
?INFOMSG("Menu Services selected!"),
{noreply, State};
handle_event(#wx{id = ?TstX_MPkg, obj = Obj, userData = UD, event = Evt}, State) ->
?INFOMSG("Menu Packages selected!"),
{noreply, State};
handle_event(#wx{id = ?TstX_MEnv, obj = Obj, userData = UD, event = Evt}, State) ->
?INFOMSG("Menu Environments selected!"),
%% Build environment tree and display.
Parent = State#state.parent,
ERoot = wxTreeCtrl:new(Parent),
ERootId = wxTreeCtrl:addRoot(ERoot,"Environments"),
wxTreeCtrl:appendItem(ERoot,ERootId,"E1"),
wxTreeCtrl:appendItem(ERoot,ERootId,"E2"),
wxTreeCtrl:appendItem(ERoot,ERootId,"E3"),
wxTreeCtrl:expand(ERoot,ERootId),
Options = [{flag, ?wxEXPAND}, {proportion, 1}],
Sizer = wxFrame:getSizer(Parent),
wxSizer:add(Sizer,ERoot,Options),
wxFrame:setSizer(Parent,Sizer),
wxFrame:refresh(Parent),
{noreply, State};
handle_event(#wx{id = ?TstX_MQit, obj = Obj, userData = UD, event = Evt}, State) ->
?INFOMSG("Menu Quit selected!"),
wxFrame:destroy(Obj);
handle_event(#wx{id = Id, obj = Obj, userData = UD, event = Evt}, State) ->
?INFOMSG("Menu selected - Not implemented!"),
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling call messages
%%
%% @spec handle_call(Request, From, State) ->
%% {reply, Reply, State} |
%% {reply, Reply, State, Timeout} |
%% {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, Reply, State} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling cast messages
%%
%% @spec handle_cast(Msg, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_cast(_Msg, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Handling all non call/cast messages
%%
%% @spec handle_info(Info, State) -> {noreply, State} |
%% {noreply, State, Timeout} |
%% {stop, Reason, State}
%% @end
%%--------------------------------------------------------------------
handle_info(_Info, State) ->
{noreply, State}.
%%--------------------------------------------------------------------
%% @private
%% @doc
%% This function is called by a wx_object when it is about to
%% terminate. It should be the opposite of Module:init/1 and do any
%% necessary cleaning up. When it returns, the wx_object terminates
%% with Reason. The return value is ignored.
%%
%% @spec terminate(Reason, State) -> void()
%% @end
%%--------------------------------------------------------------------
terminate(_Reason, State) ->
?INFOMSG("Stopping GUI Windows Server!"),
wx:destroy(),
exit(0).
%%--------------------------------------------------------------------
%% @private
%% @doc
%% Convert process state when code is changed
%%
%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
%% @end
%%--------------------------------------------------------------------
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
This is the entires code, hope this helps....
wx-1.6.1 wx:debug() added to check return values from the wx server/window manager.
NOTES : a) Frame is being created, initialized, displayed and connect()'ed in init() b) The State record returned by init(), contains Frame c) When event is triggered by selecting the menuItem, the tree is added to Frame passed using State d) Behavior is same irrespective, of using the XRC file or building the Frame by hand in init(). e) However, if the handle to the Window server/manger (WX) is passed in the State record in init() and used in the event handler as parent to create a new frame, the tree displays correctly.\ in the new frame.
The entire code can be posted on request
Any help will be very much appreciated.