clean code and added some g_free()
[irreco.git] / irreco / src / core / irreco_button_creator_dlg.c
blob09d6a49d9fa7b237b82a7e8145b5b693fa703736
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * irreco - Ir Remote Control
4 * Copyright (C) 2008 Pekka Gehör (pegu6@msn.com)
6 * irreco_button_creator_dlg.c is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * irreco_button_creator_dlg.c is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "irreco_button_creator_dlg.h"
21 #include "irreco_theme_creator_backgrounds.h"
22 #include "irreco_theme_creator_dlg.h"
23 #include <hildon/hildon-banner.h>
24 #include <hildon/hildon-color-button.h>
25 #include <hildon/hildon-file-chooser-dialog.h>
26 #include <hildon/hildon-font-selection-dialog.h>
27 #include <hildon/hildon-program.h>
28 #include <hildon/hildon-color-chooser.h>
29 #include <hildon/hildon-color-chooser-dialog.h>
34 /**
35 * @addtogroup IrrecoButtonCreatorDlg
36 * @ingroup Irreco
39 * @{
42 /**
43 * @file
44 * Source file of @ref IrrecoButtonCreatorDlg.
46 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
47 /* Datatypes */
48 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
50 #define IRRECO_BUTTON_PREVIEW_WIDHT (IRRECO_SCREEN_WIDTH/5)
51 #define IRRECO_BUTTON_PREVIEW_HEIGHT (IRRECO_SCREEN_HEIGHT/5)
52 /*#define BUTTON_LIST_DIR "/usr/lib/irreco/buttonnames/button_list.conf"*/
53 #define BUTTON_LIST_DIR (BUTTONLIST"button_list.conf")
54 /** Loader states. */
55 enum
57 LOADER_STATE_INIT,
58 LOADER_STATE_THEME,
59 LOADER_STATE_BUTTONS,
60 LOADER_STATE_BACKGROUNDS,
61 LOADER_STATE_END
65 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
66 /* Prototypes */
67 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
68 static void
69 _signal_image_clicked(GtkButton *button, IrrecoButtonCreatorDlg *self);
70 static void
71 _unpressed_event_signal_image_clicked(GtkWidget *widget, GdkEventButton *event,
72 IrrecoButtonCreatorDlg *self);
73 static void
74 _pressed_event_signal_image_clicked(GtkWidget *widget, GdkEventButton *event,
75 IrrecoButtonCreatorDlg *self);
76 static gboolean _draw_preview_image(GtkButton *button,
77 IrrecoButtonCreatorDlg *self,
78 const gchar *image);
79 static void _toggle_button_toggled(GtkToggleButton *togglebutton,
80 IrrecoButtonCreatorDlg *self);
81 static void fill_buttons_combobox(IrrecoButtonCreatorDlg *self);
82 static void
83 _select_font_format(GtkButton *button, IrrecoButtonCreatorDlg *self);
85 static void
86 _set_font_format(IrrecoButtonCreatorDlg *self, const gchar *button_format,
87 const gchar *text);
88 void
89 _set_button_details(IrrecoButtonCreatorDlg *self, IrrecoTheme *irreco_theme,
90 IrrecoThemeButton *button);
91 void
92 _unbutton_size_changed(GtkSpinButton *spinbutton, IrrecoButtonCreatorDlg*self);
93 void
94 _button_size_changed(GtkSpinButton *spinbutton, IrrecoButtonCreatorDlg*self);
95 void
96 _set_new_button_details(IrrecoButtonCreatorDlg *self, IrrecoThemeButton *button);
97 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
98 /* Construction & Destruction */
99 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
102 * @name Construction & Destruction
103 * @{
106 G_DEFINE_TYPE (IrrecoButtonCreatorDlg, irreco_button_creator_dlg,
107 IRRECO_TYPE_INTERNAL_DLG)
108 static void irreco_button_creator_dlg_constructed(GObject *object)
110 /* TODO: Add initialization code here */
112 IrrecoButtonCreatorDlg *self;
113 GtkWidget *scrolled_table;
114 GtkWidget *table;
115 GtkWidget *frame_unpressed;
116 GtkWidget *frame_pressed;
117 GtkWidget *label_name;
118 GtkWidget *label_select_unpressed;
119 GtkWidget *label_select_pressed;
120 GtkWidget *event_box_unpressed;
121 GtkWidget *vbox_unpressed_preview;
122 GtkWidget *vbox_pressed_preview;
123 GtkWidget *label_size;
125 /* Settings */
126 GtkWidget *table_settings;
127 GtkWidget *label_allow_text;
128 GtkWidget *scrolled_table_settings;
129 GtkWidget *label_text_padding;
130 GtkWidget *label_text_h_align;
131 GtkWidget *label_text_v_align;
132 GtkWidget *label_text_format_up;
133 GtkWidget *label_text_format_down;
134 IRRECO_ENTER
136 G_OBJECT_CLASS(irreco_button_creator_dlg_parent_class)->constructed(object);
138 self = IRRECO_BUTTON_CREATOR_DLG(object);
139 /* Construct dialog. */
140 gtk_window_set_title(GTK_WINDOW(self), _("Create a Button"));
141 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
142 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
143 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
145 /*Buttons*/
146 self->cancel_button= gtk_dialog_add_button(GTK_DIALOG(self),
147 _("Cancel"),
148 GTK_RESPONSE_CANCEL);
149 self->add_button= gtk_dialog_add_button(GTK_DIALOG(self), _("Add"),
150 GTK_RESPONSE_OK);
151 gtk_widget_set_sensitive(self->add_button, FALSE);
153 /* Create widgets. */
154 self->notebook = gtk_notebook_new();
156 /* Create widgets. */
157 scrolled_table = gtk_scrolled_window_new(NULL, NULL);
158 table = gtk_table_new(7, 9, TRUE);
159 label_name = gtk_label_new(_("Name:"));
160 label_select_unpressed = gtk_label_new(_("Unpressed:"));
161 label_select_pressed = gtk_label_new(_("Pressed:"));
162 self->event_box_pressed = gtk_event_box_new();
163 event_box_unpressed = gtk_event_box_new();
164 self->combobox_name = gtk_combo_box_entry_new_text();
165 frame_unpressed = gtk_frame_new("");
166 frame_pressed = gtk_frame_new("");
167 vbox_unpressed_preview = gtk_vbox_new(FALSE, 8);
168 vbox_pressed_preview = gtk_vbox_new(FALSE, 8);
169 self->label_unpressed_size = gtk_label_new("");
170 self->label_pressed_size = gtk_label_new("");
171 self->preview_image_unpressed = gtk_image_new();
172 self->preview_image_pressed = gtk_image_new();
173 self->add_button_unpressed = gtk_button_new_with_label(_("Select"));
174 self->add_button_pressed = gtk_button_new_with_label(_("Select"));
176 /* Create settings widgets*/
177 scrolled_table_settings = gtk_scrolled_window_new(NULL, NULL);
178 table_settings = gtk_table_new(7, 9, TRUE);
179 label_allow_text = gtk_label_new(_("AllowText:"));
180 label_text_format_up = gtk_label_new(_("TextFormatUp:"));
181 label_text_format_down = gtk_label_new(_("TextFormatDown:"));
182 self->label_sample_text_format_up = gtk_label_new(_("Sample1"));
183 self->label_sample_text_format_down = gtk_label_new(_("Sample2"));
184 label_text_padding = gtk_label_new(_("TextPadding:"));
185 label_text_h_align = gtk_label_new(_("TextHAlign:"));
186 label_text_v_align = gtk_label_new(_("TextVAlign:"));
187 label_size = gtk_label_new(_("ButtonSize:"));
188 self->allow_text = gtk_toggle_button_new();
189 self->text_format_up = gtk_button_new_with_label(_("Select"));
190 self->text_format_down = gtk_button_new_with_label(_("Select"));
191 self->text_padding = gtk_spin_button_new_with_range(0, 20, 1);
192 self->text_h_align = gtk_spin_button_new_with_range(0, 1, 0.1);;
193 self->text_v_align = gtk_spin_button_new_with_range(0, 1, 0.1);;
194 self->unbutton_size = gtk_spin_button_new_with_range(2, 5, 0.1);
195 self->button_size = gtk_spin_button_new_with_range(2, 5, 0.1);
196 self->unpressed_path = g_string_new(NULL);
197 self->pressed_path = g_string_new(NULL);
199 /* set button name/sensitive */
200 gtk_widget_set_name(self->add_button_unpressed,"unpressed");
201 gtk_widget_set_name(self->add_button_pressed,"pressed");
203 gtk_widget_set_sensitive(self->add_button_pressed, FALSE);
204 gtk_widget_set_sensitive(self->event_box_pressed, FALSE);
205 gtk_widget_set_sensitive(self->text_format_up, FALSE);
206 gtk_widget_set_sensitive(self->text_format_down, FALSE);
208 gtk_widget_set_sensitive(self->label_sample_text_format_up, FALSE);
209 gtk_widget_set_sensitive(self->label_sample_text_format_down, FALSE);
210 /* Set frame text bold */
211 gtk_frame_set_label_widget(GTK_FRAME(frame_unpressed),
212 irreco_gtk_label_bold(
213 "Unpressed", 0, 0, 0, 0, 0, 0));
214 gtk_frame_set_label_widget(GTK_FRAME(frame_pressed),
215 irreco_gtk_label_bold(
216 "Pressed", 0, 0, 0, 0, 0, 0));
218 /* equal to the text of the left-side */
219 gtk_misc_set_alignment(GTK_MISC(label_name), 0, 0.5);
220 gtk_misc_set_alignment(GTK_MISC(label_select_unpressed), 0, 0.5);
221 gtk_misc_set_alignment(GTK_MISC(label_select_pressed), 0, 0.5);
223 gtk_misc_set_alignment(GTK_MISC(label_allow_text), 0, 0.5);
224 gtk_misc_set_alignment(GTK_MISC(label_text_format_up), 0, 0.5);
225 gtk_misc_set_alignment(GTK_MISC(label_text_format_down), 0, 0.5);
226 gtk_misc_set_alignment(GTK_MISC(self->label_sample_text_format_up), 0, 0.5);
227 gtk_misc_set_alignment(GTK_MISC(self->label_sample_text_format_down), 0, 0.5);
228 gtk_misc_set_alignment(GTK_MISC(label_text_padding), 0, 0.5);
229 gtk_misc_set_alignment(GTK_MISC(label_text_h_align), 0, 0.5);
230 gtk_misc_set_alignment(GTK_MISC(label_text_v_align), 0, 0.5);
231 gtk_misc_set_alignment(GTK_MISC(label_size), 0, 0.5);
233 /* Set table on the scrolled */
234 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(
235 scrolled_table),
236 GTK_WIDGET(table));
238 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_table),
239 GTK_POLICY_NEVER,
240 GTK_POLICY_AUTOMATIC);
242 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(
243 scrolled_table_settings),
244 GTK_WIDGET(table_settings));
246 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(
247 scrolled_table_settings),
248 GTK_POLICY_NEVER,
249 GTK_POLICY_AUTOMATIC);
251 /* Create Notebook tabs. */
253 gtk_notebook_append_page(GTK_NOTEBOOK(self->notebook),
254 irreco_gtk_align(GTK_WIDGET(scrolled_table),
255 0, 0, 1, 1, 8, 8, 8, 8),
256 gtk_label_new("Button"));
257 gtk_notebook_append_page(GTK_NOTEBOOK(self->notebook),
258 irreco_gtk_align(GTK_WIDGET(
259 scrolled_table_settings),
260 0, 0, 1, 1, 8, 8, 8, 8),
261 gtk_label_new("Settings"));
263 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(self)->vbox),
264 self->notebook);
266 /* Set widgets on the table */
268 gtk_table_set_row_spacings(GTK_TABLE(table), 6);
269 gtk_table_set_col_spacings(GTK_TABLE(table), 6);
271 gtk_table_attach_defaults(GTK_TABLE(table), label_name, 0, 3, 0, 1);
272 gtk_table_attach_defaults(GTK_TABLE(table),
273 label_select_unpressed, 0, 3, 1, 2);
274 gtk_table_attach_defaults(GTK_TABLE(table),
275 label_select_pressed, 0, 3, 2, 3);
276 gtk_table_attach_defaults(GTK_TABLE(table),
277 self->combobox_name, 3, 9, 0, 1);
278 gtk_table_attach_defaults(GTK_TABLE(table),
279 self->add_button_unpressed, 3, 9, 1, 2);
280 gtk_table_attach_defaults(GTK_TABLE(table),
281 self->add_button_pressed, 3, 9, 2, 3);
282 gtk_table_attach_defaults(GTK_TABLE(table),
283 frame_unpressed, 0, 4, 3, 7);
284 gtk_table_attach_defaults(GTK_TABLE(table), frame_pressed, 5, 9, 3, 7);
285 /* Set widgets on the table_settings */
286 gtk_table_set_row_spacings(GTK_TABLE(table_settings), 6);
287 gtk_table_set_col_spacings(GTK_TABLE(table_settings), 6);
289 gtk_table_attach_defaults(GTK_TABLE(table_settings),
290 label_allow_text, 0, 4, 0, 1);
291 gtk_table_attach_defaults(GTK_TABLE(table_settings),
292 label_text_format_up, 0, 4, 1, 2);
293 gtk_table_attach_defaults(GTK_TABLE(table_settings),
294 label_text_format_down, 0, 4, 2, 3);
295 gtk_table_attach_defaults(GTK_TABLE(table_settings),
296 self->label_sample_text_format_up, 4, 7, 1, 2);
297 gtk_table_attach_defaults(GTK_TABLE(table_settings),
298 self->label_sample_text_format_down, 4, 7, 2, 3);
299 gtk_table_attach_defaults(GTK_TABLE(table_settings),
300 label_text_padding, 0, 4, 3, 4);
301 gtk_table_attach_defaults(GTK_TABLE(table_settings),
302 label_text_h_align, 0, 4, 4, 5);
303 gtk_table_attach_defaults(GTK_TABLE(table_settings),
304 label_text_v_align, 0, 4, 5, 6);
305 gtk_table_attach_defaults(GTK_TABLE(table_settings),
306 label_size, 0, 4, 6, 7);
308 gtk_table_attach_defaults(GTK_TABLE(table_settings),
309 self->allow_text, 4, 9, 0, 1);
310 gtk_table_attach_defaults(GTK_TABLE(table_settings),
311 self->text_format_up, 7, 9, 1, 2);
312 gtk_table_attach_defaults(GTK_TABLE(table_settings),
313 self->text_format_down, 7, 9, 2, 3);
314 gtk_table_attach_defaults(GTK_TABLE(table_settings),
315 self->text_padding, 4, 9, 3, 4);
316 gtk_table_attach_defaults(GTK_TABLE(table_settings),
317 self->text_h_align, 4, 9, 4, 5);
318 gtk_table_attach_defaults(GTK_TABLE(table_settings),
319 self->text_v_align, 4, 9, 5, 6);
320 gtk_table_attach_defaults(GTK_TABLE(table_settings),
321 self->unbutton_size, 4, 6, 6, 7);
322 gtk_table_attach_defaults(GTK_TABLE(table_settings),
323 self->button_size, 7, 9, 6, 7);
324 /* set preview image*/
326 gtk_container_add(GTK_CONTAINER(frame_unpressed), event_box_unpressed);
327 gtk_container_add(GTK_CONTAINER(event_box_unpressed),
328 vbox_unpressed_preview);
329 gtk_container_add(GTK_CONTAINER(vbox_unpressed_preview),
330 self->preview_image_unpressed);
331 gtk_container_add(GTK_CONTAINER(vbox_unpressed_preview),
332 self->label_unpressed_size);
334 gtk_container_add(GTK_CONTAINER(frame_pressed), self->event_box_pressed);
335 gtk_container_add(GTK_CONTAINER(self->event_box_pressed),
336 vbox_pressed_preview);
337 gtk_container_add(GTK_CONTAINER(vbox_pressed_preview),
338 self->preview_image_pressed);
339 gtk_container_add(GTK_CONTAINER(vbox_pressed_preview),
340 self->label_pressed_size);
345 /*Added button list on combobox*/
346 fill_buttons_combobox(self);
348 /* set default settings on tab */
349 gtk_button_set_label(GTK_BUTTON(self->allow_text), "NO");
351 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_padding), 5);
352 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_h_align), 0.5);
353 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_v_align), 0.5);
354 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->unbutton_size), 10);
355 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->button_size), 10);
357 /* Set button name */
358 gtk_widget_set_name(self->text_format_up, "unpressed");
359 gtk_widget_set_name(self->text_format_down, "pressed");
360 /* Button signals. */
361 g_signal_connect(G_OBJECT(self->add_button_unpressed), "clicked",
362 G_CALLBACK(_signal_image_clicked), self);
363 g_signal_connect(G_OBJECT(self->add_button_pressed), "clicked",
364 G_CALLBACK(_signal_image_clicked), self);
366 g_signal_connect(G_OBJECT(event_box_unpressed), "button-release-event",
367 G_CALLBACK(_unpressed_event_signal_image_clicked),
368 self);
369 g_signal_connect(G_OBJECT(self->event_box_pressed),
370 "button-release-event",
371 G_CALLBACK(_pressed_event_signal_image_clicked),
372 self);
374 g_signal_connect(G_OBJECT(self->allow_text), "toggled",
375 G_CALLBACK(_toggle_button_toggled), self);
377 g_signal_connect(G_OBJECT(self->text_format_up), "clicked",
378 G_CALLBACK(_select_font_format), self);
379 g_signal_connect(G_OBJECT(self->text_format_down), "clicked",
380 G_CALLBACK(_select_font_format), self);
381 g_signal_connect(G_OBJECT(self->unbutton_size), "value-changed",
382 G_CALLBACK(_unbutton_size_changed), self);
383 g_signal_connect(G_OBJECT(self->button_size), "value-changed",
384 G_CALLBACK(_button_size_changed), self);
386 gtk_widget_set_size_request(GTK_WIDGET(self), 696, 396);
387 gtk_widget_show_all(GTK_WIDGET(self));
389 /* Hide Button size widgets */
390 gtk_widget_hide(label_size);
391 gtk_widget_hide(self->unbutton_size);
392 gtk_widget_hide(self->button_size);
393 IRRECO_RETURN
396 static void
397 irreco_button_creator_dlg_init (IrrecoButtonCreatorDlg *object)
399 IRRECO_ENTER
400 IRRECO_RETURN
403 static void
404 irreco_button_creator_dlg_finalize (GObject *object)
406 /* TODO: Add deinitalization code here */
408 IrrecoButtonCreatorDlg *self;
409 IRRECO_ENTER
411 self = IRRECO_BUTTON_CREATOR_DLG(object);
413 G_OBJECT_CLASS(irreco_button_creator_dlg_parent_class)->finalize(object);
414 IRRECO_RETURN
418 static void
419 irreco_button_creator_dlg_class_init (IrrecoButtonCreatorDlgClass *klass)
421 GObjectClass* object_class = G_OBJECT_CLASS (klass);
423 object_class->finalize = irreco_button_creator_dlg_finalize;
424 object_class->constructed = irreco_button_creator_dlg_constructed;
427 GtkWidget
428 *irreco_button_creator_dlg_new(IrrecoData *irreco_data, GtkWindow *parent)
430 IrrecoButtonCreatorDlg *self;
432 IRRECO_ENTER
434 self = g_object_new(IRRECO_TYPE_BUTTON_CREATOR_DLG,
435 "irreco-data", irreco_data, NULL);
436 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
438 IRRECO_RETURN_PTR(self);
441 /** @} */
443 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
444 /* Private Functions */
445 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
447 * @name Private Functions
448 * @{
452 *Fill combobox from file
454 static void fill_buttons_combobox(IrrecoButtonCreatorDlg *self)
457 GKeyFile *button_list;
458 gchar **keys;
459 gsize length;
460 gint i=0;
461 GError *error = NULL;
463 IRRECO_ENTER
465 if (irreco_file_exists(BUTTON_LIST_DIR)) {
466 button_list = g_key_file_new();
467 g_key_file_load_from_file(button_list, BUTTON_LIST_DIR,
468 G_KEY_FILE_NONE, &error);
469 keys = g_key_file_get_keys(button_list, "button-names", &length,
470 &error);
471 for(i=0; i<length; i++){
472 gtk_combo_box_append_text(GTK_COMBO_BOX(self->combobox_name),
473 g_key_file_get_string(button_list,
474 "button-names", keys[i], &error));
476 g_key_file_free(button_list);
478 if(keys != NULL) g_free(keys);
481 IRRECO_RETURN
484 void
485 _unbutton_size_changed(GtkSpinButton *spinbutton, IrrecoButtonCreatorDlg*self)
487 GString *image_path = g_string_new(self->unpressed_path->str);
489 IRRECO_ENTER
491 _draw_preview_image(GTK_BUTTON(self->text_format_up), self,
492 image_path->str);
494 g_string_free(image_path, TRUE);
495 IRRECO_RETURN
497 void
498 _button_size_changed(GtkSpinButton *spinbutton, IrrecoButtonCreatorDlg*self)
500 GString *image_path = g_string_new(self->unpressed_path->str);
502 IRRECO_ENTER
504 _draw_preview_image(GTK_BUTTON(self->text_format_down), self,
505 image_path->str);
507 g_string_free(image_path, TRUE);
508 IRRECO_RETURN
512 * Draw preview with current image.
514 static gboolean
515 _draw_preview_image(GtkButton *button, IrrecoButtonCreatorDlg *self,
516 const gchar *image)
518 GError *error = NULL;
519 GdkPixbuf *pixbuf = NULL;
520 GString *size = g_string_new(NULL);
522 IRRECO_ENTER
524 g_assert(self != NULL);
525 /* compared with whichever image */
526 if (g_str_equal("pressed", gtk_widget_get_name(GTK_WIDGET(button)))) {
527 gint button_width = 0;
528 gint button_height = 0;
529 GdkPixbuf *pixbuf_size = NULL;
531 if (image != NULL) {
532 pixbuf = gdk_pixbuf_new_from_file_at_scale(image,
533 IRRECO_BUTTON_PREVIEW_WIDHT,
534 IRRECO_BUTTON_PREVIEW_HEIGHT,
535 TRUE, &error);
536 /* set sensitive if image are selected */
537 gtk_widget_set_sensitive(self->add_button, TRUE);
539 if (irreco_gerror_check_print(&error)) {
540 IRRECO_RETURN_BOOL(FALSE);
544 g_string_printf(self->pressed_path, "%s", image);
545 gtk_widget_realize(GTK_WIDGET(self->preview_image_pressed));
546 gtk_image_set_from_pixbuf(GTK_IMAGE(self->preview_image_pressed),
547 GDK_PIXBUF(pixbuf));
549 /* Show image real size */
550 pixbuf_size = gdk_pixbuf_new_from_file(image, &error);
551 button_width = atoi(g_strdup_printf("%.0f",
552 (gdk_pixbuf_get_width(
553 pixbuf_size))/
554 (6-gtk_spin_button_get_value(GTK_SPIN_BUTTON(
555 self->button_size)))));
556 button_height = atoi(g_strdup_printf("%.0f",
557 (gdk_pixbuf_get_height(
558 pixbuf_size)/
559 (6-gtk_spin_button_get_value(GTK_SPIN_BUTTON(
560 self->button_size))))));
562 if (pixbuf_size != NULL) g_object_unref(G_OBJECT(pixbuf_size));
564 g_string_printf(size, "%sx%s", g_strdup_printf("%d", button_width),
565 g_strdup_printf("%d", button_height));
567 gtk_label_set_text(GTK_LABEL(self->label_pressed_size),
568 size->str);
570 } else {
571 gint button_width = 0;
572 gint button_height = 0;
573 GdkPixbuf *pixbuf_size = NULL;
575 if (image != NULL) {
576 pixbuf = gdk_pixbuf_new_from_file_at_scale(image,
577 IRRECO_BUTTON_PREVIEW_WIDHT,
578 IRRECO_BUTTON_PREVIEW_HEIGHT,
579 TRUE, &error);
580 if (irreco_gerror_check_print(&error)) {
581 IRRECO_RETURN_BOOL(FALSE);
584 gtk_widget_realize(GTK_WIDGET(self->preview_image_unpressed));
585 gtk_image_set_from_pixbuf(GTK_IMAGE(
586 self->preview_image_unpressed),
587 GDK_PIXBUF(pixbuf));
589 /* Show image real size */
590 pixbuf_size = gdk_pixbuf_new_from_file(image, &error);
591 button_width = atoi(g_strdup_printf("%.0f",
592 (gdk_pixbuf_get_width(
593 pixbuf_size))/
594 (6-gtk_spin_button_get_value(GTK_SPIN_BUTTON(
595 self->unbutton_size)))));
596 button_height = atoi(g_strdup_printf("%.0f",
597 (gdk_pixbuf_get_height(
598 pixbuf_size)/
599 (6-gtk_spin_button_get_value(GTK_SPIN_BUTTON(
600 self->unbutton_size))))));
601 if (pixbuf_size != NULL) g_object_unref(G_OBJECT(pixbuf_size));
603 g_string_printf(size, "%sx%s", g_strdup_printf("%d", button_width),
604 g_strdup_printf("%d", button_height));
606 gtk_label_set_text(GTK_LABEL(self->label_unpressed_size),
607 size->str);
608 g_string_printf(self->unpressed_path, "%s", image);
610 /* Set button sensitive */
611 gtk_widget_set_sensitive(self->add_button_pressed, TRUE);
612 gtk_widget_set_sensitive(self->event_box_pressed, TRUE);
615 g_string_free(size, TRUE);
616 if (pixbuf != NULL) g_object_unref(G_OBJECT(pixbuf));
617 IRRECO_RETURN_BOOL(TRUE);
620 static void _select_image(GtkButton *button, IrrecoButtonCreatorDlg *self)
623 GtkWidget *file_dlg = NULL;
624 gchar *image_dir = NULL;
625 IRRECO_ENTER
627 /* Create image select dialog. */
629 file_dlg = hildon_file_chooser_dialog_new(GTK_WINDOW(self),
630 GTK_FILE_CHOOSER_ACTION_OPEN);
631 gtk_window_set_title(GTK_WINDOW(file_dlg),_("Select button image"));
632 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(file_dlg), TRUE);
633 image_dir = g_build_path("/", getenv("HOME"), "MyDocs/.images/", NULL);
634 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(file_dlg),
635 image_dir);
636 g_free(image_dir);
638 /* Loop until user cancels or we get a valid image. */
639 gtk_widget_show_all(GTK_WIDGET(file_dlg));
640 while (gtk_dialog_run(GTK_DIALOG(file_dlg)) == GTK_RESPONSE_OK) {
641 gchar *filename;
642 filename = gtk_file_chooser_get_filename(
643 GTK_FILE_CHOOSER(file_dlg));
645 /* Attempt to display the image. */
646 if (_draw_preview_image(button, self, filename)) {
647 irreco_gstring_set_and_free(self->filename, filename);
648 filename = NULL;
649 break;
651 /* Cleanup */
652 } else {
653 gchar *basename = g_path_get_basename(filename);
654 irreco_error_dlg_printf(GTK_WINDOW(file_dlg),
655 _("Cannot open image \"%s\""),
656 basename);
657 g_free(basename);
658 g_free(filename);
662 gtk_widget_destroy(file_dlg);
663 IRRECO_RETURN
665 gboolean irreco_button_creator_dlg_check_details(IrrecoButtonCreatorDlg *self)
667 gboolean rvalue = TRUE;
668 gchar *name = NULL;
669 gchar *style_name = NULL;
670 IRRECO_ENTER
672 name = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
673 self->combobox_name));
674 style_name = g_strconcat(self->theme->name->str ,"/", name, NULL);
675 /*check that it is not the same name button*/
676 if (irreco_string_table_exists (self->theme->buttons, name)) {
677 irreco_error_dlg(GTK_WINDOW(self),
678 "Button has already existed");
679 rvalue = FALSE;
680 } else if (irreco_string_table_exists(self->theme->buttons,
681 style_name)) {
682 irreco_error_dlg(GTK_WINDOW(self),
683 "Button has already existed");
684 rvalue = FALSE;
686 if (g_utf8_strlen(name, 1) ==0) {
687 irreco_error_dlg(GTK_WINDOW(self),
688 "Set Button name");
689 rvalue = FALSE;
692 if (name != NULL) g_free(name);
693 if (style_name != NULL) g_free(style_name);
694 IRRECO_RETURN_BOOL(rvalue);
696 /** @} */
698 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
699 /* Functions */
700 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
702 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
703 /* Public Functions */
704 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
706 gboolean irreco_button_creator_dlg_run(IrrecoData *irreco_data,
707 IrrecoTheme * irreco_theme,
708 GtkWindow *parent_window,
709 IrrecoThemeButton *button)
711 IrrecoButtonCreatorDlg *self;
712 gint response;
713 gboolean loop = TRUE;
714 gboolean rvalue = FALSE;
715 gboolean edit = FALSE;
716 IRRECO_ENTER
718 self = (IrrecoButtonCreatorDlg*)irreco_button_creator_dlg_new(
719 irreco_data, parent_window);
720 self->irreco_data = irreco_data;
721 self->theme = irreco_theme;
723 IRRECO_DEBUG("Button: %s\n", button->name->str);
724 if (g_utf8_strlen(button->name->str, 1) >0) {
725 /* Sets the button details */
726 _set_button_details(self, irreco_theme, button);
727 edit = TRUE;
731 do {
732 response = gtk_dialog_run(GTK_DIALOG(self));
733 switch (response) {
734 case GTK_RESPONSE_OK:
736 if (edit) {
737 _set_new_button_details(self, button);
738 irreco_theme_button_print(button);
739 rvalue = TRUE;
740 loop = FALSE;
743 } else {
744 /* Check button name */
745 if (irreco_button_creator_dlg_check_details(self)) {
746 _set_new_button_details(self, button);
747 irreco_theme_button_print(button);
748 rvalue = TRUE;
749 loop = FALSE;
750 } else {
751 rvalue = FALSE;
752 loop = TRUE;
756 break;
758 case GTK_RESPONSE_CANCEL:
759 IRRECO_DEBUG("GTK_RESPONSE_CANCEL\n");
760 rvalue = FALSE;
761 loop = FALSE;
762 break;
764 default:
765 IRRECO_DEBUG("default\n");
766 break;
769 } while (loop);
771 gtk_widget_destroy(GTK_WIDGET(self));
772 IRRECO_RETURN_BOOL(rvalue);
776 void
777 _set_button_details(IrrecoButtonCreatorDlg *self, IrrecoTheme *irreco_theme,
778 IrrecoThemeButton *button)
780 IRRECO_ENTER
782 /* Set button information on table */
783 gtk_combo_box_prepend_text(GTK_COMBO_BOX(self->combobox_name),
784 button->name->str);
785 gtk_combo_box_set_active(GTK_COMBO_BOX(self->combobox_name),
787 _draw_preview_image(GTK_BUTTON(self->add_button_unpressed),
788 self,
789 button->image_up->str);
790 _draw_preview_image(GTK_BUTTON(self->add_button_pressed),
791 self,
792 button->image_down->str);
793 if (button->allow_text) {
794 gtk_button_clicked(GTK_BUTTON(self->allow_text));
797 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_padding),
798 button->text_padding);
800 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_h_align),
801 button->text_h_align);
802 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_v_align),
803 button->text_v_align);
804 /*set text format */
805 if (strlen(button->text_format_up->str) > 0) {
807 _set_font_format(self, button->text_format_up->str,
808 gtk_label_get_text(GTK_LABEL(
809 self->label_sample_text_format_up)));
811 if (strlen(button->text_format_down->str) > 0) {
813 _set_font_format(self, button->text_format_down->str,
814 gtk_label_get_text(GTK_LABEL(
815 self->label_sample_text_format_down)));
817 /* Set button label & windown title */
818 gtk_button_set_label(GTK_BUTTON(self->add_button), "Save");
819 gtk_window_set_title(GTK_WINDOW(self), _("Edit a Button"));
820 gtk_widget_set_sensitive(self->combobox_name, FALSE);
823 IRRECO_RETURN
826 void
827 _set_new_button_details(IrrecoButtonCreatorDlg *self, IrrecoThemeButton *button)
831 gchar *style_name = NULL;
832 gboolean allow_text = FALSE;
833 gint text_padding = 0;
834 gfloat text_h_align = 0;
835 gfloat text_v_align = 0;
836 IRRECO_ENTER
838 style_name = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
839 self->combobox_name));
840 allow_text = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
841 self->allow_text));
842 text_padding = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(
843 self->text_padding));
844 text_h_align = gtk_spin_button_get_value(GTK_SPIN_BUTTON(
845 self->text_h_align));
846 text_v_align = gtk_spin_button_get_value(GTK_SPIN_BUTTON(
847 self->text_v_align));
849 irreco_theme_button_set(button,
850 style_name,
851 style_name,
852 allow_text,
853 self->unpressed_path->str,
854 self->pressed_path->str,
855 self->unpressed_format,
856 self->pressed_format,
857 text_padding,
858 text_h_align,
859 text_v_align);
861 if (style_name != NULL) g_free(style_name);
862 IRRECO_RETURN
865 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
866 /* Events and Callbacks */
867 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
870 * @name Events and Callbacks
871 * @{
874 static void
875 _signal_image_clicked(GtkButton *button, IrrecoButtonCreatorDlg *self)
877 IRRECO_ENTER
878 _select_image(button, self);
879 IRRECO_RETURN
882 static void _unpressed_event_signal_image_clicked(GtkWidget *widget,
883 GdkEventButton *event,
884 IrrecoButtonCreatorDlg *self)
886 IRRECO_ENTER
888 gtk_button_clicked(GTK_BUTTON(self->add_button_unpressed));
890 IRRECO_RETURN
893 static void _pressed_event_signal_image_clicked(GtkWidget *widget,
894 GdkEventButton *event,
895 IrrecoButtonCreatorDlg *self)
897 IRRECO_ENTER
899 gtk_button_clicked(GTK_BUTTON(self->add_button_pressed));
901 IRRECO_RETURN
904 static void _toggle_button_toggled(GtkToggleButton *togglebutton,
905 IrrecoButtonCreatorDlg *self)
907 IRRECO_ENTER
909 if (gtk_toggle_button_get_active(togglebutton)) {
910 gtk_button_set_label(GTK_BUTTON(self->allow_text), "YES");
911 gtk_widget_set_sensitive(self->text_format_up, TRUE);
912 gtk_widget_set_sensitive(self->text_format_down, TRUE);
913 } else {
914 gtk_button_set_label(GTK_BUTTON(self->allow_text), "NO");
915 gtk_widget_set_sensitive(self->text_format_up, FALSE);
916 gtk_widget_set_sensitive(self->text_format_down, FALSE);
918 IRRECO_RETURN
922 *Select button font format
925 static void _select_font_format(GtkButton *button, IrrecoButtonCreatorDlg *self)
927 GtkWidget *dialog;
928 gchar *font;
929 GString *format = g_string_new(NULL);
930 gchar *span = ">%s</span>";
933 IRRECO_ENTER
934 dialog = gtk_font_selection_dialog_new("Button font");
936 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
938 font = gtk_font_selection_dialog_get_font_name(
939 GTK_FONT_SELECTION_DIALOG(dialog));
941 g_string_printf(format,
942 "<span font_desc=\"%s\" %s", font, span);
945 if (strcmp("unpressed", gtk_widget_get_name(
946 GTK_WIDGET(button))) == 0) {
947 self->unpressed_format = format->str;
948 _set_font_format(self, format->str,
949 gtk_label_get_text(GTK_LABEL(
950 self->label_sample_text_format_up)));
951 } else {
952 self->pressed_format = format->str;
953 _set_font_format(self, format->str,
954 gtk_label_get_text(GTK_LABEL(
955 self->label_sample_text_format_down)));
958 gtk_widget_destroy(GTK_WIDGET(dialog));
959 IRRECO_RETURN
963 * Set button text format
965 static void
966 _set_font_format(IrrecoButtonCreatorDlg *self, const gchar *button_format,
967 const gchar *text)
971 gchar *markup;
972 GString *format = g_string_new(NULL);
974 IRRECO_ENTER
976 g_string_printf(format, "%s", button_format);
978 markup = g_markup_printf_escaped(format->str, text);
980 IRRECO_DEBUG("TEXT: %s\n", markup);
982 if (strcmp(text, gtk_label_get_text(GTK_LABEL(
983 self->label_sample_text_format_up))) == 0) {
984 gtk_label_set_markup(GTK_LABEL(self->label_sample_text_format_up),
985 markup);
986 gtk_widget_set_sensitive(self->label_sample_text_format_up, TRUE);
987 } else {
989 gtk_label_set_markup(GTK_LABEL(self->label_sample_text_format_down),
990 markup);
991 gtk_widget_set_sensitive(self->label_sample_text_format_down, TRUE);
994 if(markup != NULL) g_free(markup);
995 g_string_free(format, TRUE);
997 IRRECO_RETURN
999 /** @} */