home · contact · privacy
Abstract pad scrolling; windows.c only checks for validity of new pad offset, doesn...
[plomrogue] / windows.c
index cf64b4e11fb0da2db2271929fde9b3de51f2466c..13dd161150df1b2988c9528b50876c768cf40302 100644 (file)
--- 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 (if possible) or right.
-  if      ('+' == dir)
-    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;
@@ -49,23 +42,34 @@ void append_window (struct WinMeta * win_meta, struct Win * win) {
   update_windows(win_meta, win); }
 
 void suspend_window (struct WinMeta * win_meta, struct Win * win) {
-// Destroy win, suspend from window chain. Update geometry of following rows, as well as activity selection.
+// Destroy win, suspend from chain. Update geometry of following rows and pad, as well as activity selection.
   destroy_window(win);
   if (win_meta->chain_start != win)    // Give win's position in the chain to element next to it in the chain.
     win->prev->next = win->next;
   else
     win_meta->chain_start = win->next;
+  char pad_refitted = 0;
   if (win_meta->chain_end != win) {                 // Let chain element next to win know its new predecessor.
     win->next->prev = win->prev;
     if (win_meta->active == win)                          // If win was active, shift active window pointer to
       win_meta->active = win->next;                       // the next chain element, if that is a window ...
-    update_windows(win_meta, win->next); }
+    update_windows(win_meta, win->next);
+    pad_refitted = 1; }
   else {
     win_meta->chain_end = win->prev;
     if (win_meta->active == win)                                       // ... or else to the previous element.
       win_meta->active = win->prev; }
   win->prev = 0;
-  win->next = 0; }
+  win->next = 0;
+  if (0 == pad_refitted) {                                                          // Refit pad if necessary.
+    uint16_t lastwincol = 0;
+    struct Win * win_p = win_meta->chain_start;
+    while (win_p != 0) {
+      if (getbegx(win_p->curses) + win_p->width > lastwincol + 1)
+        lastwincol = getbegx(win_p->curses) + win_p->width - 1;
+      win_p = win_p->next; }
+    if (getmaxx(win_meta->pad) != lastwincol)
+      wresize(win_meta->pad, getmaxy(win_meta->pad), lastwincol + 2); } }
 
 struct yx place_window (struct WinMeta * win_meta, struct Win * win) {
 // Based on position and sizes of previous window, find fitting place for current window.
@@ -113,8 +117,8 @@ void update_windows (struct WinMeta * win_meta, struct Win * win) {
     else if (win_p == win && startyx.x + win->width > lastwincol + 1)
       lastwincol = startyx.x + win->width - 1;
     win_p = win_p->next; }
-  if (getmaxx(win_meta->pad) != lastwincol) {
-    wresize(win_meta->pad, getmaxy(win_meta->pad), lastwincol + 2); }
+  if (getmaxx(win_meta->pad) != lastwincol)
+    wresize(win_meta->pad, getmaxy(win_meta->pad), lastwincol + 2);
   win->curses = subpad(win_meta->pad, win->height, win->width, startyx.y, startyx.x);
   if (0 != win->next)
     update_windows (win_meta, win->next); }
@@ -171,8 +175,24 @@ void draw_windows (struct Win * win) {
   if (0 != win->next) {
     draw_windows (win->next); } }
 
+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->height > (strlen(scrolldesc) + 1))
+    offset = (win_meta->height - strlen(scrolldesc)) / 2;
+  for (y = 0; y < win_meta->height; y++)
+    if (y >= offset && y < strlen(scrolldesc) + offset)
+      mvwaddch(win_meta->pad, y, x, scrolldesc[y - offset] | A_REVERSE);
+    else
+      mvwaddch(win_meta->pad, y, x, dir | A_REVERSE);
+  free(scrolldesc); }
+
 void draw_all_windows (struct WinMeta * win_meta) {
-// Draw all windows and their borders.
+// Draw pad with all windows and their borders, plus scrolling hints.
   erase();
   wnoutrefresh(win_meta->screen);
   werase(win_meta->pad);
@@ -191,8 +211,15 @@ void draw_all_windows (struct WinMeta * win_meta) {
       mvwaddch(win_meta->pad, all_corners[i].tr.y, all_corners[i].tr.x, '+');
       mvwaddch(win_meta->pad, all_corners[i].bl.y, all_corners[i].bl.x, '+');
       mvwaddch(win_meta->pad, all_corners[i].br.y, all_corners[i].br.x, '+'); }
-    pnoutrefresh(win_meta->pad, 0, win_meta->pad_offset, 0, 0, win_meta->height, win_meta->width - 1);
-    free(all_corners); }
+    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, '<');
+    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,
+                                  getmaxx(win_meta->pad) - (win_meta->pad_offset + win_meta->width), '>');
+    pnoutrefresh(win_meta->pad, 0, win_meta->pad_offset, 0, 0, win_meta->height, win_meta->width - 1); }
   doupdate(); }
 
 void resize_active_window (struct WinMeta * win_meta, uint16_t height, uint16_t width) {
@@ -253,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; }