Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

/src/buttonwidget.cc

Go to the documentation of this file.
00001 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 # include "../config.h"
00005 #endif
00006 
00007 #include "buttonwidget.hh"
00008 #include "client.hh"
00009 
00010 namespace ob {
00011 
00012 ButtonWidget::ButtonWidget(otk::Widget *parent,
00013                            WidgetBase::WidgetType type,
00014                            Client *client)
00015   : otk::Widget(parent),
00016     WidgetBase(type),
00017     _client(client),
00018     _pressed(false),
00019     _button(0),
00020     _state(false)
00021 {
00022 }
00023 
00024 
00025 ButtonWidget::~ButtonWidget()
00026 {
00027 }
00028 
00029 
00030 void ButtonWidget::setTextures()
00031 {
00032   bool p = _pressed;
00033 
00034   switch (type()) {
00035   case Type_AllDesktopsButton:
00036     if (_client->desktop() == (signed)0xffffffff)
00037       p = true;
00038     break;
00039   case Type_MaximizeButton:
00040     if (_client->maxHorz() || _client->maxVert())
00041       p = true;
00042     break;
00043   default:
00044     break;
00045   }
00046   
00047   switch (type()) {
00048   case Type_LeftGrip:
00049   case Type_RightGrip:
00050     if (_focused)
00051       setTexture(_style->gripFocusBackground());
00052     else
00053       setTexture(_style->gripUnfocusBackground());
00054     break;
00055   case Type_AllDesktopsButton:
00056   case Type_MaximizeButton:
00057   case Type_CloseButton:
00058   case Type_IconifyButton:
00059     if (p) {
00060       if (_focused)
00061         setTexture(_style->buttonPressFocusBackground());
00062       else
00063         setTexture(_style->buttonPressUnfocusBackground());
00064     } else {
00065       if (_focused)
00066         setTexture(_style->buttonUnpressFocusBackground());
00067       else
00068         setTexture(_style->buttonUnpressUnfocusBackground());
00069     }
00070     break;
00071   default:
00072     assert(false); // there's no other button widgets!
00073   }
00074 }
00075 
00076 
00077 void ButtonWidget::setStyle(otk::RenderStyle *style)
00078 {
00079   otk::Widget::setStyle(style);
00080   setTextures();
00081 
00082   switch (type()) {
00083   case Type_LeftGrip:
00084   case Type_RightGrip:
00085     setBorderColor(_style->frameBorderColor());
00086     break;
00087   case Type_AllDesktopsButton:
00088   case Type_CloseButton:
00089   case Type_MaximizeButton:
00090   case Type_IconifyButton:
00091     break;
00092   default:
00093     assert(false); // there's no other button widgets!
00094   }
00095 }
00096 
00097 
00098 void ButtonWidget::update()
00099 {
00100   switch (type()) {
00101   case Type_AllDesktopsButton:
00102     if ((_client->desktop() == (signed)0xffffffff) != _state) {
00103       _state = !_state;
00104       setTextures();
00105     }
00106     break;
00107   case Type_MaximizeButton:
00108     if ((_client->maxHorz() || _client->maxVert()) != _state) {
00109       _state = !_state;
00110       setTextures();
00111     }
00112     break;
00113   default:
00114     break;
00115   }
00116   
00117   otk::Widget::update();
00118 }
00119 
00120 
00121 void ButtonWidget::renderForeground()
00122 {
00123   otk::PixmapMask *pm;
00124   int width;
00125   bool draw = _dirty;
00126 
00127   otk::Widget::renderForeground();
00128 
00129   if (draw) {
00130     switch (type()) {
00131     case Type_AllDesktopsButton:
00132       pm = _style->alldesktopsMask();
00133       break;
00134     case Type_CloseButton:
00135       pm = _style->closeMask();
00136       break;
00137     case Type_MaximizeButton:
00138       pm = _style->maximizeMask();
00139       break;
00140     case Type_IconifyButton:
00141       pm = _style->iconifyMask();
00142       break;
00143     case Type_LeftGrip:
00144     case Type_RightGrip:
00145       return; // no drawing
00146     default:
00147       assert(false); // there's no other button widgets!
00148     }
00149 
00150     assert(pm->mask);
00151     if (pm->mask == None) return; // no mask for the button, leave it empty
00152 
00153     width = _rect.width();
00154 
00155     otk::RenderColor *color = (_focused ? _style->buttonFocusColor() :
00156                                _style->buttonUnfocusColor());
00157 
00158     // set the clip region
00159     int x = (width - pm->w) / 2, y = (width - pm->h) / 2;
00160     XSetClipMask(**otk::display, color->gc(), pm->mask);
00161     XSetClipOrigin(**otk::display, color->gc(), x, y);
00162 
00163     // fill in the clipped region
00164     XFillRectangle(**otk::display, _surface->pixmap(), color->gc(), x, y,
00165                    x + pm->w, y + pm->h);
00166 
00167     // unset the clip region
00168     XSetClipMask(**otk::display, color->gc(), None);
00169     XSetClipOrigin(**otk::display, color->gc(), 0, 0);
00170   }
00171 }
00172 
00173 
00174 void ButtonWidget::adjust()
00175 {
00176   // nothing to adjust. no children.
00177 }
00178 
00179 
00180 void ButtonWidget::focus()
00181 {
00182   otk::Widget::focus();
00183   setTextures();
00184 }
00185 
00186 
00187 void ButtonWidget::unfocus()
00188 {
00189   otk::Widget::unfocus();
00190   setTextures();
00191 }
00192 
00193 
00194 void ButtonWidget::buttonPressHandler(const XButtonEvent &e)
00195 {
00196   otk::Widget::buttonPressHandler(e);
00197   if (_button) return;
00198   _button = e.button;
00199   _pressed = true;
00200   setTextures();
00201   update();
00202 }
00203 
00204 
00205 void ButtonWidget::buttonReleaseHandler(const XButtonEvent &e)
00206 {
00207   otk::Widget::buttonPressHandler(e);
00208   if (e.button != _button) return;
00209   _button = 0;
00210   _pressed = false;
00211   setTextures();
00212   update();
00213 }
00214 
00215 }

Generated on Tue Feb 4 22:58:57 2003 for Openbox by doxygen1.3-rc2