1738a2684b5ac5c83c23b73ebee6869cecd4525f
[supertux.git] / src / gui / menu.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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  02111-1307, USA.
19
20 #include <config.h>
21
22 #include <sys/types.h>
23 #include <ctype.h>
24
25 #include <iostream>
26 #include <sstream>
27 #include <cstdlib>
28 #include <cstdio>
29 #include <string>
30 #include <cassert>
31 #include <stdexcept>
32
33 #include "menu.hpp"
34 #include "mainloop.hpp"
35 #include "video/drawing_context.hpp"
36 #include "gettext.hpp"
37 #include "math/vector.hpp"
38 #include "main.hpp"
39 #include "resources.hpp"
40 #include "timer.hpp"
41 #include "control/joystickkeyboardcontroller.hpp"
42
43 static const float MENU_REPEAT_INITIAL = 0.4;
44 static const float MENU_REPEAT_RATE = 0.2;
45 static const float FLICK_CURSOR_TIME = 0.5;
46
47 extern SDL_Surface* screen;
48
49 std::vector<Menu*> Menu::last_menus;
50 Menu* Menu::current_ = 0;
51 Font* Menu::default_font;
52 Font* Menu::active_font;
53 Font* Menu::deactive_font;
54 Font* Menu::label_font;
55 Font* Menu::field_font;
56
57 /* just displays a Yes/No text that can be used to confirm stuff */
58 bool confirm_dialog(Surface *background, std::string text)
59 {
60   //Surface* cap_screen = Surface::CaptureScreen();
61   Menu* dialog = new Menu;
62   dialog->add_deactive(-1, text);
63   dialog->add_hl();
64   dialog->add_entry(true, _("Yes"));
65   dialog->add_entry(false, _("No"));
66   dialog->add_hl();
67   
68   Menu::set_current(dialog);
69
70   DrawingContext context;
71
72   // TODO make this a screen and not another mainloop...
73   while(true)
74     {
75       SDL_Event event;
76       while (SDL_PollEvent(&event)) {
77         if(event.type == SDL_QUIT)
78           main_loop->quit();
79         main_controller->process_event(event);
80         dialog->event(event);
81       }
82
83       if(background == NULL)
84         context.draw_gradient(Color(0.8, 0.95, 0.85), Color(0.8, 0.8, 0.8),
85                               LAYER_BACKGROUND0);
86       else
87         context.draw_surface(background, Vector(0,0), LAYER_BACKGROUND0);
88
89       dialog->draw(context);
90       dialog->update();
91
92       switch (dialog->check())
93         {
94         case true:
95           //delete cap_screen;
96           Menu::set_current(0);
97           delete dialog;
98           return true;
99           break;
100         case false:
101           //delete cap_screen;
102           Menu::set_current(0);
103           delete dialog;
104           return false;
105           break;
106         default:
107           break;
108         }
109
110       mouse_cursor->draw(context);
111       context.do_drawing();
112       SDL_Delay(25);
113     }
114
115   return false;
116 }
117
118 void
119 Menu::push_current(Menu* pmenu)
120 {
121   if (current_)
122     last_menus.push_back(current_);
123
124   current_ = pmenu;
125   current_->effect_time = real_time;
126 }
127
128 void
129 Menu::pop_current()
130 {
131   if (last_menus.size() >= 1) {
132     current_ = last_menus.back();
133     current_->effect_time = real_time;
134     last_menus.pop_back();
135   } else {
136     current_ = 0;
137   }
138 }
139
140 void
141 Menu::set_current(Menu* menu)
142 {
143   last_menus.clear();
144
145   if (menu)
146     menu->effect_time = real_time;
147
148   current_ = menu;
149   // just to be sure...
150   main_controller->reset();
151 }
152
153 MenuItem::MenuItem(MenuItemKind _kind, int _id)
154   : kind(_kind) , id(_id)
155 {
156   toggled = false;
157   selected = false;
158   target_menu = 0;
159 }
160
161 void
162 MenuItem::change_text(const  std::string& text_)
163 {
164   text = text_;
165 }
166
167 void
168 MenuItem::change_input(const  std::string& text_)
169 {
170   input = text_;
171 }
172
173 std::string MenuItem::get_input_with_symbol(bool active_item)
174 {
175   if(!active_item) {
176     input_flickering = true;
177   } else {
178     input_flickering = ((int) (real_time / FLICK_CURSOR_TIME)) % 2;
179   }
180
181   char str[1024];
182   if(input_flickering)
183     snprintf(str, sizeof(str), "%s ",input.c_str());
184   else
185     snprintf(str, sizeof(str), "%s_",input.c_str());
186
187   std::string string = str;
188
189   return string;
190 }
191
192 Menu::~Menu()
193 {
194   for(std::vector<MenuItem*>::iterator i = items.begin();
195       i != items.end(); ++i)
196     delete *i;
197   if(current_ == this)
198     current_ = NULL;
199 }
200
201 Menu::Menu()
202 {
203   hit_item = -1;
204   menuaction = MENU_ACTION_NONE;
205   delete_character = 0;
206   mn_input_char = '\0';
207
208   pos_x        = SCREEN_WIDTH/2;
209   pos_y        = SCREEN_HEIGHT/2;
210   arrange_left = 0;
211   active_item  = -1;
212
213   checkbox.reset(new Surface("images/engine/menu/checkbox-unchecked.png"));
214   checkbox_checked.reset(new Surface("images/engine/menu/checkbox-checked.png"));
215   back.reset(new Surface("images/engine/menu/arrow-back.png"));
216   arrow_left.reset(new Surface("images/engine/menu/arrow-left.png"));
217   arrow_right.reset(new Surface("images/engine/menu/arrow-right.png"));
218 }
219
220 void Menu::set_pos(float x, float y, float rw, float rh)
221 {
222   pos_x = x + get_width() * rw;
223   pos_y = y + get_height() * rh;
224 }
225
226 /* Add an item to a menu */
227 void
228 Menu::additem(MenuItem* item)
229 {
230   items.push_back(item);
231
232   /* If a new menu is being built, the active item shouldn't be set to
233    * something that isnt selectable. Set the active_item to the first
234    * selectable item added
235    */
236   if (active_item == -1
237       && item->kind != MN_HL 
238       && item->kind != MN_LABEL
239       && item->kind != MN_DEACTIVE) {
240     active_item = items.size() - 1;
241   }
242 }
243
244 void
245 Menu::add_hl()
246 {
247   additem(new MenuItem(MN_HL));
248 }
249
250 void
251 Menu::add_label(const std::string& text)
252 {
253   MenuItem* item = new MenuItem(MN_LABEL);
254   item->text = text;
255   additem(item);
256 }
257
258 void
259 Menu::add_controlfield(int id, const std::string& text,
260                 const std::string& mapping)
261 {
262   MenuItem* item = new MenuItem(MN_CONTROLFIELD, id);
263   item->change_text(text);
264         item->change_input(mapping);
265   additem(item);
266 }
267
268 void
269 Menu::add_entry(int id, const std::string& text)
270 {
271   MenuItem* item = new MenuItem(MN_ACTION, id);
272   item->text = text;
273   additem(item);
274 }
275
276 void
277 Menu::add_deactive(int id, const std::string& text)
278 {
279   MenuItem* item = new MenuItem(MN_DEACTIVE, id);
280   item->text = text;
281   additem(item);
282 }
283
284 void
285 Menu::add_toggle(int id, const std::string& text, bool toogled)
286 {
287   MenuItem* item = new MenuItem(MN_TOGGLE, id);
288   item->text = text;
289   item->toggled = toogled;
290   additem(item);
291 }
292
293 void
294 Menu::add_back(const std::string& text)
295 {
296   MenuItem* item = new MenuItem(MN_BACK);
297   item->text = text;
298   additem(item);
299 }
300
301 void
302 Menu::add_submenu(const std::string& text, Menu* submenu, int id)
303 {
304   MenuItem* item = new MenuItem(MN_GOTO, id);
305   item->text = text;
306   item->target_menu = submenu;
307   additem(item);
308 }
309
310 void
311 Menu::clear()
312 {
313   for(std::vector<MenuItem*>::iterator i = items.begin();
314       i != items.end(); ++i) {
315     delete *i;
316   }
317   items.clear();
318   active_item = -1;
319 }
320
321 /* Process actions done on the menu */
322 void
323 Menu::update()
324 {
325   /** check main input controller... */
326   if(main_controller->pressed(Controller::UP)) {
327     menuaction = MENU_ACTION_UP;
328     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
329   }
330   if(main_controller->hold(Controller::UP) && 
331       menu_repeat_time != 0 && real_time > menu_repeat_time) {
332     menuaction = MENU_ACTION_UP;
333     menu_repeat_time = real_time + MENU_REPEAT_RATE;
334   } 
335   if(main_controller->pressed(Controller::DOWN)) {
336     menuaction = MENU_ACTION_DOWN;
337     menu_repeat_time = real_time + MENU_REPEAT_INITIAL;
338   }
339   if(main_controller->hold(Controller::DOWN) && 
340       menu_repeat_time != 0 && real_time > menu_repeat_time) {
341     menuaction = MENU_ACTION_DOWN;
342     menu_repeat_time = real_time + MENU_REPEAT_RATE;
343   }
344   if(main_controller->pressed(Controller::JUMP)
345      || main_controller->pressed(Controller::ACTION)
346      || main_controller->pressed(Controller::MENU_SELECT)) {
347     menuaction = MENU_ACTION_HIT;
348   }
349   if(main_controller->pressed(Controller::PAUSE_MENU)) {
350     menuaction = MENU_ACTION_BACK;
351   }
352
353   hit_item = -1;
354   if(items.size() == 0)
355     return;
356   
357   int last_active_item = active_item;
358   switch(menuaction) {
359     case MENU_ACTION_UP:
360       do {
361         if (active_item > 0)
362           --active_item;
363         else
364           active_item = int(items.size())-1;
365       } while ((items[active_item]->kind == MN_HL 
366                 || items[active_item]->kind == MN_LABEL
367                 || items[active_item]->kind == MN_DEACTIVE)
368                && (active_item != last_active_item));
369       
370       break;
371       
372     case MENU_ACTION_DOWN:
373       do {
374         if(active_item < int(items.size())-1 )
375           ++active_item;
376         else
377           active_item = 0;
378       } while ((items[active_item]->kind == MN_HL
379                 || items[active_item]->kind == MN_LABEL
380                 || items[active_item]->kind == MN_DEACTIVE)
381                && (active_item != last_active_item));
382       
383       break;
384       
385     case MENU_ACTION_LEFT:
386       if(items[active_item]->kind == MN_STRINGSELECT) {
387         if(items[active_item]->selected > 0)
388           items[active_item]->selected--;
389         else
390           items[active_item]->selected = items[active_item]->list.size()-1;
391       }
392       break;
393       
394     case MENU_ACTION_RIGHT:
395       if(items[active_item]->kind == MN_STRINGSELECT) {
396         if(items[active_item]->selected+1 < items[active_item]->list.size())
397           items[active_item]->selected++;
398         else
399           items[active_item]->selected = 0;
400       }
401       break;
402       
403     case MENU_ACTION_HIT: {
404       hit_item = active_item;
405       switch (items[active_item]->kind) {
406         case MN_GOTO:
407           assert(items[active_item]->target_menu != 0);
408           Menu::push_current(items[active_item]->target_menu);
409           break;
410           
411         case MN_TOGGLE:
412           items[active_item]->toggled = !items[active_item]->toggled;
413           menu_action(items[active_item]);
414           break;
415           
416         case MN_CONTROLFIELD:
417           menu_action(items[active_item]);
418           break;
419           
420         case MN_ACTION:
421           menu_action(items[active_item]);
422           break;
423           
424         case MN_TEXTFIELD:
425         case MN_NUMFIELD:
426           menuaction = MENU_ACTION_DOWN;
427           update();
428           break;
429           
430         case MN_BACK:
431           Menu::pop_current();
432           break;
433         default:
434           break;
435       }
436       break;
437     }
438     
439     case MENU_ACTION_REMOVE:
440       if(items[active_item]->kind == MN_TEXTFIELD
441          || items[active_item]->kind == MN_NUMFIELD)
442       {
443         if(!items[active_item]->input.empty())
444         {
445           int i = items[active_item]->input.size();
446           
447           while(delete_character > 0)   /* remove charactes */
448           {
449             items[active_item]->input.resize(i-1);
450             delete_character--;
451           }
452         }
453       }
454       break;
455       
456     case MENU_ACTION_INPUT:
457       if(items[active_item]->kind == MN_TEXTFIELD
458          || (items[active_item]->kind == MN_NUMFIELD 
459              && mn_input_char >= '0' && mn_input_char <= '9'))
460       {
461         items[active_item]->input.push_back(mn_input_char);
462       }
463       break;
464       
465     case MENU_ACTION_BACK:
466       Menu::pop_current();
467       break;
468
469     case MENU_ACTION_NONE:
470       break;
471   }
472   menuaction = MENU_ACTION_NONE;
473
474   assert(active_item < int(items.size()));
475 }
476
477 int
478 Menu::check()
479 {
480   if (hit_item != -1)
481     return items[hit_item]->id;
482   else
483     return -1;
484 }
485
486 void
487 Menu::menu_action(MenuItem* )
488 {}
489
490 void
491 Menu::draw_item(DrawingContext& context, int index)
492 {
493   float menu_height = get_height();
494   float menu_width = get_width();  
495
496   MenuItem& pitem = *(items[index]);
497
498   int effect_offset = 0;
499   if(effect_time != 0) {
500     if(real_time - effect_time > 0.5) {
501       effect_time = 0;
502     } else {
503       float effect_delta = (0.5 - (real_time - effect_time)) * 250;
504       effect_offset = (int) ((index % 2) ? effect_delta : -effect_delta);
505     }
506   }
507
508   Font* text_font = default_font;
509   float x_pos       = pos_x;
510   float y_pos       = pos_y + 24*index - menu_height/2 + 12 + effect_offset;
511   int shadow_size = 2;
512   int text_width  = int(text_font->get_text_width(pitem.text));
513   int input_width = int(text_font->get_text_width(pitem.input) + 10);
514   int list_width = 0;
515   if(pitem.list.size() > 0) {
516     list_width = (int) text_font->get_text_width(pitem.list[pitem.selected]);
517   }
518   
519   if (arrange_left)
520     x_pos += 24 - menu_width/2 + (text_width + input_width + list_width)/2;
521
522   if(index == active_item)
523     {
524       shadow_size = 3;
525       text_font = active_font;
526     }
527
528   switch (pitem.kind)
529     {
530     case MN_DEACTIVE:
531       {
532         context.draw_text(deactive_font, pitem.text,
533                           Vector(SCREEN_WIDTH/2, y_pos - int(deactive_font->get_height()/2)),
534                           CENTER_ALLIGN, LAYER_GUI);
535         break;
536       }
537
538     case MN_HL:
539       {
540         // TODO
541         float x = pos_x - menu_width/2;
542         float y = y_pos - 12 - effect_offset;
543         /* Draw a horizontal line with a little 3d effect */
544         context.draw_filled_rect(Vector(x, y + 6),
545                                  Vector(menu_width, 4),
546                                  Color(0.6f, 0.7f, 1.0f, 1.0f), LAYER_GUI);
547         context.draw_filled_rect(Vector(x, y + 6),
548                                  Vector(menu_width, 2),
549                                  Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI);
550         break;
551       }
552     case MN_LABEL:
553       {
554         context.draw_text(label_font, pitem.text,
555                           Vector(SCREEN_WIDTH/2, y_pos - int(label_font->get_height()/2)),
556                           CENTER_ALLIGN, LAYER_GUI);
557         break;
558       }
559     case MN_TEXTFIELD:
560     case MN_NUMFIELD:
561     case MN_CONTROLFIELD:
562       {
563         float width = text_width + input_width + 5;
564         float text_pos = SCREEN_WIDTH/2 - width/2;
565         float input_pos = text_pos + text_width + 10;
566
567         context.draw_filled_rect(
568           Vector(input_pos - 5, y_pos - 10),
569           Vector(input_width + 10, 20),
570           Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI-5);
571         context.draw_filled_rect(
572           Vector(input_pos - 4, y_pos - 9),
573           Vector(input_width + 8, 18),
574           Color(0, 0, 0, 0.5f), LAYER_GUI-4);
575
576         if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD)
577           {
578             if(active_item == index)
579               context.draw_text(field_font,
580                                 pitem.get_input_with_symbol(true),
581                                 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
582                                 LEFT_ALLIGN, LAYER_GUI);
583             else
584               context.draw_text(field_font,
585                                 pitem.get_input_with_symbol(false),
586                                 Vector(input_pos, y_pos - int(field_font->get_height()/2)),
587                                 LEFT_ALLIGN, LAYER_GUI);
588           }
589         else
590           context.draw_text(field_font, pitem.input,
591                             Vector(input_pos, y_pos - int(field_font->get_height()/2)),
592                             LEFT_ALLIGN, LAYER_GUI);
593
594         context.draw_text(text_font, pitem.text,
595                           Vector(text_pos, y_pos - int(text_font->get_height()/2)),
596                           LEFT_ALLIGN, LAYER_GUI);
597         break;
598       }
599     case MN_STRINGSELECT:
600       {
601         int list_pos_2 = list_width + 16;
602         int list_pos   = list_width/2;
603         int text_pos   = (text_width + 16)/2;
604
605         /* Draw arrows */
606         context.draw_surface(arrow_left.get(),
607                              Vector(x_pos - list_pos + text_pos - 17, y_pos - 8),
608                              LAYER_GUI);
609         context.draw_surface(arrow_right.get(),
610                              Vector(x_pos - list_pos + text_pos - 1 + list_pos_2, y_pos - 8),
611                              LAYER_GUI);
612
613         /* Draw input background */
614         context.draw_filled_rect(
615           Vector(x_pos - list_pos + text_pos - 1, y_pos - 10),
616           Vector(list_pos_2 + 2, 20),
617           Color(1.0f, 1.0f, 1.0f, 1.0f), LAYER_GUI - 4);
618         context.draw_filled_rect(
619           Vector(x_pos - list_pos + text_pos, y_pos - 9),
620           Vector(list_pos_2, 18),
621           Color(0, 0, 0, 0.5f), LAYER_GUI - 5);
622
623         context.draw_text(text_font, pitem.list[pitem.selected],
624                                  Vector(SCREEN_WIDTH/2 + text_pos, y_pos - int(text_font->get_height()/2)),
625                                  CENTER_ALLIGN, LAYER_GUI);
626         context.draw_text(text_font, pitem.text,
627                                  Vector(SCREEN_WIDTH/2  + list_pos_2/2, y_pos - int(text_font->get_height()/2)),
628                                  CENTER_ALLIGN, LAYER_GUI);
629         break;
630       }
631     case MN_BACK:
632       {
633         context.draw_text(text_font, pitem.text,
634                           Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
635                           CENTER_ALLIGN, LAYER_GUI);
636         context.draw_surface(back.get(),
637                              Vector(x_pos + text_width/2  + 16, y_pos - 8),
638                              LAYER_GUI);
639         break;
640       }
641
642     case MN_TOGGLE:
643       {
644         context.draw_text(text_font, pitem.text,
645                           Vector(SCREEN_WIDTH/2, y_pos - (text_font->get_height()/2)),
646                           CENTER_ALLIGN, LAYER_GUI);
647
648         if(pitem.toggled)
649           context.draw_surface(checkbox_checked.get(),
650                                Vector(x_pos + (text_width+16)/2, y_pos - 8),
651                                LAYER_GUI + 1);
652         else
653           context.draw_surface(checkbox.get(),
654                                Vector(x_pos + (text_width+16)/2, y_pos - 8),
655                                LAYER_GUI + 1);
656         break;
657       }
658     case MN_ACTION:
659       context.draw_text(text_font, pitem.text,
660                         Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
661                         CENTER_ALLIGN, LAYER_GUI);
662       break;
663
664     case MN_GOTO:
665       context.draw_text(text_font, pitem.text,
666                         Vector(SCREEN_WIDTH/2, y_pos - int(text_font->get_height()/2)),
667                         CENTER_ALLIGN, LAYER_GUI);
668       break;
669     }
670 }
671
672 float Menu::get_width() const
673 {
674   /* The width of the menu has to be more than the width of the text
675      with the most characters */
676   float menu_width = 0;
677   for(unsigned int i = 0; i < items.size(); ++i)
678   {
679     Font* font = default_font;
680     if(items[i]->kind == MN_LABEL)
681       font = label_font;
682
683     float w = font->get_text_width(items[i]->text) +
684         label_font->get_text_width(items[i]->input) + 16;
685     if(items[i]->kind == MN_TOGGLE)
686       w += 32;
687     
688     if(w > menu_width)
689       menu_width = w;
690   }
691   
692   return menu_width + 24;
693 }
694
695 float Menu::get_height() const
696 {
697   return items.size() * 24;
698 }
699
700 /* Draw the current menu. */
701 void
702 Menu::draw(DrawingContext& context)
703 {
704   if(MouseCursor::current()) {
705     MouseCursor::current()->draw(context);
706   }
707   
708   float menu_height = get_height();
709   float menu_width = get_width();  
710
711   /* Draw a transparent background */
712   context.draw_filled_rect(
713     Vector(pos_x - menu_width/2, pos_y - 24*items.size()/2 - 10),
714     Vector(menu_width,menu_height + 20),
715     Color(0.6f, 0.7f, 0.8f, 0.5f), LAYER_GUI-10);
716
717   for(unsigned int i = 0; i < items.size(); ++i)
718     {
719       draw_item(context, i);
720     }
721 }
722
723 MenuItem&
724 Menu::get_item_by_id(int id)
725 {
726   for(std::vector<MenuItem*>::iterator i = items.begin();
727       i != items.end(); ++i) {
728     MenuItem& item = **i;
729     
730     if(item.id == id)
731       return item;
732   }
733
734   throw std::runtime_error("MenuItem not found");
735 }
736
737 const MenuItem&
738 Menu::get_item_by_id(int id) const
739 {
740   for(std::vector<MenuItem*>::const_iterator i = items.begin();
741       i != items.end(); ++i) {
742     const MenuItem& item = **i;
743     
744     if(item.id == id)
745       return item;
746   }
747
748   throw std::runtime_error("MenuItem not found");
749 }
750
751 int Menu::get_active_item_id()
752 {
753   return items[active_item]->id;
754 }
755
756 bool
757 Menu::is_toggled(int id) const
758 {
759   return get_item_by_id(id).toggled;
760 }
761
762 /* Check for menu event */
763 void
764 Menu::event(const SDL_Event& event)
765 {
766   if(effect_time != 0)
767     return;
768
769   switch(event.type) {
770     case SDL_MOUSEBUTTONDOWN:
771       {
772         int x = int(event.motion.x * float(SCREEN_WIDTH)/screen->w);
773         int y = int(event.motion.y * float(SCREEN_HEIGHT)/screen->h);
774
775         if(x > pos_x - get_width()/2 &&
776             x < pos_x + get_width()/2 &&
777             y > pos_y - get_height()/2 &&
778             y < pos_y + get_height()/2)
779           {
780             menuaction = MENU_ACTION_HIT;
781           }
782       }
783       break;
784
785     case SDL_MOUSEMOTION:
786       {
787         float x = event.motion.x * SCREEN_WIDTH/screen->w;
788         float y = event.motion.y * SCREEN_HEIGHT/screen->h;
789
790         if(x > pos_x - get_width()/2 &&
791             x < pos_x + get_width()/2 &&
792             y > pos_y - get_height()/2 &&
793             y < pos_y + get_height()/2)
794           {
795             int new_active_item 
796               = static_cast<int> ((y - (pos_y - get_height()/2)) / 24);
797           
798             /* only change the mouse focus to a selectable item */
799             if ((items[new_active_item]->kind != MN_HL)
800                 && (items[new_active_item]->kind != MN_LABEL)
801                 && (items[new_active_item]->kind != MN_DEACTIVE))
802               active_item = new_active_item;
803             
804             if(MouseCursor::current())
805               MouseCursor::current()->set_state(MC_LINK);
806           }
807         else
808         {
809           if(MouseCursor::current())
810             MouseCursor::current()->set_state(MC_NORMAL);
811         }
812       }
813       break;
814
815     default:
816       break;
817     }
818 }
819
820 void
821 Menu::set_active_item(int id)
822 {
823   for(size_t i = 0; i < items.size(); ++i) {
824     MenuItem* item = items[i];
825     if(item->id == id) {
826       active_item = i;
827       break;
828     }
829   }
830 }