Search code examples
erlangwxwidgets

wxErlang - What is wrong with this code?


I am very new to erlang and am trying to get my head around wxerlang but have hit a wall. Can someone have a look at this code and tell me what is wrong. I think it is something very obvious, but I just can't work it out.


-module(main).

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

-behavoiur(wx_object).
-export([start/0]).  %% API
-export([init/1, handle_call/3, handle_event/2, handle_info/2, terminate/2]). %% Call Backs

-record(state, {win, action}).

-define(NEW_APP, 101).

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

init(Options) ->
    wx:new(Options),
    Frame = wxFrame:new(wx:null(), ?wxID_ANY, "Rails IDE", [{size,{1000,500}}]),

    MB = wxMenuBar:new(),
    wxFrame:setMenuBar(Frame,MB),
    File    = wxMenu:new([]),
    wxMenu:append(File, ?NEW_APP, "&New"),
    wxMenu:append(File, ?wxID_EXIT, "&Quit"),

    wxMenuBar:append(MB, File, "&File"),

    wxFrame:connect(Frame, command_menu_selected),

    _SB = wxFrame:createStatusBar(Frame,[]),

    MainSplitter = wxSplitterWindow:new(Frame, []),
    LeftSplitter = wxSplitterWindow:new(MainSplitter, []),
    CenterSplitter = wxSplitterWindow:new(MainSplitter, []),
    RightSplitter = wxSplitterWindow:new(MainSplitter, []),
    BottomSplitter = wxSplitterWindow:new(MainSplitter, []),

    wxSplitterWindow:setMinimumPaneSize(MainSplitter, 1),
    wxSplitterWindow:setMinimumPaneSize(LeftSplitter, 1),
    wxSplitterWindow:setMinimumPaneSize(CenterSplitter, 1),
    wxSplitterWindow:setMinimumPaneSize(RightSplitter, 1),
    wxSplitterWindow:setMinimumPaneSize(BottomSplitter, 1),

    wxFrame:show(Frame),

    State = #state{win=Frame},
    {Frame, State}.

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

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

handle_event(#wx{id = Id,
                 event = #wxCommand{type = command_menu_selected}},
             State = #state{}) ->
    case Id of
        ?NEW_APP ->
            Panel = newAppDialog(State#state.win),
            {noreply,  State#state{action=Panel}};
        ?wxID_EXIT ->
            {stop, normal, State};
        _ ->
            {noreply, State}
    end;

handle_event(Ev,State) ->
    io:format("~p Got event ~p ~n",[?MODULE, Ev]),
    {noreply, State}.

terminate(_Reason, _State) ->
    wx:destroy().


newAppDialog(Frame) ->
    Panel = wxPanel:new(Frame, []),

    %% Setup sizers
    MainSizer = wxBoxSizer:new(?wxVERTICAL),
    SubSizer = wxStaticBoxSizer:new(?wxVERTICAL, Panel, [{label, "Create a new Rails app."}]),

    Label1 = wxStaticText:new(Panel, 1, "App root"),
    DirPicker = wxDirPickerCtrl:new(Panel, 2,
                                    [{path, "/"},
                                     {style, ?wxDIRP_USE_TEXTCTRL},
                                     {message, "Select app root"}]),
    Label2 = wxStaticText:new(Panel, 3, "App name"),
    TextCtrl = wxTextCtrl:new(Panel, 4),
    Button = wxButton:new(Panel, ?wxID_OK),

    %% Add to sizers
            PickerOptions = [{border, 4},{flag, ?wxALL bor ?wxEXPAND}],
    wxSizer:add(SubSizer, Label1, PickerOptions ),
    wxSizer:add(SubSizer, DirPicker, PickerOptions ),
    wxSizer:add(SubSizer, Label2, PickerOptions ),
    wxSizer:add(SubSizer, TextCtrl, PickerOptions),
    wxSizer:add(SubSizer, Button, PickerOptions),

    SizerOptions  = [{flag, ?wxEXPAND}],
    wxSizer:add(MainSizer, SubSizer, SizerOptions),

    wxWindow:connect(Panel, command_button_clicked),
    wxPanel:setSizer(Panel, MainSizer),
    wxSizer:layout(MainSizer),
    Panel.

Solution

  • Do you get a compilation error?

    Change the include_lib line to

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

    With that change it compiles and I get a blank window when it is run (I'm using erl 5.7.2 on Mac OS X). Is that what you expect?

    If you're new to Erlang it's probably easier to start with something more straightforward. It's not too difficult to understand the wx_object man page but only once you've got a handle on OTP and have written a couple of test servers first in my humble opinion. Once you're at that point overlaying how wx works on top of that is a more simple step. Doing both at the same time will be more of a challenge, but your mileage may vary of course...!