+ changeClientList();
+}
+
+void Screen::lowerWindow(Client *client)
+{
+ Window wins[2]; // only ever restack 2 windows.
+
+ assert(!_stacking.empty()); // this would be bad
+
+ ClientList::iterator it = --_stacking.end();
+ const ClientList::iterator end = _stacking.begin();
+
+ if (client->modal() && client->transientFor()) {
+ // don't let a modal window lower below its transient_for
+ it = std::find(_stacking.begin(), _stacking.end(), client->transientFor());
+ assert(it != _stacking.end());
+
+ wins[0] = (it == _stacking.begin() ? _focuswindow :
+ ((*(--ClientList::const_iterator(it)))->frame->window()));
+ wins[1] = client->frame->window();
+ if (wins[0] == wins[1]) return; // already right above the window
+
+ _stacking.remove(client);
+ _stacking.insert(it, client);
+ } else {
+ for (; it != end && (*it)->layer() < client->layer(); --it);
+ if (*it == client) return; // already the bottom, return
+
+ wins[0] = (*it)->frame->window();
+ wins[1] = client->frame->window();
+
+ _stacking.remove(client);
+ _stacking.insert(++it, client);
+ }
+
+ XRestackWindows(**otk::display, wins, 2);
+ changeStackingList();
+}
+
+void Screen::raiseWindow(Client *client)
+{
+ Window wins[2]; // only ever restack 2 windows.
+
+ assert(!_stacking.empty()); // this would be bad
+
+ // remove the client before looking so we can't run into ourselves
+ _stacking.remove(client);
+
+ ClientList::iterator it = _stacking.begin();
+ const ClientList::iterator end = _stacking.end();
+
+ // the stacking list is from highest to lowest
+ for (; it != end && (*it)->layer() > client->layer(); ++it);
+
+ /*
+ if our new position is the top, we want to stack under the _focuswindow
+ otherwise, we want to stack under the previous window in the stack.
+ */
+ wins[0] = (it == _stacking.begin() ? _focuswindow :
+ ((*(--ClientList::const_iterator(it)))->frame->window()));
+ wins[1] = client->frame->window();
+
+ _stacking.insert(it, client);
+
+ XRestackWindows(**otk::display, wins, 2);
+
+ // if the window has a modal child, then raise it after us to put it on top
+ if (client->modalChild())
+ raiseWindow(client->modalChild());
+ else
+ changeStackingList(); // no need to do this twice!
+}
+
+void Screen::changeDesktop(long desktop)
+{
+ if (!(desktop >= 0 && desktop < _num_desktops)) return;
+
+ printf("Moving to desktop %ld\n", desktop);
+
+ long old = _desktop;
+
+ _desktop = desktop;
+ otk::Property::set(_info->rootWindow(),
+ otk::Property::atoms.net_current_desktop,
+ otk::Property::atoms.cardinal, _desktop);
+
+ if (old == _desktop) return;
+
+ ClientList::iterator it, end = clients.end();
+ for (it = clients.begin(); it != end; ++it) {
+ if ((*it)->desktop() == old) {
+ (*it)->frame->hide();
+ } else if ((*it)->desktop() == _desktop) {
+ (*it)->frame->show();
+ }
+ }
+
+ // force the callbacks to fire
+ if (!openbox->focusedClient())
+ openbox->setFocusedClient(0);
+}
+
+void Screen::changeNumDesktops(long num)
+{
+ assert(num > 0);
+
+ if (!(num > 0)) return;
+
+ // move windows on desktops that will no longer exist!
+ ClientList::iterator it, end = clients.end();
+ for (it = clients.begin(); it != end; ++it) {
+ int d = (*it)->desktop();
+ if (d >= num && !(d == (signed) 0xffffffff ||
+ d == Client::ICONIC_DESKTOP)) {
+ XEvent ce;
+ ce.xclient.type = ClientMessage;
+ ce.xclient.message_type = otk::Property::atoms.net_wm_desktop;
+ ce.xclient.display = **otk::display;
+ ce.xclient.window = (*it)->window();
+ ce.xclient.format = 32;
+ ce.xclient.data.l[0] = num - 1;
+ XSendEvent(**otk::display, _info->rootWindow(), False,
+ SubstructureNotifyMask | SubstructureRedirectMask, &ce);
+ }
+ }
+
+ _num_desktops = num;
+ otk::Property::set(_info->rootWindow(),
+ otk::Property::atoms.net_number_of_desktops,
+ otk::Property::atoms.cardinal, _num_desktops);
+
+ // set the viewport hint
+ unsigned long *viewport = new unsigned long[_num_desktops * 2];
+ memset(viewport, 0, sizeof(unsigned long) * _num_desktops * 2);
+ otk::Property::set(_info->rootWindow(),
+ otk::Property::atoms.net_desktop_viewport,
+ otk::Property::atoms.cardinal,
+ viewport, _num_desktops * 2);
+ delete [] viewport;
+
+ // update the work area hint
+ changeWorkArea();
+
+ // change our desktop if we're on one that no longer exists!
+ if (_desktop >= num)
+ changeDesktop(num - 1);
+}
+
+
+void Screen::updateDesktopNames()
+{
+ unsigned long num = (unsigned) -1;
+
+ if (!otk::Property::get(_info->rootWindow(),
+ otk::Property::atoms.net_desktop_names,
+ otk::Property::utf8, &num, &_desktop_names))
+ _desktop_names.clear();
+ while ((long)_desktop_names.size() < _num_desktops)
+ _desktop_names.push_back("Unnamed");
+}
+
+
+void Screen::setDesktopName(long i, const otk::ustring &name)
+{
+ assert(i >= 0);
+
+ if (i >= _num_desktops) return;
+
+ otk::Property::StringVect newnames = _desktop_names;
+ newnames[i] = name;
+ otk::Property::set(_info->rootWindow(),
+ otk::Property::atoms.net_desktop_names,
+ otk::Property::utf8, newnames);
+}
+
+
+void Screen::installColormap(bool install) const
+{
+ if (install)
+ XInstallColormap(**otk::display, _info->colormap());
+ else
+ XUninstallColormap(**otk::display, _info->colormap());
+}
+
+
+void Screen::propertyHandler(const XPropertyEvent &e)
+{
+ otk::EventHandler::propertyHandler(e);
+
+ // compress changes to a single property into a single change
+ XEvent ce;
+ while (XCheckTypedWindowEvent(**otk::display, _info->rootWindow(),
+ e.type, &ce)) {
+ // XXX: it would be nice to compress ALL changes to a property, not just
+ // changes in a row without other props between.
+ if (ce.xproperty.atom != e.atom) {
+ XPutBackEvent(**otk::display, &ce);
+ break;
+ }
+ }
+
+ if (e.atom == otk::Property::atoms.net_desktop_names)
+ updateDesktopNames();
+}
+
+
+void Screen::clientMessageHandler(const XClientMessageEvent &e)
+{
+ otk::EventHandler::clientMessageHandler(e);
+
+ if (e.format != 32) return;
+
+ if (e.message_type == otk::Property::atoms.net_current_desktop) {
+ changeDesktop(e.data.l[0]);
+ } else if (e.message_type == otk::Property::atoms.net_number_of_desktops) {
+ changeNumDesktops(e.data.l[0]);
+ }
+}
+
+
+void Screen::mapRequestHandler(const XMapRequestEvent &e)
+{
+ otk::EventHandler::mapRequestHandler(e);
+
+#ifdef DEBUG
+ printf("MapRequest for 0x%lx\n", e.window);
+#endif // DEBUG
+
+ Client *c = openbox->findClient(e.window);
+ if (c) {
+#ifdef DEBUG
+ printf("DEBUG: MAP REQUEST CAUGHT IN SCREEN. IGNORED.\n");
+#endif
+ } else
+ manageWindow(e.window);