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/tabbox.hpp"
00035 #include "framework/textbox.hpp"
00036 #include "framework/application.hpp"
00037
00038 #include "platform/font.hpp"
00039 #include "platform/display.hpp"
00040 #include "platform/lowlevel.hpp"
00041
00042 namespace gsgl
00043 {
00044
00045 using namespace data;
00046 using namespace platform;
00047
00048 namespace framework
00049 {
00050
00051 tabbox::tabbox(widget *parent,
00052 int x, int y, int w, int h,
00053 const color & fg, const color & bg,
00054 const string & tab_font_face,
00055 const int tab_font_size,
00056 const int tab_bar_height)
00057 : widget(parent, x, y, w, h, fg, bg), active_tab_index(-1),
00058 tab_font(new font(tab_font_face, tab_font_size, fg)), tab_bar_height(tab_bar_height)
00059 {
00060
00061 }
00062
00063
00064 tabbox::~tabbox()
00065 {
00066
00067
00068 delete tab_font;
00069
00070 for (list<tab_rec>::iterator i = tabs.iter(); i.is_valid(); ++i)
00071 {
00072 delete i->title_bar;
00073
00074 }
00075 }
00076
00077
00078 void tabbox::add_tab(const string & name, widget *contents)
00079 {
00080 textbox *title_bar = new textbox(0, 0, 0, 0, 0, get_foreground(), get_background(), tab_font->get_face(), tab_font->get_size());
00081 title_bar->get_text() = name;
00082
00083 contents->get_x() = 0;
00084 contents->get_y() = 0;
00085 contents->get_w() = get_w();
00086 contents->get_h() = get_h() - tab_bar_height;
00087 add_child(contents);
00088
00089 tab_rec tr;
00090 tr.name = name;
00091 tr.title_bar = title_bar;
00092 tr.contents = contents;
00093 tabs.append(tr);
00094 };
00095
00096
00097 void tabbox::draw()
00098 {
00099
00100 if (active_tab_index == -1)
00101 {
00102 if (tabs.size())
00103 {
00104 active_tab_index = 0;
00105 }
00106 else
00107 {
00108
00109 return;
00110 }
00111 }
00112
00113
00114 int tab_bar_width = get_w() / tabs.size();
00115
00116 for (gsgl::index_t i = 0; i < tabs.size(); ++i)
00117 {
00118 widget *title_bar = tabs[i].title_bar;
00119 assert(title_bar);
00120
00121 title_bar->get_w() = tab_bar_width;
00122 title_bar->get_h() = tab_bar_height;
00123
00124 title_bar->get_x() = i * title_bar->get_w();
00125 title_bar->get_y() = get_h() - tab_bar_height;
00126
00127
00128 glMatrixMode(GL_MODELVIEW);
00129 glPushMatrix();
00130 glTranslatef(static_cast<GLfloat>(title_bar->get_x()), static_cast<GLfloat>(title_bar->get_y()), 0);
00131
00132 title_bar->draw();
00133
00134 glPopMatrix();
00135
00136
00137 widget *contents = tabs[i].contents;
00138
00139 if (i == active_tab_index)
00140 contents->set_flags(WIDGET_INVISIBLE | WIDGET_INACTIVE, false);
00141 else
00142 contents->set_flags(WIDGET_INVISIBLE | WIDGET_INACTIVE, true);
00143 }
00144
00145
00146 glDisable(GL_BLEND);
00147
00148 get_foreground().set();
00149 glLineWidth(1.0f);
00150
00151 glBegin(GL_LINE_STRIP);
00152 glVertex2i(0, 0);
00153 glVertex2i(0, get_h() - tab_bar_height);
00154 glVertex2i(active_tab_index * tab_bar_width, get_h() - tab_bar_height);
00155 glVertex2i(active_tab_index * tab_bar_width, get_h());
00156 glVertex2i((active_tab_index+1) * tab_bar_width, get_h());
00157 glVertex2i((active_tab_index+1) * tab_bar_width, get_h() - tab_bar_height);
00158 glVertex2i(get_w(), get_h() - tab_bar_height);
00159 glVertex2i(get_w(), 0);
00160 glVertex2i(0, 0);
00161 glEnd();
00162
00163
00164 }
00165
00166
00167 bool tabbox::handle_event(const SDL_Event & e)
00168 {
00169 switch (e.type)
00170 {
00171 case SDL_MOUSEBUTTONUP:
00172
00173 if (e.button.button == SDL_BUTTON_LEFT)
00174 {
00175 int mouse_x = e.button.x;
00176 int mouse_y = application::global_instance()->get_console()->get_height() - e.button.y;
00177
00178 get_local(mouse_x, mouse_y);
00179
00180 for (int i = 0; i < tabs.size(); ++i)
00181 {
00182 int tab_x = tabs[i].title_bar->get_x();
00183 int tab_y = tabs[i].title_bar->get_y();
00184 int tab_w = tabs[i].title_bar->get_w();
00185 int tab_h = tabs[i].title_bar->get_h();
00186
00187 if ((tab_x <= mouse_x && mouse_x <= tab_x + tab_w)
00188 && (tab_y <= mouse_y && mouse_y <= tab_y + tab_h))
00189 {
00190 active_tab_index = i;
00191 return true;
00192 }
00193 }
00194 }
00195
00196 break;
00197 default:
00198 break;
00199 }
00200
00201 return false;
00202 }
00203
00204 }
00205
00206 }