These are my wxErlang study file.
- box_sizer_test.erl - BoxSizer
- countdown_gui.erl - Count down timer
- draw_rect.erl - Draw green rectangle on a panel
| *.beam |
These are my wxErlang study file.
| -module(box_sizer_test). | |
| -export([start/0]). | |
| -include_lib("wx/include/wx.hrl"). | |
| start() -> | |
| wx:new(), | |
| Frame = wxFrame:new(wx:null(), ?wxID_ANY, "BoxSizer"), | |
| Button1 = wxButton:new(Frame, ?wxID_ANY, [{label, "Button 1"}]), | |
| Button2 = wxButton:new(Frame, ?wxID_ANY, [{label, "Button 2"}]), | |
| MainSizer = wxBoxSizer:new(?wxVERTICAL), | |
| wxSizer:add(MainSizer, Button1, [{flag, ?wxEXPAND}, {proportion, 1}]), | |
| wxSizer:add(MainSizer, Button2, [{flag, ?wxBOTTOM bor ?wxEXPAND}]), | |
| wxWindow:setSizer(Frame, MainSizer), | |
| wxFrame:show(Frame). |
| -module(buffered_paint). | |
| -export([start/0]). | |
| -include_lib("wx/include/wx.hrl"). | |
| start() -> | |
| wx:new(), | |
| Frame = wxFrame:new(wx:null(), ?wxID_ANY, "buffered_paint", [{size, {250, 200}}]), | |
| Buffer = init_buffer(Frame), | |
| wxFrame:connect(Frame, close_window), | |
| wxFrame:connect(Frame, paint), | |
| wxFrame:connect(Frame, size), | |
| wxFrame:show(Frame), | |
| main_loop(Frame, Buffer, 0, 100), | |
| wx:destroy(), | |
| ok. | |
| main_loop(Frame, Buffer, X, Y) -> | |
| receive | |
| #wx{event = #wxClose{}} -> | |
| wxWindow:destroy(Frame); | |
| #wx{obj = Obj, event = #wxPaint{}} -> | |
| DC = wxBufferedDC:new(wxClientDC:new(Obj), [{buffer, Buffer}]), | |
| wxBufferedDC:destroy(DC), | |
| main_loop(Frame, Buffer, X, Y); | |
| #wx{obj = Obj, event = #wxSize{}} -> | |
| NewBuffer = init_buffer(Obj), | |
| %wxFrame:refresh(Obj), | |
| main_loop(Frame, NewBuffer, X, Y); | |
| Event -> | |
| io:format("Receive event: ~w~n", [Event]), | |
| main_loop(Frame, Buffer, X, Y) | |
| end. | |
| init_buffer(Frame) -> | |
| {W, H} = wxFrame:getClientSize(Frame), | |
| Buffer = wxBitmap:new(W, H), | |
| draw_to_buffer(Frame, Buffer), | |
| Buffer. | |
| draw_to_buffer(Frame, Buffer) -> | |
| DC = wxBufferedDC:new(wxClientDC:new(Frame), [{buffer, Buffer}]), | |
| wxDC:setBackground(DC, ?wxWHITE_BRUSH), | |
| wxDC:clear(DC), | |
| wxDC:drawText(DC, "Hoge hoge", {10, 10}), | |
| wxDC:drawLine(DC, {10, 10}, {200, 10}), | |
| {W, H} = wxDC:getSize(DC), | |
| wxDC:setPen(DC, wxPen:new(?wxBLUE, [{width, 3}])), | |
| wxDC:drawCircle(DC, {W div 2, H div 2}, 50), | |
| wxBufferedDC:destroy(DC). |
| %% this sample is from https://arifishaq.files.wordpress.com/2017/12/wxerlang-getting-started.pdf | |
| -module(countdown_gui). | |
| -export([start/0]). | |
| -export([handle_click/2, update_gui/3]). | |
| -include_lib("wx/include/wx.hrl"). | |
| start() -> | |
| wx:new(), | |
| Frame = wxFrame:new(wx:null(), ?wxID_ANY, "Countdown"), | |
| %% build and layout the GUI components | |
| Label = wxStaticText:new(Frame, ?wxID_ANY, "Seconds remaining"), | |
| Counter = wxTextCtrl:new(Frame, ?wxID_ANY, [{value, "42"}, {size, {150, 50}}]), | |
| Font = wxFont:new(42, ?wxFONTFAMILY_DEFAULT, ?wxFONTSTYLE_NORMAL, ?wxFONTWEIGHT_BOLD), | |
| wxTextCtrl:setFont(Counter, Font), | |
| Button = wxButton:new(Frame, ?wxID_ANY, [{label, "Start"}, {pos, {0, 64}}, {size, {150, 50}}]), | |
| MainSizer = wxBoxSizer:new(?wxVERTICAL), | |
| wxSizer:add(MainSizer, Label, [{flag, ?wxALL bor ?wxALIGN_CENTER}, {border, 5}]), | |
| wxSizer:add(MainSizer, Counter, [{flag, ?wxEXPAND bor ?wxALL}, {border, 5}]), | |
| wxSizer:add(MainSizer, Button, [{flag, ?wxEXPAND bor ?wxALIGN_BOTTOM}, {border, 5}]), | |
| wxWindow:setSizer(Frame, MainSizer), | |
| wxSizer:setSizeHints(MainSizer, Frame), | |
| wxButton:connect(Button, command_button_clicked, [{callback, fun handle_click/2}, {userData, #{counter => Counter, env => wx:get_env()}}]), | |
| wxFrame:show(Frame). | |
| handle_click(#wx{obj = Button, userData = #{counter := Counter, env := Env}}, | |
| _Event) -> | |
| wx:set_env(Env), | |
| Label = wxButton:getLabel(Button), | |
| case list_to_integer(wxTextCtrl:getValue(Counter)) of | |
| 0 when Label =:= "Start" -> | |
| ok; | |
| _ when Label =:= "Start" -> | |
| wxTextCtrl:setEditable(Counter, false), | |
| wxButton:setLabel(Button, "Stop"), | |
| timer:apply_after(1000, ?MODULE, update_gui, [Counter, Button, Env]); | |
| _ when Label =:= "Stop" -> | |
| wxTextCtrl:setEditable(Counter, true), | |
| wxButton:setLabel(Button, "Start") | |
| end. | |
| update_gui(Counter, Button, Env) -> | |
| wx:set_env(Env), | |
| case wxButton:getLabel(Button) of | |
| "Stop" -> | |
| Value = wxTextCtrl:getValue(Counter), | |
| case list_to_integer(Value) of | |
| 1 -> | |
| wxTextCtrl:setValue(Counter, "0"), | |
| wxTextCtrl:setEditable(Counter, true), | |
| wxButton:setLabel(Button, "Start"); | |
| N -> | |
| wxTextCtrl:setValue(Counter, integer_to_list(N - 1)), | |
| timer:apply_after(1000, ?MODULE, update_gui, [Counter, Button, Env]) | |
| end; | |
| "Start" -> | |
| ok | |
| end. |
| -module(draw_rect). | |
| -export([start/0]). | |
| -include_lib("wx/include/wx.hrl"). | |
| start() -> | |
| wx:new(), | |
| Frame = wxFrame:new(wx:null(), ?wxID_ANY, "draw rect"), | |
| Panel = wxPanel:new(Frame, [{size, {300, 300}}]), | |
| wxPanel:connect(Panel, paint, [{callback, fun on_paint/2}]), | |
| wxFrame:show(Frame). | |
| on_paint(#wx{obj = Panel}, _Event) -> | |
| DC = wxPaintDC:new(Panel), | |
| Pen = wxPen:new({0, 255, 0}), | |
| wxPaintDC:setPen(DC, Pen), | |
| Brush = wxBrush:new({0, 255, 0}), | |
| wxPaintDC:setBrush(DC, Brush), | |
| wxPaintDC:drawRectangle(DC, {100, 100, 100, 100}). |
| -module(key_loop). | |
| -export([start/0]). | |
| -include_lib("wx/include/wx.hrl"). | |
| start() -> | |
| wx:new(), | |
| Frame = wxFrame:new(wx:null(), ?wxID_ANY, "draw rect"), | |
| Panel = wxPanel:new(Frame, [{size, {300, 300}}]), | |
| wxFrame:connect(Frame, close_window), | |
| wxPanel:connect(Panel, paint), | |
| wxFrame:show(Frame), | |
| main_loop(Frame, Panel, 0, 100), | |
| wx:destroy(), | |
| ok. | |
| main_loop(Frame, Panel, X, Y) -> | |
| receive | |
| #wx{event = #wxClose{}} -> | |
| wxWindow:destroy(Frame); | |
| #wx{obj = Obj, event = #wxPaint{}} -> | |
| draw_rect(Obj, X, Y), | |
| main_loop(Frame, Panel, X, Y); | |
| Event -> | |
| io:format("Receive event: ~w~n", [Event]), | |
| main_loop(Frame, Panel, X, Y) | |
| after | |
| 50 -> | |
| Jpressed = wx_misc:getKeyState($J), | |
| Kpressed = wx_misc:getKeyState($K), | |
| NextY = if | |
| Jpressed -> Y + 10; | |
| Kpressed -> Y - 10; | |
| true -> Y | |
| end, | |
| draw_rect(Panel, X, NextY), | |
| main_loop(Frame, Panel, (X + 10) rem 300, NextY) | |
| end. | |
| draw_rect(Panel, X, Y) -> | |
| DC = wxClientDC:new(Panel), | |
| wxDC:clear(DC), | |
| Pen = wxPen:new({0, 255, 0}), | |
| wxDC:setPen(DC, Pen), | |
| Brush = wxBrush:new({0, 255, 0}), | |
| wxDC:setBrush(DC, Brush), | |
| wxDC:drawRectangle(DC, {X, Y, 16, 16}), | |
| wxClientDC:destroy(DC). |
| % This program is ported from python sample in https://bty.sakura.ne.jp/wp/archives/42. | |
| % This program uses Japanese characters. | |
| -module(listctrl). | |
| -export([start/0]). | |
| -include_lib("wx/include/wx.hrl"). | |
| start() -> | |
| Captions = ["都道府県", "男女計", "男", "女"], | |
| Data = [ | |
| ["北海道", "5570", "2638", "2933"], | |
| ["青森県", "1,407", "663", "744"], | |
| ["岩手県", "1,364", "652", "712"], | |
| ["宮城県", "2,347", "1,140", "1,208"], | |
| ["秋田県", "1,121", "527", "593"], | |
| ["山形県", "1,198", "575", "623"], | |
| ["福島県", "2,067", "1,004", "1,063"], | |
| ["茨城県", "2,969", "1,477", "1,492"], | |
| ["栃木県", "2,014", "1,001", "1,013"], | |
| ["群馬県", "2,016", "993", "1,024"], | |
| ["埼玉県", "7,090", "3,570", "3,520"], | |
| ["千葉県", "6,098", "3,047", "3,051"], | |
| ["東京都", "12,758", "6,354", "6,405"], | |
| ["神奈川県", "8,880", "4,484", "4,396"] | |
| ], | |
| wx:new(), | |
| Frame = wxFrame:new(wx:null(), ?wxID_ANY, "ListCtrl test"), | |
| ListCtrl = wxListCtrl:new(Frame, [{style, ?wxLC_REPORT}]), | |
| listctrl_add_Captions(ListCtrl, Captions), | |
| listctrl_set_data(ListCtrl, Data), | |
| wxFrame:show(Frame), | |
| ok. | |
| listctrl_add_Captions(ListCtrl, Captions) -> | |
| lists:foldl( | |
| fun(Caption, Column) -> | |
| wxListCtrl:insertColumn(ListCtrl, Column, Caption), | |
| Column + 1 | |
| end, | |
| 0, | |
| Captions | |
| ). | |
| listctrl_set_data(ListCtrl, Data) -> | |
| lists:foldl( | |
| fun(Datum, Row) -> | |
| wxListCtrl:insertItem(ListCtrl, Row, hd(Datum)), | |
| lists:foldl( | |
| fun(Num, Column) -> | |
| wxListCtrl:setItem(ListCtrl, Row, Column, Num), | |
| Column + 1 | |
| end, | |
| 1, | |
| tl(Datum) | |
| ), | |
| Row + 1 | |
| end, | |
| 0, | |
| Data | |
| ). |