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