4
using TeeJee.FileSystem;
5
using TeeJee.JsonHelper;
6
using TeeJee.ProcessHelper;
10
namespace TeeJee.GtkHelper{
14
// messages -----------
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);
26
public void gtk_do_events (){
28
/* Do pending events */
30
while(Gtk.events_pending ())
31
Gtk.main_iteration ();
34
public void gtk_set_busy (bool busy, Gtk.Window win) {
36
/* Show or hide busy cursor on window */
38
Gdk.Cursor? cursor = null;
41
cursor = new Gdk.Cursor(Gdk.CursorType.WATCH);
44
cursor = new Gdk.Cursor(Gdk.CursorType.ARROW);
47
var window = win.get_window ();
50
window.set_cursor (cursor);
56
public void gtk_messagebox(
57
string title, string message, Gtk.Window? parent_win, bool is_error = false){
59
/* Shows a simple message box */
61
var type = Gtk.MessageType.INFO;
63
type = Gtk.MessageType.ERROR;
66
type = Gtk.MessageType.INFO;
69
/*var dlg = new Gtk.MessageDialog.with_markup(null, Gtk.DialogFlags.MODAL, type, Gtk.ButtonsType.OK, message);
71
dlg.set_default_size (200, -1);
72
if (parent_win != null){
73
dlg.set_transient_for(parent_win);
79
var dlg = new CustomMessageDialog(title,message,type,parent_win, Gtk.ButtonsType.OK);
83
public string gtk_inputbox(
84
string title, string message, Gtk.Window? parent_win, bool mask_password = false){
86
/* Shows a simple input prompt */
89
Gtk.Box vbox_main = new Box (Orientation.VERTICAL, 0);
93
Gtk.Label lbl_input = new Gtk.Label(title);
94
lbl_input.xalign = (float) 0.0;
95
lbl_input.label = message;
98
Gtk.Entry txt_input = new Gtk.Entry();
99
txt_input.margin_top = 3;
100
txt_input.set_visibility(false);
103
var dlg = new Gtk.Dialog.with_buttons(title, parent_win, DialogFlags.MODAL);
105
dlg.set_default_size (300, -1);
106
if (parent_win != null){
107
dlg.set_transient_for(parent_win);
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);
118
dlg.add_button(_("OK"),Gtk.ResponseType.OK);
119
dlg.add_button(_("Cancel"),Gtk.ResponseType.CANCEL);
122
txt_input.key_press_event.connect ((w, event) => {
123
if (event.keyval == 65293) {
124
dlg.response(Gtk.ResponseType.OK);
131
int response = dlg.run();
132
string input_text = txt_input.text;
135
if (response == Gtk.ResponseType.CANCEL){
146
public bool gtk_combobox_set_value (ComboBox combo, int index, string val){
148
/* Conveniance function to set combobox value */
152
TreeModel model = (TreeModel) combo.model;
154
bool iterExists = model.get_iter_first (out iter);
156
model.get(iter, 1, out comboVal);
157
if (comboVal == val){
158
combo.set_active_iter(iter);
161
iterExists = model.iter_next (ref iter);
167
public string gtk_combobox_get_value (ComboBox combo, int index, string default_value){
169
/* Conveniance function to get combobox value */
171
if ((combo.model == null) || (combo.active < 0)) { return default_value; }
175
combo.get_active_iter (out iter);
176
TreeModel model = (TreeModel) combo.model;
177
model.get(iter, index, out val);
182
public GLib.Object gtk_combobox_get_selected_object (
185
GLib.Object default_value){
187
/* Conveniance function to get combobox value */
189
if ((combo.model == null) || (combo.active < 0)) { return default_value; }
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);
200
public int gtk_combobox_get_value_enum (ComboBox combo, int index, int default_value){
202
/* Conveniance function to get combobox value */
204
if ((combo.model == null) || (combo.active < 0)) { return default_value; }
208
combo.get_active_iter (out iter);
209
TreeModel model = (TreeModel) combo.model;
210
model.get(iter, index, out val);
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;
227
public Gtk.Image? get_shared_icon(
229
string fallback_icon_file_name,
231
string icon_directory = AppShortName + "/images"){
233
Gdk.Pixbuf pix_icon = null;
234
Gtk.Image img_icon = null;
237
Gtk.IconTheme icon_theme = Gtk.IconTheme.get_default();
238
pix_icon = icon_theme.load_icon (icon_name, icon_size, 0);
240
//log_error (e.message);
243
string fallback_icon_file_path = "/usr/share/%s/%s".printf(icon_directory, fallback_icon_file_name);
245
if (pix_icon == null){
247
pix_icon = new Gdk.Pixbuf.from_file_at_size (fallback_icon_file_path, icon_size, icon_size);
249
log_error (e.message);
253
if (pix_icon == null){
254
log_error (_("Missing Icon") + ": '%s', '%s'".printf(icon_name, fallback_icon_file_path));
257
img_icon = new Gtk.Image.from_pixbuf(pix_icon);
263
public Gdk.Pixbuf? get_shared_icon_pixbuf(string icon_name,
264
string fallback_file_name,
266
string icon_directory = AppShortName + "/images"){
268
var img = get_shared_icon(icon_name, fallback_file_name, icon_size, icon_directory);
269
var pixbuf = (img == null) ? null : img.pixbuf;
273
// styles ----------------
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);
280
css_provider.load_from_data(css,-1);
281
} catch (GLib.Error e) {
285
foreach(var widget in widgets){
287
widget.get_style_context().add_provider(
288
css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
290
widget.get_style_context().add_class("style_%d".printf(CSS_AUTO_CLASS_INDEX));
294
// treeview -----------------
296
public int gtk_treeview_model_count(TreeModel model){
299
if (model.get_iter_first(out iter)){
301
while(model.iter_next(ref iter)){
308
public void gtk_stripe_row(
309
Gtk.CellRenderer cell,
311
string odd_color = "#F4F6F7",
312
string even_color = "#FFFFFF"){
314
if (cell is Gtk.CellRendererText){
315
(cell as Gtk.CellRendererText).background = odd_row ? odd_color : even_color;
317
else if (cell is Gtk.CellRendererPixbuf){
318
(cell as Gtk.CellRendererPixbuf).cell_background = odd_row ? odd_color : even_color;
322
public void gtk_treeview_redraw(Gtk.TreeView treeview){
323
var model = treeview.model;
324
treeview.model = null;
325
treeview.model = model;
330
public void gtk_menu_add_separator(Gtk.Menu menu){
331
Gdk.RGBA gray = Gdk.RGBA();
332
gray.parse ("rgba(200,200,200,1)");
335
var menu_item = new Gtk.SeparatorMenuItem();
336
menu_item.override_color (StateFlags.NORMAL, gray);
337
menu.append(menu_item);
340
public Gtk.MenuItem gtk_menu_add_item(
344
Gtk.Image? icon_image,
345
Gtk.SizeGroup? sg_icon = null,
346
Gtk.SizeGroup? sg_label = null){
348
var menu_item = new Gtk.MenuItem();
349
menu.append(menu_item);
351
var box = new Gtk.Box(Orientation.HORIZONTAL, 3);
356
if (icon_image == null){
357
var dummy = new Gtk.Label("");
360
if (sg_icon != null){
361
sg_icon.add_widget(dummy);
367
if (sg_icon != null){
368
sg_icon.add_widget(icon_image);
374
var lbl = new Gtk.Label(label);
375
lbl.xalign = (float) 0.0;
376
lbl.margin_right = 6;
379
if (sg_label != null){
380
sg_label.add_widget(lbl);
383
box.set_tooltip_text(tooltip);
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;
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){
411
// file chooser ----------------
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);