Diff of /trunk/src/osm-gps-map-osd-classic.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 88 by harbaum, Mon Aug 31 14:22:28 2009 UTC revision 102 by harbaum, Tue Sep 8 19:08:37 2009 UTC
# Line 25  Line 25 
25  /* OSD_DIAMETER */  /* OSD_DIAMETER */
26  /* OSD_X, OSD_Y */  /* OSD_X, OSD_Y */
27    
28    #define OSD_SCALE
29    
30    #define OSD_SCALE_W  120
31    #define OSD_SCALE_H   20
32    
33  #ifndef USE_CAIRO  #ifndef USE_CAIRO
34  #error "OSD control display lacks a non-cairo implementation!"  #error "OSD control display lacks a non-cairo implementation!"
35  #endif  #endif
# Line 39  typedef struct { Line 44  typedef struct {
44      /* the offscreen representation of the OSD */      /* the offscreen representation of the OSD */
45      cairo_surface_t *overlay;      cairo_surface_t *overlay;
46    
47    #ifdef OSD_SCALE
48        cairo_surface_t *scale;
49    #endif
50    
51  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
52      /* values to handle the "source" menu */      /* values to handle the "source" menu */
53      cairo_surface_t *map_source;      cairo_surface_t *map_source;
# Line 342  osd_source_content(osm_gps_map_osd_t *os Line 351  osd_source_content(osm_gps_map_osd_t *os
351                                      CAIRO_FONT_WEIGHT_BOLD);                                      CAIRO_FONT_WEIGHT_BOLD);
352              cairo_set_font_size (cr, OSD_FONT_SIZE);              cairo_set_font_size (cr, OSD_FONT_SIZE);
353    
354              int i, step = (priv->height - 2*OSD_TEXT_BORDER)              int i, step = (priv->height - 2*OSD_TEXT_BORDER) /
355                  / OSM_GPS_MAP_SOURCE_LAST;                  OSM_GPS_MAP_SOURCE_LAST;
356              for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {              for(i=OSM_GPS_MAP_SOURCE_NULL+1;i<=OSM_GPS_MAP_SOURCE_LAST;i++) {
357                  cairo_text_extents_t extents;                  cairo_text_extents_t extents;
358                  const char *src = osm_gps_map_source_get_friendly_name(i);                  const char *src = osm_gps_map_source_get_friendly_name(i);
# Line 431  osd_render_source_sel(osm_gps_map_osd_t Line 440  osd_render_source_sel(osm_gps_map_osd_t
440      cairo_destroy(cr);      cairo_destroy(cr);
441  }  }
442    
443    /* re-allocate the buffer used to draw the menu. This is used */
444    /* to collapse/expand the buffer */
445  static void  static void
446  osd_source_reallocate(osm_gps_map_osd_t *osd) {  osd_source_reallocate(osm_gps_map_osd_t *osd) {
447      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
# Line 440  osd_source_reallocate(osm_gps_map_osd_t Line 451  osd_source_reallocate(osm_gps_map_osd_t
451    
452      int w = OSD_S_W, h = OSD_S_H;      int w = OSD_S_W, h = OSD_S_H;
453      if(priv->expanded) {      if(priv->expanded) {
         /* ... and right of it the waypoint id */  
454          cairo_text_extents_t extents;          cairo_text_extents_t extents;
455    
456          /* determine content size */          /* determine content size */
# Line 456  osd_source_reallocate(osm_gps_map_osd_t Line 466  osd_source_reallocate(osm_gps_map_osd_t
466              const char *src = osm_gps_map_source_get_friendly_name(i);              const char *src = osm_gps_map_source_get_friendly_name(i);
467              cairo_text_extents (cr, src, &extents);              cairo_text_extents (cr, src, &extents);
468    
             //            printf("Source %d: %s = %f %f\n", i, src,  
             //                   extents.width, extents.height);  
   
469              if(extents.width > max_w) max_w = extents.width;              if(extents.width > max_w) max_w = extents.width;
470              if(extents.height > max_h) max_h = extents.height;              if(extents.height > max_h) max_h = extents.height;
471          }          }
# Line 477  osd_source_reallocate(osm_gps_map_osd_t Line 484  osd_source_reallocate(osm_gps_map_osd_t
484          cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);          cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);
485    
486      osd_render_source_sel(osd);      osd_render_source_sel(osd);
   
487  }  }
488    
489  #define OSD_HZ      15  #define OSD_HZ      15
# Line 509  static gboolean osd_source_animate(gpoin Line 515  static gboolean osd_source_animate(gpoin
515    
516      /* count runs linearly from 0 to 1000, map this nicely onto a position */      /* count runs linearly from 0 to 1000, map this nicely onto a position */
517    
     /* simple linear mapping */  
     //    priv->shift = (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +  
     //        (diff * priv->count)/1000;  
   
518      /* nicer sinoid mapping */      /* nicer sinoid mapping */
519      float m = 0.5-cos(priv->count * M_PI / 1000.0)/2;      float m = 0.5-cos(priv->count * M_PI / 1000.0)/2;
520      priv->shift = (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +      priv->shift = (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +
# Line 553  osd_source_toggle(osm_gps_map_osd_t *osd Line 555  osd_source_toggle(osm_gps_map_osd_t *osd
555      priv->handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ, osd_source_animate, osd);      priv->handler_id = gtk_timeout_add(OSD_TIME/OSD_HZ, osd_source_animate, osd);
556  }  }
557    
558    /* check if the user clicked inside the source selection area */
559  static osd_button_t  static osd_button_t
560  osd_source_check(osm_gps_map_osd_t *osd, gint x, gint y) {  osd_source_check(osm_gps_map_osd_t *osd, gint x, gint y) {
561      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
# Line 581  osd_source_check(osm_gps_map_osd_t *osd, Line 584  osd_source_check(osm_gps_map_osd_t *osd,
584      }      }
585    
586      /* check for clicks into data area */      /* check for clicks into data area */
587      if(priv->expanded) {      if(priv->expanded && !priv->handler_id) {
588            /* re-adjust from puller top to content top */
589            if(OSD_S_Y < 0)
590                y += OSD_S_EXP_H - OSD_S_PH;
591    
592          if(x > OSD_S_PW &&          if(x > OSD_S_PW &&
593             x < OSD_S_PW + OSD_S_EXP_W &&             x < OSD_S_PW + OSD_S_EXP_W &&
594             y > 0 &&             y > 0 &&
595             y < OSD_S_EXP_H) {             y < OSD_S_EXP_H) {
596    
597                int step = (priv->height - 2*OSD_TEXT_BORDER)
598                    / OSM_GPS_MAP_SOURCE_LAST;
599    
600              printf("in OSD source menu area\n");              y -= OSD_TEXT_BORDER - OSD_TEXT_SKIP;
601                y /= step;
602                y += 1;
603    
604                gint old = 0;
605                g_object_get(osd->widget, "map-source", &old, NULL);
606    
607                if(y > OSM_GPS_MAP_SOURCE_NULL &&
608                   y <= OSM_GPS_MAP_SOURCE_LAST &&
609                   old != y) {
610                    g_object_set(osd->widget, "map-source", y, NULL);
611    
612                    osd_render_source_sel(osd);
613                    osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
614                }
615    
616                /* return "clicked in OSD background" to prevent further */
617                /* processing by application */
618              return OSD_BG;              return OSD_BG;
619          }          }
620      }      }
# Line 701  osd_zoom_labels(cairo_t *cr, gint x, gin Line 727  osd_zoom_labels(cairo_t *cr, gint x, gin
727      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);
728  }  }
729    
730    static char *
731    dist_str_metric(int dist)
732    {
733        if(dist<1000)
734            return g_strdup_printf("%u m",  dist);
735    
736        return g_strdup_printf("%u km", dist/1000);
737    }
738    
739    #define OSD_SCALE_FONT_SIZE 12
740    
741    static void
742    osd_render_scale(osm_gps_map_osd_t *osd)
743    {
744        osd_priv_t *priv = (osd_priv_t*)osd->priv;
745        float m_per_pix = osm_gps_map_get_scale(OSM_GPS_MAP(osd->widget));
746    
747        /* first fill with transparency */
748        cairo_t *cr = cairo_create(priv->scale);
749        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
750        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
751        //    cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
752        cairo_paint(cr);
753        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
754    
755        /* determine the size of the scale width in meters */
756        float width = (OSD_SCALE_W-2) * m_per_pix;
757        printf("%d pixels is %f m\n", OSD_SCALE_W-2, width);
758    
759        /* scale this to useful values */
760        int exp = logf(width)*M_LOG10E;
761        int mant = width/pow(10,exp);
762        int width_metric = mant * pow(10,exp);
763        char *dist_str = dist_str_metric(width_metric);
764        width_metric /= m_per_pix;
765    
766        /* and now the hard part: scale for useful imperial values :-( */
767        /* try to convert to feet, 1ft == 0.3048 m */
768        width /= 0.3048;
769        float imp_scale = 0.3048;
770        char *dist_imp_unit = "ft";
771    
772        if(width >= 100) {
773            /* 1yd == 3 feet */
774            width /= 3.0;
775            imp_scale *= 3.0;
776            dist_imp_unit = "yd";
777    
778            if(width >= 1760.0) {
779                /* 1mi == 1760 yd */
780                width /= 1760.0;
781                imp_scale *= 1760.0;
782                dist_imp_unit = "mi";
783            }
784        }
785    
786        printf("this is %f %s\n", width, dist_imp_unit);
787        printf("this is %f pixels\n", width * imp_scale / m_per_pix);
788    
789        cairo_select_font_face (cr, "Sans",
790                                CAIRO_FONT_SLANT_NORMAL,
791                                CAIRO_FONT_WEIGHT_BOLD);
792        cairo_set_font_size (cr, OSD_SCALE_FONT_SIZE);
793        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
794    
795        cairo_text_extents_t extents;
796        cairo_text_extents (cr, dist_str, &extents);
797    
798        cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
799        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/3, OSD_SCALE_FONT_SIZE);
800        cairo_text_path (cr, dist_str);
801        cairo_set_line_width (cr, 2);
802        cairo_stroke (cr);
803    
804        cairo_set_source_rgb(cr, 0.0, 0.0, 0.0);
805        cairo_move_to (cr, OSD_SCALE_FONT_SIZE/3, OSD_SCALE_FONT_SIZE);
806        cairo_show_text (cr, dist_str);
807    
808        /* draw white line */
809        cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
810        cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
811        cairo_set_line_width (cr, 4);
812        cairo_move_to (cr, 2, 2*OSD_SCALE_FONT_SIZE/3);
813        cairo_rel_line_to (cr, 0,  2*OSD_SCALE_FONT_SIZE/3);
814        cairo_rel_line_to (cr, width_metric, 0);
815        cairo_rel_line_to (cr, 0, -2*OSD_SCALE_FONT_SIZE/3);
816        cairo_stroke(cr);
817    
818        /* draw black line */
819        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
820        cairo_set_line_width (cr, 2);
821        cairo_move_to (cr, 2, 2*OSD_SCALE_FONT_SIZE/3);
822        cairo_rel_line_to (cr, 0,  2*OSD_SCALE_FONT_SIZE/3);
823        cairo_rel_line_to (cr, width_metric, 0);
824        cairo_rel_line_to (cr, 0, -2*OSD_SCALE_FONT_SIZE/3);
825        cairo_stroke(cr);
826    
827        /* xyz */
828    
829        cairo_destroy(cr);
830    }
831    
832  static void  static void
833  osd_render(osm_gps_map_osd_t *osd) {  osd_render(osm_gps_map_osd_t *osd)
834    {
835      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
836    
837  #ifndef OSD_COLOR  #ifndef OSD_COLOR
# Line 787  osd_render(osm_gps_map_osd_t *osd) { Line 916  osd_render(osm_gps_map_osd_t *osd) {
916  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
917      osd_render_source_sel(osd);      osd_render_source_sel(osd);
918  #endif  #endif
919    
920    #ifdef OSD_SCALE
921        osd_render_scale(osd);
922    #endif
923  }  }
924    
925  static void  static void
# Line 808  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 941  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
941                                             OSD_S_W+2, OSD_S_H+2);                                             OSD_S_W+2, OSD_S_H+2);
942  #endif  #endif
943    
944    #ifdef OSD_SCALE
945            priv->scale =
946                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
947                                           OSD_SCALE_W+2, OSD_SCALE_H+2);
948    #endif
949    
950          /* ... and render it */          /* ... and render it */
951          osd_render(osd);          osd_render(osd);
952      }      }
# Line 847  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 986  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
986      cairo_paint(cr);      cairo_paint(cr);
987  #endif  #endif
988    
989    #ifdef OSD_SCALE
990        x =  OSD_X;
991        y = -OSD_Y;
992        if(x < 0) x += osd->widget->allocation.width - OSD_SCALE_W;
993        if(y < 0) y += osd->widget->allocation.height - OSD_SCALE_H;
994    
995        cairo_set_source_surface(cr, priv->scale, x, y);
996        cairo_paint(cr);
997    #endif
998    
999      cairo_destroy(cr);      cairo_destroy(cr);
1000  }  }
1001    
# Line 866  osd_free(osm_gps_map_osd_t *osd) Line 1015  osd_free(osm_gps_map_osd_t *osd)
1015           cairo_surface_destroy(priv->map_source);           cairo_surface_destroy(priv->map_source);
1016  #endif  #endif
1017    
1018    #ifdef OSD_SCALE
1019        if (priv->scale)
1020             cairo_surface_destroy(priv->scale);
1021    #endif
1022    
1023      g_free(priv);      g_free(priv);
1024  }  }
1025    

Legend:
Removed from v.88  
changed lines
  Added in v.102