The Battle for Wesnoth  1.15.1+dev
tooltip.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include "gui/dialogs/tooltip.hpp"
18 
23 #include "gui/widgets/settings.hpp"
24 #include "gui/widgets/window.hpp"
25 
26 static lg::log_domain log_config("config");
27 #define ERR_CFG LOG_STREAM(warn, log_config)
28 
29 namespace gui2
30 {
31 namespace dialogs
32 {
33 
34 /*WIKI
35  * @page = GUIWindowDefinitionWML
36  * @order = 2_tip
37  *
38  * == Tip float ==
39  *
40  * Generic window to show a floating tip window. The class has several
41  * subclasses using the same format. For example there will be tooltips and
42  * helptips, both using this class.
43  *
44  * @begin{table}{dialog_widgets}
45  *
46  * label & & styled_widget & m &
47  * This text contains the message to show in the tip. $
48  *
49  * @end{table}
50  *
51  * In the canvas of the windows used in this dialog the following variables are
52  * defined:
53  *
54  * @begin{table}{formula}
55  * mouse_x & unsigned & The x coordinate of the mouse pointer when
56  * the window was created. $
57  * mouse_y & unsigned & The y coordinate of the mouse pointer when
58  * the window was created. $
59  * @end{table}
60  */
61 
62 REGISTER_WINDOW(tooltip_large)
63 
64 /**
65  * Class to show the tips.
66  *
67  * At the moment two kinds of tips are known:
68  * * tooltip
69  * * helptip
70  */
71 class tooltip : public modeless_dialog
72 {
73 public:
74  tooltip() : modeless_dialog(), window_id_(), message_(), mouse_()
75  {
76  // To make Coverity happy
77  source_rect_.x = 0;
78  source_rect_.y = 0;
79  source_rect_.w = 0;
80  source_rect_.h = 0;
81  }
82 
83  void set_window_id(const std::string& window_id)
84  {
85  window_id_ = window_id;
86  }
87 
89  {
90  message_ = message;
91  }
92 
93  void set_mouse(const point& mouse)
94  {
95  mouse_ = mouse;
96  }
97 
98  void set_source_rect(const SDL_Rect& rect)
99  {
100  source_rect_ = rect;
101  }
102 
103 private:
104  /** The id of the window to use to show the tip. */
105  std::string window_id_;
106 
107  /** The message to show. */
109 
110  /** The position of the mouse. */
112 
113  /** The size of the requestor. */
114  SDL_Rect source_rect_;
115 
116  /** Inherited from modeless_dialog. */
117  virtual const std::string& window_id() const override;
118 
119  /** Inherited from modeless_dialog. */
120  virtual void pre_show(window& window) override;
121 };
122 
124 {
125  find_widget<styled_widget>(&window, "label", false).set_label(message_);
126 
127  window.set_variable("mouse_x", wfl::variant(mouse_.x));
128  window.set_variable("mouse_y", wfl::variant(mouse_.y));
129 
130  window.set_variable("source_x", wfl::variant(source_rect_.x));
131  window.set_variable("source_y", wfl::variant(source_rect_.y));
132  window.set_variable("source_w", wfl::variant(source_rect_.w));
133  window.set_variable("source_h", wfl::variant(source_rect_.h));
134 }
135 
136 const std::string& tooltip::window_id() const
137 {
138  return window_id_;
139 }
140 
141 namespace tip
142 {
143 
144 static tooltip& tip()
145 {
146  /*
147  * Allocating a static tip object causes a segmentation fault when Wesnoth
148  * terminates. So instead create an object on the heap and never free it.
149  */
150  static tooltip* t = new tooltip();
151  return *t;
152 }
153 
154 void show(const std::string& window_id,
155  const t_string& message,
156  const point& mouse,
157  const SDL_Rect& source_rect)
158 {
159  /*
160  * For now allow invalid tip names, might turn them to invalid wml messages
161  * later on.
162  */
163  tooltip& t = tip();
164  t.set_window_id(window_id);
165  t.set_message(message);
166  t.set_mouse(mouse);
167  t.set_source_rect(source_rect);
168  try
169  {
170  t.show();
171  }
172  catch(const window_builder_invalid_id&)
173  {
174  ERR_CFG << "Tip with the requested id '" << window_id
175  << "' doesn't exist, fall back to the default.\n";
176  t.set_window_id("tooltip_large");
177  try
178  {
179  t.show();
180  }
181  catch(const window_builder_invalid_id&)
182  {
183  ERR_CFG << "Default tooltip doesn't exist, no message shown."
184  << std::endl;
185  }
186  }
187 }
188 
189 void remove()
190 {
191  tip().hide();
192 }
193 
194 } // namespace tip
195 
196 } // namespace dialogs
197 } // namespace gui2
SDL_Rect source_rect_
The size of the requestor.
Definition: tooltip.cpp:114
Main class to show messages to the user.
Definition: message.hpp:34
void set_source_rect(const SDL_Rect &rect)
Definition: tooltip.cpp:98
This file contains the window object, this object is a top level container which has the event manage...
void set_mouse(const point &mouse)
Definition: tooltip.cpp:93
#define ERR_CFG
Definition: tooltip.cpp:27
int x
x coordinate.
Definition: point.hpp:44
Generic file dialog.
Definition: field-fwd.hpp:22
Helper struct to signal that get_window_builder failed.
std::string window_id_
The id of the window to use to show the tip.
Definition: tooltip.cpp:105
This file contains the settings handling of the widget library.
void set_message(const t_string &message)
Definition: tooltip.cpp:88
void hide()
Hides the window.
static tooltip & tip()
Definition: tooltip.cpp:144
Various uncategorised dialogs.
virtual void pre_show(window &window) override
Inherited from modeless_dialog.
Definition: tooltip.cpp:123
void set_window_id(const std::string &window_id)
Definition: tooltip.cpp:83
point mouse_
The position of the mouse.
Definition: tooltip.cpp:111
Holds a 2D point.
Definition: point.hpp:23
void show(const bool allow_interaction=false, const unsigned auto_close_time=0)
Shows the window.
static lg::log_domain log_config("config")
t_string message_
The message to show.
Definition: tooltip.cpp:108
double t
Definition: astarsearch.cpp:64
The popup class shows windows that are shown non-modal.
Class to show the tips.
Definition: tooltip.cpp:71
virtual const std::string & window_id() const override
Inherited from modeless_dialog.
Definition: tooltip.cpp:136
int y
y coordinate.
Definition: point.hpp:47
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63