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.Main;
42 import gdk.Event;
43 import gtk.Window;
44 import gtk.Widget;
45 import gtk.TreeIter;
46 import gtk.TreePath;
47 import gtk.TreeView;
48 import gtk.TreeViewColumn;
49 import gtk.CellRendererText;
50 import gtk.CellRendererToggle;
51 import gtk.ListStore;
52 
53 import gdk.RGBA;
54 import gdk.Color;
55 
56 enum {
57     COLUMN_NAME,
58     COLUMN_TEXT,
59     COLUMN_TEXT_VISIBLE,
60     COLUMN_BOOL,
61     COLUMN_BOOL_VISIBLE,
62     COLUMN_TEXT_COLOR,
63     COLUMN_TEXT_COLOR_RGBA,
64 }
65 
66 void main(string[] args){
67     Main.init(args);
68     ListStore store = new ListStore( [
69         GType.STRING,
70         GType.STRING,
71         GType.INT,
72         GType.INT,
73         GType.INT,
74         Color.getType(),
75         RGBA.getType(),
76         ] );
77 
78     void appendRecord( string name, string value, bool isBoolean, RGBA rgba, Color color ){
79         auto it = store.createIter();
80         store.setValue( it, COLUMN_NAME, name );
81         store.setValue( it, COLUMN_TEXT, value );
82         store.setValue( it, COLUMN_TEXT_VISIBLE, !isBoolean );
83         store.setValue( it, COLUMN_BOOL, value == "true" );
84         store.setValue( it, COLUMN_BOOL_VISIBLE, isBoolean  );
85         store.setValue( it, COLUMN_TEXT_COLOR_RGBA, rgba );
86         store.setValue( it, COLUMN_TEXT_COLOR, color );
87     }
88     // fill store with data
89     appendRecord( "Loops", "10", false, new RGBA(1.0,0.0,0.0,1.0), new Color(64,64,64) );
90     appendRecord( "Name", "keinfarbton", false, new RGBA(0.0,1.0,0.0,1.0), new Color(127,127,127) );
91     appendRecord( "Verbose", "true", true, new RGBA(0.0,0.0,1.0,1.0), new Color(200,200,200) );
92 
93     auto wnd = new Window( "Celleditor Demo" );
94     auto tv  = new TreeView();
95     wnd.add(tv);
96 
97     // create first column with text renderer
98     TreeViewColumn column = new TreeViewColumn();
99     column.setTitle( "Name" );
100     tv.appendColumn(column);
101 
102     CellRendererText cell_text = new CellRendererText();
103     column.packStart(cell_text, 0 );
104     column.addAttribute(cell_text, "text", COLUMN_NAME);
105     column.addAttribute(cell_text, "background-gdk", COLUMN_TEXT_COLOR);
106     column.addAttribute(cell_text, "foreground-rgba", COLUMN_TEXT_COLOR_RGBA);
107 
108     // create second column with two renderers
109     column = new TreeViewColumn();
110     column.setTitle( "Value" );
111     tv.appendColumn(column);
112 
113     CellRendererToggle cell_bool = new CellRendererToggle();
114     column.packStart(cell_bool, 0 );
115     column.addAttribute(cell_bool, "active", COLUMN_BOOL);
116     column.addAttribute(cell_bool, "visible", COLUMN_BOOL_VISIBLE);
117 
118     cell_text = new CellRendererText();
119     column.packStart(cell_text, 0 );
120     column.addAttribute(cell_text, "text", COLUMN_TEXT);
121     column.addAttribute(cell_text, "visible", COLUMN_TEXT_VISIBLE);
122     cell_text.setProperty( "editable", 1 );
123 
124     // change value in store on toggle event
125     cell_bool.addOnToggled( delegate void(string p, CellRendererToggle){
126         auto path = new TreePath( p );
127         auto it = new TreeIter( store, path );
128         store.setValue(it, COLUMN_BOOL, it.getValueInt( COLUMN_BOOL ) ? 0 : 1 );
129     });
130 
131     // change the text in the store on end of edit
132     cell_text.addOnEdited( delegate void(string p, string v, CellRendererText cell ){
133         auto path = new TreePath( p );
134         auto it = new TreeIter( store, path );
135         store.setValue( it, COLUMN_TEXT, v );
136     });
137 
138     tv.setModel(store);
139     wnd.showAll();
140 
141     wnd.addOnDelete( delegate bool (Event event, Widget widget) {
142         widget.destroy();
143         Main.quit();
144         return false;
145     });
146 
147     Main.run();
148 }
149