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

gEDA-cvs: CVS update: gschem.c



  User: peterb  
  Date: 07/05/05 08:40:45

  Modified:    .        gschem.c x_menus.c x_window.c
  Log:
  Add "Recent Files" list to gschem.
  
  
  
  Applied patch #1706411 by Ivan Stankovic, which adds a list of recently-
  
  accessed files to gschem's File menu.
  
  
  
  
  Revision  Changes    Path
  1.42      +4 -0      eda/geda/gaf/gschem/src/gschem.c
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: gschem.c
  ===================================================================
  RCS file: /home/cvspsrv/cvsroot/eda/geda/gaf/gschem/src/gschem.c,v
  retrieving revision 1.41
  retrieving revision 1.42
  diff -u -b -r1.41 -r1.42
  --- gschem.c	21 Apr 2007 23:05:36 -0000	1.41
  +++ gschem.c	5 May 2007 12:40:44 -0000	1.42
  @@ -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();
  
  
  
  1.42      +227 -0    eda/geda/gaf/gschem/src/x_menus.c
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: x_menus.c
  ===================================================================
  RCS file: /home/cvspsrv/cvsroot/eda/geda/gaf/gschem/src/x_menus.c,v
  retrieving revision 1.41
  retrieving revision 1.42
  diff -u -b -r1.41 -r1.42
  --- x_menus.c	10 Feb 2007 21:25:31 -0000	1.41
  +++ x_menus.c	5 May 2007 12:40:45 -0000	1.42
  @@ -22,6 +22,7 @@
   #include <string.h>
   #endif
   
  +#include <glib/gstdio.h>
   #include <libgeda/libgeda.h>
   
   #include "../include/globals.h"
  @@ -363,3 +364,229 @@
       s_log_message(_("Tried to set the sensitivity on a non-existent popup menu_item\n")); 
     }
   }
  +
  +#if !GLIB_CHECK_VERSION(2,6,0)
  +
  +/* disable recent files support */
  +inline void x_menu_attach_recent_files_submenu(TOPLEVEL *w_current)
  +{
  +   GtkWidget *recent_menu_item;
  +
  +   recent_menu_item = (GtkWidget *) gtk_object_get_data(GTK_OBJECT(
  +            w_current->menubar), "File/Recent files");
  +   gtk_widget_destroy(recent_menu_item);
  +}
  +
  +inline void recent_files_load() { }
  +inline void recent_files_save() { }
  +inline void recent_files_add(const char *filename) { }
  +
  +#else
  +
  +/* The list of recently loaded files. */
  +static GList *recent_files = NULL;
  +
  +#define RECENT_FILES_STORE ".gEDA/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);
  +
  +   if(!g_file_test(file, G_FILE_TEST_EXISTS)) {
  +      gchar *dir = g_build_filename(g_get_home_dir(), ".gEDA", NULL);
  +      g_mkdir(dir, S_IRWXU | S_IRWXG);
  +      g_free(dir);
  +
  +      recent_files_create_empty();
  +   }
  +
  +   if(!g_key_file_load_from_file(kf, file, G_KEY_FILE_NONE, NULL)) {
  +      /* error opening key file, create an empty one and try again */
  +      recent_files_create_empty();
  +      if(!g_key_file_load_from_file(kf, file, G_KEY_FILE_NONE, NULL))
  +         return;
  +   }
  +
  +   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();
  +      return;
  +   }
  +
  +   while(len > 0) {
  +      len--;
  +      recent_files = g_list_prepend(recent_files, list[len]);
  +   }
  +
  +   g_free(list);
  +   g_free(file);
  +   g_key_file_free(kf);
  +}
  +
  +#endif /* GLIB_MINOR_VERSION < 6 */
  
  
  
  1.54      +6 -0      eda/geda/gaf/gschem/src/x_window.c
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: x_window.c
  ===================================================================
  RCS file: /home/cvspsrv/cvsroot/eda/geda/gaf/gschem/src/x_window.c,v
  retrieving revision 1.53
  retrieving revision 1.54
  diff -u -b -r1.53 -r1.54
  --- x_window.c	21 Apr 2007 23:05:37 -0000	1.53
  +++ x_window.c	5 May 2007 12:40:45 -0000	1.54
  @@ -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 */
  
  
  


_______________________________________________
geda-cvs mailing list
geda-cvs@xxxxxxxxxxxxxx
http://www.seul.org/cgi-bin/mailman/listinfo/geda-cvs