widget.h

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 #ifndef WT_WIDGET_H
00022 #define WT_WIDGET_H
00023 
00024 #include <wt/object.h>
00025 #include <wt/layout.h>
00026 #include <wt/pixmap.h>
00027 #include <wt/rect.h>
00028 #include <wt/region.h>
00029 #include <wt/sizepolicy.h>
00030 #include <wt/flags.h>
00031 #include <wt/focusdata.h>
00032 
00033 namespace Wt {
00034 class PaintEvent;
00035 class ResizeEvent;
00036 class MoveEvent;
00037 class LayoutHintEvent;
00038 class ShowEvent;
00039 class HideEvent;
00040 class CloseEvent;
00041 class Layout;
00042 class Painter;
00043 class Color;
00044 class Font;
00045 
00046 class Widget : public Object, public LayoutItem, public Pixmap {
00047 public:
00048     typedef enum  {
00049         NoFocus = 0,
00050         TabFocus = 1 << 0,
00051         ClickFocus = 1 << 1,
00052         StrongFocus = TabFocus | ClickFocus,
00053         WheelFocus = 1 << 2 | StrongFocus
00054     } FocusPolicy;
00055 
00056     /// \todo document why we need explicit?
00057     explicit Widget(Widget *parent = 0, const std::string &name = "Widget::anon", int Wflags = 0);
00058     virtual ~Widget();
00059 
00060     /// parents are guaranteed to be widgets and not general wobjects
00061     Widget *parentWidget() const {
00062         return static_cast<Widget *>(parent());
00063     }
00064 
00065     Widget *topParentWidget() const {
00066         return static_cast<Widget *>(topParent());
00067     }
00068 
00069     void reparent(Widget *new_parent);
00070 
00071     // Excluding the window frame
00072     int width() const {
00073         return geometry().width();
00074     }
00075     int height() const {
00076         return geometry().height();
00077     }
00078     Size size() const {
00079         return geometry().size();
00080     }
00081 
00082     Rect rect() const {
00083         return Rect(0, 0, width(), height());
00084     }
00085 
00086     Point mapToGlobal(const Point & pos) const;
00087     Point mapFromGlobal(const Point & pos) const;
00088     Point mapToParent(const Point & pos) const;
00089     Point mapFromParent(const Point & pos) const;
00090     Region mapToGlobal(const Region & region) const;
00091     Region mapToParent(const Region & region) const;
00092 
00093     // change widget's state
00094     virtual void setBackgroundColor(const Color &c);
00095     const Color& backgroundColor() const;
00096     virtual void setForegroundColor(const Color &c);
00097     const Color& foregroundColor() const;
00098 
00099     virtual bool setBackgroundPixmapFlags(int flags, int alignment);
00100     virtual void setBackgroundPixmap(const Pixmap& pixmap,
00101                                      int flags = 0, int alignment = 0);
00102     const Pixmap& backgroundPixmap() const {
00103         return bg_pixmap_;
00104     }
00105     int backgroundPixmapFlags() const {
00106         return bg_flags;
00107     }
00108     int backgroundPixmapAlignment() const {
00109         return bg_alignment;
00110     }
00111 
00112     const Font& font() const;
00113     virtual void setFont(const Font& font);
00114     void unsetFont();
00115 
00116     /// event handling
00117     virtual bool event(Event *e);
00118 
00119     virtual void resize(int w, int h);
00120     void resize(const Size& size);
00121     virtual void move(int x, int y);
00122     void move(const Point& p);
00123     virtual void setGeometry(int x, int y, int w, int h);
00124     virtual void setGeometry(const Rect& r);
00125 
00126     static void update(Widget *w, const Region &region);
00127 
00128     void update (const Rect& r);
00129     void update(int x, int y, int w, int h);
00130     void update();
00131     void repaint(const Rect& r, bool erase = true);
00132     void repaint(const Region& r, bool erase = true);
00133     void repaint(int x, int y, int w, int h, bool erase = true);
00134     void repaint(bool erase);
00135     void repaint();
00136     void updateGeometry();
00137 
00138     void raise();
00139     void lower();
00140 
00141     bool isShown () const {
00142         return !hidden;
00143     }
00144     bool isHidden() const {
00145         return hidden;
00146     }
00147     bool isEmpty() const {
00148         return isHidden();
00149     }
00150 
00151     virtual Size sizeHint() const;
00152     virtual void setSizeHint(const Size& size);
00153     virtual Size minimumSize() const;
00154     virtual void setMinimumSize(const Size& size);
00155     virtual Size maximumSize() const;
00156     virtual void setMaximumSize(const Size& size);
00157     virtual SizePolicy sizePolicy() const;
00158     virtual void setSizePolicy(const SizePolicy& size_policy);
00159     virtual SizePolicy::ExpandData expanding() const;
00160 
00161     Layout *layout() const {
00162         return layout_;
00163     }
00164 
00165     void setLayout(Layout *layout);
00166 
00167     void erase(const Rect& r);
00168     void erase(const Region& r);
00169     void erase(int x, int y, int w, int h);
00170     void erase();
00171 
00172     void drawText(int x, int y, const std::string& text);
00173     void drawText(const Point& p, const std::string& text);
00174 
00175     Widget *childAt(const Point& p, bool self_include = false) const;
00176     Widget *childAt(int x, int y, bool self_include = false) const {
00177         return childAt(Point(x, y), self_include);
00178     }
00179 
00180     Widget *focusWidget() const {
00181         return focus_data_.focusWidget();
00182     }
00183 
00184     bool hasFocus() const {
00185         return (focusWidget() == this);
00186     }
00187 
00188     bool isFocusEnabled(int reason) const;
00189     virtual void setFocus(int reason);
00190 
00191     void grabKeyboard();
00192     void releaseKeyboard();
00193     static Widget *keyboardGrabber();
00194 
00195     void grabMouse();
00196     void releaseMouse();
00197     static Widget *mouseGrabber();
00198 
00199     virtual void hide();
00200     virtual void show();
00201 
00202     bool close();
00203     virtual bool close(bool destroy);
00204 
00205     void setHidden(bool do_hide = true);
00206     void setShown(bool do_show = true);
00207 
00208     virtual void enable();
00209     virtual void disable();
00210 
00211     void setEnabled(bool do_enable = true);
00212     void setDisabled(bool do_disable = true);
00213 
00214     /// public attributes
00215     WVar<bool> hidden;
00216     WVar<bool> enabled;
00217     WVar<bool> mouseTracking;
00218     WVar<int> focusPolicy;
00219     WVar<int> ownFont;
00220 
00221 protected:
00222     /// event handling specialization
00223     virtual void mousePressEvent(MouseEvent *);
00224     virtual void mouseReleaseEvent(MouseEvent *);
00225     virtual void mouseDoubleClickEvent(MouseEvent *);
00226     virtual void mouseMoveEvent(MouseEvent *);
00227     virtual void wheelEvent(WheelEvent *);
00228     virtual void focusInEvent(FocusEvent *);
00229     virtual void focusOutEvent(FocusEvent *);
00230     virtual void keyPressEvent(KeyEvent *);
00231     virtual void keyReleaseEvent(KeyEvent *);
00232     virtual void enterEvent(Event *);
00233     virtual void leaveEvent(Event *);
00234     virtual void paintEvent(PaintEvent *);
00235     virtual void resizeEvent(ResizeEvent *);
00236     virtual void moveEvent(MoveEvent *);
00237     virtual void showEvent(ShowEvent *);
00238     virtual void hideEvent(HideEvent *);
00239     virtual void closeEvent(CloseEvent *);
00240 
00241     /// widget should be able to draw itself
00242     virtual void draw(Painter *p, const Region& region);
00243 
00244     bool autoerase() {
00245         return !(wflags_ & W::NoAutoErase);
00246     }
00247 
00248     bool autoclip() {
00249         return !(wflags_ & W::PaintUnclipped);
00250     }
00251 
00252     // on state change
00253     virtual void fontChange(const Font& oldFont);
00254 
00255     Signal01<void, Layout *> layoutChanged;
00256 
00257     void updateBackground();
00258 
00259     FocusData *focusData() const {
00260         return &focus_data_;
00261     }
00262 
00263     virtual bool focusNextPrevChild(bool next);
00264 
00265     void hiddenChanged(bool hidden_new);
00266 
00267     bool is_hidden(bool val) const;
00268     bool is_enabled(bool val) const;
00269 
00270 private:
00271     int wflags_;
00272     bool need_redraw_;
00273     Pixmap bg_pixmap_;
00274     Pixmap bg_scaled_;
00275     int bg_flags;
00276     int bg_alignment;
00277     Painter *painter_;
00278 
00279     Layout *layout_;
00280     Size l_size_hint_;
00281     Size l_min_size_;
00282     Size l_max_size_;
00283 
00284     static FocusData focus_data_;
00285 };
00286 
00287 }
00288 #endif // WT_WIDGET_H

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.