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 import pango.PgFontDescription;
57 import std.stdio;
58 
59 enum {
60     COLUMN_NAME,
61     COLUMN_TEXT,
62     COLUMN_TEXT_VISIBLE,
63     COLUMN_BOOL,
64     COLUMN_BOOL_VISIBLE,
65     COLUMN_TEXT_COLOR,
66     COLUMN_TEXT_COLOR_RGBA,
67     COLUMN_TEXT_FONT_DESCRIPTION,
68 }
69 
70 void main(string[] args){
71     Main.init(args);
72     ListStore store = new ListStore( [
73         GType.STRING,
74         GType.STRING,
75         GType.INT,
76         GType.INT,
77         GType.INT,
78         Color.getType(),
79         RGBA.getType(),
80         PgFontDescription.getType(),
81         ] );
82 
83     void appendRecord( string name, string value, bool isBoolean, RGBA rgba, Color color ){
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         store.setValue( it, COLUMN_TEXT_COLOR, color );
92         store.setValue( it, COLUMN_TEXT_FONT_DESCRIPTION, new PgFontDescription() );
93     }
94     // fill store with data
95     appendRecord( "Loops", "10", false, new RGBA(1.0,0.0,0.0,1.0), new Color(64,64,64) );
96     appendRecord( "Name", "keinfarbton", false, new RGBA(0.0,1.0,0.0,1.0), new Color(127,127,127) );
97     appendRecord( "Verbose", "true", true, new RGBA(0.0,0.0,1.0,1.0), new Color(200,200,200) );
98 
99     auto wnd = new Window( "Celleditor Demo" );
100     auto tv  = new TreeView();
101     wnd.add(tv);
102 
103     // create first column with text renderer
104     TreeViewColumn column = new TreeViewColumn();
105     column.setTitle( "Name" );
106     tv.appendColumn(column);
107 
108     CellRendererText cell_text = new CellRendererText();
109     column.packStart(cell_text, 0 );
110     column.addAttribute(cell_text, "text", COLUMN_NAME);
111     column.addAttribute(cell_text, "background-gdk", COLUMN_TEXT_COLOR);
112     column.addAttribute(cell_text, "foreground-rgba", COLUMN_TEXT_COLOR_RGBA);
113 
114     // create second column with two renderers
115     column = new TreeViewColumn();
116     column.setTitle( "Value" );
117     tv.appendColumn(column);
118 
119     CellRendererToggle cell_bool = new CellRendererToggle();
120     column.packStart(cell_bool, 0 );
121     column.addAttribute(cell_bool, "active", COLUMN_BOOL);
122     column.addAttribute(cell_bool, "visible", COLUMN_BOOL_VISIBLE);
123 
124     cell_text = new CellRendererText();
125     column.packStart(cell_text, 0 );
126     column.addAttribute(cell_text, "text", COLUMN_TEXT);
127     column.addAttribute(cell_text, "visible", COLUMN_TEXT_VISIBLE);
128     cell_text.setProperty( "editable", 1 );
129 
130     // change value in store on toggle event
131     cell_bool.addOnToggled( delegate void(string p, CellRendererToggle){
132         auto path = new TreePath( p );
133         auto it = new TreeIter( store, path );
134         store.setValue(it, COLUMN_BOOL, it.getValueInt( COLUMN_BOOL ) ? 0 : 1 );
135 
136         auto val = store.getValue(it, COLUMN_TEXT_FONT_DESCRIPTION);
137 
138         import gobject.Type;
139 
140         writeln(Type.isA(PgFontDescription.getType(), GType.BOXED));
141         writeln(PgFontDescription.getType(), " ", val.gType);
142 
143         auto font = val.get!PgFontDescription();
144 
145         writeln(font.getFamily());
146     });
147 
148     // change the text in the store on end of edit
149     cell_text.addOnEdited( delegate void(string p, string v, CellRendererText cell ){
150         auto path = new TreePath( p );
151         auto it = new TreeIter( store, path );
152         store.setValue( it, COLUMN_TEXT, v );
153     });
154 
155     tv.setModel(store);
156     wnd.showAll();
157 
158     wnd.addOnDelete( delegate bool (Event event, Widget widget) {
159         widget.destroy();
160         Main.quit();
161         return false;
162     });
163 
164     Main.run();
165 }
166