4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 Timer Button::popup_timer;
31 Button::Button(std::string icon_file, std::string ninfo, SDLKey nshortcut, int x, int y, int mw, int mh)
33 popup_timer.init(false);
35 add_icon(icon_file,mw,mh);
51 void Button::add_icon(std::string icon_file, int mw, int mh)
55 if(!icon_file.empty())
57 snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
58 if(!faccessible(filename))
59 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
63 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
66 if(mw != -1 || mh != -1)
68 icon.push_back(new Surface(filename,USE_ALPHA));
69 icon.back()->resize(mw,mh);
72 icon.push_back(new Surface(filename,USE_ALPHA));
78 if(state == BUTTON_HOVER)
79 if(!popup_timer.check())
82 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
83 fillrect(rect.x+1,rect.y+1,rect.w-2,rect.h-2,175,175,175,200);
85 for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
86 (*it)->draw(rect.x,rect.y);
91 viewport.set_translation(Vector(rect.x, rect.y));
92 drawable->draw(viewport, 0);
100 if(0 > rect.x - (int)strlen(info.c_str()) * white_small_text->w)
101 i = rect.w + strlen(info.c_str()) * white_small_text->w;
104 white_small_text->draw(info.c_str(), i + rect.x - strlen(info.c_str()) * white_small_text->w, rect.y, 1);
105 sprintf(str,"(%s)", SDL_GetKeyName(shortcut));
106 white_small_text->draw(str, i + rect.x - strlen(str) * white_small_text->w, rect.y + white_small_text->h+2, 1);
108 if(state == BUTTON_PRESSED || state == BUTTON_DEACTIVE)
109 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
110 else if(state == BUTTON_HOVER)
111 fillrect(rect.x,rect.y,rect.w,rect.h,150,150,150,128);
116 for(std::vector<Surface*>::iterator it = icon.begin(); it != icon.end(); ++it)
122 void Button::event(SDL_Event &event)
124 if(state == BUTTON_DEACTIVE)
127 SDLKey key = event.key.keysym.sym;
129 if(event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP)
131 if(event.button.x < rect.x || event.button.x >= rect.x + rect.w ||
132 event.button.y < rect.y || event.button.y >= rect.y + rect.h)
135 if(event.button.button == SDL_BUTTON_RIGHT)
140 else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 4) /* Mouse wheel up. */
142 state = BUTTON_WHEELUP;
145 else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 5) /* Mouse wheel down. */
147 state = BUTTON_WHEELDOWN;
151 if(event.button.button == SDL_BUTTON_LEFT)
152 if(event.type == SDL_MOUSEBUTTONDOWN)
153 state = BUTTON_PRESSED;
155 state = BUTTON_CLICKED;
157 else if(event.type == SDL_MOUSEMOTION)
159 if(event.motion.x < rect.x || event.motion.x >= rect.x + rect.w ||
160 event.motion.y < rect.y || event.motion.y >= rect.y + rect.h)
166 state = BUTTON_HOVER;
167 popup_timer.start(1500);
175 else if(event.type == SDL_KEYDOWN)
178 state = BUTTON_PRESSED;
180 else if(event.type == SDL_KEYUP)
182 if(state == BUTTON_PRESSED && key == shortcut)
183 state = BUTTON_CLICKED;
187 int Button::get_state()
194 case BUTTON_WHEELDOWN:
203 ButtonPanel::ButtonPanel(int x, int y, int w, int h)
215 Button* ButtonPanel::event(SDL_Event& event)
220 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
223 if((*it)->state != BUTTON_NONE)
225 if(hlast && (*it)->state == BUTTON_CLICKED)
238 ButtonPanel::~ButtonPanel()
240 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
247 void ButtonPanel::draw()
252 fillrect(rect.x,rect.y,rect.w,rect.h,100,100,100,200);
253 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
256 if(hlast && it == last_clicked)
258 fillrect((*it)->get_pos().x,(*it)->get_pos().y,(*it)->get_pos().w,(*it)->get_pos().h,100,100,100,128);
264 void ButtonPanel::additem(Button* pbutton, int tag)
266 int max_cols, row, col;
268 item.push_back(pbutton);
270 /* A button_panel takes control of the buttons it contains and arranges them */
272 max_cols = rect.w / bw;
274 row = (item.size()-1) / max_cols;
275 col = (item.size()-1) % max_cols;
277 item[item.size()-1]->rect.x = rect.x + col * bw;
278 item[item.size()-1]->rect.y = rect.y + row * bh;
279 item[item.size()-1]->tag = tag;
283 void ButtonPanel::set_button_size(int w, int h)
289 Button* ButtonPanel::manipulate_button(int i)
291 if(int(item.size())-1 < i)
292 return item[item.size()-1];
297 void ButtonPanel::highlight_last(bool b)