如何获得wxFrame的sizer? (更新 - 2 [所有代码])

时间:2016-08-03 22:02:21

标签: erlang wxwidgets wxerlang

Display of tree, when added in event handler

%%%-------------------------------------------------------------------
%%% @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}.

这是entires代码,希望这会有所帮助......

WX-1.6.1 添加了wx:debug()来检查来自wx服务器/窗口管理器的返回值。

注意:    a)正在创建,初始化,显示框架并在init()中连接()    b)init()返回的状态记录包含Frame    c)当通过选择menuItem触发事件时,树被添加到使用State传递的Frame中    d)无论是使用XRC文件还是在init()中手动构建Frame,行为都是相同的。    e)但是,如果Window服务器/管理器(WX)的句柄在init()中的状态记录中传递并在事件处理程序中用作创建新帧的父项,则树将正确显示。\在新框架中

整个代码可以根据要求发布

非常感谢任何帮助。

CSP

0 个答案:

没有答案