The Battle for Wesnoth  1.17.0-dev
tooltip.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://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 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
18 #include "gui/dialogs/tooltip.hpp"
19 
24 #include "gui/widgets/settings.hpp"
25 #include "gui/widgets/window.hpp"
26 
27 static lg::log_domain log_config("config");
28 #define ERR_CFG LOG_STREAM(warn, log_config)
29 
30 namespace gui2::dialogs
31 {
32 
33 REGISTER_WINDOW(tooltip_large)
34 
35 /**
36  * @ingroup GUIWindowDefinitionWML
37  *
38  * Class to show the tips.
39  *
40  * At the moment two kinds of tips are known:
41  * * tooltip
42  * * helptip
43  *
44  * Generic window to show a floating tip window.
45  * The class has several subclasses using the same format.
46  * For example there will be tooltips and helptips, both using this class.
47  * Key |Type |Mandatory|Description
48  * ------------------|--------------|---------|-----------
49  * label | control |yes |This text contains the message to show in the tip.
50  *
51  * In the canvas of the windows used in this dialog the following variables are defined:
52  * Variable |Type |Description
53  * ------------------|-----------------------------------|-----------
54  * mouse_x | @ref guivartype_string "unsigned" |The x coordinate of the mouse pointer when the window was created.
55  * mouse_y | @ref guivartype_string "unsigned" |The y coordinate of the mouse pointer when the window was created.
56  */
57 class tooltip : public modeless_dialog
58 {
59 public:
60  tooltip() : modeless_dialog(), window_id_(), message_(), mouse_()
61  {
62  // To make Coverity happy
63  source_rect_.x = 0;
64  source_rect_.y = 0;
65  source_rect_.w = 0;
66  source_rect_.h = 0;
67  }
68 
69  void set_window_id(const std::string& window_id)
70  {
71  window_id_ = window_id;
72  }
73 
75  {
76  message_ = message;
77  }
78 
79  void set_mouse(const point& mouse)
80  {
81  mouse_ = mouse;
82  }
83 
84  void set_source_rect(const SDL_Rect& rect)
85  {
86  source_rect_ = rect;
87  }
88 
89 private:
90  /** The id of the window to use to show the tip. */
91  std::string window_id_;
92 
93  /** The message to show. */
95 
96  /** The position of the mouse. */
98 
99  /** The size of the requestor. */
100  SDL_Rect source_rect_;
101 
102  /** Inherited from modeless_dialog. */
103  virtual const std::string& window_id() const override;
104 
105  /** Inherited from modeless_dialog. */
106  virtual void pre_show(window& window) override;
107 };
108 
110 {
111  find_widget<styled_widget>(&window, "label", false).set_label(message_);
112 
113  window.set_variable("mouse_x", wfl::variant(mouse_.x));
114  window.set_variable("mouse_y", wfl::variant(mouse_.y));
115 
116  window.set_variable("source_x", wfl::variant(source_rect_.x));
117  window.set_variable("source_y", wfl::variant(source_rect_.y));
118  window.set_variable("source_w", wfl::variant(source_rect_.w));
119  window.set_variable("source_h", wfl::variant(source_rect_.h));
120 }
121 
122 const std::string& tooltip::window_id() const
123 {
124  return window_id_;
125 }
126 
127 namespace tip
128 {
129 
130 static tooltip& tip()
131 {
132  /*
133  * Allocating a static tip object causes a segmentation fault when Wesnoth
134  * terminates. So instead create an object on the heap and never free it.
135  */
136  static tooltip* t = new tooltip();
137  return *t;
138 }
139 
140 void show(const std::string& window_id,
141  const t_string& message,
142  const point& mouse,
143  const SDL_Rect& source_rect)
144 {
145  /*
146  * For now allow invalid tip names, might turn them to invalid wml messages
147  * later on.
148  */
149  tooltip& t = tip();
150  t.set_window_id(window_id);
151  t.set_message(message);
152  t.set_mouse(mouse);
153  t.set_source_rect(source_rect);
154  try
155  {
156  t.show();
157  }
158  catch(const window_builder_invalid_id&)
159  {
160  ERR_CFG << "Tip with the requested id '" << window_id
161  << "' doesn't exist, fall back to the default.\n";
162  t.set_window_id("tooltip_large");
163  try
164  {
165  t.show();
166  }
167  catch(const window_builder_invalid_id&)
168  {
169  ERR_CFG << "Default tooltip doesn't exist, no message shown."
170  << std::endl;
171  }
172  }
173 }
174 
175 void remove()
176 {
177  tip().hide();
178 }
179 
180 } // namespace tip
181 
182 } // namespace dialogs
SDL_Rect source_rect_
The size of the requestor.
Definition: tooltip.cpp:100
Main class to show messages to the user.
Definition: message.hpp:35
void set_source_rect(const SDL_Rect &rect)
Definition: tooltip.cpp:84
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:79
#define ERR_CFG
Definition: tooltip.cpp:28
int x
x coordinate.
Definition: point.hpp:45
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:91
This file contains the settings handling of the widget library.
void set_message(const t_string &message)
Definition: tooltip.cpp:74
void hide()
Hides the window.
static tooltip & tip()
Definition: tooltip.cpp:130
virtual void pre_show(window &window) override
Inherited from modeless_dialog.
Definition: tooltip.cpp:109
void set_window_id(const std::string &window_id)
Definition: tooltip.cpp:69
point mouse_
The position of the mouse.
Definition: tooltip.cpp:97
Holds a 2D point.
Definition: point.hpp:24
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:94
double t
Definition: astarsearch.cpp:65
The popup class shows windows that are shown non-modal.
Class to show the tips.
Definition: tooltip.cpp:57
virtual const std::string & window_id() const override
Inherited from modeless_dialog.
Definition: tooltip.cpp:122
int y
y coordinate.
Definition: point.hpp:48
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:65