Skip to content
API Documentation
BastionGuard™
GtkHelper.vala
Go to the documentation of this file.
1
2
7using TeeJee.System;
8using TeeJee.Misc;
9
11
12 using Gtk;
13
14 // messages -----------
15
16 public void gtk_do_events (){
17
18 /* Do pending events */
19
20 while (GLib.MainContext.default().pending())
21 GLib.MainContext.default().iteration(false);
22 }
23
24 public void gtk_set_busy (bool busy, Gtk.Window win) {
25
26 /* Show or hide busy cursor on window */
27 if (busy)
28 win.set_cursor_from_name("wait");
29 else
30 win.set_cursor_from_name(null);
31
33 }
34
35 public static void gtk_messagebox(string title, string message, Gtk.Window? parent_win, bool is_error = false){
36
37 /* Shows a simple message box */
38
39 Gtk.MessageType type = is_error ? Gtk.MessageType.ERROR : Gtk.MessageType.INFO;
40
41 var dlg = new CustomMessageDialog(title,message,type,parent_win, Gtk.ButtonsType.OK);
42 dlg.present();
43 dlg.destroy();
44 }
45
46 public string? gtk_inputbox(string title, string message, Gtk.Window? parent_win, bool mask_password = false){
47
48 Gtk.Box vbox_main = new Gtk.Box(Orientation.VERTICAL, 0);
49 vbox_main.margin_start = 0;
50 vbox_main.margin_end = 0;
51 vbox_main.margin_top = 0;
52 vbox_main.margin_bottom = 0;
53
54 Gtk.Label lbl_input = new Gtk.Label(title);
55 lbl_input.xalign = (float) 0.0;
56 lbl_input.label = message;
57
58 Gtk.Entry txt_input = new Gtk.Entry();
59 txt_input.margin_top = 3;
60 txt_input.set_visibility(!mask_password);
61
62 var dlg = new Gtk.Dialog.with_buttons(title, parent_win, DialogFlags.MODAL);
63 dlg.title = title;
64 dlg.set_default_size (300, -1);
65 if (parent_win != null){
66 dlg.set_transient_for(parent_win);
67 dlg.set_modal(true);
68 }
69
70 var content = (Box) dlg.get_content_area ();
71 vbox_main.append (lbl_input);
72 vbox_main.append (txt_input);
73 content.append(vbox_main);
74 content.margin_start = 6;
75 content.margin_end = 6;
76 content.margin_top = 6;
77 content.margin_bottom = 6;
78 dlg.add_button(_("OK"),Gtk.ResponseType.OK);
79 dlg.add_button(_("Cancel"),Gtk.ResponseType.CANCEL);
80 txt_input.activate.connect (() => { dlg.response(Gtk.ResponseType.OK); });
81
82 int response = gtk_dialog_run(dlg);
83 string input_text = txt_input.text;
84 dlg.destroy();
85
86 if (response == Gtk.ResponseType.CANCEL){
87 return null;
88 }
89 else{
90 return input_text;
91 }
92 }
93
94 public void wait_and_close_window(int milliseconds, Gtk.Window window){
96 int millis = 0;
97 while(millis < milliseconds){
98 sleep(200);
99 millis += 200;
101 }
102 window.destroy();
103 }
104
105 // combo ---------
106
107
108 public int gtk_dialog_run(Gtk.Dialog dlg){
109 int result = Gtk.ResponseType.CLOSE;
110 var loop = new GLib.MainLoop(null, false);
111 dlg.response.connect((response_id) => { result = response_id; loop.quit(); });
112 dlg.close_request.connect(() => { result = Gtk.ResponseType.CLOSE; if (loop.is_running()) loop.quit(); return false; });
113 dlg.present();
114 loop.run();
115 return result;
116 }
117
118 public bool gtk_combobox_set_value (Gtk.ComboBoxText combo, int index, string val){
119
120 /* Convenience function to set combobox value */
121
122 for (int i = 0; ; i++) {
123 combo.active = i;
124 var text = combo.get_active_text();
125 if (text == null) {
126 combo.active = -1;
127 return false;
128 }
129 if (text == val) {
130 return true;
131 }
132 }
133 }
134
135 public string gtk_combobox_get_value (Gtk.ComboBoxText combo, int index, string default_value){
136
137 /* Convenience function to get combobox value */
138
139 var text = combo.get_active_text();
140 return (text != null) ? text : default_value;
141 }
142
143 // utility ------------------
144
145 // add_stack
146 public static Gtk.Stack add_notebook(Gtk.Box box, bool show_tabs = true, bool show_border = true){
147
148 var stack = new Gtk.Stack();
149 stack.margin_start = 0;
150 stack.margin_end = 0;
151 stack.margin_top = 0;
152 stack.margin_bottom = 0;
153 stack.hexpand = true;
154 stack.vexpand = true;
155 box.append(stack);
156
157 return stack;
158 }
159
160 // add_listbox
161 public static Gtk.ListBox add_treeview(Gtk.Box box, Gtk.SelectionMode selection_mode = Gtk.SelectionMode.SINGLE){
162
163 var listbox = new Gtk.ListBox();
164 listbox.selection_mode = selection_mode;
165
166 var scrollwin = new Gtk.ScrolledWindow();
167 scrollwin.set_child(listbox);
168 scrollwin.hexpand = true;
169 scrollwin.vexpand = true;
170 box.append(scrollwin);
171
172 return listbox;
173 }
174
175 // GTK4 compatibility shim: tree columns were removed.
176 public static Gtk.Label add_column_text(Gtk.ListBox listbox, string title, out Gtk.Label cell){
177 cell = new Gtk.Label(title);
178 cell.xalign = 0.0f;
179 return cell;
180 }
181
182 // GTK4 compatibility shim: tree columns/cell renderers were removed.
183 public static Gtk.Box add_column_icon_radio_text(Gtk.ListBox listbox, string title,
184 out Gtk.Image cell_pix, out Gtk.CheckButton cell_radio, out Gtk.Label cell_text){
185 var row = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 6);
186 cell_pix = new Gtk.Image();
187 cell_radio = new Gtk.CheckButton();
188 cell_text = new Gtk.Label(title);
189 cell_text.xalign = 0.0f;
190 row.append(cell_pix);
191 row.append(cell_radio);
192 row.append(cell_text);
193 return row;
194 }
195
196 // add_label_scrolled
197 public static Gtk.Label add_label_scrolled(Gtk.Box box, string text, bool show_border = false, bool wrap = false, int ellipsize_chars = 40){
198
199 // ScrolledWindow
200 var scroll = new Gtk.ScrolledWindow();
201 scroll.hscrollbar_policy = PolicyType.NEVER;
202 scroll.vscrollbar_policy = PolicyType.ALWAYS;
203 scroll.hexpand = true;
204 scroll.vexpand = true;
205 box.append(scroll);
206
207 var label = new Gtk.Label(text);
208 label.xalign = (float) 0.0;
209 label.yalign = (float) 0.0;
210 label.margin_start = 6;
211 label.margin_end = 6;
212 label.margin_top = 6;
213 label.margin_bottom = 6;
214 label.set_use_markup(true);
215 scroll.set_child(label);
216
217 if (wrap){
218 label.wrap = true;
219 label.wrap_mode = Pango.WrapMode.WORD;
220 }
221 else {
222 label.wrap = false;
223 label.ellipsize = Pango.EllipsizeMode.MIDDLE;
224 label.max_width_chars = ellipsize_chars;
225 }
226
227 if (show_border){
228 }
229
230 return label;
231 }
232
233 // add_label
234 private Gtk.Label add_label(Gtk.Box box, string text, bool bold = false, bool italic = false, bool large = false){
235
236 string msg = "<span%s%s%s>%s</span>".printf(
237 (bold ? " weight=\"bold\"" : ""),
238 (italic ? " style=\"italic\"" : ""),
239 (large ? " size=\"x-large\"" : ""),
240 text);
241
242 var label = new Gtk.Label(msg);
243 label.set_use_markup(true);
244 label.xalign = (float) 0.0;
245 label.wrap = true;
246 label.wrap_mode = Pango.WrapMode.WORD;
247 box.append(label);
248 return label;
249 }
250
251 public static string format_text(string text, bool bold = false, bool italic = false, bool large = false){
252
253 string msg = "<span%s%s%s>%s</span>".printf(
254 (bold ? " weight=\"bold\"" : ""),
255 (italic ? " style=\"italic\"" : ""),
256 (large ? " size=\"x-large\"" : ""),
257 escape_html(text));
258
259 return msg;
260 }
261
262 // add_label_header
263 public static Gtk.Label add_label_header(Gtk.Box box, string text, bool large_heading = false){
264
265 var label = add_label(box, escape_html(text), true, false, large_heading);
266 label.margin_bottom = 12;
267 return label;
268 }
269
270 // add_checkbox
271 public static Gtk.CheckButton add_checkbox(Gtk.Box box, string text){
272
273 var chk = new Gtk.CheckButton.with_label(text);
274 chk.label = text;
275 box.append(chk);
276
277
278 /*
279 chk.toggled.connect(()=>{
280 chk.active;
281 });
282 */
283
284 return chk;
285 }
286
287 // add_spin
288 public static Gtk.SpinButton add_spin(Gtk.Box box, double min, double max, double val, int digits = 0, double step = 1, double step_page = 1){
289
290 var adj = new Gtk.Adjustment(val, min, max, step, step_page, 0);
291 var spin = new Gtk.SpinButton(adj, step, digits);
292 spin.xalign = (float) 0.5;
293 box.append(spin);
294
295 /*
296 spin.value_changed.connect(()=>{
297 label.sensitive = spin.sensitive;
298 });
299 */
300
301 return spin;
302 }
303
304 // add_button
305 public static Gtk.Button add_button(Gtk.Box box, string text, string tooltip, Gtk.SizeGroup? size_group, Gtk.Image? icon = null){
306
307 var button = new Gtk.Button();
308 box.append(button);
309
310 button.set_label(text);
311 button.set_tooltip_text(tooltip);
312
313 if (icon != null){
314 button.set_child(icon);
315 }
316
317 if (size_group != null){
318 size_group.add_widget(button);
319 }
320
321 return button;
322 }
323
324 public static Gtk.Box add_button_box(Gtk.Box box, Gtk.Orientation orientation = Gtk.Orientation.HORIZONTAL,
325 Gtk.Align halign = Gtk.Align.CENTER, int spacing = 6){
326
327 var bbox = new Gtk.Box(orientation, spacing);
328 bbox.halign = halign;
329 box.append(bbox);
330
331 return bbox;
332 }
333}
334
#define _(s)
CustomMessageDialog(string _msg_title, string _msg_body, Gtk.MessageType _msg_type, Window? parent, Gtk.ButtonsType _buttons_type)
static Gtk.Label add_column_text(Gtk.ListBox listbox, string title, out Gtk.Label cell)
string gtk_combobox_get_value(Gtk.ComboBoxText combo, int index, string default_value)
string? gtk_inputbox(string title, string message, Gtk.Window? parent_win, bool mask_password=false)
static Gtk.SpinButton add_spin(Gtk.Box box, double min, double max, double val, int digits=0, double step=1, double step_page=1)
void gtk_set_busy(bool busy, Gtk.Window win)
static Gtk.Box add_button_box(Gtk.Box box, Gtk.Orientation orientation=Gtk.Orientation.HORIZONTAL, Gtk.Align halign=Gtk.Align.CENTER, int spacing=6)
Gtk.Label add_label(Gtk.Box box, string text, bool bold=false, bool italic=false, bool large=false)
int gtk_dialog_run(Gtk.Dialog dlg)
static Gtk.Button add_button(Gtk.Box box, string text, string tooltip, Gtk.SizeGroup? size_group, Gtk.Image? icon=null)
static Gtk.Label add_label_header(Gtk.Box box, string text, bool large_heading=false)
static Gtk.Box add_column_icon_radio_text(Gtk.ListBox listbox, string title, out Gtk.Image cell_pix, out Gtk.CheckButton cell_radio, out Gtk.Label cell_text)
static Gtk.Stack add_notebook(Gtk.Box box, bool show_tabs=true, bool show_border=true)
static Gtk.CheckButton add_checkbox(Gtk.Box box, string text)
static Gtk.ListBox add_treeview(Gtk.Box box, Gtk.SelectionMode selection_mode=Gtk.SelectionMode.SINGLE)
bool gtk_combobox_set_value(Gtk.ComboBoxText combo, int index, string val)
static string format_text(string text, bool bold=false, bool italic=false, bool large=false)
static void gtk_messagebox(string title, string message, Gtk.Window? parent_win, bool is_error=false)
static Gtk.Label add_label_scrolled(Gtk.Box box, string text, bool show_border=false, bool wrap=false, int ellipsize_chars=40)
void wait_and_close_window(int milliseconds, Gtk.Window window)
string escape_html(string html)
void sleep(int milliseconds)