floating_textbox.cpp

Go to the documentation of this file.
00001 /* $Id: floating_textbox.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2006 - 2012 by Joerg Hinrichs <joerg.hinrichs@alice-dsl.de>
00004    wesnoth playturn Copyright (C) 2003 by David White <dave@whitevine.net>
00005    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY.
00013 
00014    See the COPYING file for more details.
00015 */
00016 
00017 #include "global.hpp"
00018 
00019 #include "floating_textbox.hpp"
00020 #include "game_display.hpp"
00021 #include "game_preferences.hpp"
00022 #include "log.hpp"
00023 #include "resources.hpp"
00024 
00025 #include <ctime>
00026 
00027 static lg::log_domain log_display("display");
00028 #define ERR_DP LOG_STREAM(err, log_display)
00029 
00030 namespace gui{
00031     floating_textbox::floating_textbox() :
00032         box_(NULL),
00033         check_(NULL),
00034         mode_(TEXTBOX_NONE),
00035         label_string_(),
00036         label_(0)
00037     {}
00038 
00039     void floating_textbox::close(game_display& gui)
00040     {
00041         if(!active()) {
00042             return;
00043         }
00044         if(check_ != NULL) {
00045             if(mode_ == TEXTBOX_MESSAGE) {
00046                 preferences::set_message_private(check_->checked());
00047             }
00048         }
00049         box_.assign(NULL);
00050         check_.assign(NULL);
00051         font::remove_floating_label(label_);
00052         mode_ = TEXTBOX_NONE;
00053         gui.invalidate_all();
00054     }
00055 
00056     void floating_textbox::update_location(game_display& gui)
00057     {
00058         if (box_ == NULL)
00059             return;
00060 
00061         const SDL_Rect& area = gui.map_outside_area();
00062 
00063         const int border_size = 10;
00064 
00065         const int ypos = area.y+area.h-30 - (check_ != NULL ? check_->height() + border_size : 0);
00066 
00067         if (label_ != 0)
00068             font::remove_floating_label(label_);
00069 
00070         font::floating_label flabel(label_string_);
00071         flabel.set_color(font::YELLOW_COLOR);
00072         flabel.set_position(area.x + border_size, ypos);
00073         flabel.set_alignment(font::LEFT_ALIGN);
00074         flabel.set_clip_rect(area);
00075 
00076         label_ = font::add_floating_label(flabel);
00077 
00078         if (label_ == 0)
00079             return;
00080 
00081         const SDL_Rect& label_area = font::get_floating_label_rect(label_);
00082         const int textbox_width = area.w - label_area.w - border_size*3;
00083 
00084         if(textbox_width <= 0) {
00085             font::remove_floating_label(label_);
00086             return;
00087         }
00088 
00089         if(box_ != NULL) {
00090             box_->set_volatile(true);
00091             const SDL_Rect rect = create_rect(
00092                   area.x + label_area.w + border_size * 2
00093                 , ypos
00094                 , textbox_width
00095                 , box_->height());
00096 
00097             box_->set_location(rect);
00098         }
00099 
00100         if(check_ != NULL) {
00101             check_->set_volatile(true);
00102             check_->set_location(box_->location().x,box_->location().y + box_->location().h + border_size);
00103         }
00104     }
00105 
00106     void floating_textbox::show(gui::TEXTBOX_MODE mode, const std::string& label,
00107         const std::string& check_label, bool checked, game_display& gui)
00108     {
00109         close(gui);
00110 
00111         label_string_ = label;
00112         mode_ = mode;
00113 
00114         if(check_label != "") {
00115             check_.assign(new gui::button(gui.video(),check_label,gui::button::TYPE_CHECK));
00116             check_->set_check(checked);
00117         }
00118 
00119 
00120         box_.assign(new gui::textbox(gui.video(),100,"",true,256,0.8,0.6));
00121 
00122         update_location(gui);
00123     }
00124 
00125     void floating_textbox::tab(const std::set<std::string>& dictionary)
00126     {
00127         if(active() == false) {
00128             return;
00129         }
00130 
00131         std::string text = box_->text();
00132         std::vector<std::string> matches(dictionary.begin(), dictionary.end());
00133         const bool line_start = utils::word_completion(text, matches);
00134 
00135         if (matches.empty()) return;
00136         if (matches.size() == 1 && mode_ == gui::TEXTBOX_MESSAGE) {
00137             text.append(line_start ? ": " : " ");
00138         } else if (matches.size() > 1) {
00139             std::string completion_list = utils::join(matches, " ");
00140             resources::screen->add_chat_message(time(NULL), "", 0, completion_list,
00141                     events::chat_handler::MESSAGE_PRIVATE, false);
00142         }
00143         box_->set_text(text);
00144     }
00145 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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