clean code and changed version nro
[irreco.git] / irreco / src / core / irreco_button_creator_dlg.c
blobb103d499c3e859a007cdff2b710a0fe7a896817f
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);
154 /* Create widgets. */
155 self->notebook = gtk_notebook_new();
157 /* Create widgets. */
158 scrolled_table = gtk_scrolled_window_new(NULL, NULL);
159 table = gtk_table_new(7, 9, TRUE);
160 label_name = gtk_label_new(_("Name:"));
161 label_select_unpressed = gtk_label_new(_("Unpressed:"));
162 label_select_pressed = gtk_label_new(_("Pressed:"));
163 self->event_box_pressed = gtk_event_box_new();
164 event_box_unpressed = gtk_event_box_new();
165 self->combobox_name = gtk_combo_box_entry_new_text();
166 frame_unpressed = gtk_frame_new("");
167 frame_pressed = gtk_frame_new("");
168 vbox_unpressed_preview = gtk_vbox_new(FALSE, 8);
169 vbox_pressed_preview = gtk_vbox_new(FALSE, 8);
170 self->label_unpressed_size = gtk_label_new("");
171 self->label_pressed_size = gtk_label_new("");
172 self->preview_image_unpressed = gtk_image_new();
173 self->preview_image_pressed = gtk_image_new();
174 self->add_button_unpressed = gtk_button_new_with_label(_("Select"));
175 self->add_button_pressed = gtk_button_new_with_label(_("Select"));
177 /* Create settings widgets*/
178 scrolled_table_settings = gtk_scrolled_window_new(NULL, NULL);
179 table_settings = gtk_table_new(7, 9, TRUE);
180 label_allow_text = gtk_label_new(_("AllowText:"));
181 label_text_format_up = gtk_label_new(_("TextFormatUp:"));
182 label_text_format_down = gtk_label_new(_("TextFormatDown:"));
183 self->label_sample_text_format_up = gtk_label_new(_("Sample1"));
184 self->label_sample_text_format_down = gtk_label_new(_("Sample2"));
185 label_text_padding = gtk_label_new(_("TextPadding:"));
186 label_text_h_align = gtk_label_new(_("TextHAlign:"));
187 label_text_v_align = gtk_label_new(_("TextVAlign:"));
188 label_size = gtk_label_new(_("ButtonSize:"));
189 self->allow_text = gtk_toggle_button_new();
190 self->text_format_up = gtk_button_new_with_label(_("Select"));
191 self->text_format_down = gtk_button_new_with_label(_("Select"));
192 self->text_padding = gtk_spin_button_new_with_range(0, 20, 1);
193 self->text_h_align = gtk_spin_button_new_with_range(0, 1, 0.1);;
194 self->text_v_align = gtk_spin_button_new_with_range(0, 1, 0.1);;
195 self->unbutton_size = gtk_spin_button_new_with_range(2, 5, 0.1);
196 self->button_size = gtk_spin_button_new_with_range(2, 5, 0.1);
197 self->unpressed_path = g_string_new(NULL);
198 self->pressed_path = g_string_new(NULL);
200 /* set button name/sensitive */
201 gtk_widget_set_name(self->add_button_unpressed,"unpressed");
202 gtk_widget_set_name(self->add_button_pressed,"pressed");
204 gtk_widget_set_sensitive(self->add_button_pressed, FALSE);
205 gtk_widget_set_sensitive(self->event_box_pressed, FALSE);
206 gtk_widget_set_sensitive(self->text_format_up, FALSE);
207 gtk_widget_set_sensitive(self->text_format_down, FALSE);
208 /* Set frame text bold */
209 gtk_frame_set_label_widget(GTK_FRAME(frame_unpressed),
210 irreco_gtk_label_bold(
211 "Unpressed", 0, 0, 0, 0, 0, 0));
212 gtk_frame_set_label_widget(GTK_FRAME(frame_pressed),
213 irreco_gtk_label_bold(
214 "Pressed", 0, 0, 0, 0, 0, 0));
216 /* equal to the text of the left-side */
217 gtk_misc_set_alignment(GTK_MISC(label_name), 0, 0.5);
218 gtk_misc_set_alignment(GTK_MISC(label_select_unpressed), 0, 0.5);
219 gtk_misc_set_alignment(GTK_MISC(label_select_pressed), 0, 0.5);
221 gtk_misc_set_alignment(GTK_MISC(label_allow_text), 0, 0.5);
222 gtk_misc_set_alignment(GTK_MISC(label_text_format_up), 0, 0.5);
223 gtk_misc_set_alignment(GTK_MISC(label_text_format_down), 0, 0.5);
224 gtk_misc_set_alignment(GTK_MISC(self->label_sample_text_format_up), 0, 0.5);
225 gtk_misc_set_alignment(GTK_MISC(self->label_sample_text_format_down), 0, 0.5);
226 gtk_misc_set_alignment(GTK_MISC(label_text_padding), 0, 0.5);
227 gtk_misc_set_alignment(GTK_MISC(label_text_h_align), 0, 0.5);
228 gtk_misc_set_alignment(GTK_MISC(label_text_v_align), 0, 0.5);
229 gtk_misc_set_alignment(GTK_MISC(label_size), 0, 0.5);
231 /* Set table on the scrolled */
232 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(
233 scrolled_table),
234 GTK_WIDGET(table));
236 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_table),
237 GTK_POLICY_NEVER,
238 GTK_POLICY_AUTOMATIC);
240 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(
241 scrolled_table_settings),
242 GTK_WIDGET(table_settings));
244 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(
245 scrolled_table_settings),
246 GTK_POLICY_NEVER,
247 GTK_POLICY_AUTOMATIC);
249 /* Create Notebook tabs. */
251 gtk_notebook_append_page(GTK_NOTEBOOK(self->notebook),
252 irreco_gtk_align(GTK_WIDGET(scrolled_table),
253 0, 0, 1, 1, 8, 8, 8, 8),
254 gtk_label_new("Button"));
255 gtk_notebook_append_page(GTK_NOTEBOOK(self->notebook),
256 irreco_gtk_align(GTK_WIDGET(
257 scrolled_table_settings),
258 0, 0, 1, 1, 8, 8, 8, 8),
259 gtk_label_new("Settings"));
261 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(self)->vbox),
262 self->notebook);
264 /* Set widgets on the table */
266 gtk_table_set_row_spacings(GTK_TABLE(table), 6);
267 gtk_table_set_col_spacings(GTK_TABLE(table), 6);
269 gtk_table_attach_defaults(GTK_TABLE(table), label_name, 0, 3, 0, 1);
270 gtk_table_attach_defaults(GTK_TABLE(table),
271 label_select_unpressed, 0, 3, 1, 2);
272 gtk_table_attach_defaults(GTK_TABLE(table),
273 label_select_pressed, 0, 3, 2, 3);
274 gtk_table_attach_defaults(GTK_TABLE(table),
275 self->combobox_name, 3, 9, 0, 1);
276 gtk_table_attach_defaults(GTK_TABLE(table),
277 self->add_button_unpressed, 3, 9, 1, 2);
278 gtk_table_attach_defaults(GTK_TABLE(table),
279 self->add_button_pressed, 3, 9, 2, 3);
280 gtk_table_attach_defaults(GTK_TABLE(table),
281 frame_unpressed, 0, 4, 3, 7);
282 gtk_table_attach_defaults(GTK_TABLE(table), frame_pressed, 5, 9, 3, 7);
283 /* Set widgets on the table_settings */
284 gtk_table_set_row_spacings(GTK_TABLE(table_settings), 6);
285 gtk_table_set_col_spacings(GTK_TABLE(table_settings), 6);
287 gtk_table_attach_defaults(GTK_TABLE(table_settings),
288 label_allow_text, 0, 4, 0, 1);
289 gtk_table_attach_defaults(GTK_TABLE(table_settings),
290 label_text_format_up, 0, 4, 1, 2);
291 gtk_table_attach_defaults(GTK_TABLE(table_settings),
292 label_text_format_down, 0, 4, 2, 3);
293 gtk_table_attach_defaults(GTK_TABLE(table_settings),
294 self->label_sample_text_format_up, 4, 7, 1, 2);
295 gtk_table_attach_defaults(GTK_TABLE(table_settings),
296 self->label_sample_text_format_down, 4, 7, 2, 3);
297 gtk_table_attach_defaults(GTK_TABLE(table_settings),
298 label_text_padding, 0, 4, 3, 4);
299 gtk_table_attach_defaults(GTK_TABLE(table_settings),
300 label_text_h_align, 0, 4, 4, 5);
301 gtk_table_attach_defaults(GTK_TABLE(table_settings),
302 label_text_v_align, 0, 4, 5, 6);
303 gtk_table_attach_defaults(GTK_TABLE(table_settings),
304 label_size, 0, 4, 6, 7);
306 gtk_table_attach_defaults(GTK_TABLE(table_settings),
307 self->allow_text, 4, 9, 0, 1);
308 gtk_table_attach_defaults(GTK_TABLE(table_settings),
309 self->text_format_up, 7, 9, 1, 2);
310 gtk_table_attach_defaults(GTK_TABLE(table_settings),
311 self->text_format_down, 7, 9, 2, 3);
312 gtk_table_attach_defaults(GTK_TABLE(table_settings),
313 self->text_padding, 4, 9, 3, 4);
314 gtk_table_attach_defaults(GTK_TABLE(table_settings),
315 self->text_h_align, 4, 9, 4, 5);
316 gtk_table_attach_defaults(GTK_TABLE(table_settings),
317 self->text_v_align, 4, 9, 5, 6);
318 gtk_table_attach_defaults(GTK_TABLE(table_settings),
319 self->unbutton_size, 4, 6, 6, 7);
320 gtk_table_attach_defaults(GTK_TABLE(table_settings),
321 self->button_size, 7, 9, 6, 7);
322 /* set preview image*/
324 gtk_container_add(GTK_CONTAINER(frame_unpressed), event_box_unpressed);
325 gtk_container_add(GTK_CONTAINER(event_box_unpressed),
326 vbox_unpressed_preview);
327 gtk_container_add(GTK_CONTAINER(vbox_unpressed_preview),
328 self->preview_image_unpressed);
329 gtk_container_add(GTK_CONTAINER(vbox_unpressed_preview),
330 self->label_unpressed_size);
332 gtk_container_add(GTK_CONTAINER(frame_pressed), self->event_box_pressed);
333 gtk_container_add(GTK_CONTAINER(self->event_box_pressed),
334 vbox_pressed_preview);
335 gtk_container_add(GTK_CONTAINER(vbox_pressed_preview),
336 self->preview_image_pressed);
337 gtk_container_add(GTK_CONTAINER(vbox_pressed_preview),
338 self->label_pressed_size);
343 /*Added button list on combobox*/
344 fill_buttons_combobox(self);
346 /* set default settings on tab */
347 gtk_button_set_label(GTK_BUTTON(self->allow_text), "NO");
349 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_padding), 5);
350 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_h_align), 0.5);
351 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_v_align), 0.5);
352 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->unbutton_size), 10);
353 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->button_size), 10);
355 /* Set button name */
356 gtk_widget_set_name(self->text_format_up, "unpressed");
357 gtk_widget_set_name(self->text_format_down, "pressed");
358 /* Button signals. */
359 g_signal_connect(G_OBJECT(self->add_button_unpressed), "clicked",
360 G_CALLBACK(_signal_image_clicked), self);
361 g_signal_connect(G_OBJECT(self->add_button_pressed), "clicked",
362 G_CALLBACK(_signal_image_clicked), self);
364 g_signal_connect(G_OBJECT(event_box_unpressed), "button-release-event",
365 G_CALLBACK(_unpressed_event_signal_image_clicked),
366 self);
367 g_signal_connect(G_OBJECT(self->event_box_pressed),
368 "button-release-event",
369 G_CALLBACK(_pressed_event_signal_image_clicked),
370 self);
372 g_signal_connect(G_OBJECT(self->allow_text), "toggled",
373 G_CALLBACK(_toggle_button_toggled), self);
375 g_signal_connect(G_OBJECT(self->text_format_up), "clicked",
376 G_CALLBACK(_select_font_format), self);
377 g_signal_connect(G_OBJECT(self->text_format_down), "clicked",
378 G_CALLBACK(_select_font_format), self);
379 g_signal_connect(G_OBJECT(self->unbutton_size), "value-changed",
380 G_CALLBACK(_unbutton_size_changed), self);
381 g_signal_connect(G_OBJECT(self->button_size), "value-changed",
382 G_CALLBACK(_button_size_changed), self);
384 gtk_widget_set_size_request(GTK_WIDGET(self), 696, 396);
385 gtk_widget_show_all(GTK_WIDGET(self));
387 /* Hide Button size widgets */
388 gtk_widget_hide(label_size);
389 gtk_widget_hide(self->unbutton_size);
390 gtk_widget_hide(self->button_size);
392 IRRECO_RETURN
395 static void
396 irreco_button_creator_dlg_init (IrrecoButtonCreatorDlg *object)
398 IRRECO_ENTER
399 IRRECO_RETURN
402 static void
403 irreco_button_creator_dlg_finalize (GObject *object)
405 /* TODO: Add deinitalization code here */
407 IrrecoButtonCreatorDlg *self;
408 IRRECO_ENTER
410 self = IRRECO_BUTTON_CREATOR_DLG(object);
412 G_OBJECT_CLASS(irreco_button_creator_dlg_parent_class)->finalize(object);
413 IRRECO_RETURN
417 static void
418 irreco_button_creator_dlg_class_init (IrrecoButtonCreatorDlgClass *klass)
420 GObjectClass* object_class = G_OBJECT_CLASS (klass);
422 object_class->finalize = irreco_button_creator_dlg_finalize;
423 object_class->constructed = irreco_button_creator_dlg_constructed;
426 GtkWidget
427 *irreco_button_creator_dlg_new(IrrecoData *irreco_data, GtkWindow *parent)
429 IrrecoButtonCreatorDlg *self;
431 IRRECO_ENTER
433 self = g_object_new(IRRECO_TYPE_BUTTON_CREATOR_DLG,
434 "irreco-data", irreco_data, NULL);
435 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
437 IRRECO_RETURN_PTR(self);
440 /** @} */
442 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
443 /* Private Functions */
444 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
446 * @name Private Functions
447 * @{
451 *Fill combobox from file
453 static void fill_buttons_combobox(IrrecoButtonCreatorDlg *self)
456 GKeyFile *button_list;
457 gchar **keys;
458 gsize length;
459 gint i=0;
460 GError *error = NULL;
462 IRRECO_ENTER
464 if (irreco_file_exists(BUTTON_LIST_DIR)) {
465 button_list = g_key_file_new();
466 g_key_file_load_from_file(button_list, BUTTON_LIST_DIR,
467 G_KEY_FILE_NONE, &error);
468 keys = g_key_file_get_keys(button_list, "button-names", &length,
469 &error);
470 for(i=0; i<length; i++){
471 gtk_combo_box_append_text(GTK_COMBO_BOX(self->combobox_name),
472 g_key_file_get_string(button_list,
473 "button-names", keys[i], &error));
475 g_key_file_free(button_list);
477 if(keys != NULL) g_free(keys);
480 IRRECO_RETURN
483 void
484 _unbutton_size_changed(GtkSpinButton *spinbutton, IrrecoButtonCreatorDlg*self)
486 GString *image_path = g_string_new(self->unpressed_path->str);
488 IRRECO_ENTER
490 _draw_preview_image(GTK_BUTTON(self->text_format_up), self,
491 image_path->str);
493 g_string_free(image_path, TRUE);
494 IRRECO_RETURN
496 void
497 _button_size_changed(GtkSpinButton *spinbutton, IrrecoButtonCreatorDlg*self)
499 GString *image_path = g_string_new(self->unpressed_path->str);
501 IRRECO_ENTER
503 _draw_preview_image(GTK_BUTTON(self->text_format_down), self,
504 image_path->str);
506 g_string_free(image_path, TRUE);
507 IRRECO_RETURN
511 * Draw preview with current image.
513 static gboolean
514 _draw_preview_image(GtkButton *button, IrrecoButtonCreatorDlg *self,
515 const gchar *image)
517 GError *error = NULL;
518 GdkPixbuf *pixbuf = NULL;
519 GString *size = g_string_new(NULL);
521 IRRECO_ENTER
523 g_assert(self != NULL);
524 /* compared with whichever image */
525 if (g_str_equal("pressed", gtk_widget_get_name(GTK_WIDGET(button)))) {
526 gint button_width = 0;
527 gint button_height = 0;
528 GdkPixbuf *pixbuf_size = NULL;
530 if (image != NULL) {
531 pixbuf = gdk_pixbuf_new_from_file_at_scale(image,
532 IRRECO_BUTTON_PREVIEW_WIDHT,
533 IRRECO_BUTTON_PREVIEW_HEIGHT,
534 TRUE, &error);
535 /* set sensitive if image are selected */
536 gtk_widget_set_sensitive(self->add_button, TRUE);
538 if (irreco_gerror_check_print(&error)) {
539 IRRECO_RETURN_BOOL(FALSE);
543 g_string_printf(self->pressed_path, "%s", image);
544 gtk_widget_realize(GTK_WIDGET(self->preview_image_pressed));
545 gtk_image_set_from_pixbuf(GTK_IMAGE(self->preview_image_pressed),
546 GDK_PIXBUF(pixbuf));
548 /* Show image real size */
549 pixbuf_size = gdk_pixbuf_new_from_file(image, &error);
550 button_width = atoi(g_strdup_printf("%.0f",
551 (gdk_pixbuf_get_width(
552 pixbuf_size))/
553 (6-gtk_spin_button_get_value(GTK_SPIN_BUTTON(
554 self->button_size)))));
555 button_height = atoi(g_strdup_printf("%.0f",
556 (gdk_pixbuf_get_height(
557 pixbuf_size)/
558 (6-gtk_spin_button_get_value(GTK_SPIN_BUTTON(
559 self->button_size))))));
561 if (pixbuf_size != NULL) g_object_unref(G_OBJECT(pixbuf_size));
563 g_string_printf(size, "%sx%s", g_strdup_printf("%d", button_width),
564 g_strdup_printf("%d", button_height));
566 gtk_label_set_text(GTK_LABEL(self->label_pressed_size),
567 size->str);
569 } else {
570 gint button_width = 0;
571 gint button_height = 0;
572 GdkPixbuf *pixbuf_size = NULL;
574 if (image != NULL) {
575 pixbuf = gdk_pixbuf_new_from_file_at_scale(image,
576 IRRECO_BUTTON_PREVIEW_WIDHT,
577 IRRECO_BUTTON_PREVIEW_HEIGHT,
578 TRUE, &error);
579 if (irreco_gerror_check_print(&error)) {
580 IRRECO_RETURN_BOOL(FALSE);
583 gtk_widget_realize(GTK_WIDGET(self->preview_image_unpressed));
584 gtk_image_set_from_pixbuf(GTK_IMAGE(
585 self->preview_image_unpressed),
586 GDK_PIXBUF(pixbuf));
588 /* Show image real size */
589 pixbuf_size = gdk_pixbuf_new_from_file(image, &error);
590 button_width = atoi(g_strdup_printf("%.0f",
591 (gdk_pixbuf_get_width(
592 pixbuf_size))/
593 (6-gtk_spin_button_get_value(GTK_SPIN_BUTTON(
594 self->unbutton_size)))));
595 button_height = atoi(g_strdup_printf("%.0f",
596 (gdk_pixbuf_get_height(
597 pixbuf_size)/
598 (6-gtk_spin_button_get_value(GTK_SPIN_BUTTON(
599 self->unbutton_size))))));
600 if (pixbuf_size != NULL) g_object_unref(G_OBJECT(pixbuf_size));
602 g_string_printf(size, "%sx%s", g_strdup_printf("%d", button_width),
603 g_strdup_printf("%d", button_height));
605 gtk_label_set_text(GTK_LABEL(self->label_unpressed_size),
606 size->str);
607 g_string_printf(self->unpressed_path, "%s", image);
609 /* Set button sensitive */
610 gtk_widget_set_sensitive(self->add_button_pressed, TRUE);
611 gtk_widget_set_sensitive(self->event_box_pressed, TRUE);
614 g_string_free(size, TRUE);
615 if (pixbuf != NULL) g_object_unref(G_OBJECT(pixbuf));
616 IRRECO_RETURN_BOOL(TRUE);
619 static void _select_image(GtkButton *button, IrrecoButtonCreatorDlg *self)
622 GtkWidget *file_dlg = NULL;
623 gchar *image_dir = NULL;
624 IRRECO_ENTER
626 /* Create image select dialog. */
628 file_dlg = hildon_file_chooser_dialog_new(GTK_WINDOW(self),
629 GTK_FILE_CHOOSER_ACTION_OPEN);
630 gtk_window_set_title(GTK_WINDOW(file_dlg),_("Select button image"));
631 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(file_dlg), TRUE);
632 image_dir = g_build_path("/", getenv("HOME"), "MyDocs/.images/", NULL);
633 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(file_dlg),
634 image_dir);
635 g_free(image_dir);
637 /* Loop until user cancels or we get a valid image. */
638 gtk_widget_show_all(GTK_WIDGET(file_dlg));
639 while (gtk_dialog_run(GTK_DIALOG(file_dlg)) == GTK_RESPONSE_OK) {
640 gchar *filename;
641 filename = gtk_file_chooser_get_filename(
642 GTK_FILE_CHOOSER(file_dlg));
644 /* Attempt to display the image. */
645 if (_draw_preview_image(button, self, filename)) {
646 irreco_gstring_set_and_free(self->filename, filename);
647 filename = NULL;
648 break;
650 /* Cleanup */
651 } else {
652 gchar *basename = g_path_get_basename(filename);
653 irreco_error_dlg_printf(GTK_WINDOW(file_dlg),
654 _("Cannot open image \"%s\""),
655 basename);
656 g_free(basename);
657 g_free(filename);
661 gtk_widget_destroy(file_dlg);
662 IRRECO_RETURN
664 gboolean irreco_button_creator_dlg_check_details(IrrecoButtonCreatorDlg *self)
666 gboolean rvalue = TRUE;
667 gchar *name = NULL;
668 gchar *style_name = NULL;
669 IRRECO_ENTER
671 name = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
672 self->combobox_name));
673 style_name = g_strconcat(self->theme->name->str ,"/", name, NULL);
674 /*check that it is not the same name button*/
675 if (irreco_string_table_exists (self->theme->buttons, name)) {
676 irreco_error_dlg(GTK_WINDOW(self),
677 "Button has already existed");
678 rvalue = FALSE;
679 } else if (irreco_string_table_exists(self->theme->buttons,
680 style_name)) {
681 irreco_error_dlg(GTK_WINDOW(self),
682 "Button has already existed");
683 rvalue = FALSE;
685 if (g_utf8_strlen(name, 1) ==0) {
686 irreco_error_dlg(GTK_WINDOW(self),
687 "Set Button name");
688 rvalue = FALSE;
690 IRRECO_RETURN_BOOL(rvalue);
692 /** @} */
694 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
695 /* Functions */
696 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
698 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
699 /* Public Functions */
700 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
702 gboolean irreco_button_creator_dlg_run(IrrecoData *irreco_data,
703 IrrecoTheme * irreco_theme,
704 GtkWindow *parent_window,
705 IrrecoThemeButton *button)
707 IrrecoButtonCreatorDlg *self;
708 gint response;
709 gboolean loop = TRUE;
710 gboolean rvalue = FALSE;
711 gboolean edit = FALSE;
712 IRRECO_ENTER
714 self = (IrrecoButtonCreatorDlg*)irreco_button_creator_dlg_new(
715 irreco_data, parent_window);
716 self->irreco_data = irreco_data;
717 self->theme = irreco_theme;
719 IRRECO_DEBUG("Button: %s\n", button->name->str);
720 if (g_utf8_strlen(button->name->str, 1) >0) {
721 /* Sets the button details */
722 _set_button_details(self, irreco_theme, button);
723 edit = TRUE;
727 do {
728 response = gtk_dialog_run(GTK_DIALOG(self));
729 switch (response) {
730 case GTK_RESPONSE_OK:
732 if (edit) {
733 _set_new_button_details(self, button);
734 irreco_theme_button_print(button);
735 rvalue = TRUE;
736 loop = FALSE;
739 } else {
740 /* Check button name */
741 if (irreco_button_creator_dlg_check_details(self)) {
742 _set_new_button_details(self, button);
743 irreco_theme_button_print(button);
744 rvalue = TRUE;
745 loop = FALSE;
746 } else {
747 rvalue = FALSE;
748 loop = TRUE;
752 break;
754 case GTK_RESPONSE_CANCEL:
755 IRRECO_DEBUG("GTK_RESPONSE_CANCEL\n");
756 rvalue = FALSE;
757 loop = FALSE;
758 break;
760 default:
761 IRRECO_DEBUG("default\n");
762 break;
765 } while (loop);
767 gtk_widget_destroy(GTK_WIDGET(self));
768 IRRECO_RETURN_BOOL(rvalue);
772 void
773 _set_button_details(IrrecoButtonCreatorDlg *self, IrrecoTheme *irreco_theme,
774 IrrecoThemeButton *button)
776 IRRECO_ENTER
778 /* Set button information on table */
779 gtk_combo_box_prepend_text(GTK_COMBO_BOX(self->combobox_name),
780 button->name->str);
781 gtk_combo_box_set_active(GTK_COMBO_BOX(self->combobox_name),
783 _draw_preview_image(GTK_BUTTON(self->add_button_unpressed),
784 self,
785 button->image_up->str);
786 _draw_preview_image(GTK_BUTTON(self->add_button_pressed),
787 self,
788 button->image_down->str);
789 if (button->allow_text) {
790 gtk_button_clicked(GTK_BUTTON(self->allow_text));
793 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_padding),
794 button->text_padding);
796 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_h_align),
797 button->text_h_align);
798 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_v_align),
799 button->text_v_align);
800 /*set text format */
801 if (strlen(button->text_format_up->str) > 0) {
803 _set_font_format(self, button->text_format_up->str,
804 gtk_label_get_text(GTK_LABEL(
805 self->label_sample_text_format_up)));
807 if (strlen(button->text_format_down->str) > 0) {
809 _set_font_format(self, button->text_format_down->str,
810 gtk_label_get_text(GTK_LABEL(
811 self->label_sample_text_format_down)));
813 /* Set button label & windown title */
814 gtk_button_set_label(GTK_BUTTON(self->add_button), "Save");
815 gtk_window_set_title(GTK_WINDOW(self), _("Edit Button"));
816 gtk_widget_set_sensitive(self->combobox_name, FALSE);
819 IRRECO_RETURN
822 void
823 _set_new_button_details(IrrecoButtonCreatorDlg *self, IrrecoThemeButton *button)
827 gchar *style_name = NULL;
828 gboolean allow_text = FALSE;
829 gint text_padding = 0;
830 gfloat text_h_align = 0;
831 gfloat text_v_align = 0;
832 IRRECO_ENTER
834 style_name = gtk_combo_box_get_active_text(GTK_COMBO_BOX(
835 self->combobox_name));
836 allow_text = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
837 self->allow_text));
838 text_padding = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(
839 self->text_padding));
840 text_h_align = gtk_spin_button_get_value(GTK_SPIN_BUTTON(
841 self->text_h_align));
842 text_v_align = gtk_spin_button_get_value(GTK_SPIN_BUTTON(
843 self->text_v_align));
845 irreco_theme_button_set(button,
846 style_name,
847 style_name,
848 allow_text,
849 self->unpressed_path->str,
850 self->pressed_path->str,
851 self->unpressed_format,
852 self->pressed_format,
853 text_padding,
854 text_h_align,
855 text_v_align);
857 g_free(style_name);
858 IRRECO_RETURN
861 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
862 /* Events and Callbacks */
863 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
866 * @name Events and Callbacks
867 * @{
870 static void
871 _signal_image_clicked(GtkButton *button, IrrecoButtonCreatorDlg *self)
873 IRRECO_ENTER
874 _select_image(button, self);
875 IRRECO_RETURN
878 static void _unpressed_event_signal_image_clicked(GtkWidget *widget,
879 GdkEventButton *event,
880 IrrecoButtonCreatorDlg *self)
882 IRRECO_ENTER
884 gtk_button_clicked(GTK_BUTTON(self->add_button_unpressed));
886 IRRECO_RETURN
889 static void _pressed_event_signal_image_clicked(GtkWidget *widget,
890 GdkEventButton *event,
891 IrrecoButtonCreatorDlg *self)
893 IRRECO_ENTER
895 gtk_button_clicked(GTK_BUTTON(self->add_button_pressed));
897 IRRECO_RETURN
900 static void _toggle_button_toggled(GtkToggleButton *togglebutton,
901 IrrecoButtonCreatorDlg *self)
903 IRRECO_ENTER
905 if (gtk_toggle_button_get_active(togglebutton)) {
906 gtk_button_set_label(GTK_BUTTON(self->allow_text), "YES");
907 gtk_widget_set_sensitive(self->text_format_up, TRUE);
908 gtk_widget_set_sensitive(self->text_format_down, TRUE);
909 } else {
910 gtk_button_set_label(GTK_BUTTON(self->allow_text), "NO");
911 gtk_widget_set_sensitive(self->text_format_up, FALSE);
912 gtk_widget_set_sensitive(self->text_format_down, FALSE);
914 IRRECO_RETURN
918 *Select button font format
921 static void _select_font_format(GtkButton *button, IrrecoButtonCreatorDlg *self)
923 GtkWidget *dialog;
924 const gchar *font;
925 GString *format = g_string_new(NULL);
926 const gchar *span = ">%s</span>";
929 IRRECO_ENTER
930 dialog = gtk_font_selection_dialog_new("Button font");
932 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
934 font = gtk_font_selection_dialog_get_font_name(
935 GTK_FONT_SELECTION_DIALOG(dialog));
937 /*g_string_printf(format,
938 "<span foreground=\"%s\" font_desc=\"%s\" %s",
939 "red", font, span);*/
940 g_string_printf(format,
941 "<span font_desc=\"%s\" %s", font, span);
944 if (strcmp("unpressed", gtk_widget_get_name(
945 GTK_WIDGET(button))) == 0) {
946 self->unpressed_format = format->str;
947 _set_font_format(self, format->str,
948 gtk_label_get_text(GTK_LABEL(
949 self->label_sample_text_format_up)));
950 } else {
951 self->pressed_format = format->str;
952 _set_font_format(self, format->str,
953 gtk_label_get_text(GTK_LABEL(
954 self->label_sample_text_format_down)));
957 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 } else {
988 gtk_label_set_markup(GTK_LABEL(self->label_sample_text_format_down),
989 markup);
992 if(markup != NULL) g_free(markup);
993 g_string_free(format, TRUE);
995 IRRECO_RETURN
997 /** @} */