]> git.sur5r.net Git - glabels/blob - glabels2/src/object-editor-lsize-page.c
2009-02-21 JimEvins <evins@snaught.com>
[glabels] / glabels2 / src / object-editor-lsize-page.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /*
4  *  (GLABELS) Label and Business Card Creation program for GNOME
5  *
6  *  object-editor.c:  object properties editor module
7  *
8  *  Copyright (C) 2003  Jim Evins <evins@snaught.com>.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24 #include <config.h>
25
26 #include "object-editor.h"
27
28 #include <glib/gi18n.h>
29 #include <gtk/gtklabel.h>
30 #include <gtk/gtkspinbutton.h>
31 #include <math.h>
32
33 #include "prefs.h"
34
35 #include "object-editor-private.h"
36
37 #include "debug.h"
38
39 /*===========================================*/
40 /* Private macros                            */
41 /*===========================================*/
42
43 #define LENGTH(x,y) sqrt( (x)*(x) + (y)*(y) )
44 #define ANGLE(x,y)  ( (180.0/G_PI)*atan2( -(y), (x) ) )
45 #define COMP_X(l,a) ( (l) * cos( (G_PI/180.0)*(a) ) )
46 #define COMP_Y(l,a) ( -(l) * sin( (G_PI/180.0)*(a) ) )
47                                                                                 
48
49 /*===========================================*/
50 /* Private data types                        */
51 /*===========================================*/
52
53 /*===========================================*/
54 /* Private globals                           */
55 /*===========================================*/
56
57 /*===========================================*/
58 /* Local function prototypes                 */
59 /*===========================================*/
60
61
62 \f
63 /*--------------------------------------------------------------------------*/
64 /* PRIVATE.  Prepare line size page.                                        */
65 /*--------------------------------------------------------------------------*/
66 void
67 gl_object_editor_prepare_lsize_page (glObjectEditor       *editor)
68 {
69         const gchar  *units_string;
70         gdouble       climb_rate;
71         gint          digits;
72
73         gl_debug (DEBUG_EDITOR, "START");
74
75         /* Extract widgets from XML tree. */
76         gl_util_get_builder_widgets (editor->priv->gui,
77                                      "lsize_page_vbox",     &editor->priv->lsize_page_vbox,
78                                      "lsize_r_spin",        &editor->priv->lsize_r_spin,
79                                      "lsize_theta_spin",    &editor->priv->lsize_theta_spin,
80                                      "lsize_r_units_label", &editor->priv->lsize_r_units_label,
81                                      NULL);
82
83         /* Get configuration information */
84         units_string = gl_prefs_get_units_string ();
85         editor->priv->units_per_point = gl_prefs_get_units_per_point ();
86         climb_rate = gl_prefs_get_units_step_size ();
87         digits = gl_prefs_get_units_precision ();
88
89         /* Modify widgets based on configuration */
90         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin), digits);
91         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin),
92                                         climb_rate, 0);
93         gtk_label_set_text (GTK_LABEL(editor->priv->lsize_r_units_label), units_string);
94
95         /* Un-hide */
96         gtk_widget_show_all (editor->priv->lsize_page_vbox);
97
98         /* Connect signals */
99         g_signal_connect_swapped (G_OBJECT (editor->priv->lsize_r_spin),
100                                   "changed",
101                                   G_CALLBACK (gl_object_editor_changed_cb),
102                                   G_OBJECT (editor));
103         g_signal_connect_swapped (G_OBJECT (editor->priv->lsize_theta_spin),
104                                   "changed",
105                                   G_CALLBACK (gl_object_editor_changed_cb),
106                                   G_OBJECT (editor));
107
108         gl_debug (DEBUG_EDITOR, "END");
109 }
110
111 /*****************************************************************************/
112 /* Set line size.                                                            */
113 /*****************************************************************************/
114 void
115 gl_object_editor_set_lsize (glObjectEditor      *editor,
116                             gdouble              dx,
117                             gdouble              dy)
118 {
119         gdouble r, theta;
120
121         gl_debug (DEBUG_EDITOR, "START");
122
123         editor->priv->stop_signals = TRUE;
124
125         /* save a copy in internal units */
126         editor->priv->dx = dx;
127         editor->priv->dy = dy;
128
129         /* convert internal units to displayed units */
130         gl_debug (DEBUG_EDITOR, "internal dx,dy = %g, %g", dx, dy);
131         dx *= editor->priv->units_per_point;
132         dy *= editor->priv->units_per_point;
133         gl_debug (DEBUG_EDITOR, "display dx,dy = %g, %g", dx, dy);
134
135         r     = LENGTH (dx, dy);
136         theta = ANGLE (dx, dy);
137
138         /* Set widget values */
139         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin), r);
140         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->lsize_theta_spin),
141                                    theta);
142
143         editor->priv->stop_signals = FALSE;
144
145         gl_debug (DEBUG_EDITOR, "END");
146 }
147
148 /*****************************************************************************/
149 /* Set maximum line size.                                                    */
150 /*****************************************************************************/
151 void
152 gl_object_editor_set_max_lsize (glObjectEditor      *editor,
153                                 gdouble              dx_max,
154                                 gdouble              dy_max)
155 {
156         gdouble tmp;
157
158         gl_debug (DEBUG_EDITOR, "START");
159
160         if (editor->priv->lsize_page_vbox)
161         {
162
163                 editor->priv->stop_signals = TRUE;
164
165                 /* save a copy in internal units */
166                 editor->priv->dx_max = dx_max;
167                 editor->priv->dy_max = dy_max;
168
169                 /* convert internal units to displayed units */
170                 gl_debug (DEBUG_EDITOR, "internal dx_max,dy_max = %g, %g", dx_max, dy_max);
171                 dx_max *= editor->priv->units_per_point;
172                 dy_max *= editor->priv->units_per_point;
173                 gl_debug (DEBUG_EDITOR, "display dx_max,dy_max = %g, %g", dx_max, dy_max);
174
175                 /* Set widget values */
176                 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin));
177                 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin),
178                                            0.0, 2.0*LENGTH (dx_max, dy_max));
179                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin), tmp);
180
181                 editor->priv->stop_signals = FALSE;
182
183         }
184
185         gl_debug (DEBUG_EDITOR, "END");
186 }
187
188 /*****************************************************************************/
189 /* Query line size.                                                          */
190 /*****************************************************************************/
191 void
192 gl_object_editor_get_lsize (glObjectEditor      *editor,
193                             gdouble             *dx,
194                             gdouble             *dy)
195 {
196         gdouble r, theta;
197
198         gl_debug (DEBUG_EDITOR, "START");
199
200         /* Get values from widgets */
201         r = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin));
202         theta = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->lsize_theta_spin));
203
204         /* convert everything back to our internal units (points) */
205         r /= editor->priv->units_per_point;
206
207         *dx = COMP_X (r, theta);
208         *dy = COMP_Y (r, theta);
209
210         /* save a copy in internal units */
211         editor->priv->dx = *dx;
212         editor->priv->dy = *dy;
213
214         gl_debug (DEBUG_EDITOR, "END");
215 }
216
217 /*****************************************************************************/
218 /* PRIVATE. Prefs changed callback.  Update units related items.            */
219 /*****************************************************************************/
220 void
221 lsize_prefs_changed_cb (glObjectEditor *editor)
222 {
223         const gchar  *units_string;
224         gdouble       climb_rate;
225         gint          digits;
226
227         gl_debug (DEBUG_EDITOR, "START");
228
229         /* Get new configuration information */
230         units_string = gl_prefs_get_units_string ();
231         editor->priv->units_per_point = gl_prefs_get_units_per_point ();
232         climb_rate = gl_prefs_get_units_step_size ();
233         digits = gl_prefs_get_units_precision ();
234
235         /* Update characteristics of r_spin */
236         editor->priv->stop_signals = TRUE;
237         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin),
238                                     digits);
239         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin),
240                                         climb_rate, 0);
241         editor->priv->stop_signals = FALSE;
242
243         /* Update r_units_label */
244         gtk_label_set_text (GTK_LABEL(editor->priv->lsize_r_units_label),
245                             units_string);
246
247         /* Update values of r_spin/theta_spin */
248         gl_object_editor_set_lsize (editor,
249                                     editor->priv->dx,
250                                     editor->priv->dy);
251         gl_object_editor_set_max_lsize (editor,
252                                         editor->priv->dx_max,
253                                         editor->priv->dy_max);
254
255         gl_debug (DEBUG_EDITOR, "END");
256 }
257