00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "framework/widget.hpp"
00035 #include "framework/application.hpp"
00036
00037 #include "platform/display.hpp"
00038 #include "platform/lowlevel.hpp"
00039
00040 namespace gsgl
00041 {
00042
00043 using namespace data;
00044 using namespace platform;
00045
00046 namespace framework
00047 {
00048
00049 widget::widget(widget *parent, const int x, const int y, const int w, const int h, const color & fg, const color & bg)
00050 : flags(NO_FLAGS), parent(parent), next_tab(0), prev_tab(0), x(x), y(y), w(w), h(h), foreground(fg), background(bg), tex(0), handler(0)
00051 {
00052 if (parent)
00053 parent->add_child(this);
00054 }
00055
00056
00057 widget::~widget()
00058 {
00059 for (simple_stack<widget *>::iterator i = children.iter(); i.is_valid(); ++i)
00060 delete *i;
00061 }
00062
00063
00064 void widget::set_flags(const gsgl::flags_t f, bool flag_on)
00065 {
00066 if (flag_on)
00067 flags |= f;
00068 else
00069 flags &= ~f;
00070 }
00071
00072
00073 void widget::add_child(widget *child)
00074 {
00075 children.push(child);
00076 child->parent = this;
00077 }
00078
00079
00080 void widget::remove_child(widget *child)
00081 {
00082 for (simple_stack<widget *>::iterator i = children.iter(); i.is_valid(); ++i)
00083 {
00084 if (*i == child)
00085 {
00086 child->parent = 0;
00087 children.remove(i);
00088 break;
00089 }
00090 }
00091 }
00092
00093
00094 void widget::draw()
00095 {
00096 glPushAttrib(GL_ALL_ATTRIB_BITS);
00097 glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
00098
00099 glEnable(GL_BLEND);
00100 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00101
00102 if (tex)
00103 {
00104 glEnable(GL_TEXTURE_2D);
00105 tex->bind();
00106 }
00107
00108 background.set();
00109
00110 display::draw_rectangle(0, 0, static_cast<float>(w), static_cast<float>(h));
00111
00112 glPopClientAttrib();
00113 glPopAttrib();
00114 }
00115
00116
00117 bool widget::handle_event(const SDL_Event & e)
00118 {
00119 return false;
00120 }
00121
00122
00123
00124 void widget::get_abs(int & x, int & y)
00125 {
00126 int res_x = x;
00127 int res_y = y;
00128
00129 for (widget *w = parent; w; w = w->parent)
00130 {
00131 res_x += w->x;
00132 res_y += w->y;
00133 }
00134
00135 x = res_x;
00136 y = res_y;
00137 }
00138
00139
00140 void widget::get_local(int & x, int & y)
00141 {
00142 int res_x = x;
00143 int res_y = y;
00144
00145 for (widget *w = this; w; w = w->parent)
00146 {
00147 res_x -= w->x;
00148 res_y -= w->y;
00149 }
00150
00151 x = res_x;
00152 y = res_y;
00153 }
00154
00155
00156 bool widget::button_down_here(int button)
00157 {
00158 int prev_button, prev_x, prev_y;
00159 application::global_instance()->get_mousedown_info(prev_button, prev_x, prev_y);
00160 get_local(prev_x, prev_y);
00161 return prev_button == button
00162 && prev_x >= 0 && prev_x < get_w()
00163 && prev_y >= 0 && prev_y < get_h();
00164 }
00165
00166
00167 void widget::draw_line(int x0, int y0, int x1, int y1)
00168 {
00169 get_abs(x0, y0);
00170 get_abs(x1, y1);
00171
00172 glBegin(GL_LINES);
00173 glVertex2i(x0, y0);
00174 glVertex2i(x1, y1);
00175 glEnd();
00176 }
00177
00178
00179 void widget::draw_box(int x, int y, int w, int h)
00180 {
00181 get_abs(x, y);
00182
00183 display::draw_rectangle(static_cast<float>(x), static_cast<float>(y), static_cast<float>(x+w), static_cast<float>(y+h));
00184 }
00185
00186 }
00187
00188 }