window.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/spacer.h>
00023 #include <wt/label.h>
00024 #include <wt/window.h>
00025 #include <wt/application.h>
00026 
00027 namespace Wt {
00028 
00029 /*!
00030 \todo window properties
00031     - border width
00032     - border color
00033 */
00034 
00035 // window frame
00036 Window::Frame::Frame(Widget* parent, const std::string &name, int wflags)
00037         : VBox(0, name, wflags),
00038         titlebar_b(0),
00039         title_(0),
00040 pressed_(false) {
00041     // we have to do it in two steps in order to trick the layout system
00042     // to not manage us. The problem is that Widget constructor issues
00043     // a sendPostedEvents(). By that time this is not yet a Window::Frame
00044     // and the Layout cannot distinguish it
00045     reparent(parent);
00046     // you can't create system top level windows with this frame
00047     assert(parent);
00048 
00049     VBox::setBackgroundColor("gray30");
00050     const bool dialog_border = wflags & W::Style_DialogBorder;
00051     spacing = 0;
00052     lineWidth = (dialog_border) ? 1 : 2;
00053     margin = (dialog_border) ? 1 : 3;
00054 
00055     const bool has_title = wflags & W::Style_Title;
00056     if (!has_title)
00057         return;
00058 
00059     titlebar_b = new HBox(this, "HBox::TitleBar");
00060     SizePolicy sp;
00061     sp.expandData = SizePolicy::Horizontal;
00062     titlebar_b->setSizePolicy(sp);
00063 
00064     titlebar_b->add
00065     (new HSpacer);
00066 
00067     title_ = new Label("Wt::Window", titlebar_b, "TitleBar::Title");
00068     title_->margin = 0;
00069     title_->lineWidth = 0;
00070     title_->spacing = 0;
00071 
00072     titlebar_b->add
00073     (new HSpacer);
00074 }
00075 
00076 void  Window::Frame::setFont(const Font& font) {
00077     if (title_)
00078         title_->setFont(font);
00079 }
00080 
00081 const Font& Window::Frame::font() const {
00082     const Widget *w = (title_) ? title_ : static_cast<const Widget *>(this);
00083     return w->font();
00084 }
00085 
00086 void Window::Frame::setText(const std::string& text) {
00087     if (title_)
00088         title_->setText(text);
00089 }
00090 
00091 std::string Window::Frame::text() const {
00092     return (title_) ? title_->text() : "";
00093 }
00094 
00095 void Window::Frame::setForegroundColor(const Color& color) {
00096     if (titlebar_b)
00097         titlebar_b->setForegroundColor(color);
00098     if (title_)
00099         title_->setForegroundColor(color);
00100 }
00101 
00102 const Color& Window::Frame::foregroundColor() const {
00103     const Widget *w = (titlebar_b) ? titlebar_b : static_cast<const Widget *>(this);
00104     return w->foregroundColor();
00105 }
00106 
00107 void Window::Frame::setBackgroundColor(const Color& color) {
00108     if (titlebar_b)
00109         titlebar_b->setBackgroundColor(color);
00110     if (title_)
00111         title_->setBackgroundColor(color);
00112 }
00113 
00114 const Color& Window::Frame::backgroundColor() const {
00115     const Widget *w = (titlebar_b) ? titlebar_b : static_cast<const Widget *>(this);
00116     return w->backgroundColor();
00117 }
00118 
00119 void Window::Frame::childEvent(ChildEvent *e) {
00120     /* we can have only one child and when it dies we die to */
00121     if (e->type() == Event::ChildRemoved) {
00122         delete this;
00123     }
00124 }
00125 
00126 void Window::Frame::mousePressEvent(MouseEvent *me) {
00127     if (titlebar_b && (me->button() & LeftButton) &&
00128             titlebar_b->geometry().contains(me->pos())) {
00129         pressed_ = true;
00130         pos_start_ = me->globalPos();
00131         topleft_start_ = geometry().topLeft();
00132     }
00133     me->accept();
00134 }
00135 
00136 void Window::Frame::mouseReleaseEvent(MouseEvent *me) {
00137     if (pressed_ && me->button() & LeftButton)
00138         pressed_ = false;
00139     me->accept();
00140 }
00141 
00142 void Window::Frame::enterEvent(Event *) {
00143     pressed_ = false;
00144 }
00145 
00146 void Window::Frame::leaveEvent(Event *) {
00147     pressed_ = false;
00148 }
00149 
00150 void Window::Frame::mouseMoveEvent(MouseEvent *me) {
00151     if (pressed_) {
00152         move(topleft_start_ + me->globalPos() - pos_start_);
00153     }
00154     me->accept();
00155 }
00156 
00157 /// window
00158 /*! if parent is NULL then the widget is a system toplevel window */
00159 Window::Window(Widget *parent, const std::string &name, int wflags)
00160         : Widget(0, name, (wflags & W::Style_NoBorder) ?
00161                  wflags : wflags & ~W::Type_Dialog),
00162         frame_((parent && !(wflags & W::Style_NoBorder)) ?
00163        new Window::Frame(parent, name + "::Frame", wflags) : 0) {
00164     Widget *p = (frame_) ? static_cast<Widget *>(frame_)
00165                 : static_cast<Widget *>(parent);
00166     reparent(p);
00167     Application::sendPostedEvents();
00168 }
00169 
00170 void Window::setTitleFont(const Font& font) {
00171     if (frame_)
00172         frame_->setFont(font);
00173 }
00174 
00175 const Font& Window::titleFont() const {
00176     const Widget *w = (frame_) ? frame_ : static_cast<const Widget *>(this);
00177     return w->font();
00178 }
00179 
00180 void Window::setTitle(const std::string& text) {
00181     if (frame_)
00182         frame_->setText(text);
00183 }
00184 
00185 std::string Window::title() const {
00186     return (frame_) ? frame_->text() : "";
00187 }
00188 
00189 void Window::setTitleColor(const Color& color) {
00190     if (frame_)
00191         frame_->setForegroundColor(color);
00192 }
00193 
00194 const Color& Window::titleColor() const {
00195     const Widget *w = (frame_) ? frame_ : static_cast<const Widget *>(this);
00196     return w->foregroundColor();
00197 }
00198 
00199 void Window::setTitleBarColor(const Color& color) {
00200     if (frame_)
00201         frame_->setBackgroundColor(color);
00202 }
00203 
00204 const Color& Window::titleBarColor() const {
00205     const Widget *w = (frame_) ? frame_ : static_cast<const Widget *>(this);
00206     return w->backgroundColor();
00207 }
00208 
00209 void Window::raise() {
00210     Widget *w = (frame_) ? frame_ : static_cast<Widget *>(this);
00211     w->Widget::raise();
00212 }
00213 
00214 void Window::lower() {
00215     Widget *w = (frame_) ? frame_ : static_cast<Widget *>(this);
00216     w->Widget::lower();
00217 }
00218 
00219 void Window::hide() {
00220     Widget *w = (frame_) ? frame_ : static_cast<Widget *>(this);
00221     w->Widget::hide();
00222 }
00223 
00224 void Window::show() {
00225     Widget *w = (frame_) ? frame_ : static_cast<Widget *>(this);
00226     w->Widget::show();
00227 }
00228 
00229 void Window::setBackgroundColor(const Color& color) {
00230     if (frame_) {
00231         frame_->Widget::setBackgroundColor(color);
00232     }
00233     Widget::setBackgroundColor(color);
00234 }
00235 
00236 }

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