2 // C Implementation: button
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
9 // Copyright: See COPYING file that comes with this distribution
20 Timer Button::popup_timer;
22 Button::Button(std::string icon_file, std::string ninfo, SDLKey nshortcut, int x, int y, int mw, int mh)
24 popup_timer.init(false);
28 if(!icon_file.empty())
30 snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
31 if(!faccessible(filename))
32 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
36 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
39 if(mw != -1 || mh != -1)
41 texture_load(&icon,filename,USE_ALPHA);
52 SDL_SoftStretch(icon.sdl_surface, NULL, icon.sdl_surface, &dest);
55 texture_load(&icon,filename,USE_ALPHA);
71 void Button::change_icon(std::string icon_file, int /*mw*/, int /*mh*/)
75 if(!icon_file.empty())
77 snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
78 if(!faccessible(filename))
79 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
83 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
87 texture_load(&icon,filename,USE_ALPHA);
92 if(state == BUTTON_HOVER)
93 if(!popup_timer.check())
96 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
97 fillrect(rect.x+1,rect.y+1,rect.w-2,rect.h-2,175,175,175,200);
100 texture_draw(bkgd,rect.x,rect.y);
102 texture_draw(&icon,rect.x,rect.y);
108 if(0 > rect.x - (int)strlen(info.c_str()) * white_small_text.w)
109 i = rect.w + strlen(info.c_str()) * white_small_text.w;
112 text_draw(&white_small_text, info.c_str(), i + rect.x - strlen(info.c_str()) * white_small_text.w, rect.y, 1);
113 sprintf(str,"(%s)", SDL_GetKeyName(shortcut));
114 text_draw(&white_small_text, str, i + rect.x - strlen(str) * white_small_text.w, rect.y + white_small_text.h+2, 1);
116 if(state == BUTTON_PRESSED)
117 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
118 else if(state == BUTTON_HOVER)
119 fillrect(rect.x,rect.y,rect.w,rect.h,150,150,150,128);
127 void Button::event(SDL_Event &event)
129 SDLKey key = event.key.keysym.sym;
131 if(event.motion.x > rect.x && event.motion.x < rect.x + rect.w &&
132 event.motion.y > rect.y && event.motion.y < rect.y + rect.h)
134 if(event.type == SDL_MOUSEBUTTONDOWN)
136 if(event.button.button == SDL_BUTTON_LEFT)
138 state = BUTTON_PRESSED;
145 else if(event.type == SDL_MOUSEBUTTONUP)
147 if(event.button.button == SDL_BUTTON_LEFT && state == BUTTON_PRESSED)
149 state = BUTTON_CLICKED;
151 else if(event.button.button != SDL_BUTTON_LEFT && state != BUTTON_PRESSED)
157 if(state != BUTTON_PRESSED && state != BUTTON_CLICKED)
159 state = BUTTON_HOVER;
160 mouse_cursor->set_state(MC_LINK);
163 else if((event.type != SDL_KEYDOWN && event.type != SDL_KEYUP) || event.type == SDL_MOUSEMOTION)
172 if(event.type == SDL_KEYDOWN)
175 state = BUTTON_PRESSED;
177 else if(event.type == SDL_KEYUP)
179 if(state == BUTTON_PRESSED && key == shortcut)
180 state = BUTTON_CLICKED;
182 else if(event.type == SDL_MOUSEMOTION)
184 popup_timer.start(1500);
194 int Button::get_state()
197 if(state == BUTTON_CLICKED)
209 ButtonPanel::ButtonPanel(int x, int y, int w, int h)
220 Button* ButtonPanel::event(SDL_Event& event)
224 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
227 if((*it)->state != BUTTON_NONE)
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)
260 void ButtonPanel::additem(Button* pbutton, int tag)
262 int max_cols, row, col;
264 item.push_back(pbutton);
266 /* A button_panel takes control of the buttons it contains and arranges them */
268 max_cols = rect.w / bw;
270 row = (item.size()-1) / max_cols;
271 col = (item.size()-1) % max_cols;
273 item[item.size()-1]->rect.x = rect.x + col * bw;
274 item[item.size()-1]->rect.y = rect.y + row * bh;
275 item[item.size()-1]->tag = tag;