gEDA-dev: [PATCH] gschem: recent files support

Ivan Stankovic ivan.stankovic at fer.hr
Mon Apr 23 07:02:20 EDT 2007


On Sat, Apr 21, 2007 at 11:23:40AM +0100, Peter Clifton wrote:
> My strategy for dealing with the guile stuff is to find a similar thing
> I want to copy, then use grep to chase it through the various places it
> is implemented.
> 
> Good luck, and great work on the patch so far.

Thanks for the help, I appreciate it. Please see the new patch
attached.

-- 
Ivan Stankovic, ivan.stankovic at fer.hr

"Protect your digital freedom and privacy, eliminate DRM, 
learn more at http://www.defectivebydesign.org/what_is_drm"
-------------- next part --------------
diff -X dontdiff -ru orig-geda/gaf/gschem/include/prototype.h orig-geda-recent/gaf/gschem/include/prototype.h
--- orig-geda/gaf/gschem/include/prototype.h	2007-04-23 10:46:57.000000000 +0200
+++ orig-geda-recent/gaf/gschem/include/prototype.h	2007-04-23 11:36:33.000000000 +0200
@@ -834,6 +834,10 @@
 gint do_popup(TOPLEVEL *w_current, GdkEventButton *event);
 void x_menus_sensitivity(TOPLEVEL *w_current, const char *buf, int flag);
 void x_menus_popup_sensitivity(TOPLEVEL *w_current, const char *buf, int flag);
+void x_menu_attach_recent_files_submenu(TOPLEVEL *w_current);
+void recent_files_load();
+void recent_files_save();
+void recent_files_add(const char *filename);
 /* x_multiattrib.c */
 void x_multiattrib_open (TOPLEVEL *toplevel, OBJECT *object);
 /* x_multimulti.c */
diff -X dontdiff -ru orig-geda/gaf/gschem/lib/system-gschemrc.in orig-geda-recent/gaf/gschem/lib/system-gschemrc.in
--- orig-geda/gaf/gschem/lib/system-gschemrc.in	2007-04-23 10:46:58.000000000 +0200
+++ orig-geda-recent/gaf/gschem/lib/system-gschemrc.in	2007-04-23 11:14:30.000000000 +0200
@@ -1574,6 +1574,7 @@
 	   ("Save All" 		file-save-all 		file-save-all)
 	   ("Print..." 		file-print 		file-print)
 	   ("Write image..." 	file-image 		file-image)
+	   ("Recent files"      no-action 		no-action)
 	   ("SEPARATOR"         no-action 		no-action)
 	   ("Execute Script..." file-script 		file-script)
 	   ("SEPARATOR"         no-action 		no-action)
diff -X dontdiff -ru orig-geda/gaf/gschem/src/gschem.c orig-geda-recent/gaf/gschem/src/gschem.c
--- orig-geda/gaf/gschem/src/gschem.c	2007-04-22 10:38:28.000000000 +0200
+++ orig-geda-recent/gaf/gschem/src/gschem.c	2007-04-23 11:37:09.000000000 +0200
@@ -200,6 +200,10 @@
   }
   g_free(input_str);
 
+  /* Load recent files list. This must be done
+   * before calling x_window_setup(). */
+  recent_files_load();
+
   /* At end, complete set up of window. */
   colormap = gdk_colormap_get_system ();
   x_window_setup_colors();
diff -X dontdiff -ru orig-geda/gaf/gschem/src/x_menus.c orig-geda-recent/gaf/gschem/src/x_menus.c
--- orig-geda/gaf/gschem/src/x_menus.c	2007-02-10 22:25:31.000000000 +0100
+++ orig-geda-recent/gaf/gschem/src/x_menus.c	2007-04-23 12:51:54.000000000 +0200
@@ -363,3 +363,202 @@
     s_log_message(_("Tried to set the sensitivity on a non-existent popup menu_item\n")); 
   }
 }
+
+
+/* The list of recently loaded files. */
+static GList *recent_files = NULL;
+
+#define RECENT_FILES_STORE ".gschem-recent-files"
+
+static void recent_file_clicked(gpointer filename)
+{
+   PAGE *page;
+   TOPLEVEL *w;
+
+   w = s_toplevel_new();
+   x_window_setup(w);
+   page = x_window_open_page(w, (char *)filename);
+   x_window_set_current_page(w, page);
+   s_log_message (_("New Window created [%s]\n"), (char *)filename);
+}
+
+/*! \brief Attach a submenu with filenames to the 'Recent files'
+ *         menu item.
+ *
+ *  Called from x_window_setup().
+ */
+void x_menu_attach_recent_files_submenu(TOPLEVEL *w_current)
+{
+   gulong id;
+   GtkWidget *recent_menu_item, *recent_submenu;
+
+   recent_menu_item = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(
+            w_current->menubar), "File/Recent files");
+
+   /* disconnect all unblocked signals */
+   while(1) {
+      id = g_signal_handler_find(recent_menu_item, G_SIGNAL_MATCH_UNBLOCKED,
+            0, 0, NULL, NULL, NULL);
+      if(id == 0)
+         break;
+      gtk_signal_disconnect(recent_menu_item, id);
+   }
+
+   /* remove 'q' from the menu item string; there has to be a better way to 
+    * create a menu item without a hotkey being assigned to it automatically */
+   GtkWidget *label = gtk_bin_get_child(GTK_BIN(recent_menu_item));
+   gtk_label_set_text(GTK_LABEL(label), "Recent files");
+
+   recent_submenu = gtk_menu_new();
+   GList *p = recent_files;
+   while(p) {
+      GtkWidget *tmp = gtk_menu_item_new_with_label((gchar *)p->data);
+      gtk_signal_connect_object(GTK_OBJECT(tmp), "activate",
+            GTK_SIGNAL_FUNC (recent_file_clicked),
+            p->data);
+      gtk_menu_append(GTK_MENU(recent_submenu), tmp);
+      p = g_list_next(p);
+   }
+   gtk_widget_show_all(recent_submenu);
+   gtk_menu_item_set_submenu(GTK_MENU_ITEM(recent_menu_item), recent_submenu);
+}
+
+/*! \brief Add a filename to the list of recent files.
+ */
+void recent_files_add(const char *filename)
+{
+   gchar *basename;
+   GtkWidget *recent_menu_item, *recent_submenu;
+
+   basename = g_path_get_basename(filename);
+   if(strstr(basename, "untitled_") == basename) {
+      g_free(basename);
+      return;
+   }
+
+   g_free(basename);
+
+   /* check if it is a duplicate */
+   GList *p = recent_files;
+   while(p) {
+      if(strcmp((char *)p->data, filename) == 0 )
+         return;
+      p = g_list_next(p);
+   }
+
+   filename = g_strdup(filename);
+   recent_files = g_list_prepend(recent_files, (gpointer)filename);
+
+   /* walk through all toplevels and add the filename to the recent file submenu */
+   TOPLEVEL *w = global_window_current;
+   while(w->prev)
+      w = w->prev;
+
+   while(w) {
+      if(w->menubar == NULL) {
+         w = w->next;
+         continue;
+      }
+
+      recent_menu_item = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(
+               w->menubar), "File/Recent files");
+      recent_submenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(recent_menu_item));
+
+      GtkWidget *s = gtk_menu_item_new_with_label(filename);
+      gtk_widget_show(s);
+      gtk_menu_shell_prepend(GTK_MENU_SHELL(recent_submenu), s);
+      gtk_signal_connect_object(GTK_OBJECT(s), "activate",
+            GTK_SIGNAL_FUNC (recent_file_clicked),
+            (gpointer)filename);
+      w = w->next;
+   }
+}
+
+/*! \brief Make RECENT_FILES_STORE contain an empty file list.
+ */
+static void recent_files_create_empty()
+{
+   gchar *c;
+   const gchar * const tmp[] = { NULL };
+   GKeyFile *kf = g_key_file_new();
+   gchar *file = g_build_filename(g_get_home_dir(), RECENT_FILES_STORE, NULL);
+
+   g_key_file_set_string_list(kf, "Recent files", "Files", tmp, 0);
+   c = g_key_file_to_data(kf, NULL, NULL);
+   g_key_file_free(kf);
+
+   g_file_set_contents(file, c, -1, NULL);
+   g_free(c);
+   g_free(file);
+}
+
+/*! \brief Save the list of recent files to RECENT_FILES_STORE.
+ */
+void recent_files_save()
+{
+   gchar **files = NULL;
+   int num = 0;
+   gchar *c;
+   gchar *file = g_build_filename(g_get_home_dir(), RECENT_FILES_STORE, NULL);
+
+   GList *p = recent_files;
+   if(p == NULL) 
+      return;
+
+   while(p) {
+      files = g_realloc(files, (num + 1) * sizeof(gchar *));
+      files[num++] = (gchar *)p->data;
+      p = g_list_next(p);
+   }
+
+   GKeyFile *kf = g_key_file_new();
+
+   g_key_file_set_string_list(kf, "Recent files", "Files", 
+         (const gchar **)files, num);
+   c = g_key_file_to_data(kf, NULL, NULL);
+   g_file_set_contents(file, c, -1, NULL);
+
+   g_free(c);
+   g_free(file);
+   g_free(files);
+   g_key_file_free(kf);
+}
+
+/*! \brief Load the recent file list using data from
+ *         RECENT_FILES_STORE. 
+ *
+ *  Must be called before any other recent-files-related
+ *  functions.
+ */
+void recent_files_load()
+{
+   GKeyFile *kf = g_key_file_new();
+   gchar *file = g_build_filename(g_get_home_dir(), RECENT_FILES_STORE, NULL);
+
+again:
+   if(!g_key_file_load_from_file(kf, file, G_KEY_FILE_NONE, NULL)) {
+      /* error opening key file, create an empty one */
+      recent_files_create_empty();
+      goto again;
+   }
+
+   gsize len;
+   gchar **list = g_key_file_get_string_list(kf, "Recent files",
+         "Files", &len, NULL);
+
+   if(list == NULL) {
+      /* error reading key file, don't bother to correct;
+       * just overwrite it with an empty one */
+      recent_files_create_empty();
+      goto again;
+   }
+
+   while(len > 0) {
+      len--;
+      recent_files = g_list_prepend(recent_files, list[len]);
+   }
+
+   g_free(list);
+   g_free(file);
+   g_key_file_free(kf);
+}
diff -X dontdiff -ru orig-geda/gaf/gschem/src/x_window.c orig-geda-recent/gaf/gschem/src/x_window.c
--- orig-geda/gaf/gschem/src/x_window.c	2007-04-22 10:38:37.000000000 +0200
+++ orig-geda-recent/gaf/gschem/src/x_window.c	2007-04-23 12:48:04.000000000 +0200
@@ -157,6 +157,8 @@
 
   /* X related stuff */
   x_window_create_main (toplevel);
+
+  x_menu_attach_recent_files_submenu(toplevel);
 }
 
 /*! \todo Finish function documentation!!!
@@ -828,6 +830,7 @@
 
   /* just closed last window, so quit */
   if (last_window) {
+    recent_files_save();
     gschem_quit();
   }
   
@@ -950,6 +953,7 @@
    * it will get done in x_window_set_current_page(...)
    */
   x_pagesel_update (toplevel); /* ??? */
+  recent_files_add(filename);
 
   return page;
 }
@@ -1046,6 +1050,8 @@
     /* reset page CHANGED flag */
     page->CHANGED = 0;
 
+    /* update recent file list */
+    recent_files_add(filename);
   }
 
   /* log status of operation */


More information about the geda-dev mailing list