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