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_cmd_chain_editor.h"
21 #include "irreco_cmd_chain_setup_dlg.h"
22 #include "irreco_cmd_dlg.h"
25 * @addtogroup IrrecoCmdChainEditor
28 * User interface for editing IrrecoCmdChains.
35 * Source file of @ref IrrecoCmdChainEditor.
40 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
42 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
43 static void irreco_cmd_chain_editor_set_sensitivity(IrrecoCmdChainEditor
*self
);
44 static void irreco_cmd_chain_editor_clear(IrrecoCmdChainEditor
*self
);
45 void irreco_cmd_chain_editor_add_clicked(GtkButton
*button
,
46 IrrecoCmdChainEditor
*self
);
47 void irreco_cmd_chain_editor_remove_clicked(GtkButton
*button
,
48 IrrecoCmdChainEditor
*self
);
49 void irreco_cmd_chain_editor_up_clicked(GtkButton
*button
,
50 IrrecoCmdChainEditor
*self
);
51 void irreco_cmd_chain_editor_down_clicked(GtkButton
*button
,
52 IrrecoCmdChainEditor
*self
);
53 void irreco_cmd_chain_editor_properties_clicked(GtkButton
*button
,
54 IrrecoCmdChainEditor
*self
);
55 void irreco_cmd_chain_editor_selection_changed(GtkTreeSelection
* selection
,
56 IrrecoCmdChainEditor
*self
);
60 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
61 /* Construction & Destruction */
62 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
65 * @name Construction & Destruction
69 G_DEFINE_TYPE(IrrecoCmdChainEditor
, irreco_cmd_chain_editor
,
70 IRRECO_TYPE_INTERNAL_WIDGET
)
72 static void irreco_cmd_chain_editor_finalize(GObject
*object
)
75 G_OBJECT_CLASS(irreco_cmd_chain_editor_parent_class
)->finalize(object
);
76 irreco_cmd_chain_editor_clear(IRRECO_CMD_CHAIN_EDITOR(object
));
80 static void irreco_cmd_chain_editor_class_init(IrrecoCmdChainEditorClass
*klass
)
82 GObjectClass
*object_class
;
85 object_class
= G_OBJECT_CLASS (klass
);
86 object_class
->finalize
= irreco_cmd_chain_editor_finalize
;
90 static void irreco_cmd_chain_editor_init(IrrecoCmdChainEditor
*self
)
96 gtk_box_set_spacing(GTK_BOX(self
), 1);
98 self
->edit_mode
= IRRECO_INDIRECT_EDITING
;
99 self
->old_cmd_chain
= NULL
;
100 self
->new_cmd_chain
= NULL
;
102 self
->listbox
= irreco_listbox_text_new();
103 self
->hbox
= gtk_hbox_new(0, 0);
105 gtk_box_pack_start(GTK_BOX(self
), self
->listbox
, 1, 1, 1);
106 gtk_box_pack_start(GTK_BOX(self
), self
->hbox
, 0, 0, 0);
108 self
->add
= gtk_button_new_from_stock(GTK_STOCK_ADD
);
109 self
->remove
= gtk_button_new_from_stock(GTK_STOCK_REMOVE
);
110 self
->up
= gtk_button_new_from_stock(GTK_STOCK_GO_UP
);
111 self
->down
= gtk_button_new_from_stock(GTK_STOCK_GO_DOWN
);
112 self
->properties
= gtk_button_new();
114 /* Set properties icon. We use just the icon to save space. */
115 icon
= gtk_widget_render_icon(self
->properties
, GTK_STOCK_PROPERTIES
,
116 GTK_ICON_SIZE_BUTTON
, NULL
);
117 image
= gtk_image_new_from_pixbuf(icon
);
118 gtk_button_set_image(GTK_BUTTON(self
->properties
), image
);
120 gtk_box_pack_start_defaults(GTK_BOX(self
->hbox
), self
->add
);
121 gtk_box_pack_start_defaults(GTK_BOX(self
->hbox
), self
->remove
);
122 gtk_box_pack_start_defaults(GTK_BOX(self
->hbox
), self
->up
);
123 gtk_box_pack_start_defaults(GTK_BOX(self
->hbox
), self
->down
);
124 gtk_box_pack_start_defaults(GTK_BOX(self
->hbox
), self
->properties
);
126 g_signal_connect(G_OBJECT(self
->add
), "clicked",
127 G_CALLBACK(irreco_cmd_chain_editor_add_clicked
),
129 g_signal_connect(G_OBJECT(self
->remove
), "clicked",
130 G_CALLBACK(irreco_cmd_chain_editor_remove_clicked
),
132 g_signal_connect(G_OBJECT(self
->up
), "clicked",
133 G_CALLBACK(irreco_cmd_chain_editor_up_clicked
),
135 g_signal_connect(G_OBJECT(self
->down
), "clicked",
136 G_CALLBACK(irreco_cmd_chain_editor_down_clicked
),
138 g_signal_connect(G_OBJECT(self
->properties
), "clicked",
139 G_CALLBACK(irreco_cmd_chain_editor_properties_clicked
),
141 g_signal_connect(G_OBJECT(IRRECO_LISTBOX(
142 self
->listbox
)->tree_selection
),
144 G_CALLBACK(irreco_cmd_chain_editor_selection_changed
),
147 irreco_cmd_chain_editor_set_sensitivity(self
);
152 GtkWidget
* irreco_cmd_chain_editor_new(IrrecoData
*irreco_data
,
155 IrrecoCmdChainEditor
*self
;
158 self
= g_object_new(IRRECO_TYPE_CMD_CHAIN_EDITOR
,
159 "irreco-data", irreco_data
,
161 irreco_cmd_chain_editor_set_parent_window(self
, parent
);
162 IRRECO_RETURN_PTR(GTK_WIDGET(self
));
169 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
170 /* Private Functions */
171 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
174 * @name Private Functions
178 static void irreco_cmd_chain_editor_clear(IrrecoCmdChainEditor
*self
)
181 irreco_cmd_chain_destroy(self
->new_cmd_chain
);
182 self
->old_cmd_chain
= NULL
;
183 self
->new_cmd_chain
= NULL
;
188 * Get the command chain which we should be editing.
190 static IrrecoCmdChain
*
191 irreco_cmd_chain_editor_get_edit_chain(IrrecoCmdChainEditor
*self
)
194 switch (self
->edit_mode
) {
195 case IRRECO_INDIRECT_EDITING
:
196 g_assert(self
->new_cmd_chain
);
197 IRRECO_RETURN_PTR(self
->new_cmd_chain
);
199 case IRRECO_DIRECT_EDITING
:
200 g_assert(self
->old_cmd_chain
);
201 IRRECO_RETURN_PTR(self
->old_cmd_chain
);
204 /* Invalid enum value. */
205 g_assert_not_reached();
208 static void irreco_cmd_chain_editor_populate_listbox(IrrecoCmdChainEditor
*self
)
210 IrrecoCmdChain
*edit_chain
;
213 edit_chain
= irreco_cmd_chain_editor_get_edit_chain(self
);
214 irreco_listbox_clear(IRRECO_LISTBOX(self
->listbox
));
215 IRRECO_CMD_CHAIN_FOREACH(edit_chain
, command
)
216 irreco_listbox_text_append(IRRECO_LISTBOX_TEXT(self
->listbox
),
217 irreco_cmd_get_long_name(command
),
219 IRRECO_CMD_CHAIN_FOREACH_END
220 irreco_listbox_set_selection(IRRECO_LISTBOX(self
->listbox
), 0);
224 static void irreco_cmd_chain_editor_set_sensitivity(IrrecoCmdChainEditor
*self
)
229 sel_index
= irreco_listbox_get_selection_index(
230 IRRECO_LISTBOX(self
->listbox
));
232 if (sel_index
== -1) {
233 gtk_widget_set_sensitive(self
->remove
, FALSE
);
234 gtk_widget_set_sensitive(self
->up
, FALSE
);
235 gtk_widget_set_sensitive(self
->down
, FALSE
);
236 } else if (IRRECO_LISTBOX(self
->listbox
)->list_store
->length
== 1) {
237 gtk_widget_set_sensitive(self
->remove
, TRUE
);
238 gtk_widget_set_sensitive(self
->up
, FALSE
);
239 gtk_widget_set_sensitive(self
->down
, FALSE
);
240 } else if (sel_index
== 0) {
241 gtk_widget_set_sensitive(self
->remove
, TRUE
);
242 gtk_widget_set_sensitive(self
->up
, FALSE
);
243 gtk_widget_set_sensitive(self
->down
, TRUE
);
244 } else if (sel_index
==
245 (IRRECO_LISTBOX(self
->listbox
)->list_store
->length
- 1)) {
246 gtk_widget_set_sensitive(self
->remove
, TRUE
);
247 gtk_widget_set_sensitive(self
->up
, TRUE
);
248 gtk_widget_set_sensitive(self
->down
, FALSE
);
250 gtk_widget_set_sensitive(self
->remove
, TRUE
);
251 gtk_widget_set_sensitive(self
->up
, TRUE
);
252 gtk_widget_set_sensitive(self
->down
, TRUE
);
261 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
262 /* Public Functions */
263 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
266 * @name Public Functions
271 * Set parent window for popup dialogs.
273 void irreco_cmd_chain_editor_set_parent_window(IrrecoCmdChainEditor
*self
,
277 self
->parent_window
= parent
;
281 void irreco_cmd_chain_editor_set_edit_mode(IrrecoCmdChainEditor
*self
,
282 IrrecoEditMode edit_mode
)
286 case IRRECO_INDIRECT_EDITING
:
287 case IRRECO_DIRECT_EDITING
:
288 self
->edit_mode
= edit_mode
;
292 IRRECO_ERROR("Invalid edit mode \"%i\".\n", edit_mode
);
298 void irreco_cmd_chain_editor_set_chain(IrrecoCmdChainEditor
*self
,
299 IrrecoCmdChain
*cmd_chain
)
303 g_assert(cmd_chain
!= NULL
);
304 irreco_cmd_chain_editor_clear(self
);
306 if (self
->edit_mode
== IRRECO_INDIRECT_EDITING
) {
307 self
->old_cmd_chain
= cmd_chain
;
308 self
->new_cmd_chain
= irreco_cmd_chain_create();
309 irreco_cmd_chain_copy(cmd_chain
, self
->new_cmd_chain
);
311 self
->old_cmd_chain
= cmd_chain
;
314 irreco_cmd_chain_editor_populate_listbox(self
);
318 IrrecoCmdChain
*irreco_cmd_chain_editor_get_chain(IrrecoCmdChainEditor
*self
)
321 IRRECO_RETURN_PTR(self
->old_cmd_chain
);
324 void irreco_cmd_chain_editor_set_autosize(IrrecoCmdChainEditor
*self
,
332 gtk_widget_get_size_request(self
->hbox
, &width
, &height
);
333 if (max_width
< width
) max_width
= width
;
334 irreco_listbox_set_autosize(IRRECO_LISTBOX(self
->listbox
),
335 min_width
, max_width
,
336 min_height
, max_height
- height
);
340 void irreco_cmd_chain_editor_apply_changes(IrrecoCmdChainEditor
*self
)
344 g_assert(self
->edit_mode
== IRRECO_INDIRECT_EDITING
);
346 if (self
->old_cmd_chain
!= NULL
&& self
->new_cmd_chain
!= NULL
) {
347 IRRECO_DEBUG("Saving command chain changes.\n");
348 irreco_cmd_chain_print(self
->new_cmd_chain
);
349 irreco_cmd_chain_copy(self
->new_cmd_chain
, self
->old_cmd_chain
);
354 void irreco_cmd_chain_editor_reset_changes(IrrecoCmdChainEditor
*self
)
358 g_assert(self
->edit_mode
== IRRECO_INDIRECT_EDITING
);
360 if (self
->old_cmd_chain
!= NULL
&& self
->new_cmd_chain
!= NULL
) {
361 irreco_cmd_chain_copy(self
->old_cmd_chain
, self
->new_cmd_chain
);
366 void irreco_cmd_chain_editor_clear_chain(IrrecoCmdChainEditor
*self
)
369 irreco_cmd_chain_remove_all(self
->new_cmd_chain
);
370 irreco_cmd_chain_editor_populate_listbox(self
);
378 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
379 /* Signals, Events and Callbacks */
380 /*=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-*/
383 * @name Events and Callbacks
387 void irreco_cmd_chain_editor_add_clicked(GtkButton
*button
,
388 IrrecoCmdChainEditor
*self
)
390 IrrecoCmdChain
*edit_chain
;
394 command
= irreco_cmd_create();
395 edit_chain
= irreco_cmd_chain_editor_get_edit_chain(self
);
397 if (irreco_cmd_dlg_run(irreco_internal_widget_get_irreco_data(
398 IRRECO_INTERNAL_WIDGET(self
)), command
, self
->parent_window
)) {
400 irreco_cmd_chain_append(edit_chain
, command
);
401 irreco_listbox_text_append(IRRECO_LISTBOX_TEXT(self
->listbox
),
402 irreco_cmd_get_long_name(command
),
404 gtk_widget_grab_focus(IRRECO_LISTBOX(self
->listbox
)->tree_view
);
406 irreco_cmd_destroy(command
);
409 irreco_cmd_chain_editor_set_sensitivity(self
);
413 void irreco_cmd_chain_editor_remove_clicked(GtkButton
*button
,
414 IrrecoCmdChainEditor
*self
)
417 IrrecoCmdChain
*edit_chain
;
420 sel_index
= irreco_listbox_get_selection_index(
421 IRRECO_LISTBOX(self
->listbox
));
422 edit_chain
= irreco_cmd_chain_editor_get_edit_chain(self
);
423 irreco_cmd_chain_remove(edit_chain
, sel_index
);
424 irreco_listbox_remove_selected(IRRECO_LISTBOX(self
->listbox
));
425 irreco_cmd_chain_editor_set_sensitivity(self
);
426 gtk_widget_grab_focus(IRRECO_LISTBOX(self
->listbox
)->tree_view
);
430 void irreco_cmd_chain_editor_up_clicked(GtkButton
*button
,
431 IrrecoCmdChainEditor
*self
)
434 IrrecoCmdChain
*edit_chain
;
437 sel_index
= irreco_listbox_get_selection_index(
438 IRRECO_LISTBOX(self
->listbox
));
439 edit_chain
= irreco_cmd_chain_editor_get_edit_chain(self
);
440 irreco_cmd_chain_move(edit_chain
, sel_index
, sel_index
- 1);
441 irreco_listbox_move_selected_up(IRRECO_LISTBOX(self
->listbox
));
442 irreco_cmd_chain_editor_set_sensitivity(self
);
443 gtk_widget_grab_focus(IRRECO_LISTBOX(self
->listbox
)->tree_view
);
447 void irreco_cmd_chain_editor_down_clicked(GtkButton
*button
,
448 IrrecoCmdChainEditor
*self
)
451 IrrecoCmdChain
*edit_chain
;
454 sel_index
= irreco_listbox_get_selection_index(
455 IRRECO_LISTBOX(self
->listbox
));
456 edit_chain
= irreco_cmd_chain_editor_get_edit_chain(self
);
457 irreco_cmd_chain_move(edit_chain
, sel_index
, sel_index
+ 1);
458 irreco_listbox_move_selected_down(IRRECO_LISTBOX(self
->listbox
));
459 irreco_cmd_chain_editor_set_sensitivity(self
);
460 gtk_widget_grab_focus(IRRECO_LISTBOX(self
->listbox
)->tree_view
);
464 void irreco_cmd_chain_editor_properties_clicked(GtkButton
*button
,
465 IrrecoCmdChainEditor
*self
)
468 IrrecoCmdChain
*edit_chain
;
471 edit_chain
= irreco_cmd_chain_editor_get_edit_chain(self
);
472 dlg
= irreco_cmd_chain_setup_dlg_new(self
->parent_window
, edit_chain
);
473 gtk_dialog_run(GTK_DIALOG(dlg
));
474 gtk_widget_destroy(dlg
);
478 void irreco_cmd_chain_editor_selection_changed(GtkTreeSelection
*selection
,
479 IrrecoCmdChainEditor
*self
)
482 irreco_cmd_chain_editor_set_sensitivity(self
);