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

wxErlang 中 wxGrid 一些测试使用

柴磊
2023-12-01
-module(ex_grid).

-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]).

-include_lib("wx/include/wx.hrl").

-record(state, 
    {
      parent,
      config,
      grid
    }).

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

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

handle_event(#wx{event = #wxKey{type = key_up, keyCode = KeyCode}}, State) ->
    #state{grid = Grid} = State,
    case KeyCode of
        $a ->
            wxGrid:insertRows(Grid),
            io:format("a");
        _  ->
            ok
    end,
    {noreply, State#state{}};

handle_event(#wx{event = #wxGrid{type = grid_cell_change,
                 row = Row, col = Col}},
         State = #state{grid = Grid}) ->
    Val = wxGrid:getCellValue(State#state.grid, Row, Col),
    io:format("Cell {~p,~p} changed to ~p.\n",[Row,Col,Val]),
    draw(Grid),
    {noreply, State};

handle_event(#wx{id=Id, event=#wxCommand{type=command_button_clicked}}, 
             #state{parent=Panel, config=Config} = State) ->
    case Id of
      1100 ->
           Parent = proplists:get_value(parent, Config),  
           ToolBar = wxFrame:getToolBar(Parent),
           io:format("get toolbar = ~p~n", [ToolBar]),
           wxWindow:show(ToolBar, [{show, false}]),
           io:format("Panel command_button_clicked~n ");
      1000 ->
           io:format("Frame command_button_clicked~n ");
      1001 ->
           io:format("Reparent command_button_clicked~n ");
      _ -> 
           Parent = proplists:get_value(parent, Config),  
           ToolBar = wxFrame:getToolBar(Parent),
           wxWindow:show(ToolBar, [{show, true}]),
           io:format("Id = ~p~n", [Id])
    end,
    {noreply,State};

handle_event(#wx{event = #wxFontPicker{type = command_fontpicker_changed, font = Font}}, 
             #state{} = State) ->
    io:format("Font changed to ~p~n", [Font]),
    {noreply, State};

handle_event(Event, State) ->
    io:format("Event = ~p~n", [Event]),
    {noreply, State}.
%% Callbacks handled as normal gen_server callbacks
handle_info(_Msg, State) ->
    {noreply, State}.

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

handle_call(_Msg, _From, State) ->
    {reply,{error, nyi}, State}.

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

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

terminate(_Reason, _State) ->
    ok.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Local functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

create_grid(Panel) ->
    %% Create the grid with 100 * 5 cells
    Grid = wxGrid:new(Panel, 2, []),
    wxGrid:createGrid(Grid, 8, 7),
    % wxGrid:setCellBackgroundColour(Grid, ?wxBLACK),
    wxGrid:setCellTextColour(Grid, ?wxRED),
    wxGrid:setBackgroundColour(Grid, ?wxRED),
    Font = wxFont:new(12, ?wxFONTFAMILY_ROMAN ,?wxFONTSTYLE_NORMAL,
                          ?wxFONTWEIGHT_NORMAL, []),
    %% Fun to set the values and flags of the cells
    Fun =
    fun(Row) ->
        wxGrid:setCellValue(Grid, Row, 0, "IF1502"),
        wxGrid:setCellValue(Grid, Row, 1, "Editable"),
        wxGrid:setCellValue(Grid, Row, 2, "Editable"),
        wxGrid:setCellValue(Grid, Row, 3, "IF1502"),
        wxGrid:setCellTextColour(Grid, Row, 3, ?wxWHITE),
        wxGrid:setReadOnly(Grid, Row, 3, [{isReadOnly,true}]),
        wxGrid:setCellValue(Grid, Row, 4, "Editable"),
        wxGrid:setRowSize(Grid, Row, 30),
        case Row rem 4 of
            0 -> 
               Font0 = wxFont:new(12, ?wxFONTFAMILY_DEFAULT ,?wxFONTSTYLE_NORMAL,
                          ?wxFONTWEIGHT_NORMAL, []),
               wxGrid:setCellFont(Grid, Row, 0, Font0),
               Font00 = wxFont:new(12, ?wxFONTFAMILY_ROMAN ,?wxFONTSTYLE_ITALIC,
                          ?wxFONTWEIGHT_NORMAL, []),
               wxGrid:setCellFont(Grid, Row, 3, Font00),
               wxGrid:setCellBackgroundColour(Grid, Row, 3, ?wxRED);
            1 -> 
               Font1 = wxFont:new(12, ?wxFONTFAMILY_DECORATIVE ,?wxFONTSTYLE_NORMAL,
                          ?wxFONTWEIGHT_NORMAL, []),
               wxGrid:setCellFont(Grid, Row, 0, Font1),
               
               wxGrid:setCellBackgroundColour(Grid, Row, 3, ?wxGREEN),
               wxGrid:setCellTextColour(Grid, Row, 2, {255,215,0,255});
            2 -> 
               Font2 = wxFont:new(11, ?wxFONTFAMILY_ROMAN ,?wxFONTSTYLE_NORMAL,
                          ?wxFONTWEIGHT_NORMAL, []),
               wxFont:setWeight(Font2, ?wxFONTWEIGHT_BOLD),
               wxFont:setDefaultEncoding(?wxFONTENCODING_MACROMAN),

               Font22 = wxFont:new(11, ?wxFONTFAMILY_ROMAN ,?wxFONTSTYLE_NORMAL,
                          ?wxFONTWEIGHT_NORMAL, []),
               wxFont:setWeight(Font22, ?wxFONTWEIGHT_LIGHT),
               wxGrid:setCellFont(Grid, Row, 0, Font2),
               wxGrid:setCellFont(Grid, Row, 3, Font22),
               wxGrid:setCellBackgroundColour(Grid, Row, 3, ?wxBLUE);
            _ -> 
               wxGrid:setCellBackgroundColour(Grid, Row, 1, ?wxCYAN),

               wxGrid:setCellValue(Grid, Row, 1,
                         "Centered"),
               wxGrid:setCellAlignment(Grid, Row, 4,
                         0,?wxALIGN_CENTER),
               wxGrid:setCellValue(Grid, Row, 4,
                         "Centered"),
               wxGrid:setCellAlignment(Grid, Row, 1,
                         ?wxALIGN_RIGHT,0),
               wxGrid:setCellTextColour(Grid, Row, 3, ?wxBLACK),
               wxGrid:setCellAlignment(Grid, Row, 2,
                         ?wxALIGN_CENTER,
                         ?wxALIGN_CENTER),
               wxGrid:setCellFont(Grid, Row, 0, Font),
               wxGrid:setCellValue(Grid, Row, 2,
                         "Centered")
             
        end
    end,
    %% Apply the fun to each row
    wx:foreach(Fun, lists:seq(0, 16)),
    lists:foreach(
            fun(Seq) ->
                wxGrid:setColSize(Grid, Seq, 100)
            end, lists:seq(0, 4)),
    % wxGrid:setRowLabelSize(Grid, 0),
    wxGrid:setColLabelSize(Grid, 0),
    %%
    Attr = wxGrid:getOrCreateCellAttr(Grid, 1, 1),
    % wxGridCellAttr:setTextColour(Attr, ?wxRED),
    % wxGrid:setSelectionForeground(Grid, ?wxGREEN),
    % wxGrid:setSelectionBackground(Grid, ?wxBLUE), 
    wxGrid:selectRow(Grid, 3),
    
    io:format("selected row ~n"),
    %%
    Editor = wxGrid:getCellEditor(Grid, 2, 2),
    
    wxGrid:setRowSize(Grid, 0, 30),
    wxGrid:setRowLabelSize(Grid, 30),
    wxGrid:setColLabelSize(Grid, 0), 
    wxGrid:setSelectionBackground(Grid, {170, 175, 180}), 
    wxGrid:setCellBackgroundColour(Grid, 1, 1, ?wxBLUE),
    wxGrid:connect(Grid, grid_cell_change),
    wxGrid:connect(Grid, key_up),
    Grid.

new() ->
    _WX = wx:new(),
    Size = {size, {600, 600}},
    Pos = {pos, {200, 300}},
    Style = {style, ?wxDEFAULT_FRAME_STYLE},
    NOptions = [Pos, Size, Style],
    Frame = makeFrame("wxGrid", NOptions),
    start([{parent, Frame}]).

do_init(Config) ->
    Parent = proplists:get_value(parent, Config),  
    Panel = wxPanel:new(Parent, []),
    %% Setup sizers
    MainSizer = wxBoxSizer:new(?wxVERTICAL),
    Sizer = wxBoxSizer:new(?wxVERTICAL),
    Button = wxButton:new(Panel, 1100, [{label, "testP"},  
                          {style, ?wxNO_BORDER bor ?wxBU_EXACTFIT}]),    
    wxButton:setBackgroundColour(Button, ?wxBLACK),
    Grid = create_grid(Panel),
    
    L1 = label_to_bitmap("日",  ?wxGREEN_BRUSH, {255, 248, 80}, {24, 24}),
    StaticBitmap = wxStaticBitmap:new(Panel, 1, L1, [{style, ?wxBORDER_SUNKEN}]),
    BBT1 = wxBitmapButton:new(Panel, 14, L1),
    wxButton:connect(Button, command_button_clicked),
    %% Add to sizers
    Options = [{flag, ?wxEXPAND}, {proportion, 1}],
    
    Frame = wxFrame:new(Panel, ?wxID_ANY, ""),
    ToolBar = wxFrame:createToolBar(Frame, [{style, ?wxTB_DOCKABLE bor ?wxTB_HORIZONTAL}]),
     
    KLabels = ["日","周","月","季","X", "1", "3", "5", "15", "30", "60", "Y"],
    Font = wxFont:new(12, ?wxFONTFAMILY_TELETYPE,
          ?wxFONTSTYLE_NORMAL,
          ?wxFONTWEIGHT_NORMAL, []),
    wxFont:setPointSize(Font, 9),
    lists:foreach(
            fun(KLabel) ->
                ButtonK = wxButton:new(ToolBar, ?wxID_ANY, [{label, KLabel}, {size, {30, 26}}]),
                wxButton:connect(ButtonK, command_button_clicked), 
                wxWindow:setFont(ButtonK, Font),   
                wxToolBar:addControl(ToolBar, ButtonK)
            end, KLabels),
    wxToolBar:addSeparator(ToolBar),
    wxToolBar:addTool(ToolBar, ?wxID_NEW, "New", wxArtProvider:getBitmap("wxART_NEW"),
          [{shortHelp, "New"}]),
    wxToolBar:setToolLongHelp(ToolBar, ?wxID_NEW, "This is long help for 'New'"),
    wxToolBar:addTool(ToolBar, ?wxID_OPEN, "Open", wxArtProvider:getBitmap("wxART_FILE_OPEN"),
          [{shortHelp, "Open"}]),
    wxToolBar:setToolLongHelp(ToolBar, ?wxID_OPEN, "This is long help for 'Open'"),
    wxToolBar:addSeparator(ToolBar),
    wxToolBar:addTool(ToolBar, ?wxID_COPY, "Copy", wxArtProvider:getBitmap("wxART_COPY"),
          [{shortHelp, "Copy"}]),
    wxToolBar:setToolLongHelp(ToolBar, ?wxID_COPY, "This is long help for 'Copy'"),
    wxToolBar:addTool(ToolBar, ?wxID_PASTE, "Paste", wxArtProvider:getBitmap("wxART_PASTE"),
          [{shortHelp, "Paste"}]),
    wxToolBar:setToolLongHelp(ToolBar, ?wxID_PASTE, "This is long help for 'Paste'"),

    wxToolBar:addControl(ToolBar,wxStaticText:new(ToolBar, 5, "This is a tool bar", 
                               [{style, ?wxBORDER_SUNKEN}])),

    ButtonR = wxButton:new(ToolBar, 1001, [{label, "Reparent"},  
                          {style, ?wxNO_BORDER bor ?wxBU_EXACTFIT}]),     
    wxButton:connect(ButtonR, command_button_clicked),          
    wxToolBar:addControl(ToolBar, ButtonR) ,     
    wxToolBar:realize(ToolBar),
    wxWindow:reparent(ToolBar, Panel),
    % wxFrame:show(Frame, [{show, false}]),
    % wxWindow:destroy(Frame),
    L2 = test_bmp(),
    TestBitmap = wxStaticBitmap:new(Panel, 11, L2, [{size, {500, 200}}]),
    wxSizer:add(Sizer, ToolBar),

    wxSizer:add(Sizer, Button),
    wxSizer:add(Sizer,  BBT1),
    wxSizer:add(Sizer,  StaticBitmap),
    wxSizer:add(Sizer,  TestBitmap),
    wxSizer:add(Sizer, Grid, Options),
    wxSizer:add(MainSizer, Sizer, Options),

    wxPanel:setSizer(Panel, MainSizer),
    wxGrid:setBackgroundColour(Grid, ?wxBLACK),
    wxFrame:show(Parent),
    {Panel, #state{parent=Panel, config=Config, grid = Grid}}.

test_bmp() ->
    Bmp = wxBitmap:new(500, 200),
    DC = wxMemoryDC:new(),
    wxMemoryDC:selectObject(DC,  Bmp),
    wxDC:setBackground(DC,  ?wxGREEN_BRUSH),
    wxDC:clear(DC),
    wxDC:setPen(DC, wxPen:new(?wxRED, [{width, 3}])), 
    wxDC:drawArc(DC, {5, 5}, {90, 90}, {45, 45}),
    wxDC:drawCheckMark(DC, {20, 20, 20, 20}),

    Image = wxImage:new("360.jpg"),
    Image2 = wxImage:scale(Image, 50, 50),
    Bmp1 = wxBitmap:new(Image2),
    wxDC:drawBitmap(DC, Bmp1, {100 ,100}),
    wxDC:drawCircle(DC, {20, 160}, 20), 
    wxDC:setBrush(DC, ?wxNullBrush),
    wxDC:drawEllipse(DC, {40, 160, 100, 30}),
    wxDC:drawLabel(DC, "ShankYan", {100, 0, 50, 30}),
    % wxDC:drawLines(DC, [{2,2}, {12, 12}, {22,22}, {33,33}, {44, 44}, {55,55}], 
    %                     [{xoffset, 10}, {yoffset, 10}]) ,
    % wxDC:drawRotatedText(DC, "ShankYan", {150, 0}, 180),
    wxDC:drawRoundedRectangle(DC, {150, 0, 50, 30}, 50), 
    wxMemoryDC:destroy(DC),
    Bmp.
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 wxGrid"),
    ToolBar = wxFrame:createToolBar(Frame, [{style, ?wxTB_DOCKABLE bor ?wxTB_HORIZONTAL}]),
    
    KLabels = ["日","周","月","季","X", "1", "3", "5", "15", "30", "60", "Y"],
    Font = wxFont:new(12, ?wxFONTFAMILY_TELETYPE,
          ?wxFONTSTYLE_NORMAL,
          ?wxFONTWEIGHT_NORMAL, []),
    wxFont:setPointSize(Font, 9),
    lists:foreach(
            fun(KLabel) ->
                ButtonK = wxButton:new(ToolBar, ?wxID_ANY, [{label, KLabel}, {size, {30, 26}}]),
                wxButton:connect(ButtonK, command_button_clicked), 
                wxWindow:setFont(ButtonK, Font),
                wxWindow:setBackgroundColour(ButtonK, {16#FF, 16#C1, 16#25}),  
                wxWindow:setForegroundColour(ButtonK, ?wxWHITE), 
                wxToolBar:addControl(ToolBar, ButtonK)

                % ButtonK = wxStaticText:new(ToolBar, 1, KLabel, []),
                
                % % wxWindow:setForegroundColour(ButtonK, ?wxGREEN),
                % wxToolBar:addControl(ToolBar, ButtonK)
            end, KLabels),
    wxToolBar:addSeparator(ToolBar),

    wxToolBar:addTool(ToolBar, ?wxID_NEW, "New", wxArtProvider:getBitmap("wxART_NEW"),
          [{shortHelp, "New"}]),
    wxToolBar:setToolLongHelp(ToolBar, ?wxID_NEW, "This is long help for 'New'"),
    wxToolBar:addTool(ToolBar, ?wxID_OPEN, "Open", wxArtProvider:getBitmap("wxART_FILE_OPEN"),
          [{shortHelp, "Open"}]),
    wxToolBar:setToolLongHelp(ToolBar, ?wxID_OPEN, "This is long help for 'Open'"),
    wxToolBar:addSeparator(ToolBar),
    wxToolBar:addTool(ToolBar, ?wxID_COPY, "Copy", wxArtProvider:getBitmap("wxART_COPY"),
          [{shortHelp, "Copy"}]),
    wxToolBar:setToolLongHelp(ToolBar, ?wxID_COPY, "This is long help for 'Copy'"),
    wxToolBar:addTool(ToolBar, ?wxID_PASTE, "Paste", wxArtProvider:getBitmap("wxART_PASTE"),
          [{shortHelp, "Paste"}]),
    wxToolBar:setToolLongHelp(ToolBar, ?wxID_PASTE, "This is long help for 'Paste'"),

    wxToolBar:addControl(ToolBar,wxStaticText:new(ToolBar, 5, "This is a tool bar")), 

    Button = wxButton:new(ToolBar, 1000, [{label, "test"},  
                          {style, ?wxNO_BORDER bor ?wxBU_EXACTFIT}]),               
    wxToolBar:addControl(ToolBar, Button) , 
    FontPicker = wxFontPickerCtrl:new(ToolBar, 3, [{size, {120, 30}}]),
    wxToolBar:addControl(ToolBar, FontPicker) ,
    wxToolBar:realize(ToolBar),
    wxFrame:setToolBar(Frame,ToolBar),
    io:format("ToolBar = ~p~n", [ToolBar]),
    wxFrame:connect(Frame, command_menu_selected),
    wxFontPickerCtrl:connect(FontPicker, command_fontpicker_changed, []),
    wxButton:connect(Button, command_button_clicked),
    Frame.   
    
draw(Grid) ->
    Num = wxGrid:getNumberRows(Grid),
    wxGrid:insertRows(Grid, [{pos, Num}, {numRows, 1}]).

label_to_bitmap(Label, BkColor, LColor, {W, H}) ->
    Bmp = wxBitmap:new(W, H),
    DC = wxMemoryDC:new(),
    wxMemoryDC:selectObject(DC,  Bmp),
    wxDC:setBackground(DC, BkColor),
    wxDC:clear(DC),
    Font = wxDC:getFont(DC),
    CommonFont = Font,
    wxFont:setWeight(CommonFont, 90),
    wxFont:setPointSize(CommonFont, 10),
    wxDC:setTextForeground(DC, LColor),
    wxDC:drawText(DC, Label, {5, 5}),
    wxMemoryDC:destroy(DC),
    Bmp.

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

 类似资料: