You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MonitorArrangement.cxx 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* Copyright 2021 Hugo Lundin <huglu@cendio.se> for Cendio AB.
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. #include <set>
  19. #include <vector>
  20. #include <string>
  21. #include <utility>
  22. #include <sstream>
  23. #include <assert.h>
  24. #include <algorithm>
  25. #include <FL/Fl.H>
  26. #include <FL/fl_draw.H>
  27. #include <FL/Fl_Button.H>
  28. #include <rfb/Rect.h>
  29. #include "MonitorArrangement.h"
  30. static const Fl_Boxtype FL_CHECKERED_BOX = FL_FREE_BOXTYPE;
  31. MonitorArrangement::MonitorArrangement(
  32. int x, int y, int w, int h)
  33. : Fl_Group(x, y, w, h),
  34. SELECTION_COLOR(fl_lighter(FL_BLUE)),
  35. AVAILABLE_COLOR(fl_lighter(fl_lighter(fl_lighter(FL_BACKGROUND_COLOR)))),
  36. m_monitors()
  37. {
  38. // Used for required monitors.
  39. Fl::set_boxtype(FL_CHECKERED_BOX, checkered_pattern_draw, 0, 0, 0, 0);
  40. box(FL_DOWN_BOX);
  41. color(fl_lighter(FL_BACKGROUND_COLOR));
  42. layout();
  43. end();
  44. }
  45. MonitorArrangement::~MonitorArrangement()
  46. {
  47. }
  48. std::set<int> MonitorArrangement::get()
  49. {
  50. std::set<int> indices;
  51. for (int i = 0; i < (int) m_monitors.size(); i++) {
  52. if (m_monitors[i]->value() == 1)
  53. indices.insert(i);
  54. }
  55. return indices;
  56. }
  57. void MonitorArrangement::set(std::set<int> indices)
  58. {
  59. for (int i = 0; i < (int) m_monitors.size(); i++) {
  60. bool selected = std::find(indices.begin(), indices.end(), i) != indices.end();
  61. m_monitors[i]->value(selected ? 1 : 0);
  62. }
  63. }
  64. void MonitorArrangement::draw()
  65. {
  66. for (int i = 0; i < (int) m_monitors.size(); i++) {
  67. Fl_Button * monitor = m_monitors[i];
  68. if (is_required(i)) {
  69. monitor->box(FL_CHECKERED_BOX);
  70. monitor->color(SELECTION_COLOR);
  71. } else {
  72. monitor->box(FL_BORDER_BOX);
  73. monitor->color(AVAILABLE_COLOR);
  74. monitor->selection_color(SELECTION_COLOR);
  75. }
  76. }
  77. Fl_Group::draw();
  78. }
  79. void MonitorArrangement::layout()
  80. {
  81. int x, y, w, h;
  82. double scale = this->scale();
  83. const double MARGIN_SCALE_FACTOR = 0.99;
  84. std::pair<int, int> offset = this->offset();
  85. for (int i = 0; i < Fl::screen_count(); i++) {
  86. Fl::screen_xywh(x, y, w, h, i);
  87. Fl_Button *monitor = new Fl_Button(
  88. /* x = */ this->x() + offset.first + x*scale + (1 - MARGIN_SCALE_FACTOR)*x*scale,
  89. /* y = */ this->y() + offset.second + y*scale + (1 - MARGIN_SCALE_FACTOR)*y*scale,
  90. /* w = */ w*scale*MARGIN_SCALE_FACTOR,
  91. /* h = */ h*scale*MARGIN_SCALE_FACTOR
  92. );
  93. monitor->clear_visible_focus();
  94. monitor->callback(monitor_pressed, this);
  95. monitor->type(FL_TOGGLE_BUTTON);
  96. monitor->when(FL_WHEN_CHANGED);
  97. m_monitors.push_back(monitor);
  98. }
  99. }
  100. bool MonitorArrangement::is_required(int m)
  101. {
  102. // A selected monitor is never required.
  103. if (m_monitors[m]->value() == 1)
  104. return false;
  105. // If no monitors are selected, none are required.
  106. std::set<int> selected = get();
  107. if (selected.size() <= 0)
  108. return false;
  109. // Go through all selected monitors and find the monitor
  110. // indices that bounds the fullscreen frame buffer. If
  111. // the given monitor's coordinates are inside the bounds,
  112. // while not being selected, it is instead required.
  113. int x, y, w, h;
  114. int top_y, bottom_y, left_x, right_x;
  115. std::set<int>::iterator it = selected.begin();
  116. // Base the rest of the calculations on the dimensions
  117. // obtained for the first monitor.
  118. Fl::screen_xywh(x, y, w, h, *it);
  119. top_y = y;
  120. bottom_y = y + h;
  121. left_x = x;
  122. right_x = x + w;
  123. // Go through the rest of the monitors,
  124. // exhausting the rest of the iterator.
  125. for (; it != selected.end(); it++) {
  126. Fl::screen_xywh(x, y, w, h, *it);
  127. if (y < top_y) {
  128. top_y = y;
  129. }
  130. if ((y + h) > bottom_y) {
  131. bottom_y = y + h;
  132. }
  133. if (x < left_x) {
  134. left_x = x;
  135. }
  136. if ((x + w) > right_x) {
  137. right_x = x + w;
  138. }
  139. }
  140. rfb::Rect viewport, monitor;
  141. viewport.setXYWH(left_x, top_y, right_x - left_x, bottom_y - top_y);
  142. Fl::screen_xywh(x, y, w, h, m);
  143. monitor.setXYWH(x, y, w, h);
  144. return monitor.enclosed_by(viewport);
  145. }
  146. double MonitorArrangement::scale()
  147. {
  148. const int MARGIN = 20;
  149. std::pair<int, int> size = this->size();
  150. double s_w = static_cast<double>(this->w()-MARGIN) / static_cast<double>(size.first);
  151. double s_h = static_cast<double>(this->h()-MARGIN) / static_cast<double>(size.second);
  152. // Choose the one that scales the least, in order to
  153. // maximize our use of the given bounding area.
  154. if (s_w > s_h)
  155. return s_h;
  156. else
  157. return s_w;
  158. }
  159. std::pair<int, int> MonitorArrangement::size()
  160. {
  161. int x, y, w, h;
  162. int top, bottom, left, right;
  163. int x_min, x_max, y_min, y_max;
  164. x_min = x_max = y_min = y_max = 0;
  165. for (int i = 0; i < Fl::screen_count(); i++) {
  166. Fl::screen_xywh(x, y, w, h, i);
  167. top = y;
  168. bottom = y + h;
  169. left = x;
  170. right = x + w;
  171. if (top < y_min)
  172. y_min = top;
  173. if (bottom > y_max)
  174. y_max = bottom;
  175. if (left < x_min)
  176. x_min = left;
  177. if (right > x_max)
  178. x_max = right;
  179. }
  180. return std::make_pair(x_max - x_min, y_max - y_min);
  181. }
  182. std::pair<int, int> MonitorArrangement::offset()
  183. {
  184. double scale = this->scale();
  185. std::pair<int, int> size = this->size();
  186. std::pair<int, int> origin = this->origin();
  187. int offset_x = (this->w()/2) - (size.first/2 * scale);
  188. int offset_y = (this->h()/2) - (size.second/2 * scale);
  189. return std::make_pair(offset_x + abs(origin.first)*scale, offset_y + abs(origin.second)*scale);
  190. }
  191. std::pair<int, int> MonitorArrangement::origin()
  192. {
  193. int x, y, w, h, ox, oy;
  194. ox = oy = 0;
  195. for (int i = 0; i < Fl::screen_count(); i++) {
  196. Fl::screen_xywh(x, y, w, h, i);
  197. if (x < ox)
  198. ox = x;
  199. if (y < oy)
  200. oy = y;
  201. }
  202. return std::make_pair(ox, oy);
  203. }
  204. void MonitorArrangement::monitor_pressed(Fl_Widget *widget, void *user_data)
  205. {
  206. MonitorArrangement *self = (MonitorArrangement *) user_data;
  207. // When a monitor is selected, FLTK changes the state of it for us.
  208. // However, selecting a monitor might implicitly change the state of
  209. // others (if they become required). FLTK only redraws the selected
  210. // monitor. Therefore, we must trigger a redraw of the whole widget
  211. // manually.
  212. self->redraw();
  213. }
  214. void MonitorArrangement::checkered_pattern_draw(
  215. int x, int y, int width, int height, Fl_Color color)
  216. {
  217. bool draw_checker = false;
  218. const int CHECKER_SIZE = 8;
  219. fl_color(fl_lighter(fl_lighter(fl_lighter(color))));
  220. fl_rectf(x, y, width, height);
  221. fl_color(Fl::draw_box_active() ? color : fl_inactive(color));
  222. // Round up the square count. Later on, we remove square area that are
  223. // outside the given bounding area.
  224. const int count = (width + CHECKER_SIZE - 1) / CHECKER_SIZE;
  225. for (int i = 0; i < count; i++) {
  226. for (int j = 0; j < count; j++) {
  227. draw_checker = (i + j) % 2 == 0;
  228. if (draw_checker) {
  229. fl_rectf(
  230. /* x = */ x + i * CHECKER_SIZE,
  231. /* y = */ y + j * CHECKER_SIZE,
  232. /* w = */ CHECKER_SIZE - std::max(0, ((i + 1) * CHECKER_SIZE) - width),
  233. /* h = */ CHECKER_SIZE - std::max(0, ((j + 1) * CHECKER_SIZE) - height)
  234. );
  235. }
  236. }
  237. }
  238. fl_color(Fl::draw_box_active() ? FL_BLACK : fl_inactive(FL_BLACK));
  239. fl_rect(x, y, width, height);
  240. }