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 Button::Button(std::string icon_file, std::string ninfo, SDLKey nshortcut, int x, int y, int mw, int mh)
24 if(!icon_file.empty())
26 snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
27 if(!faccessible(filename))
28 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
32 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
35 if(mw != -1 || mh != -1)
37 texture_load(&icon,filename,USE_ALPHA);
48 SDL_SoftStretch(icon.sdl_surface, NULL, icon.sdl_surface, &dest);
51 texture_load(&icon,filename,USE_ALPHA);
67 void Button::change_icon(std::string icon_file, int mw, int mh)
71 if(!icon_file.empty())
73 snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str());
74 if(!faccessible(filename))
75 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
79 snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str());
83 texture_load(&icon,filename,USE_ALPHA);
88 fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200);
89 fillrect(rect.x+1,rect.y+1,rect.w-2,rect.h-2,175,175,175,200);
92 texture_draw(bkgd,rect.x,rect.y);
94 texture_draw(&icon,rect.x,rect.y);
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 text_draw(&white_small_text, 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 text_draw(&white_small_text, str, i + rect.x - strlen(str) * white_small_text.w, rect.y + white_small_text.h+2, 1);
108 if(state == BUTTON_PRESSED)
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);
119 void Button::event(SDL_Event &event)
121 SDLKey key = event.key.keysym.sym;
123 if(event.motion.x > rect.x && event.motion.x < rect.x + rect.w &&
124 event.motion.y > rect.y && event.motion.y < rect.y + rect.h)
126 if(event.type == SDL_MOUSEBUTTONDOWN)
128 if(event.button.button == SDL_BUTTON_LEFT)
130 state = BUTTON_PRESSED;
137 else if(event.type == SDL_MOUSEBUTTONUP)
139 if(event.button.button == SDL_BUTTON_LEFT && state == BUTTON_PRESSED)
141 state = BUTTON_CLICKED;
143 else if(event.button.button != SDL_BUTTON_LEFT && state != BUTTON_PRESSED)
149 if(state != BUTTON_PRESSED && state != BUTTON_CLICKED)
151 state = BUTTON_HOVER;
152 mouse_cursor->set_state(MC_LINK);
155 else if(event.type != SDL_KEYDOWN && event.type != SDL_KEYUP)
164 if(event.type == SDL_KEYDOWN)
167 state = BUTTON_PRESSED;
169 else if(event.type == SDL_KEYUP)
171 if(state == BUTTON_PRESSED && key == shortcut)
172 state = BUTTON_CLICKED;
174 else if(event.type == SDL_MOUSEMOTION)
184 int Button::get_state()
187 if(state == BUTTON_CLICKED)
199 ButtonPanel::ButtonPanel(int x, int y, int w, int h)
210 Button* ButtonPanel::event(SDL_Event& event)
214 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
217 if((*it)->state != -1)
228 ButtonPanel::~ButtonPanel()
230 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
237 void ButtonPanel::draw()
241 fillrect(rect.x,rect.y,rect.w,rect.h,100,100,100,200);
242 for(std::vector<Button*>::iterator it = item.begin(); it != item.end(); ++it)
249 void ButtonPanel::additem(Button* pbutton, int tag)
251 int max_cols, row, col;
253 item.push_back(pbutton);
255 /* A button_panel takes control of the buttons it contains and arranges them */
257 max_cols = rect.w / bw;
259 row = (item.size()-1) / max_cols;
260 col = (item.size()-1) % max_cols;
262 item[item.size()-1]->rect.x = rect.x + col * bw;
263 item[item.size()-1]->rect.y = rect.y + row * bh;
264 item[item.size()-1]->tag = tag;