1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * gtkD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with gtkD; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
17  */
18 
19 
20 /*****************************************************************************
21 
22     Authors: Frank Benoit <keinfarbton@googlemail.com>
23              muntyan #gtk+
24 
25     This demo shows a table with key/value pairs. The values can be edited.
26     There are two types of values: strings and bools. Dependent of the type,
27     either a CellRendererText or CellRendererToggle is used to show and edit
28     the value.
29 
30     This is done by connecting the visibility of a CellRenderer to a
31     ListStore column.
32 
33 *****************************************************************************/
34 module DemoMultiCellRenderer;
35 
36 import gtk.Main;
37 import gdk.Event;
38 import gtk.Window;
39 import gtk.Widget;
40 import gtk.TreeIter;
41 import gtk.TreePath;
42 import gtk.TreeView;
43 import gtk.TreeViewColumn;
44 import gtk.CellRendererText;
45 import gtk.CellRendererToggle;
46 import gtk.ListStore;
47 
48 enum {
49     COLUMN_NAME,
50     COLUMN_TEXT,
51     COLUMN_TEXT_VISIBLE,
52     COLUMN_BOOL,
53     COLUMN_BOOL_VISIBLE
54 }
55 
56 void main(){
57     Main.init(null);
58     ListStore store = new ListStore( [
59         GType.STRING,
60         GType.STRING,
61         GType.INT,
62         GType.INT,
63         GType.INT] );
64 
65     void appendRecord( string name, string value, bool isBoolean ){
66         auto it = store.createIter();
67         store.setValue( it, COLUMN_NAME, name );
68         store.setValue( it, COLUMN_TEXT, value );
69         store.setValue( it, COLUMN_TEXT_VISIBLE, !isBoolean );
70         store.setValue( it, COLUMN_BOOL, value == "true" );
71         store.setValue( it, COLUMN_BOOL_VISIBLE, isBoolean  );
72     }
73     // fill store with data
74     appendRecord( "Loops", "10", false );
75     appendRecord( "Name", "keinfarbton", false );
76     appendRecord( "Verbose", "true", true );
77 
78     auto wnd = new Window( "Celleditor Demo" );
79     auto tv  = new TreeView();
80     wnd.add(tv);
81 
82     // create first column with text renderer
83     TreeViewColumn column = new TreeViewColumn();
84     column.setTitle( "Name" );
85     tv.appendColumn(column);
86 
87     CellRendererText cell_text = new CellRendererText();
88     column.packStart(cell_text, 0 );
89     column.addAttribute(cell_text, "text", COLUMN_NAME);
90 
91     // create second column with two renderers
92     column = new TreeViewColumn();
93     column.setTitle( "Value" );
94     tv.appendColumn(column);
95 
96     CellRendererToggle cell_bool = new CellRendererToggle();
97     column.packStart(cell_bool, 0 );
98     column.addAttribute(cell_bool, "active", COLUMN_BOOL);
99     column.addAttribute(cell_bool, "visible", COLUMN_BOOL_VISIBLE);
100 
101     cell_text = new CellRendererText();
102     column.packStart(cell_text, 0 );
103     column.addAttribute(cell_text, "text", COLUMN_TEXT);
104     column.addAttribute(cell_text, "visible", COLUMN_TEXT_VISIBLE);
105     cell_text.setProperty( "editable", 1 );
106 
107     // change value in store on toggle event
108     cell_bool.addOnToggled( delegate void(string p, CellRendererToggle){
109         auto path = new TreePath( p );
110         auto it = new TreeIter( store, path );
111         store.setValue(it, COLUMN_BOOL, it.getValueInt( COLUMN_BOOL ) ? 0 : 1 );
112     });
113 
114     // change the text in the store on end of edit
115     cell_text.addOnEdited( delegate void(string p, string v, CellRendererText cell ){
116         auto path = new TreePath( p );
117         auto it = new TreeIter( store, path );
118         store.setValue( it, COLUMN_TEXT, v );
119     });
120 
121     tv.setModel(store);
122     wnd.showAll();
123 
124     wnd.addOnDelete( delegate bool (Event event, Widget widget) {
125         widget.destroy();
126         Main.quit();
127         return false;
128     });
129 
130     Main.run();
131 }
132