~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Utility/GtkHelper.vala

  • Committer: Tony George
  • Date: 2016-10-16 13:42:12 UTC
  • Revision ID: tony.george.kol@gmail.com-20161016134212-ibjsptmgbha7a2mw
Fixed scheduled backups; Initialize display for the scheduled cron task;

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
 
 
3
using TeeJee.Logging;
 
4
using TeeJee.FileSystem;
 
5
using TeeJee.JsonHelper;
 
6
using TeeJee.ProcessHelper;
 
7
using TeeJee.System;
 
8
using TeeJee.Misc;
 
9
 
 
10
namespace TeeJee.GtkHelper{
 
11
 
 
12
        using Gtk;
 
13
 
 
14
        // messages -----------
 
15
        
 
16
        public void show_err_log(Gtk.Window parent, bool disable_log = true){
 
17
                if ((err_log != null) && (err_log.length > 0)){
 
18
                        gtk_messagebox(_("Error"), err_log, parent, true);
 
19
                }
 
20
 
 
21
                if (disable_log){
 
22
                        err_log_disable();
 
23
                }
 
24
        }
 
25
        
 
26
        public void gtk_do_events (){
 
27
 
 
28
                /* Do pending events */
 
29
 
 
30
                while(Gtk.events_pending ())
 
31
                        Gtk.main_iteration ();
 
32
        }
 
33
 
 
34
        public void gtk_set_busy (bool busy, Gtk.Window win) {
 
35
 
 
36
                /* Show or hide busy cursor on window */
 
37
 
 
38
                Gdk.Cursor? cursor = null;
 
39
 
 
40
                if (busy){
 
41
                        cursor = new Gdk.Cursor(Gdk.CursorType.WATCH);
 
42
                }
 
43
                else{
 
44
                        cursor = new Gdk.Cursor(Gdk.CursorType.ARROW);
 
45
                }
 
46
 
 
47
                var window = win.get_window ();
 
48
 
 
49
                if (window != null) {
 
50
                        window.set_cursor (cursor);
 
51
                }
 
52
 
 
53
                gtk_do_events ();
 
54
        }
 
55
 
 
56
        public void gtk_messagebox(
 
57
                string title, string message, Gtk.Window? parent_win, bool is_error = false){
 
58
 
 
59
                /* Shows a simple message box */
 
60
 
 
61
                var type = Gtk.MessageType.INFO;
 
62
                if (is_error){
 
63
                        type = Gtk.MessageType.ERROR;
 
64
                }
 
65
                else{
 
66
                        type = Gtk.MessageType.INFO;
 
67
                }
 
68
 
 
69
                /*var dlg = new Gtk.MessageDialog.with_markup(null, Gtk.DialogFlags.MODAL, type, Gtk.ButtonsType.OK, message);
 
70
                dlg.title = title;
 
71
                dlg.set_default_size (200, -1);
 
72
                if (parent_win != null){
 
73
                        dlg.set_transient_for(parent_win);
 
74
                        dlg.set_modal(true);
 
75
                }
 
76
                dlg.run();
 
77
                dlg.destroy();*/
 
78
 
 
79
                var dlg = new CustomMessageDialog(title,message,type,parent_win, Gtk.ButtonsType.OK);
 
80
                dlg.run();
 
81
                dlg.destroy();
 
82
        }
 
83
 
 
84
        public string? gtk_inputbox(
 
85
                string title, string message, Gtk.Window? parent_win, bool mask_password = false){
 
86
 
 
87
                /* Shows a simple input prompt */
 
88
 
 
89
                //vbox_main
 
90
        Gtk.Box vbox_main = new Box (Orientation.VERTICAL, 0);
 
91
        vbox_main.margin = 0;
 
92
 
 
93
                //lbl_input
 
94
                Gtk.Label lbl_input = new Gtk.Label(title);
 
95
                lbl_input.xalign = (float) 0.0;
 
96
                lbl_input.label = message;
 
97
 
 
98
                //txt_input
 
99
                Gtk.Entry txt_input = new Gtk.Entry();
 
100
                txt_input.margin_top = 3;
 
101
                txt_input.set_visibility(false);
 
102
 
 
103
                //create dialog
 
104
                var dlg = new Gtk.Dialog.with_buttons(title, parent_win, DialogFlags.MODAL);
 
105
                dlg.title = title;
 
106
                dlg.set_default_size (300, -1);
 
107
                if (parent_win != null){
 
108
                        dlg.set_transient_for(parent_win);
 
109
                        dlg.set_modal(true);
 
110
                }
 
111
 
 
112
                //add widgets
 
113
                var content = (Box) dlg.get_content_area ();
 
114
                vbox_main.pack_start (lbl_input, false, true, 0);
 
115
                vbox_main.pack_start (txt_input, false, true, 0);
 
116
                content.add(vbox_main);
 
117
                content.margin = 6;
 
118
                
 
119
                //add buttons
 
120
                var actions = (Box) dlg.get_action_area ();
 
121
                dlg.add_button(_("OK"),Gtk.ResponseType.OK);
 
122
                dlg.add_button(_("Cancel"),Gtk.ResponseType.CANCEL);
 
123
                //actions.margin = 6;
 
124
                actions.margin_top = 12;
 
125
                
 
126
                //keyboard shortcuts
 
127
                txt_input.key_press_event.connect ((w, event) => {
 
128
                        if (event.keyval == 65293) {
 
129
                                dlg.response(Gtk.ResponseType.OK);
 
130
                                return true;
 
131
                        }
 
132
                        return false;
 
133
                });
 
134
 
 
135
                dlg.show_all();
 
136
                int response = dlg.run();
 
137
                string input_text = txt_input.text;
 
138
                dlg.destroy();
 
139
 
 
140
                if (response == Gtk.ResponseType.CANCEL){
 
141
                        return null;
 
142
                }
 
143
                else{
 
144
                        return input_text;
 
145
                }
 
146
        }
 
147
 
 
148
 
 
149
        // combo ---------
 
150
        
 
151
        public bool gtk_combobox_set_value (ComboBox combo, int index, string val){
 
152
 
 
153
                /* Conveniance function to set combobox value */
 
154
 
 
155
                TreeIter iter;
 
156
                string comboVal;
 
157
                TreeModel model = (TreeModel) combo.model;
 
158
 
 
159
                bool iterExists = model.get_iter_first (out iter);
 
160
                while (iterExists){
 
161
                        model.get(iter, 1, out comboVal);
 
162
                        if (comboVal == val){
 
163
                                combo.set_active_iter(iter);
 
164
                                return true;
 
165
                        }
 
166
                        iterExists = model.iter_next (ref iter);
 
167
                }
 
168
 
 
169
                return false;
 
170
        }
 
171
 
 
172
        public string gtk_combobox_get_value (ComboBox combo, int index, string default_value){
 
173
 
 
174
                /* Conveniance function to get combobox value */
 
175
 
 
176
                if ((combo.model == null) || (combo.active < 0)) { return default_value; }
 
177
 
 
178
                TreeIter iter;
 
179
                string val = "";
 
180
                combo.get_active_iter (out iter);
 
181
                TreeModel model = (TreeModel) combo.model;
 
182
                model.get(iter, index, out val);
 
183
 
 
184
                return val;
 
185
        }
 
186
 
 
187
        public GLib.Object gtk_combobox_get_selected_object (
 
188
                ComboBox combo,
 
189
                int index,
 
190
                GLib.Object default_value){
 
191
 
 
192
                /* Conveniance function to get combobox value */
 
193
 
 
194
                if ((combo.model == null) || (combo.active < 0)) { return default_value; }
 
195
 
 
196
                TreeIter iter;
 
197
                GLib.Object val = null;
 
198
                combo.get_active_iter (out iter);
 
199
                TreeModel model = (TreeModel) combo.model;
 
200
                model.get(iter, index, out val);
 
201
 
 
202
                return val;
 
203
        }
 
204
        
 
205
        public int gtk_combobox_get_value_enum (ComboBox combo, int index, int default_value){
 
206
 
 
207
                /* Conveniance function to get combobox value */
 
208
 
 
209
                if ((combo.model == null) || (combo.active < 0)) { return default_value; }
 
210
 
 
211
                TreeIter iter;
 
212
                int val;
 
213
                combo.get_active_iter (out iter);
 
214
                TreeModel model = (TreeModel) combo.model;
 
215
                model.get(iter, index, out val);
 
216
 
 
217
                return val;
 
218
        }
 
219
 
 
220
        // icon -------
 
221
        
 
222
        public Gdk.Pixbuf? get_app_icon(int icon_size, string format = ".png"){
 
223
                var img_icon = get_shared_icon(AppShortName, AppShortName + format,icon_size,"pixmaps");
 
224
                if (img_icon != null){
 
225
                        return img_icon.pixbuf;
 
226
                }
 
227
                else{
 
228
                        return null;
 
229
                }
 
230
        }
 
231
 
 
232
        public Gtk.Image? get_shared_icon(
 
233
                string icon_name,
 
234
                string fallback_icon_file_name,
 
235
                int icon_size,
 
236
                string icon_directory = AppShortName + "/images"){
 
237
                        
 
238
                Gdk.Pixbuf pix_icon = null;
 
239
                Gtk.Image img_icon = null;
 
240
 
 
241
                try {
 
242
                        Gtk.IconTheme icon_theme = Gtk.IconTheme.get_default();
 
243
                        
 
244
                        pix_icon = icon_theme.load_icon_for_scale (
 
245
                                icon_name, Gtk.IconSize.MENU, icon_size, Gtk.IconLookupFlags.FORCE_SIZE);
 
246
                                
 
247
                } catch (Error e) {
 
248
                        //log_error (e.message);
 
249
                }
 
250
 
 
251
                string fallback_icon_file_path = "/usr/share/%s/%s".printf(icon_directory, fallback_icon_file_name);
 
252
 
 
253
                if (pix_icon == null){
 
254
                        try {
 
255
                                pix_icon = new Gdk.Pixbuf.from_file_at_size (fallback_icon_file_path, icon_size, icon_size);
 
256
                        } catch (Error e) {
 
257
                                log_error (e.message);
 
258
                        }
 
259
                }
 
260
 
 
261
                if (pix_icon == null){
 
262
                        log_error (_("Missing Icon") + ": '%s', '%s'".printf(icon_name, fallback_icon_file_path));
 
263
                }
 
264
                else{
 
265
                        img_icon = new Gtk.Image.from_pixbuf(pix_icon);
 
266
                }
 
267
 
 
268
                return img_icon;
 
269
        }
 
270
 
 
271
        public Gdk.Pixbuf? get_shared_icon_pixbuf(string icon_name,
 
272
                string fallback_file_name,
 
273
                int icon_size,
 
274
                string icon_directory = AppShortName + "/images"){
 
275
                        
 
276
                var img = get_shared_icon(icon_name, fallback_file_name, icon_size, icon_directory);
 
277
                var pixbuf = (img == null) ? null : img.pixbuf;
 
278
                return pixbuf;
 
279
        }
 
280
 
 
281
        // styles ----------------
 
282
 
 
283
        public static int CSS_AUTO_CLASS_INDEX = 0;
 
284
        public static void gtk_apply_css(Gtk.Widget[] widgets, string css_style){
 
285
                var css_provider = new Gtk.CssProvider();
 
286
                var css = ".style_%d { %s }".printf(++CSS_AUTO_CLASS_INDEX, css_style);
 
287
                try {
 
288
                        css_provider.load_from_data(css,-1);
 
289
                } catch (GLib.Error e) {
 
290
            warning(e.message);
 
291
        }
 
292
 
 
293
        foreach(var widget in widgets){
 
294
                        
 
295
                        widget.get_style_context().add_provider(
 
296
                                css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
 
297
                                
 
298
                        widget.get_style_context().add_class("style_%d".printf(CSS_AUTO_CLASS_INDEX));
 
299
                }
 
300
        }
 
301
        
 
302
        // treeview -----------------
 
303
        
 
304
        public int gtk_treeview_model_count(TreeModel model){
 
305
                int count = 0;
 
306
                TreeIter iter;
 
307
                if (model.get_iter_first(out iter)){
 
308
                        count++;
 
309
                        while(model.iter_next(ref iter)){
 
310
                                count++;
 
311
                        }
 
312
                }
 
313
                return count;
 
314
        }
 
315
 
 
316
        public void gtk_stripe_row(
 
317
                Gtk.CellRenderer cell,
 
318
                bool odd_row,
 
319
                string odd_color = "#F4F6F7",
 
320
                string even_color = "#FFFFFF"){
 
321
 
 
322
                if (cell is Gtk.CellRendererText){
 
323
                        (cell as Gtk.CellRendererText).background = odd_row ? odd_color : even_color;
 
324
                }
 
325
                else if (cell is Gtk.CellRendererPixbuf){
 
326
                        (cell as Gtk.CellRendererPixbuf).cell_background = odd_row ? odd_color : even_color;
 
327
                }
 
328
        }
 
329
 
 
330
        public void gtk_treeview_redraw(Gtk.TreeView treeview){
 
331
                var model = treeview.model;
 
332
                treeview.model = null;
 
333
                treeview.model = model;
 
334
        }
 
335
        
 
336
        // menu
 
337
        
 
338
        public void gtk_menu_add_separator(Gtk.Menu menu){
 
339
                Gdk.RGBA gray = Gdk.RGBA();
 
340
                gray.parse ("rgba(200,200,200,1)");
 
341
                
 
342
                // separator
 
343
                var menu_item = new Gtk.SeparatorMenuItem();
 
344
                menu_item.override_color (StateFlags.NORMAL, gray);
 
345
                menu.append(menu_item);
 
346
        }
 
347
 
 
348
        public Gtk.MenuItem gtk_menu_add_item(
 
349
                Gtk.Menu menu,
 
350
                string label,
 
351
                string tooltip,
 
352
                Gtk.Image? icon_image,
 
353
                Gtk.SizeGroup? sg_icon = null,
 
354
                Gtk.SizeGroup? sg_label = null){
 
355
 
 
356
                var menu_item = new Gtk.MenuItem();
 
357
                menu.append(menu_item);
 
358
                        
 
359
                var box = new Gtk.Box(Orientation.HORIZONTAL, 3);
 
360
                menu_item.add(box);
 
361
 
 
362
                // add icon
 
363
 
 
364
                if (icon_image == null){
 
365
                        var dummy = new Gtk.Label("");
 
366
                        box.add(dummy);
 
367
 
 
368
                        if (sg_icon != null){
 
369
                                sg_icon.add_widget(dummy);
 
370
                        }
 
371
                }
 
372
                else{
 
373
                        box.add(icon_image);
 
374
 
 
375
                        if (sg_icon != null){
 
376
                                sg_icon.add_widget(icon_image);
 
377
                        }
 
378
                }
 
379
                
 
380
                // add label
 
381
                
 
382
                var lbl = new Gtk.Label(label);
 
383
                lbl.xalign = (float) 0.0;
 
384
                lbl.margin_right = 6;
 
385
                box.add(lbl);
 
386
 
 
387
                if (sg_label != null){
 
388
                        sg_label.add_widget(lbl);
 
389
                }
 
390
 
 
391
                box.set_tooltip_text(tooltip);
 
392
 
 
393
                return menu_item;
 
394
        }
 
395
 
 
396
        // build ui
 
397
 
 
398
        public Gtk.Label gtk_box_add_header(Gtk.Box box, string text){
 
399
                var label = new Gtk.Label("<b>" + text + "</b>");
 
400
                label.set_use_markup(true);
 
401
                label.xalign = (float) 0.0;
 
402
                label.margin_bottom = 6;
 
403
                box.add(label);
 
404
 
 
405
                return label;
 
406
        }
 
407
 
 
408
        // misc
 
409
        
 
410
        public bool gtk_container_has_child(Gtk.Container container, Gtk.Widget widget){
 
411
                foreach(var child in container.get_children()){
 
412
                        if (child == widget){
 
413
                                return true;
 
414
                        }
 
415
                }
 
416
                return false;
 
417
        }
 
418
 
 
419
 
 
420
        private void text_view_append(Gtk.TextView view, string text){
 
421
                TextIter iter;
 
422
                view.buffer.get_end_iter(out iter);
 
423
                view.buffer.insert(ref iter, text, text.length);
 
424
        }
 
425
 
 
426
        private void text_view_prepend(Gtk.TextView view, string text){
 
427
                TextIter iter;
 
428
                view.buffer.get_start_iter(out iter);
 
429
                view.buffer.insert(ref iter, text, text.length);
 
430
        }
 
431
 
 
432
        private void text_view_scroll_to_end(Gtk.TextView view){
 
433
                TextIter iter;
 
434
                view.buffer.get_end_iter(out iter);
 
435
                view.scroll_to_iter(iter, 0.0, false, 0.0, 0.0);
 
436
        }
 
437
 
 
438
        private void text_view_scroll_to_start(Gtk.TextView view){
 
439
                TextIter iter;
 
440
                view.buffer.get_start_iter(out iter);
 
441
                view.scroll_to_iter(iter, 0.0, false, 0.0, 0.0);
 
442
        }
 
443
        
 
444
        // file chooser ----------------
 
445
        
 
446
        public Gtk.FileFilter create_file_filter(string group_name, string[] patterns) {
 
447
                var filter = new Gtk.FileFilter ();
 
448
                filter.set_filter_name(group_name);
 
449
                foreach(string pattern in patterns) {
 
450
                        filter.add_pattern (pattern);
 
451
                }
 
452
                return filter;
 
453
        }
 
454
 
 
455
        // utility ------------------
 
456
 
 
457
        // add_notebook
 
458
        private Gtk.Notebook add_notebook(
 
459
                Gtk.Box box, bool show_tabs = true, bool show_border = true){
 
460
                        
 
461
        // notebook
 
462
                var book = new Gtk.Notebook();
 
463
                book.margin = 0;
 
464
                book.show_tabs = show_tabs;
 
465
                book.show_border = show_border;
 
466
                
 
467
                box.pack_start(book, true, true, 0);
 
468
                
 
469
                return book;
 
470
        }
 
471
 
 
472
        // add_tab
 
473
        private Gtk.Box add_tab(
 
474
                Gtk.Notebook book, string title, int margin = 12, int spacing = 6){
 
475
                        
 
476
                // label
 
477
                var label = new Gtk.Label(title);
 
478
 
 
479
        // vbox
 
480
        var vbox = new Box (Gtk.Orientation.VERTICAL, spacing);
 
481
        vbox.margin = margin;
 
482
        book.append_page (vbox, label);
 
483
 
 
484
        return vbox;
 
485
        }
 
486
 
 
487
        // add_treeview
 
488
        private Gtk.TreeView add_treeview(Gtk.Box box,
 
489
                Gtk.SelectionMode selection_mode = Gtk.SelectionMode.SINGLE){
 
490
                        
 
491
                // TreeView
 
492
                var treeview = new TreeView();
 
493
                treeview.get_selection().mode = selection_mode;
 
494
                treeview.set_rules_hint (true);
 
495
                treeview.show_expanders = true;
 
496
                treeview.enable_tree_lines = true;
 
497
 
 
498
                // ScrolledWindow
 
499
                var scrollwin = new ScrolledWindow(null, null);
 
500
                scrollwin.set_shadow_type (ShadowType.ETCHED_IN);
 
501
                scrollwin.add (treeview);
 
502
                scrollwin.expand = true;
 
503
                box.add(scrollwin);
 
504
 
 
505
                return treeview;
 
506
        }
 
507
 
 
508
        // add_column_text
 
509
        private Gtk.TreeViewColumn add_column_text(
 
510
                Gtk.TreeView treeview, string title, out Gtk.CellRendererText cell){
 
511
                        
 
512
                // TreeViewColumn
 
513
                var col = new Gtk.TreeViewColumn();
 
514
                col.title = title;
 
515
                
 
516
                cell = new Gtk.CellRendererText();
 
517
                cell.xalign = (float) 0.0;
 
518
                col.pack_start (cell, false);
 
519
                treeview.append_column(col);
 
520
                
 
521
                return col;
 
522
        }
 
523
 
 
524
 
 
525
        // add_column_icon
 
526
        private Gtk.TreeViewColumn add_column_icon(
 
527
                Gtk.TreeView treeview, string title, out Gtk.CellRendererPixbuf cell){
 
528
                
 
529
                // TreeViewColumn
 
530
                var col = new Gtk.TreeViewColumn();
 
531
                col.title = title;
 
532
                
 
533
                cell = new Gtk.CellRendererPixbuf();
 
534
                cell.xpad = 2;
 
535
                col.pack_start (cell, false);
 
536
                treeview.append_column(col);
 
537
 
 
538
                return col;
 
539
        }
 
540
 
 
541
        // add_column_icon_and_text
 
542
        private Gtk.TreeViewColumn add_column_icon_and_text(
 
543
                Gtk.TreeView treeview, string title,
 
544
                out Gtk.CellRendererPixbuf cell_pix, out Gtk.CellRendererText cell_text){
 
545
                        
 
546
                // TreeViewColumn
 
547
                var col = new Gtk.TreeViewColumn();
 
548
                col.title = title;
 
549
 
 
550
                cell_pix = new Gtk.CellRendererPixbuf();
 
551
                cell_pix.xpad = 2;
 
552
                col.pack_start (cell_pix, false);
 
553
                
 
554
                cell_text = new Gtk.CellRendererText();
 
555
                cell_text.xalign = (float) 0.0;
 
556
                col.pack_start (cell_text, false);
 
557
                treeview.append_column(col);
 
558
 
 
559
                return col;
 
560
        }
 
561
 
 
562
        // add_column_radio_and_text
 
563
        private Gtk.TreeViewColumn add_column_radio_and_text(
 
564
                Gtk.TreeView treeview, string title,
 
565
                out Gtk.CellRendererToggle cell_radio, out Gtk.CellRendererText cell_text){
 
566
                        
 
567
                // TreeViewColumn
 
568
                var col = new Gtk.TreeViewColumn();
 
569
                col.title = title;
 
570
 
 
571
                cell_radio = new Gtk.CellRendererToggle();
 
572
                cell_radio.xpad = 2;
 
573
                cell_radio.radio = true;
 
574
                cell_radio.activatable = true;
 
575
                col.pack_start (cell_radio, false);
 
576
                
 
577
                cell_text = new Gtk.CellRendererText();
 
578
                cell_text.xalign = (float) 0.0;
 
579
                col.pack_start (cell_text, false);
 
580
                treeview.append_column(col);
 
581
 
 
582
                return col;
 
583
        }
 
584
 
 
585
        // add_column_icon_radio_text
 
586
        private Gtk.TreeViewColumn add_column_icon_radio_text(
 
587
                Gtk.TreeView treeview, string title,
 
588
                out Gtk.CellRendererPixbuf cell_pix,
 
589
                out Gtk.CellRendererToggle cell_radio,
 
590
                out Gtk.CellRendererText cell_text){
 
591
                        
 
592
                // TreeViewColumn
 
593
                var col = new Gtk.TreeViewColumn();
 
594
                col.title = title;
 
595
 
 
596
                cell_pix = new Gtk.CellRendererPixbuf();
 
597
                cell_pix.xpad = 2;
 
598
                col.pack_start (cell_pix, false);
 
599
 
 
600
                cell_radio = new Gtk.CellRendererToggle();
 
601
                cell_radio.xpad = 2;
 
602
                cell_radio.radio = true;
 
603
                cell_radio.activatable = true;
 
604
                col.pack_start (cell_radio, false);
 
605
                
 
606
                cell_text = new Gtk.CellRendererText();
 
607
                cell_text.xalign = (float) 0.0;
 
608
                col.pack_start (cell_text, false);
 
609
                treeview.append_column(col);
 
610
 
 
611
                return col;
 
612
        }
 
613
 
 
614
        // add_label_scrolled
 
615
        private Gtk.Label add_label_scrolled(
 
616
                Gtk.Box box, string text,
 
617
                bool show_border = false, bool wrap = false, int ellipsize_chars = 40){
 
618
 
 
619
                // ScrolledWindow
 
620
                var scroll = new Gtk.ScrolledWindow(null, null);
 
621
                scroll.hscrollbar_policy = PolicyType.NEVER;
 
622
                scroll.vscrollbar_policy = PolicyType.ALWAYS;
 
623
                scroll.expand = true;
 
624
                box.add(scroll);
 
625
                
 
626
                var label = new Gtk.Label(text);
 
627
                label.xalign = (float) 0.0;
 
628
                label.yalign = (float) 0.0;
 
629
                label.margin = 6;
 
630
                label.set_use_markup(true);
 
631
                scroll.add(label);
 
632
 
 
633
                if (wrap){
 
634
                        label.wrap = true;
 
635
                        label.wrap_mode = Pango.WrapMode.WORD;
 
636
                }
 
637
                else {
 
638
                        label.wrap = false;
 
639
                        label.ellipsize = Pango.EllipsizeMode.MIDDLE;
 
640
                        label.max_width_chars = ellipsize_chars;
 
641
                }
 
642
 
 
643
                if (show_border){
 
644
                        scroll.set_shadow_type (ShadowType.ETCHED_IN);
 
645
                }
 
646
                else{
 
647
                        label.margin_left = 0;
 
648
                }
 
649
                
 
650
                return label;
 
651
        }
 
652
 
 
653
        // add_text_view
 
654
        private Gtk.TextView add_text_view(
 
655
                Gtk.Box box, string text){
 
656
 
 
657
                // ScrolledWindow
 
658
                var scrolled = new Gtk.ScrolledWindow(null, null);
 
659
                scrolled.hscrollbar_policy = PolicyType.NEVER;
 
660
                scrolled.vscrollbar_policy = PolicyType.ALWAYS;
 
661
                scrolled.expand = true;
 
662
                box.add(scrolled);
 
663
                
 
664
                var view = new Gtk.TextView();
 
665
                view.wrap_mode = Gtk.WrapMode.WORD_CHAR;
 
666
                view.accepts_tab = false;
 
667
                view.editable = false;
 
668
                view.cursor_visible = false;
 
669
                view.buffer.text = text;
 
670
                view.sensitive = false;
 
671
                scrolled.add (view);
 
672
 
 
673
                return view;
 
674
        }
 
675
                
 
676
        // add_label
 
677
        private Gtk.Label add_label(
 
678
                Gtk.Box box, string text, bool bold = false,
 
679
                bool italic = false, bool large = false){
 
680
                        
 
681
                string msg = "<span%s%s%s>%s</span>".printf(
 
682
                        (bold ? " weight=\"bold\"" : ""),
 
683
                        (italic ? " style=\"italic\"" : ""),
 
684
                        (large ? " size=\"x-large\"" : ""),
 
685
                        text);
 
686
                        
 
687
                var label = new Gtk.Label(msg);
 
688
                label.set_use_markup(true);
 
689
                label.xalign = (float) 0.0;
 
690
                label.wrap = true;
 
691
                label.wrap_mode = Pango.WrapMode.WORD;
 
692
                box.add(label);
 
693
                return label;
 
694
        }
 
695
 
 
696
        private string format_text(
 
697
                string text,
 
698
                bool bold = false, bool italic = false, bool large = false){
 
699
                        
 
700
                string msg = "<span%s%s%s>%s</span>".printf(
 
701
                        (bold ? " weight=\"bold\"" : ""),
 
702
                        (italic ? " style=\"italic\"" : ""),
 
703
                        (large ? " size=\"x-large\"" : ""),
 
704
                        escape_html(text));
 
705
                        
 
706
                return msg;
 
707
        }
 
708
 
 
709
        // add_label_header
 
710
        private Gtk.Label add_label_header(
 
711
                Gtk.Box box, string text, bool large_heading = false){
 
712
                
 
713
                var label = add_label(box, escape_html(text), true, false, large_heading);
 
714
                label.margin_bottom = 12;
 
715
                return label;
 
716
        }
 
717
 
 
718
        // add_label_subnote
 
719
        private Gtk.Label add_label_subnote(
 
720
                Gtk.Box box, string text){
 
721
                
 
722
                var label = add_label(box, text, false, true);
 
723
                label.margin_left = 6;
 
724
                return label;
 
725
        }
 
726
 
 
727
        // add_radio
 
728
        private Gtk.RadioButton add_radio(
 
729
                Gtk.Box box, string text, Gtk.RadioButton? another_radio_in_group){
 
730
 
 
731
                Gtk.RadioButton radio = null;
 
732
 
 
733
                if (another_radio_in_group == null){
 
734
                        radio = new Gtk.RadioButton(null);
 
735
                }
 
736
                else{
 
737
                        radio = new Gtk.RadioButton.from_widget(another_radio_in_group);
 
738
                }
 
739
 
 
740
                radio.label = text;
 
741
                
 
742
                box.add(radio);
 
743
 
 
744
                foreach(var child in radio.get_children()){
 
745
                        if (child is Gtk.Label){
 
746
                                var label = (Gtk.Label) child;
 
747
                                label.use_markup = true;
 
748
                                break;
 
749
                        }
 
750
                }
 
751
                
 
752
                return radio;
 
753
        }
 
754
 
 
755
        // add_checkbox
 
756
        private Gtk.CheckButton add_checkbox(
 
757
                Gtk.Box box, string text){
 
758
 
 
759
                var chk = new Gtk.CheckButton.with_label(text);
 
760
                chk.label = text;
 
761
                box.add(chk);
 
762
 
 
763
                foreach(var child in chk.get_children()){
 
764
                        if (child is Gtk.Label){
 
765
                                var label = (Gtk.Label) child;
 
766
                                label.use_markup = true;
 
767
                                break;
 
768
                        }
 
769
                }
 
770
                
 
771
                /*
 
772
                chk.toggled.connect(()=>{
 
773
                        chk.active;
 
774
                });
 
775
                */
 
776
 
 
777
                return chk;
 
778
        }
 
779
 
 
780
        // add_spin
 
781
        private Gtk.SpinButton add_spin(
 
782
                Gtk.Box box, double min, double max, double val,
 
783
                int digits = 0, double step = 1, double step_page = 1){
 
784
 
 
785
                var adj = new Gtk.Adjustment(val, min, max, step, step_page, 0);
 
786
                var spin  = new Gtk.SpinButton(adj, step, digits);
 
787
                spin.xalign = (float) 0.5;
 
788
                box.add(spin);
 
789
 
 
790
                /*
 
791
                spin.value_changed.connect(()=>{
 
792
                        label.sensitive = spin.sensitive;
 
793
                });
 
794
                */
 
795
 
 
796
                return spin;
 
797
        }
 
798
 
 
799
        // add_button
 
800
        private Gtk.Button add_button(
 
801
                Gtk.Box box, string text, string tooltip,
 
802
                ref Gtk.SizeGroup? size_group,
 
803
                Gtk.Image? icon = null){
 
804
                        
 
805
                var button = new Gtk.Button();
 
806
        box.add(button);
 
807
 
 
808
        button.set_label(text);
 
809
        button.set_tooltip_text(tooltip);
 
810
 
 
811
        if (icon != null){
 
812
                        button.set_image(icon);
 
813
                        button.set_always_show_image(true);
 
814
                }
 
815
 
 
816
                if (size_group == null){
 
817
                        size_group = new Gtk.SizeGroup(SizeGroupMode.HORIZONTAL);
 
818
                }
 
819
                
 
820
                size_group.add_widget(button);
 
821
                
 
822
        return button;
 
823
        }
 
824
 
 
825
        // add_toggle_button
 
826
        private Gtk.ToggleButton add_toggle_button(
 
827
                Gtk.Box box, string text, string tooltip,
 
828
                ref Gtk.SizeGroup? size_group,
 
829
                Gtk.Image? icon = null){
 
830
                        
 
831
                var button = new Gtk.ToggleButton();
 
832
        box.add(button);
 
833
 
 
834
        button.set_label(text);
 
835
        button.set_tooltip_text(tooltip);
 
836
 
 
837
        if (icon != null){
 
838
                        button.set_image(icon);
 
839
                        button.set_always_show_image(true);
 
840
                }
 
841
 
 
842
                if (size_group == null){
 
843
                        size_group = new Gtk.SizeGroup(SizeGroupMode.HORIZONTAL);
 
844
                }
 
845
                
 
846
                size_group.add_widget(button);
 
847
                
 
848
        return button;
 
849
        }
 
850
        
 
851
        // add_directory_chooser
 
852
        private Gtk.Entry add_directory_chooser(
 
853
                Gtk.Box box, string selected_directory, Gtk.Window parent_window){
 
854
                        
 
855
                // Entry
 
856
                var entry = new Gtk.Entry();
 
857
                entry.hexpand = true;
 
858
                //entry.margin_left = 6;
 
859
                entry.secondary_icon_stock = "gtk-open";
 
860
                entry.placeholder_text = _("Enter path or browse for directory");
 
861
                box.add (entry);
 
862
 
 
863
                if ((selected_directory != null) && dir_exists(selected_directory)){
 
864
                        entry.text = selected_directory;
 
865
                }
 
866
 
 
867
                entry.icon_release.connect((p0, p1) => {
 
868
                        //chooser
 
869
                        var chooser = new Gtk.FileChooserDialog(
 
870
                            _("Select Path"),
 
871
                            parent_window,
 
872
                            FileChooserAction.SELECT_FOLDER,
 
873
                            "_Cancel",
 
874
                            Gtk.ResponseType.CANCEL,
 
875
                            "_Open",
 
876
                            Gtk.ResponseType.ACCEPT
 
877
                        );
 
878
 
 
879
                        chooser.select_multiple = false;
 
880
                        chooser.set_filename(selected_directory);
 
881
 
 
882
                        if (chooser.run() == Gtk.ResponseType.ACCEPT) {
 
883
                                entry.text = chooser.get_filename();
 
884
 
 
885
                                //App.repo = new SnapshotRepo.from_path(entry.text, this);
 
886
                                //check_backup_location();
 
887
                        }
 
888
 
 
889
                        chooser.destroy();
 
890
                });
 
891
 
 
892
                return entry;
 
893
        }
 
894
 
 
895
 
 
896
}
 
897