all repos — openbox @ c26e5ea7519b02d7b83261faefeec657bb65c4a4

openbox fork - make it a bit more like ryudo

otk/widget.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-

#include "config.h"
#include "widget.hh"
#include "display.hh"
#include "surface.hh"
#include "rendertexture.hh"
#include "rendercolor.hh"
#include "eventdispatcher.hh"
#include "screeninfo.hh"

#include <climits>
#include <cassert>
#include <algorithm>

namespace otk {

Widget::Widget(int screen, EventDispatcher *ed, Direction direction, int bevel,
               bool overrideredir)
  : _texture(0),
    _screen(screen),
    _parent(0),
    _window(0),
    _surface(0),
    _event_mask(ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
                ExposureMask | StructureNotifyMask),
    _alignment(RenderStyle::CenterJustify),
    _direction(direction),
    _max_size(INT_MAX, INT_MAX),
    _visible(false),
    _bordercolor(0),
    _borderwidth(0),
    _bevel(bevel),
    _dirty(true),
    _dispatcher(ed),
    _ignore_config(0)
{
  createWindow(overrideredir);
  _dispatcher->registerHandler(_window, this);
}

Widget::Widget(Widget *parent, Direction direction, int bevel)
  : _texture(0),
    _screen(parent->_screen),
    _parent(parent),
    _window(0),
    _surface(0),
    _event_mask(ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
                ExposureMask | StructureNotifyMask),
    _alignment(RenderStyle::CenterJustify),
    _direction(direction),
    _max_size(INT_MAX, INT_MAX),
    _visible(false),
    _bordercolor(0),
    _borderwidth(0),
    _bevel(bevel),
    _dirty(true),
    _dispatcher(parent->_dispatcher),
    _ignore_config(0)
{
  assert(parent);
  createWindow(false);
  parent->addChild(this);
  if (parent->visible()) parent->layout();
  _dispatcher->registerHandler(_window, this);
}

Widget::~Widget()
{
  assert(_children.empty()); // this would be bad. theyd have a hanging _parent
  
  if (_surface) delete _surface;
  if (_parent) _parent->removeChild(this);

  _dispatcher->clearHandler(_window);
  XDestroyWindow(**display, _window);
}

void Widget::show(bool children)
{
  if (children) {
    std::list<Widget*>::iterator it , end = _children.end();
    for (it = _children.begin(); it != end; ++it) {
      (*it)->show(true);
    }
  }
  if (!_visible) {
    _visible = true;
    XMapWindow(**display, _window);
    update();
  }
}

void Widget::hide()
{
  if (_visible) {
    _visible = false;
    XUnmapWindow(**display, _window);
    if (_parent) _parent->layout();
  }
} 

void Widget::setEventMask(long e)
{
  XSelectInput(**display, _window, e);
  _event_mask = e;
}

void Widget::update()
{
  if (!_visible) return;
  _dirty = true;
  if (parent())
    parent()->layout(); // relay-out us and our siblings
  else {
    render();
    layout();
  }
}

void Widget::moveresize(const Rect &r)
{
  int w, h;
  w = std::min(std::max(r.width(), minSize().width()), maxSize().width());
  h = std::min(std::max(r.height(), minSize().height()), maxSize().height());

  if (r.x() == area().x() && r.y() == area().y() &&
      w == area().width() && h == area().height()) {
    return; // no change, don't cause a big layout chain to occur!
  }
  
  internal_moveresize(r.x(), r.y(), w, h);

  update();
}

void Widget::internal_moveresize(int x, int y, int w, int h)
{
  assert(w > 0);
  assert(h > 0);
  assert(_borderwidth >= 0);
  _dirty = true;
  XMoveResizeWindow(**display, _window, x, y,
                    w - _borderwidth * 2,
                    h - _borderwidth * 2);
  _ignore_config++;

  _area = Rect(x, y, w, h);
}

void Widget::setAlignment(RenderStyle::Justify a)
{
  _alignment = a;  
  layout();
}
  
void Widget::createWindow(bool overrideredir)
{
  const ScreenInfo *info = display->screenInfo(_screen);
  XSetWindowAttributes attrib;
  unsigned long mask = CWEventMask | CWBorderPixel;

  attrib.event_mask = _event_mask;
  attrib.border_pixel = (_bordercolor ?
                         _bordercolor->pixel():
                         BlackPixel(**display, _screen));

  if (overrideredir) {
    mask |= CWOverrideRedirect;
    attrib.override_redirect = true;
  }
  
  _window = XCreateWindow(**display, (_parent ?
                                      _parent->_window :
                                      RootWindow(**display, _screen)),
                          _area.x(), _area.y(),
                          _area.width(), _area.height(),
                          _borderwidth,
                          info->depth(),
                          InputOutput,
                          info->visual(),
                          mask,
                          &attrib);
  assert(_window != None);
  ++_ignore_config;
}

void Widget::setBorderWidth(int w)
{
  assert(w >= 0);
  if (!parent()) return; // top-level windows cannot have borders
  if (w == borderWidth()) return; // no change
  
  _borderwidth = w;
  XSetWindowBorderWidth(**display, _window, _borderwidth);

  calcDefaultSizes();
  update();
}

void Widget::setMinSize(const Size &s)
{
  _min_size = s;
  update();
}

void Widget::setMaxSize(const Size &s)
{
  _max_size = s;
  update();
}

void Widget::setBorderColor(const RenderColor *c)
{
  _bordercolor = c;
  XSetWindowBorder(**otk::display, _window,
                   c ? c->pixel() : BlackPixel(**otk::display, _screen));
}

void Widget::setBevel(int b)
{
  _bevel = b;
  calcDefaultSizes();
  layout();
}

void Widget::layout()
{
  if (_children.empty() || !_visible) return;
  if (_direction == Horizontal)
    layoutHorz();
  else
    layoutVert();
}

void Widget::layoutHorz()
{
  std::list<Widget*>::iterator it, end;

  // work with just the visible children
  std::list<Widget*> visible = _children;
  for (it = visible.begin(), end = visible.end(); it != end;) {
    std::list<Widget*>::iterator next = it; ++next;
    if (!(*it)->visible())
      visible.erase(it);
    it = next;
  }

  if (visible.empty()) return;

  int x, y, w, h; // working area
  x = y = _bevel;
  w = _area.width() - _borderwidth * 2 - _bevel * 2;
  h = _area.height() - _borderwidth * 2 - _bevel * 2;
  if (w < 0 || h < 0) return; // not worth laying anything out!

  int free = w - (visible.size() - 1) * _bevel;
  if (free < 0) free = 0;
  int each;
  
  std::list<Widget*> adjustable = visible;

  // find the 'free' space, and how many children will be using it
  for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
    std::list<Widget*>::iterator next = it; ++next;
    free -= (*it)->minSize().width();
    if (free < 0) free = 0;
    if ((*it)->maxSize().width() - (*it)->minSize().width() <= 0)
      adjustable.erase(it);
    it = next;
  }
  // some widgets may have max widths that restrict them, find the 'true'
  // amount of free space after these widgets are not included
  if (!adjustable.empty()) {
    do {
      each = free / adjustable.size();
      for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
        std::list<Widget*>::iterator next = it; ++next;
        int m = (*it)->maxSize().width() - (*it)->minSize().width();
        if (m > 0 && m < each) {
          free -= m;
          if (free < 0) free = 0;
          adjustable.erase(it);
          break; // if one is found to be fixed, then the free space needs to
                 // change, and the rest need to be reexamined
        }
        it = next;
      }
    } while (it != end && !adjustable.empty());
  }

  // place/size the widgets
  if (!adjustable.empty())
    each = free / adjustable.size();
  else
    each = 0;
  for (it = visible.begin(), end = visible.end(); it != end; ++it) {
    int w;
    // is the widget adjustable?
    std::list<Widget*>::const_iterator
      found = std::find(adjustable.begin(), adjustable.end(), *it);
    if (found != adjustable.end()) {
      // adjustable
      w = (*it)->minSize().width() + each;
    } else {
      // fixed
      w = (*it)->minSize().width();
    }
    // align it vertically
    int yy = y;
    int hh = std::max(std::min(h, (*it)->_max_size.height()),
                      (*it)->_min_size.height());
    if (hh < h) {
      switch(_alignment) {
      case RenderStyle::RightBottomJustify:
        yy += h - hh;
        break;
      case RenderStyle::CenterJustify:
        yy += (h - hh) / 2;
        break;
      case RenderStyle::LeftTopJustify:
        break;
      }
    }
    (*it)->internal_moveresize(x, yy, w, hh);
    (*it)->render();
    (*it)->layout();
    x += w + _bevel;
  }
}

void Widget::layoutVert()
{
  std::list<Widget*>::iterator it, end;

  // work with just the visible children
  std::list<Widget*> visible = _children;
  for (it = visible.begin(), end = visible.end(); it != end;) {
    std::list<Widget*>::iterator next = it; ++next;
    if (!(*it)->visible())
      visible.erase(it);
    it = next;
  }

  if (visible.empty()) return;

  int x, y, w, h; // working area
  x = y = _bevel;
  w = _area.width() - _borderwidth * 2 - _bevel * 2;
  h = _area.height() - _borderwidth * 2 - _bevel * 2;
  if (w < 0 || h < 0) return; // not worth laying anything out!

  int free = h - (visible.size() - 1) * _bevel;
  if (free < 0) free = 0;
  int each;

  std::list<Widget*> adjustable = visible;

  // find the 'free' space, and how many children will be using it
  for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
    std::list<Widget*>::iterator next = it; ++next;
    free -= (*it)->minSize().height();
    if (free < 0) free = 0;
    if ((*it)->maxSize().height() - (*it)->minSize().height() <= 0)
      adjustable.erase(it);
    it = next;
  }
  // some widgets may have max heights that restrict them, find the 'true'
  // amount of free space after these widgets are not included
  if (!adjustable.empty()) {
    do {
      each = free / adjustable.size();
      for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
        std::list<Widget*>::iterator next = it; ++next;
        int m = (*it)->maxSize().height() - (*it)->minSize().height();
        if (m > 0 && m < each) {
          free -= m;
          if (free < 0) free = 0;
          adjustable.erase(it);
          break; // if one is found to be fixed, then the free space needs to
                 // change, and the rest need to be reexamined
        }
        it = next;
      }
    } while (it != end && !adjustable.empty());
  }

  // place/size the widgets
  if (!adjustable.empty())
  each = free / adjustable.size();
  else
    each = 0;
  for (it = visible.begin(), end = visible.end(); it != end; ++it) {
    int h;
    // is the widget adjustable?
    std::list<Widget*>::const_iterator
      found = std::find(adjustable.begin(), adjustable.end(), *it);
    if (found != adjustable.end()) {
      // adjustable
      h = (*it)->minSize().height() + each;
    } else {
      // fixed
      h = (*it)->minSize().height();
    }
    // align it horizontally
    int xx = x;
    int ww = std::max(std::min(w, (*it)->_max_size.width()),
                      (*it)->_min_size.width());
    if (ww < w) {
      switch(_alignment) {
      case RenderStyle::RightBottomJustify:
        xx += w - ww;
        break;
      case RenderStyle::CenterJustify:
        xx += (w - ww) / 2;
        break;
      case RenderStyle::LeftTopJustify:
        break;
      }
    }
    (*it)->internal_moveresize(xx, y, ww, h);
    (*it)->render();
    (*it)->layout();
    y += h + _bevel; 
  }
}

void Widget::render()
{
  if (!_dirty) return;
  if (!_texture) {
    XSetWindowBackgroundPixmap(**display, _window, ParentRelative);
    return;
  }
  if (_borderwidth * 2 > _area.width() ||
      _borderwidth * 2 > _area.height())
    return; // no surface to draw on
  
  Surface *s = new Surface(_screen, Size(_area.width() - _borderwidth * 2,
                                         _area.height() - _borderwidth * 2));
  display->renderControl(_screen)->drawBackground(*s, *_texture);

  renderForeground(*s); // for inherited types to render onto the _surface

  XSetWindowBackgroundPixmap(**display, _window, s->pixmap());
  XClearWindow(**display, _window);

  // delete the old surface *after* its pixmap isn't in use anymore
  if (_surface) delete _surface;

  _surface = s;

  _dirty = false;
}

void Widget::renderChildren()
{
  std::list<Widget*>::iterator it, end = _children.end();
  for (it = _children.begin(); it != end; ++it)
    (*it)->render();
}

void Widget::exposeHandler(const XExposeEvent &e)
{
  EventHandler::exposeHandler(e);
  XClearArea(**display, _window, e.x, e.y, e.width, e.height, false);
}

void Widget::configureHandler(const XConfigureEvent &e)
{
  if (_ignore_config) {
    _ignore_config--;
  } else {
    // only interested in these for top level windows
    if (_parent) return;
    
    XEvent ev;
    ev.xconfigure.width = e.width;
    ev.xconfigure.height = e.height;
    while (XCheckTypedWindowEvent(**display, window(), ConfigureNotify, &ev));

    if (!(ev.xconfigure.width == area().width() &&
          ev.xconfigure.height == area().height())) {
      _area = Rect(_area.position(), Size(e.width, e.height));
      update();
    }
  }
}

}