Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

treeview example finished

git-svn-id: https://gtknode.googlecode.com/svn/trunk@23 9999ab98-4a1b-0410-ba6a-d90168ca9a37
  • Loading branch information...
commit 419aedd898e17079a99c5b484fa1636a3f05666d 1 parent 8d6ef0e
@massemanet authored
View
118 priv/examples/treeview/treeview.erl
@@ -7,9 +7,26 @@
%%%-------------------------------------------------------------------
-module(treeview).
-export([start/0,stop/0]).
+-export([list_insert/2,tree_insert/2]).
+
-import(filename,[dirname/1,join/1]).
--record(ld,{}).
+-import(lists,[flatten/1,foldl/3]).
+-record(ld,{col_list1,col_list2,col_tree1,col_tree2,tree_store,list_store}).
+
+list_data() ->
+ [[1,"one"],
+ [2,"two"]].
+
+tree_data() ->
+ [{["one",1],
+ [{["sub1",666],
+ [{["sub2_1",667],
+ []},
+ {["sub2_2",668],
+ []}]}]},
+ {["two",2],[]}].
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
start() ->
case whereis(?MODULE) of
undefined -> spawn(fun init/0);
@@ -23,18 +40,109 @@ init() ->
gtknode:start(?MODULE),
GladeFile = join([dirname(code:which(?MODULE)),?MODULE])++".glade",
g('GN_glade_init',[GladeFile]),
- loop(#ld{}).
+ LD = #ld{col_list1=init_tree_view_column(0,"list_1"),
+ col_list2=init_tree_view_column(1,"list_1"),
+ col_tree1=init_tree_view_column(0,"tree_1"),
+ col_tree2=init_tree_view_column(1,"tree_1"),
+ tree_store=new_tree_store([string,integer]),
+ list_store=new_list_store([integer,string])},
+ loop(tree_view(LD)).
loop(LD) ->
receive
- {?MODULE,{signal,{button_quit,'clicked'}}} ->quit();
- {?MODULE,{signal,{window,'delete-event'}}} ->quit();
- quit ->quit();
+ {?MODULE,{signal,{radiobutton_list,toggled}}}-> loop(tree_view(LD));
+ {?MODULE,{signal,{button_insert,clicked}}} -> loop(insert(LD));
+ {?MODULE,{signal,{window,'delete-event'}}} -> quit();
+ {?MODULE,{signal,{button_quit,'clicked'}}} -> quit();
+ quit -> quit();
X -> io:fwrite("got ~p~n",[X]),loop(LD)
end.
quit() -> gtknode:stop(?MODULE).
+insert(LD) ->
+ case g('Gtk_toggle_button_get_active',[radiobutton_list]) of
+ true -> list_insert(LD#ld.list_store,list_data());
+ false-> tree_insert(LD#ld.tree_store,tree_data())
+ end,
+ LD.
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+tree_view(LD) ->
+ {Store,Cols} = get_store_data(LD),
+ g('Gtk_tree_view_set_model',[treeview,Store]),
+ remove_cols(),
+ [g('Gtk_tree_view_append_column',[treeview,C]) || C <- Cols],
+ LD.
+
+remove_cols() ->
+ case g('Gtk_tree_view_get_column',[treeview,0]) of
+ 'NULL' -> ok;
+ Col -> g('Gtk_tree_view_remove_column',[treeview,Col]),remove_cols()
+ end.
+
+get_store_data(LD) ->
+ case g('Gtk_toggle_button_get_active',[radiobutton_list]) of
+ true -> {LD#ld.list_store,[LD#ld.col_list1,LD#ld.col_list2]};
+ false-> {LD#ld.tree_store,[LD#ld.col_tree1,LD#ld.col_tree2]}
+ end.
+
+init_tree_view_column(DataCol,Title) ->
+ Col = g('Gtk_tree_view_column_new',[]),
+ Renderer = g('Gtk_cell_renderer_text_new',[]),
+ g('Gtk_tree_view_column_pack_start',[Col,Renderer,false]),
+ g('Gtk_tree_view_column_set_title', [Col,Title]),
+ g('Gtk_tree_view_column_set_resizable',[Col,true]),
+ g('Gtk_tree_view_column_add_attribute',[Col,Renderer,"text",DataCol]),
+ Col.
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+new_tree_store(Cols)->
+ g('Gtk_tree_store_newv',[length(Cols),Cols]).
+
+tree_insert(Store,Rows) ->
+ g(flatten(tree_insert(Store,[0],Rows))).
+
+tree_insert(_Store,_Path,[]) -> [];
+tree_insert(Store,Path,[{Row,SubTree}|T]) ->
+ [update_iter(Path, Store),
+ tree_insert_row(Store,0,Row),
+ tree_insert(Store,[0|Path],SubTree),
+ tree_insert(Store,[hd(Path)+1|tl(Path)],T)].
+
+tree_insert_row(_Store,_N,[]) -> [];
+tree_insert_row(Store,N,[Col|Cols]) ->
+ [tree_insert_val(Store,N,Col),
+ tree_insert_row(Store,N+1,Cols)].
+
+tree_insert_val(Store,N,Val) ->
+ [{'GN_value_set',[val,Val]},
+ {'Gtk_tree_store_set_value', [Store,iter,N,val]}].
+
+update_iter([I], Store) ->
+ {'Gtk_tree_store_insert',[Store,iter,'NULL',I]};
+update_iter([I|D], Store) ->
+ [{'Gtk_tree_model_get_iter_from_string',[Store,daddy,pth(D)]},
+ {'Gtk_tree_store_insert',[Store,iter,daddy,I]}].
+
+pth(P) -> pth(P,[]).
+pth([I],O) -> [$0+I|O];
+pth([H|T],O) -> pth(T,[$:,$0+H|O]).
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+new_list_store(Cols)->
+ g('Gtk_list_store_newv',[length(Cols),Cols]).
+
+list_insert(Store,Rows) ->
+ Row_f = fun() -> {'Gtk_list_store_append',[Store,iter]} end,
+ Col_f = fun(Row) -> foldl(fun col_f/2, {0,Store,[]}, Row) end,
+ g(flatten([[Row_f()|element(3,Col_f(Row))] || Row<-Rows])).
+
+col_f(Val,{N,Store,O}) ->
+ {N+1,Store,
+ [{'GN_value_set',[val,Val]},
+ {'Gtk_list_store_set_value',[Store,iter,N,val]}|O]}.
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
g(C,As) -> g([{C,As}]).
g(CAs) ->
case gtknode:cmd(?MODULE,CAs) of
View
146 priv/examples/treeview/treeview.glade
@@ -21,16 +21,144 @@
<signal name="delete_event" handler="erl" last_modification_time="Tue, 21 Nov 2006 10:06:52 GMT"/>
<child>
- <widget class="GtkTreeView" id="treeview">
+ <widget class="GtkVBox" id="vbox1">
<property name="visible">True</property>
- <property name="can_focus">True</property>
- <property name="headers_visible">False</property>
- <property name="rules_hint">False</property>
- <property name="reorderable">False</property>
- <property name="enable_search">True</property>
- <property name="fixed_height_mode">False</property>
- <property name="hover_selection">False</property>
- <property name="hover_expand">False</property>
+ <property name="homogeneous">False</property>
+ <property name="spacing">0</property>
+
+ <child>
+ <widget class="GtkTreeView" id="treeview">
+ <property name="visible">True</property>
+ <property name="can_focus">True</property>
+ <property name="headers_visible">True</property>
+ <property name="rules_hint">False</property>
+ <property name="reorderable">False</property>
+ <property name="enable_search">True</property>
+ <property name="fixed_height_mode">False</property>
+ <property name="hover_selection">False</property>
+ <property name="hover_expand">False</property>
+ </widget>
+ <packing>
+ <property name="padding">0</property>
+ <property name="expand">True</property>
+ <property name="fill">True</property>
+ </packing>
+ </child>
+
+ <child>
+ <widget class="GtkHBox" id="hbox1">
+ <property name="visible">True</property>
+ <property name="homogeneous">False</property>
+ <property name="spacing">0</property>
+
+ <child>
+ <widget class="GtkRadioButton" id="radiobutton_list">
+ <property name="visible">True</property>
+ <property name="can_focus">True</property>
+ <property name="label" translatable="yes">list</property>
+ <property name="use_underline">True</property>
+ <property name="relief">GTK_RELIEF_NORMAL</property>
+ <property name="focus_on_click">True</property>
+ <property name="active">False</property>
+ <property name="inconsistent">False</property>
+ <property name="draw_indicator">True</property>
+ <signal name="toggled" handler="erl" last_modification_time="Tue, 21 Nov 2006 12:52:00 GMT"/>
+ </widget>
+ <packing>
+ <property name="padding">0</property>
+ <property name="expand">False</property>
+ <property name="fill">False</property>
+ </packing>
+ </child>
+
+ <child>
+ <widget class="GtkRadioButton" id="radiobutton_tree">
+ <property name="visible">True</property>
+ <property name="can_focus">True</property>
+ <property name="label" translatable="yes">tree</property>
+ <property name="use_underline">True</property>
+ <property name="relief">GTK_RELIEF_NORMAL</property>
+ <property name="focus_on_click">True</property>
+ <property name="active">False</property>
+ <property name="inconsistent">False</property>
+ <property name="draw_indicator">True</property>
+ <property name="group">radiobutton_list</property>
+ </widget>
+ <packing>
+ <property name="padding">0</property>
+ <property name="expand">False</property>
+ <property name="fill">False</property>
+ </packing>
+ </child>
+
+ <child>
+ <widget class="GtkLabel" id="label1">
+ <property name="visible">True</property>
+ <property name="label" translatable="yes"></property>
+ <property name="use_underline">False</property>
+ <property name="use_markup">False</property>
+ <property name="justify">GTK_JUSTIFY_LEFT</property>
+ <property name="wrap">False</property>
+ <property name="selectable">False</property>
+ <property name="xalign">0.5</property>
+ <property name="yalign">0.5</property>
+ <property name="xpad">0</property>
+ <property name="ypad">0</property>
+ <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
+ <property name="width_chars">-1</property>
+ <property name="single_line_mode">False</property>
+ <property name="angle">0</property>
+ </widget>
+ <packing>
+ <property name="padding">0</property>
+ <property name="expand">True</property>
+ <property name="fill">True</property>
+ </packing>
+ </child>
+
+ <child>
+ <widget class="GtkButton" id="button_insert">
+ <property name="border_width">6</property>
+ <property name="visible">True</property>
+ <property name="can_focus">True</property>
+ <property name="label" translatable="yes">Insert</property>
+ <property name="use_underline">True</property>
+ <property name="relief">GTK_RELIEF_NORMAL</property>
+ <property name="focus_on_click">True</property>
+ <signal name="clicked" handler="erl" last_modification_time="Tue, 21 Nov 2006 14:40:15 GMT"/>
+ </widget>
+ <packing>
+ <property name="padding">0</property>
+ <property name="expand">False</property>
+ <property name="fill">False</property>
+ </packing>
+ </child>
+
+ <child>
+ <widget class="GtkButton" id="button_quit">
+ <property name="border_width">6</property>
+ <property name="visible">True</property>
+ <property name="can_focus">True</property>
+ <property name="label" translatable="yes">Quit</property>
+ <property name="use_underline">True</property>
+ <property name="relief">GTK_RELIEF_NORMAL</property>
+ <property name="focus_on_click">True</property>
+ <signal name="clicked" handler="erl" last_modification_time="Tue, 21 Nov 2006 13:26:26 GMT"/>
+ </widget>
+ <packing>
+ <property name="padding">0</property>
+ <property name="expand">False</property>
+ <property name="fill">False</property>
+ <property name="pack_type">GTK_PACK_END</property>
+ </packing>
+ </child>
+ </widget>
+ <packing>
+ <property name="padding">0</property>
+ <property name="expand">False</property>
+ <property name="fill">False</property>
+ </packing>
+ </child>
</widget>
</child>
</widget>
Please sign in to comment.
Something went wrong with that request. Please try again.