]> git.sur5r.net Git - glabels/blob - src/object-editor-data-page.c
Upload to unstable
[glabels] / src / object-editor-data-page.c
1 /*
2  *  object-editor-data-page.c
3  *  Copyright (C) 2003-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include "object-editor.h"
24
25 #include <glib/gi18n.h>
26 #include <gtk/gtk.h>
27 #include <string.h>
28 #include <math.h>
29
30 #include "prefs.h"
31 #include "field-button.h"
32 #include "builder-util.h"
33
34 #include "object-editor-private.h"
35
36 #include "debug.h"
37
38
39 /*===========================================*/
40 /* Private macros                            */
41 /*===========================================*/
42
43
44 /*===========================================*/
45 /* Private data types                        */
46 /*===========================================*/
47
48
49 /*===========================================*/
50 /* Private globals                           */
51 /*===========================================*/
52
53
54 /*===========================================*/
55 /* Local function prototypes                 */
56 /*===========================================*/
57
58 static void data_radio_toggled_cb                (glObjectEditor        *editor);
59
60
61 /*--------------------------------------------------------------------------*/
62 /* PRIVATE.  Prepare data page.                                             */
63 /*--------------------------------------------------------------------------*/
64 void
65 gl_object_editor_prepare_data_page (glObjectEditor *editor)
66 {
67         gl_debug (DEBUG_EDITOR, "START");
68
69         /* Extract widgets from XML tree. */
70         gl_builder_util_get_widgets (editor->priv->builder,
71                                      "data_page_vbox",     &editor->priv->data_page_vbox,
72                                      "data_literal_radio", &editor->priv->data_literal_radio,
73                                      "data_key_radio",     &editor->priv->data_key_radio,
74                                      "data_text_entry",    &editor->priv->data_text_entry,
75                                      "data_key_hbox",      &editor->priv->data_key_hbox,
76                                      NULL);
77
78         editor->priv->data_key_combo = gl_field_button_new (NULL);
79         gtk_box_pack_start (GTK_BOX (editor->priv->data_key_hbox),
80                             editor->priv->data_key_combo,
81                             TRUE, TRUE, 0);
82
83         /* Un-hide */
84         gtk_widget_show_all (editor->priv->data_page_vbox);
85
86         /* Connect signals */
87         g_signal_connect_swapped (G_OBJECT (editor->priv->data_text_entry),
88                                   "changed",
89                                   G_CALLBACK (gl_object_editor_changed_cb),
90                                   G_OBJECT (editor));
91         g_signal_connect_swapped (G_OBJECT (editor->priv->data_key_combo),
92                                   "changed",
93                                   G_CALLBACK (gl_object_editor_changed_cb),
94                                   G_OBJECT (editor));
95         g_signal_connect_swapped (G_OBJECT (editor->priv->data_literal_radio),
96                                   "toggled",
97                                   G_CALLBACK (data_radio_toggled_cb),
98                                   G_OBJECT (editor));
99         g_signal_connect_swapped (G_OBJECT (editor->priv->data_key_radio),
100                                   "toggled",
101                                   G_CALLBACK (data_radio_toggled_cb),
102                                   G_OBJECT (editor));
103
104         gl_debug (DEBUG_EDITOR, "END");
105 }
106
107
108 /*--------------------------------------------------------------------------*/
109 /* PRIVATE.  data radio callback.                                           */
110 /*--------------------------------------------------------------------------*/
111 static void
112 data_radio_toggled_cb (glObjectEditor *editor)
113 {
114         gl_debug (DEBUG_WDGT, "START");
115  
116         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->data_literal_radio))) {
117                 gtk_widget_set_sensitive (editor->priv->data_text_entry, TRUE);
118                 gtk_widget_set_sensitive (editor->priv->data_key_combo, FALSE);
119                 gtk_widget_set_sensitive (editor->priv->data_format_label, FALSE);
120                 gtk_widget_set_sensitive (editor->priv->data_ex_label, FALSE);
121                 gtk_widget_set_sensitive (editor->priv->data_digits_label, FALSE);
122                 gtk_widget_set_sensitive (editor->priv->data_digits_spin, FALSE);
123         } else {
124                 gtk_widget_set_sensitive (editor->priv->data_text_entry, FALSE);
125                 gtk_widget_set_sensitive (editor->priv->data_key_combo, TRUE);
126                 gtk_widget_set_sensitive (editor->priv->data_format_label, TRUE);
127                 gtk_widget_set_sensitive (editor->priv->data_ex_label, TRUE);
128                 gtk_widget_set_sensitive (editor->priv->data_digits_label,
129                                           !editor->priv->data_format_fixed_flag);
130                 gtk_widget_set_sensitive (editor->priv->data_digits_spin,
131                                           !editor->priv->data_format_fixed_flag);
132         }
133  
134         gl_object_editor_changed_cb (editor);
135  
136         gl_debug (DEBUG_WDGT, "END");
137 }
138
139
140 /*****************************************************************************/
141 /* Set data.                                                                 */
142 /*****************************************************************************/
143 void
144 gl_object_editor_set_data (glObjectEditor      *editor,
145                             gboolean            merge_flag,
146                             glTextNode         *text_node)
147 {
148         gint pos;
149  
150         gl_debug (DEBUG_EDITOR, "START");
151
152  
153         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->data_literal_radio),
154                                          data_radio_toggled_cb, editor);
155         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->data_key_radio),
156                                          data_radio_toggled_cb, editor);
157         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->data_text_entry),
158                                          gl_object_editor_changed_cb, editor);
159         g_signal_handlers_block_by_func (G_OBJECT (editor->priv->data_key_combo),
160                                          gl_object_editor_changed_cb, editor);
161
162
163         gtk_widget_set_sensitive (editor->priv->data_key_radio, merge_flag);
164  
165         if (!text_node->field_flag || !merge_flag)
166         {
167                  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
168                                               (editor->priv->data_literal_radio), TRUE); 
169                 gtk_widget_set_sensitive (editor->priv->data_text_entry, TRUE);
170                 gtk_widget_set_sensitive (editor->priv->data_key_combo, FALSE);
171                 gtk_widget_set_sensitive (editor->priv->data_format_label, FALSE);
172                 gtk_widget_set_sensitive (editor->priv->data_ex_label, FALSE);
173                 gtk_widget_set_sensitive (editor->priv->data_digits_spin, FALSE);
174                 gtk_widget_set_sensitive (editor->priv->data_digits_label, FALSE);
175  
176                 gtk_editable_delete_text (GTK_EDITABLE (editor->priv->data_text_entry), 0, -1);
177                 pos = 0;
178                 if (text_node->data != NULL )
179                 {
180                         gtk_editable_insert_text (GTK_EDITABLE (editor->priv->data_text_entry),
181                                                   text_node->data,
182                                                   strlen (text_node->data),
183                                                   &pos);
184                 }
185         }
186         else
187         {
188                 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON
189                                               (editor->priv->data_key_radio), TRUE);
190                                                                                 
191                 gtk_widget_set_sensitive (editor->priv->data_text_entry, FALSE);
192                 gtk_widget_set_sensitive (editor->priv->data_key_combo, TRUE);
193                 gtk_widget_set_sensitive (editor->priv->data_format_label, TRUE);
194                 gtk_widget_set_sensitive (editor->priv->data_ex_label, TRUE);
195                 gtk_widget_set_sensitive (editor->priv->data_digits_label,
196                                           !editor->priv->data_format_fixed_flag);
197                 gtk_widget_set_sensitive (editor->priv->data_digits_spin,
198                                           !editor->priv->data_format_fixed_flag);
199                                                                                 
200
201                 gl_field_button_set_key (GL_FIELD_BUTTON (editor->priv->data_key_combo),
202                                          text_node->data);
203         }
204                                                                                 
205
206         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->data_literal_radio),
207                                            data_radio_toggled_cb, editor);
208         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->data_key_radio),
209                                            data_radio_toggled_cb, editor);
210         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->data_text_entry),
211                                            gl_object_editor_changed_cb, editor);
212         g_signal_handlers_unblock_by_func (G_OBJECT (editor->priv->data_key_combo),
213                                            gl_object_editor_changed_cb, editor);
214
215
216         gl_debug (DEBUG_EDITOR, "END");
217 }
218
219
220 /*****************************************************************************/
221 /* Query data.                                                               */
222 /*****************************************************************************/
223 glTextNode *
224 gl_object_editor_get_data (glObjectEditor      *editor)
225 {
226         glTextNode *text_node;
227  
228         gl_debug (DEBUG_EDITOR, "START");
229  
230         text_node = g_new0(glTextNode,1);
231  
232         if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (editor->priv->data_literal_radio)))
233         {
234                 text_node->field_flag = FALSE;
235                 text_node->data =
236                     gtk_editable_get_chars (GTK_EDITABLE (editor->priv->data_text_entry), 0, -1);
237         }
238         else
239         {
240                 text_node->field_flag = TRUE;
241                 text_node->data =
242                         gl_field_button_get_key (GL_FIELD_BUTTON (editor->priv->data_key_combo));
243         }
244  
245         gl_debug (DEBUG_EDITOR, "text_node: field_flag=%d, data=%s",
246                   text_node->field_flag, text_node->data);
247
248         gl_debug (DEBUG_EDITOR, "END");
249  
250         return text_node;
251 }
252
253
254
255 /*
256  * Local Variables:       -- emacs
257  * mode: C                -- emacs
258  * c-basic-offset: 8      -- emacs
259  * tab-width: 8           -- emacs
260  * indent-tabs-mode: nil  -- emacs
261  * End:                   -- emacs
262  */