[dia] [meta info] Implement "<ObjectMenu>/Follow Link..."



commit 9f855a3ee4f0310f2e7f0109a3bc483c450bb0a2
Author: Hans Breuer <hans breuer org>
Date:   Thu Aug 5 20:13:54 2010 +0200

    [meta info] Implement "<ObjectMenu>/Follow Link..."
    
    From the Object::meta::url it is now possible to follow links
    either internally (just diagram opening) or via the shell. The
    former is used when given meta string with either absolute or
    relative path, but without some protocol prefix.
    If there is some protocol prefix the string is interpreted as
    external link an passed to gtk_show_uri (ShellExecute on win32)
    otherwise the new internal function dia_file_open is used to
    import and open the file.

 app/commands.c       |   32 ++++++++++++++++++++++++++------
 app/commands.h       |    3 +++
 app/disp_callbacks.c |   40 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+), 6 deletions(-)
---
diff --git a/app/commands.c b/app/commands.c
index 5a8bf83..be5cde4 100644
--- a/app/commands.c
+++ b/app/commands.c
@@ -699,17 +699,21 @@ help_manual_callback (GtkAction *action)
   g_free(helpindex);
 }
 
-static void 
-activate_url (GtkAboutDialog *about,
-              const gchar    *link,
-	      gpointer        data)
+void 
+activate_url (GtkWidget   *parent,
+              const gchar *link,
+	      gpointer     data)
 {
 #ifdef G_OS_WIN32
   ShellExecuteA (0, "open", link, NULL, NULL, SW_SHOWNORMAL);
 #else
 # if GTK_CHECK_VERSION(2,14,0)
   GdkScreen *screen;
-  screen = gtk_widget_get_screen (GTK_WIDGET(about));
+  
+  if (parent)
+    screen = gtk_widget_get_screen (GTK_WIDGET(parent));
+  else
+    screen = gdk_screen_get_default ();
   gtk_show_uri(screen, link, gtk_get_current_event_time (), NULL);
 # else
   gchar *command = getenv("BROWSER");
@@ -743,7 +747,7 @@ help_about_callback (GtkAction *action)
   gchar *filename = g_build_filename (dirname, "dia-splash.png", NULL);
   GdkPixbuf *logo = gdk_pixbuf_new_from_file(filename, NULL);
 
-  gtk_about_dialog_set_url_hook (activate_url, NULL, NULL);
+  gtk_about_dialog_set_url_hook ((GtkAboutDialogActivateLinkFunc)activate_url, NULL, NULL);
   gtk_show_about_dialog (NULL,
 	"logo", logo,
         "name", "Dia",
@@ -1238,4 +1242,20 @@ objects_align_v_callback (GtkAction *action)
   undo_set_transactionpoint(dia->undo);
 }
 
+/*! Open a file and show it in a new display */
+void
+dia_file_open (const gchar *filename,
+               DiaImportFilter *ifilter)
+{
+  Diagram *diagram;
+
+  if (!ifilter)
+    ifilter = filter_guess_import_filter(filename);
 
+  diagram = diagram_load(filename, ifilter);
+  if (diagram != NULL) {
+    diagram_update_extents(diagram);
+    layer_dialog_set_diagram(diagram);
+    new_display(diagram);
+  }
+}
diff --git a/app/commands.h b/app/commands.h
index 6a06e27..f37147e 100644
--- a/app/commands.h
+++ b/app/commands.h
@@ -30,6 +30,8 @@ void file_preferences_callback (GtkAction *action);
 void help_manual_callback (GtkAction *action);
 void help_about_callback  (GtkAction *action);
 
+void activate_url (GtkWidget *parent, const gchar *url, gpointer data);
+
 void edit_copy_callback       (GtkAction *action);
 void edit_cut_callback        (GtkAction *action);
 void edit_paste_callback      (GtkAction *action);
@@ -89,5 +91,6 @@ void objects_align_v_callback           (GtkAction *action);
 void dialogs_properties_callback (GtkAction *action);
 void dialogs_layers_callback     (GtkAction *action);
 
+void dia_file_open (const gchar *filename, DiaImportFilter *ifilter);
 
 #endif /* COMMANDS_H */
diff --git a/app/disp_callbacks.c b/app/disp_callbacks.c
index 19e5127..b9a25fd 100644
--- a/app/disp_callbacks.c
+++ b/app/disp_callbacks.c
@@ -44,6 +44,7 @@
 #include "highlight.h"
 #include "textedit.h"
 #include "lib/parent.h"
+#include "dia_dirs.h"
 
 /* This contains the point that was clicked to get this menu */
 static Point object_menu_clicked_point;
@@ -128,7 +129,45 @@ add_properties_menu_item (GtkMenu *menu, gboolean separator)
   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
   gtk_widget_show(menu_item);
 }
+static void
+_follow_link_callback (GtkAction *action, gpointer data)
+{
+  DiaObject *obj;
+  DDisplay *ddisp = ddisplay_active();
+  gchar *url;
+
+  if (!ddisp) return;
+
+  obj = (DiaObject *)ddisp->diagram->data->selected->data;
+
+  url = dia_object_get_meta (obj, "url");
+
+  if (!url) return;
 
+  if (strstr (url, "://") == NULL) {
+    if (!g_path_is_absolute(url)) {
+      gchar *p = NULL;
+      
+      if (ddisp->diagram->filename)
+        p = dia_absolutize_filename (ddisp->diagram->filename, url);
+      if (p) {
+	g_free (url);
+	url = p;
+      }
+    }
+    dia_file_open (url, NULL);
+  } else
+    activate_url (GTK_WIDGET (ddisp->shell), url, NULL);
+  g_free (url);
+}
+static void
+add_follow_link_menu_item (GtkMenu *menu)
+{
+  GtkWidget *menu_item = gtk_menu_item_new_with_label(_("Follow linkâ?¦"));
+  g_signal_connect(GTK_OBJECT(menu_item), "activate", G_CALLBACK(_follow_link_callback), NULL);
+  gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
+  gtk_widget_show(menu_item);
+}
 static void
 create_object_menu(DiaMenu *dia_menu)
 {
@@ -190,6 +229,7 @@ create_object_menu(DiaMenu *dia_menu)
 
   /* Finally add a Properties... menu item for objects*/
   add_properties_menu_item(GTK_MENU (menu), i > 0);
+  add_follow_link_menu_item(GTK_MENU (menu));
 
   dia_menu->app_data = menu;
   dia_menu->app_data_free = dia_menu_free;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]