erlang 中 wxTextCtrl 使用总结


-module(text_ctrl).

-behaviour(wx_object).

-export([new/0, start/1, destroy/0]).
-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").
-define(APPEND, 1).
-define(CLEAR, 2).
-define(INSERTIONPOSITION, 3).
-define(BREAKLINE, 4).
-define(TESTSTRING, 5).
-record(state,
  {
      frame,
      parent,
      config
   }).
%%%===================================================================
%%% API
%%%===================================================================
%%--------------------------------------------------------------------
%% @doc
%% @spec
%% @end
%%--------------------------------------------------------------------
new() ->
    _WX = wx:new(),
    Size = {size, {400, 600}},
    Pos = {pos, {200, 300}},
    Style = {style, ?wxDEFAULT_FRAME_STYLE},
    NOptions = [Pos, Size, Style],
    Frame = makeFrame("wxTextCtrl Text", NOptions),
    new([{parent, Frame}]).

new(Config) ->
    start(Config).

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

%%--------------------------------------------------------------------
%% @doc
%% @spec
%% @end
%% wxclient_position_select:destroy()
%%--------------------------------------------------------------------
destroy() ->
    wx_object:call(?MODULE, shutdown).

show() ->
    wx_object:call(?MODULE, show).

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

handle_info(Info, State) ->
    io:format("Got Info ~p
",[Info]),
    {noreply, State}.

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

handle_call(show, _From, #state{frame = Frame} = State) ->
    wxWindow:center(Frame),
    wxFrame:show(Frame),
    {reply, ok,  State};

handle_call(CallMsg, _From, State) ->
    io:format("Got Call ~p
",[CallMsg]),
    {reply, ok, State}.

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

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

terminate(_Reason, _State) ->
    ok.

handle_event(#wx{id = ?APPEND}, #state{config = TextCrl} = State) ->
    wxTextCtrl:appendText(TextCrl, "KuangFengyidao"),
    {noreply, State};

handle_event(#wx{id = ?BREAKLINE}, #state{config = TextCrl} = State) ->
    wxTextCtrl:appendText(TextCrl, "yidaoKuangFengyidaoKuangFengyidaoKuangFengyidao
"),
    wxTextCtrl:appendText(TextCrl, any_to_list(<<"asdgdfgdsdf">>)),
    wxTextCtrl:appendText(TextCrl, any_to_list(<<"235433563452323456">>)),
    {noreply, State};

handle_event(#wx{id = ?CLEAR}, #state{config = TextCrl} = State) ->
    wxTextCtrl:clear(TextCrl),
    {noreply, State};

handle_event(#wx{id = ?TESTSTRING}, #state{config = TextCrl} = State) ->
    % L1 = ["a","b","c","d"],
    % S1 = binary_to_list(list_to_binary(L1)),
    %------------------
    % L2 = [a, b, 1,<<"sub">>, <<233,157,158,230,179,149,232,175,183,230,177,130>>, d, "cde"],
    %      [{<<"cde">>,<<"a1603">>},{<<"id">>,351603},{<<"status">>,<<"sub">>},
    %       {<<"is">>,0},{<<"monitor">>,0},{<<"currenct">>,<<"CNNA">>}

    % L2 =[<<"code,">>, <<"id,">>, <<"status, ">>, <<"is">>, <<"monitor">>,
    %      <<"currency
">>, a],
    %%%%% method one:
    % L2 =  [<<"au">>,411412, <<"asub">>,0,0,<<"CNNY">>,<<233,187,132,233,135,145,49,52,49,50>>,
    %        <<"au">>,<<"SABD">>,<<"auui">>,1,20,12,1000,5,<<"201215">>,1,2],
    % S2 = to_string(L2),
    % io:format("S2 = ~p~n", [S2]),
    % wxTextCtrl:appendText(TextCrl, S2),
    %%%% method two:
    % L3 =
    %  [{<<"code">>,<<"a1603">>},{<<"id">>,351603},{<<"status">>,<<"unsub">>},
    %   {<<"is">>,0},{<<"monitor">>,0},{<<"currenct">>,<<"CNNY">>},
    %   {<<"name">>,<<233,187,132,229,164,167,232,177,134,49,229,143,183,49,54,48,51>>}],
    % lists:foreach(fun({TupleDataA, TupleDataB}) ->
    %                   Step1 = ["
" | any_to_list(TupleDataA)],
    %                   Step2 = [":" | any_to_list(TupleDataB)],
    %                   Last = [Step1 | Step2],
    %                   wxTextCtrl:appendText(TextCrl, Last)
    %               end, L3),
    %%method three
   A =  {struct,
     [{<<"code">>,<<"3">>},
      {<<"id">>,351603},
      {<<"status">>,<<"sub">>},
      {<<"is">>,0},
      {<<"monitor">>,0},
      {<<"currenct">>,<<"ABCD">>},
      {<<"name">>,<<233,187,132,229,164,167,232,177,134,49,229,143,183,49,54,48,51>>},
      {<<"out">>,<<"603">>},
      {<<"change">>,<<"DBCEA">>},
      {<<"pr">>,<<"a">>},
      {<<"pro">>,1},
      {<<"del">>,2016},
      {<<"deli">>,3},
      {<<"volume">>,10},
      {<<"price">>,1},
      {<<"ex">>,<<"200314">>},
      {<<"allo">>,2},
      {<<"de">>,0}]},
   B = lists:flatten(io_lib:format("~p",[A])),
    wxTextCtrl:appendText(TextCrl, B),
    {noreply, State};

handle_event(#wx{id = ?INSERTIONPOSITION}, #state{config = TextCrl} = State) ->
    Point = wxTextCtrl:getInsertionPoint(TextCrl),
    io:format("InsertionPoint = ~p~n", [Point]),
    Position = wxTextCtrl:getLastPosition(TextCrl),
    io:format("getLastPosition = ~p~n", [Position]),
    {noreply, State};   
handle_event(#wx{},  State) ->
    {noreply, State}.

%%-------------------------
%%%===================================================================
%%% API
%%%===================================================================
do_init(Config) ->
    %% define parent Panel
    Parent = proplists:get_value(parent, Config),
    Panel = wxPanel:new(Parent, []),  
    %% define controls ?
    AccountInput   = wxTextCtrl:new(Panel, 11, [{value, ""}, {size, {380, 300}}, 
                                    {style, ?wxTE_LEFT  bor ?wxTE_MULTILINE}]),
    Append = wxButton:new(Panel, ?APPEND, [{label, "Append"}]),
    Clear = wxButton:new(Panel, ?CLEAR, [{label, "Clear"}]),
    InsertionPosition = wxButton:new(Panel, ?INSERTIONPOSITION, [{label, "InsertPosition"}]),
    BreakLine = wxButton:new(Panel, ?BREAKLINE, [{label, "BreakLine"}]),
    TestString = wxButton:new(Panel, ?TESTSTRING, [{label, "TestString"}]),
    %% organization
    MainSizer = wxBoxSizer:new(?wxVERTICAL),
    wxSizer:add(MainSizer, AccountInput),
    wxSizer:add(MainSizer, Append),
    wxSizer:add(MainSizer, Clear),
    wxSizer:add(MainSizer, InsertionPosition),
    wxSizer:add(MainSizer, BreakLine),
    wxSizer:add(MainSizer, TestString),
    wxPanel:setSizer(Panel, MainSizer),
    wxWindow:center(Parent),
    wxFrame:show(Parent),
    % wxFrame:setBackgroundColour(Parent, ?wxBLUE),
    wxButton:connect(Append, command_button_clicked),
    wxButton:connect(Clear,  command_button_clicked),
    wxButton:connect(InsertionPosition, command_button_clicked),
    wxButton:connect(BreakLine, command_button_clicked),
    wxButton:connect(TestString, command_button_clicked),
    {Panel, #state{frame = Parent, parent = Panel, config = AccountInput}}.

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.

any_to_list(undefined) ->
    "";
any_to_list(List) when is_list(List) ->
    List;
any_to_list(Bin) when is_binary(Bin) ->
    case unicode:characters_to_binary(Bin, utf8, utf8) of
        Bin -> unicode:characters_to_list(Bin);
        _ -> binary_to_list(Bin)
    end;
any_to_list(Atom) when is_atom(Atom) ->
    atom_to_list(Atom);
any_to_list(Number) when is_integer(Number) ->
    integer_to_list(Number);
any_to_list(_) ->
    throw(badarg).

%%--------------------------
to_string(AtomList)  when is_list(AtomList) ->
    to_string(AtomList,"");
to_string(_)  ->
    {error,error_type}.

to_string([], R) -> 
    lists:reverse(R);
to_string([H|T], R) ->
    Tile = ["
"| R],
    to_string(T, [any_to_list(H) | Tile]);
to_string(_, _)  ->
    {error,error_type}.
原文地址:https://www.cnblogs.com/ShankYan/p/4119899.html