widgets/combo.cpp

Go to the documentation of this file.
00001 /* $Id: combo.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2003 - 2012 by David White <dave@whitevine.net>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #define GETTEXT_DOMAIN "wesnoth-lib"
00017 
00018 #include "global.hpp"
00019 
00020 #include "widgets/combo.hpp"
00021 #include "display.hpp"
00022 #include "show_dialog.hpp"
00023 
00024 
00025 namespace gui {
00026 
00027 const std::string combo::empty_combo_label = "";
00028 
00029 const int combo::font_size = font::SIZE_SMALL;
00030 const int combo::horizontal_padding = 10;
00031 const int combo::vertical_padding = 10;
00032 
00033 combo::combo(display& disp, const std::vector<std::string>& items)
00034     : button(disp.video(), items.empty() ? empty_combo_label : items[0]),
00035       items_(items), selected_(0), oldSelected_(0), disp_(&disp)
00036 {
00037 }
00038 
00039 int combo::selected() const
00040 {
00041     return selected_;
00042 }
00043 
00044 bool combo::changed()
00045 {
00046     if (oldSelected_ != selected_) {
00047         oldSelected_ = selected_;
00048         return true;
00049     } else
00050         return false;
00051 }
00052 
00053 void combo::set_items(const std::vector<std::string>& items)
00054 {
00055     items_ = items;
00056     selected_ = -1;
00057 }
00058 
00059 void combo::set_selected_internal(int val)
00060 {
00061     const size_t index = size_t(val);
00062     if (val == selected_ || index >= items_.size())
00063         return;
00064     set_label(items_[index]);
00065     oldSelected_ = selected_;
00066     selected_ = val;
00067 }
00068 
00069 void combo::set_selected(int val)
00070 {
00071     set_selected_internal(val);
00072     oldSelected_ = selected_;
00073 }
00074 
00075 void combo::make_drop_down_menu()
00076 {
00077     SDL_Rect const &loc = location();
00078     set_selected_internal(gui::show_dialog(*disp_, NULL, "", "", gui::MESSAGE, &items_,
00079                                            NULL, "", NULL, -1, NULL, loc.x, loc.y + loc.h));
00080 }
00081 
00082 void combo::process_event()
00083 {
00084     if (!pressed())
00085         return;
00086     make_drop_down_menu();
00087 }
00088 
00089 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:58 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs