Irreco for N900 (Maemo 5) update. Push for 0.8.* changes.
[irreco.git] / irreco / src / core / irreco_button_dlg.c
blob15ca5a2f0d99251fbf36e95cfec76e030faa4871
1 /*
2 * irreco - Ir Remote Control
3 * Copyright (C) 2007 Arto Karppinen (arto.karppinen@iki.fi)
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "irreco_button_dlg.h"
21 #include "irreco_button.h"
22 #include "irreco_style_browser_dlg.h"
23 #include "irreco_cmd_chain_editor.h"
25 /**
26 * @addtogroup IrrecoButtonDlg
27 * @ingroup Irreco
29 * @todo PURPOCE OF CLASS.
31 * @{
34 /**
35 * @file
36 * Source file of @ref IrrecoButtonDlg.
39 enum {
40 IRRECO_STYLE_DLG_SHOW = 1,
41 IRRECO_RESPONSE_CLEAR
46 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
47 /* Prototypes */
48 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
49 void irreco_button_dlg_create_window(IrrecoButtonDlg * button_dlg,
50 IrrecoWindow * parent,
51 const gchar * title);
52 void irreco_button_dlg_scrolled_size_request(GtkWidget * widget,
53 GtkRequisition * requisition,
54 gpointer user_data);
55 void irreco_button_dlg_size_request(GtkWidget *widget,
56 GtkRequisition *requisition,
57 gpointer user_data);
58 void irreco_button_dlg_style_click(IrrecoButton * irreco_button,
59 GtkWidget * dialog);
63 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
64 /* Button dialog structure management. */
65 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
67 /**
68 * @name Button dialog structure management
70 * @{
74 * IrrecoButtonDialog
76 * Allows user to change the style, command and other properties of a button.
78 IrrecoButtonDlg* irreco_button_dlg_create(IrrecoData * irreco_data)
80 IrrecoButtonDlg* button_dlg;
81 IRRECO_ENTER
83 button_dlg = g_slice_new0(IrrecoButtonDlg);
84 button_dlg->irreco_data = irreco_data;
85 button_dlg->accepted_cmd_chain = irreco_cmd_chain_create();
86 button_dlg->accepted_theme_name = g_string_new("");
87 IRRECO_RETURN_PTR(button_dlg);
90 void irreco_button_dlg_clear(IrrecoButtonDlg * button_dlg)
92 IRRECO_ENTER
94 button_dlg->new_style = NULL;
95 g_free(button_dlg->accepted_title);
96 button_dlg->accepted_title = NULL;
97 irreco_cmd_chain_remove_all(button_dlg->accepted_cmd_chain);
98 button_dlg->accepted_style = NULL;
100 IRRECO_RETURN
103 void irreco_button_dlg_destroy(IrrecoButtonDlg * button_dlg)
105 IRRECO_ENTER
106 irreco_button_dlg_clear(button_dlg);
107 irreco_cmd_chain_destroy(button_dlg->accepted_cmd_chain);
108 g_slice_free(IrrecoButtonDlg, button_dlg);
110 IRRECO_RETURN
113 void irreco_button_dlg_print_data(const gchar * title,
114 IrrecoCmdChain * cmd_chain,
115 IrrecoThemeButton * style)
117 IRRECO_ENTER
118 IRRECO_PRINTF("Title: \"%s\"\n", title);
120 if (style == NULL) {
121 IRRECO_PRINTF("Style is NULL\n");
122 } else {
123 IRRECO_PRINTF("Title: \"%s\"\n", style->name->str);
126 if (cmd_chain == NULL) {
127 IRRECO_PRINTF("Command chain is NULL\n");
128 } else {
129 IRRECO_PRINTF("Command chain:\n");
130 irreco_cmd_chain_print(cmd_chain);
133 IRRECO_RETURN
136 void irreco_button_dlg_set(IrrecoButtonDlg * button_dlg,
137 const gchar * title,
138 IrrecoCmdChain * cmd_chain,
139 IrrecoThemeButton * style)
141 IRRECO_ENTER
142 IRRECO_DEBUG("Setting button dialog:\n");
143 irreco_button_dlg_print_data(title, cmd_chain, style);
145 irreco_button_dlg_clear(button_dlg);
146 button_dlg->accepted_title = g_strdup(title);
147 if (cmd_chain != NULL) {
148 irreco_cmd_chain_copy(cmd_chain,
149 button_dlg->accepted_cmd_chain);
151 button_dlg->accepted_style = style;
152 IRRECO_RETURN
156 * Copy information from IrrecoButton into IrrecoButtonDlg.
158 void irreco_button_dlg_data_from_button(IrrecoButtonDlg * button_dlg,
159 IrrecoButton * irreco_button)
161 IRRECO_ENTER
162 irreco_button_dlg_set(button_dlg, irreco_button->title,
163 irreco_button_get_cmd_chain(irreco_button),
164 irreco_button->style);
166 IRRECO_RETURN
170 * Copy information from IrrecoButtonDlg to IrrecoButton.
172 void irreco_button_dlg_data_to_button(IrrecoButtonDlg * button_dlg,
173 IrrecoButton * irreco_button)
175 IRRECO_ENTER
176 irreco_button_set_title(irreco_button, button_dlg->accepted_title);
177 irreco_button_set_cmd_chain(irreco_button,
178 button_dlg->accepted_cmd_chain);
179 irreco_button_set_style(irreco_button, button_dlg->accepted_style);
180 IRRECO_RETURN
183 void irreco_button_dlg_print(IrrecoButtonDlg * button_dlg)
185 IrrecoCmdChain *new_cmd_chain = NULL;
186 IRRECO_ENTER
188 IRRECO_DEBUG("Accepted data:\n");
189 irreco_button_dlg_print_data(button_dlg->accepted_title,
190 button_dlg->accepted_cmd_chain,
191 button_dlg->accepted_style);
192 IRRECO_DEBUG("New data:\n");
194 IRRECO_DEBUG_LINE
195 IRRECO_DEBUG("%p\n", (void*) button_dlg->editor);
196 if (button_dlg->editor) {
197 IRRECO_DEBUG_LINE
198 new_cmd_chain = irreco_cmd_chain_editor_get_chain(
199 IRRECO_CMD_CHAIN_EDITOR(button_dlg->editor));
201 IRRECO_DEBUG_LINE
203 irreco_button_dlg_print_data(
204 gtk_entry_get_text(GTK_ENTRY(button_dlg->title_entry)),
205 new_cmd_chain, button_dlg->new_style);
207 IRRECO_RETURN
210 void irreco_button_dlg_update_dialog(IrrecoButtonDlg * button_dlg)
212 IrrecoTheme *accepted_theme = NULL;
213 const gchar *key;
214 IRRECO_ENTER
216 irreco_cmd_chain_editor_set_chain(IRRECO_CMD_CHAIN_EDITOR(
217 button_dlg->editor),
218 button_dlg->accepted_cmd_chain);
220 if (button_dlg->accepted_title != NULL) {
221 gtk_entry_set_text(GTK_ENTRY(button_dlg->title_entry),
222 button_dlg->accepted_title);
223 } else {
224 gtk_entry_set_text(GTK_ENTRY(button_dlg->title_entry), "");
227 IRRECO_DEBUG("ACCEPTED THEME NAME %s\n",
228 button_dlg->accepted_theme_name->str);
230 IRRECO_DEBUG("ACCEPTED BUTTON INDEX %d\n",
231 button_dlg->accepted_button_index);
232 IRRECO_LINE
233 if (button_dlg->accepted_theme_name->len == 0) {
234 IRRECO_RETURN
237 irreco_string_table_get(button_dlg->irreco_data->theme_manager->themes,
238 button_dlg->accepted_theme_name->str,
239 (gpointer *) &accepted_theme);
241 if (accepted_theme != NULL) {
242 irreco_string_table_index(accepted_theme->buttons,
243 button_dlg->accepted_button_index,
244 &key, (gpointer *)
245 &button_dlg->accepted_style);
246 gtk_entry_set_text(GTK_ENTRY(button_dlg->title_entry),
247 button_dlg->accepted_style->name->str);
248 } else {
249 button_dlg->accepted_style = NULL;
253 irreco_button_set_style(button_dlg->style_button,
254 button_dlg->accepted_style);
256 irreco_button_create_widget(button_dlg->style_button);
258 button_dlg->new_style = button_dlg->accepted_style;
260 IRRECO_RETURN
264 * Show button dialog.
266 * Returns TRUE if user clicks OK.
267 * Returns FALSE otherwise.
269 gboolean irreco_button_dlg_run(IrrecoButtonDlg * button_dlg,
270 IrrecoWindow * parent,
271 const gchar * title)
273 IrrecoData *irreco_data = button_dlg->irreco_data;
274 gint rvalue = -1;
275 IRRECO_ENTER
277 /* Prepare dialog. */
278 irreco_button_dlg_create_window(button_dlg, parent, title);
279 irreco_button_dlg_update_dialog(button_dlg);
281 /* Show dialog. */
282 gtk_widget_show_all(button_dlg->dialog);
284 if (button_dlg->accepted_style != NULL); {
285 irreco_button_set_style(button_dlg->style_button,
286 button_dlg->accepted_style);
287 button_dlg->new_style = button_dlg->accepted_style;
290 irreco_button_create_widget(button_dlg->style_button);
293 while (rvalue == -1) {
294 switch (gtk_dialog_run(GTK_DIALOG(button_dlg->dialog))) {
295 case GTK_RESPONSE_DELETE_EVENT:
296 rvalue = FALSE;
297 break;
299 case GTK_RESPONSE_ACCEPT: {
300 IrrecoTheme *theme = NULL;
302 g_free(button_dlg->accepted_title);
304 button_dlg->accepted_title =
305 g_strdup(gtk_entry_get_text(GTK_ENTRY(
306 button_dlg->title_entry)));
308 button_dlg->accepted_style = button_dlg->new_style;
310 irreco_cmd_chain_editor_apply_changes(
311 IRRECO_CMD_CHAIN_EDITOR(button_dlg->editor));
313 if (button_dlg->new_style == NULL) {
314 rvalue = TRUE;
315 break;
318 g_string_printf(button_dlg->accepted_theme_name, "%s",
319 button_dlg->new_style->theme_name->str);
321 irreco_string_table_get(
322 irreco_data->theme_manager->themes,
323 button_dlg->accepted_theme_name->str,
324 (gpointer *) &theme);
326 if (theme != NULL) {
328 irreco_string_table_get_index(theme->buttons,
329 button_dlg->new_style,
330 &button_dlg->accepted_button_index);
333 if(irreco_string_table_lenght(theme->buttons) ==
334 ++button_dlg->accepted_button_index) {
335 button_dlg->accepted_button_index = 0;
339 rvalue = TRUE;
341 break;
343 case IRRECO_STYLE_DLG_SHOW:
344 if (irreco_show_style_browser_dlg(irreco_data,
345 GTK_WINDOW(button_dlg->dialog),
346 &button_dlg->new_style)) {
347 irreco_button_set_style(
348 button_dlg->style_button,
349 button_dlg->new_style);
351 irreco_button_create_widget(
352 button_dlg->style_button);
354 /* Large buttons may blow up the dialog a bit,
355 so resize the dialog as small as possible. */
356 gtk_window_resize(GTK_WINDOW(button_dlg->dialog), 1, 1);
358 break;
360 case IRRECO_RESPONSE_CLEAR:
361 gtk_entry_set_text(GTK_ENTRY(
362 button_dlg->title_entry), "");
364 irreco_cmd_chain_editor_clear_chain(
365 IRRECO_CMD_CHAIN_EDITOR(button_dlg->editor));
367 break;
371 gtk_widget_destroy(button_dlg->dialog);
372 button_dlg->dialog = NULL;
373 irreco_button_layout_destroy(button_dlg->layout);
374 button_dlg->layout = NULL;
375 button_dlg->title_entry = NULL;
376 button_dlg->style_button = NULL;
377 button_dlg->editor = NULL;
378 IRRECO_RETURN_INT(rvalue);
381 /** @} */
383 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
384 /* Button dialog building. */
385 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
388 * @name Button dialog building
389 * @{
392 void irreco_button_dlg_create_window(IrrecoButtonDlg * button_dlg,
393 IrrecoWindow * parent,
394 const gchar * title)
396 GtkWidget *hbox;
397 GtkWidget *vbox_left;
398 GtkWidget *vbox_right;
400 GtkWidget *style_frame;
401 GtkWidget *style_scrolled;
402 GtkWidget *style_fixed;
404 GtkWidget *editor_frame;
405 IRRECO_ENTER
407 /* Create objects. */
408 button_dlg->dialog = gtk_dialog_new_with_buttons(
409 title, irreco_window_get_gtk_window(parent),
410 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT |
411 GTK_DIALOG_NO_SEPARATOR,
412 GTK_STOCK_CLEAR, IRRECO_RESPONSE_CLEAR,
413 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
414 NULL);
415 hbox = gtk_hbox_new(FALSE, 10);
416 vbox_left = gtk_vbox_new(FALSE, 0);
417 vbox_right = gtk_vbox_new(FALSE, 0);
419 button_dlg->title_entry = gtk_entry_new();
420 style_frame = gtk_frame_new(NULL);
421 style_scrolled = gtk_scrolled_window_new(NULL, NULL);
422 style_fixed = gtk_fixed_new();
423 button_dlg->layout = irreco_button_layout_create(style_fixed, NULL);
424 button_dlg->style_button = irreco_button_create(button_dlg->layout,
425 0, 0, _("Change"),
426 NULL, NULL);
428 /* Build dialog. */
429 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(button_dlg->dialog)->vbox),
430 irreco_gtk_align(hbox, 0, 0, 1, 1, 8, 8, 8, 8));
431 gtk_container_add(GTK_CONTAINER(hbox), vbox_left);
432 gtk_container_add(GTK_CONTAINER(hbox), vbox_right);
434 /* Connect signals. */
435 g_signal_connect(G_OBJECT(button_dlg->dialog), "size-request",
436 G_CALLBACK(irreco_button_dlg_size_request),
437 NULL);
438 g_signal_connect(G_OBJECT(style_scrolled), "size-request",
439 G_CALLBACK(irreco_button_dlg_scrolled_size_request),
440 NULL);
442 /* Build left side of dialog. */
443 gtk_box_pack_start(GTK_BOX(vbox_left),
444 irreco_gtk_label(_("Name"), 0.5, 1, 0, 0, 0, 0),
445 0, 0, 0);
446 gtk_box_pack_start(GTK_BOX(vbox_left),
447 button_dlg->title_entry,
448 0, 0, 0);
449 gtk_box_pack_start(GTK_BOX(vbox_left),
450 irreco_gtk_label(_("Style"), 0.5, 1, 10, 0, 0, 0),
451 0, 0, 0);
452 gtk_box_pack_start(GTK_BOX(vbox_left),
453 style_frame,
454 1, 1, 1);
455 gtk_container_add(GTK_CONTAINER(style_frame), style_scrolled);
456 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(style_scrolled),
457 GTK_POLICY_AUTOMATIC,
458 GTK_POLICY_AUTOMATIC);
459 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(
460 style_scrolled),
461 irreco_gtk_align(style_fixed,
462 0.5, 0.5, 0, 0, 8, 8, 8, 8));
464 /* Build right side of dialog. */
465 button_dlg->editor = irreco_cmd_chain_editor_new(
466 button_dlg->irreco_data, GTK_WINDOW(button_dlg->dialog));
467 irreco_cmd_chain_editor_set_chain(IRRECO_CMD_CHAIN_EDITOR(
468 button_dlg->editor), button_dlg->accepted_cmd_chain);
469 editor_frame = gtk_frame_new(NULL);
471 gtk_box_pack_start(GTK_BOX(vbox_right), irreco_gtk_label(
472 _("Command Chain"), 0.5, 1, 0, 0, 0, 0),
473 0, 0, 0);
474 gtk_container_add(GTK_CONTAINER(vbox_right), editor_frame);
475 gtk_container_add(GTK_CONTAINER(editor_frame), button_dlg->editor);
477 /* Connect callbacks. */
478 irreco_button_layout_set_press_callback(button_dlg->layout,
479 irreco_button_down);
480 irreco_button_layout_set_release_callback(button_dlg->layout,
481 irreco_button_dlg_style_click);
482 irreco_button_layout_set_callback_data(button_dlg->layout,
483 button_dlg->dialog);
485 IRRECO_RETURN
488 /** @} */
492 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
493 /* Signal callbacks. */
494 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
497 * @name Signal callbacks
498 * @{
502 * Adjust size GtkRequisition in a way that will hide the scrollbar if possible.
504 void irreco_button_dlg_scrolled_size_request(GtkWidget * widget,
505 GtkRequisition * requisition,
506 gpointer user_data)
508 GtkAdjustment *adjustment;
509 IRRECO_ENTER
511 adjustment = gtk_scrolled_window_get_vadjustment(
512 GTK_SCROLLED_WINDOW(widget));
513 requisition->height = adjustment->upper;
514 adjustment = gtk_scrolled_window_get_hadjustment(
515 GTK_SCROLLED_WINDOW(widget));
516 requisition->width = adjustment->upper;
518 IRRECO_DEBUG("value %f\n", adjustment->value);
519 IRRECO_DEBUG("lower %f\n", adjustment->lower);
520 IRRECO_DEBUG("upper %f\n", adjustment->upper);
521 IRRECO_DEBUG("step_increment %f\n", adjustment->step_increment);
522 IRRECO_DEBUG("page_increment %f\n", adjustment->page_increment);
523 IRRECO_DEBUG("page_size %f\n", adjustment->page_size);
526 IRRECO_RETURN
529 void irreco_button_dlg_size_request(GtkWidget *widget,
530 GtkRequisition *requisition,
531 gpointer user_data)
533 IRRECO_ENTER
534 requisition->height = 300;
535 IRRECO_RETURN
538 void irreco_button_dlg_style_click(IrrecoButton * irreco_button,
539 GtkWidget * dialog)
541 IRRECO_ENTER
542 gtk_dialog_response(GTK_DIALOG(dialog), IRRECO_STYLE_DLG_SHOW);
543 irreco_button_up(irreco_button);
544 IRRECO_RETURN
547 /** @} */
548 /** @} */