+void vik_viewport_set_draw_scale ( VikViewport *vvp, gboolean draw_scale )
+{
+ vvp->draw_scale = draw_scale;
+}
+
+gboolean vik_viewport_get_draw_scale ( VikViewport *vvp )
+{
+ return vvp->draw_scale;
+}
+
+void vik_viewport_draw_scale ( VikViewport *vvp )
+{
+ if ( vvp->draw_scale ) {
+ VikCoord left, right;
+ gdouble unit, base, diff, old_unit, old_diff, ratio;
+ gint odd, len, PAD = 10, SCSIZE = 5, HEIGHT=10;
+ PangoFontDescription *pfd;
+ PangoLayout *pl;
+ gchar s[128];
+
+ g_return_if_fail ( vvp != NULL );
+
+ vik_viewport_screen_to_coord ( vvp, 0, vvp->height, &left );
+ vik_viewport_screen_to_coord ( vvp, vvp->width/SCSIZE, vvp->height, &right );
+
+ base = vik_coord_diff ( &left, &right ); // in meters
+ ratio = (vvp->width/SCSIZE)/base;
+
+ unit = 1;
+ diff = fabs(base-unit);
+ old_unit = unit;
+ old_diff = diff;
+ odd = 1;
+ while (diff <= old_diff) {
+ old_unit = unit;
+ old_diff = diff;
+ unit = unit * (odd%2 ? 5 : 2);
+ diff = fabs(base-unit);
+ odd++;
+ }
+ unit = old_unit;
+ len = unit * ratio;
+
+ /* white background */
+ vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
+ PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
+ vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
+ PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
+ vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
+ PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
+ /* black scale */
+ vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
+ PAD, vvp->height-PAD, PAD + len, vvp->height-PAD);
+ vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
+ PAD, vvp->height-PAD, PAD, vvp->height-PAD-HEIGHT);
+ vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
+ PAD + len, vvp->height-PAD, PAD + len, vvp->height-PAD-HEIGHT);
+ if (odd%2) {
+ int i;
+ for (i=1; i<5; i++) {
+ vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
+ PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
+ vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
+ PAD+i*len/5, vvp->height-PAD, PAD+i*len/5, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
+ }
+ } else {
+ int i;
+ for (i=1; i<10; i++) {
+ vik_viewport_draw_line(vvp, vvp->scale_bg_gc,
+ PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
+ vik_viewport_draw_line(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
+ PAD+i*len/10, vvp->height-PAD, PAD+i*len/10, vvp->height-PAD-((i==5)?(2*HEIGHT/3):(HEIGHT/2)));
+ }
+ }
+ pl = gtk_widget_create_pango_layout (GTK_WIDGET(&vvp->drawing_area), NULL);
+ pfd = pango_font_description_from_string ("Sans 8"); // FIXME: settable option? global variable?
+ pango_layout_set_font_description (pl, pfd);
+ pango_font_description_free (pfd);
+
+ if (unit >= 1000) {
+ sprintf(s, "%d km", (int)unit/1000);
+ } else {
+ sprintf(s, "%d m", (int)unit);
+ }
+ pango_layout_set_text(pl, s, -1);
+ vik_viewport_draw_layout(vvp, GTK_WIDGET(&vvp->drawing_area)->style->black_gc,
+ PAD + len + PAD, vvp->height - PAD - 10, pl);
+ g_object_unref(pl);
+ pl = NULL;
+ }
+}
+
+void vik_viewport_set_draw_centermark ( VikViewport *vvp, gboolean draw_centermark )
+{
+ vvp->draw_centermark = draw_centermark;
+}
+
+gboolean vik_viewport_get_draw_centermark ( VikViewport *vvp )
+{
+ return vvp->draw_centermark;
+}
+
+void vik_viewport_draw_centermark ( VikViewport *vvp )
+{
+ if ( !vvp->draw_centermark )
+ return;
+
+ const int len = 30;
+ const int gap = 4;
+ int center_x = vvp->width/2;
+ int center_y = vvp->height/2;
+ GdkGC * black_gc = GTK_WIDGET(&vvp->drawing_area)->style->black_gc;
+
+ /* white back ground */
+ vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x - len, center_y, center_x - gap, center_y);
+ vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x + gap, center_y, center_x + len, center_y);
+ vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y - len, center_x, center_y - gap);
+ vik_viewport_draw_line(vvp, vvp->scale_bg_gc, center_x, center_y + gap, center_x, center_y + len);
+ /* black fore ground */
+ vik_viewport_draw_line(vvp, black_gc, center_x - len, center_y, center_x - gap, center_y);
+ vik_viewport_draw_line(vvp, black_gc, center_x + gap, center_y, center_x + len, center_y);
+ vik_viewport_draw_line(vvp, black_gc, center_x, center_y - len, center_x, center_y - gap);
+ vik_viewport_draw_line(vvp, black_gc, center_x, center_y + gap, center_x, center_y + len);
+
+}
+