mp3splt-gtk
widgets_helper.c
Go to the documentation of this file.
1 /**********************************************************
2  *
3  * mp3splt-gtk -- utility based on mp3splt,
4  * for mp3/ogg splitting without decoding
5  *
6  * Copyright: (C) 2005-2012 Alexandru Munteanu
7  * Contact: io_fx@yahoo.fr
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 
40 static guint _wh_add_row_to_table();
41 static GtkWidget *_wh_put_in_new_hbox_with_margin(GtkWidget *widget, gint margin);
42 static void _wh_attach_to_table(GtkWidget *table, GtkWidget *widget,
43  guint start_column, guint end_column, guint row, int expand);
44 static void _wh_add_in_table_with_label(GtkWidget *table, const gchar *label_text,
45  GtkWidget *widget, int expand);
46 
56 GtkWidget *wh_set_title_and_get_vbox(GtkWidget *widget, const gchar *title)
57 {
58  GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
59 
60  GtkWidget *label = gtk_label_new(NULL);
61  gtk_label_set_markup(GTK_LABEL(label), title);
62 
63  GtkWidget *label_hbox = gtk_hbox_new(FALSE, 0);
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 = gtk_hbox_new(FALSE, 0);
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 
75 GtkWidget *wh_new_table()
76 {
77  GtkWidget *table = gtk_table_new(1, 2, FALSE);
78  gtk_table_set_col_spacing(GTK_TABLE(table), 0, 0);
79  gtk_table_set_col_spacing(GTK_TABLE(table), 1, 5);
80  return table;
81 }
82 
83 void wh_add_in_table(GtkWidget *table, GtkWidget *widget)
84 {
85  guint last_row = _wh_add_row_to_table(table);
86 
87  _wh_attach_to_table(table, widget, 1, 3, last_row, TRUE);
88 }
89 
90 void wh_add_in_table_with_label_expand(GtkWidget *table, const gchar *label_text, GtkWidget *widget)
91 {
92  _wh_add_in_table_with_label(table, label_text, widget, TRUE);
93 }
94 
95 void wh_add_in_table_with_label(GtkWidget *table, const gchar *label_text, GtkWidget *widget)
96 {
97  _wh_add_in_table_with_label(table, label_text, widget, FALSE);
98 }
99 
100 GtkWidget *wh_put_in_new_hbox_with_margin_level(GtkWidget *widget, gint margin_level)
101 {
102  return _wh_put_in_new_hbox_with_margin(widget, 6 * margin_level);
103 }
104 
105 GtkWidget *wh_new_entry(void *callback)
106 {
107  GtkWidget *entry = gtk_entry_new();
108  gtk_editable_set_editable(GTK_EDITABLE(entry), TRUE);
109 
110  if (callback)
111  {
112  g_signal_connect(G_OBJECT(entry), "changed", G_CALLBACK(callback), NULL);
113  }
114 
115  return entry;
116 }
117 
118 GtkWidget *wh_new_button(const gchar *button_label)
119 {
120  return gtk_button_new_with_mnemonic(button_label);
121 }
122 
123 void wh_get_widget_size(GtkWidget *widget, gint *width, gint *height)
124 {
125 #if GTK_MAJOR_VERSION <= 2
126  GtkAllocation allocation;
127  gtk_widget_get_allocation(widget, &allocation);
128 
129  if (width != NULL)
130  {
131  *width = allocation.width;
132  }
133 
134  if (height != NULL)
135  {
136  *height= allocation.height;
137  }
138 #else
139  if (width != NULL)
140  {
141  *width = gtk_widget_get_allocated_width(widget);
142  }
143 
144  if (height != NULL)
145  {
146  *height = gtk_widget_get_allocated_height(widget);
147  }
148 #endif
149 }
150 
151 static void _wh_folder_changed_event(GtkFileChooser *chooser, gpointer data)
152 {
153  ui_state *ui = (ui_state *) data;
154  ui_set_browser_directory(ui, gtk_file_chooser_get_current_folder(chooser));
155 }
156 
157 void wh_set_browser_directory_handler(ui_state *ui, GtkWidget* dialog)
158 {
159  const gchar *browser_dir = ui_get_browser_directory(ui);
160  if (browser_dir)
161  {
162  gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), browser_dir);
163  }
164 
165  g_signal_connect(GTK_FILE_CHOOSER(dialog), "current-folder-changed",
166  G_CALLBACK(_wh_folder_changed_event), ui);
167 }
168 
169 static guint _wh_add_row_to_table(GtkWidget *table)
170 {
171  guint rows;
172  guint columns;
173 
174  g_object_get(G_OBJECT(table),
175  "n-rows", &rows,
176  "n-columns", &columns,
177  NULL);
178 
179  guint new_rows = rows + 1;
180 
181  gtk_table_resize(GTK_TABLE(table), new_rows, columns);
182  gtk_table_set_row_spacing(GTK_TABLE(table), new_rows - 1, 4);
183 
184  return new_rows;
185 }
186 
187 static GtkWidget *_wh_put_in_new_hbox_with_margin(GtkWidget *widget, gint margin)
188 {
189  GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
190  gtk_box_pack_start(GTK_BOX(hbox), widget, TRUE, TRUE, margin);
191  return hbox;
192 }
193 
194 static void _wh_add_in_table_with_label(GtkWidget *table, const gchar *label_text,
195  GtkWidget *widget, int expand)
196 {
197  guint last_row = _wh_add_row_to_table(table);
198 
199  GtkWidget *label = gtk_label_new(label_text);
200  gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
201 
202  _wh_attach_to_table(table, label, 1, 2, last_row, FALSE);
203  _wh_attach_to_table(table, widget, 2, 3, last_row, expand);
204 }
205 
206 static void _wh_attach_to_table(GtkWidget *table, GtkWidget *widget,
207  guint start_column, guint end_column, guint row, int expand)
208 {
209  GtkWidget *my_widget = widget;
210  GtkWidget *hbox;
211 
212  GtkAttachOptions xoptions = GTK_FILL;
213  if (expand)
214  {
215  xoptions |= GTK_EXPAND;
216  }
217  else
218  {
219  hbox = gtk_hbox_new(FALSE, 0);
220  gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, FALSE, 0);
221  my_widget = hbox;
222  }
223 
224  gtk_table_attach(GTK_TABLE(table), my_widget,
225  start_column, end_column, row-1, row,
226  xoptions, GTK_FILL | GTK_EXPAND,
227  0, 0);
228 }
229