news download themes documentation links










Button.cc

00001 // Button.cc for FbTk - fluxbox toolkit
00002 // Copyright (c) 2002-2003 Henrik Kinnunen (fluxgen at users.sourceforge.net)
00003 //
00004 // Permission is hereby granted, free of charge, to any person obtaining a
00005 // copy of this software and associated documentation files (the "Software"),
00006 // to deal in the Software without restriction, including without limitation
00007 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 // and/or sell copies of the Software, and to permit persons to whom the
00009 // Software is furnished to do so, subject to the following conditions:
00010 //
00011 // The above copyright notice and this permission notice shall be included in
00012 // all copies or substantial portions of the Software.
00013 //
00014 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00017 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00019 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00020 // DEALINGS IN THE SOFTWARE.
00021 
00022 // $Id: Button.cc,v 1.17 2004/01/08 22:05:12 fluxgen Exp $
00023 
00024 #include "Button.hh"
00025 
00026 #include "Command.hh"
00027 #include "EventManager.hh"
00028 #include "App.hh"
00029 
00030 namespace FbTk {
00031 
00032 Button::Button(int screen_num, int x, int y,
00033                unsigned int width, unsigned int height):
00034     FbWindow(screen_num, x, y, width, height,
00035              ExposureMask | ButtonPressMask | ButtonReleaseMask),
00036     m_foreground_pm(0),
00037     m_background_pm(0),
00038     m_pressed_pm(0),
00039     m_pressed_color("black", screen_num),
00040     m_gc(DefaultGC(FbTk::App::instance()->display(), screen_num)),
00041     m_pressed(false) {
00042 
00043     // add this to eventmanager
00044     FbTk::EventManager::instance()->add(*this, *this); 
00045 }
00046 
00047 Button::Button(const FbWindow &parent, int x, int y, 
00048                unsigned int width, unsigned int height):
00049     FbWindow(parent, x, y, width, height,
00050              ExposureMask | ButtonPressMask | ButtonReleaseMask),
00051     m_foreground_pm(0),
00052     m_background_pm(0),
00053     m_pressed_pm(0),
00054     m_pressed_color("black", parent.screenNumber()),
00055     m_gc(DefaultGC(FbTk::App::instance()->display(), screenNumber())),
00056     m_pressed(false) {
00057     // add this to eventmanager
00058     FbTk::EventManager::instance()->add(*this, *this);
00059 }
00060 
00061 Button::~Button() {
00062 
00063 }
00064 
00065 void Button::setOnClick(RefCount<Command> &cmd, int button) {
00066     // we only handle buttons 1 to 5
00067     if (button > 5 || button == 0)
00068         return;
00069     //set on click command for the button
00070     m_onclick[button - 1] = cmd;
00071 }
00072 
00073 void Button::setPixmap(Pixmap pm) {
00074     m_foreground_pm = pm;
00075 }
00076 
00077 void Button::setPressedPixmap(Pixmap pm) {
00078     m_pressed_pm = pm;
00079 }
00080 
00081 void Button::setPressedColor(const FbTk::Color &color) {
00082     m_pressed_color = color;
00083 }
00084 
00085 void Button::setBackgroundColor(const Color &color) {
00086     m_background_pm = 0; // we're using background color now
00087     m_background_color = color;    
00088     FbTk::FbWindow::setBackgroundColor(color);
00089 }
00090 
00091 void Button::setBackgroundPixmap(Pixmap pm) {
00092     m_background_pm = pm;
00093     FbTk::FbWindow::setBackgroundPixmap(pm);
00094 }
00095 
00096 void Button::buttonPressEvent(XButtonEvent &event) {
00097     if (m_pressed_pm != 0)
00098         FbWindow::setBackgroundPixmap(m_pressed_pm);
00099     else if (m_pressed_color.isAllocated())
00100         FbWindow::setBackgroundColor(m_pressed_color);
00101         
00102     m_pressed = true;    
00103     clear();
00104     updateTransparent();
00105 }
00106 
00107 void Button::buttonReleaseEvent(XButtonEvent &event) {
00108     m_pressed = false;
00109     if (m_background_pm)
00110         setBackgroundPixmap(m_background_pm);
00111     else
00112         setBackgroundColor(m_background_color);
00113 
00114     clear(); // clear background
00115 
00116     if (m_foreground_pm) { // draw foreground pixmap
00117         Display *disp = App::instance()->display();
00118 
00119         if (m_gc == 0) // get default gc if we dont have one
00120             m_gc = DefaultGC(disp, screenNumber());
00121 
00122         XCopyArea(disp, m_foreground_pm, window(), m_gc, 0, 0, width(), height(), 0, 0);
00123     }
00124 
00125     updateTransparent();
00126 
00127     // finaly, execute command (this must be done last since this object might be deleted by the command)
00128     if (event.button > 0 && event.button <= 5 &&
00129         event.x > 0 && event.x < static_cast<signed>(width()) &&
00130         event.y > 0 && event.y < static_cast<signed>(height()) &&
00131         m_onclick[event.button -1].get() != 0)
00132         m_onclick[event.button - 1]->execute();
00133 
00134 
00135 }
00136 
00137 void Button::exposeEvent(XExposeEvent &event) {
00138     if (m_background_pm)
00139         setBackgroundPixmap(m_background_pm);
00140     else
00141         setBackgroundColor(m_background_color);
00142 
00143     clearArea(event.x, event.y, event.width, event.height);
00144     updateTransparent(event.x, event.y, event.width, event.height);
00145 }
00146 
00147 }; // end namespace FbTk

Fluxbox CVS-Jan-2003




      



Got comments about the page? Send them to webmaster.
If you have general Fluxbox related questions ask them on our irc channel or mailing lists.

Show Source








Designed by aLEczapKA