Added the image size change
[irreco.git] / irreco / src / core / irreco_button_creator_dlg.c
blob927377fd4c870f56713ba0d1505e66a661ded2ad
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 "/home/pekka/git/irreco/irreco/src/core/button_list.conf"
53 /** Loader states. */
54 enum
56 LOADER_STATE_INIT,
57 LOADER_STATE_THEME,
58 LOADER_STATE_BUTTONS,
59 LOADER_STATE_BACKGROUNDS,
60 LOADER_STATE_END
64 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
65 /* Prototypes */
66 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
67 static void
68 _signal_image_clicked(GtkButton *button, IrrecoButtonCreatorDlg *self);
69 static void
70 _unpressed_event_signal_image_clicked(GtkWidget *widget, GdkEventButton *event,
71 IrrecoButtonCreatorDlg *self);
72 static void
73 _pressed_event_signal_image_clicked(GtkWidget *widget, GdkEventButton *event,
74 IrrecoButtonCreatorDlg *self);
75 static gboolean _draw_preview_image(GtkButton *button,
76 IrrecoButtonCreatorDlg *self,
77 const gchar *image);
78 static void _toggle_button_toggled(GtkToggleButton *togglebutton,
79 IrrecoButtonCreatorDlg *self);
80 static void fill_buttons_combobox(IrrecoButtonCreatorDlg *self);
81 static void
82 _select_font_format(GtkButton *button, IrrecoButtonCreatorDlg *self);
84 static void
85 _set_font_format(IrrecoButtonCreatorDlg *self, const gchar *button_format,
86 const gchar *text);
87 void
88 _set_button_details(IrrecoButtonCreatorDlg *self, IrrecoTheme *irreco_theme,
89 IrrecoThemeButton *button);
90 void
91 _unbutton_size_changed(GtkSpinButton *spinbutton, IrrecoButtonCreatorDlg*self);
92 void
93 _button_size_changed(GtkSpinButton *spinbutton, IrrecoButtonCreatorDlg*self);
94 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
95 /* Construction & Destruction */
96 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
98 /**
99 * @name Construction & Destruction
100 * @{
103 G_DEFINE_TYPE (IrrecoButtonCreatorDlg, irreco_button_creator_dlg,
104 IRRECO_TYPE_INTERNAL_DLG)
105 static void irreco_button_creator_dlg_constructed(GObject *object)
107 /* TODO: Add initialization code here */
109 IrrecoData *irreco_data;
110 IrrecoButtonCreatorDlg *self;
112 GtkWidget *table;
113 GtkWidget *frame_unpressed;
114 GtkWidget *frame_pressed;
115 GtkWidget *label_name;
116 GtkWidget *label_select_unpressed;
117 GtkWidget *label_select_pressed;
118 GtkWidget *event_box_unpressed;
119 GtkWidget *vbox_unpressed_preview;
120 GtkWidget *vbox_pressed_preview;
121 GtkWidget *label_size;
123 /* Settings */
124 GtkWidget *table_settings;
125 GtkWidget *label_allow_text;
127 GtkWidget *label_text_padding;
128 GtkWidget *label_text_h_align;
129 GtkWidget *label_text_v_align;
134 IRRECO_ENTER
136 G_OBJECT_CLASS(irreco_button_creator_dlg_parent_class)->constructed(object);
138 self = IRRECO_BUTTON_CREATOR_DLG(object);
140 irreco_data = irreco_internal_dlg_get_irreco_data(IRRECO_INTERNAL_DLG(self));
142 /* Construct dialog. */
143 gtk_window_set_title(GTK_WINDOW(self), _("Button Creator "));
144 gtk_window_set_modal(GTK_WINDOW(self), TRUE);
145 gtk_window_set_destroy_with_parent(GTK_WINDOW(self), TRUE);
146 gtk_dialog_set_has_separator(GTK_DIALOG(self), FALSE);
148 /*Buttons*/
149 self->cancel_button= gtk_dialog_add_button(GTK_DIALOG(self),
150 _("Cancel"),
151 GTK_RESPONSE_CANCEL);
152 self->add_button= gtk_dialog_add_button(GTK_DIALOG(self), _("Add"),
153 GTK_RESPONSE_OK);
154 gtk_widget_set_sensitive(self->add_button, FALSE);
157 /* Create widgets. */
158 self->notebook = gtk_notebook_new();
160 /* Create widgets. */
162 table = gtk_table_new(7, 9, TRUE);
163 label_name = gtk_label_new(_ ("Name: "));
164 label_select_unpressed = gtk_label_new(_("Unpressed: "));
165 label_select_pressed = gtk_label_new(_ ("Pressed: "));
166 self->event_box_pressed = gtk_event_box_new();
167 event_box_unpressed = gtk_event_box_new();
168 self->combobox_name = gtk_combo_box_entry_new_text();
169 frame_unpressed = gtk_frame_new("");
170 frame_pressed = gtk_frame_new("");
171 vbox_unpressed_preview = gtk_vbox_new(FALSE, 8);
172 vbox_pressed_preview = gtk_vbox_new(FALSE, 8);
173 self->label_unpressed_size = gtk_label_new("");
174 self->label_pressed_size = gtk_label_new("");
175 self->preview_image_unpressed = gtk_image_new();
176 self->preview_image_pressed = gtk_image_new();
177 self->add_button_unpressed = gtk_button_new_with_label(_("Select"));
178 self->add_button_pressed = gtk_button_new_with_label(_("Select"));
179 label_size = gtk_label_new(_("ButtonSize:"));
181 /* settings */
182 table_settings = gtk_table_new(7, 9, TRUE);
183 label_allow_text = gtk_label_new(_("AllowText:"));
184 self->label_text_format_up = gtk_label_new(_("TextFormatUp:"));
185 self->label_text_format_down = gtk_label_new(_("TextFormatDown:"));
186 label_text_padding = gtk_label_new(_("Text:Padding:"));
187 label_text_h_align = gtk_label_new(_("TextHAlign:"));
188 label_text_v_align = gtk_label_new(_("TextVAlign:"));
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);
198 self->unpressed_path = g_string_new(NULL);
199 self->pressed_path = g_string_new(NULL);
201 /* set button name/sensitive */
202 gtk_widget_set_name(self->add_button_unpressed,"unpressed");
203 gtk_widget_set_name(self->add_button_pressed,"pressed");
205 gtk_widget_set_sensitive(self->add_button_pressed, FALSE);
206 gtk_widget_set_sensitive(self->event_box_pressed, FALSE);
207 gtk_widget_set_sensitive(self->text_format_up, FALSE);
208 gtk_widget_set_sensitive(self->text_format_down, FALSE);
209 /* Set frame text bold */
210 gtk_frame_set_label_widget(GTK_FRAME(frame_unpressed),
211 irreco_gtk_label_bold(
212 "Unpressed", 0, 0, 0, 0, 0, 0));
213 gtk_frame_set_label_widget(GTK_FRAME(frame_pressed),
214 irreco_gtk_label_bold(
215 "Pressed", 0, 0, 0, 0, 0, 0));
217 /* Create Notebook tabs. */
219 gtk_notebook_append_page(GTK_NOTEBOOK(self->notebook),
220 irreco_gtk_align(GTK_WIDGET(table),
221 0, 0, 1, 1, 8, 8, 8, 8),
222 gtk_label_new("BUTTONS"));
223 gtk_notebook_append_page(GTK_NOTEBOOK(self->notebook),
224 irreco_gtk_align(GTK_WIDGET(table_settings),
225 0, 0, 1, 1, 8, 8, 8, 8),
226 gtk_label_new("SETTINGS"));
228 gtk_box_pack_start_defaults(GTK_BOX(GTK_DIALOG(self)->vbox),
229 self->notebook);
231 /* Set widgets on the table */
233 gtk_table_set_row_spacings(GTK_TABLE(table), 6);
234 gtk_table_set_col_spacings(GTK_TABLE(table), 6);
236 gtk_table_attach_defaults(GTK_TABLE(table), label_name, 0, 3, 0, 1);
237 gtk_table_attach_defaults(GTK_TABLE(table),
238 label_select_unpressed, 0, 3, 1, 2);
239 gtk_table_attach_defaults(GTK_TABLE(table),
240 label_select_pressed, 0, 3, 2, 3);
241 gtk_table_attach_defaults(GTK_TABLE(table),
242 self->combobox_name, 3, 9, 0, 1);
243 gtk_table_attach_defaults(GTK_TABLE(table),
244 self->add_button_unpressed, 3, 9, 1, 2);
245 gtk_table_attach_defaults(GTK_TABLE(table),
246 self->add_button_pressed, 3, 9, 2, 3);
247 gtk_table_attach_defaults(GTK_TABLE(table),
248 frame_unpressed, 0, 4, 3, 7);
249 gtk_table_attach_defaults(GTK_TABLE(table), frame_pressed, 5, 9, 3, 7);
250 /* Set widgets on the table_settings */
251 gtk_table_set_row_spacings(GTK_TABLE(table_settings), 6);
252 gtk_table_set_col_spacings(GTK_TABLE(table_settings), 6);
254 gtk_table_attach_defaults(GTK_TABLE(table_settings),
255 label_allow_text, 0, 4, 0, 1);
256 gtk_table_attach_defaults(GTK_TABLE(table_settings),
257 self->label_text_format_up, 0, 4, 1, 2);
258 gtk_table_attach_defaults(GTK_TABLE(table_settings),
259 self->label_text_format_down, 0, 4, 2, 3);
260 gtk_table_attach_defaults(GTK_TABLE(table_settings),
261 label_text_padding, 0, 4, 3, 4);
262 gtk_table_attach_defaults(GTK_TABLE(table_settings),
263 label_text_h_align, 0, 4, 4, 5);
264 gtk_table_attach_defaults(GTK_TABLE(table_settings),
265 label_text_v_align, 0, 4, 5, 6);
266 gtk_table_attach_defaults(GTK_TABLE(table_settings),
267 label_size, 0, 4, 6, 7);
269 gtk_table_attach_defaults(GTK_TABLE(table_settings),
270 self->allow_text, 4, 9, 0, 1);
271 gtk_table_attach_defaults(GTK_TABLE(table_settings),
272 self->text_format_up, 4, 9, 1, 2);
273 gtk_table_attach_defaults(GTK_TABLE(table_settings),
274 self->text_format_down, 4, 9, 2, 3);
275 gtk_table_attach_defaults(GTK_TABLE(table_settings),
276 self->text_padding, 4, 9, 3, 4);
277 gtk_table_attach_defaults(GTK_TABLE(table_settings),
278 self->text_h_align, 4, 9, 4, 5);
279 gtk_table_attach_defaults(GTK_TABLE(table_settings),
280 self->text_v_align, 4, 9, 5, 6);
281 gtk_table_attach_defaults(GTK_TABLE(table_settings),
282 self->unbutton_size, 4, 6, 6, 7);
283 gtk_table_attach_defaults(GTK_TABLE(table_settings),
284 self->button_size, 7, 9, 6, 7);
285 /* set preview image*/
287 gtk_container_add(GTK_CONTAINER(frame_unpressed), event_box_unpressed);
288 gtk_container_add(GTK_CONTAINER(event_box_unpressed),
289 vbox_unpressed_preview);
290 gtk_container_add(GTK_CONTAINER(vbox_unpressed_preview),
291 self->preview_image_unpressed);
292 gtk_container_add(GTK_CONTAINER(vbox_unpressed_preview),
293 self->label_unpressed_size);
295 gtk_container_add(GTK_CONTAINER(frame_pressed), self->event_box_pressed);
296 gtk_container_add(GTK_CONTAINER(self->event_box_pressed),
297 vbox_pressed_preview);
298 gtk_container_add(GTK_CONTAINER(vbox_pressed_preview),
299 self->preview_image_pressed);
300 gtk_container_add(GTK_CONTAINER(vbox_pressed_preview),
301 self->label_pressed_size);
306 /*Added button list on combobox*/
307 fill_buttons_combobox(self);
309 /* set default settings on tab */
310 gtk_button_set_label(GTK_BUTTON(self->allow_text), "NO");
312 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_padding), 5);
313 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_h_align), 0.5);
314 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_v_align), 0.5);
315 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->unbutton_size), 10);
316 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->button_size), 10);
318 /* Set button name */
319 gtk_widget_set_name(self->text_format_up, "unpressed");
320 gtk_widget_set_name(self->text_format_down, "pressed");
321 /* Button signals. */
322 g_signal_connect(G_OBJECT(self->add_button_unpressed), "clicked",
323 G_CALLBACK(_signal_image_clicked), self);
324 g_signal_connect(G_OBJECT(self->add_button_pressed), "clicked",
325 G_CALLBACK(_signal_image_clicked), self);
327 g_signal_connect(G_OBJECT(event_box_unpressed), "button-release-event",
328 G_CALLBACK(_unpressed_event_signal_image_clicked),
329 self);
330 g_signal_connect(G_OBJECT(self->event_box_pressed),
331 "button-release-event",
332 G_CALLBACK(_pressed_event_signal_image_clicked),
333 self);
335 g_signal_connect(G_OBJECT(self->allow_text), "toggled",
336 G_CALLBACK(_toggle_button_toggled), self);
338 g_signal_connect(G_OBJECT(self->text_format_up), "clicked",
339 G_CALLBACK(_select_font_format), self);
340 g_signal_connect(G_OBJECT(self->text_format_down), "clicked",
341 G_CALLBACK(_select_font_format), self);
342 g_signal_connect(G_OBJECT(self->unbutton_size), "value-changed",
343 G_CALLBACK(_unbutton_size_changed), self);
344 g_signal_connect(G_OBJECT(self->button_size), "value-changed",
345 G_CALLBACK(_button_size_changed), self);
346 gtk_widget_show_all(GTK_WIDGET(self));
348 IRRECO_RETURN
350 static void
351 irreco_button_creator_dlg_init (IrrecoButtonCreatorDlg *object)
353 IRRECO_ENTER
354 IRRECO_RETURN
357 static void
358 irreco_button_creator_dlg_finalize (GObject *object)
360 /* TODO: Add deinitalization code here */
362 IrrecoButtonCreatorDlg *self;
363 IRRECO_ENTER
365 self = IRRECO_BUTTON_CREATOR_DLG(object);
367 G_OBJECT_CLASS(irreco_button_creator_dlg_parent_class)->finalize(object);
368 IRRECO_RETURN
372 static void
373 irreco_button_creator_dlg_class_init (IrrecoButtonCreatorDlgClass *klass)
375 GObjectClass* object_class = G_OBJECT_CLASS (klass);
377 object_class->finalize = irreco_button_creator_dlg_finalize;
378 object_class->constructed = irreco_button_creator_dlg_constructed;
381 GtkWidget
382 *irreco_button_creator_dlg_new(IrrecoData *irreco_data, GtkWindow *parent)
384 IrrecoButtonCreatorDlg *self;
386 IRRECO_ENTER
388 self = g_object_new(IRRECO_TYPE_BUTTON_CREATOR_DLG,
389 "irreco-data", irreco_data, NULL);
390 irreco_dlg_set_parent(IRRECO_DLG(self), parent);
392 IRRECO_RETURN_PTR(self);
395 /** @} */
397 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
398 /* Private Functions */
399 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
401 * @name Private Functions
402 * @{
406 *Fill combobox from file
408 static void fill_buttons_combobox(IrrecoButtonCreatorDlg *self)
411 GKeyFile *button_list;
412 gchar **keys;
413 gsize length;
414 gint i=0;
415 GError *error = NULL;
417 IRRECO_ENTER
418 button_list = g_key_file_new();
419 g_key_file_load_from_file(button_list, BUTTON_LIST_DIR,
420 G_KEY_FILE_NONE, &error);
422 keys = g_key_file_get_keys(button_list, "button-names", &length,
423 &error);
425 for(i=0; i<length; i++){
426 gtk_combo_box_append_text(GTK_COMBO_BOX(self->combobox_name),
427 g_key_file_get_string(button_list,
428 "button-names", keys[i], &error));
431 g_key_file_free(button_list);
432 if(keys != NULL) g_free(keys);
434 IRRECO_RETURN
437 void
438 _unbutton_size_changed(GtkSpinButton *spinbutton, IrrecoButtonCreatorDlg*self)
440 GString *image_path = g_string_new(self->unpressed_path->str);
442 IRRECO_ENTER
444 IRRECO_PRINTF("PATH: %s\n", self->unpressed_path->str);
446 _draw_preview_image(GTK_BUTTON(self->text_format_up), self,
447 image_path->str);
449 g_string_free(image_path, TRUE);
450 IRRECO_RETURN
452 void
453 _button_size_changed(GtkSpinButton *spinbutton, IrrecoButtonCreatorDlg*self)
455 GString *image_path = g_string_new(self->unpressed_path->str);
457 IRRECO_ENTER
459 IRRECO_PRINTF("PATH: %s\n", self->unpressed_path->str);
461 _draw_preview_image(GTK_BUTTON(self->text_format_down), self,
462 image_path->str);
464 g_string_free(image_path, TRUE);
465 IRRECO_RETURN
469 * Draw preview with current image.
471 static gboolean
472 _draw_preview_image(GtkButton *button, IrrecoButtonCreatorDlg *self,
473 const gchar *image)
475 GError *error = NULL;
476 GdkPixbuf *pixbuf = NULL;
477 GString *size = g_string_new(NULL);
479 IRRECO_ENTER
481 g_assert(self != NULL);
482 /* compared with whichever image */
483 if (g_str_equal("pressed", gtk_widget_get_name(GTK_WIDGET(button)))) {
484 gint button_width = 0;
485 gint button_height = 0;
486 GdkPixbuf *pixbuf_size = NULL;
488 if (image != NULL) {
489 pixbuf = gdk_pixbuf_new_from_file_at_scale(image,
490 IRRECO_BUTTON_PREVIEW_WIDHT,
491 IRRECO_BUTTON_PREVIEW_HEIGHT,
492 TRUE, &error);
493 /* set sensitive if image are selected */
494 gtk_widget_set_sensitive(self->add_button, TRUE);
496 if (irreco_gerror_check_print(&error)) {
497 IRRECO_RETURN_BOOL(FALSE);
501 g_string_printf(self->pressed_path, "%s", image);
502 gtk_widget_realize(GTK_WIDGET(self->preview_image_pressed));
503 gtk_image_set_from_pixbuf(GTK_IMAGE(self->preview_image_pressed),
504 GDK_PIXBUF(pixbuf));
506 /* Show image real size */
507 pixbuf_size = gdk_pixbuf_new_from_file(image, &error);
508 button_width = atoi(g_strdup_printf("%.0f",
509 (gdk_pixbuf_get_width(
510 pixbuf_size))/
511 (6-gtk_spin_button_get_value(GTK_SPIN_BUTTON(
512 self->button_size)))));
513 button_height = atoi(g_strdup_printf("%.0f",
514 (gdk_pixbuf_get_height(
515 pixbuf_size)/
516 (6-gtk_spin_button_get_value(GTK_SPIN_BUTTON(
517 self->button_size))))));
519 if (pixbuf_size != NULL) g_object_unref(G_OBJECT(pixbuf_size));
521 g_string_printf(size, "%sx%s", g_strdup_printf("%d", button_width),
522 g_strdup_printf("%d", button_height));
524 gtk_label_set_text(GTK_LABEL(self->label_pressed_size),
525 size->str);
527 } else {
528 gint button_width = 0;
529 gint button_height = 0;
530 GdkPixbuf *pixbuf_size = NULL;
532 if (image != NULL) {
533 pixbuf = gdk_pixbuf_new_from_file_at_scale(image,
534 IRRECO_BUTTON_PREVIEW_WIDHT,
535 IRRECO_BUTTON_PREVIEW_HEIGHT,
536 TRUE, &error);
537 if (irreco_gerror_check_print(&error)) {
538 IRRECO_RETURN_BOOL(FALSE);
541 gtk_widget_realize(GTK_WIDGET(self->preview_image_unpressed));
542 gtk_image_set_from_pixbuf(GTK_IMAGE(
543 self->preview_image_unpressed),
544 GDK_PIXBUF(pixbuf));
546 /* Show image real size */
547 pixbuf_size = gdk_pixbuf_new_from_file(image, &error);
548 button_width = atoi(g_strdup_printf("%.0f",
549 (gdk_pixbuf_get_width(
550 pixbuf_size))/
551 (6-gtk_spin_button_get_value(GTK_SPIN_BUTTON(
552 self->unbutton_size)))));
553 button_height = atoi(g_strdup_printf("%.0f",
554 (gdk_pixbuf_get_height(
555 pixbuf_size)/
556 (6-gtk_spin_button_get_value(GTK_SPIN_BUTTON(
557 self->unbutton_size))))));
558 if (pixbuf_size != NULL) g_object_unref(G_OBJECT(pixbuf_size));
560 g_string_printf(size, "%sx%s", g_strdup_printf("%d", button_width),
561 g_strdup_printf("%d", button_height));
563 gtk_label_set_text(GTK_LABEL(self->label_unpressed_size),
564 size->str);
565 g_string_printf(self->unpressed_path, "%s", image);
567 /* Set button sensitive */
568 gtk_widget_set_sensitive(self->add_button_pressed, TRUE);
569 gtk_widget_set_sensitive(self->event_box_pressed, TRUE);
572 g_string_free(size, TRUE);
573 if (pixbuf != NULL) g_object_unref(G_OBJECT(pixbuf));
574 IRRECO_RETURN_BOOL(TRUE);
578 static void _select_image(GtkButton *button, IrrecoButtonCreatorDlg *self)
581 GtkWidget *file_dlg = NULL;
582 gchar *image_dir = NULL;
583 IRRECO_ENTER
585 /* Create image select dialog. */
587 file_dlg = hildon_file_chooser_dialog_new(GTK_WINDOW(self),
588 GTK_FILE_CHOOSER_ACTION_OPEN);
589 gtk_window_set_title(GTK_WINDOW(file_dlg),_("Select button image"));
590 gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(file_dlg), TRUE);
591 image_dir = g_build_path("/", getenv("HOME"), "MyDocs/.images/", NULL);
592 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(file_dlg),
593 image_dir);
594 g_free(image_dir);
596 /* Loop until user cancels or we get a valid image. */
597 gtk_widget_show_all(GTK_WIDGET(file_dlg));
598 while (gtk_dialog_run(GTK_DIALOG(file_dlg)) == GTK_RESPONSE_OK) {
599 gchar *filename;
600 filename = gtk_file_chooser_get_filename(
601 GTK_FILE_CHOOSER(file_dlg));
603 /* Attempt to display the image. */
604 if (_draw_preview_image(button, self, filename)) {
605 irreco_gstring_set_and_free(self->filename, filename);
606 filename = NULL;
607 break;
609 /* Cleanup */
610 } else {
611 gchar *basename = g_path_get_basename(filename);
612 irreco_error_dlg_printf(GTK_WINDOW(file_dlg),
613 _("Cannot open image \"%s\""),
614 basename);
615 IRRECO_PRINTF("Image invalid.\n");
617 g_free(basename);
618 g_free(filename);
622 gtk_widget_destroy(file_dlg);
623 IRRECO_RETURN
627 /** @} */
629 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
630 /* Functions */
631 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
633 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
634 /* Public Functions */
635 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
637 gboolean irreco_button_creator_dlg_run(IrrecoData *irreco_data,
638 IrrecoTheme * irreco_theme,
639 GtkWindow *parent_window,
640 IrrecoThemeButton *button)
642 IrrecoButtonCreatorDlg *self;
643 gint response;
644 gboolean loop = TRUE;
645 gboolean rvalue = FALSE;
646 IRRECO_ENTER
648 self = (IrrecoButtonCreatorDlg*)irreco_button_creator_dlg_new(
649 irreco_data, parent_window);
650 self->irreco_data = irreco_data;
651 self->theme = irreco_theme;
653 IRRECO_PRINTF("Button: %s\n", button->name->str);
655 /*if(irreco_theme != NULL) {*/
656 if (g_utf8_strlen(button->name->str, 1) >0) {
657 /* Sets the button details */
658 _set_button_details(self, irreco_theme, button);
661 do {
662 response = gtk_dialog_run(GTK_DIALOG(self));
663 switch (response) {
664 case GTK_RESPONSE_OK:
668 irreco_theme_button_set(button,
669 gtk_combo_box_get_active_text(
670 GTK_COMBO_BOX(
671 self->combobox_name)),
672 gtk_combo_box_get_active_text(
673 GTK_COMBO_BOX(
674 self->combobox_name)),
675 gtk_toggle_button_get_active(
676 GTK_TOGGLE_BUTTON(
677 self->allow_text)),
678 self->unpressed_path->str,
679 self->pressed_path->str,
680 self->unpressed_format,
681 self->pressed_format,
682 gtk_spin_button_get_value_as_int(
683 GTK_SPIN_BUTTON(
684 self->text_padding)),
685 gtk_spin_button_get_value(
686 GTK_SPIN_BUTTON(
687 self->text_h_align)),
688 gtk_spin_button_get_value(
689 GTK_SPIN_BUTTON(
690 self->text_v_align)));
691 irreco_theme_button_print(button);
692 rvalue = TRUE;
693 loop = FALSE;
694 break;
696 case GTK_RESPONSE_CANCEL:
697 IRRECO_DEBUG("GTK_RESPONSE_CANCEL\n");
698 rvalue = FALSE;
699 loop = FALSE;
700 break;
702 default:
703 IRRECO_DEBUG("default\n");
704 break;
707 } while (loop);
709 gtk_widget_destroy(GTK_WIDGET(self));
710 IRRECO_RETURN_BOOL(rvalue);
714 void
715 _set_button_details(IrrecoButtonCreatorDlg *self, IrrecoTheme *irreco_theme,
716 IrrecoThemeButton *button)
718 IRRECO_ENTER
720 /* Set button information on table */
721 gtk_combo_box_prepend_text(GTK_COMBO_BOX(self->combobox_name),
722 button->name->str);
723 gtk_combo_box_set_active(GTK_COMBO_BOX(self->combobox_name),
725 _draw_preview_image(GTK_BUTTON(self->add_button_unpressed),
726 self,
727 button->image_up->str);
728 _draw_preview_image(GTK_BUTTON(self->add_button_pressed),
729 self,
730 button->image_down->str);
731 if (button->allow_text) {
732 gtk_button_clicked(GTK_BUTTON(self->allow_text));
735 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_padding),
736 button->text_padding);
738 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_h_align),
739 button->text_h_align);
740 gtk_spin_button_set_value(GTK_SPIN_BUTTON(self->text_v_align),
741 button->text_v_align);
742 /*set text format */
743 if (strlen(button->text_format_up->str) > 0) {
745 _set_font_format(self, button->text_format_up->str,
746 gtk_label_get_text(GTK_LABEL(
747 self->label_text_format_up)));
749 if (strlen(button->text_format_down->str) > 0) {
751 _set_font_format(self, button->text_format_down->str,
752 gtk_label_get_text(GTK_LABEL(
753 self->label_text_format_down)));
755 /* Set button label & windown title */
756 gtk_button_set_label(GTK_BUTTON(self->add_button), "Save");
757 gtk_window_set_title(GTK_WINDOW(self), _("Button Editor "));
760 IRRECO_RETURN
762 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
763 /* Events and Callbacks */
764 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
767 * @name Events and Callbacks
768 * @{
771 static void
772 _signal_image_clicked(GtkButton *button, IrrecoButtonCreatorDlg *self)
774 IRRECO_ENTER
775 _select_image(button, self);
776 IRRECO_RETURN
779 static void _unpressed_event_signal_image_clicked(GtkWidget *widget,
780 GdkEventButton *event,
781 IrrecoButtonCreatorDlg *self)
783 IRRECO_ENTER
785 gtk_button_clicked(GTK_BUTTON(self->add_button_unpressed));
787 IRRECO_RETURN
790 static void _pressed_event_signal_image_clicked(GtkWidget *widget,
791 GdkEventButton *event,
792 IrrecoButtonCreatorDlg *self)
794 IRRECO_ENTER
796 gtk_button_clicked(GTK_BUTTON(self->add_button_pressed));
798 IRRECO_RETURN
802 static void _toggle_button_toggled(GtkToggleButton *togglebutton,
803 IrrecoButtonCreatorDlg *self)
805 IRRECO_ENTER
807 if (gtk_toggle_button_get_active(togglebutton)) {
808 gtk_button_set_label(GTK_BUTTON(self->allow_text), "YES");
809 gtk_widget_set_sensitive(self->text_format_up, TRUE);
810 gtk_widget_set_sensitive(self->text_format_down, TRUE);
811 } else {
812 gtk_button_set_label(GTK_BUTTON(self->allow_text), "NO");
813 gtk_widget_set_sensitive(self->text_format_up, FALSE);
814 gtk_widget_set_sensitive(self->text_format_down, FALSE);
816 IRRECO_RETURN
820 *Select button font format
823 static void _select_font_format(GtkButton *button, IrrecoButtonCreatorDlg *self)
825 GtkWidget *dialog;
826 const gchar *font;
827 GString *format = g_string_new(NULL);
828 const gchar *span = ">%s</span>";
831 IRRECO_ENTER
832 dialog = gtk_font_selection_dialog_new("Button font");
834 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
836 font = gtk_font_selection_dialog_get_font_name(
837 GTK_FONT_SELECTION_DIALOG(dialog));
839 /*g_string_printf(format,
840 "<span foreground=\"%s\" font_desc=\"%s\" %s",
841 "red", font, span);*/
842 g_string_printf(format,
843 "<span font_desc=\"%s\" %s", font, span);
846 if (strcmp("unpressed", gtk_widget_get_name(
847 GTK_WIDGET(button))) == 0) {
848 self->unpressed_format = format->str;
849 _set_font_format(self, format->str,
850 gtk_label_get_text(GTK_LABEL(
851 self->label_text_format_up)));
852 } else {
853 self->pressed_format = format->str;
854 _set_font_format(self, format->str,
855 gtk_label_get_text(GTK_LABEL(
856 self->label_text_format_down)));
859 gtk_widget_destroy(GTK_WIDGET(dialog));
861 IRRECO_RETURN
864 # if 0
865 static void _select_font_format(GtkButton *button, IrrecoButtonCreatorDlg *self)
868 HildonFontSelectionDialog *dialog = NULL;
869 PangoFontDescription *font = NULL;
870 gint size;
871 gboolean bold, italic;
872 gchar *family = NULL;
873 gchar *text = NULL;
874 const gchar *span = ">%s</span>";
876 PangoColor *color;
877 GdkColor *color2;
878 GString *format = g_string_new(NULL);
879 gchar *markup;
880 gchar *value;
881 gchar *color_string;
882 IRRECO_ENTER
883 font = pango_font_description_new();
885 /* create dialog */
887 dialog = HILDON_FONT_SELECTION_DIALOG(hildon_font_selection_dialog_new(
888 NULL, NULL));
890 gtk_widget_show_all (GTK_WIDGET(dialog));
891 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_OK) {
892 g_object_get(G_OBJECT(dialog),
893 "family", &family,
894 "size", &size,
895 "bold", &bold,
896 "italic", &italic,
897 "color", &color,
898 "color", &color2,
899 NULL);
900 color_string[0] = '#';
901 sprintf(&color_string[1], "%.2X", color->red /256);
902 sprintf(&color_string[3], "%.2X", color->green/256);
903 sprintf(&color_string[5], "%.2X", color->blue /256);
906 pango_font_description_set_family(font, family);
907 pango_font_description_set_size(font, size * PANGO_SCALE);
908 if (bold) {
909 pango_font_description_set_weight(font, PANGO_WEIGHT_BOLD);
910 } else {
911 pango_font_description_set_weight(font, PANGO_WEIGHT_NORMAL);
913 if (italic) {
914 pango_font_description_set_style(font, PANGO_STYLE_ITALIC);
915 } else {
916 pango_font_description_set_style(font, PANGO_STYLE_NORMAL);
918 IRRECO_PRINTF("PANGO: %s\n", pango_font_description_to_string(font));
920 text = pango_color_to_string(color);
921 IRRECO_PRINTF("FONT: %s\n", text);
923 g_string_printf(format,
924 "<span foreground=\"%s\" font_desc=\"%s %d\" %s",
925 color_string, family, size, span);
927 if (bold) {
928 g_string_printf(format,
929 "<span foreground=\"%s\" font_desc=\"%s %s %d\" %s",
930 text, family, "Bold", size, span);
931 if (italic) {
932 g_string_printf(format,
933 "<span foreground=\"%s\" font_desc=\"%s %s %s %d\" %s",
934 text, family, "Bold", "Italic", size, span);
937 if (italic && !(bold)) {
938 g_string_printf(format,
939 "<span foreground=\"%s\" font_desc=\"%s %s %d\" %s",
940 text, family, "Italic", size, span);
943 markup = g_markup_printf_escaped(format->str, "text-format-up:");
944 IRRECO_PRINTF("FONT: %s\n", markup);
946 gtk_label_set_markup(GTK_LABEL(self->label_text_format_up), markup);
948 value = g_strdup_printf ("%u,%u,%u",
949 color->red,
950 color->green,
951 color->blue);
954 IRRECO_PRINTF("FONT: %s\n", color_string);
956 IRRECO_PRINTF("FONT: %s\n", value);
961 if(markup != NULL) g_free(markup);
962 if(text != NULL) g_free(text);
963 if(family != NULL) g_free(family);
964 g_string_free(format, TRUE);
966 pango_color_free(color);
968 gtk_widget_destroy(GTK_WIDGET(dialog));
970 IRRECO_RETURN
972 #endif
974 * Set button text format
976 static void _set_font_format(IrrecoButtonCreatorDlg *self, const gchar *button_format,
977 const gchar *text)
981 gchar *markup;
982 GString *format = g_string_new(NULL);
984 IRRECO_ENTER
986 g_string_printf(format, "%s", button_format);
988 markup = g_markup_printf_escaped(format->str, text);
990 IRRECO_PRINTF("TEXT: %s\n", markup);
992 if (strcmp(text, gtk_label_get_text(GTK_LABEL(
993 self->label_text_format_up))) == 0) {
994 gtk_label_set_markup(GTK_LABEL(self->label_text_format_up),
995 markup);
996 } else {
998 gtk_label_set_markup(GTK_LABEL(self->label_text_format_down),
999 markup);
1002 if(markup != NULL) g_free(markup);
1003 g_string_free(format, TRUE);
1005 IRRECO_RETURN
1009 /** @} */