+ Window *windows;
+ unsigned int size = _stacking.size();
+
+ assert(size == clients.size()); // just making sure.. :)
+
+
+ // create an array of the window ids (from bottom to top, reverse order!)
+ if (size > 0) {
+ Window *win_it;
+
+ windows = new Window[size];
+ win_it = windows;
+ ClientList::const_reverse_iterator it = _stacking.rbegin();
+ const ClientList::const_reverse_iterator end = _stacking.rend();
+ for (; it != end; ++it, ++win_it)
+ *win_it = (*it)->window();
+ } else
+ windows = (Window*) 0;
+
+ otk::Property::set(_info->rootWindow(),
+ otk::Property::atoms.net_client_list_stacking,
+ otk::Property::atoms.window, windows, size);
+
+ if (size)
+ delete [] windows;
+}
+
+
+void Screen::manageWindow(Window window)
+{
+ Client *client = 0;
+ XWMHints *wmhint;
+ XSetWindowAttributes attrib_set;
+ XEvent e;
+ XWindowAttributes attrib;
+
+ otk::display->grab();
+
+ // check if it has already been unmapped by the time we started mapping
+ // the grab does a sync so we don't have to here
+ if (XCheckTypedWindowEvent(**otk::display, window, DestroyNotify, &e) ||
+ XCheckTypedWindowEvent(**otk::display, window, UnmapNotify, &e)) {
+ XPutBackEvent(**otk::display, &e);
+
+ otk::display->ungrab();
+ return; // don't manage it
+ }
+
+ if (!XGetWindowAttributes(**otk::display, window, &attrib) ||
+ attrib.override_redirect) {
+ otk::display->ungrab();
+ return; // don't manage it
+ }
+
+ // is the window a docking app
+ if ((wmhint = XGetWMHints(**otk::display, window))) {
+ if ((wmhint->flags & StateHint) &&
+ wmhint->initial_state == WithdrawnState) {
+ //slit->addClient(w); // XXX: make dock apps work!
+
+ otk::display->ungrab();
+ XFree(wmhint);
+ return;
+ }
+ XFree(wmhint);
+ }
+
+ // choose the events we want to receive on the CLIENT window
+ attrib_set.event_mask = Client::event_mask;
+ attrib_set.do_not_propagate_mask = Client::no_propagate_mask;
+ XChangeWindowAttributes(**otk::display, window,
+ CWEventMask|CWDontPropagate, &attrib_set);
+
+ // create the Client class, which gets all of the hints on the window
+ client = new Client(_number, window);
+ // register for events
+ openbox->registerHandler(window, client);
+ // add to the wm's map
+ openbox->addClient(window, client);
+
+ // we dont want a border on the client
+ client->toggleClientBorder(false);
+
+ // specify that if we exit, the window should not be destroyed and should be
+ // reparented back to root automatically
+ XChangeSaveSet(**otk::display, window, SetModeInsert);
+
+ // create the decoration frame for the client window
+ client->frame = new Frame(client);
+ // register the plate for events (map req's)
+ // this involves removing itself from the handler list first, since it is
+ // auto added to the list, being a widget. we won't get any events on the
+ // plate except for events for the client (SubstructureRedirectMask)
+ openbox->clearHandler(client->frame->plate());
+ openbox->registerHandler(client->frame->plate(), client);
+
+ // add to the wm's map
+ Window *w = client->frame->allWindows();
+ for (unsigned int i = 0; w[i]; ++i)
+ openbox->addClient(w[i], client);
+ delete [] w;
+
+ // reparent the client to the frame
+ client->frame->grabClient();
+
+ if (openbox->state() != Openbox::State_Starting) {
+ // position the window intelligenty .. hopefully :)
+ // call the python PLACEWINDOW binding
+ EventData data(_number, client, EventAction::PlaceWindow, 0);
+ openbox->bindings()->fireEvent(&data);
+ }
+
+ EventData ddata(_number, client, EventAction::DisplayingWindow, 0);
+ openbox->bindings()->fireEvent(&ddata);
+
+ client->showhide();
+
+ client->applyStartupState();
+
+ otk::display->ungrab();
+
+ // add to the screen's list
+ clients.push_back(client);
+ // once the client is in the list, update our strut to include the new
+ // client's (it is good that this happens after window placement!)
+ updateStruts();
+ // this puts into the stacking order, then raises it
+ _stacking.push_back(client);
+ raiseWindow(client);
+ // update the root properties
+ changeClientList();
+
+ openbox->bindings()->grabButtons(true, client);
+
+ EventData ndata(_number, client, EventAction::NewWindow, 0);
+ openbox->bindings()->fireEvent(&ndata);
+
+#ifdef DEBUG
+ printf("Managed window 0x%lx frame 0x%lx\n",
+ window, client->frame->window());
+#endif
+}
+
+
+void Screen::unmanageWindow(Client *client)
+{
+ Frame *frame = client->frame;
+
+ // call the python CLOSEWINDOW binding
+ EventData data(_number, client, EventAction::CloseWindow, 0);
+ openbox->bindings()->fireEvent(&data);
+
+ openbox->bindings()->grabButtons(false, client);
+
+ // remove from the wm's map
+ openbox->removeClient(client->window());
+ Window *w = frame->allWindows();
+ for (unsigned int i = 0; w[i]; ++i)
+ openbox->addClient(w[i], client);
+ delete [] w;
+ // unregister for handling events
+ openbox->clearHandler(client->window());
+
+ // remove the window from our save set
+ XChangeSaveSet(**otk::display, client->window(), SetModeDelete);
+
+ // we dont want events no more
+ XSelectInput(**otk::display, client->window(), NoEventMask);
+
+ frame->hide();
+
+ // give the client its border back
+ client->toggleClientBorder(true);
+
+ // reparent the window out of the frame
+ frame->releaseClient();
+
+#ifdef DEBUG
+ Window framewin = client->frame->window();
+#endif
+ delete client->frame;
+ client->frame = 0;
+
+ // remove from the stacking order
+ _stacking.remove(client);
+
+ // remove from the screen's list
+ clients.remove(client);
+
+ // once the client is out of the list, update our strut to remove it's
+ // influence
+ updateStruts();
+
+ // unset modal before dropping our focus
+ client->_modal = false;
+
+ // unfocus the client (calls the focus callbacks)
+ if (client->focused()) client->unfocus();
+
+#ifdef DEBUG
+ printf("Unmanaged window 0x%lx frame 0x%lx\n", client->window(), framewin);
+#endif
+
+ delete client;
+
+ // update the root properties
+ 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
+
+ Client *m = client->findModalChild();
+ // if we have a modal child, raise it instead, we'll go along tho later
+ if (m) raiseWindow(m);
+
+ // 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() || m == *it); ++it);
+