desktop: open a file via xdg-open without additional output window
authorFriedrich Beckmann <friedrich.beckmann@gmx.de>
Sun, 28 Jun 2020 16:46:43 +0000 (18:46 +0200)
committerFriedrich Beckmann <friedrich.beckmann@gmx.de>
Sun, 28 Jun 2020 16:46:43 +0000 (18:46 +0200)
Currently there is a difference between opening an additional data
file via the Edit->Open menu or by opening a file via xdg-open (same
for doubleclick on Filebrowser). In the latter case the on_open
callback is called which runs an additional post_initialize. That
results in an additional output window.

I changed the behavior such that post_initialize is only called
when there is no psppire window yet. That happens when psppire
is started from a terminal with the file as argument on linux.
Further I check if there is an empty datawindow and replace that
in the same way as it is done via the Edit->Open menu.

src/ui/gui/main.c
src/ui/gui/psppire.c
src/ui/gui/psppire.h

index cb5519b5f3ba2e3f0e15e189a432b2ad3b49d8b2..9ca6d3836d1d72c9a24d734076bf4106be7f00fd 100644 (file)
@@ -238,7 +238,6 @@ wait_for_splash (GApplication *app, GtkWindow *x)
     }
 }
 
-
 static void
 on_activate (GApplication * app, gpointer ud)
 {
@@ -250,15 +249,50 @@ on_activate (GApplication * app, gpointer ud)
   wait_for_splash (app, x);
 }
 
+GtkWindow *
+find_empty_data_window (GApplication *app)
+{
+  GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
+  while (wl)
+    {
+      if (wl->data && PSPPIRE_IS_DATA_WINDOW (GTK_WINDOW (wl->data)) &&
+          psppire_data_window_is_empty (PSPPIRE_DATA_WINDOW (wl->data)))
+        return GTK_WINDOW (wl->data);
+      wl = wl->next;
+    }
+  return NULL;
+}
+
+GtkWindow *
+find_psppire_window (GApplication *app)
+{
+  GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
+  while (wl)
+    {
+      if (wl->data && PSPPIRE_IS_WINDOW (GTK_WINDOW (wl->data)))
+        return GTK_WINDOW (wl->data);
+      wl = wl->next;
+    }
+  return NULL;
+}
 
 static void
 on_open (GApplication *app, GFile **files, gint n_files, gchar * hint,
          gpointer ud)
 {
-  post_initialise (app);
+  /* If the application is already open and we open another file
+     via xdg-open on linux or via the file manager, then open is
+     called. Check if we already have a psppire window. */
+  if (find_psppire_window (app) == NULL)
+    post_initialise (app);
+
+  /* When a new data file is opened, then try to find an empty
+     data window which will then be replaced as in the open file
+     dialog */
+  GtkWindow *victim = find_empty_data_window (app);
 
   gchar *file = g_file_get_parse_name (files[0]);
-  GtkWindow *x = psppire_preload_file (file);
+  GtkWindow *x = psppire_preload_file (file, victim);
   g_free (file);
 
   wait_for_splash (app, x);
index ea8250a6b9314092d7efa084fe83734f58cb7cf7..9181d7e571b963575c2aea5ec00e4a6bf9beb2a6 100644 (file)
@@ -175,7 +175,7 @@ psppire_set_lexer (struct lexer *lexer)
 
 
 GtkWindow *
-psppire_preload_file (const gchar *file)
+psppire_preload_file (const gchar *file, GtkWindow *victim)
 {
   const gchar *local_encoding = "UTF-8";
 
@@ -191,7 +191,7 @@ psppire_preload_file (const gchar *file)
   /* Check to see if the file is a .sav or a .por file.  If not
      assume that it is a syntax file */
   if (retval == 1)
-    w = open_data_window (NULL, filename, NULL, NULL);
+    w = open_data_window (PSPPIRE_WINDOW (victim), filename, NULL, NULL);
   else if (retval == 0)
     {
       char *error = spv_detect (filename);
index 311e7c19ad4f37ae9f962d86da98044c1606a007..f56052fb7d7ce0452b1c53054d4f4a4b6eee9141 100644 (file)
@@ -48,7 +48,7 @@ void psppire_set_lexer (struct lexer *);
 
 void register_selection_functions (void);
 
-GtkWindow * psppire_preload_file (const gchar *file);
+GtkWindow * psppire_preload_file (const gchar *file, GtkWindow *victim);
 
 
 #endif /* PSPPIRE_H */