Advertisement
Guest User

Untitled

a guest
Jun 11th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 4.47 KB | None | 0 0
  1.  
  2. -module(example).
  3. -author("Doug Edmunds").
  4. %% updated version of wxcd08, by Alan Gingras
  5. %% resolves exiting during mid-run
  6. -compile(export_all).
  7. -include_lib("wx/include/wx.hrl").
  8.  
  9. start() ->
  10.   State = make_window(),
  11.   loop (State),
  12.   wx:destroy().
  13.  
  14.  
  15.  
  16.  
  17. make_window() ->
  18.   Server = wx:new(),
  19.   Frame = wxFrame:new(Server, -1, "Countdown", [{size,{250, 150}}]),
  20.   Panel  = wxPanel:new(Frame),
  21.  
  22. %% create widgets
  23. %% the order entered here does not control appearance
  24.   T1001 = wxTextCtrl:new(Panel, 1001,[{value, "10"}]), %set default value
  25.   ST2001 = wxStaticText:new(Panel, 2001,"Output Area",[]),
  26.   B101  = wxButton:new(Panel, 101, [{label, "&Countdown"}]),
  27.   B102  = wxButton:new(Panel, ?wxID_EXIT, [{label, "E&xit"}]),
  28.  
  29. %%You can create sizers before or after the widgets that will go into them, but
  30. %%the widgets have to exist before they are added to sizer.
  31.   OuterSizer  = wxBoxSizer:new(?wxHORIZONTAL),
  32.   MainSizer   = wxBoxSizer:new(?wxVERTICAL),
  33.   InputSizer  = wxStaticBoxSizer:new(?wxHORIZONTAL, Panel, [{label, "Enter an integer"}]),
  34.   ButtonSizer = wxBoxSizer:new(?wxHORIZONTAL),
  35.  
  36. %% Note that the widget is added using the VARIABLE, not the ID.
  37. %% The order they are added here controls appearance.
  38.  
  39.   wxSizer:add(InputSizer, T1001, []),
  40.   wxSizer:add(InputSizer, 40, 0, []),
  41.  
  42.   wxSizer:addSpacer(MainSizer, 10),  %spacer
  43.   wxSizer:add(MainSizer, InputSizer,[]),
  44.   wxSizer:addSpacer(MainSizer, 5),  %spacer
  45.  
  46.   wxSizer:add(MainSizer, ST2001, []),
  47.   wxSizer:addSpacer(MainSizer, 10),  %spacer
  48.  
  49.   wxSizer:add(ButtonSizer, B101,  []),
  50.   wxSizer:add(ButtonSizer, B102,  []),
  51.   wxSizer:add(MainSizer, ButtonSizer, []),
  52.  
  53.   wxSizer:addSpacer(OuterSizer, 20), % spacer
  54.   wxSizer:add(OuterSizer, MainSizer, []),
  55.  
  56. %% Now 'set' OuterSizer into the Panel
  57.   wxPanel:setSizer(Panel, OuterSizer),
  58.  
  59.   wxFrame:show(Frame),
  60.  
  61. % create two listeners
  62.   wxFrame:connect( Frame, close_window),
  63.   wxPanel:connect(Panel, command_button_clicked),
  64.  
  65. %% the return value, which is stored in State
  66.   {Frame, T1001, ST2001, self()}.
  67.  
  68.  
  69.  
  70.  
  71. loop(State) ->
  72.   {Frame, T1001, ST2001, Pid} = State,  % break State back down into its components
  73.   io:format("--waiting in the loop--~n", []), % optional, feedback to the shell
  74.   receive
  75.  
  76.   % a connection get the close_window signal
  77.   % and sends this message to the server
  78.     #wx{event=#wxClose{}} ->
  79.       if
  80.         Pid /= self() -> Pid ! { -1 };
  81.         true -> ok
  82.       end,
  83.       io:format("~p Closing window ~n",[self()]), %optional, goes to shell
  84.       %now we use the reference to Frame
  85.       wxWindow:destroy(Frame),  %closes the window
  86.       ok;  % we exit the loop
  87.  
  88.     #wx{id = ?wxID_EXIT, event=#wxCommand{type = command_button_clicked} } ->
  89.       %%     {wx, ?wxID_EXIT, _,_,_} ->
  90.       %this message is sent when the exit button (ID 102) is clicked
  91.       %the other fields in the tuple are not important to us.
  92.       if
  93.         Pid /= self() -> Pid ! { -1 };
  94.         true -> ok
  95.       end,
  96.       io:format("~p Closing window ~n",[self()]), %optional, goes to shell
  97.       wxWindow:destroy(Frame),
  98.       ok;  % we exit the loop
  99.  
  100.     #wx{id = 101, event=#wxCommand{type = command_button_clicked}} ->
  101.       %this message is sent when the Countdown button (ID 101) is clicked
  102.       T1001_val = wxTextCtrl:getValue(T1001),
  103.       case is_valid_list_to_integer(T1001_val) of
  104.         true ->
  105.           T1001_int = list_to_integer(T1001_val),
  106.           MyPid = self(),
  107.           CntdwnPid = spawn( fun() -> cntdwn( T1001_int, MyPid ) end );
  108.         _ ->
  109.           wxStaticText:setLabel(ST2001, "Only integers are allowed"),
  110.           CntdwnPid = Pid
  111.       end,
  112.       loop( {Frame, T1001, ST2001, CntdwnPid } );
  113.     { 0 } ->
  114.       OutputStr = "ZERO",
  115.       wxStaticText:setLabel(ST2001, OutputStr),
  116.       loop( State );
  117.  
  118.     { N } ->
  119.       OutputStr = integer_to_list(N),
  120.       wxStaticText:setLabel(ST2001, OutputStr),
  121.       loop( State );
  122.  
  123.     Msg ->
  124.       %everything else ends up here
  125.       io:format("loop default triggered: Got ~n ~p ~n", [Msg]),
  126.       loop(State)
  127.  
  128.   end.
  129.  
  130.  
  131.  
  132.  
  133. cntdwn( N, Pid ) when N > 0 ->
  134.   % assumes N is an integer
  135.   io:format("~w~n", [N]),
  136.   Pid ! { N },
  137.   receive
  138.     { -1 } -> io:format( "Cancelled~n" ),
  139.       ok
  140.   after 1000 ->
  141.     cntdwn(N-1, Pid)
  142.   end;
  143. cntdwn(_, Pid) ->
  144.   io:format("ZERO~n"),
  145.   Pid ! { 0 },
  146.   ok.
  147.  
  148. is_valid_list_to_integer(Input) ->
  149.   try list_to_integer(Input) of
  150.     _An_integer -> true
  151.   catch
  152.     error: _Reason -> false  %Reason is badarg
  153.   end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement