home · contact · privacy
Some shortening / re-styling of place_win()'s code and comments.
[plomrogue] / src / windows.c
index f71be6526af7d3322952369e57e312e581f2901f..fafa566edf670842173455564a3fb74dbb2b7673 100644 (file)
@@ -107,13 +107,13 @@ 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 top 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 not, fit w's top left to top right of last top predecessor. */
         struct Win * w_top = w->prev;
         while (w_top->start.y != 1)
         {
@@ -121,9 +121,7 @@ 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 to bottom left of its ->prev if enough space. */
         uint16_t w_prev_maxy = w->prev->start.y + w->prev->framesize.y;
         if (   w->framesize.x <= w->prev->framesize.x
             && w->framesize.y <  world.wmeta->padsize.y - w_prev_maxy)
@@ -132,36 +130,31 @@ 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 w' top left to the top right of the last
+         * predecessor w_test 1) not followed by windows with a left corner
+         * further rightwards than its own 2) with enough space rightwards for w
+         * until the bottom right of w_thr directly throning over it 3) and with
+         * this same space extending far 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_thr;
+            while (w_test != w_top)
             {
-                w_upup = w_up->prev;
-                while (1)
-                {
-                    if (w_up->start.y != w_upup->start.y)
-                    {
-                        break;
-                    }
-                    w_upup = w_upup->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)
+                w_thr = w_test->prev;
+                for (; w_test->start.y <= w_thr->start.y; w_thr = w_thr->prev);
+                uint16_t w_thr_bottom = w_thr->start.y + w_thr->framesize.y;
+                uint16_t free_width = (w_thr->start.x + w_thr->framesize.x)
+                                      - (w_test->start.x + w_test->framesize.x);
+                if (   w->framesize.y < world.wmeta->padsize.y - w_thr_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_thr_bottom + 1;
                     break;
                 }
-                w_up = w_upup;
+                w_test = w_thr;
             }
         }
     }