File: example_treeview_treestore.cc

package info (click to toggle)
gtkmm3.0 3.24.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,680 kB
  • sloc: xml: 121,333; cpp: 8,531; sh: 4,250; makefile: 262; perl: 236
file content (429 lines) | stat: -rw-r--r-- 15,044 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* Tree View/Tree Store
 *
 * The GtkTreeStore is used to store data in tree form, to be
 * used later on by a GtkTreeView to display it. This demo builds
 * a simple GtkTreeStore and displays it. If you're new to the
 * GtkTreeView widgets and associates, look into the GtkListStore
 * example first.
 *
 */

#include <gtkmm.h>

class CellItem_Holiday
{
public:
  CellItem_Holiday();
  CellItem_Holiday(Glib::ustring label, bool alex, bool havoc, bool tim, bool owen, bool dave, bool world_holiday);
  CellItem_Holiday(Glib::ustring label, const std::vector<CellItem_Holiday>& children);
  CellItem_Holiday(const CellItem_Holiday& src);
  ~CellItem_Holiday();
  CellItem_Holiday& operator=(const CellItem_Holiday& src);

  Glib::ustring m_label;
  bool m_alex;
  bool m_havoc;
  bool m_tim;
  bool m_owen;
  bool m_dave;
  bool m_world_holiday; /* shared by the European hackers */
  std::vector<CellItem_Holiday> m_children;
};

class Example_TreeView_TreeStore : public Gtk::Window
{
public:
  Example_TreeView_TreeStore();
  ~Example_TreeView_TreeStore() override;

protected:
  //vfunc overrides:
  void on_realize() override;

  virtual void create_model();
  virtual void add_columns();
  virtual void add_items();
  virtual void treestore_add_item(const CellItem_Holiday& foo);

  //Member widgets:
  Gtk::Box m_VBox;
  Gtk::ScrolledWindow m_ScrolledWindow;
  Gtk::Label m_Label;
  Gtk::TreeView m_TreeView;
  Glib::RefPtr<Gtk::TreeStore> m_refTreeStore;

  typedef std::vector<CellItem_Holiday> type_vecItems;
  type_vecItems m_vecItems;

  struct ModelColumns : public Gtk::TreeModelColumnRecord
  {
    Gtk::TreeModelColumn<Glib::ustring>  holiday_name;
    Gtk::TreeModelColumn<bool>           alex;
    Gtk::TreeModelColumn<bool>           havoc;
    Gtk::TreeModelColumn<bool>           tim;
    Gtk::TreeModelColumn<bool>           owen;
    Gtk::TreeModelColumn<bool>           dave;
    Gtk::TreeModelColumn<bool>           visible;
    Gtk::TreeModelColumn<bool>           world;

    ModelColumns();
  };

  const ModelColumns m_columns;
};


Example_TreeView_TreeStore::ModelColumns::ModelColumns()
{
  add(holiday_name);
  add(alex); add(havoc); add(tim); add(owen); add(dave);
  add(visible); add(world);
}


CellItem_Holiday::CellItem_Holiday()
{
  m_alex = false;
  m_havoc = false;
  m_tim = false;
  m_owen = false;
  m_dave = false;
  m_world_holiday = false;
}

CellItem_Holiday::CellItem_Holiday(Glib::ustring label, bool alex, bool havoc, bool tim, bool owen, bool dave, bool world_holiday)
{
  m_label = label;
  m_alex = alex;
  m_havoc = havoc;
  m_tim = tim;
  m_owen = owen;
  m_dave = dave;
  m_world_holiday = world_holiday;
}

CellItem_Holiday::CellItem_Holiday(Glib::ustring label, const std::vector<CellItem_Holiday>& children)
{
  m_label = label;
  m_alex = false;
  m_havoc = false;
  m_tim = false;
  m_owen = false;
  m_dave = false;
  m_world_holiday = false;
  m_children = children;
}

CellItem_Holiday::CellItem_Holiday(const CellItem_Holiday& src)
{
  operator=(src);
}

CellItem_Holiday::~CellItem_Holiday()
{
}

CellItem_Holiday& CellItem_Holiday::operator=(const CellItem_Holiday& src)
{
  m_label = src.m_label;
  m_alex = src.m_alex;
  m_havoc = src.m_havoc;
  m_tim = src.m_tim;
  m_owen = src.m_owen;
  m_dave = src.m_dave;
  m_world_holiday = src.m_world_holiday;
  m_children = src.m_children;

  return *this;
}


//Called by DemoWindow;
Gtk::Window* do_treeview_treestore()
{
  return new Example_TreeView_TreeStore();
}


Example_TreeView_TreeStore::Example_TreeView_TreeStore()
: m_VBox(Gtk::ORIENTATION_VERTICAL, 8),
  m_Label("Jonathan's Holiday Card Planning Sheet")
{
  set_title("Card planning sheet");
  set_border_width(8);
  set_default_size(650, 400);

  add(m_VBox);
  m_VBox.pack_start(m_Label, Gtk::PACK_SHRINK);

  m_ScrolledWindow.set_shadow_type(Gtk::SHADOW_ETCHED_IN);
  m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
  m_VBox.pack_start(m_ScrolledWindow);

  /* create model */
  create_model();

  /* create tree view */
  m_TreeView.set_model(m_refTreeStore);
  Glib::RefPtr<Gtk::TreeSelection> refTreeSelection = m_TreeView.get_selection();
  refTreeSelection->set_mode(Gtk::SELECTION_MULTIPLE);


  add_columns();
  m_ScrolledWindow.add(m_TreeView);

  show_all();
}

Example_TreeView_TreeStore::~Example_TreeView_TreeStore()
{
}

void Example_TreeView_TreeStore::add_items()
{
  std::vector<CellItem_Holiday> january;
  january.push_back( CellItem_Holiday("New Years Day", true, true, true, true, false, true) );
  january.push_back( CellItem_Holiday("Presidential Inauguration", false, true, false, true, false, false) );
  january.push_back( CellItem_Holiday("Martin Luther King Jr. day", false, true, false, true, false, false) );

  std::vector<CellItem_Holiday> february;
  february.push_back( CellItem_Holiday("Presidents' Day", false, true, false, true, false, false) );
  february.push_back( CellItem_Holiday("Groundhog Day", false, false, false, false, false, false) );
  february.push_back( CellItem_Holiday("Valentine's Day", false, false, false, false, true, true) );

  std::vector<CellItem_Holiday> march;
  march.push_back( CellItem_Holiday("National Tree Planting Day", false, false, false, false, false, false) );
  march.push_back( CellItem_Holiday("St Patrick's Day", false, false, false, false, false, true) );

  std::vector<CellItem_Holiday> april;
  april.push_back( CellItem_Holiday("April Fools' Day", false, false, false, false, false, true) );
  april.push_back( CellItem_Holiday("Army Day", false, false, false, false, false, false) );
  april.push_back( CellItem_Holiday("Earth Day", false, false, false, false, false, true) );
  april.push_back( CellItem_Holiday("Administrative Professionals' Day", false, false, false, false, false, false) );

  std::vector<CellItem_Holiday> may;
  may.push_back( CellItem_Holiday("Nurses' Day", false, false, false, false, false, false) );
  may.push_back( CellItem_Holiday("National Day of Prayer", false, false, false, false, false, false) );
  may.push_back( CellItem_Holiday("Mothers' Day", false, false, false, false, false, true) );
  may.push_back( CellItem_Holiday("Armed Forces Day", false, false, false, false, false, false) );
  may.push_back( CellItem_Holiday("Memorial Day", true, true, true, true, false, true) );

  std::vector<CellItem_Holiday> june;
  june.push_back( CellItem_Holiday("June Fathers' Day", false, false, false, false, false, true) );
  june.push_back( CellItem_Holiday("Juneteenth (Liberation of Slaves)", false, false, false, false, false, false) );
  june.push_back( CellItem_Holiday("Flag Day", false, true, false, true, false, false) );

  std::vector<CellItem_Holiday> july;
  july.push_back( CellItem_Holiday("Parents' Day", false, false, false, false, false, true) );
  july.push_back( CellItem_Holiday("Independence Day", false, true, false, true, false, false) );

  std::vector<CellItem_Holiday> august;
  august.push_back( CellItem_Holiday("Air Force Day", false, false, false, false, false, false) );
  august.push_back( CellItem_Holiday("Coast Guard Day", false, false, false, false, false, false) );
  august.push_back( CellItem_Holiday("Friendship Day", false, false, false, false, false, false) );

  std::vector<CellItem_Holiday> september;
  september.push_back( CellItem_Holiday("Grandparents' Day", false, false, false, false, false, true) );
  september.push_back( CellItem_Holiday("Citizenship Day or Constitution Day", false, false, false, false, false, false) );
  september.push_back( CellItem_Holiday("Labor Day", true, true, true, true, false, true) );

  std::vector<CellItem_Holiday> october;
  october.push_back( CellItem_Holiday("National Children's Day", false, false, false, false, false, false) );
  october.push_back( CellItem_Holiday("Bosses' Day", false, false, false, false, false, false) );
  october.push_back( CellItem_Holiday("Sweetest Day", false, false, false, false, false, false) );
  october.push_back( CellItem_Holiday("Mother-in-Law's Day", false, false, false, false, false, false) );
  october.push_back( CellItem_Holiday("Navy Day", false, false, false, false, false, false) );
  october.push_back( CellItem_Holiday("Columbus Day", false, true, false, true, false, false) );
  october.push_back( CellItem_Holiday("Halloween", false, false, false, false, false, true) );

  std::vector<CellItem_Holiday> november;
  november.push_back( CellItem_Holiday("Marine Corps Day", false, false, false, false, false, false) );
  november.push_back( CellItem_Holiday("Veterans' Day", true, true, true, true, false, true) );
  november.push_back( CellItem_Holiday("Thanksgiving", false, true, false, true, false, false) );

  std::vector<CellItem_Holiday> december;
  december.push_back( CellItem_Holiday("Pearl Harbor Remembrance Day", false, false, false, false, false, false) );
  december.push_back( CellItem_Holiday("Christmas", true, true, true, true, false, true) );
  december.push_back( CellItem_Holiday("Kwanzaa", false, false, false, false, false, false) );

  m_vecItems.push_back( CellItem_Holiday("January", january) );
  m_vecItems.push_back( CellItem_Holiday("February", february) );
  m_vecItems.push_back( CellItem_Holiday("March", march) );
  m_vecItems.push_back( CellItem_Holiday("April", april) );
  m_vecItems.push_back( CellItem_Holiday("May", may) );
  m_vecItems.push_back( CellItem_Holiday("June", june) );
  m_vecItems.push_back( CellItem_Holiday("July", july) );
  m_vecItems.push_back( CellItem_Holiday("August", august) );
  m_vecItems.push_back( CellItem_Holiday("September", september) );
  m_vecItems.push_back( CellItem_Holiday("October", october) );
  m_vecItems.push_back( CellItem_Holiday("November", november) );
  m_vecItems.push_back( CellItem_Holiday("December", december) );
}

void Example_TreeView_TreeStore::create_model()
{
  m_refTreeStore = Gtk::TreeStore::create(m_columns);

  add_items();

  std::for_each(
      m_vecItems.begin(), m_vecItems.end(),
      sigc::mem_fun(*this, &Example_TreeView_TreeStore::treestore_add_item));

#if 0 /* testing code: should work, try it out if you like */

  Gtk::TreeIterChildren children = m_refTreeStore->children();

  for(auto node : children)
  {
    g_print("outer loop\n");

    for(auto row : node.children())
    {
      g_print("inner loop\n");
      row = m_refTreeStore->erase(row);
    }
  }

#endif /* testing code */
}

void Example_TreeView_TreeStore::treestore_add_item(const CellItem_Holiday& foo)
{
  Gtk::TreeRow row = *(m_refTreeStore->append());

  row[m_columns.holiday_name] = foo.m_label;
  row[m_columns.alex]         = foo.m_alex;
  row[m_columns.havoc]        = foo.m_havoc;
  row[m_columns.tim]          = foo.m_tim;
  row[m_columns.owen]         = foo.m_owen;
  row[m_columns.dave]         = foo.m_dave;
  row[m_columns.visible]      = false;
  row[m_columns.world]        = foo.m_world_holiday;

  //Add Children:
  for(const auto& child : foo.m_children)
  {
    Gtk::TreeRow child_row = *(m_refTreeStore->append(row.children()));

    child_row[m_columns.holiday_name] = child.m_label;
    child_row[m_columns.alex]         = child.m_alex;
    child_row[m_columns.havoc]        = child.m_havoc;
    child_row[m_columns.tim]          = child.m_tim;
    child_row[m_columns.owen]         = child.m_owen;
    child_row[m_columns.dave]         = child.m_dave;
    child_row[m_columns.visible]      = true;
    child_row[m_columns.world]        = child.m_world_holiday;
  }
}

void Example_TreeView_TreeStore::add_columns()
{
  /* column for holiday names */
  {
    int cols_count = m_TreeView.append_column("Holiday",  m_columns.holiday_name);
    Gtk::TreeViewColumn* pColumn = m_TreeView.get_column(cols_count-1);
    if(pColumn)
    {
      Gtk::CellRenderer* pRenderer = pColumn->get_first_cell();
      pRenderer->property_xalign().set_value(0.0);

      pColumn->set_clickable();
    }
  }

  /* column for Alex */
  {
    int cols_count = m_TreeView.append_column_editable("Alex", m_columns.alex);
    Gtk::TreeViewColumn* pColumn = m_TreeView.get_column(cols_count-1);
    if(pColumn)
    {
      Gtk::CellRendererToggle* pRenderer = static_cast<Gtk::CellRendererToggle*>(pColumn->get_first_cell());
      pRenderer->property_xalign().set_value(0.0);

      pColumn->add_attribute(pRenderer->property_visible(), m_columns.visible);
      pColumn->add_attribute(pRenderer->property_activatable(), m_columns.world);

      pColumn->set_sizing(Gtk::TREE_VIEW_COLUMN_FIXED);
      pColumn->set_fixed_width(50);
      pColumn->set_clickable();
    }
  }

  /* column for Havoc */
  {
    int cols_count = m_TreeView.append_column_editable("Havoc", m_columns.havoc);
    Gtk::TreeViewColumn* pColumn = m_TreeView.get_column(cols_count-1);
    if(pColumn)
    {
      Gtk::CellRendererToggle* pRenderer = static_cast<Gtk::CellRendererToggle*>(pColumn->get_first_cell());
      pRenderer->property_xalign().set_value(0.0);

      pColumn->add_attribute(pRenderer->property_visible(), m_columns.visible);

      pColumn->set_sizing(Gtk::TREE_VIEW_COLUMN_FIXED);
      pColumn->set_fixed_width(50);
      pColumn->set_clickable();
    }
  }

  /* column for Tim */
  {
    int cols_count = m_TreeView.append_column_editable("Tim", m_columns.tim);
    Gtk::TreeViewColumn* pColumn = m_TreeView.get_column(cols_count-1);
    if(pColumn)
    {
      Gtk::CellRendererToggle* pRenderer = static_cast<Gtk::CellRendererToggle*>(pColumn->get_first_cell());
      pRenderer->property_xalign().set_value(0.0);

      pColumn->add_attribute(pRenderer->property_visible(), m_columns.visible);
      pColumn->add_attribute(pRenderer->property_activatable(), m_columns.world);

      pColumn->set_sizing(Gtk::TREE_VIEW_COLUMN_FIXED);
      pColumn->set_fixed_width(50);
      pColumn->set_clickable();
    }
  }

  /* column for Owen */
  {
    int cols_count = m_TreeView.append_column_editable("Owen", m_columns.owen);
    Gtk::TreeViewColumn* pColumn = m_TreeView.get_column(cols_count-1);
    if(pColumn)
    {
      Gtk::CellRendererToggle* pRenderer = static_cast<Gtk::CellRendererToggle*>(pColumn->get_first_cell());
      pRenderer->property_xalign().set_value(0.0);

      pColumn->add_attribute(pRenderer->property_visible(), m_columns.visible);

      pColumn->set_sizing(Gtk::TREE_VIEW_COLUMN_FIXED);
      pColumn->set_fixed_width(50);
      pColumn->set_clickable();
    }
  }

  /* column for Dave */
  {
    int cols_count = m_TreeView.append_column("Dave", m_columns.dave);
    Gtk::TreeViewColumn* pColumn = m_TreeView.get_column(cols_count-1);
    if(pColumn)
    {
      Gtk::CellRendererToggle* pRenderer = static_cast<Gtk::CellRendererToggle*>(pColumn->get_first_cell());
      pRenderer->property_xalign().set_value(0.0);

      pColumn->add_attribute(pRenderer->property_visible(), m_columns.visible);

      pColumn->set_sizing(Gtk::TREE_VIEW_COLUMN_FIXED);
      pColumn->set_fixed_width(50);
      pColumn->set_clickable();
    }
  }
}


void Example_TreeView_TreeStore::on_realize()
{
  m_TreeView.expand_all();
  //call base class:
  Window::on_realize();
}