From a2bafe4a71fbf31baa32b00581b2a744e96753af Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 10 Jun 2013 09:46:28 +0200
Subject: [PATCH] Abstract pad scrolling; windows.c only checks for validity of
 new pad offset, doesn't generate the new offset itself.

---
 roguelike.c |  7 +++++++
 roguelike.h |  1 +
 windows.c   | 14 ++++++--------
 windows.h   |  2 +-
 4 files changed, 15 insertions(+), 9 deletions(-)

diff --git a/roguelike.c b/roguelike.c
index 6ae5243..f648e00 100644
--- a/roguelike.c
+++ b/roguelike.c
@@ -72,6 +72,13 @@ void toggle_window (struct WinMeta * win_meta, struct Win * win) {
   else
     append_window(win_meta, win); }
 
+void scroll_pad (struct WinMeta * win_meta, char dir) {
+// Try to scroll pad left or right.
+  if      ('+' == dir)
+    reset_pad_offset(win_meta, win_meta->pad_offset + 1);
+  else if ('-' == dir)
+    reset_pad_offset(win_meta, win_meta->pad_offset - 1); }
+
 void growshrink_active_window (struct WinMeta * win_meta, char change) {
 // Grow or shrink active window horizontally or vertically by one cell size.
   if (0 != win_meta->active) {
diff --git a/roguelike.h b/roguelike.h
index fcca90d..3fdbbaf 100644
--- a/roguelike.h
+++ b/roguelike.h
@@ -31,6 +31,7 @@ uint32_t read_uint32_bigendian(FILE * file);
 void write_uint32_bigendian(uint32_t x, FILE * file);
 void save_game(struct World *);
 void toggle_window (struct WinMeta *, struct Win *);
+void scroll_pad (struct WinMeta *, char);
 void growshrink_active_window (struct WinMeta *, char);
 struct Map init_map ();
 void map_scroll (struct Map *, char);
diff --git a/windows.c b/windows.c
index 3f9cda8..13dd161 100644
--- a/windows.c
+++ b/windows.c
@@ -17,13 +17,6 @@ struct WinMeta init_win_meta (WINDOW * screen) {
   win_meta.active = 0;
   return win_meta; }
 
-void scroll_pad (struct WinMeta * win_meta, char dir) {
-// Scroll pad left or right (if possible).
-  if      ('+' == dir && win_meta->pad_offset + win_meta->width < getmaxx(win_meta->pad) - 1)
-    win_meta->pad_offset++;
-  else if ('-' == dir && win_meta->pad_offset > 0)
-    win_meta->pad_offset--; }
-
 struct Win init_window (struct WinMeta * win_meta, char * title, void * data, void * func) {
 // Create and populate Win struct with sane default values.
   struct Win win;
@@ -221,7 +214,7 @@ void draw_all_windows (struct WinMeta * win_meta) {
     free(all_corners);
     uint16_t y;
     if (win_meta->pad_offset > 0)
-        draw_vertical_scroll_hint(win_meta, win_meta->pad_offset, win_meta->pad_offset + 1, '<');
+      draw_vertical_scroll_hint(win_meta, win_meta->pad_offset, win_meta->pad_offset + 1, '<');
     if (win_meta->pad_offset + win_meta->width < getmaxx(win_meta->pad) - 1)
       for (y = 0; y < win_meta->height; y++)
         draw_vertical_scroll_hint(win_meta, win_meta->pad_offset + win_meta->width - 1,
@@ -287,3 +280,8 @@ void shift_active_window (struct WinMeta * win_meta, char dir) {
           append_window(win_meta, wins[i]);
     free(wins);
     win_meta->active = win_shift; } }
+
+void reset_pad_offset(struct WinMeta * win_meta, uint16_t new_offset) {
+// Apply new_offset to windows pad, if it proves to be sane.
+  if (new_offset >= 0 && new_offset + win_meta->width < getmaxx(win_meta->pad))
+    win_meta->pad_offset = new_offset; }
diff --git a/windows.h b/windows.h
index 522f26f..df69731 100644
--- a/windows.h
+++ b/windows.h
@@ -29,7 +29,6 @@ struct Corners {
   struct yx br; };
 
 struct  WinMeta init_win_meta (WINDOW *);
-void scroll_pad (struct WinMeta *, char);
 struct Win init_window (struct WinMeta *, char *, void *, void *);
 void append_window (struct WinMeta *, struct Win *);
 void suspend_window (struct WinMeta *, struct Win *);
@@ -44,3 +43,4 @@ void draw_all_windows (struct WinMeta *);
 void resize_active_window (struct WinMeta *, uint16_t, uint16_t);
 void cycle_active_window (struct WinMeta *, char);
 void shift_active_window (struct WinMeta *, char);
+void reset_pad_offset (struct WinMeta *, uint16_t);
-- 
2.30.2