]> git.sur5r.net Git - glabels/blob - src/object-editor-position-page.c
Theme friendly, glabels specific, action icons.
[glabels] / src / object-editor-position-page.c
1 /*
2  *  object-editor-position-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 <math.h>
28
29 #include "prefs.h"
30 #include "builder-util.h"
31 #include "units-util.h"
32
33 #include "object-editor-private.h"
34
35 #include "debug.h"
36
37
38 /*===========================================*/
39 /* Private macros                            */
40 /*===========================================*/
41
42
43 /*===========================================*/
44 /* Private data types                        */
45 /*===========================================*/
46
47
48 /*===========================================*/
49 /* Private globals                           */
50 /*===========================================*/
51
52
53 /*===========================================*/
54 /* Local function prototypes                 */
55 /*===========================================*/
56
57
58 /*--------------------------------------------------------------------------*/
59 /* PRIVATE.  Prepare position page.                                         */
60 /*--------------------------------------------------------------------------*/
61 void
62 gl_object_editor_prepare_position_page (glObjectEditor *editor)
63 {
64         lglUnits      units;
65         const gchar  *units_string;
66         gdouble       climb_rate;
67         gint          digits;
68
69         gl_debug (DEBUG_EDITOR, "START");
70
71         /* Extract widgets from XML tree. */
72         gl_builder_util_get_widgets (editor->priv->builder,
73                                      "pos_page_vbox",     &editor->priv->pos_page_vbox,
74                                      "pos_x_spin",        &editor->priv->pos_x_spin,
75                                      "pos_y_spin",        &editor->priv->pos_y_spin,
76                                      "pos_x_units_label", &editor->priv->pos_x_units_label,
77                                      "pos_y_units_label", &editor->priv->pos_y_units_label,
78                                      NULL);
79
80         /* Get configuration information */
81         units = gl_prefs_model_get_units (gl_prefs);
82         units_string = lgl_units_get_name (units);
83         editor->priv->units_per_point = lgl_units_get_units_per_point (units);
84         climb_rate = gl_units_util_get_step_size (units);
85         digits = gl_units_util_get_precision (units);
86
87         /* Modify widgets based on configuration */
88         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->pos_x_spin), digits);
89         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->pos_x_spin),
90                                         climb_rate, 10.0*climb_rate);
91         gtk_label_set_text (GTK_LABEL(editor->priv->pos_x_units_label), units_string);
92         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->pos_y_spin), digits);
93         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->pos_y_spin),
94                                         climb_rate, 10.0*climb_rate);
95         gtk_label_set_text (GTK_LABEL(editor->priv->pos_y_units_label), units_string);
96
97         /* Un-hide */
98         gtk_widget_show_all (editor->priv->pos_page_vbox);
99
100         /* Connect signals */
101         g_signal_connect_swapped (G_OBJECT (editor->priv->pos_x_spin),
102                                   "changed",
103                                   G_CALLBACK (gl_object_editor_changed_cb),
104                                   G_OBJECT (editor));
105         g_signal_connect_swapped (G_OBJECT (editor->priv->pos_y_spin),
106                                   "changed",
107                                   G_CALLBACK (gl_object_editor_changed_cb),
108                                   G_OBJECT (editor));
109
110         gl_debug (DEBUG_EDITOR, "END");
111 }
112
113
114 /*****************************************************************************/
115 /* Set position.                                                             */
116 /*****************************************************************************/
117 void
118 gl_object_editor_set_position (glObjectEditor      *editor,
119                                  gdouble                x,
120                                  gdouble                y)
121 {
122         gl_debug (DEBUG_EDITOR, "START");
123
124         editor->priv->stop_signals = TRUE;
125
126         /* save a copy in internal units */
127         editor->priv->x = x;
128         editor->priv->y = y;
129
130         /* convert internal units to displayed units */
131         gl_debug (DEBUG_EDITOR, "internal x,y = %g, %g", x, y);
132         x *= editor->priv->units_per_point;
133         y *= editor->priv->units_per_point;
134         gl_debug (DEBUG_EDITOR, "display x,y = %g, %g", x, y);
135
136         /* Set widget values */
137         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->pos_x_spin), x);
138         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->pos_y_spin), y);
139
140         editor->priv->stop_signals = FALSE;
141
142         gl_debug (DEBUG_EDITOR, "END");
143 }
144
145
146 /*****************************************************************************/
147 /* Set maximum position.                                                     */
148 /*****************************************************************************/
149 void
150 gl_object_editor_set_max_position (glObjectEditor      *editor,
151                                      gdouble                x_max,
152                                      gdouble                y_max)
153 {
154         gdouble tmp;
155
156         gl_debug (DEBUG_EDITOR, "START");
157
158         editor->priv->stop_signals = TRUE;
159
160         /* save a copy in internal units */
161         editor->priv->x_max = x_max;
162         editor->priv->y_max = y_max;
163
164         /* convert internal units to displayed units */
165         gl_debug (DEBUG_EDITOR, "internal x_max,y_max = %g, %g", x_max, y_max);
166         x_max *= editor->priv->units_per_point;
167         y_max *= editor->priv->units_per_point;
168         gl_debug (DEBUG_EDITOR, "display x_max,y_max = %g, %g", x_max, y_max);
169
170         /* Set widget values */
171         tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->pos_x_spin));
172         gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->pos_x_spin),
173                                    -x_max, 2.0*x_max);
174         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->pos_x_spin), tmp);
175         tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->pos_y_spin));
176         gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->pos_y_spin),
177                                    -y_max, 2.0*y_max);
178         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->pos_y_spin), tmp);
179
180         editor->priv->stop_signals = FALSE;
181
182         gl_debug (DEBUG_EDITOR, "END");
183 }
184
185
186 /*****************************************************************************/
187 /* Query position.                                                           */
188 /*****************************************************************************/
189 void
190 gl_object_editor_get_position (glObjectEditor      *editor,
191                                  gdouble               *x,
192                                  gdouble               *y)
193 {
194         gl_debug (DEBUG_EDITOR, "START");
195
196         /* Get values from widgets */
197         *x = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->pos_x_spin));
198         *y = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->pos_y_spin));
199
200         /* convert everything back to our internal units (points) */
201         *x /= editor->priv->units_per_point;
202         *y /= editor->priv->units_per_point;
203
204         /* save a copy in internal units */
205         editor->priv->x = *x;
206         editor->priv->y = *y;
207
208         gl_debug (DEBUG_EDITOR, "END");
209 }
210
211
212 /*****************************************************************************/
213 /* PRIVATE. Prefs changed callback.  Update units related items.            */
214 /*****************************************************************************/
215 void
216 position_prefs_changed_cb (glObjectEditor *editor)
217 {
218         lglUnits      units;
219         const gchar  *units_string;
220         gdouble       climb_rate;
221         gint          digits;
222
223         gl_debug (DEBUG_EDITOR, "START");
224
225         /* Get new configuration information */
226         units = gl_prefs_model_get_units (gl_prefs);
227         units_string = lgl_units_get_name (units);
228         editor->priv->units_per_point = lgl_units_get_units_per_point (units);
229         climb_rate = gl_units_util_get_step_size (units);
230         digits = gl_units_util_get_precision (units);
231
232         /* Update characteristics of x_spin/y_spin */
233         editor->priv->stop_signals = TRUE;
234         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->pos_x_spin),
235                                     digits);
236         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->pos_y_spin),
237                                     digits);
238         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->pos_x_spin),
239                                         climb_rate, 10.0*climb_rate);
240         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->pos_y_spin),
241                                         climb_rate, 10.0*climb_rate);
242         editor->priv->stop_signals = FALSE;
243
244         /* Update units_labels */
245         gtk_label_set_text (GTK_LABEL(editor->priv->pos_x_units_label),
246                             units_string);
247         gtk_label_set_text (GTK_LABEL(editor->priv->pos_y_units_label),
248                             units_string);
249
250         /* Update values of x_spin/y_spin */
251         gl_object_editor_set_position (editor,
252                                        editor->priv->x,
253                                        editor->priv->y);
254         gl_object_editor_set_max_position (editor,
255                                            editor->priv->x_max,
256                                            editor->priv->y_max);
257
258         gl_debug (DEBUG_EDITOR, "END");
259 }
260
261
262
263 /*
264  * Local Variables:       -- emacs
265  * mode: C                -- emacs
266  * c-basic-offset: 8      -- emacs
267  * tab-width: 8           -- emacs
268  * indent-tabs-mode: nil  -- emacs
269  * End:                   -- emacs
270  */