+ } else if (e.message_type == otk::Property::atoms.net_close_window) {
+#ifdef DEBUG
+ printf("net_close_window for 0x%lx\n", _window);
+#endif
+ close();
+ } else if (e.message_type == otk::Property::atoms.net_active_window) {
+#ifdef DEBUG
+ printf("net_active_window for 0x%lx\n", _window);
+#endif
+ if (_iconic)
+ setDesktop(openbox->screen(_screen)->desktop());
+ if (_shaded)
+ shade(false);
+ focus();
+ openbox->screen(_screen)->raiseWindow(this);
+ } else if (e.message_type == otk::Property::atoms.openbox_active_window) {
+ if (_iconic)
+ setDesktop(openbox->screen(_screen)->desktop());
+ if (e.data.l[0] && _shaded)
+ shade(false);
+ focus();
+ if (e.data.l[1])
+ openbox->screen(_screen)->raiseWindow(this);
+ }
+}
+
+
+#if defined(SHAPE)
+void Client::shapeHandler(const XShapeEvent &e)
+{
+ otk::EventHandler::shapeHandler(e);
+
+ if (e.kind == ShapeBounding) {
+ _shaped = e.shaped;
+ frame->adjustShape();
+ }
+}
+#endif
+
+
+void Client::resize(Corner anchor, unsigned int w, unsigned int h)
+{
+ if (!(_functions & Func_Resize)) return;
+ internal_resize(anchor, w, h);
+}
+
+
+void Client::internal_resize(Corner anchor, unsigned int w, unsigned int h,
+ bool user, int x, int y)
+{
+ w -= _base_size.width();
+ h -= _base_size.height();
+
+ if (user) {
+ // for interactive resizing. have to move half an increment in each
+ // direction.
+ unsigned int mw = w % _size_inc.width(); // how far we are towards the next
+ // size inc
+ unsigned int mh = h % _size_inc.height();
+ unsigned int aw = _size_inc.width() / 2; // amount to add
+ unsigned int ah = _size_inc.height() / 2;
+ // don't let us move into a new size increment
+ if (mw + aw >= _size_inc.width()) aw = _size_inc.width() - mw - 1;
+ if (mh + ah >= _size_inc.height()) ah = _size_inc.height() - mh - 1;
+ w += aw;
+ h += ah;
+
+ // if this is a user-requested resize, then check against min/max sizes
+ // and aspect ratios
+
+ // smaller than min size or bigger than max size?
+ if (w < _min_size.width()) w = _min_size.width();
+ else if (w > _max_size.width()) w = _max_size.width();
+ if (h < _min_size.height()) h = _min_size.height();
+ else if (h > _max_size.height()) h = _max_size.height();
+
+ // adjust the height ot match the width for the aspect ratios
+ if (_min_ratio)
+ if (h * _min_ratio > w) h = static_cast<int>(w / _min_ratio);
+ if (_max_ratio)
+ if (h * _max_ratio < w) h = static_cast<int>(w / _max_ratio);
+ }
+
+ // keep to the increments
+ w /= _size_inc.width();
+ h /= _size_inc.height();
+
+ // you cannot resize to nothing
+ if (w < 1) w = 1;
+ if (h < 1) h = 1;
+
+ // store the logical size
+ _logical_size = otk::Size(w, h);
+
+ w *= _size_inc.width();
+ h *= _size_inc.height();
+
+ w += _base_size.width();
+ h += _base_size.height();
+
+ if (x == INT_MIN || y == INT_MIN) {
+ x = _area.x();
+ y = _area.y();
+ switch (anchor) {
+ case TopLeft:
+ break;
+ case TopRight:
+ x -= w - _area.width();
+ break;
+ case BottomLeft:
+ y -= h - _area.height();
+ break;
+ case BottomRight:
+ x -= w - _area.width();
+ y -= h - _area.height();
+ break;
+ }
+ }
+
+ _area = otk::Rect(_area.position(), otk::Size(w, h));
+
+ XResizeWindow(**otk::display, _window, w, h);
+
+ // resize the frame to match the request
+ frame->adjustSize();
+ internal_move(x, y);
+}
+
+
+void Client::move(int x, int y)
+{
+ if (!(_functions & Func_Move)) return;
+ frame->frameGravity(x, y); // get the client's position based on x,y for the
+ // frame
+ internal_move(x, y);
+}
+
+
+void Client::internal_move(int x, int y)
+{
+ _area = otk::Rect(otk::Point(x, y), _area.size());
+
+ // move the frame to be in the requested position
+ if (frame) { // this can be called while mapping, before frame exists
+ frame->adjustPosition();
+
+ // send synthetic configure notify (we don't need to if we aren't mapped
+ // yet)
+ XEvent event;
+ event.type = ConfigureNotify;
+ event.xconfigure.display = **otk::display;
+ event.xconfigure.event = _window;
+ event.xconfigure.window = _window;
+
+ // root window coords with border in mind
+ event.xconfigure.x = x - _border_width + frame->size().left;
+ event.xconfigure.y = y - _border_width + frame->size().top;
+
+ event.xconfigure.width = _area.width();
+ event.xconfigure.height = _area.height();
+ event.xconfigure.border_width = _border_width;
+ event.xconfigure.above = frame->plate();
+ event.xconfigure.override_redirect = False;
+ XSendEvent(event.xconfigure.display, event.xconfigure.window, False,
+ StructureNotifyMask, &event);
+#if 0//def DEBUG
+ printf("Sent synthetic ConfigureNotify %d,%d %d,%d to 0x%lx\n",
+ event.xconfigure.x, event.xconfigure.y, event.xconfigure.width,
+ event.xconfigure.height, event.xconfigure.window);
+#endif
+ }
+}
+
+
+void Client::close()
+{
+ XEvent ce;
+
+ if (!(_functions & Func_Close)) return;
+
+ // XXX: itd be cool to do timeouts and shit here for killing the client's
+ // process off
+ // like... if the window is around after 5 seconds, then the close button
+ // turns a nice red, and if this function is called again, the client is
+ // explicitly killed.
+
+ ce.xclient.type = ClientMessage;
+ ce.xclient.message_type = otk::Property::atoms.wm_protocols;
+ ce.xclient.display = **otk::display;
+ ce.xclient.window = _window;
+ ce.xclient.format = 32;
+ ce.xclient.data.l[0] = otk::Property::atoms.wm_delete_window;
+ ce.xclient.data.l[1] = CurrentTime;
+ ce.xclient.data.l[2] = 0l;
+ ce.xclient.data.l[3] = 0l;
+ ce.xclient.data.l[4] = 0l;
+ XSendEvent(**otk::display, _window, false, NoEventMask, &ce);
+}
+
+
+void Client::changeState()
+{
+ unsigned long state[2];
+ state[0] = _wmstate;
+ state[1] = None;
+ otk::Property::set(_window, otk::Property::atoms.wm_state,
+ otk::Property::atoms.wm_state, state, 2);
+
+ Atom netstate[10];
+ int num = 0;
+ if (_modal)
+ netstate[num++] = otk::Property::atoms.net_wm_state_modal;
+ if (_shaded)
+ netstate[num++] = otk::Property::atoms.net_wm_state_shaded;
+ if (_iconic)
+ netstate[num++] = otk::Property::atoms.net_wm_state_hidden;
+ if (_skip_taskbar)
+ netstate[num++] = otk::Property::atoms.net_wm_state_skip_taskbar;
+ if (_skip_pager)
+ netstate[num++] = otk::Property::atoms.net_wm_state_skip_pager;
+ if (_fullscreen)
+ netstate[num++] = otk::Property::atoms.net_wm_state_fullscreen;
+ if (_max_vert)
+ netstate[num++] = otk::Property::atoms.net_wm_state_maximized_vert;
+ if (_max_horz)
+ netstate[num++] = otk::Property::atoms.net_wm_state_maximized_horz;
+ if (_above)
+ netstate[num++] = otk::Property::atoms.net_wm_state_above;
+ if (_below)
+ netstate[num++] = otk::Property::atoms.net_wm_state_below;
+ otk::Property::set(_window, otk::Property::atoms.net_wm_state,
+ otk::Property::atoms.atom, netstate, num);
+
+ calcLayer();
+
+ if (frame)
+ frame->adjustState();
+}
+
+
+void Client::changeAllowedActions(void)
+{
+ Atom actions[9];
+ int num = 0;
+
+ actions[num++] = otk::Property::atoms.net_wm_action_change_desktop;
+
+ if (_functions & Func_Shade)
+ actions[num++] = otk::Property::atoms.net_wm_action_shade;
+ if (_functions & Func_Close)
+ actions[num++] = otk::Property::atoms.net_wm_action_close;
+ if (_functions & Func_Move)
+ actions[num++] = otk::Property::atoms.net_wm_action_move;
+ if (_functions & Func_Iconify)
+ actions[num++] = otk::Property::atoms.net_wm_action_minimize;
+ if (_functions & Func_Resize)
+ actions[num++] = otk::Property::atoms.net_wm_action_resize;
+ if (_functions & Func_Fullscreen)
+ actions[num++] = otk::Property::atoms.net_wm_action_fullscreen;
+ if (_functions & Func_Maximize) {
+ actions[num++] = otk::Property::atoms.net_wm_action_maximize_horz;
+ actions[num++] = otk::Property::atoms.net_wm_action_maximize_vert;
+ }
+
+ otk::Property::set(_window, otk::Property::atoms.net_wm_allowed_actions,
+ otk::Property::atoms.atom, actions, num);
+
+ // make sure the window isn't breaking any rules now
+
+ if (!(_functions & Func_Shade) && _shaded)
+ if (frame) shade(false);
+ else _shaded = false;
+ if (!(_functions & Func_Iconify) && _iconic)
+ if (frame) setDesktop(openbox->screen(_screen)->desktop());
+ else _iconic = false;
+ if (!(_functions & Func_Fullscreen) && _fullscreen)
+ if (frame) fullscreen(false);
+ else _fullscreen = false;
+ if (!(_functions & Func_Maximize) && (_max_horz || _max_vert))
+ if (frame) maximize(false, 0);
+ else _max_vert = _max_horz = false;
+}
+
+
+void Client::remaximize()
+{
+ int dir;
+ if (_max_horz && _max_vert)
+ dir = 0;
+ else if (_max_horz)
+ dir = 1;
+ else if (_max_vert)
+ dir = 2;
+ else
+ return; // not maximized
+ _max_horz = _max_vert = false;
+ maximize(true, dir, false);
+}
+
+
+void Client::applyStartupState()
+{
+ // these are in a carefully crafted order..
+
+ if (_modal) {
+ _modal = false;
+ setModal(true);
+ }
+
+ if (_iconic) {
+ _iconic = false;
+ setDesktop(ICONIC_DESKTOP);
+ }
+ if (_fullscreen) {
+ _fullscreen = false;
+ fullscreen(true, false);
+ }
+ if (_shaded) {
+ _shaded = false;
+ shade(true);
+ }
+ if (_urgent)
+ fireUrgent();
+
+ if (_max_vert && _max_horz) {
+ _max_vert = _max_horz = false;
+ maximize(true, 0, false);
+ } else if (_max_vert) {
+ _max_vert = false;
+ maximize(true, 2, false);
+ } else if (_max_horz) {
+ _max_horz = false;
+ maximize(true, 1, false);
+ }
+
+ if (_skip_taskbar); // nothing to do for this
+ if (_skip_pager); // nothing to do for this
+ if (_modal); // nothing to do for this
+ if (_above); // nothing to do for this
+ if (_below); // nothing to do for this
+}
+
+
+void Client::fireUrgent()
+{
+ // call the python UrgentWindow callbacks
+ EventData data(_screen, this, EventAction::UrgentWindow, 0);
+ openbox->bindings()->fireEvent(&data);
+}
+
+
+void Client::shade(bool shade)
+{
+ if (!(_functions & Func_Shade) || // can't
+ _shaded == shade) return; // already done
+
+ // when we're iconic, don't change the wmstate
+ if (!_iconic)
+ _wmstate = shade ? IconicState : NormalState;
+ _shaded = shade;
+ changeState();
+ frame->adjustSize();
+}
+
+
+void Client::maximize(bool max, int dir, bool savearea)
+{
+ assert(dir == 0 || dir == 1 || dir == 2);
+ if (!(_functions & Func_Maximize)) return; // can't
+
+ // check if already done
+ if (max) {
+ if (dir == 0 && _max_horz && _max_vert) return;
+ if (dir == 1 && _max_horz) return;
+ if (dir == 2 && _max_vert) return;
+ } else {
+ if (dir == 0 && !_max_horz && !_max_vert) return;
+ if (dir == 1 && !_max_horz) return;
+ if (dir == 2 && !_max_vert) return;
+ }
+
+ const otk::Rect &a = openbox->screen(_screen)->area();
+ int x = frame->area().x(), y = frame->area().y(),
+ w = _area.width(), h = _area.height();
+
+ if (max) {
+ if (savearea) {
+ long dimensions[4];
+ long *readdim;
+ unsigned long n = 4;
+
+ dimensions[0] = x;
+ dimensions[1] = y;
+ dimensions[2] = w;
+ dimensions[3] = h;
+
+ // get the property off the window and use it for the dimentions we are
+ // already maxed on
+ if (otk::Property::get(_window, otk::Property::atoms.openbox_premax,
+ otk::Property::atoms.cardinal, &n,
+ (long unsigned**) &readdim)) {
+ if (n >= 4) {
+ if (_max_horz) {
+ dimensions[0] = readdim[0];
+ dimensions[2] = readdim[2];
+ }
+ if (_max_vert) {
+ dimensions[1] = readdim[1];
+ dimensions[3] = readdim[3];
+ }
+ }
+ delete readdim;
+ }
+
+ otk::Property::set(_window, otk::Property::atoms.openbox_premax,
+ otk::Property::atoms.cardinal,
+ (long unsigned*)dimensions, 4);
+ }
+ if (dir == 0 || dir == 1) { // horz
+ x = a.x();
+ w = a.width();
+ }
+ if (dir == 0 || dir == 2) { // vert
+ y = a.y();
+ h = a.height() - frame->size().top - frame->size().bottom;
+ }
+ } else {
+ long *dimensions;
+ long unsigned n = 4;
+
+ if (otk::Property::get(_window, otk::Property::atoms.openbox_premax,
+ otk::Property::atoms.cardinal, &n,
+ (long unsigned**) &dimensions)) {
+ if (n >= 4) {
+ if (dir == 0 || dir == 1) { // horz
+ x = (signed int)dimensions[0];
+ w = (signed int)dimensions[2];
+ }
+ if (dir == 0 || dir == 2) { // vert
+ y = (signed int)dimensions[1];
+ h = (signed int)dimensions[3];
+ }
+ }
+ delete dimensions;
+ } else {
+ // pick some fallbacks...
+ if (dir == 0 || dir == 1) { // horz
+ x = a.x() + a.width() / 4;
+ w = a.width() / 2;
+ }
+ if (dir == 0 || dir == 2) { // vert
+ y = a.y() + a.height() / 4;
+ h = a.height() / 2;
+ }
+ }
+ }
+
+ if (dir == 0 || dir == 1) // horz
+ _max_horz = max;
+ if (dir == 0 || dir == 2) // vert
+ _max_vert = max;
+
+ if (!_max_horz && !_max_vert)
+ otk::Property::erase(_window, otk::Property::atoms.openbox_premax);
+
+ changeState(); // change the state hints on the client
+
+ frame->frameGravity(x, y); // figure out where the client should be going
+ internal_resize(TopLeft, w, h, true, x, y);