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