home · contact · privacy
Fixed window positioning bug, cleared up code and comments in place_win().
[plomrogue] / src / windows.c
index c87b6cea98d6b1ebcdbd6cc7b285359af64cf2e0..b075b80a11bd02c8de5e538166c2ff8b24ec8350 100644 (file)
  */
 static void refit_pad();
 
-
-
 /* Update geometry (sizes, positions) of window "w" and its successors in the
  * window chain. For the positioning algorithm place_win() is used.
  */
 static void update_wins(struct Win * w);
 static void place_win(struct Win * w);
 
-
-
 /* Draw scroll hint (a line saying that there are "dist" more elements of
  * "unit" further into the direction symbolized by the "dir" char) into virtual
  * screen pad, onto an appropriate edge of either a window or the screen; the
@@ -43,13 +39,9 @@ static void scroll_hint(struct yx_uint16 fsize, char dir, uint16_t dist,
 static void winscroll_hint(struct Win * w, char dir, uint16_t dist);
 static void padscroll_hint(char dir, uint16_t dist);
 
-
-
 /* Draw contents of all windows in window chain from window "w" onwards. */
 static void draw_wins(struct Win * w);
 
-
-
 /* draw_win_borderlines() draws the vertical and horizontal borders of window
  * "w" sans corners into the virtual screen "pad", and draws the top border
  * line as the windows' title bar (highlighted if the window is described
@@ -67,8 +59,6 @@ static void draw_wins_borderlines(struct Win * w, struct Win * w_active,
                                   WINDOW * pad);
 static void draw_wins_bordercorners(struct Win * w, WINDOW * pad);
 
-
-
 /* Shift active window forwards / backwards in window chain. */
 static void shift_win_forward();
 static void shift_win_backward();
@@ -117,13 +107,15 @@ static void update_wins(struct Win * w)
 
 static void place_win(struct Win * w)
 {
-    /* First window goes into the upper-left corner. */
+    /* If w is first window, it goes into the upper-left corner. */
     w->start.x = 0;
     w->start.y = 1;                             /* Leave space for title bar. */
     if (0 != w->prev)
     {
 
-        /* Non-first window fallbacks to: fit rightwards of rightmost border. */
+        /* If w is not first winddow, fit its top left corner to the top right
+         * corner of w_top, the last predecessor window starting at the top.
+         */
         struct Win * w_top = w->prev;
         while (w_top->start.y != 1)
         {
@@ -131,8 +123,8 @@ static void place_win(struct Win * w)
         }
         w->start.x = w_top->start.x + w_top->framesize.x + 1;
 
-        /* Fit window below its predecessor if that one directly thrones over
-         * empty space wide and high enough.
+        /* Fit w's top left corner to bottom left corner of its immediate
+         * predecessor window if enough empty space is found there.
          */
         uint16_t w_prev_maxy = w->prev->start.y + w->prev->framesize.y;
         if (   w->framesize.x <= w->prev->framesize.x
@@ -142,36 +134,35 @@ static void place_win(struct Win * w)
             w->start.y = w_prev_maxy + 1;
         }
 
-        /* Failing that, try to open a new sub column below the nearest
-         * predecessor window that thrones over enough empty space.
+        /* Failing that, try to fit the top left corner to the top right corner
+         * of the last predecessor w_test 1) not followed by top windows or any
+         * other windows with a left corner further rightwards than its own left
+         * corner 2) with enough space rightwards for w until the bottom right
+         * corner of w_thro directly throning over it 3) and with this same
+         * space extending for enough to the bottom for fitting in w.
          */
         else
         {
-            struct Win * w_up = w->prev;
-            struct Win * w_upup = w_up;
-            uint16_t widthdiff;
-            while (w_up != w_top)
+            struct Win * w_test = w->prev;
+            struct Win * w_thro;
+            while (w_test != w_top)
             {
-                w_upup = w_up->prev;
-                while (1)
+                w_thro = w_test->prev;
+                while (w_test->start.y <= w_thro->start.y)
                 {
-                    if (w_up->start.y != w_upup->start.y)
-                    {
-                        break;
-                    }
-                    w_upup = w_upup->prev;
+                    w_thro = w_thro->prev;
                 }
-                w_prev_maxy = w_upup->start.y + w_upup->framesize.y;
-                widthdiff = (w_upup->start.x + w_upup->framesize.x)
-                            - (w_up->start.x + w_up->framesize.x);
-                if (   w->framesize.y < world.wmeta->padsize.y - w_prev_maxy
-                    && w->framesize.x < widthdiff)
+                uint16_t w_thro_bottom = w_thro->start.y + w_thro->framesize.y;
+                uint16_t free_width = (w_thro->start.x + w_thro->framesize.x)
+                                      - (w_test->start.x + w_test->framesize.x);
+                if (   w->framesize.y < world.wmeta->padsize.y - w_thro_bottom
+                    && w->framesize.x < free_width)
                 {
-                    w->start.x = w_up->start.x + w_up->framesize.x + 1 ;
-                    w->start.y = w_prev_maxy + 1;
+                    w->start.x = w_test->start.x + w_test->framesize.x + 1;
+                    w->start.y = w_thro_bottom + 1;
                     break;
                 }
-                w_up = w_upup;
+                w_test = w_thro;
             }
         }
     }