]> git.street.me.uk Git - andy/viking.git/commitdiff
Store cursor info directly into tool struct
authorGuilhem Bonnefille <guilhem.bonnefille@gmail.com>
Sun, 16 Mar 2008 19:28:45 +0000 (19:28 +0000)
committerGuilhem Bonnefille <guilhem.bonnefille@gmail.com>
Sun, 16 Mar 2008 19:28:45 +0000 (19:28 +0000)
src/main.c
src/vikdemlayer.c
src/vikgeoreflayer.c
src/viklayer.c
src/viklayer.h
src/vikmapslayer.c
src/viktrwlayer.c
src/vikwindow.c
src/vikwindow.h

index 899548cb9198b60a25377478a627a2ea360a809d..b8ccaf2f1bcb2272ae18d1e14e02e0b55b45a432 100644 (file)
@@ -157,9 +157,6 @@ int main( int argc, char *argv[] )
   a_datasource_gc_init();
 #endif
 
-  vik_layer_cursors_init ();
-  vik_window_cursors_init ();
-
   /* Set the icon */
   main_icon = gdk_pixbuf_from_pixdata(&viking_icon, FALSE, NULL);
   gtk_window_set_default_icon(main_icon);
@@ -181,8 +178,6 @@ int main( int argc, char *argv[] )
   a_mapcache_uninit ();
   a_dems_uninit ();
   a_preferences_uninit ();
-  vik_layer_cursors_uninit ();
-  vik_window_cursors_uninit ();
 
   return 0;
 }
index ea57fca92f26f0690a6ebe1202ea5f068ee2fb37..ca295e8a245582119ec2ac3743134ccb0d7f112d 100644 (file)
@@ -115,7 +115,7 @@ static gboolean dem_layer_download_click ( VikDEMLayer *vdl, GdkEventButton *eve
 static VikToolInterface dem_tools[] = {
   { N_("DEM Download/Import"), (VikToolConstructorFunc) dem_layer_download_create, NULL, NULL, NULL,
     (VikToolMouseFunc) dem_layer_download_click, NULL,  (VikToolMouseFunc) dem_layer_download_release,
-    (VikToolKeyFunc) NULL, &cursor_demdl },
+    (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_demdl },
 };
 
 
index 53b62f6c0b6a956be8cdb97802d958c258813b9e..94cdaee90dd50d1628bed14187e41ae55939611e 100644 (file)
@@ -65,11 +65,11 @@ static gboolean georef_layer_zoom_press ( VikGeorefLayer *vgl, GdkEventButton *e
 static VikToolInterface georef_tools[] = {
   { N_("Georef Move Map"), (VikToolConstructorFunc) georef_layer_move_create, NULL, NULL, NULL,
     (VikToolMouseFunc) georef_layer_move_press, NULL, (VikToolMouseFunc) georef_layer_move_release,
-    (VikToolKeyFunc) NULL, &cursor_geomove },
+    (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_geomove },
 
   { N_("Georef Zoom Tool"), (VikToolConstructorFunc) georef_layer_zoom_create, NULL, NULL, NULL,
     (VikToolMouseFunc) georef_layer_zoom_press, NULL, NULL,
-    (VikToolKeyFunc) NULL, &cursor_geozoom },
+    (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_geozoom },
 };
 
 VikLayerInterface vik_georef_layer_interface = {
index 94b961e60364938b2cc320626efa9fd9be31d797..629a62c7a6967ee60bee7d5b8bc77627c30a327d 100644 (file)
@@ -476,54 +476,3 @@ static gboolean layer_properties_factory ( VikLayer *vl, VikViewport *vp )
   }
 }
 
-static GdkCursor ***layers_cursors;
-
-GdkCursor *vik_layer_get_tool_cursor ( gint layer_id, gint tool_id )
-{
-  if ( layer_id >= VIK_LAYER_NUM_TYPES )
-    return NULL;
-  if ( tool_id >= vik_layer_interfaces[layer_id]->tools_count )
-    return NULL;
-  return layers_cursors[layer_id][tool_id];
-}
-
-void vik_layer_cursors_init()
-{
-  gint i, j;
-  layers_cursors = g_malloc ( sizeof(GdkCursor **) * VIK_LAYER_NUM_TYPES );
-  for ( i = 0 ; i < VIK_LAYER_NUM_TYPES; i++ ) {
-    if ( vik_layer_interfaces[i]->tools_count ) {
-      layers_cursors[i] = g_malloc ( sizeof(GdkCursor *) *  vik_layer_interfaces[i]->tools_count );
-      for ( j = 0; j < vik_layer_interfaces[i]->tools_count; j++ ) {
-        if ( vik_layer_interfaces[i]->tools[j].cursor ) {
-          const GdkPixdata *cursor_pixdata = vik_layer_interfaces[i]->tools[j].cursor;
-          GError *cursor_load_err = NULL;
-          GdkPixbuf *cursor_pixbuf = gdk_pixbuf_from_pixdata (cursor_pixdata, FALSE, &cursor_load_err);
-          /* TODO: settable offeset */
-          GdkCursor *cursor = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 3, 3 );
-          layers_cursors[i][j] = cursor;
-
-          g_object_unref ( G_OBJECT(cursor_pixbuf) );
-        }
-        else
-          layers_cursors[i][j] = NULL;
-      }
-    } else
-      layers_cursors[i] = NULL;
-  }
-}
-
-void vik_layer_cursors_uninit()
-{
-  gint i, j;
-  for ( i = 0 ; i < VIK_LAYER_NUM_TYPES; i++ ) {
-    if ( vik_layer_interfaces[i]->tools_count ) {
-      for ( j = 0; j < vik_layer_interfaces[i]->tools_count; j++ ) {
-        if ( layers_cursors[i][j] )
-          gdk_cursor_unref ( layers_cursors[i][j] );
-      }
-      g_free ( layers_cursors[i] );
-    }
-  }
-  g_free ( layers_cursors );
-}
index ca6788c833bda62d9432a503f27a3b0404f326d6..12bd452811e18c8ebba38e959bbc64b9a1914bf1 100644 (file)
@@ -109,7 +109,9 @@ struct _VikToolInterface {
   VikToolMouseFunc move;
   VikToolMouseFunc release;
   VikToolKeyFunc key_press; /* return FALSE if we don't use the key press -- should return AFLSE most of the time if we want any shortcuts / UI keybindings to work! use sparingly. */
-  const GdkPixdata *cursor;
+  GdkCursorType cursor_type;
+  const GdkPixdata *cursor_data;
+  const GdkCursor *cursor;
 };
 
 /* Parameters (for I/O and Properties) */
@@ -280,11 +282,4 @@ VikLayer *vik_layer_get_and_reset_trigger();
 void vik_layer_emit_update_secondary ( VikLayer *vl ); /* to be called by aggregate layer only. doesn't set the trigger */
 void vik_layer_emit_update_although_invisible ( VikLayer *vl );
 
-GdkCursor *vik_layer_get_tool_cursor ( gint layer_id, gint tool_id );
-void vik_layer_cursors_init();
-void vik_layer_cursors_uninit();
-
-
-
-
 #endif
index 2b95c3eccd8d26502f277743ea9c61675ab16542..f7c5895229bc08743417454309398ca120d9aca8 100644 (file)
@@ -123,7 +123,7 @@ enum { PARAM_MAPTYPE=0, PARAM_CACHE_DIR, PARAM_ALPHA, PARAM_AUTODOWNLOAD, PARAM_
 static VikToolInterface maps_tools[] = {
   { N_("Maps Download"), (VikToolConstructorFunc) maps_layer_download_create, NULL, NULL, NULL,  
     (VikToolMouseFunc) maps_layer_download_click, NULL,  (VikToolMouseFunc) maps_layer_download_release,
-    (VikToolKeyFunc) NULL, &cursor_mapdl },
+    (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_mapdl },
 };
 
 VikLayerInterface vik_maps_layer_interface = {
index ec8a99d815e31040d96943066a08b5d676748a76..c968cf4dfd44962764ac95a73dca1068a35ca560 100644 (file)
@@ -295,30 +295,30 @@ static void highest_wp_number_remove_wp(VikTrwLayer *vtl, const gchar *old_wp_na
 
 static VikToolInterface trw_layer_tools[] = {
   { N_("Create Waypoint"), (VikToolConstructorFunc) tool_new_waypoint_create,    NULL, NULL, NULL, 
-    (VikToolMouseFunc) tool_new_waypoint_click,    NULL, NULL, (VikToolKeyFunc) NULL, &cursor_addwp },
+    (VikToolMouseFunc) tool_new_waypoint_click,    NULL, NULL, (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_addwp },
 
   { N_("Create Track"),    (VikToolConstructorFunc) tool_new_track_create,       NULL, NULL, NULL, 
     (VikToolMouseFunc) tool_new_track_click, (VikToolMouseFunc) tool_new_track_move, NULL,
-    (VikToolKeyFunc) tool_new_track_key_press, &cursor_addtr },
+    (VikToolKeyFunc) tool_new_track_key_press, GDK_CURSOR_IS_PIXMAP, &cursor_addtr },
 
   { N_("Begin Track"),    (VikToolConstructorFunc) tool_begin_track_create,       NULL, NULL, NULL, 
-    (VikToolMouseFunc) tool_begin_track_click,       NULL, NULL, (VikToolKeyFunc) NULL, &cursor_begintr },
+    (VikToolMouseFunc) tool_begin_track_click,       NULL, NULL, (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_begintr },
 
   { N_("Edit Waypoint"),   (VikToolConstructorFunc) tool_edit_waypoint_create,   NULL, NULL, NULL, 
     (VikToolMouseFunc) tool_edit_waypoint_click,   
     (VikToolMouseFunc) tool_edit_waypoint_move,
-    (VikToolMouseFunc) tool_edit_waypoint_release, (VikToolKeyFunc) NULL, &cursor_edwp },
+    (VikToolMouseFunc) tool_edit_waypoint_release, (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_edwp },
 
   { N_("Edit Trackpoint"), (VikToolConstructorFunc) tool_edit_trackpoint_create, NULL, NULL, NULL, 
     (VikToolMouseFunc) tool_edit_trackpoint_click,
     (VikToolMouseFunc) tool_edit_trackpoint_move,
-    (VikToolMouseFunc) tool_edit_trackpoint_release, (VikToolKeyFunc) NULL, &cursor_edtr },
+    (VikToolMouseFunc) tool_edit_trackpoint_release, (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_edtr },
 
   { N_("Show Picture"),    (VikToolConstructorFunc) tool_show_picture_create,    NULL, NULL, NULL, 
-    (VikToolMouseFunc) tool_show_picture_click,    NULL, NULL, (VikToolKeyFunc) NULL, &cursor_showpic },
+    (VikToolMouseFunc) tool_show_picture_click,    NULL, NULL, (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_showpic },
 
   { N_("Magic Scissors"),  (VikToolConstructorFunc) tool_magic_scissors_create,  NULL, NULL, NULL,
-    (VikToolMouseFunc) tool_magic_scissors_click, NULL, NULL, (VikToolKeyFunc) NULL, &cursor_iscissors },
+    (VikToolMouseFunc) tool_magic_scissors_click, NULL, NULL, (VikToolKeyFunc) NULL, GDK_CURSOR_IS_PIXMAP, &cursor_iscissors },
 };
 enum { TOOL_CREATE_WAYPOINT=0, TOOL_CREATE_TRACK, TOOL_BEGIN_TRACK, TOOL_EDIT_WAYPOINT, TOOL_EDIT_TRACKPOINT, TOOL_SHOW_PICTURE, NUM_TOOLS };
 
index 563227ab99abc8dac876d8b322980f6ee710d753..fdf78ce9d4413efadafc6bcf6182b23f48d1c9d5 100644 (file)
@@ -31,6 +31,7 @@
 #include "dems.h"
 #include "print.h"
 #include "preferences.h"
+#include "icons/icons.h"
 
 #include <stdlib.h>
 #include <math.h>
@@ -108,6 +109,7 @@ static toolbox_tools_t* toolbox_create(VikWindow *vw);
 static void toolbox_add_tool(toolbox_tools_t *vt, VikToolInterface *vti, gint layer_type );
 static int toolbox_get_tool(toolbox_tools_t *vt, const gchar *tool_name);
 static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name);
+static const GdkCursor *toolbox_get_cursor(toolbox_tools_t *vt, const gchar *tool_name);
 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event);
 static void toolbox_move (toolbox_tools_t *vt, GdkEventButton *event);
 static void toolbox_release (toolbox_tools_t *vt, GdkEventButton *event);
@@ -179,10 +181,6 @@ static guint window_signals[VW_LAST_SIGNAL] = { 0 };
 
 static gchar *tool_names[NUMBER_OF_TOOLS] = { N_("Pan"), N_("Zoom"), N_("Ruler") };
 
-GdkCursor *vw_cursor_pan = NULL;
-GdkCursor *vw_cursor_zoom = NULL;
-GdkCursor *vw_cursor_ruler = NULL;
-
 GType vik_window_get_type (void)
 {
   static GType vw_type = 0;
@@ -409,13 +407,16 @@ void vik_window_set_redraw_trigger(VikLayer *vl)
 
 static void window_configure_event ( VikWindow *vw )
 {
-  static first = 1;
+  static int first = 1;
   draw_redraw ( vw );
   if (first) {
     // This is a hack to set the cursor corresponding to the first tool
     // FIXME find the correct way to initialize both tool and its cursor
+    const GdkCursor *cursor = NULL;
     first = 0;
-    gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_pan );
+    cursor = toolbox_get_cursor(vw->vt, "Pan");
+    /* We set cursor, even if it is NULL: it resets to default */
+    gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, cursor );
   }
 }
 
@@ -874,7 +875,10 @@ static VikToolInterface ruler_tool =
     (VikToolActivationFunc) ruler_deactivate, 
     (VikToolMouseFunc) ruler_click, 
     (VikToolMouseFunc) ruler_move, 
-    (VikToolMouseFunc) ruler_release };
+    (VikToolMouseFunc) ruler_release,
+    NULL,
+    GDK_CURSOR_IS_PIXMAP,
+    &cursor_ruler };
 /*** end ruler code ********************************************************/
 
 
@@ -917,7 +921,10 @@ static VikToolInterface zoom_tool =
     (VikToolActivationFunc) NULL,
     (VikToolMouseFunc) zoomtool_click, 
     (VikToolMouseFunc) zoomtool_move,
-    (VikToolMouseFunc) zoomtool_release };
+    (VikToolMouseFunc) zoomtool_release,
+    NULL,
+    GDK_CURSOR_IS_PIXMAP,
+    &cursor_zoom };
 /*** end zoom code ********************************************************/
 
 /********************************************************************************
@@ -958,7 +965,9 @@ static VikToolInterface pan_tool =
     (VikToolActivationFunc) NULL,
     (VikToolMouseFunc) pantool_click, 
     (VikToolMouseFunc) pantool_move,
-    (VikToolMouseFunc) pantool_release };
+    (VikToolMouseFunc) pantool_release,
+    NULL,
+    GDK_FLEUR };
 /*** end pan code ********************************************************/
 
 static void draw_pan_cb ( GtkAction *a, VikWindow *vw )
@@ -1172,6 +1181,24 @@ static void toolbox_activate(toolbox_tools_t *vt, const gchar *tool_name)
   vt->active_tool = tool;
 }
 
+static const GdkCursor *toolbox_get_cursor(toolbox_tools_t *vt, const gchar *tool_name)
+{
+  int tool = toolbox_get_tool(vt, tool_name);
+  toolbox_tool_t *t = &vt->tools[tool];
+  if (t->ti.cursor == NULL) {
+    if (t->ti.cursor_type == GDK_CURSOR_IS_PIXMAP && t->ti.cursor_data != NULL) {
+      GError *cursor_load_err = NULL;
+      GdkPixbuf *cursor_pixbuf = gdk_pixbuf_from_pixdata (t->ti.cursor_data, FALSE, &cursor_load_err);
+      /* TODO: settable offeset */
+      t->ti.cursor = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 3, 3 );
+      g_object_unref ( G_OBJECT(cursor_pixbuf) );
+    } else {
+      t->ti.cursor = gdk_cursor_new ( t->ti.cursor_type );
+    }
+  }
+  return t->ti.cursor;
+}
+
 static void toolbox_click (toolbox_tools_t *vt, GdkEventButton *event)
 {
   VikLayer *vl = vik_layers_panel_get_selected ( vt->vw->viking_vlp );
@@ -1214,20 +1241,22 @@ static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
 {
   /* White Magic, my friends ... White Magic... */
   int layer_id, tool_id;
+  const GdkCursor *cursor = NULL;
 
   toolbox_activate(vw->vt, gtk_action_get_name(a));
 
+  cursor = toolbox_get_cursor(vw->vt, gtk_action_get_name(a));
+  /* We set cursor, even if it is NULL: it resets to default */
+  gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, cursor );
+
   if (!strcmp(gtk_action_get_name(a), "Pan")) {
     vw->current_tool = TOOL_PAN;
-    gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_pan );
   } 
   else if (!strcmp(gtk_action_get_name(a), "Zoom")) {
     vw->current_tool = TOOL_ZOOM;
-    gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_zoom );
   } 
   else if (!strcmp(gtk_action_get_name(a), "Ruler")) {
     vw->current_tool = TOOL_RULER;
-    gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vw_cursor_ruler );
   }
   else {
     /* TODO: only enable tools from active layer */
@@ -1237,7 +1266,6 @@ static void menu_tool_cb ( GtkAction *old, GtkAction *a, VikWindow *vw )
            vw->current_tool = TOOL_LAYER;
            vw->tool_layer_id = layer_id;
            vw->tool_tool_id = tool_id;
-           gdk_window_set_cursor ( GTK_WIDGET(vw->viking_vvp)->window, vik_layer_get_tool_cursor ( layer_id, tool_id ) );
        }
       }
     }
@@ -2055,7 +2083,6 @@ static void window_create_ui( VikWindow *window )
 
 
 
-#include "icons/icons.h"
 static struct { 
   const GdkPixdata *data;
   gchar *stock_id;
@@ -2092,27 +2119,3 @@ register_vik_icons (GtkIconFactory *icon_factory)
   }
 }
 
-void vik_window_cursors_init()
-{
-  GdkPixbuf *cursor_pixbuf;
-  GError *cursor_load_err;
-
-  vw_cursor_pan = gdk_cursor_new(GDK_FLEUR);
-
-  cursor_pixbuf = gdk_pixbuf_from_pixdata (&cursor_zoom, FALSE, &cursor_load_err);
-  vw_cursor_zoom = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 6, 6 );
-
-  g_object_unref ( G_OBJECT(cursor_pixbuf) );
-
-  cursor_pixbuf = gdk_pixbuf_from_pixdata (&cursor_ruler, FALSE, &cursor_load_err);
-  vw_cursor_ruler = gdk_cursor_new_from_pixbuf ( gdk_display_get_default(), cursor_pixbuf, 6, 6 );
-
-  g_object_unref ( G_OBJECT(cursor_pixbuf) );
-}
-
-void vik_window_cursors_uninit()
-{
-  gdk_cursor_unref ( vw_cursor_pan );
-  gdk_cursor_unref ( vw_cursor_zoom );
-  gdk_cursor_unref ( vw_cursor_ruler );
-}
index 89cdcdfdf1609425ac034988a015a9917c82e085..682b776dc1141ff3f8d4b9beb30db29504abba95 100644 (file)
@@ -57,9 +57,6 @@ void vik_window_selected_layer(VikWindow *vw, struct _VikLayer *vl);
 struct _VikViewport * vik_window_viewport(VikWindow *vw);
 void vik_window_set_redraw_trigger(struct _VikLayer *vl);
 
-void vik_window_cursors_init();
-void vik_window_cursors_uninit();
-
 void vik_window_enable_layer_tool ( VikWindow *vw, gint layer_id, gint tool_id );
 
 G_END_DECLS