]> git.street.me.uk Git - andy/viking.git/blob - src/vikcoordlayer.c
Remove dependencies to gob2
[andy/viking.git] / src / vikcoordlayer.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 #include <math.h>
25 #include <glib/gi18n.h>
26
27 #include "viking.h"
28 #include "icons/icons.h"
29
30 static void coord_layer_marshall( VikCoordLayer *vcl, guint8 **data, gint *len );
31 static VikCoordLayer *coord_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp );
32 static gboolean coord_layer_set_param ( VikCoordLayer *vcl, guint16 id, VikLayerParamData data, VikViewport *vp );
33 static VikLayerParamData coord_layer_get_param ( VikCoordLayer *vcl, guint16 id );
34 static void coord_layer_update_gc ( VikCoordLayer *vcl, VikViewport *vp, const gchar *color );
35 static void coord_layer_post_read ( VikLayer *vl, VikViewport *vp, gboolean from_file );
36
37 static VikLayerParamScale param_scales[] = {
38   { 0.05, 60.0, 0.25, 10 },
39   { 1, 10, 1, 0 },
40 };
41
42 static VikLayerParam coord_layer_params[] = {
43   { "color", VIK_LAYER_PARAM_COLOR, VIK_LAYER_GROUP_NONE, N_("Color:"), VIK_LAYER_WIDGET_COLOR, 0 },
44   { "min_inc", VIK_LAYER_PARAM_DOUBLE, VIK_LAYER_GROUP_NONE, N_("Minutes Width:"), VIK_LAYER_WIDGET_SPINBUTTON, param_scales + 0 },
45   { "line_thickness", VIK_LAYER_PARAM_UINT, VIK_LAYER_GROUP_NONE, N_("Line Thickness:"), VIK_LAYER_WIDGET_SPINBUTTON, param_scales + 1 },
46 };
47
48
49 enum { PARAM_COLOR = 0, PARAM_MIN_INC, PARAM_LINE_THICKNESS, NUM_PARAMS };
50
51 VikLayerInterface vik_coord_layer_interface = {
52   "Coord",
53   &vikcoordlayer_pixbuf,
54
55   NULL,
56   0,
57
58   coord_layer_params,
59   NUM_PARAMS,
60   NULL,
61   0,
62
63   VIK_MENU_ITEM_ALL,
64
65   (VikLayerFuncCreate)                  vik_coord_layer_create,
66   (VikLayerFuncRealize)                 NULL,
67                                         coord_layer_post_read,
68   (VikLayerFuncFree)                    vik_coord_layer_free,
69
70   (VikLayerFuncProperties)              NULL,
71   (VikLayerFuncDraw)                    vik_coord_layer_draw,
72   (VikLayerFuncChangeCoordMode)         NULL,
73
74   (VikLayerFuncSetMenuItemsSelection)   NULL,
75   (VikLayerFuncGetMenuItemsSelection)   NULL,
76
77   (VikLayerFuncAddMenuItems)            NULL,
78   (VikLayerFuncSublayerAddMenuItems)    NULL,
79
80   (VikLayerFuncSublayerRenameRequest)   NULL,
81   (VikLayerFuncSublayerToggleVisible)   NULL,
82
83   (VikLayerFuncMarshall)                coord_layer_marshall,
84   (VikLayerFuncUnmarshall)              coord_layer_unmarshall,
85
86   (VikLayerFuncSetParam)                coord_layer_set_param,
87   (VikLayerFuncGetParam)                coord_layer_get_param,
88
89   (VikLayerFuncReadFileData)            NULL,
90   (VikLayerFuncWriteFileData)           NULL,
91
92   (VikLayerFuncDeleteItem)              NULL,
93   (VikLayerFuncCopyItem)                NULL,
94   (VikLayerFuncPasteItem)               NULL,
95   (VikLayerFuncFreeCopiedItem)          NULL,
96   (VikLayerFuncDragDropRequest)         NULL,
97 };
98
99 struct _VikCoordLayer {
100   VikLayer vl;
101   GdkGC *gc;
102   gdouble deg_inc;
103   guint8 line_thickness;
104   GdkColor *color;
105 };
106
107 GType vik_coord_layer_get_type ()
108 {
109   static GType vcl_type = 0;
110
111   if (!vcl_type)
112   {
113     static const GTypeInfo vcl_info =
114     {
115       sizeof (VikCoordLayerClass),
116       NULL, /* base_init */
117       NULL, /* base_finalize */
118       NULL, /* class init */
119       NULL, /* class_finalize */
120       NULL, /* class_data */
121       sizeof (VikCoordLayer),
122       0,
123       NULL /* instance init */
124     };
125     vcl_type = g_type_register_static ( VIK_LAYER_TYPE, "VikCoordLayer", &vcl_info, 0 );
126   }
127
128   return vcl_type;
129 }
130
131 static void coord_layer_marshall( VikCoordLayer *vcl, guint8 **data, gint *len )
132 {
133   vik_layer_marshall_params ( VIK_LAYER(vcl), data, len );
134 }
135
136 static VikCoordLayer *coord_layer_unmarshall( guint8 *data, gint len, VikViewport *vvp )
137 {
138   VikCoordLayer *rv = vik_coord_layer_new ( vvp );
139   vik_layer_unmarshall_params ( VIK_LAYER(rv), data, len, vvp );
140   return rv;
141 }
142
143 gboolean coord_layer_set_param ( VikCoordLayer *vcl, guint16 id, VikLayerParamData data, VikViewport *vp )
144 {
145   switch ( id )
146   {
147     case PARAM_COLOR: if ( vcl->color ) gdk_color_free ( vcl->color ); vcl->color = gdk_color_copy( &(data.c)); break;
148     case PARAM_MIN_INC: vcl->deg_inc = data.d / 60.0; break;
149     case PARAM_LINE_THICKNESS: if ( data.u >= 1 && data.u <= 15 ) vcl->line_thickness = data.u; break;
150   }
151   return TRUE;
152 }
153
154 static VikLayerParamData coord_layer_get_param ( VikCoordLayer *vcl, guint16 id )
155 {
156   VikLayerParamData rv;
157   switch ( id )
158   {
159     case PARAM_COLOR:
160       if (vcl->color)
161       {
162         rv.c.pixel = vcl->color->pixel;
163         rv.c.red = vcl->color->red;
164         rv.c.green = vcl->color->green;
165         rv.c.blue = vcl->color->blue;
166       }
167       break;
168     case PARAM_MIN_INC: rv.d = vcl->deg_inc * 60.0; break;
169     case PARAM_LINE_THICKNESS: rv.i = vcl->line_thickness; break;
170   }
171   return rv;
172 }
173
174 static void coord_layer_post_read ( VikLayer *vl, VikViewport *vp, gboolean from_file )
175 {
176   VikCoordLayer *vcl = VIK_COORD_LAYER(vl);
177   if ( vcl->gc )
178     g_object_unref ( G_OBJECT(vcl->gc) );
179
180   vcl->gc = vik_viewport_new_gc_from_color ( vp, vcl->color, vcl->line_thickness );
181
182 }
183
184 VikCoordLayer *vik_coord_layer_new ( )
185 {
186   GdkColor InitColor;
187   
188   VikCoordLayer *vcl = VIK_COORD_LAYER ( g_object_new ( VIK_COORD_LAYER_TYPE, NULL ) );
189   vik_layer_init ( VIK_LAYER(vcl), VIK_LAYER_COORD );
190
191   InitColor.pixel = 0;
192   InitColor.red = 65535;
193   InitColor.green = 65535;
194   InitColor.blue = 65535;
195
196   vcl->gc = NULL;
197   vcl->deg_inc = 1.0/60.0;
198   vcl->line_thickness = 3;
199   vcl->color = gdk_color_copy (&InitColor);
200   return vcl;
201 }
202
203 void vik_coord_layer_draw ( VikCoordLayer *vcl, gpointer data )
204 {
205   VikViewport *vp = (VikViewport *) data;
206
207   if ( !vcl->gc ) {
208     return;
209   }
210
211   if ( vik_viewport_get_coord_mode(vp) != VIK_COORD_UTM ) 
212   {
213     VikCoord left, right, left2, right2;
214     gdouble l, r, i, j;
215     gint x1, y1, x2, y2, smod = 1, mmod = 1;
216     gboolean mins = FALSE, secs = FALSE;
217     GdkGC *dgc = vik_viewport_new_gc_from_color(vp, vcl->color, vcl->line_thickness);
218     GdkGC *mgc = vik_viewport_new_gc_from_color(vp, vcl->color, MAX(vcl->line_thickness/2, 1));
219     GdkGC *sgc = vik_viewport_new_gc_from_color(vp, vcl->color, MAX(vcl->line_thickness/5, 1));
220
221     vik_viewport_screen_to_coord ( vp, 0, 0, &left );
222     vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), 0, &right );
223     vik_viewport_screen_to_coord ( vp, 0, vik_viewport_get_height(vp), &left2 );
224     vik_viewport_screen_to_coord ( vp, vik_viewport_get_width(vp), vik_viewport_get_height(vp), &right2 );
225
226 #define CLINE(gc, c1, c2) { \
227           vik_viewport_coord_to_screen(vp, (c1), &x1, &y1);  \
228           vik_viewport_coord_to_screen(vp, (c2), &x2, &y2);  \
229           vik_viewport_draw_line (vp, (gc), x1, y1, x2, y2); \
230         }
231
232     l = left.east_west;
233     r = right.east_west;
234     if (60*fabs(l-r) < 4) {
235       secs = TRUE;
236       smod = MIN(6, (int)ceil(3600*fabs(l-r)/30.0));
237     }
238     if (fabs(l-r) < 4) {
239       mins = TRUE;
240       mmod = MIN(6, (int)ceil(60*fabs(l-r)/30.0));
241     }
242     for (i=floor(l*60); i<ceil(r*60); i+=1.0) {
243       if (secs) {
244         for (j=i*60+1; j<(i+1)*60; j+=1.0) {
245           left.east_west = j/3600.0;
246           left2.east_west = j/3600.0;
247           if ((int)j % smod == 0) CLINE(sgc, &left, &left2);
248         }
249       }
250       if (mins) {
251         left.east_west = i/60.0;
252         left2.east_west = i/60.0;
253         if ((int)i % mmod == 0) CLINE(mgc, &left, &left2);
254       }
255       if ((int)i % 60 == 0) {
256         left.east_west = i/60.0;
257         left2.east_west = i/60.0;
258         CLINE(dgc, &left, &left2);
259       }
260     }
261
262     vik_viewport_screen_to_coord ( vp, 0, 0, &left );
263     l = left2.north_south;
264     r = left.north_south;
265     for (i=floor(l*60); i<ceil(r*60); i+=1.0) {
266       if (secs) {
267         for (j=i*60+1; j<(i+1)*60; j+=1.0) {
268           left.north_south = j/3600.0;
269           right.north_south = j/3600.0;
270           if ((int)j % smod == 0) CLINE(sgc, &left, &right);
271         }
272       }
273       if (mins) {
274         left.north_south = i/60.0;
275         right.north_south = i/60.0;
276         if ((int)i % mmod == 0) CLINE(mgc, &left, &right);
277       }
278       if ((int)i % 60 == 0) {
279         left.north_south = i/60.0;
280         right.north_south = i/60.0;
281         CLINE(dgc, &left, &right);
282       }
283     }
284 #undef CLINE
285     g_object_unref(dgc);
286     g_object_unref(sgc);
287     g_object_unref(mgc);
288     return;
289   }
290
291   if ( vik_viewport_get_coord_mode(vp) == VIK_COORD_UTM ) 
292   {
293     const struct UTM *center = (const struct UTM *)vik_viewport_get_center ( vp );
294     gdouble xmpp = vik_viewport_get_xmpp ( vp ), ympp = vik_viewport_get_ympp ( vp );
295     guint16 width = vik_viewport_get_width ( vp ), height = vik_viewport_get_height ( vp );
296     struct LatLon ll, ll2, min, max;
297     double lon;
298     int x1, x2;
299     struct UTM utm;
300
301     utm = *center;
302     utm.northing = center->northing - ( ympp * height / 2 );
303
304     a_coords_utm_to_latlon ( &utm, &ll );
305
306     utm.northing = center->northing + ( ympp * height / 2 );
307
308     a_coords_utm_to_latlon ( &utm, &ll2 );
309
310     {
311       /* find corner coords in lat/lon.
312         start at whichever is less: top or bottom left lon. goto whichever more: top or bottom right lon
313       */
314       struct LatLon topleft, topright, bottomleft, bottomright;
315       struct UTM temp_utm;
316       temp_utm = *center;
317       temp_utm.easting -= (width/2)*xmpp;
318       temp_utm.northing += (height/2)*ympp;
319       a_coords_utm_to_latlon ( &temp_utm, &topleft );
320       temp_utm.easting += (width*xmpp);
321       a_coords_utm_to_latlon ( &temp_utm, &topright );
322       temp_utm.northing -= (height*ympp);
323       a_coords_utm_to_latlon ( &temp_utm, &bottomright );
324       temp_utm.easting -= (width*xmpp);
325       a_coords_utm_to_latlon ( &temp_utm, &bottomleft );
326       min.lon = (topleft.lon < bottomleft.lon) ? topleft.lon : bottomleft.lon;
327       max.lon = (topright.lon > bottomright.lon) ? topright.lon : bottomright.lon;
328       min.lat = (bottomleft.lat < bottomright.lat) ? bottomleft.lat : bottomright.lat;
329       max.lat = (topleft.lat > topright.lat) ? topleft.lat : topright.lat;
330     }
331
332     lon = ((double) ((long) ((min.lon)/ vcl->deg_inc))) * vcl->deg_inc;
333     ll.lon = ll2.lon = lon;
334
335     for (; ll.lon <= max.lon; ll.lon+=vcl->deg_inc, ll2.lon+=vcl->deg_inc )
336     {
337       a_coords_latlon_to_utm ( &ll, &utm );
338       x1 = ( (utm.easting - center->easting) / xmpp ) + (width / 2);
339       a_coords_latlon_to_utm ( &ll2, &utm );
340       x2 = ( (utm.easting - center->easting) / xmpp ) + (width / 2);
341       vik_viewport_draw_line (vp, vcl->gc, x1, height, x2, 0);
342     }
343
344     utm = *center;
345     utm.easting = center->easting - ( xmpp * width / 2 );
346
347     a_coords_utm_to_latlon ( &utm, &ll );
348
349     utm.easting = center->easting + ( xmpp * width / 2 );
350
351     a_coords_utm_to_latlon ( &utm, &ll2 );
352
353     /* really lat, just reusing a variable */
354     lon = ((double) ((long) ((min.lat)/ vcl->deg_inc))) * vcl->deg_inc;
355     ll.lat = ll2.lat = lon;
356
357     for (; ll.lat <= max.lat ; ll.lat+=vcl->deg_inc, ll2.lat+=vcl->deg_inc )
358     {
359       a_coords_latlon_to_utm ( &ll, &utm );
360       x1 = (height / 2) - ( (utm.northing - center->northing) / ympp );
361       a_coords_latlon_to_utm ( &ll2, &utm );
362       x2 = (height / 2) - ( (utm.northing - center->northing) / ympp );
363       vik_viewport_draw_line (vp, vcl->gc, width, x2, 0, x1);
364     }
365   }
366 }
367
368 void vik_coord_layer_free ( VikCoordLayer *vcl )
369 {
370   if ( vcl->gc != NULL )
371     g_object_unref ( G_OBJECT(vcl->gc) );
372
373   if ( vcl->color != NULL )
374     gdk_color_free ( vcl->color );
375 }
376
377 static void coord_layer_update_gc ( VikCoordLayer *vcl, VikViewport *vp, const gchar *color )
378 {
379   GdkColor InitColor;
380   
381   if ( vcl->color )
382     gdk_color_free ( vcl->color );
383
384   gdk_color_parse( color, &InitColor);
385   vcl->color = gdk_color_copy( &InitColor );
386
387   if ( vcl->gc )
388     g_object_unref ( G_OBJECT(vcl->gc) );
389
390   vcl->gc = vik_viewport_new_gc_from_color ( vp, vcl->color, vcl->line_thickness );
391 }
392
393 VikCoordLayer *vik_coord_layer_create ( VikViewport *vp )
394 {
395   VikCoordLayer *vcl = vik_coord_layer_new ();
396   coord_layer_update_gc ( vcl, vp, "red" );
397   return vcl;
398 }
399