00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #ifndef __GUI_WIDGETS_WIDGET_HPP_INCLUDED__
00016 #define __GUI_WIDGETS_WIDGET_HPP_INCLUDED__
00017
00018 #include "gui/widgets/helper.hpp"
00019 #include "sdl_utils.hpp"
00020
00021 #include "SDL.h"
00022
00023 #include <string>
00024
00025 namespace gui2 {
00026
00027 class tevent_handler;
00028
00029
00030
00031 class tevent_executor
00032 {
00033 public:
00034 tevent_executor() :
00035 wants_mouse_hover_(false),
00036 wants_mouse_left_double_click_(false),
00037 wants_mouse_middle_double_click_(false),
00038 wants_mouse_right_double_click_(false)
00039 {}
00040 virtual ~tevent_executor() {}
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 virtual void mouse_enter(tevent_handler&) {}
00054 virtual void mouse_move(tevent_handler&) {}
00055 virtual void mouse_hover(tevent_handler&) {}
00056 virtual void mouse_leave(tevent_handler&) {}
00057
00058 virtual void mouse_left_button_down(tevent_handler&) {}
00059 virtual void mouse_left_button_up(tevent_handler&) {}
00060 virtual void mouse_left_button_click(tevent_handler&) {}
00061 virtual void mouse_left_button_double_click(tevent_handler&) {}
00062
00063 virtual void mouse_middle_button_down(tevent_handler&) {}
00064 virtual void mouse_middle_button_up(tevent_handler&) {}
00065 virtual void mouse_middle_button_click(tevent_handler&) {}
00066 virtual void mouse_middle_button_double_click(tevent_handler&) {}
00067
00068 virtual void mouse_right_button_down(tevent_handler&) {}
00069 virtual void mouse_right_button_up(tevent_handler&) {}
00070 virtual void mouse_right_button_click(tevent_handler&) {}
00071 virtual void mouse_right_button_double_click(tevent_handler&) {}
00072
00073
00074
00075
00076
00077
00078 virtual void key_press(tevent_handler&, bool&, SDLKey, SDLMod, Uint16) {}
00079
00080 virtual void window_resize(tevent_handler&, const unsigned ,
00081 const unsigned ) {}
00082
00083
00084 virtual void help_key(tevent_handler&) {}
00085
00086 bool wants_mouse_hover() const { return wants_mouse_hover_; }
00087
00088 bool wants_mouse_left_double_click() const { return wants_mouse_left_double_click_; }
00089 bool wants_mouse_middle_double_click() const { return wants_mouse_middle_double_click_; }
00090 bool wants_mouse_right_double_click() const { return wants_mouse_right_double_click_; }
00091
00092 tevent_executor& set_wants_mouse_hover(const bool hover = true)
00093 { wants_mouse_hover_ = hover; return *this; }
00094
00095 tevent_executor& set_wants_mouse_left_double_click(const bool click = true)
00096 { wants_mouse_left_double_click_ = click; return *this; }
00097
00098 tevent_executor& set_wants_mouse_middle_double_click(const bool click = true)
00099 { wants_mouse_middle_double_click_ = click; return *this; }
00100
00101 tevent_executor& set_wants_mouse_right_double_click(const bool click = true)
00102 { wants_mouse_right_double_click_ = click; return *this; }
00103
00104 private:
00105
00106
00107 bool wants_mouse_hover_;
00108 bool wants_mouse_left_double_click_;
00109 bool wants_mouse_middle_double_click_;
00110 bool wants_mouse_right_double_click_;
00111 };
00112
00113 class twindow;
00114
00115
00116
00117 class twidget : public virtual tevent_executor
00118 {
00119 public:
00120 twidget() :
00121 id_(""),
00122 definition_("default"),
00123 parent_(0),
00124 x_(-1),
00125 y_(-1),
00126 w_(0),
00127 h_(0),
00128 dirty_(true)
00129 {}
00130 virtual ~twidget() {}
00131
00132 twidget* parent() { return parent_; }
00133 void set_parent(twidget* parent) { parent_ = parent; }
00134
00135 const std::string& id() const { return id_; }
00136 void set_id(const std::string& id) { id_ = id; }
00137
00138 const std::string& definition() const { return definition_; }
00139
00140
00141
00142 virtual void set_definition(const std::string& definition)
00143 { definition_ = definition; }
00144
00145
00146
00147 virtual void draw(surface& ) = 0;
00148
00149
00150
00151
00152
00153 int get_x() const { return x_; }
00154 int get_y() const { return y_; }
00155 unsigned get_width() const { return w_; }
00156 unsigned get_height() const { return h_; }
00157
00158
00159 virtual bool dirty() const { return dirty_; }
00160
00161
00162 virtual tpoint get_minimum_size() const = 0;
00163
00164
00165 virtual tpoint get_best_size() const = 0;
00166
00167
00168 virtual tpoint get_maximum_size() const = 0;
00169
00170
00171 virtual void set_size(const SDL_Rect& rect)
00172 {
00173 x_ = rect.x;
00174 y_ = rect.y;
00175 w_ = rect.w;
00176 h_ = rect.h;
00177 dirty_ = true;
00178 }
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 virtual twidget* find_widget(const tpoint& coordinate,
00194 const bool must_be_active)
00195 {
00196 return coordinate.x >= x_ && coordinate.x < (x_ + w_) &&
00197 coordinate.y >= y_ && coordinate.y < (y_ + h_) ? this : 0;
00198 }
00199
00200
00201 virtual const twidget* find_widget(const tpoint& coordinate,
00202 const bool must_be_active) const
00203 {
00204 return coordinate.x >= x_ && coordinate.x < (x_ + w_) &&
00205 coordinate.y >= y_ && coordinate.y < (y_ + h_) ? this : 0;
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220 virtual twidget* find_widget(const std::string& id,
00221 const bool must_be_active)
00222 { return id_ == id ? this : 0; }
00223
00224
00225 virtual const twidget* find_widget(const std::string& id,
00226 const bool must_be_active) const
00227 { return id_ == id ? this : 0; }
00228
00229
00230
00231
00232
00233
00234 virtual bool has_widget(const twidget* widget) const
00235 { return widget == this; }
00236
00237
00238 twindow* get_window();
00239
00240
00241 virtual void load_config() {}
00242
00243 SDL_Rect get_rect() const
00244 { return ::create_rect( x_, y_, w_, h_ ); }
00245
00246
00247
00248
00249
00250
00251 virtual bool has_vertical_scrollbar() const { return false; }
00252 virtual bool has_horizontal_scrollbar() const { return false; }
00253
00254 protected:
00255 virtual void set_dirty(const bool dirty = true)
00256 {
00257 dirty_ = dirty;
00258 if(parent_ && dirty) parent_->set_dirty(true);
00259 }
00260
00261 private:
00262
00263
00264
00265
00266 std::string id_;
00267
00268
00269
00270
00271 std::string definition_;
00272
00273 twidget* parent_;
00274 int x_, y_;
00275 unsigned w_, h_;
00276 bool dirty_;
00277
00278 };
00279
00280
00281
00282
00283
00284
00285
00286 class tselectable_
00287 {
00288 public:
00289 virtual ~tselectable_() {}
00290
00291
00292 virtual bool is_selected() const = 0;
00293
00294
00295 virtual void set_selected(const bool = true) = 0;
00296 };
00297
00298 }
00299
00300 #endif