pushbutton.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/pushbutton.h>
00022 #include <wt/painter.h>
00023 
00024 namespace Wt {
00025 
00026 void PushButton::setButtonSize() {
00027     //int align = alignment;
00028     const Pixmap& px = pixmap();
00029     Size pixmap_size, text_size;
00030 
00031     //w = h = 2 * margin;
00032 
00033     if (px) {
00034         pixmap_size = px.size();
00035     }
00036     text_size = font().size(text());
00037     const Size text_hint_size = Size(text_size.width() * 2,
00038                                      text_size.height() * 3 / 2);
00039 
00040     if (px && !pixmap_size.contains(text_size)) {
00041         Warn("Text '%s' bigger than pixmap for button %s\n",
00042              text().c_str(), name().c_str());
00043     }
00044 
00045     setSizePolicy(SizePolicy(px ? SizePolicy::Fixed
00046                              :SizePolicy::Minimum,
00047                              SizePolicy::Fixed, 0, 0));
00048     const Size min_size = (px) ? pixmap_size : text_size;
00049     const Size hint_size = (px) ? pixmap_size : text_hint_size;
00050 
00051     trace("pushbutton") << this << " min_size " << min_size <<
00052     " hint_size " << hint_size << std::endl;
00053 
00054     setSizeHint(hint_size);
00055     setMinimumSize(min_size);
00056 }
00057 
00058 void PushButton::init() {
00059     down.changed.connect(
00060         sigc::slot0<void>(
00061             sigc::mem_fun(*this, &PushButton::onButtonStatusChange)));
00062 }
00063 
00064 PushButton::PushButton(Widget *parent, const std::string &name)
00065         : Button(parent, name),
00066 down(false) {
00067     init();
00068 }
00069 
00070 void PushButton::setPixmap(const Pixmap& pixmap_up, const Pixmap& pixmap_down) {
00071     pixmap_up_ = pixmap_up;
00072     pixmap_down_ = pixmap_down;
00073     onButtonStatusChange();
00074 }
00075 
00076 void PushButton::drawButton(Painter *p) {
00077     const Pixmap& px = pixmap();
00078     if (!px)
00079         return;
00080     p->drawPixmap(Point(0, 0), px);
00081     if (!enabled) {
00082         Color disabled = "gray30";
00083         disabled.setAlpha(35);
00084         p->blendRect(px.rect(), Brush(disabled));
00085     }
00086 }
00087 
00088 void PushButton::drawButtonLabel(Painter *p) {
00089     if (text().length()) {
00090         const Size s = font().size(text());
00091         const Rect dst = Rect::align(rect(), s, alignment);
00092         Point offset(dst.x(),
00093                      dst.y() + font().ascent() + ((down) ? 1 : 0));
00094         if (!enabled) {
00095             p->save();
00096             p->setPen(Pen("gray30"));
00097             p->drawText(offset, text());
00098             p->restore();
00099         } else {
00100             p->drawText(offset, text());
00101         }
00102     }
00103 }
00104 
00105 void PushButton::buttonPressEvent() {
00106     trace("pushbutton") << "Pressed" << std::endl;
00107     down = true;
00108 }
00109 
00110 void PushButton::buttonCancelEvent() {
00111     trace("pushbutton") << "Canceled" << std::endl;
00112     down = false;
00113 }
00114 
00115 void PushButton::buttonReleaseEvent() {
00116     trace("pushbutton") << "Released" << std::endl;
00117     down = false;
00118 }
00119 
00120 void PushButton::buttonClickEvent() {
00121     trace("pushbutton") << "Clicked" << std::endl;
00122 }
00123 
00124 void PushButton::onButtonStatusChange() {
00125     trace("pushbutton", "ButtonStatusChanged\n");
00126     setPixmap(down ? pixmap_down_ : pixmap_up_);
00127 }
00128 
00129 } // namespace Wt

Generated Fri Jul 28 19:23:00 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.