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"
26 * @addtogroup IrrecoButtonDlg
29 * @todo PURPOCE OF CLASS.
36 * Source file of @ref IrrecoButtonDlg.
40 IRRECO_STYLE_DLG_SHOW
= 1,
46 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
48 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
49 void irreco_button_dlg_create_window(IrrecoButtonDlg
* button_dlg
,
50 IrrecoWindow
* parent
,
52 void irreco_button_dlg_scrolled_size_request(GtkWidget
* widget
,
53 GtkRequisition
* requisition
,
55 void irreco_button_dlg_size_request(GtkWidget
*widget
,
56 GtkRequisition
*requisition
,
58 void irreco_button_dlg_style_click(IrrecoButton
* irreco_button
,
63 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
64 /* Button dialog structure management. */
65 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
68 * @name Button dialog structure management
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
;
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
)
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
;
103 void irreco_button_dlg_destroy(IrrecoButtonDlg
* button_dlg
)
106 irreco_button_dlg_clear(button_dlg
);
107 irreco_cmd_chain_destroy(button_dlg
->accepted_cmd_chain
);
108 g_slice_free(IrrecoButtonDlg
, button_dlg
);
113 void irreco_button_dlg_print_data(const gchar
* title
,
114 IrrecoCmdChain
* cmd_chain
,
115 IrrecoThemeButton
* style
)
118 IRRECO_PRINTF("Title: \"%s\"\n", title
);
121 IRRECO_PRINTF("Style is NULL\n");
123 IRRECO_PRINTF("Title: \"%s\"\n", style
->name
->str
);
126 if (cmd_chain
== NULL
) {
127 IRRECO_PRINTF("Command chain is NULL\n");
129 IRRECO_PRINTF("Command chain:\n");
130 irreco_cmd_chain_print(cmd_chain
);
136 void irreco_button_dlg_set(IrrecoButtonDlg
* button_dlg
,
138 IrrecoCmdChain
* cmd_chain
,
139 IrrecoThemeButton
* style
)
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
;
156 * Copy information from IrrecoButton into IrrecoButtonDlg.
158 void irreco_button_dlg_data_from_button(IrrecoButtonDlg
* button_dlg
,
159 IrrecoButton
* irreco_button
)
162 irreco_button_dlg_set(button_dlg
, irreco_button
->title
,
163 irreco_button_get_cmd_chain(irreco_button
),
164 irreco_button
->style
);
170 * Copy information from IrrecoButtonDlg to IrrecoButton.
172 void irreco_button_dlg_data_to_button(IrrecoButtonDlg
* button_dlg
,
173 IrrecoButton
* irreco_button
)
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
);
183 void irreco_button_dlg_print(IrrecoButtonDlg
* button_dlg
)
185 IrrecoCmdChain
*new_cmd_chain
= NULL
;
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");
195 IRRECO_DEBUG("%p\n", (void*) button_dlg
->editor
);
196 if (button_dlg
->editor
) {
198 new_cmd_chain
= irreco_cmd_chain_editor_get_chain(
199 IRRECO_CMD_CHAIN_EDITOR(button_dlg
->editor
));
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
);
210 void irreco_button_dlg_update_dialog(IrrecoButtonDlg
* button_dlg
)
212 IrrecoTheme
*accepted_theme
= NULL
;
216 irreco_cmd_chain_editor_set_chain(IRRECO_CMD_CHAIN_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
);
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
);
233 if (button_dlg
->accepted_theme_name
->len
== 0) {
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
,
245 &button_dlg
->accepted_style
);
246 gtk_entry_set_text(GTK_ENTRY(button_dlg
->title_entry
),
247 button_dlg
->accepted_style
->name
->str
);
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
;
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
,
273 IrrecoData
*irreco_data
= button_dlg
->irreco_data
;
277 /* Prepare dialog. */
278 irreco_button_dlg_create_window(button_dlg
, parent
, title
);
279 irreco_button_dlg_update_dialog(button_dlg
);
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
:
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
) {
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
);
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;
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);
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
));
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
);
383 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
384 /* Button dialog building. */
385 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
388 * @name Button dialog building
392 void irreco_button_dlg_create_window(IrrecoButtonDlg
* button_dlg
,
393 IrrecoWindow
* parent
,
397 GtkWidget
*vbox_left
;
398 GtkWidget
*vbox_right
;
400 GtkWidget
*style_frame
;
401 GtkWidget
*style_scrolled
;
402 GtkWidget
*style_fixed
;
404 GtkWidget
*editor_frame
;
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
,
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
,
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
),
438 g_signal_connect(G_OBJECT(style_scrolled
), "size-request",
439 G_CALLBACK(irreco_button_dlg_scrolled_size_request
),
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),
446 gtk_box_pack_start(GTK_BOX(vbox_left
),
447 button_dlg
->title_entry
,
449 gtk_box_pack_start(GTK_BOX(vbox_left
),
450 irreco_gtk_label(_("Style"), 0.5, 1, 10, 0, 0, 0),
452 gtk_box_pack_start(GTK_BOX(vbox_left
),
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(
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),
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
,
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
,
492 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
493 /* Signal callbacks. */
494 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
497 * @name Signal callbacks
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
,
508 GtkAdjustment
*adjustment
;
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);
529 void irreco_button_dlg_size_request(GtkWidget
*widget
,
530 GtkRequisition
*requisition
,
534 requisition
->height
= 300;
538 void irreco_button_dlg_style_click(IrrecoButton
* irreco_button
,
542 gtk_dialog_response(GTK_DIALOG(dialog
), IRRECO_STYLE_DLG_SHOW
);
543 irreco_button_up(irreco_button
);