wiki:ErlangHackable1

Erlang on hackable:1

Erlang works out of the box on hackable:1
Here are the steps to make a little gtk app :

  • install erlang :
apt-get install erlang
  • install gtknode :

Download gtknode at  http://code.google.com/p/gtknode/ Then :

./configure --prefix=/usr
make
make install

(If you get an error message because distcc doesn't work, take a look at:  http://wiki.openmoko.org/wiki/Development_Environment and if needed edit the distcc address in roots .bashrc)

  • test and enjoy :
%%%-------------------------------------------------------------------
-module(testerl).

-export([start/0]).

-record(widget, {id, name}).

-define(G(C,A),gtknode:cmd(hello,C,A)).

create_button(Winstruct, Label)->
    Id = ?G('Gtk_button_new_with_label',[Label]),
    ?G('Gtk_box_pack_start', [get_vbox(Winstruct), Id, false, false, 5]),
    ?G('Gtk_widget_show',[Id]),
    Widget = #widget{name = "Name", id=Id},
    add_widget_to_struct(Widget, Winstruct).


get_vbox(Winstruct)->
    [Vbox] = dict:fetch(vbox, Winstruct),
    Vbox.
    
    
add_widget_to_struct(Widget, Winstruct) when is_record(Widget, widget)->
    Widgets = dict:fetch(widgets, Winstruct),
    dict:store(widgets, [Widget|Widgets], Winstruct). 

add_to_struct([], Dict)->
    Dict;
add_to_struct([{Key, Value}|Tail], Dict)->
    DictBis = dict:append(Key, Value, Dict),
    add_to_struct(Tail, DictBis).

start() ->
    gtknode:start(hello),
    Win = ?G('Gtk_window_new',['GTK_WINDOW_TOPLEVEL']),
    ScrolledWindow = ?G('Gtk_scrolled_window_new', [ 'NULL', 'NULL']),
    ?G('Gtk_container_add',[Win,ScrolledWindow]),
    ?G('Gtk_scrolled_window_set_policy', [ScrolledWindow, 'GTK_POLICY_NEVER', 'GTK_POLICY_ALWAYS']),

    Viewport = ?G('Gtk_viewport_new', ['NULL', 'NULL']),
    ?G('Gtk_container_add',[ScrolledWindow, Viewport]),
 
    Vbox = ?G('Gtk_vbox_new', [false, 0]),
    io:fwrite("Vbox = ~p~n", [Vbox]),
    ?G('Gtk_container_add',[Viewport,Vbox]),
 
    But = ?G('Gtk_button_new_with_label',["butté"]),
    
    ?G('Gtk_widget_set_name', [But, "button"]),
    ?G('Gtk_box_pack_start', [Vbox, But, false, false, 5]),
    
    ?G('Gtk_widget_show',[Win]),
    ?G('Gtk_widget_show',[ScrolledWindow]),    
    ?G('Gtk_widget_show',[Viewport]),
    ?G('Gtk_widget_show',[Vbox]),
    ?G('Gtk_widget_show',[But]),
    ?G('GN_signal_connect',[But,clicked]),
    ?G('GN_signal_connect',[Win,destroy]),
   
    Winstruct = add_to_struct([{window, Win}, {vbox, Vbox}, {widgets, []}], dict:new()),
    loop(Winstruct, But).


loop(Winstruct, But) ->
    receive
        {hello,{signal,{But,clicked}}} ->
            io:fwrite("widget : ~p  ~p~n",[But, clicked]),
            io:fwrite("winstruct = ~p~n", [Winstruct]),
            WinstructB = create_button(Winstruct, "toto va a la plage"),
            loop(WinstructB, But);
        {hello,{signal,{_,destroy}}}  ->
            gtknode:stop(hello)
    end.
erl -sname anode@localhost
(anode@localhost)1> c(testerl).
(anode@localhost)2> testerl:start().