The Battle for Wesnoth  1.17.12+dev
tooltip.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2022
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(const std::string& window_id, const t_string& message,
61  const point& mouse, const SDL_Rect& source_rect)
62  : modeless_dialog(window_id)
63  {
64  find_widget<styled_widget>(this, "label", false).set_label(message);
65 
66  set_variable("mouse_x", wfl::variant(mouse.x));
67  set_variable("mouse_y", wfl::variant(mouse.y));
68 
69  set_variable("source_x", wfl::variant(source_rect.x));
70  set_variable("source_y", wfl::variant(source_rect.y));
71  set_variable("source_w", wfl::variant(source_rect.w));
72  set_variable("source_h", wfl::variant(source_rect.h));
73  }
74 };
75 
76 namespace tip
77 {
78 
79 static std::unique_ptr<tooltip> tip;
80 
81 void show(const std::string& window_id,
82  const t_string& message,
83  const point& mouse,
84  const SDL_Rect& source_rect)
85 {
86  /*
87  * For now allow invalid tip names, might turn them to invalid wml messages
88  * later on.
89  */
90  tip.reset(new tooltip(window_id, message, mouse, source_rect));
91  try
92  {
93  tip->show();
94  }
95  catch(const window_builder_invalid_id&)
96  {
97  ERR_CFG << "Tip with the requested id '" << window_id
98  << "' doesn't exist, fall back to the default.";
99  tip.reset(new tooltip("tooltip_large", message, mouse, source_rect));
100  try
101  {
102  tip->show();
103  }
104  catch(const window_builder_invalid_id&)
105  {
106  ERR_CFG << "Default tooltip doesn't exist, no message shown.";
107  }
108  }
109 }
110 
111 void remove()
112 {
113  tip.reset();
114 }
115 
116 } // namespace tip
117 
118 } // namespace dialogs
Main class to show messages to the user.
Definition: message.hpp:35
This file contains the window object, this object is a top level container which has the event manage...
#define ERR_CFG
Definition: tooltip.cpp:28
Helper struct to signal that get_window_builder failed.
This file contains the settings handling of the widget library.
static std::unique_ptr< tooltip > tip
Definition: tooltip.cpp:79
tooltip(const std::string &window_id, const t_string &message, const point &mouse, const SDL_Rect &source_rect)
Definition: tooltip.cpp:60
Holds a 2D point.
Definition: point.hpp:24
static lg::log_domain log_config("config")
The popup class shows windows that are shown non-modal.
Class to show the tips.
Definition: tooltip.cpp:57
std::string tooltip
Shown when hovering over an entry in the filter&#39;s drop-down list.
Definition: manager.cpp:219
void show(const std::string &window_id, const t_string &message, const point &mouse, const SDL_Rect &source_rect)
Shows a tip.
Definition: tooltip.cpp:81