wxErlang; wxAuiManager; dets

-module(ex_aui).

-behaviour(wx_object).

%% Client API
-export([start/1, new/0]).

%% wx_object callbacks
-export([init/1, terminate/2,  code_change/3,
     handle_info/2, handle_call/3, handle_cast/2, handle_event/2]).

-define(DETS_FILENAME, "abc.conf").
-include_lib("wx/include/wx.hrl").
-record(state, 
    {
      parent,
      config,
      aui,
      one,
      two,
      three,
      right,
      center
    }).

start(Config) ->
    wx_object:start_link(?MODULE, Config, []).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
init(Config) ->
    wx:batch(fun() -> do_init(Config) end).

-define(pi, wxAuiPaneInfo).

do_init(Config) ->
    Parent = proplists:get_value(parent, Config), 
    {Width, Height} = wxFrame:getSize(Parent), 
    io:format("===>init get Frame Size ~p~n", [{Width, Height}]),
    Panel = wxPanel:new(Parent, []),
    %% Setup sizers
    MainSizer = wxBoxSizer:new(?wxVERTICAL),

    Manager = wxAuiManager:new([{managed_wnd, Panel}]),

    % RightPos  = value(right_pos, {-1, -1}),
    RightSize = value(right_size, {trunc( Width / 3), trunc(Height / 3)}),
    io:format("=====>>RightSize = ~p~n", [RightSize]),
    Pane = ?pi:new(),
    ?pi:closeButton(Pane),
    ?pi:right(Pane),
    ?pi:dockable(Pane, [{b, true}]),
    ?pi:floatingSize(Pane, 300,200),
    % ?pi:minSize(Pane, {50,50}),
    ?pi:paneBorder(Pane),
    ?pi:floatable(Pane, [{b, true}]),
    ?pi:name(Pane, "Right"),
    ?pi:bestSize(Pane, RightSize),

    
    Right = create_pane(11, {right_pos, {-1, -1}}, {right_size, {trunc( Width / 3), trunc(Height / 3)}}, Panel, Manager, Pane),
    OneSize = value(one_size, {300, 200}),
    io:format("OneSize = ~p~n", [OneSize]),
    One = create_pane(12, {one_pos, {-1, -1}}, {one_size, {300, 200}}, Panel, Manager,
        ?pi:gripper(?pi:bestSize(?pi:name(?pi:caption(?pi:top(?pi:new(Pane)), "One"), "One"), 
                     {trunc(Width), trunc(Height / 3)}), [{visible, false}])),
    TwoSize = value(two_size, {300, 200}),
    io:format("TwoSize = ~p~n", [TwoSize]),
    Two = create_pane(13, {two_pos, {-1, -1}}, {two_size, {300, 200}}, Panel, Manager,
        ?pi:bestSize(?pi:name(?pi:caption(?pi:left(?pi:new(Pane)), "two"), "two"),  {trunc( Width / 3), trunc(Height / 3)})),
    ThreeSize = value(three_size, {300, 200}),
    Three = create_pane(14, {three_pos, {-1, -1}}, {three_size, {300, 200}}, Panel, Manager,
        ?pi:bestSize(?pi:name(?pi:caption(?pi:bottom(?pi:new(Pane)), "Three"), "Three"),  {trunc(Width), trunc(Height / 3)})),

    Pane2 = wxAuiPaneInfo:new(Pane),
        ?pi:name(?pi:centrePane(Pane2), "Center"),
    % CenterSize = value(center_size, {300, 200}),
    Center = create_notebook(15, {center_pos, {-1, -1}}, {center_size, {-1, -1}}, 
                             Panel, Manager, ?pi:new(Pane2)),
    wxPanel:setSizer(Panel, MainSizer),
    wxAuiManager:connect(Manager, aui_pane_button, [{skip,true}]),
    wxAuiManager:connect(Manager, aui_pane_maximize, [{skip,true}]),
    wxAuiManager:update(Manager),
    process_flag(trap_exit, true),
    wxFrame:connect(Parent, size,  []),
    wxFrame:show(Parent),
    {Panel, #state{parent=Panel, config=Config, 
                   one = One, two = Two, center = Center, right = Right,
                   aui=Manager, three = Three}}.

value(Key, Default) ->
    dets:open_file(?DETS_FILENAME, [{type, set}]),
    GetValue =  
    case dets:lookup(?DETS_FILENAME, Key) of 
        [] ->
            Default;
        {error, Reason} ->
            io:format("get login error, Reason :~p~n", [Reason]),
            Default;
        [{_, Value}] ->
            io:format("get login name, Name :~p~n", [Value]),
            Value
    end,
    dets:close(?DETS_FILENAME),
    GetValue.

store(Key, Value) ->
    dets:open_file(?DETS_FILENAME, [{type, set}]),
    dets:insert(?DETS_FILENAME, {Key, Value}),
    dets:close(?DETS_FILENAME).

store(Object) ->
    dets:open_file(?DETS_FILENAME, [{type, set}]),
    dets:insert(?DETS_FILENAME, Object),
    dets:close(?DETS_FILENAME).

getPosition(Tab) -> 
    PossiblePosition =
      [wxAuiPaneInfo:isLeftDockable(Tab), 
       wxAuiPaneInfo:isRightDockable(Tab),
       wxAuiPaneInfo:isTopDockable(Tab),
       wxAuiPaneInfo:isBottomDockable(Tab)],
    io:format("PossiblePosition = ~p~n", [PossiblePosition]),
    {Pos, _Seq} = 
    lists:foldl(fun(Position,  {ThePos, Seq}) ->
                  case  Position of
                      false ->
                          {ThePos, Seq + 1};
                      true ->
                          {Seq, Seq + 1}
                   end
                end, {0, 0},  PossiblePosition),
    Pos.    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Callbacks handled as normal gen_server callbacks
handle_info(Msg, State) ->
    io:format("Got Info ~p\n", [Msg]),
    {noreply, State}.

handle_call(shutdown, _From, #state{parent=Panel, aui=Manager} =  State) ->
    wxAuiManager:unInit(Manager),
    wxAuiManager:destroy(Manager),
    wxPanel:destroy(Panel),
    {stop, normal, ok, State};

handle_call(Msg, _From, State) ->
    io:format("Got Call ~p\n", [Msg]),
    {reply,{error, nyi}, State}.

handle_cast(Msg, State) ->
    io:format("Got cast ~p~n",[Msg]),
    {noreply,State}.

%% Async Events are handled in handle_event as in handle_info
handle_event(#wx{obj = Notebook,
         event = #wxCommand{type = command_button_clicked}},
         #state{parent = Parent, aui = Manager, 
                one = One, two = Two, center = Center, right = Right,
                three = Three} = State) ->
    % (1)
    % Tab = wxPanel:new(Notebook, []),
    % wxButton:new(Tab, ?wxID_ANY, [{label,"New tab"}]),
    % wxAuiNotebook:insertPage(Notebook, 1, Tab, "OMG TAB!! ", [{select, false}]),
    % (2)
    % io:format("Parent Size = ~p~n", [wxWindow:getSize(Parent)]),
    % io:format("Three Size = ~p~n", [wxWindow:getSize(Three)]),
    % io:format("Two Size = ~p~n", [wxWindow:getSize(Two)]),
    % io:format("One Size = ~p~n", [wxWindow:getSize(One)]),
    % io:format("Right Size = ~p~n", [wxWindow:getSize(Right)]),
    % io:format("Center Size = ~p~n", [wxWindow:getSize(Center)]),
    % io:format("---------------------------~n"),
    % io:format("Parent Pos = ~p~n", [wxWindow:getPosition(Parent)]),
    % io:format("Three Pos = ~p~n", [wxWindow:getPosition(Three)]),
    % io:format("Two Pos = ~p~n", [wxWindow:getPosition(Two)]),
    % io:format("One Pos = ~p~n", [wxWindow:getPosition(One)]),
    % io:format("Right Pos = ~p~n", [wxWindow:getPosition(Right)]),
    % io:format("Center Pos = ~p~n", [wxWindow:getPosition(Center)]),
    % io:format("---------------------------~n"),
    % (2.2) Its wrong//
    % Pos1 = getPosition(wxAuiManager:getPane(Manager, "One")),
    % Pos2 = getPosition(wxAuiManager:getPane(Manager, "Two")),   
    % Pos3 = getPosition(wxAuiManager:getPane(Manager, "Center")), 
    % Pos4 = getPosition(wxAuiManager:getPane(Manager, "Right")),
    % Pos5 = getPosition(wxAuiManager:getPane(Manager, "Three")),
    % io:format("Positions =One:~p, Two = ~p, Center = ~p, Right = ~p, Three = ~p~n", 
    %      [Pos1, Pos2, Pos3, Pos4, Pos5]),
    % (3)wxAuiPaneInfoArray
    % What = wxAuiManager:getAllPanes(Manager),
    % io:format("What = ~p~n", [What]),
    % (4)
    O = get_input({wxTextCtrl, 12}) ,
    io:format("R = ~p~n", [O]),
    io:format("One = ~p~n", [One]),
    io:format("tests : ~p~n", [wxWindow:getPosition(One)]),
    Pos1 = wxWindow:getPosition(One),
    io:format("Pos1 = ~p~n", [Pos1]),
    Pos2 = wxWindow:getPosition(Two),  
    Pos3 = wxWindow:getPosition(Three), 
    Pos4 = wxWindow:getPosition(Right),
    Pos5 = wxWindow:getPosition(Center),

    Size1 = wxWindow:getSize(One),
    Size2 = wxWindow:getSize(Two),   
    Size3 = wxWindow:getSize(Three), 
    Size4 = wxWindow:getSize(Right),
    Size5 = wxWindow:getSize(Center),
    % Pos5 = wxWindow:getPosition(wxWindow:findWindowById(14, [])),
    % Size1 = wxWindow:getSize(wxWindow:findWindowById(12, [])),
    {Width, Height} = wxFrame:getSize(Parent), 
    store([{one_pos, Pos1}, {two_pos, Pos2}, {three_pos, Pos5}, 
           {right_pos, Pos4}, {center_pos, Pos3},
           {one_size, {trunc(Width), trunc(Height / 3)}}, 
           {two_size, {trunc( Width / 3), trunc(Height / 3)}}, 
           {three_size, {trunc(Width), trunc(Height / 3)}}, 
           {right_size, Size4}, {center_size, Size3}]) ,
    {noreply, State};

handle_event(#wx{event = #wxSize{type = size, size ={_W, _H}}}, State) ->
    #state{parent=Panel, config=Config} = State,
    Parent = proplists:get_value(parent, Config), 
    {Width, Height} = wxWindow:getVirtualSize(Panel),
    {ParentWidth, ParentHeight} = wxWindow:getVirtualSize(Parent),
    io:format("Panel size = ~p, frame size = ~p~n", 
              [{Width, Height}, {ParentWidth, ParentHeight}]),
    {noreply, State};

handle_event(#wx{obj = Notebook,
         event = #wxAuiNotebook{type = command_auinotebook_page_changed,
                    selection = Sel}}, State) ->
    io:format("You have changed page to ~p.\n",
        [wxAuiNotebook:getPageText(Notebook, Sel)]),
    {noreply, State};
handle_event(#wx{event = #wxAuiNotebook{type = command_auinotebook_page_close}}, State) ->
    io:format("You have closed a page.\n",[]),
    {noreply, State};
handle_event(#wx{event = #wxAuiManager{type = aui_pane_button,
                       button = Button}}, State) ->
    case Button of
    ?wxAUI_BUTTON_CLOSE ->
        io:format("You have closed a pane.\n",[]);
    ?wxAUI_BUTTON_MAXIMIZE_RESTORE ->
        ok;
    ?wxAUI_BUTTON_PIN ->
        io:format("You have pinned a pane.\n",[])
    end,
    {noreply, State};
handle_event(#wx{event = #wxAuiManager{type = aui_pane_maximize}}, State) ->
    io:format("You have maximized a pane.\n",[]),
    {noreply, State};
handle_event(#wx{event = #wxAuiManager{type = aui_pane_restore}}, State) ->
    io:format("You have restored a pane.\n",[]),
    {noreply, State};
handle_event(Ev = #wx{}, State) ->
    io:format("~p\n", [Ev]),
    {noreply, State}.

code_change(_, _, State) ->
    {stop, ignore, State}.

terminate(_Reason, #state{parent = Panel, 
                          one = One, two = Two, center = Center, right = Right,
                          three = Three} = State) ->
    % O = get_input({wxTextCtrl, 12}) ,
    % io:format("R = ~p~n", [O]),
    % io:format("One = ~p~n", [One]),
    % io:format("Parent Pos = ~p~n", [wxWindow:getPosition(Panel)]),
    % io:format("tests : ~p~n", [wxWindow:getPosition(One)]),
    % Pos1 = wxWindow:getPosition(One),
    % io:format("Pos1 = ~p~n", [Pos1]),
    % Pos2 = wxWindow:getPosition(Two),  
    % Pos3 = wxWindow:getPosition(Three), 
    % Pos4 = wxWindow:getPosition(Right),
    % Pos5 = wxWindow:getPosition(Center),

    % Size1 = wxWindow:getSize(One),
    % Size2 = wxWindow:getSize(Two),   
    % Size3 = wxWindow:getSize(Three), 
    % Size4 = wxWindow:getSize(Right),
    % Size5 = wxWindow:getSize(Center),
 
    % % Pos5 = wxWindow:getPosition(wxWindow:findWindowById(14, [])),
    % % Size1 = wxWindow:getSize(wxWindow:findWindowById(12, [])),
    % store([{one_pos, Pos1}, {two_pos, Pos2}, {three_pos, Pos5}, 
    %        {right_pos, Pos4}, {center_pos, Pos3},
    %        {one_size, Size1}, {two_size, Size2}, {three_size, Size5}, 
    %        {right_size, Size4}, {center_size, Size3}]) ,
    ok.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Local functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
create_notebook(ID, {Pos, DefaultPos}, {Size, DefaultSize}, Parent, Manager, Pane) ->
    Style = (0
         bor ?wxAUI_NB_DEFAULT_STYLE
         bor ?wxAUI_NB_TOP
         bor ?wxAUI_NB_WINDOWLIST_BUTTON
         bor ?wxAUI_NB_CLOSE_ON_ACTIVE_TAB
         bor ?wxAUI_NB_TAB_MOVE
         bor ?wxAUI_NB_SCROLL_BUTTONS
        ),
    WindowPos  = value(Pos, DefaultPos),
    WindowSize = value(Size, DefaultSize),
    Notebook = wxAuiNotebook:new(Parent, [{id, ID},
        {style, Style}, {size,  WindowSize}, {pos, WindowPos}]),
    Tab1 = wxPanel:new(Notebook, []),
    wxPanel:setBackgroundColour(Tab1, ?wxBLACK),
    wxButton:new(Tab1, ?wxID_ANY, [{label,"New tab"}]),
    wxAuiNotebook:addPage(Notebook, Tab1, "You can", []),

    Tab2 = wxPanel:new(Notebook, []),
    wxPanel:setBackgroundColour(Tab2, ?wxRED),
    wxButton:new(Tab2, ?wxID_ANY, [{label,"New tab"}]),
    wxAuiNotebook:addPage(Notebook, Tab2, "rearrange", []),

    Tab3 = wxPanel:new(Notebook, []),
    wxPanel:setBackgroundColour(Tab3, ?wxGREEN),
    wxButton:new(Tab3, ?wxID_ANY, [{label,"New tab"}]),
    wxAuiNotebook:addPage(Notebook, Tab3, "these tabs", []),

    wxAuiManager:addPane(Manager, Notebook, Pane),

    wxAuiNotebook:connect(Notebook, command_button_clicked),
    wxAuiNotebook:connect(Notebook, command_auinotebook_page_close, [{skip, false}]),
    wxAuiNotebook:connect(Notebook, command_auinotebook_page_changed),
    Notebook.


create_pane(ID, {Pos, DefaultPos}, {Size, DefaultSize}, Parent, Manager, Pane) ->
    WindowPos  = value(Pos, DefaultPos),
    WindowSize = value(Size, DefaultSize),
    TextCtrl = wxTextCtrl:new(Parent, ID, 
                         [{size,  WindowSize}, 
                          {pos, WindowPos},
                          {value, "An empty pane"},
                          {style, 0
                           bor ?wxDEFAULT
                           bor ?wxTE_MULTILINE}]),
    wxAuiManager:addPane(Manager, TextCtrl, Pane),
    TextCtrl.

new() ->
    _WX = wx:new(),
    Size = {size, {1400, 800}},
    Pos = {pos, {0, 0}},
    Style = {style, ?wxDEFAULT_FRAME_STYLE bor ?wxMAXIMIZE},
    NOptions = [Pos, Size, Style],
    Frame = makeFrame("aui Test", NOptions),
    start([{parent, Frame}]).

makeFrame(Title, Options) ->
    Frame = wxFrame:new(wx:null(), ?wxID_ANY, Title, Options),
    MenuSet  = wxMenu:new(),
    MenuHelp = wxMenu:new(),
    wxMenu:append(MenuHelp, 1, "About..."),
    MenuBar = wxMenuBar:new(),
    wxMenuBar:append(MenuBar, MenuSet, "Setting"),
    wxMenuBar:append(MenuBar, MenuHelp, "Help"),
    wxFrame:setMenuBar(Frame, MenuBar),
    wxFrame:createStatusBar(Frame),
    wxFrame:setStatusText(Frame,"deal wxTextCtrl"),
    wxFrame:connect(Frame, command_menu_selected),
    Frame.   
 

get_input({Type, WxId}) ->
    Wx = wxWindow:findWindowById(WxId, []),
    case Type of
        wxTextCtrl ->
            wx:typeCast(Wx, wxTextCtrl);
        wxComboBox ->
            wx:typeCast(Wx, wxComboBox);
        wxCheckBox ->
            wx:typeCast(Wx, wxCheckBox);
        wxAuiNotebook ->
            wx:typeCast(Wx, wxAuiNotebook);
        _ ->
            lager:error("Type ~p is not supported"),
            undefined
    end.

转载于:https://www.cnblogs.com/ShankYan/p/4365132.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值