+static rectList calcSpace(const OpenboxWindow &win, const rectList &spaces) {
+ rectList result;
+ rectList::const_iterator siter;
+ for(siter=spaces.begin(); siter!=spaces.end(); ++siter) {
+ if(win.getArea().Intersect(*siter)) {
+ //Check for space to the left of the window
+ if(win.getXFrame() > siter->x())
+ result.push_back(Rect(siter->x(), siter->y(),
+ win.getXFrame() - siter->x() - 1,
+ siter->h()));
+ //Check for space above the window
+ if(win.getYFrame() > siter->y())
+ result.push_back(Rect(siter->x(), siter->y(),
+ siter->w(),
+ win.getYFrame() - siter->y() - 1));
+ //Check for space to the right of the window
+ if((win.getXFrame()+win.getWidth()) <
+ (siter->x()+siter->w()))
+ result.push_back(Rect(win.getXFrame() + win.getWidth() + 1,
+ siter->y(),
+ siter->x() + siter->w() -
+ win.getXFrame() - win.getWidth() - 1,
+ siter->h()));
+ //Check for space below the window
+ if((win.getYFrame()+win.getHeight()) <
+ (siter->y()+siter->h()))
+ result.push_back(Rect(siter->x(),
+ win.getYFrame() + win.getHeight() + 1,
+ siter->w(),
+ siter->y() + siter->h()-
+ win.getYFrame() - win.getHeight() - 1));
+
+ }
+ else
+ result.push_back(*siter);
+ }
+ return result;
+}
+
+//BestFitPlacement finds the smallest free space that fits the window
+//to be placed. It currentl ignores whether placement is right to left or top
+//to bottom.
+Point *Workspace::bestFitPlacement(const Size &win_size, const Rect &space)
+{
+ const Rect *best;
+ rectList spaces;
+ LinkedListIterator<OpenboxWindow> it(windowList);
+ rectList::const_iterator siter;
+ spaces.push_back(space); //initially the entire screen is free
+ it.reset();
+
+ //Find Free Spaces
+ for (OpenboxWindow *cur=it.current(); cur!=NULL; it++, cur=it.current())
+ spaces = calcSpace(*cur, spaces);
+
+ //Find first space that fits the window
+ best = 0;
+ for (siter=spaces.begin(); siter!=spaces.end(); ++siter) {
+ if ((siter->w() >= win_size.w()) &&
+ (siter->h() >= win_size.h()))
+ best = siter;
+ }
+
+ if (best != 0)
+ return new Point(best->origin());
+ else
+ return new Point(200, 0);
+}
+
+inline Point *Workspace::rowSmartPlacement(const Size &win_size,
+ const Rect &space){
+ bool placed=false;
+ int test_x, test_y, place_x = 0, place_y = 0;
+ int start_pos = 0;
+ int change_y =
+ ((screen->getColPlacementDirection() == BScreen::TopBottom) ? 1 : -1);
+ int change_x =
+ ((screen->getRowPlacementDirection() == BScreen::LeftRight) ? 1 : -1);
+ int delta_x = 8, delta_y = 8;
+ LinkedListIterator<OpenboxWindow> it(windowList);
+
+ test_y = (screen->getColPlacementDirection() == BScreen::TopBottom) ?
+ start_pos : screen->getHeight() - win_size.h() - start_pos;
+
+ while(!placed &&
+ ((screen->getColPlacementDirection() == BScreen::BottomTop) ?
+ test_y > 0 : test_y + win_size.h() < (signed) space.h())) {
+ test_x = (screen->getRowPlacementDirection() == BScreen::LeftRight) ?
+ start_pos : space.w() - win_size.w() - start_pos;
+ while (!placed &&
+ ((screen->getRowPlacementDirection() == BScreen::RightLeft) ?
+ test_x > 0 : test_x + win_size.w() < (signed) space.w())) {
+ placed = true;
+
+ it.reset();
+ for (OpenboxWindow *curr = it.current(); placed && curr;
+ it++, curr = it.current()) {
+ int curr_w = curr->getWidth() + (screen->getBorderWidth() * 4);
+ int curr_h =
+ ((curr->isShaded()) ? curr->getTitleHeight() : curr->getHeight()) +
+ (screen->getBorderWidth() * 4);
+
+ if (curr->getXFrame() < test_x + win_size.w() &&
+ curr->getXFrame() + curr_w > test_x &&
+ curr->getYFrame() < test_y + win_size.h() &&
+ curr->getYFrame() + curr_h > test_y) {
+ placed = false;
+ }
+ }
+
+ // Removed code for checking toolbar and slit
+ // The space passed in should not include either
+
+ if (placed) {
+ place_x = test_x;
+ place_y = test_y;
+
+ break;
+ }
+
+ test_x += (change_x * delta_x);
+ }
+
+ test_y += (change_y * delta_y);
+ }
+ return new Point(place_x, place_y);
+}
+