2009/3/4 Rob Kendrick <rjek@netsurf-browser.org>
On Wed, 4 Mar 2009 00:16:01 +0100
Pawe³ Blokus <pblokus@gmail.com> wrote:

> It's me again :)
>
> I've spent some time getting more into NS code and tried to add some
> functionality by the way :) That's the outcome:
>
> http://pastebin.com/f77e252d7

Can you please email this patch to the list, so it is not lost?  Thanks.


Index: desktop/history_core.c
===================================================================
--- desktop/history_core.c    (wersja 6688)
+++ desktop/history_core.c    (kopia robocza)
 -29,6 +29,7 @@
 #include "content/content.h"
 #include "content/urldb.h"
 #include "css/css.h"
+#include "desktop/browser.h"
 #include "desktop/gui.h"
 #include "desktop/history_core.h"
 #include "desktop/plotters.h"
 -63,6 +64,8 @@
     int x;  /**< Position of node. */
     int y;  /**< Position of node. */
     struct bitmap *bitmap;  /**< Thumbnail bitmap, or 0. */
+    int sx; /**< X scroll offset. */
+    int sy; /**< Y scroll offset. */   
 };
 
 /** History tree for a window. */
 -257,6 +260,8 @@
     entry->forward = entry->forward_pref = entry->forward_last = 0;
     entry->children = 0;
     entry->bitmap = 0;
+    entry->sx = -1;
+    entry->sy = -1;
     if (history->current) {
         if (history->current->forward_last)
             history->current->forward_last->next = entry;
 -455,6 +460,10 @@
     else
         url = entry->page.url;
 
+    /* the scroll offsets have to be remembered before changing
+    history->current */
+    history_update_current_scroll(bw);
+   
     if (new_window) {
         current = history->current;
         history->current = entry;
 -717,3 +726,43 @@
 
     return 0;
 }
+
+/**
+ * Save in the history the scroll offsets of the current page
+ * \param  bw    browser window with the page
+ */
+
+void history_update_current_scroll(struct browser_window *bw)
+{
+    int sx, sy;
+    struct history *history = bw->history;
+   
+    if (!history || !history->current)
+        return;
+   
+    gui_window_get_scroll(bw->window, &sx, &sy);
+    history->current->sx = sx;
+    history->current->sy = sy;
+    printf("setting scroll\t%i\t%i for %s\n", sx, sy, history->current->page.url);
+}
+
+/**
+ * Load from the history the scroll offsets of the current page
+ * \param  bw    browser window with the page
+ * \param  sx    updated to x offset
+ * \param  sy    updated to y offset
+ * \return     true on success
+ */
+
+bool history_get_current_scroll(struct browser_window *bw, int *sx, int *sy)
+{
+    struct history *history = bw->history;
+   
+    if (!history || !history->current)
+        return false;
+   
+    *sx = history->current->sx;
+    *sy = history->current->sy;
+    printf("getting scroll\t%i\t%i for %s\n", *sx, *sy, history->current->page.url);   
+    return true;
+}
Index: desktop/history_core.h
===================================================================
--- desktop/history_core.h    (wersja 6688)
+++ desktop/history_core.h    (kopia robocza)
 -44,5 +44,6 @@
 bool history_click(struct browser_window *bw, struct history *history,
         int x, int y, bool new_window);
 const char *history_position_url(struct history *history, int x, int y);
-
+void history_update_current_scroll(struct browser_window *bw);
+bool history_get_current_scroll(struct browser_window *bw, int *sx, int *sy);
 #endif
Index: desktop/browser.c
===================================================================
--- desktop/browser.c    (wersja 6688)
+++ desktop/browser.c    (kopia robocza)
 -313,7 +313,12 @@
         free(url2);
         return;
     }
-
+    /* we have to remember the scroll offsets for the current page
+    if we don't return to a page in the history (otherwise it's done by the
+    history code)*/
+    if (history_add)
+        history_update_current_scroll(bw);
+   
     free(bw->frag_id);
     bw->frag_id = NULL;
 
 -766,7 +771,7 @@
         bool scroll_to_top)
 {
     struct box *pos;
-    int x, y;
+    int x, y, sx, sy;
 
     if (!bw->current_content)
         return;
 -778,12 +783,17 @@
 
     gui_window_update_extent(bw->window);
 
-    if (scroll_to_top)
+    if (scroll_to_top) {
         gui_window_set_scroll(bw->window, 0, 0);
-
-    /* todo: don't do this if the user has scrolled */
+        sx = -1;
+    } else if (!history_get_current_scroll(bw, &sx, &sy))
+        sx = -1;
+   
+    /* if the page was scrolled before return to that position */
+    if (sx != -1) {
+        gui_window_set_scroll(bw->window, sx, sy);   
     /* if frag_id exists, then try to scroll to it */
-    if (bw->frag_id && bw->current_content->type == CONTENT_HTML) {
+    } else if (bw->frag_id && bw->current_content->type == CONTENT_HTML) {
         if ((pos = box_find_by_id(bw->current_content->data.html.layout, bw->frag_id)) != 0) {
             box_coords(pos, &x, &y);
             gui_window_set_scroll(bw->window, x, y);