button.cpp

Go to the documentation of this file.
00001 /*
00002   libwt - Vassilis Virvilis Toolkit - a widget library
00003   Copyright (C) 2006 Vassilis Virvilis <vasvir2@fastmail.fm>
00004  
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2.1 of the License, or (at your option) any later version.
00009   
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014   
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the
00017   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018   Boston, MA  02111-1307, SA.
00019 */
00020 
00021 #include <wt/event.h>
00022 #include <wt/button.h>
00023 
00024 namespace Wt {
00025 
00026 void Button::init() {
00027     pressed.connect(sigc::mem_fun(*this, &Button::buttonPressEvent));
00028     canceled.connect(sigc::mem_fun(*this, &Button::buttonCancelEvent));
00029     released.connect(sigc::mem_fun(*this, &Button::buttonReleaseEvent));
00030     clicked.connect(sigc::mem_fun(*this, &Button::buttonClickEvent));
00031 }
00032 
00033 Button::Button(Widget *parent, const std::string &name, int wflags)
00034         : Widget(parent, name, wflags),
00035         text_(""),
00036         mousepress_in(false),
00037 keypress_in(false) {
00038     focusPolicy = StrongFocus;
00039     init();
00040 }
00041 
00042 void Button::setText(const std::string &text) {
00043     text_ = text;
00044     update();
00045 }
00046 
00047 void Button::setPixmap(const Pixmap& pixmap) {
00048     pixmap_ = pixmap;
00049     update();
00050 }
00051 
00052 void Button::setSound(const Sound& snd) {
00053     click_snd = snd;
00054     clicked.connect(sigc::mem_fun(click_snd, &Sound::play));
00055 }
00056 
00057 bool Button::isExclusiveToggle() const {
00058     return false;
00059 }
00060 
00061 ButtonGroup *Button::group() const {
00062     return 0;
00063 }
00064 
00065 void Button::animateClick() {
00066     pressed();
00067     released();
00068     clicked();
00069 }
00070 
00071 bool Button::hitButton(const Point &) const {
00072     return true;
00073 }
00074 
00075 void Button::drawButton(Painter *) {}
00076 void Button::drawButtonLabel(Painter *) {}
00077 
00078 void Button::draw(Painter *p, const Region& r) {
00079     Widget::draw(p, r);
00080     drawButton(p);
00081     drawButtonLabel(p);
00082 }
00083 
00084 void Button::mousePressEvent(MouseEvent *me) {
00085     Widget::mousePressEvent(me);
00086     if (me->button() != LeftButton)
00087         return;
00088     if (hitButton(me->pos())) {
00089         mousepress_in = true;
00090         pressed();
00091     }
00092 }
00093 
00094 void Button::mouseReleaseEvent(MouseEvent *me) {
00095     Widget::mouseReleaseEvent(me);
00096     if (me->button() != LeftButton)
00097         return;
00098     if (hitButton(me->pos())) {
00099         released();
00100         if (mousepress_in) {
00101             clicked();
00102         }
00103     }
00104     mousepress_in = false;
00105 }
00106 
00107 void Button::enterEvent(Event *e) {
00108     Widget::enterEvent(e);
00109     mousepress_in = false;
00110 }
00111 
00112 void Button::leaveEvent(Event *e) {
00113     Widget::leaveEvent(e);
00114     if (mousepress_in)
00115         canceled();
00116     mousepress_in = false;
00117 }
00118 
00119 void Button::keyPressEvent(KeyEvent *ke) {
00120     Widget::keyPressEvent(ke);
00121     if (ke->key() != Key_Space &&
00122             ke->key() != Key_Enter &&
00123             ke->key() != Key_Return)
00124         return;
00125     keypress_in = true;
00126     pressed();
00127 }
00128 
00129 void Button::keyReleaseEvent(KeyEvent *ke) {
00130     Widget::keyReleaseEvent(ke);
00131     if (ke->key() != Key_Space &&
00132             ke->key() != Key_Enter &&
00133             ke->key() != Key_Return)
00134         return;
00135     released();
00136     if (keypress_in) {
00137         clicked();
00138     }
00139     keypress_in = false;
00140 }
00141 
00142 void Button::focusInEvent(FocusEvent *fe) {
00143     Widget::focusInEvent(fe);
00144     trace("button") << "have focus " << std::endl;
00145     keypress_in = false;
00146 }
00147 void Button::focusOutEvent(FocusEvent *fe) {
00148     Widget::focusOutEvent(fe);
00149     trace("button") << "lost focus " << std::endl;
00150     if (keypress_in)
00151         canceled();
00152     keypress_in = false;
00153 }
00154 
00155 void Button::update() {
00156     setButtonSize();
00157     Widget::update();
00158 }
00159 
00160 void Button::buttonPressEvent() {
00161     trace("button") << "Pressed" << std::endl;
00162 }
00163 
00164 void Button::buttonCancelEvent() {
00165     trace("button") << "Canceled" << std::endl;
00166 }
00167 
00168 void Button::buttonReleaseEvent() {
00169     trace("button") << "Released" << std::endl;
00170 }
00171 
00172 void Button::buttonClickEvent() {
00173     trace("button") << "Clicked" << std::endl;
00174 }
00175 
00176 } // namespace Wt

Generated Fri Jul 28 19:22:59 2006.
Copyright © 1998-2003 by the respective authors.

This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.