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     2018-03-29: ref to issiue #231
34     Added colors in first column GdkColor & RGBA.
35     This tests ListStore storing more complex attribute, than simple GType.
36     Jakub Zdroik <jakub.zdroik@gmail.com>
37 
38 *****************************************************************************/
39 module DemoMultiCellRenderer;
40 
41 import gtk.Application;
42 import gtk.ApplicationWindow;
43 import gdk.Event;
44 import gtk.Window;
45 import gtk.Widget;
46 import gtk.TreeIter;
47 import gtk.TreePath;
48 import gtk.TreeView;
49 import gtk.TreeViewColumn;
50 import gtk.CellRendererText;
51 import gtk.CellRendererToggle;
52 import gtk.ListStore;
53 
54 import gdk.RGBA;
55 
56 import gobject.Value;
57 
58 enum {
59     COLUMN_NAME,
60     COLUMN_TEXT,
61     COLUMN_TEXT_VISIBLE,
62     COLUMN_BOOL,
63     COLUMN_BOOL_VISIBLE,
64     COLUMN_TEXT_COLOR_RGBA,
65 }
66 
67 class DemoMultiCellRenderer : ApplicationWindow
68 {
69     this(Application application)
70     {
71         super(application);
72         setTitle("Celleditor Demo");
73 
74         ListStore store = new ListStore( [
75             GType.STRING,
76             GType.STRING,
77             GType.INT,
78             GType.INT,
79             GType.INT,
80             RGBA.getType(),
81             ] );
82 
83         void appendRecord( string name, string value, bool isBoolean, RGBA rgba){
84             auto it = store.createIter();
85             store.setValue( it, COLUMN_NAME, name );
86             store.setValue( it, COLUMN_TEXT, value );
87             store.setValue( it, COLUMN_TEXT_VISIBLE, !isBoolean );
88             store.setValue( it, COLUMN_BOOL, value == "true" );
89             store.setValue( it, COLUMN_BOOL_VISIBLE, isBoolean  );
90             store.setValue( it, COLUMN_TEXT_COLOR_RGBA, rgba );
91         }
92         // fill store with data
93         appendRecord( "Loops", "10", false, new RGBA(1.0,0.0,0.0,1.0) );
94         appendRecord( "Name", "keinfarbton", false, new RGBA(0.0,1.0,0.0,1.0) );
95         appendRecord( "Verbose", "true", true, new RGBA(0.0,0.0,1.0,1.0) );
96 
97         auto tv  = new TreeView();
98         setChild(tv);
99 
100         // create first column with text renderer
101         TreeViewColumn column = new TreeViewColumn();
102         column.setTitle( "Name" );
103         tv.appendColumn(column);
104 
105         CellRendererText cell_text = new CellRendererText();
106         column.packStart(cell_text, 0 );
107         column.addAttribute(cell_text, "text", COLUMN_NAME);
108         column.addAttribute(cell_text, "foreground-rgba", COLUMN_TEXT_COLOR_RGBA);
109 
110         // create second column with two renderers
111         column = new TreeViewColumn();
112         column.setTitle( "Value" );
113         tv.appendColumn(column);
114 
115         CellRendererToggle cell_bool = new CellRendererToggle();
116         column.packStart(cell_bool, 0 );
117         column.addAttribute(cell_bool, "active", COLUMN_BOOL);
118         column.addAttribute(cell_bool, "visible", COLUMN_BOOL_VISIBLE);
119 
120         cell_text = new CellRendererText();
121         column.packStart(cell_text, 0 );
122         column.addAttribute(cell_text, "text", COLUMN_TEXT);
123         column.addAttribute(cell_text, "visible", COLUMN_TEXT_VISIBLE);
124         cell_text.setProperty( "editable", 1 );
125 
126         // change value in store on toggle event
127         cell_bool.addOnToggled( delegate void(string p, CellRendererToggle){
128             auto path = new TreePath( p );
129             TreeIter it;
130             store.getIter(it, path);
131             store.setValue(it, COLUMN_BOOL, store.getValue!int(it, COLUMN_BOOL) ? 0 : 1 );
132         });
133 
134         // change the text in the store on end of edit
135         cell_text.addOnEdited( delegate void(string p, string v, CellRendererText cell ){
136             auto path = new TreePath( p );
137             TreeIter it;
138             store.getIter(it, path);
139             store.setValue( it, COLUMN_TEXT, v );
140         });
141 
142         tv.setModel(store);
143         show();
144     }
145 }
146 
147 
148 int main(string[] args)
149 {
150     auto application = new Application("org.gtkd.demo.multicellrenderer", GApplicationFlags.FLAGS_NONE);
151 	application.addOnActivate(delegate void(_) { new DemoMultiCellRenderer(application); });
152 	return application.run(args);
153 }