当前位置: 首页 > 工具软件 > wxErlang > 使用案例 >

wxErlang; wxAuiManager; dets

安毅
2023-12-01
-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

 类似资料:

相关阅读

相关文章

相关问答