mp3splt-gtk: src/widgets_helper.c Source File

mp3splt-gtk 0.9.3.1519

Loading...
Searching...
No Matches
widgets_helper.c
Go to the documentation of this file.
1/**********************************************************
2 * mp3splt-gtk -- utility based on mp3splt,
3 *
4 * for mp3/ogg splitting without decoding
5 *
6 * Copyright: (C) 2005-2014 Alexandru Munteanu
7 * Contact: m@ioalex.net
8 *
9 * http://mp3splt.sourceforge.net/
10 *
11 *********************************************************/
12
13/**********************************************************
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
28 * USA.
29 *
30 *********************************************************/
31
32/*!********************************************************
33 * \file
34 *
35 * this file contains the code for the widgets helpers.
36 ********************************************************/
37
38#include "widgets_helper.h"
39
40static guint _wh_add_row_to_table(GtkWidget *table);
41static void _wh_attach_to_table(GtkWidget *table, GtkWidget *widget,
42 guint start_column, guint end_column, guint row, int expand);
43static void _wh_add_in_table_with_label(GtkWidget *table, const gchar *label_text,
44 GtkWidget *widget, int expand);
45static void hide_window_from_button(GtkWidget *window, gpointer data);
46
56GtkWidget *wh_set_title_and_get_vbox(GtkWidget *widget, const gchar *title)
57{
58 GtkWidget *vbox = wh_vbox_new();
59
60 GtkWidget *label = gtk_label_new(NULL);
61 gtk_label_set_markup(GTK_LABEL(label), title);
62
63 GtkWidget *label_hbox = wh_hbox_new();
64 gtk_box_pack_start(GTK_BOX(label_hbox), label, FALSE, FALSE, 0);
65 gtk_box_pack_start(GTK_BOX(vbox), label_hbox, FALSE, FALSE, 5);
66
67 GtkWidget *hbox = wh_hbox_new();
68 gtk_box_pack_start(GTK_BOX(hbox), widget, TRUE, TRUE, 16);
69
70 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
71
72 return vbox;
73}
74
75GtkWidget *wh_new_table()
76{
77 GtkWidget *table = gtk_grid_new();
78 g_object_set_data(G_OBJECT(table), "rows", GINT_TO_POINTER(0));
79 gtk_grid_set_column_spacing(GTK_GRID(table), 5);
80 gtk_grid_set_row_spacing(GTK_GRID(table), 4);
81 return table;
82}
83
84void wh_add_in_table(GtkWidget *table, GtkWidget *widget)
85{
86 guint last_row = _wh_add_row_to_table(table);
87
88 _wh_attach_to_table(table, widget, 1, 3, last_row, TRUE);
89}
90
91void wh_add_in_table_with_label_expand(GtkWidget *table, const gchar *label_text, GtkWidget *widget)
92{
93 _wh_add_in_table_with_label(table, label_text, widget, TRUE);
94}
95
96void wh_add_in_table_with_label(GtkWidget *table, const gchar *label_text, GtkWidget *widget)
97{
98 _wh_add_in_table_with_label(table, label_text, widget, FALSE);
99}
100
101GtkWidget *wh_put_in_new_hbox_with_margin_level(GtkWidget *widget, gint margin_level)
102{
103 return wh_put_in_new_hbox(widget, 6 * margin_level, TRUE, TRUE);
104}
105
106GtkWidget *wh_put_in_new_hbox(GtkWidget *widget, gint margin, gboolean expand, gboolean fill)
107{
108 GtkWidget *hbox = wh_hbox_new();
109 gtk_box_pack_start(GTK_BOX(hbox), widget, expand, fill, margin);
110 return hbox;
111}
112
113void wh_put_in_hbox_and_attach_to_vbox(GtkWidget *widget, GtkWidget *vbox, gint vertical_margin)
114{
115 wh_put_in_hbox_and_attach_to_vbox_with_bottom_margin(widget, vbox, vertical_margin, -1);
116}
117
118void wh_put_in_hbox_and_attach_to_vbox_with_bottom_margin(GtkWidget *widget, GtkWidget *vbox,
119 gint vertical_margin, gint bottom_margin)
120{
121 GtkWidget *hbox = wh_hbox_new();
122 gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, FALSE, 0);
123 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, vertical_margin);
124
125 if (bottom_margin > 0)
126 {
127 GtkWidget *fake_hbox = wh_hbox_new();
128 gtk_box_pack_start(GTK_BOX(vbox), fake_hbox, FALSE, FALSE, bottom_margin);
129 }
130}
131
132GtkWidget *wh_new_entry(gpointer callback, ui_state *ui)
133{
134 GtkWidget *entry = gtk_entry_new();
135 gtk_editable_set_editable(GTK_EDITABLE(entry), TRUE);
136
137 if (callback)
138 {
139 g_signal_connect(G_OBJECT(entry), "changed", G_CALLBACK(callback), ui);
140 }
141
142 return entry;
143}
144
145GtkWidget *wh_new_button(const gchar *button_label)
146{
147 return gtk_button_new_with_mnemonic(button_label);
148}
149
150void wh_get_widget_size(GtkWidget *widget, gint *width, gint *height)
151{
152 if (width != NULL)
153 {
154 *width = gtk_widget_get_allocated_width(widget);
155 }
156
157 if (height != NULL)
158 {
159 *height = gtk_widget_get_allocated_height(widget);
160 }
161}
162
163GtkWidget *wh_create_int_spinner_in_box_with_top_width(gchar *before_label, gchar *after_label,
164 gdouble initial_value, gdouble minimum_value, gdouble maximum_value,
165 gdouble step_increment, gdouble page_increment,
166 gchar *after_newline_label,
167 void (*spinner_callback)(GtkWidget *spinner, ui_state *ui),
168 ui_state *ui, GtkWidget *box, gint top_width)
169{
170 GtkWidget *horiz_fake = wh_hbox_new();
171 GtkWidget *label = gtk_label_new(before_label);
172 gtk_box_pack_start(GTK_BOX(horiz_fake), label, FALSE, FALSE, 0);
173
174 GtkAdjustment *adj = (GtkAdjustment *)
175 gtk_adjustment_new(initial_value, minimum_value, maximum_value, step_increment, page_increment, 0.0);
176
177 GtkWidget *spinner = gtk_spin_button_new(adj, 0, 0);
178
179 gtk_spin_button_set_wrap(GTK_SPIN_BUTTON(spinner), TRUE);
180 g_signal_connect(G_OBJECT(spinner), "value_changed", G_CALLBACK(spinner_callback), ui);
181 gtk_box_pack_start(GTK_BOX(horiz_fake), spinner, FALSE, FALSE, 5);
182
183 if (after_label != NULL)
184 {
185 gtk_box_pack_start(GTK_BOX(horiz_fake), gtk_label_new(after_label), FALSE, FALSE, 3);
186 }
187
188 GtkWidget *fake = wh_hbox_new();
189 gtk_box_pack_start(GTK_BOX(box), fake, FALSE, FALSE, top_width);
190
191 gtk_box_pack_start(GTK_BOX(box), horiz_fake, FALSE, FALSE, 1);
192
193 if (after_newline_label != NULL)
194 {
195 horiz_fake = wh_hbox_new();
196 gtk_box_pack_start(GTK_BOX(horiz_fake), gtk_label_new(after_newline_label), FALSE, FALSE, 0);
197 gtk_box_pack_start(GTK_BOX(box), horiz_fake, FALSE, FALSE, 2);
198 }
199
200 fake = wh_hbox_new();
201 gtk_box_pack_start(GTK_BOX(box), fake, FALSE, FALSE, 2);
202
203 return spinner;
204}
205
206GtkWidget *wh_create_int_spinner_in_box(gchar *before_label, gchar *after_label,
207 gdouble initial_value,
208 gdouble minimum_value, gdouble maximum_value,
209 gdouble step_increment, gdouble page_increment,
210 gchar *after_newline_label,
211 void (*spinner_callback)(GtkWidget *spinner, ui_state *ui),
212 ui_state *ui,
213 GtkWidget *box)
214{
215 return wh_create_int_spinner_in_box_with_top_width(before_label, after_label,
216 initial_value, minimum_value, maximum_value, step_increment, page_increment,
217 after_newline_label, spinner_callback, ui, box, 2);
218}
219
220GtkWidget *wh_hbox_new()
221{
222 GtkWidget *hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
223 gtk_box_set_homogeneous(GTK_BOX(hbox), FALSE);
224 return hbox;
225}
226
227GtkWidget *wh_vbox_new()
228{
229 GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
230 gtk_box_set_homogeneous(GTK_BOX(vbox), FALSE);
231 return vbox;
232}
233
234GtkWidget *wh_hscale_new(GtkAdjustment *adjustment)
235{
236 return gtk_scale_new(GTK_ORIENTATION_HORIZONTAL, adjustment);
237}
238
239GtkWidget *wh_hscale_new_with_range(gdouble min, gdouble max, gdouble step)
240{
241 return gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, min, max, step);
242}
243
244void wh_get_pointer(GdkEventMotion *event, gint *x, gint *y, GdkModifierType *state)
245{
246 gdk_window_get_device_position(event->window, event->device, x, y, state);
247}
248
250GtkWidget *wh_create_scrolled_window()
251{
252 GtkWidget *scrolled_window = gtk_scrolled_window_new (NULL, NULL);
253 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_NONE);
254 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
255 GTK_POLICY_AUTOMATIC,
256 GTK_POLICY_AUTOMATIC);
257 return scrolled_window;
258}
259
260void wh_add_box_to_scrolled_window(GtkWidget *box, GtkWidget *scrolled_window)
261{
262 GtkWidget *viewport = gtk_viewport_new(NULL, NULL);
263 gtk_container_add(GTK_CONTAINER(viewport), box);
264 gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(viewport));
265}
266
273gboolean wh_container_has_child(GtkContainer *container, GtkWidget *my_child)
274{
275 GList *children = gtk_container_get_children(GTK_CONTAINER(container));
276 int i = 0;
277 GtkWidget *child = NULL;
278 while ((child = g_list_nth_data(children, i)) != NULL)
279 {
280 if (child == my_child)
281 {
282 return TRUE;
283 }
284 i++;
285 }
286
287 return FALSE;
288}
289
290void wh_set_image_on_button(GtkButton *button, GtkWidget *image)
291{
292 GtkImage *previous_image = GTK_IMAGE(gtk_button_get_image(button));
293 if (previous_image != NULL && previous_image == GTK_IMAGE(image)) { return; }
294 gtk_button_set_image(button, image);
295}
296
297static void _wh_folder_changed_event(GtkFileChooser *chooser, ui_state *ui)
298{
299 ui_set_browser_directory(ui, gtk_file_chooser_get_current_folder(chooser));
300}
301
302void wh_set_browser_directory_handler(ui_state *ui, GtkWidget* dialog)
303{
304 const gchar *browser_dir = ui_get_browser_directory(ui);
305 if (browser_dir)
306 {
307 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), browser_dir);
308 }
309
310 g_signal_connect(GTK_FILE_CHOOSER(dialog), "current-folder-changed",
311 G_CALLBACK(_wh_folder_changed_event), ui);
312}
313
314GtkWidget *wh_create_cool_button(gchar *icon_name, gchar *label_text,
315 gint toggle_or_not)
316{
317 GtkWidget *box = wh_hbox_new();
318 gtk_container_set_border_width(GTK_CONTAINER(box), 0);
319
320 if (icon_name != NULL)
321 {
322 GtkWidget *image = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_MENU);
323 gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
324 }
325
326 if (label_text != NULL)
327 {
328 GtkWidget *label = gtk_label_new(label_text);
329 gtk_label_set_text_with_mnemonic(GTK_LABEL(label),label_text);
330 gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 3);
331 }
332
333 GtkWidget *button;
334 if (toggle_or_not)
335 {
336 button = gtk_toggle_button_new();
337 }
338 else
339 {
340 button = gtk_button_new();
341 }
342
343 gtk_container_add(GTK_CONTAINER(button), box);
344
345 return button;
346}
347
348GtkWidget *wh_create_cool_label(gchar *icon_name, gchar *label_text)
349{
350 GtkWidget *box = wh_hbox_new();
351 gtk_container_set_border_width(GTK_CONTAINER(box), 0);
352
353 if (icon_name != NULL)
354 {
355 GtkWidget *image = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_MENU);
356 gtk_box_pack_start(GTK_BOX(box), image, FALSE, FALSE, 0);
357 }
358
359 if (label_text != NULL)
360 {
361 GtkWidget *label = gtk_label_new(label_text);
362 gtk_label_set_text_with_mnemonic(GTK_LABEL(label),label_text);
363 gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 3);
364 }
365
366 gtk_widget_show_all(box);
367
368 return box;
369}
370
371GtkWidget *wh_create_window_with_close_button(gchar *title, gint width, gint height,
372 GtkWindowPosition position, GtkWindow *parent_window,
373 GtkWidget *main_area_widget, GtkWidget *bottom_widget, ...)
374{
375 GtkWidget *window = gtk_dialog_new();
376
377 g_signal_connect(G_OBJECT(window), "delete_event", G_CALLBACK(gtk_widget_hide_on_delete), window);
378 gtk_window_set_title(GTK_WINDOW(window), title);
379 gtk_window_set_destroy_with_parent(GTK_WINDOW(window), TRUE);
380 gtk_window_set_default_size(GTK_WINDOW(window), width, height);
381 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
382
383 GtkWidget *vbox = wh_vbox_new();
384
385 GtkContainer *dialog_container = GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(window)));
386 gtk_box_pack_start(GTK_BOX(dialog_container), vbox, TRUE, TRUE, 0);
387 gtk_box_pack_start(GTK_BOX(vbox), main_area_widget, TRUE, TRUE, 2);
388
389 GtkWidget *bottom_hbox = wh_hbox_new();
390 gtk_box_pack_start(GTK_BOX(vbox), bottom_hbox, FALSE, FALSE, 3);
391
392 va_list ap;
393 va_start(ap, bottom_widget);
394 while (bottom_widget)
395 {
396 gtk_box_pack_start(GTK_BOX(bottom_hbox), bottom_widget, FALSE, FALSE, 3);
397 bottom_widget = va_arg(ap, GtkWidget *);
398 }
399 va_end(ap);
400
401 GtkWidget *close_button = wh_create_cool_button("window-close", _("_Close"), FALSE);
402 gtk_box_pack_end(GTK_BOX(bottom_hbox), close_button, FALSE, FALSE, 3);
403 g_signal_connect(G_OBJECT(close_button), "clicked",
404 G_CALLBACK(hide_window_from_button), window);
405
406 return window;
407}
408
409void wh_show_window(GtkWidget *window)
410{
411 if (!gtk_widget_get_visible(window))
412 {
413 gtk_widget_show_all(window);
414 return;
415 }
416
417 gtk_window_present(GTK_WINDOW(window));
418}
419
420static void hide_window_from_button(GtkWidget *widget, gpointer data)
421{
422 GtkWidget *window = (GtkWidget *)data;
423 gtk_widget_hide(window);
424}
425
426static guint _wh_add_row_to_table(GtkWidget *table)
427{
428 int number_of_rows = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(table), "rows"));
429 number_of_rows++;
430 g_object_set_data(G_OBJECT(table), "rows", GINT_TO_POINTER(number_of_rows));
431 gtk_grid_insert_row(GTK_GRID(table), number_of_rows);
432 return number_of_rows;
433}
434
435static void _wh_add_in_table_with_label(GtkWidget *table, const gchar *label_text,
436 GtkWidget *widget, int expand)
437{
438 guint last_row = _wh_add_row_to_table(table);
439
440 GtkWidget *label = gtk_label_new(label_text);
441 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
442
443 _wh_attach_to_table(table, label, 1, 2, last_row, FALSE);
444 _wh_attach_to_table(table, widget, 2, 3, last_row, expand);
445}
446
447static void _wh_attach_to_table(GtkWidget *table, GtkWidget *widget,
448 guint start_column, guint end_column, guint row, int expand)
449{
450 GtkWidget *my_widget = widget;
451 GtkWidget *hbox;
452
453 gtk_widget_set_halign(my_widget, GTK_ALIGN_FILL);
454
455 if (expand)
456 {
457 gtk_widget_set_hexpand(my_widget, TRUE);
458 }
459 else
460 {
461 hbox = wh_hbox_new();
462 gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, FALSE, 0);
463 my_widget = hbox;
464 }
465
466 gtk_grid_attach(GTK_GRID(table), my_widget, start_column, row - 1, end_column - start_column, 1);
467}
468
ui_state
Definition ui_types.h:509
wh_container_has_child
gboolean wh_container_has_child(GtkContainer *container, GtkWidget *my_child)
Does this GtkContainer contain that object?
Definition widgets_helper.c:273
wh_set_title_and_get_vbox
GtkWidget * wh_set_title_and_get_vbox(GtkWidget *widget, const gchar *title)
Generates a window portion containing a caption and a vbox.
Definition widgets_helper.c:56
wh_create_scrolled_window
GtkWidget * wh_create_scrolled_window()
creates a scrolled window
Definition widgets_helper.c:250