From: Dana Jansens Date: Thu, 13 Feb 2003 10:23:22 +0000 (+0000) Subject: display icons in the frame titlebar! X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=07305ec7188fd44f7b6543a3f1f53bc0db3b090d;p=chaz%2Fopenbox display icons in the frame titlebar! --- diff --git a/src/client.cc b/src/client.cc index 3175b3ba..353bda69 100644 --- a/src/client.cc +++ b/src/client.cc @@ -19,6 +19,7 @@ extern "C" { #define _(str) gettext(str) } +#include // for memcpy #include #include #include @@ -46,6 +47,7 @@ Client::Client(int screen, Window window) _disabled_decorations = 0; _group = None; _desktop = 0; + _nicons = 0; getArea(); getDesktop(); @@ -74,7 +76,8 @@ Client::Client(int screen, Window window) updateIconTitle(); updateClass(); updateStrut(); - + updateIcons(); + // this makes sure that these windows appear on all desktops if (/*_type == Type_Dock ||*/ _type == Type_Desktop) _desktop = 0xffffffff; @@ -89,6 +92,11 @@ Client::Client(int screen, Window window) Client::~Client() { + assert(_nicons > 0); // there should always be a default.. + for (int j = 0; j < _nicons; ++j) + delete [] _icons[j].data; + delete [] _icons; + // clean up childrens' references while (!_transients.empty()) { _transients.front()->_transient_for = 0; @@ -207,7 +215,7 @@ void Client::getType() void Client::setupDecorAndFunctions() { // start with everything (cept fullscreen) - _decorations = Decor_Titlebar | Decor_Handle | Decor_Border | + _decorations = Decor_Titlebar | Decor_Handle | Decor_Border | Decor_Icon | Decor_AllDesktops | Decor_Iconify | Decor_Maximize; _functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize | Func_Shade; @@ -677,6 +685,62 @@ void Client::updateTransientFor() } +void Client::updateIcons() +{ + unsigned long num = (unsigned) -1; + unsigned long *data; + unsigned long w, h, i = 0; + + for (int j = 0; j < _nicons; ++j) + delete [] _icons[j].data; + if (_nicons > 0) + delete [] _icons; + _nicons = 0; + + if (otk::Property::get(_window, otk::Property::atoms.net_wm_icon, + otk::Property::atoms.cardinal, &num, &data)) { + // figure out how man valid icons are in here + while (num - i > 2) { + w = data[i++]; + h = data[i++]; + i += w * h; + if (i > num) break; + ++_nicons; + } + + _icons = new Icon[_nicons]; + + // store the icons + i = 0; + for (int j = 0; j < _nicons; ++j) { + w = _icons[j].w = data[i++]; + h = _icons[j].h = data[i++]; + _icons[j].data = new unsigned long[w * h]; + ::memcpy(_icons[j].data, &data[i], w * h * sizeof(unsigned long)); + i += w * h; + assert(i <= num); + } + printf("i: %lu\n", i); + printf("bleffffffff\n"); + + delete [] data; + } + + if (_nicons <= 0) { + // set the default icon(s) XXX load these from the py + _nicons = 1; + _icons = new Icon[1]; + _icons[i].w = 0; + _icons[i].h = 0; + _icons[i].data = 0; + } + + assert(_nicons > 0); // there should always be a default.. + + if (frame) frame->adjustIcon(); +} + + void Client::propertyHandler(const XPropertyEvent &e) { otk::EventHandler::propertyHandler(e); @@ -720,6 +784,8 @@ void Client::propertyHandler(const XPropertyEvent &e) } else if (e.atom == otk::Property::atoms.net_wm_strut) updateStrut(); + else if (e.atom == otk::Property::atoms.net_wm_icon) + updateIcons(); } @@ -1140,6 +1206,29 @@ void Client::internal_resize(Corner anchor, int w, int h, internal_move(x, y); } +const Icon *Client::icon(const otk::Size &s) const +{ + unsigned long req = s.width() * s.height(); + // si is the smallest image >= req + // li is the largest image < req + unsigned long smallest = 0xffffffff, largest = 0, si = 0, li = 0; + + assert(_nicons > 0); // there should always be a default.. + for (int i = 0; i < _nicons; ++i) { + unsigned long size = _icons[i].w * _icons[i].h; + if (size < smallest && size >= req) { + smallest = size; + si = i; + } + if (size > largest && size <= req) { + largest = size; + li = i; + } + } + if (smallest == 0xffffffff) // didnt find one bigger than us... + return &_icons[li]; + return &_icons[si]; +} void Client::move(int x, int y) { diff --git a/src/client.hh b/src/client.hh index 6c9b74e5..27e247a8 100644 --- a/src/client.hh +++ b/src/client.hh @@ -141,12 +141,13 @@ public: enum Decoration { Decor_Titlebar = 1 << 0, //!< Display a titlebar Decor_Handle = 1 << 1, //!< Display a handle (bottom) Decor_Border = 1 << 2, //!< Display a border - Decor_Iconify = 1 << 3, //!< Display an iconify button - Decor_Maximize = 1 << 4, //!< Display a maximize button + Decor_Icon = 1 << 3, //!< Display the window's icon + Decor_Iconify = 1 << 4, //!< Display an iconify button + Decor_Maximize = 1 << 5, //!< Display a maximize button //! Display a button to toggle the window's placement on //! all desktops - Decor_AllDesktops = 1 << 5, - Decor_Close = 1 << 6 //!< Display a close button + Decor_AllDesktops = 1 << 6, + Decor_Close = 1 << 7 //!< Display a close button }; //! Holds a bitmask of Client::Decoration values typedef unsigned char DecorationFlags; @@ -342,6 +343,11 @@ private: */ FunctionFlags _functions; + //! Icons for the client as specified on the client window + Icon *_icons; + //! The number of icons in _icons + int _nicons; + //! Retrieves the window's initial gravity void getGravity(); //! Retrieves the desktop hint's value and sets Client::_desktop @@ -406,6 +412,8 @@ private: void updateStrut(); //! Updates the window's transient status, and any parents of it void updateTransientFor(); + //! Updates the window's icons + void updateIcons(); //! Change the client's state hints to match the class' data void changeState(); @@ -639,6 +647,15 @@ BB @param window The window id that the Client class should handle //! Returns the client's strut definition inline const otk::Strut &strut() const { return _strut; } + //! Returns an icon for the window + /*! + The icon chosen will be the smallest icon available that is still bigger or + equal to the specified Size.
+ If none that meet the requirements is found, the largest icon that is + smaller than the specified size will be returned. + */ + const Icon *icon(const otk::Size &s) const; + //! Move the window (actually, its frame) to a position. /*! This moves the window so that the top-left corner of its frame will be at diff --git a/src/frame.cc b/src/frame.cc index 27e9c490..c9a83452 100644 --- a/src/frame.cc +++ b/src/frame.cc @@ -9,7 +9,6 @@ extern "C" { } #include "frame.hh" -#include "client.hh" #include "openbox.hh" #include "otk/display.hh" #include "otk/surface.hh" @@ -312,10 +311,10 @@ static void render(int screen, const otk::Size &size, Window win, void Frame::adjustSize() { - Client::DecorationFlags decorations = _client->decorations(); + _decorations = _client->decorations(); const otk::RenderStyle *style = otk::RenderStyle::style(_client->screen()); - if (decorations & Client::Decor_Border) { + if (_decorations & Client::Decor_Border) { geom.bwidth = style->frameBorderWidth(); geom.cbwidth = style->clientBorderWidth(); } else { @@ -336,7 +335,7 @@ void Frame::adjustSize() // position/size and map/unmap all the windows - if (decorations & Client::Decor_Titlebar) { + if (_decorations & Client::Decor_Titlebar) { XMoveResizeWindow(**otk::display, _title, -geom.bwidth, -geom.bwidth, geom.width, geom.title_height()); _innersize.top += geom.title_height() + geom.bwidth; @@ -344,10 +343,15 @@ void Frame::adjustSize() // layout the title bar elements layoutTitle(); - } else + } else { XUnmapWindow(**otk::display, _title); + // make all the titlebar stuff not render + _decorations &= ~(Client::Decor_Icon | Client::Decor_Iconify | + Client::Decor_Maximize | Client::Decor_Close | + Client::Decor_AllDesktops); + } - if (decorations & Client::Decor_Handle) { + if (_decorations & Client::Decor_Handle) { geom.handle_y = _innersize.top + _client->area().height() + geom.cbwidth; XMoveResizeWindow(**otk::display, _handle, -geom.bwidth, geom.handle_y, geom.width, geom.handle_height); @@ -385,7 +389,7 @@ void Frame::adjustSize() // render all the elements int screen = _client->screen(); bool focus = _client->focused(); - if (decorations & Client::Decor_Titlebar) { + if (_decorations & Client::Decor_Titlebar) { render(screen, otk::Size(geom.width, geom.title_height()), _title, &_title_sur, *(focus ? style->titlebarFocusBackground() : style->titlebarUnfocusBackground()), false); @@ -398,7 +402,7 @@ void Frame::adjustSize() renderClose(); } - if (decorations & Client::Decor_Handle) { + if (_decorations & Client::Decor_Handle) { render(screen, otk::Size(geom.width, geom.handle_height), _handle, &_handle_sur, *(focus ? style->handleFocusBackground() : style->handleUnfocusBackground())); @@ -495,11 +499,13 @@ static void renderButton(int screen, bool focus, bool press, Window win, XSetWindowBackgroundPixmap(**otk::display, win, s->pixmap()); XClearWindow(**otk::display, win); if (*sur) delete *sur; + s->freePixelData(); *sur = s; } void Frame::renderMax() { + if (!(_decorations & Client::Decor_Maximize)) return; bool press = _max_press || _client->maxVert() || _client->maxHorz(); renderButton(_client->screen(), _client->focused(), press, _max, &_max_sur, geom.button_size, @@ -508,6 +514,7 @@ void Frame::renderMax() void Frame::renderDesk() { + if (!(_decorations & Client::Decor_AllDesktops)) return; bool press = _desk_press || _client->desktop() == 0xffffffff; renderButton(_client->screen(), _client->focused(), press, _desk, &_desk_sur, geom.button_size, @@ -516,6 +523,7 @@ void Frame::renderDesk() void Frame::renderIconify() { + if (!(_decorations & Client::Decor_Iconify)) return; renderButton(_client->screen(), _client->focused(), _iconify_press, _iconify, &_iconify_sur, geom.button_size, otk::RenderStyle::style(_client->screen())->iconifyMask()); @@ -523,6 +531,7 @@ void Frame::renderIconify() void Frame::renderClose() { + if (!(_decorations & Client::Decor_Close)) return; renderButton(_client->screen(), _client->focused(), _close_press, _close, &_close_sur, geom.button_size, otk::RenderStyle::style(_client->screen())->closeMask()); @@ -530,6 +539,7 @@ void Frame::renderClose() void Frame::renderIcon() { + if (!(_decorations & Client::Decor_Icon)) return; const int screen = _client->screen(); const otk::RenderControl *control = otk::display->renderControl(screen); @@ -544,7 +554,10 @@ void Frame::renderIcon() for (int y = 0; y < geom.button_size; ++y, src += w - geom.button_size) for (int x = 0; x < geom.button_size; ++x, ++dest, ++src) *dest = *src; - control->drawImage(*s, 0, 0, 0); + // draw the icon over it + const Icon *icon = _client->icon(otk::Size(geom.button_size, + geom.button_size)); + control->drawImage(*s, icon->w, icon->h, icon->data); XSetWindowBackgroundPixmap(**otk::display, _icon, s->pixmap()); XClearWindow(**otk::display, _icon); @@ -554,69 +567,124 @@ void Frame::renderIcon() void Frame::layoutTitle() { - geom.label_width = geom.width - geom.bevel * 2 - - (geom.button_size + geom.bevel) * (_layout.size() - 1); + // figure out whats being shown, and the width of the label + geom.label_width = geom.width - geom.bevel * 2; + bool n, d, i, t, m ,c; + n = d = i = t = m = c = false; + for (const char *l = _layout.c_str(); *l; ++l) { + switch (*l) { + case 'n': + case 'N': + if (!(_decorations & Client::Decor_Icon)) break; + n = true; + geom.label_width -= geom.button_size + geom.bevel; + break; + case 'd': + case 'D': + if (!(_decorations & Client::Decor_AllDesktops)) break; + d = true; + geom.label_width -= geom.button_size + geom.bevel; + break; + case 'i': + case 'I': + if (!(_decorations & Client::Decor_Iconify)) break; + i = true; + geom.label_width -= geom.button_size + geom.bevel; + break; + case 't': + case 'T': + t = true; + break; + case 'm': + case 'M': + if (!(_decorations & Client::Decor_Maximize)) break; + m = true; + geom.label_width -= geom.button_size + geom.bevel; + break; + case 'c': + case 'C': + if (!(_decorations & Client::Decor_Close)) break; + c = true; + geom.label_width -= geom.button_size + geom.bevel; + break; + } + } if (geom.label_width < 1) geom.label_width = 1; XResizeWindow(**otk::display, _label, geom.label_width, geom.font_height); + if (!n) { + _decorations &= ~Client::Decor_Icon; + XUnmapWindow(**otk::display, _icon); + } + if (!d) { + _decorations &= ~Client::Decor_AllDesktops; + XUnmapWindow(**otk::display, _desk); + } + if (!i) { + _decorations &= ~Client::Decor_Iconify; + XUnmapWindow(**otk::display, _iconify); + } + if (!t) + XUnmapWindow(**otk::display, _label); + if (!m) { + _decorations &= ~Client::Decor_Maximize; + XUnmapWindow(**otk::display, _max); + } + if (!c) { + _decorations &= ~Client::Decor_Close; + XUnmapWindow(**otk::display, _close); + } + int x = geom.bevel; - bool n, d, i, l, m ,c; - n = d = i = l = m = c = false; for (const char *lc = _layout.c_str(); *lc; ++lc) { switch (*lc) { case 'n': case 'N': + if (!n) break; geom.icon_x = x; XMapWindow(**otk::display, _icon); XMoveWindow(**otk::display, _icon, x, geom.bevel + 1); - n = true; x += geom.button_size; break; case 'd': case 'D': + if (!d) break; XMapWindow(**otk::display, _desk); XMoveWindow(**otk::display, _desk, x, geom.bevel + 1); - d = true; x += geom.button_size; break; case 'i': case 'I': + if (!i) break; XMapWindow(**otk::display, _iconify); XMoveWindow(**otk::display, _iconify, x, geom.bevel + 1); - i = true; x += geom.button_size; break; case 't': case 'T': + if (!t) break; XMapWindow(**otk::display, _label); XMoveWindow(**otk::display, _label, x, geom.bevel); - l = true; x += geom.label_width; break; case 'm': case 'M': + if (!m) break; XMapWindow(**otk::display, _max); XMoveWindow(**otk::display, _max, x, geom.bevel + 1); - m = true; x += geom.button_size; break; case 'c': case 'C': + if (!c) break; XMapWindow(**otk::display, _close); XMoveWindow(**otk::display, _close, x, geom.bevel + 1); - c = true; x += geom.button_size; break; } x += geom.bevel; } - if (!n) XUnmapWindow(**otk::display, _icon); - if (!d) XUnmapWindow(**otk::display, _desk); - if (!i) XUnmapWindow(**otk::display, _iconify); - if (!l) XUnmapWindow(**otk::display, _label); - if (!m) XUnmapWindow(**otk::display, _max); - if (!c) XUnmapWindow(**otk::display, _close); } void Frame::adjustPosition() @@ -633,8 +701,6 @@ void Frame::adjustPosition() void Frame::adjustShape() { #ifdef SHAPE - Client::DecorationFlags decorations = _client->decorations(); - if (!_client->shaped()) { // clear the shape on the frame window XShapeCombineMask(**otk::display, _frame, ShapeBounding, @@ -651,7 +717,7 @@ void Frame::adjustShape() int num = 0; XRectangle xrect[2]; - if (decorations & Client::Decor_Titlebar) { + if (_decorations & Client::Decor_Titlebar) { xrect[0].x = -geom.bevel; xrect[0].y = -geom.bevel; xrect[0].width = geom.width + geom.bwidth * 2; @@ -659,7 +725,7 @@ void Frame::adjustShape() ++num; } - if (decorations & Client::Decor_Handle) { + if (_decorations & Client::Decor_Handle) { xrect[1].x = -geom.bevel; xrect[1].y = geom.handle_y; xrect[1].width = geom.width + geom.bwidth * 2; @@ -674,13 +740,16 @@ void Frame::adjustShape() #endif // SHAPE } - void Frame::adjustState() { renderDesk(); renderMax(); } +void Frame::adjustIcon() +{ + renderIcon(); +} void Frame::grabClient() { diff --git a/src/frame.hh b/src/frame.hh index c5a9a312..df353c7d 100644 --- a/src/frame.hh +++ b/src/frame.hh @@ -9,6 +9,7 @@ extern "C" { #include } +#include "client.hh" #include "python.hh" #include "otk/strut.hh" #include "otk/rect.hh" @@ -22,8 +23,6 @@ extern "C" { namespace ob { -class Client; - //! Varius geometry settings in the frame decorations struct FrameGeometry { int width; // title and handle @@ -66,6 +65,9 @@ private: otk::Rect _area; bool _visible; + + //! The decorations that are being displayed in the frame. + Client::DecorationFlags _decorations; // decoration windows Window _frame; // sits under everything @@ -144,6 +146,7 @@ public: void adjustState(); void adjustFocus(); void adjustTitle(); + void adjustIcon(); //! Applies gravity to the client's position to find where the frame should //! be positioned.