From 5cf8ee7f5a9867ebeae9122f37d292f9c9205b20 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Sat, 15 Jun 2013 02:13:03 +0200
Subject: [PATCH] Unite all scroll hint drawing functions into one.

---
 draw_wins.c | 20 ++------------------
 draw_wins.h |  1 -
 windows.c   | 47 ++++++++++++++++++++++++++++-------------------
 windows.h   |  1 +
 4 files changed, 31 insertions(+), 38 deletions(-)

diff --git a/draw_wins.c b/draw_wins.c
index 9fa64c9..818037a 100644
--- a/draw_wins.c
+++ b/draw_wins.c
@@ -99,22 +99,6 @@ void draw_info_win (struct Win * win) {
   snprintf(text, 100, "Turn: %d", count);
   draw_with_linebreaks(win, text, 0); }
 
-void draw_horizontal_scroll_hint (struct Win * win, uint16_t y, uint32_t more_lines, char dir) {
-// Draw scroll hint line in win at row y, announce more_lines more lines in direction dir.
-  uint16_t x, offset;
-  char phrase[] = "more lines";
-  char * scrolldesc = malloc((3 * sizeof(char)) + strlen(phrase) + 10);  // 10 = max chars for uint32_t string
-  sprintf(scrolldesc, " %d %s ", more_lines, phrase);
-  offset = 1;
-  if (win->frame.size.x > (strlen(scrolldesc) + 1))
-    offset = (win->frame.size.x - strlen(scrolldesc)) / 2;
-  for (x = 0; x < win->frame.size.x; x++)
-    if (x >= offset && x < strlen(scrolldesc) + offset)
-      mvwaddch(win->frame.curses_win, y, x, scrolldesc[x - offset] | A_REVERSE);
-    else
-      mvwaddch(win->frame.curses_win, y, x, dir | A_REVERSE);
-  free(scrolldesc); }
-
 void draw_keys_win (struct Win * win) {
 // Draw keybindings window.
   struct World * world = (struct World *) win->data;
@@ -130,10 +114,10 @@ void draw_keys_win (struct Win * win) {
   attr_t attri;
   for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++) {
     if (0 == y && offset > 0) {
-      draw_horizontal_scroll_hint (win, y, offset + 1, '^');
+      draw_scroll_hint(&win->frame, y, offset + 1, '^');
       continue; }
     else if (win->frame.size.y == y + 1 && 0 < world->keyswindata->max - (win->frame.size.y + offset - 1)) {
-      draw_horizontal_scroll_hint (win, y, world->keyswindata->max - (offset + win->frame.size.y) + 2, 'v');
+      draw_scroll_hint(&win->frame, y, world->keyswindata->max - (offset + win->frame.size.y) + 2, 'v');
       continue; }
     attri = 0;
     if (y == world->keyswindata->select - offset) {
diff --git a/draw_wins.h b/draw_wins.h
index 20b5676..64f332c 100644
--- a/draw_wins.h
+++ b/draw_wins.h
@@ -3,5 +3,4 @@ void draw_text_from_bottom (struct Win *, char *);
 void draw_log_win (struct Win *);
 void draw_map_win (struct Win *);
 void draw_info_win (struct Win *);
-void draw_horizontal_scroll_hint (struct Win *, uint16_t, uint32_t, char);
 void draw_keys_win (struct Win *);
diff --git a/windows.c b/windows.c
index 92e3323..e128ef5 100644
--- a/windows.c
+++ b/windows.c
@@ -17,7 +17,6 @@ static void destroy_window (struct Win *);
 static void draw_windows_borders (struct Win *, struct Win *, struct Corners *, uint16_t);
 static void draw_window_borders (struct Win *, char);
 static void draw_windows (struct Win *);
-static void draw_vertical_scroll_hint (struct WinMeta *, uint16_t, uint32_t, char);
 
 extern struct WinMeta init_win_meta (WINDOW * screen) {
 // Create and populate WinMeta struct with sane default values.
@@ -186,21 +185,32 @@ static void draw_windows (struct Win * win) {
   if (0 != win->next) {
     draw_windows (win->next); } }
 
-static void draw_vertical_scroll_hint (struct WinMeta * win_meta, uint16_t x, uint32_t more_cols, char dir) {
-// Draw scroll hint line in win at col x of pad display, announce more_cols more columns in direction dir.
-  uint16_t y, offset;
-  char phrase[] = "more columns";
-  char * scrolldesc = malloc((3 * sizeof(char)) + strlen(phrase) + 10);  // 10 = max chars for uint32_t string
-  sprintf(scrolldesc, " %d %s ", more_cols, phrase);
-  offset = 1;
-  if (win_meta->pad.size.y > (strlen(scrolldesc) + 1))
-    offset = (win_meta->pad.size.y - strlen(scrolldesc)) / 2;
-  for (y = 0; y < win_meta->pad.size.y; y++)
-    if (y >= offset && y < strlen(scrolldesc) + offset)
-      mvwaddch(win_meta->pad.curses_win, y, x, scrolldesc[y - offset] | A_REVERSE);
+extern void draw_scroll_hint (struct Frame * frame, uint16_t pos, uint32_t dist, char dir) {
+// Draw scroll hint into frame at pos (row or col dependend on dir), mark distance of dist cells into dir.
+  char more[] = "more";
+  char unit_cols[] = "columns";
+  char unit_rows[] = "lines";
+  uint16_t dsc_space = frame->size.x;
+  char * unit = unit_rows;
+  if ('<' == dir || '>' == dir) {
+    dsc_space = frame->size.y;
+    unit = unit_cols; }
+  char * scrolldsc = malloc((4 * sizeof(char)) + strlen(more) + strlen(unit) + 10); // 10: strlen for uint32_t
+  sprintf(scrolldsc, " %d %s %s ", dist, more, unit);
+  char offset = 1, q;
+  if (dsc_space > strlen(scrolldsc) + 1)
+    offset = (dsc_space - strlen(scrolldsc)) / 2;
+  chtype symbol;
+  for (q = 0; q < dsc_space; q++) {
+    if (q >= offset && q < strlen(scrolldsc) + offset)
+      symbol = scrolldsc[q - offset] | A_REVERSE;
     else
-      mvwaddch(win_meta->pad.curses_win, y, x, dir | A_REVERSE);
-  free(scrolldesc); }
+      symbol = dir | A_REVERSE;
+    if ('<' == dir || '>' == dir)
+      mvwaddch(frame->curses_win, q, pos, symbol);
+    else
+      mvwaddch(frame->curses_win, pos, q, symbol); }
+  free(scrolldsc); }
 
 extern void draw_all_windows (struct WinMeta * win_meta) {
 // Draw pad with all windows and their borders, plus scrolling hints.
@@ -225,12 +235,11 @@ extern 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_scroll_hint(&win_meta->pad, win_meta->pad_offset, win_meta->pad_offset + 1, '<');
     if (win_meta->pad_offset + win_meta->pad.size.x < getmaxx(win_meta->pad.curses_win) - 1)
       for (y = 0; y < win_meta->pad.size.y; y++)
-        draw_vertical_scroll_hint(win_meta, win_meta->pad_offset + win_meta->pad.size.x - 1,
-                                  getmaxx(win_meta->pad.curses_win) - (win_meta->pad_offset
-                                                                       + win_meta->pad.size.x),
+        draw_scroll_hint(&win_meta->pad, win_meta->pad_offset + win_meta->pad.size.x - 1,
+                         getmaxx(win_meta->pad.curses_win) - (win_meta->pad_offset + win_meta->pad.size.x),
                                   '>');
     pnoutrefresh(win_meta->pad.curses_win, 0, win_meta->pad_offset, 0, 0, win_meta->pad.size.y,
                  win_meta->pad.size.x - 1); }
diff --git a/windows.h b/windows.h
index 757386a..2080ef3 100644
--- a/windows.h
+++ b/windows.h
@@ -27,6 +27,7 @@ extern struct WinMeta init_win_meta (WINDOW *);
 extern struct Win init_window (struct WinMeta *, char *, void *, void *);
 extern void append_window (struct WinMeta *, struct Win *);
 extern void suspend_window (struct WinMeta *, struct Win *);
+extern void draw_scroll_hint (struct Frame *, uint16_t, uint32_t, char);
 extern void draw_all_windows (struct WinMeta *);
 extern void resize_active_window (struct WinMeta *, uint16_t, uint16_t);
 extern void cycle_active_window (struct WinMeta *, char);
-- 
2.30.2