The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
floating_textbox.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2016 by Joerg Hinrichs <[email protected]>
3  wesnoth playturn Copyright (C) 2003 by David White <[email protected]>
4  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #include "global.hpp"
17 #include "floating_textbox.hpp"
18 
19 #include "display_chat_manager.hpp"
20 #include "floating_label.hpp"
21 #include "game_display.hpp"
22 #include "game_preferences.hpp"
23 #include "log.hpp"
24 #include "resources.hpp"
25 
26 #include <ctime>
27 
28 static lg::log_domain log_display("display");
29 #define ERR_DP LOG_STREAM(err, log_display)
30 
31 namespace gui{
33  box_(nullptr),
34  check_(nullptr),
35  mode_(TEXTBOX_NONE),
36  label_string_(),
37  label_(0)
38  {}
39 
41  {
42  if(!active()) {
43  return;
44  }
45  if(check_ != nullptr) {
46  if(mode_ == TEXTBOX_MESSAGE) {
48  }
49  }
50  box_.assign(nullptr);
51  check_.assign(nullptr);
54  gui.invalidate_all();
55  }
56 
58  {
59  if (box_ == nullptr)
60  return;
61 
62  const SDL_Rect& area = gui.map_outside_area();
63 
64  const int border_size = 10;
65 
66  const int ypos = area.y+area.h-30 - (check_ != nullptr ? check_->height() + border_size : 0);
67 
68  if (label_ != 0)
70 
73  flabel.set_position(area.x + border_size, ypos);
75  flabel.set_clip_rect(area);
76 
78 
79  if (label_ == 0)
80  return;
81 
82  const SDL_Rect& label_area = font::get_floating_label_rect(label_);
83  const int textbox_width = area.w - label_area.w - border_size*3;
84 
85  if(textbox_width <= 0) {
87  return;
88  }
89 
90  if(box_ != nullptr) {
91  box_->set_volatile(true);
92  const SDL_Rect rect = sdl::create_rect(
93  area.x + label_area.w + border_size * 2
94  , ypos
95  , textbox_width
96  , box_->height());
97 
98  box_->set_location(rect);
99  }
100 
101  if(check_ != nullptr) {
102  check_->set_volatile(true);
103  check_->set_location(box_->location().x,box_->location().y + box_->location().h + border_size);
104  }
105  }
106 
108  const std::string& check_label, bool checked, game_display& gui)
109  {
110  close(gui);
111 
112  label_string_ = label;
113  mode_ = mode;
114 
115  if(check_label != "") {
116  check_.assign(new gui::button(gui.video(),check_label,gui::button::TYPE_CHECK));
117  check_->set_check(checked);
118  }
119 
120 
121  box_.assign(new gui::textbox(gui.video(),100,"",true,256,font::SIZE_PLUS,0.8,0.6));
122 
123  update_location(gui);
124  }
125 
126  void floating_textbox::tab(const std::set<std::string>& dictionary)
127  {
128  if(active() == false) {
129  return;
130  }
131 
132  std::string text = box_->text();
133  std::vector<std::string> matches(dictionary.begin(), dictionary.end());
134  const bool line_start = utils::word_completion(text, matches);
135 
136  if (matches.empty()) return;
137  if (matches.size() == 1 && mode_ == gui::TEXTBOX_MESSAGE) {
138  text.append(line_start ? ": " : " ");
139  } else if (matches.size() > 1) {
140  std::string completion_list = utils::join(matches, " ");
141  resources::screen->get_chat_manager().add_chat_message(time(nullptr), "", 0, completion_list,
143  }
144  box_->set_text(text);
145  }
146 }
void set_clip_rect(const SDL_Rect &r)
game_display * screen
Definition: resources.cpp:27
void remove_floating_label(int handle)
removes the floating label given by 'handle' from the screen
General purpose widgets.
const int SIZE_PLUS
Definition: font.hpp:65
GLenum mode
Definition: glew.h:2390
util::scoped_ptr< gui::textbox > box_
void set_alignment(ALIGN align)
void update_location(game_display &gui)
void set_color(const SDL_Color &color)
void set_position(double xpos, double ypos)
void invalidate_all()
Function to invalidate all tiles.
Definition: display.cpp:3525
void set_message_private(bool value)
std::string join(T const &v, const std::string &s=",")
Generates a new string joining container items in a list.
void tab(const std::set< std::string > &dictionary)
int add_floating_label(const floating_label &flabel)
add a label floating on the screen above everything else.
SDL_Rect get_floating_label_rect(int handle)
void show(gui::TEXTBOX_MODE mode, const std::string &label, const std::string &check_label, bool checked, game_display &gui)
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
Creates an empty SDL_Rect.
Definition: rect.cpp:28
static lg::log_domain log_display("display")
display_chat_manager & get_chat_manager()
util::scoped_ptr< gui::button > check_
Standard logging facilities (interface).
const SDL_Rect & map_outside_area() const
Returns the available area for a map, this may differ from the above.
Definition: display.hpp:248
CVideo & video()
Gets the underlying screen object.
Definition: display.hpp:202
const SDL_Color YELLOW_COLOR
Definition: font.cpp:570
void add_chat_message(const time_t &time, const std::string &speaker, int side, const std::string &msg, events::chat_handler::MESSAGE_TYPE type, bool bell)
void close(game_display &gui)
GLsizei const GLcharARB ** string
Definition: glew.h:4503
bool word_completion(std::string &text, std::vector< std::string > &wordlist)
Try to complete the last word of 'text' with the 'wordlist'.
TEXTBOX_MODE mode() const