aboutsummaryrefslogtreecommitdiff
path: root/src/MinOverlapPlacement.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/MinOverlapPlacement.cc')
-rw-r--r--src/MinOverlapPlacement.cc182
1 files changed, 182 insertions, 0 deletions
diff --git a/src/MinOverlapPlacement.cc b/src/MinOverlapPlacement.cc
new file mode 100644
index 0000000..121109f
--- /dev/null
+++ b/src/MinOverlapPlacement.cc
@@ -0,0 +1,182 @@
1// MinOverlapPlacement.cc
2// Copyright (c) 2007 Fluxbox Team (fluxgen at fluxbox dot org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR (*it)
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR (*it)WISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR (*it)
20// DEALINGS IN THE SOFTWARE.
21
22// $Id$
23
24#include "MinOverlapPlacement.hh"
25
26#include "Window.hh"
27#include "Screen.hh"
28
29ScreenPlacement::PlacementPolicy MinOverlapPlacement::s_policy = ScreenPlacement::ROWMINOVERLAPPLACEMENT;
30ScreenPlacement::RowDirection MinOverlapPlacement::s_row_dir = ScreenPlacement::LEFTRIGHT;
31ScreenPlacement::ColumnDirection MinOverlapPlacement::s_col_dir = ScreenPlacement::TOPBOTTOM;
32
33MinOverlapPlacement::MinOverlapPlacement(ScreenPlacement::PlacementPolicy policy) {
34 s_policy = policy;
35}
36
37bool MinOverlapPlacement::placeWindow(
38 const std::list<FluxboxWindow *> &windowlist,
39 const FluxboxWindow &win, int &place_x, int &place_y) {
40
41 // view (screen + head) constraints
42 int head = (signed) win.screen().getCurrHead();
43 int head_left = (signed) win.screen().maxLeft(head);
44 int head_right = (signed) win.screen().maxRight(head);
45 int head_top = (signed) win.screen().maxTop(head);
46 int head_bot = (signed) win.screen().maxBottom(head);
47
48 const ScreenPlacement &screen_placement =
49 dynamic_cast<const ScreenPlacement &>(win.screen().placementStrategy());
50 s_row_dir = screen_placement.rowDirection();
51 s_col_dir = screen_placement.colDirection();
52
53 int win_w = win.width() + win.fbWindow().borderWidth()*2 + win.widthOffset();
54 int win_h = win.height() + win.fbWindow().borderWidth()*2 + win.heightOffset();
55
56 // we keep a set of open spaces on the desktop, sorted by size/location
57 std::set<Region> region_set;
58
59 // initialize the set of regions to contain the entire head
60 region_set.insert(Region(Region::TOPLEFT, head_left, head_top));
61 region_set.insert(Region(Region::TOPRIGHT, head_right - win_w, head_top));
62 region_set.insert(Region(Region::BOTTOMLEFT, head_left, head_bot - win_h));
63 region_set.insert(Region(Region::BOTTOMRIGHT, head_right - win_w,
64 head_bot - win_h));
65
66 // go through the list of windows, creating other reasonable placements
67 // at the end, we'll find the one with minimum overlap
68 // the size of this set is at most 2(n+2)(n+1) (n = number of windows)
69 // finding overlaps is therefore O(n^3), but it can probably be improved
70 std::list<FluxboxWindow *>::const_reverse_iterator it = windowlist.rbegin(),
71 it_end = windowlist.rend();
72 for (; it != it_end; ++it) {
73
74 // get the dimensions of the window
75 int bottom = (*it)->y() + (*it)->height() +
76 2*(*it)->frame().window().borderWidth();
77 int top = (*it)->y();
78 int right = (*it)->x() + (*it)->width() +
79 2*(*it)->frame().window().borderWidth();
80 int left = (*it)->x();
81
82 // go through the list of regions
83 // if this window overlaps that region and the new window still fits,
84 // it will create new regions to test
85 std::set<Region>::iterator reg_it = region_set.begin();
86 for (; reg_it != region_set.end(); ++reg_it) {
87
88 switch (reg_it->corner) {
89 case Region::TOPLEFT:
90 if (right > reg_it->x && bottom > reg_it->y) {
91 if (bottom + win_h <= head_bot)
92 region_set.insert(Region(Region::TOPLEFT,
93 reg_it->x, bottom));
94 if (right + win_w <= head_right)
95 region_set.insert(Region(Region::TOPLEFT,
96 right, reg_it->y));
97 }
98 break;
99 case Region::TOPRIGHT:
100 if (left < reg_it->x + win_w && bottom > reg_it->y) {
101 if (bottom + win_h <= head_bot)
102 region_set.insert(Region(Region::TOPRIGHT,
103 reg_it->x, bottom));
104 if (left - win_w >= head_left)
105 region_set.insert(Region(Region::TOPRIGHT,
106 left - win_w, reg_it->y));
107 }
108 break;
109 case Region::BOTTOMRIGHT:
110 if (left < reg_it->x + win_w && top < reg_it->y + win_h) {
111 if (top - win_h >= head_top)
112 region_set.insert(Region(Region::BOTTOMRIGHT,
113 reg_it->x, top - win_h));
114 if (left - win_w >= head_left)
115 region_set.insert(Region(Region::BOTTOMRIGHT,
116 left - win_w, reg_it->y));
117 }
118 break;
119 case Region::BOTTOMLEFT:
120 if (right > reg_it->x && top < reg_it->y + win_h) {
121 if (top - win_h >= head_top)
122 region_set.insert(Region(Region::BOTTOMLEFT,
123 reg_it->x, top - win_h));
124 if (right + win_w <= head_right)
125 region_set.insert(Region(Region::BOTTOMLEFT,
126 right, reg_it->y));
127 }
128 break;
129 }
130
131 }
132 }
133
134 // choose the region with minimum overlap
135 int min_so_far = win_w * win_h * windowlist.size() + 1;
136 std::set<Region>::iterator min_reg = region_set.end();
137
138 std::set<Region>::iterator reg_it = region_set.begin();
139 for (; reg_it != region_set.end(); ++reg_it) {
140
141 int overlap = 0;
142 it = windowlist.rbegin();
143 for (; it != windowlist.rend(); ++it) {
144
145 // get the dimensions of the window
146 int bottom = (*it)->y() + (*it)->height() +
147 2*(*it)->frame().window().borderWidth();
148 int top = (*it)->y();
149 int right = (*it)->x() + (*it)->width() +
150 2*(*it)->frame().window().borderWidth();
151 int left = (*it)->x();
152
153 // get the coordinates of the overlap region
154 int min_right = (right > reg_it->x + win_w) ?
155 reg_it->x + win_w : right;
156 int min_bottom = (bottom > reg_it->y + win_h) ?
157 reg_it->y + win_h : bottom;
158 int max_left = (left > reg_it->x) ? left : reg_it->x;
159 int max_top = (top > reg_it->y) ? top : reg_it->y;
160
161 // now compute the overlap and add to running total
162 if (min_right > max_left && min_bottom > max_top)
163 overlap += (min_right - max_left) * (min_bottom - max_top);
164
165 }
166
167 // if this placement is better, use it
168 if (overlap < min_so_far) {
169 min_reg = reg_it;
170 min_so_far = overlap;
171 if (overlap == 0) // can't do better than this
172 break;
173 }
174
175 }
176
177 // place window
178 place_x = min_reg->x;
179 place_y = min_reg->y;
180
181 return true;
182}