Commit 076e9575 authored by John Sullivan's avatar John Sullivan
Browse files

Bookmarks window now saves and restores its size & position within and

across sessions. And to think I only had to modify 7 files!
parent 37a083b0
2000-01-06 Elliot Lee <sopwith@redhat.com>
2000-01-07 John Sullivan <sullivan@eazel.com>
Bookmarks window position is now saved & restored within
a session and across sessions. Some of this work can be
turned into general-purpose utilities but I didn't do
that yet (I will when we have a second need for it)
* src/nautilus-bookmarklist.c:
* src/nautilus-bookmarklist.h:
(nautilus_bookmarklist_set_window_geometry):
(nautilus_bookmarklist_get_window_geometry): New functions,
take/return a string representing the window position.
* src/nautilus-bookmarklist.c:
(nautilus_bookmarklist_load_file):
(nautilus_bookmarklist_save_file): read/write xml node for
window geometry in addition to bookmarks. Also cleaned up
a couple of leaked results from xmlGetProp and reformatted.
(set_window_geometry_internal): New utility function.
* src/nautilus-bookmarks-menu.c: (edit_bookmarks_cb): Moved
window-displaying logic out of this routine and into new
routine nautilus_bookmarks_window_present(), which is now
called here.
* src/nautilus-boomarks-menu.c:
* src/nautilus-boomarks-menu.h:
(nautilus_bookmarks_menu_exiting): New function, handles telling
bookmarks window to save itself when application exits.
* src/ntl-app.c: (nautilus_app_exiting): Now calls
nautilus_bookmarks_menu_exiting.
* src/nautilus.h: Now #includes "nautilus-bookmarks-menu.h"
* src/nautilus-bookmarks-window.h:
* src/nautilus-bookmarks-window.c:
(nautilus_bookmarks_window_present): New function, handles
presenting the bookmarks window on screen regardless of current state.
(nautilus_bookmarks_window_save_geometry): New function, reads window
position & size and passes them along to
nautilus_bookmarklist_set_window_geometry for safe-keeping.
* src/nautilus-bookmarks-window.c:
(nautilus_bookmarks_window_restore_geometry): New function, gets
window geometry from bookmarklist and sets window size & position
from it (after adjusting with some sanity checks)
(create_bookmarks_window): Replaced set_default_size call with
nautilus_bookmarks_window_restore_geometry call.
2000-01-07 Elliot Lee <sopwith@redhat.com>
* src/ntl-window.c, src/ntl-index-panel.c: Fix warnings.
......
......@@ -39,6 +39,7 @@ nautilus_app_exiting()
{
/* Do those things that gotta be done just once before quitting */
nautilus_prefs_save();
nautilus_bookmarks_menu_exiting();
}
void
......
......@@ -29,6 +29,7 @@
#include <gnome-xml/parser.h>
#include <gnome-xml/tree.h>
#include <gnome-xml/xmlmemory.h>
enum {
CONTENTS_CHANGED,
......@@ -44,11 +45,14 @@ static void destroy_bookmark (gpointer list_element,
static const gchar *nautilus_bookmarklist_get_file_path (NautilusBookmarklist *bookmarks);
static void nautilus_bookmarklist_load_file (NautilusBookmarklist *bookmarks);
static void nautilus_bookmarklist_save_file (NautilusBookmarklist *bookmarks);
static void set_window_geometry_internal (const gchar *string);
static GtkObjectClass *parent_class = NULL;
static guint bookmarklist_signals[LAST_SIGNAL] = { 0 };
static gchar *window_geometry = NULL;
const unsigned default_gnomad_directory_mode = 0755;
......@@ -261,6 +265,22 @@ nautilus_bookmarklist_get_file_path (NautilusBookmarklist *bookmarks)
return file_path;
}
/**
* nautilus_bookmarklist_get_window_geometry:
*
* Get a string representing the bookmarklist's window's geometry.
* This is the value set earlier by nautilus_bookmarklist_set_window_geometry.
* @bookmarks: the list of bookmarks associated with the window.
* Return value: string representation of window's geometry, suitable for
* passing to gnome_parse_geometry(), or NULL if
* no window geometry has yet been saved for this bookmarklist.
**/
const gchar *
nautilus_bookmarklist_get_window_geometry (NautilusBookmarklist *bookmarks)
{
return window_geometry;
}
/**
* nautilus_bookmarklist_insert_item:
*
......@@ -346,12 +366,27 @@ nautilus_bookmarklist_load_file (NautilusBookmarklist *bookmarks)
{
if (strcmp(node->name, "bookmark") == 0)
{
xmlChar *xml_name;
xmlChar *xml_uri;
/* Maybe should only accept bookmarks with both a name and uri? */
xml_name = xmlGetProp (node, "name");
xml_uri = xmlGetProp (node, "uri");
bookmarks->list = g_list_append(
bookmarks->list,
nautilus_bookmark_new(
xmlGetProp(node, "name"),
xmlGetProp(node, "uri")));
nautilus_bookmark_new(xml_name, xml_uri));
xmlFree (xml_name);
xmlFree (xml_uri);
}
else if (strcmp (node->name, "window") == 0)
{
xmlChar *geometry_string;
geometry_string = xmlGetProp (node, "geometry");
set_window_geometry_internal (geometry_string);
xmlFree (geometry_string);
}
node = node->next;
}
......@@ -390,8 +425,46 @@ nautilus_bookmarklist_save_file (NautilusBookmarklist *bookmarks)
doc = xmlNewDoc("1.0");
doc->root = xmlNewDocNode(doc, NULL, "bookmarks", NULL);
/* save window position */
if (window_geometry != NULL)
{
xmlNodePtr node;
node = xmlNewChild(doc->root, NULL, "window", NULL);
xmlSetProp (node, "geometry", window_geometry);
}
/* save bookmarks */
g_list_foreach (bookmarks->list, append_bookmark_node, doc->root);
xmlSaveFile(nautilus_bookmarklist_get_file_path(bookmarks), doc);
xmlFreeDoc(doc);
}
/**
* nautilus_bookmarklist_set_window_geometry:
*
* Set a bookmarks window's geometry (position & size), in string form. This is
* stored to disk by this class, and can be retrieved later in
* the same session or in a future session.
* @bookmarks: the list of bookmarks associated with the window.
* @geometry: the new window geometry string.
**/
void
nautilus_bookmarklist_set_window_geometry (NautilusBookmarklist *bookmarks,
const gchar *geometry)
{
g_return_if_fail (NAUTILUS_IS_BOOKMARKLIST (bookmarks));
g_return_if_fail (geometry != NULL);
set_window_geometry_internal (geometry);
nautilus_bookmarklist_save_file(bookmarks);
}
static void
set_window_geometry_internal (const gchar *string)
{
g_free (window_geometry);
window_geometry = g_strdup (string);
}
\ No newline at end of file
......@@ -54,6 +54,7 @@ struct _NautilusBookmarklistClass {
typedef struct _NautilusBookmarklistClass NautilusBookmarklistClass;
GtkType nautilus_bookmarklist_get_type (void);
NautilusBookmarklist *nautilus_bookmarklist_new (void);
......@@ -74,4 +75,10 @@ guint nautilus_bookmarklist_length (NautilusBookmarklist *bookmarks);
const NautilusBookmark *nautilus_bookmarklist_item_at (NautilusBookmarklist *bookmarks,
guint index);
void nautilus_bookmarklist_set_window_geometry
(NautilusBookmarklist *bookmarks,
const gchar *geometry);
const gchar *nautilus_bookmarklist_get_window_geometry
(NautilusBookmarklist *bookmarks);
#endif /* NAUTILUS_BOOKMARKLIST_H */
......@@ -139,22 +139,7 @@ bookmark_activated_cb(GtkMenuItem* item, gpointer func_data)
static void
edit_bookmarks_cb(GtkMenuItem* item, gpointer ignored)
{
GtkWidget *gtk_window = get_bookmarks_window();
if (GTK_WIDGET_VISIBLE(gtk_window))
{
/*
* Handle behind-other-windows and iconified cases.
* This successfully leaves the window in its original position
* on top of other windows, but unfortunately does not always leave the
* window with focus. Changing window focus programatically is an
* X no-no that we'd like to find a workaround for.
*/
gdk_window_show(gtk_window->window);
}
else
{
gtk_widget_show(gtk_window);
}
nautilus_bookmarks_window_present (get_bookmarks_window());
}
static GtkWidget *
......@@ -250,6 +235,18 @@ nautilus_bookmarks_menu_clear_bookmarks (NautilusBookmarksMenu *menu)
g_list_free(children);
}
/**
* nautilus_bookmarks_menu_exiting:
*
* Last chance to save state before app exits.
* Called when application exits; don't call from anywhere else.
**/
void
nautilus_bookmarks_menu_exiting ()
{
nautilus_bookmarks_window_save_geometry (get_bookmarks_window ());
}
/**
* nautilus_bookmarks_menu_fill:
*
......
......@@ -54,5 +54,6 @@ typedef struct _NautilusBookmarksMenuClass NautilusBookmarksMenuClass;
GtkType nautilus_bookmarks_menu_get_type (void);
GtkWidget *nautilus_bookmarks_menu_new (NautilusWindow *window);
void nautilus_bookmarks_menu_exiting (void);
#endif /* NAUTILUS_BOOKMARKS_MENU_H */
......@@ -44,6 +44,8 @@ static gint uri_field_changed_signalID;
static const NautilusBookmark *get_selected_bookmark (void);
static guint get_selected_row (void);
static gboolean get_selection_exists (void);
static void nautilus_bookmarks_window_restore_geometry
(GtkWidget *window);
static void on_bookmarklist_changed (NautilusBookmarklist *,
gpointer user_data);
static void on_name_field_changed (GtkEditable *, gpointer user_data);
......@@ -78,6 +80,12 @@ static void repopulate (void);
#define BOOKMARKS_WINDOW_INITIAL_WIDTH 500
#define BOOKMARKS_WINDOW_INITIAL_HEIGHT 200
/* Used for window position & size sanity-checking. The sizes are big enough to prevent
* at least normal-sized gnome panels from obscuring the window at the screen edges.
*/
#define MINIMUM_ON_SCREEN_WIDTH 100
#define MINIMUM_ON_SCREEN_HEIGHT 100
/**
* create_bookmarks_window:
......@@ -105,9 +113,6 @@ create_bookmarks_window(NautilusBookmarklist *list)
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width (GTK_CONTAINER (window), GNOME_PAD);
gtk_window_set_title (GTK_WINDOW (window), _("nautilus: Bookmarks"));
gtk_window_set_default_size (GTK_WINDOW (window),
BOOKMARKS_WINDOW_INITIAL_WIDTH,
BOOKMARKS_WINDOW_INITIAL_HEIGHT);
gtk_widget_set_usize (window,
BOOKMARKS_WINDOW_MIN_WIDTH,
BOOKMARKS_WINDOW_MIN_HEIGHT);
......@@ -255,6 +260,104 @@ get_selection_exists ()
return GTK_CLIST(bookmark_list_widget)->rows > 0;
}
/**
* nautilus_bookmarks_window_present:
*
* Present the bookmarks window on screen in the saved position and size.
* Brings window to front and activates it.
* @window: The bookmarks window to present on screen.
**/
void
nautilus_bookmarks_window_present (GtkWidget *window)
{
g_return_if_fail (GTK_IS_WINDOW (window));
if (GTK_WIDGET_VISIBLE(window))
{
/* Hide window first so it will reappear on top */
nautilus_bookmarks_window_save_geometry (window);
gtk_widget_hide (window);
}
else
{
gtk_widget_realize (window);
}
nautilus_bookmarks_window_restore_geometry (window);
gtk_widget_show (window);
}
static void
nautilus_bookmarks_window_restore_geometry (GtkWidget *window)
{
const gchar *window_geometry;
g_return_if_fail (GTK_IS_WINDOW (window));
g_return_if_fail (NAUTILUS_IS_BOOKMARKLIST (bookmarks));
window_geometry = nautilus_bookmarklist_get_window_geometry(bookmarks);
if (window_geometry != NULL)
{
gint left, top, width, height;
if (gnome_parse_geometry (window_geometry, &left, &top, &width, &height))
{
/* Adjust for sanity, in case screen size has changed or
* stored numbers are bogus. Make the entire window fit
* on the screen, so all controls can be reached. Also
* make sure the window isn't ridiculously small. Also
* make sure the top of the window is on screen, for
* draggability (perhaps not absolutely required, depending
* on window manager, but seems like a sensible rule anyway).
*/
width = CLAMP (width, BOOKMARKS_WINDOW_MIN_WIDTH, gdk_screen_width());
height = CLAMP (height, BOOKMARKS_WINDOW_MIN_HEIGHT, gdk_screen_height());
top = CLAMP (top, 0, gdk_screen_height() - MINIMUM_ON_SCREEN_HEIGHT);
/* FIXME: If window has negative left coordinate, set_uposition sends it
* somewhere else entirely. Not sure what level contains this bug (XWindows?).
* Hacked around by pinning the left edge to zero.
*/
left = CLAMP (left, 0, gdk_screen_width() - MINIMUM_ON_SCREEN_WIDTH);
gtk_widget_set_uposition (window, left, top);
gtk_window_set_default_size (GTK_WINDOW (window), width, height);
return;
}
}
/* fall through to default if necessary */
gtk_window_set_default_size (GTK_WINDOW (window),
BOOKMARKS_WINDOW_INITIAL_WIDTH,
BOOKMARKS_WINDOW_INITIAL_HEIGHT);
/* Let window manager handle default position if no position stored */
}
/**
* nautilus_bookmarks_window_save_geometry:
*
* Save window size & position to disk.
* @window: The bookmarks window whose geometry should be saved.
**/
void
nautilus_bookmarks_window_save_geometry (GtkWidget *window)
{
g_return_if_fail (GTK_IS_WINDOW (window));
g_return_if_fail (NAUTILUS_IS_BOOKMARKLIST (bookmarks));
/* Don't bother if window is already closed */
if (GTK_WIDGET_VISIBLE (window))
{
gchar *geometry_string;
geometry_string = gnome_geometry_string(window->window);
nautilus_bookmarklist_set_window_geometry (bookmarks, geometry_string);
g_free (geometry_string);
}
}
static void
on_bookmarklist_changed(NautilusBookmarklist *bookmarks, gpointer data)
......@@ -398,7 +501,9 @@ on_window_delete_event (GtkWidget *widget,
GdkEvent *event,
gpointer user_data)
{
g_return_val_if_fail(GTK_IS_WINDOW(widget), FALSE);
nautilus_bookmarks_window_save_geometry (widget);
/* Hide but don't destroy */
gtk_widget_hide(widget);
return TRUE;
......
......@@ -28,6 +28,8 @@
#include <gnome.h>
#include "nautilus-bookmarklist.h"
GtkWidget *create_bookmarks_window(NautilusBookmarklist *bookmarks);
GtkWidget *create_bookmarks_window (NautilusBookmarklist *bookmarks);
void nautilus_bookmarks_window_present (GtkWidget *window);
void nautilus_bookmarks_window_save_geometry (GtkWidget *window);
#endif /* NAUTILUS_BOOKMARKS_WINDOW_H */
......@@ -18,5 +18,6 @@
#include "ntl-uri-map.h"
#include "ntl-app.h"
#include "nautilus-bookmarks-menu.h"
#endif
......@@ -39,6 +39,7 @@ nautilus_app_exiting()
{
/* Do those things that gotta be done just once before quitting */
nautilus_prefs_save();
nautilus_bookmarks_menu_exiting();
}
void
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment