~teejee2008/timeshift/trunk

« back to all changes in this revision

Viewing changes to src/Gtk/GtkHelper.vala

  • Committer: Tony George
  • Date: 2016-08-13 04:16:47 UTC
  • Revision ID: tony.george.kol@gmail.com-20160813041647-ivf2g6rszt00xco5
Updated project structure

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
        }
 
82
 
 
83
        public string gtk_inputbox(
 
84
                string title, string message, Gtk.Window? parent_win, bool mask_password = false){
 
85
 
 
86
                /* Shows a simple input prompt */
 
87
 
 
88
                //vbox_main
 
89
        Gtk.Box vbox_main = new Box (Orientation.VERTICAL, 0);
 
90
        vbox_main.margin = 6;
 
91
 
 
92
                //lbl_input
 
93
                Gtk.Label lbl_input = new Gtk.Label(title);
 
94
                lbl_input.xalign = (float) 0.0;
 
95
                lbl_input.label = message;
 
96
 
 
97
                //txt_input
 
98
                Gtk.Entry txt_input = new Gtk.Entry();
 
99
                txt_input.margin_top = 3;
 
100
                txt_input.set_visibility(false);
 
101
 
 
102
                //create dialog
 
103
                var dlg = new Gtk.Dialog.with_buttons(title, parent_win, DialogFlags.MODAL);
 
104
                dlg.title = title;
 
105
                dlg.set_default_size (300, -1);
 
106
                if (parent_win != null){
 
107
                        dlg.set_transient_for(parent_win);
 
108
                        dlg.set_modal(true);
 
109
                }
 
110
 
 
111
                //add widgets
 
112
                Gtk.Box content = (Box) dlg.get_content_area ();
 
113
                vbox_main.pack_start (lbl_input, false, true, 0);
 
114
                vbox_main.pack_start (txt_input, false, true, 0);
 
115
                content.add(vbox_main);
 
116
 
 
117
                //add buttons
 
118
                dlg.add_button(_("OK"),Gtk.ResponseType.OK);
 
119
                dlg.add_button(_("Cancel"),Gtk.ResponseType.CANCEL);
 
120
 
 
121
                //keyboard shortcuts
 
122
                txt_input.key_press_event.connect ((w, event) => {
 
123
                        if (event.keyval == 65293) {
 
124
                                dlg.response(Gtk.ResponseType.OK);
 
125
                                return true;
 
126
                        }
 
127
                        return false;
 
128
                });
 
129
 
 
130
                dlg.show_all();
 
131
                int response = dlg.run();
 
132
                string input_text = txt_input.text;
 
133
                dlg.destroy();
 
134
 
 
135
                if (response == Gtk.ResponseType.CANCEL){
 
136
                        return "";
 
137
                }
 
138
                else{
 
139
                        return input_text;
 
140
                }
 
141
        }
 
142
 
 
143
 
 
144
        // combo ---------
 
145
        
 
146
        public bool gtk_combobox_set_value (ComboBox combo, int index, string val){
 
147
 
 
148
                /* Conveniance function to set combobox value */
 
149
 
 
150
                TreeIter iter;
 
151
                string comboVal;
 
152
                TreeModel model = (TreeModel) combo.model;
 
153
 
 
154
                bool iterExists = model.get_iter_first (out iter);
 
155
                while (iterExists){
 
156
                        model.get(iter, 1, out comboVal);
 
157
                        if (comboVal == val){
 
158
                                combo.set_active_iter(iter);
 
159
                                return true;
 
160
                        }
 
161
                        iterExists = model.iter_next (ref iter);
 
162
                }
 
163
 
 
164
                return false;
 
165
        }
 
166
 
 
167
        public string gtk_combobox_get_value (ComboBox combo, int index, string default_value){
 
168
 
 
169
                /* Conveniance function to get combobox value */
 
170
 
 
171
                if ((combo.model == null) || (combo.active < 0)) { return default_value; }
 
172
 
 
173
                TreeIter iter;
 
174
                string val = "";
 
175
                combo.get_active_iter (out iter);
 
176
                TreeModel model = (TreeModel) combo.model;
 
177
                model.get(iter, index, out val);
 
178
 
 
179
                return val;
 
180
        }
 
181
 
 
182
        public GLib.Object gtk_combobox_get_selected_object (
 
183
                ComboBox combo,
 
184
                int index,
 
185
                GLib.Object default_value){
 
186
 
 
187
                /* Conveniance function to get combobox value */
 
188
 
 
189
                if ((combo.model == null) || (combo.active < 0)) { return default_value; }
 
190
 
 
191
                TreeIter iter;
 
192
                GLib.Object val = null;
 
193
                combo.get_active_iter (out iter);
 
194
                TreeModel model = (TreeModel) combo.model;
 
195
                model.get(iter, index, out val);
 
196
 
 
197
                return val;
 
198
        }
 
199
        
 
200
        public int gtk_combobox_get_value_enum (ComboBox combo, int index, int default_value){
 
201
 
 
202
                /* Conveniance function to get combobox value */
 
203
 
 
204
                if ((combo.model == null) || (combo.active < 0)) { return default_value; }
 
205
 
 
206
                TreeIter iter;
 
207
                int val;
 
208
                combo.get_active_iter (out iter);
 
209
                TreeModel model = (TreeModel) combo.model;
 
210
                model.get(iter, index, out val);
 
211
 
 
212
                return val;
 
213
        }
 
214
 
 
215
        // icon -------
 
216
        
 
217
        public Gdk.Pixbuf? get_app_icon(int icon_size, string format = ".png"){
 
218
                var img_icon = get_shared_icon(AppShortName, AppShortName + format,icon_size,"pixmaps");
 
219
                if (img_icon != null){
 
220
                        return img_icon.pixbuf;
 
221
                }
 
222
                else{
 
223
                        return null;
 
224
                }
 
225
        }
 
226
 
 
227
        public Gtk.Image? get_shared_icon(
 
228
                string icon_name,
 
229
                string fallback_icon_file_name,
 
230
                int icon_size,
 
231
                string icon_directory = AppShortName + "/images"){
 
232
                        
 
233
                Gdk.Pixbuf pix_icon = null;
 
234
                Gtk.Image img_icon = null;
 
235
 
 
236
                try {
 
237
                        Gtk.IconTheme icon_theme = Gtk.IconTheme.get_default();
 
238
                        pix_icon = icon_theme.load_icon (icon_name, icon_size, 0);
 
239
                } catch (Error e) {
 
240
                        //log_error (e.message);
 
241
                }
 
242
 
 
243
                string fallback_icon_file_path = "/usr/share/%s/%s".printf(icon_directory, fallback_icon_file_name);
 
244
 
 
245
                if (pix_icon == null){
 
246
                        try {
 
247
                                pix_icon = new Gdk.Pixbuf.from_file_at_size (fallback_icon_file_path, icon_size, icon_size);
 
248
                        } catch (Error e) {
 
249
                                log_error (e.message);
 
250
                        }
 
251
                }
 
252
 
 
253
                if (pix_icon == null){
 
254
                        log_error (_("Missing Icon") + ": '%s', '%s'".printf(icon_name, fallback_icon_file_path));
 
255
                }
 
256
                else{
 
257
                        img_icon = new Gtk.Image.from_pixbuf(pix_icon);
 
258
                }
 
259
 
 
260
                return img_icon;
 
261
        }
 
262
 
 
263
        public Gdk.Pixbuf? get_shared_icon_pixbuf(string icon_name,
 
264
                string fallback_file_name,
 
265
                int icon_size,
 
266
                string icon_directory = AppShortName + "/images"){
 
267
                        
 
268
                var img = get_shared_icon(icon_name, fallback_file_name, icon_size, icon_directory);
 
269
                var pixbuf = (img == null) ? null : img.pixbuf;
 
270
                return pixbuf;
 
271
        }
 
272
 
 
273
        // styles ----------------
 
274
 
 
275
        public static int CSS_AUTO_CLASS_INDEX = 0;
 
276
        public static void gtk_apply_css(Gtk.Widget[] widgets, string css_style){
 
277
                var css_provider = new Gtk.CssProvider();
 
278
                var css = ".style_%d { %s }".printf(++CSS_AUTO_CLASS_INDEX, css_style);
 
279
                try {
 
280
                        css_provider.load_from_data(css,-1);
 
281
                } catch (GLib.Error e) {
 
282
            warning(e.message);
 
283
        }
 
284
 
 
285
        foreach(var widget in widgets){
 
286
                        
 
287
                        widget.get_style_context().add_provider(
 
288
                                css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
 
289
                                
 
290
                        widget.get_style_context().add_class("style_%d".printf(CSS_AUTO_CLASS_INDEX));
 
291
                }
 
292
        }
 
293
        
 
294
        // treeview -----------------
 
295
        
 
296
        public int gtk_treeview_model_count(TreeModel model){
 
297
                int count = 0;
 
298
                TreeIter iter;
 
299
                if (model.get_iter_first(out iter)){
 
300
                        count++;
 
301
                        while(model.iter_next(ref iter)){
 
302
                                count++;
 
303
                        }
 
304
                }
 
305
                return count;
 
306
        }
 
307
 
 
308
        public void gtk_stripe_row(
 
309
                Gtk.CellRenderer cell,
 
310
                bool odd_row,
 
311
                string odd_color = "#F4F6F7",
 
312
                string even_color = "#FFFFFF"){
 
313
 
 
314
                if (cell is Gtk.CellRendererText){
 
315
                        (cell as Gtk.CellRendererText).background = odd_row ? odd_color : even_color;
 
316
                }
 
317
                else if (cell is Gtk.CellRendererPixbuf){
 
318
                        (cell as Gtk.CellRendererPixbuf).cell_background = odd_row ? odd_color : even_color;
 
319
                }
 
320
        }
 
321
 
 
322
        public void gtk_treeview_redraw(Gtk.TreeView treeview){
 
323
                var model = treeview.model;
 
324
                treeview.model = null;
 
325
                treeview.model = model;
 
326
        }
 
327
        
 
328
        // menu
 
329
        
 
330
        public void gtk_menu_add_separator(Gtk.Menu menu){
 
331
                Gdk.RGBA gray = Gdk.RGBA();
 
332
                gray.parse ("rgba(200,200,200,1)");
 
333
                
 
334
                // separator
 
335
                var menu_item = new Gtk.SeparatorMenuItem();
 
336
                menu_item.override_color (StateFlags.NORMAL, gray);
 
337
                menu.append(menu_item);
 
338
        }
 
339
 
 
340
        public Gtk.MenuItem gtk_menu_add_item(
 
341
                Gtk.Menu menu,
 
342
                string label,
 
343
                string tooltip,
 
344
                Gtk.Image? icon_image,
 
345
                Gtk.SizeGroup? sg_icon = null,
 
346
                Gtk.SizeGroup? sg_label = null){
 
347
 
 
348
                var menu_item = new Gtk.MenuItem();
 
349
                menu.append(menu_item);
 
350
                        
 
351
                var box = new Gtk.Box(Orientation.HORIZONTAL, 3);
 
352
                menu_item.add(box);
 
353
 
 
354
                // add icon
 
355
 
 
356
                if (icon_image == null){
 
357
                        var dummy = new Gtk.Label("");
 
358
                        box.add(dummy);
 
359
 
 
360
                        if (sg_icon != null){
 
361
                                sg_icon.add_widget(dummy);
 
362
                        }
 
363
                }
 
364
                else{
 
365
                        box.add(icon_image);
 
366
 
 
367
                        if (sg_icon != null){
 
368
                                sg_icon.add_widget(icon_image);
 
369
                        }
 
370
                }
 
371
                
 
372
                // add label
 
373
                
 
374
                var lbl = new Gtk.Label(label);
 
375
                lbl.xalign = (float) 0.0;
 
376
                lbl.margin_right = 6;
 
377
                box.add(lbl);
 
378
 
 
379
                if (sg_label != null){
 
380
                        sg_label.add_widget(lbl);
 
381
                }
 
382
 
 
383
                box.set_tooltip_text(tooltip);
 
384
 
 
385
                return menu_item;
 
386
        }
 
387
 
 
388
        // build ui
 
389
 
 
390
        public Gtk.Label gtk_box_add_header(Gtk.Box box, string text){
 
391
                var label = new Gtk.Label("<b>" + text + "</b>");
 
392
                label.set_use_markup(true);
 
393
                label.xalign = (float) 0.0;
 
394
                label.margin_bottom = 6;
 
395
                box.add(label);
 
396
 
 
397
                return label;
 
398
        }
 
399
 
 
400
        // misc
 
401
        
 
402
        public bool gtk_container_has_child(Gtk.Container container, Gtk.Widget widget){
 
403
                foreach(var child in container.get_children()){
 
404
                        if (child == widget){
 
405
                                return true;
 
406
                        }
 
407
                }
 
408
                return false;
 
409
        }
 
410
 
 
411
        // file chooser ----------------
 
412
        
 
413
        public Gtk.FileFilter create_file_filter(string group_name, string[] patterns) {
 
414
                var filter = new Gtk.FileFilter ();
 
415
                filter.set_filter_name(group_name);
 
416
                foreach(string pattern in patterns) {
 
417
                        filter.add_pattern (pattern);
 
418
                }
 
419
                return filter;
 
420
        }
 
421
}
 
422