]> git.sur5r.net Git - glabels/blob - src/object-editor-lsize-page.c
Imported Upstream version 2.2.8
[glabels] / 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         editor->priv->lsize_page_vbox =
77                 glade_xml_get_widget (editor->priv->gui, "lsize_page_vbox");
78         editor->priv->lsize_r_spin =
79                 glade_xml_get_widget (editor->priv->gui, "lsize_r_spin");
80         editor->priv->lsize_theta_spin =
81                 glade_xml_get_widget (editor->priv->gui, "lsize_theta_spin");
82         editor->priv->lsize_r_units_label =
83                 glade_xml_get_widget (editor->priv->gui, "lsize_r_units_label");
84
85         /* Get configuration information */
86         units_string = gl_prefs_get_units_string ();
87         editor->priv->units_per_point = gl_prefs_get_units_per_point ();
88         climb_rate = gl_prefs_get_units_step_size ();
89         digits = gl_prefs_get_units_precision ();
90
91         /* Modify widgets based on configuration */
92         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin), digits);
93         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin),
94                                         climb_rate, 10.0*climb_rate);
95         gtk_label_set_text (GTK_LABEL(editor->priv->lsize_r_units_label), units_string);
96
97         /* Un-hide */
98         gtk_widget_show_all (editor->priv->lsize_page_vbox);
99
100         /* Connect signals */
101         g_signal_connect_swapped (G_OBJECT (editor->priv->lsize_r_spin),
102                                   "changed",
103                                   G_CALLBACK (gl_object_editor_changed_cb),
104                                   G_OBJECT (editor));
105         g_signal_connect_swapped (G_OBJECT (editor->priv->lsize_theta_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 /* Set line size.                                                            */
115 /*****************************************************************************/
116 void
117 gl_object_editor_set_lsize (glObjectEditor      *editor,
118                             gdouble              dx,
119                             gdouble              dy)
120 {
121         gdouble r, theta;
122
123         gl_debug (DEBUG_EDITOR, "START");
124
125         editor->priv->stop_signals = TRUE;
126
127         /* save a copy in internal units */
128         editor->priv->dx = dx;
129         editor->priv->dy = dy;
130
131         /* convert internal units to displayed units */
132         gl_debug (DEBUG_EDITOR, "internal dx,dy = %g, %g", dx, dy);
133         dx *= editor->priv->units_per_point;
134         dy *= editor->priv->units_per_point;
135         gl_debug (DEBUG_EDITOR, "display dx,dy = %g, %g", dx, dy);
136
137         r     = LENGTH (dx, dy);
138         theta = ANGLE (dx, dy);
139
140         /* Set widget values */
141         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin), r);
142         gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->lsize_theta_spin),
143                                    theta);
144
145         editor->priv->stop_signals = FALSE;
146
147         gl_debug (DEBUG_EDITOR, "END");
148 }
149
150 /*****************************************************************************/
151 /* Set maximum line size.                                                    */
152 /*****************************************************************************/
153 void
154 gl_object_editor_set_max_lsize (glObjectEditor      *editor,
155                                 gdouble              dx_max,
156                                 gdouble              dy_max)
157 {
158         gdouble tmp;
159
160         gl_debug (DEBUG_EDITOR, "START");
161
162         if (editor->priv->lsize_page_vbox)
163         {
164
165                 editor->priv->stop_signals = TRUE;
166
167                 /* save a copy in internal units */
168                 editor->priv->dx_max = dx_max;
169                 editor->priv->dy_max = dy_max;
170
171                 /* convert internal units to displayed units */
172                 gl_debug (DEBUG_EDITOR, "internal dx_max,dy_max = %g, %g", dx_max, dy_max);
173                 dx_max *= editor->priv->units_per_point;
174                 dy_max *= editor->priv->units_per_point;
175                 gl_debug (DEBUG_EDITOR, "display dx_max,dy_max = %g, %g", dx_max, dy_max);
176
177                 /* Set widget values */
178                 tmp = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin));
179                 gtk_spin_button_set_range (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin),
180                                            0.0, 2.0*LENGTH (dx_max, dy_max));
181                 gtk_spin_button_set_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin), tmp);
182
183                 editor->priv->stop_signals = FALSE;
184
185         }
186
187         gl_debug (DEBUG_EDITOR, "END");
188 }
189
190 /*****************************************************************************/
191 /* Query line size.                                                          */
192 /*****************************************************************************/
193 void
194 gl_object_editor_get_lsize (glObjectEditor      *editor,
195                             gdouble             *dx,
196                             gdouble             *dy)
197 {
198         gdouble r, theta;
199
200         gl_debug (DEBUG_EDITOR, "START");
201
202         /* Get values from widgets */
203         r = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->lsize_r_spin));
204         theta = gtk_spin_button_get_value (GTK_SPIN_BUTTON (editor->priv->lsize_theta_spin));
205
206         /* convert everything back to our internal units (points) */
207         r /= editor->priv->units_per_point;
208
209         *dx = COMP_X (r, theta);
210         *dy = COMP_Y (r, theta);
211
212         /* save a copy in internal units */
213         editor->priv->dx = *dx;
214         editor->priv->dy = *dy;
215
216         gl_debug (DEBUG_EDITOR, "END");
217 }
218
219 /*****************************************************************************/
220 /* PRIVATE. Prefs changed callback.  Update units related items.            */
221 /*****************************************************************************/
222 void
223 lsize_prefs_changed_cb (glObjectEditor *editor)
224 {
225         const gchar  *units_string;
226         gdouble       climb_rate;
227         gint          digits;
228
229         gl_debug (DEBUG_EDITOR, "START");
230
231         /* Get new configuration information */
232         units_string = gl_prefs_get_units_string ();
233         editor->priv->units_per_point = gl_prefs_get_units_per_point ();
234         climb_rate = gl_prefs_get_units_step_size ();
235         digits = gl_prefs_get_units_precision ();
236
237         /* Update characteristics of r_spin */
238         editor->priv->stop_signals = TRUE;
239         gtk_spin_button_set_digits (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin),
240                                     digits);
241         gtk_spin_button_set_increments (GTK_SPIN_BUTTON(editor->priv->lsize_r_spin),
242                                         climb_rate, 10.0*climb_rate);
243         editor->priv->stop_signals = FALSE;
244
245         /* Update r_units_label */
246         gtk_label_set_text (GTK_LABEL(editor->priv->lsize_r_units_label),
247                             units_string);
248
249         /* Update values of r_spin/theta_spin */
250         gl_object_editor_set_lsize (editor,
251                                     editor->priv->dx,
252                                     editor->priv->dy);
253         gl_object_editor_set_max_lsize (editor,
254                                         editor->priv->dx_max,
255                                         editor->priv->dy_max);
256
257         gl_debug (DEBUG_EDITOR, "END");
258 }
259