2009-09-01 118 views
0

我對erlang非常陌生,並且試圖讓我的腦袋圍繞wxerlang,但是碰到了牆壁。有人可以看看這段代碼,告訴我什麼是錯的。我認爲這是非常明顯的,但我無法解決。wxErlang - 這段代碼有什麼問題?


-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. 
+0

這可能有助於你得到實際的錯誤 – 2009-09-02 01:08:36

+1

你也可以嘗試將代碼縮減爲一個較小的例子,它表現出同樣的問題除了使其他人更容易回答你,這是一個很好的調試自己的技巧;一旦你消除了一切無關,其餘代碼中的問題可能會變得更加明顯 – 2009-09-02 01:30:42

+0

我的問題在於handle_event/3的情況NEW_APP newAppDialog運行爲一個新的進程並退出,但沒有任何顯示在框架中,主進程仍在運行,但將退回到命令行。 我很困惑,也許我已經跳到遠處,並將採取在OTP的東西 – Damian 2009-09-02 08:21:44

回答

2

是否收到彙編錯誤?當它運行(我使用ERL 5.7.2在Mac OS X)

更改include_lib線

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

有了它編譯一個改變,我得到了一個空白窗口。這是你所期望的嗎?

如果您是Erlang的新手,可能更容易從更直接的事情開始。理解wx_object手冊頁並不難,但是隻有當你掌握了OTP並且先寫了幾個測試服務器的時候,我纔會首先發表一些愚蠢的觀點。一旦你在這一點上覆蓋wx如何工作,這是一個更簡單的步驟。在同一時間做這兩個將是一個更大的挑戰,但你的里程可能會有所不同,當然......!

+0

我經歷了spawnlink的教程並且現在對erlang有了更好的理解,雖然我顯然還有很長的路要走,但是謝謝你的建議。 http://spawnlink.com/articles/tag/erlybank/ – Damian 2009-09-06 02:15:36

+0

我還沒有看到這些教程,感謝您的鏈接... – 2009-09-06 05:40:42

0

我意識到這是一個老問題,但我注意到,

-behaviour(wx_object)。

拼寫錯誤。 (你的行爲(wx_object))