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 99 by harbaum, Mon Sep 7 19:22:02 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  100
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 456  osd_source_reallocate(osm_gps_map_osd_t Line 467  osd_source_reallocate(osm_gps_map_osd_t
467              const char *src = osm_gps_map_source_get_friendly_name(i);              const char *src = osm_gps_map_source_get_friendly_name(i);
468              cairo_text_extents (cr, src, &extents);              cairo_text_extents (cr, src, &extents);
469    
             //            printf("Source %d: %s = %f %f\n", i, src,  
             //                   extents.width, extents.height);  
   
470              if(extents.width > max_w) max_w = extents.width;              if(extents.width > max_w) max_w = extents.width;
471              if(extents.height > max_h) max_h = extents.height;              if(extents.height > max_h) max_h = extents.height;
472          }          }
# Line 477  osd_source_reallocate(osm_gps_map_osd_t Line 485  osd_source_reallocate(osm_gps_map_osd_t
485          cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);          cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w+2, h+2);
486    
487      osd_render_source_sel(osd);      osd_render_source_sel(osd);
   
488  }  }
489    
490  #define OSD_HZ      15  #define OSD_HZ      15
# Line 509  static gboolean osd_source_animate(gpoin Line 516  static gboolean osd_source_animate(gpoin
516    
517      /* 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 */
518    
     /* simple linear mapping */  
     //    priv->shift = (osd->widget->allocation.width - OSD_S_EXP_W + OSD_S_X) +  
     //        (diff * priv->count)/1000;  
   
519      /* nicer sinoid mapping */      /* nicer sinoid mapping */
520      float m = 0.5-cos(priv->count * M_PI / 1000.0)/2;      float m = 0.5-cos(priv->count * M_PI / 1000.0)/2;
521      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 556  osd_source_toggle(osm_gps_map_osd_t *osd
556      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);
557  }  }
558    
559    /* check if the user clicked inside the source selection area */
560  static osd_button_t  static osd_button_t
561  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) {
562      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 585  osd_source_check(osm_gps_map_osd_t *osd,
585      }      }
586    
587      /* check for clicks into data area */      /* check for clicks into data area */
588      if(priv->expanded) {      if(priv->expanded && !priv->handler_id) {
589            /* re-adjust from puller top to content top */
590            if(OSD_S_Y < 0)
591                y += OSD_S_EXP_H - OSD_S_PH;
592    
593          if(x > OSD_S_PW &&          if(x > OSD_S_PW &&
594             x < OSD_S_PW + OSD_S_EXP_W &&             x < OSD_S_PW + OSD_S_EXP_W &&
595             y > 0 &&             y > 0 &&
596             y < OSD_S_EXP_H) {             y < OSD_S_EXP_H) {
597    
598                int step = (priv->height - 2*OSD_TEXT_BORDER)
599                    / OSM_GPS_MAP_SOURCE_LAST;
600    
601              printf("in OSD source menu area\n");              y -= OSD_TEXT_BORDER - OSD_TEXT_SKIP;
602                y /= step;
603                y += 1;
604    
605                gint old = 0;
606                g_object_get(osd->widget, "map-source", &old, NULL);
607    
608                if(y > OSM_GPS_MAP_SOURCE_NULL &&
609                   y <= OSM_GPS_MAP_SOURCE_LAST &&
610                   old != y) {
611                    g_object_set(osd->widget, "map-source", y, NULL);
612    
613                    osd_render_source_sel(osd);
614                    osm_gps_map_repaint(OSM_GPS_MAP(osd->widget));
615                }
616    
617                /* return "clicked in OSD background" to prevent further */
618                /* processing by application */
619              return OSD_BG;              return OSD_BG;
620          }          }
621      }      }
# Line 701  osd_zoom_labels(cairo_t *cr, gint x, gin Line 728  osd_zoom_labels(cairo_t *cr, gint x, gin
728      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);      cairo_line_to (cr, x + Z_RIGHT + Z_LEN, y + Z_MID);
729  }  }
730    
731    static char *
732    dist_str_metric(int dist)
733    {
734        if(dist<1000)
735            return g_strdup_printf("%u m",  dist);
736    
737        return g_strdup_printf("%u km", dist/1000);
738    }
739    
740  static void  static void
741  osd_render(osm_gps_map_osd_t *osd) {  osd_render_scale(osm_gps_map_osd_t *osd)
742    {
743        osd_priv_t *priv = (osd_priv_t*)osd->priv;
744        float m_per_pix = osm_gps_map_get_scale(OSM_GPS_MAP(osd->widget));
745    
746        /* first fill with transparency */
747        cairo_t *cr = cairo_create(priv->scale);
748        cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
749        //    cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.0);
750        cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.2);
751        cairo_paint(cr);
752        cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
753    
754        /* determine the size of the scale width in meters */
755        float width = (OSD_SCALE_W-2) * m_per_pix;
756        printf("width = %f meters\n", width);
757    
758        /* scale this to useful values */
759        int exp = logf(width)*M_LOG10E;
760        int mant = width/pow(10,exp);
761        printf("mant = %d, exp = %d \n", mant, exp);
762    
763        int width_metric = mant * pow(10,exp);
764        char *dist_str = dist_str_metric(width_metric);
765        width_metric /= m_per_pix;
766        printf("metric scale width = %d pixels: %s\n", width_metric, dist_str);
767    
768        int font_size = 12;
769    
770        cairo_select_font_face (cr, "Sans",
771                                CAIRO_FONT_SLANT_NORMAL,
772                                CAIRO_FONT_WEIGHT_BOLD);
773        cairo_set_font_size (cr, font_size);
774        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
775    
776        cairo_text_extents_t extents;
777        cairo_text_extents (cr, dist_str, &extents);
778    
779        cairo_move_to (cr, font_size/3, font_size);
780        cairo_show_text (cr, dist_str);
781    
782        printf("height = %f, bearing = %f\n",  extents.height, extents.y_bearing);
783    
784        /* draw white line */
785        cairo_set_line_cap  (cr, CAIRO_LINE_CAP_ROUND);
786        cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
787        cairo_set_line_width (cr, 3);
788        cairo_move_to (cr, 2, 2*font_size/3);
789        cairo_rel_line_to (cr, 0,  2*font_size/3);
790        cairo_rel_line_to (cr, width_metric, 0);
791        cairo_rel_line_to (cr, 0, -2*font_size/3);
792        cairo_stroke(cr);
793    
794        /* draw black line */
795        cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
796        cairo_set_line_width (cr, 2);
797        cairo_move_to (cr, 2, 2*font_size/3);
798        cairo_rel_line_to (cr, 0,  2*font_size/3);
799        cairo_rel_line_to (cr, width_metric, 0);
800        cairo_rel_line_to (cr, 0, -2*font_size/3);
801        cairo_stroke(cr);
802    
803        /* xyz */
804    
805        cairo_destroy(cr);
806    }
807    
808    static void
809    osd_render(osm_gps_map_osd_t *osd)
810    {
811      osd_priv_t *priv = (osd_priv_t*)osd->priv;      osd_priv_t *priv = (osd_priv_t*)osd->priv;
812    
813  #ifndef OSD_COLOR  #ifndef OSD_COLOR
# Line 787  osd_render(osm_gps_map_osd_t *osd) { Line 892  osd_render(osm_gps_map_osd_t *osd) {
892  #ifdef OSD_SOURCE_SEL  #ifdef OSD_SOURCE_SEL
893      osd_render_source_sel(osd);      osd_render_source_sel(osd);
894  #endif  #endif
895    
896    #ifdef OSD_SCALE
897        osd_render_scale(osd);
898    #endif
899  }  }
900    
901  static void  static void
# Line 808  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 917  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
917                                             OSD_S_W+2, OSD_S_H+2);                                             OSD_S_W+2, OSD_S_H+2);
918  #endif  #endif
919    
920    #ifdef OSD_SCALE
921            priv->scale =
922                cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
923                                           OSD_SCALE_W+2, OSD_SCALE_H+2);
924    #endif
925    
926          /* ... and render it */          /* ... and render it */
927          osd_render(osd);          osd_render(osd);
928      }      }
# Line 847  osd_draw(osm_gps_map_osd_t *osd, GdkDraw Line 962  osd_draw(osm_gps_map_osd_t *osd, GdkDraw
962      cairo_paint(cr);      cairo_paint(cr);
963  #endif  #endif
964    
965    #ifdef OSD_SCALE
966        x =  OSD_X;
967        y = -OSD_Y;
968        if(x < 0) x += osd->widget->allocation.width - OSD_SCALE_W;
969        if(y < 0) y += osd->widget->allocation.height - OSD_SCALE_H;
970    
971        cairo_set_source_surface(cr, priv->scale, x, y);
972        cairo_paint(cr);
973    #endif
974    
975      cairo_destroy(cr);      cairo_destroy(cr);
976  }  }
977    
# Line 866  osd_free(osm_gps_map_osd_t *osd) Line 991  osd_free(osm_gps_map_osd_t *osd)
991           cairo_surface_destroy(priv->map_source);           cairo_surface_destroy(priv->map_source);
992  #endif  #endif
993    
994    #ifdef OSD_SCALE
995        if (priv->scale)
996             cairo_surface_destroy(priv->scale);
997    #endif
998    
999      g_free(priv);      g_free(priv);
1000  }  }
1001    

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