The Battle for Wesnoth  1.19.0-dev
tooltip.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2024
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 
25 static lg::log_domain log_config("config");
26 #define ERR_CFG LOG_STREAM(warn, log_config)
27 
28 namespace gui2::dialogs
29 {
30 
31 REGISTER_WINDOW(tooltip_large)
32 
33 /**
34  * @ingroup GUIWindowDefinitionWML
35  *
36  * Class to show the tips.
37  *
38  * At the moment two kinds of tips are known:
39  * * tooltip
40  * * helptip
41  *
42  * Generic window to show a floating tip window.
43  * The class has several subclasses using the same format.
44  * For example there will be tooltips and helptips, both using this class.
45  * Key |Type |Mandatory|Description
46  * ------------------|--------------|---------|-----------
47  * label | control |yes |This text contains the message to show in the tip.
48  *
49  * In the canvas of the windows used in this dialog the following variables are defined:
50  * Variable |Type |Description
51  * ------------------|-----------------------------------|-----------
52  * mouse_x | @ref guivartype_string "unsigned" |The x coordinate of the mouse pointer when the window was created.
53  * mouse_y | @ref guivartype_string "unsigned" |The y coordinate of the mouse pointer when the window was created.
54  */
55 class tooltip : public modeless_dialog
56 {
57 public:
58  tooltip(const std::string& window_id, const t_string& message,
59  const point& mouse, const SDL_Rect& source_rect)
60  : modeless_dialog(window_id)
61  {
62  find_widget<styled_widget>(this, "label", false).set_label(message);
63 
64  set_variable("mouse_x", wfl::variant(mouse.x));
65  set_variable("mouse_y", wfl::variant(mouse.y));
66 
67  set_variable("source_x", wfl::variant(source_rect.x));
68  set_variable("source_y", wfl::variant(source_rect.y));
69  set_variable("source_w", wfl::variant(source_rect.w));
70  set_variable("source_h", wfl::variant(source_rect.h));
71  }
72 };
73 
74 namespace tip
75 {
76 
77 static std::unique_ptr<tooltip> tip;
78 
79 void show(const std::string& window_id,
80  const t_string& message,
81  const point& mouse,
82  const SDL_Rect& source_rect)
83 {
84  /*
85  * For now allow invalid tip names, might turn them to invalid wml messages
86  * later on.
87  */
88  tip.reset(new tooltip(window_id, message, mouse, source_rect));
89  try
90  {
91  tip->show();
92  }
93  catch(const window_builder_invalid_id&)
94  {
95  ERR_CFG << "Tip with the requested id '" << window_id
96  << "' doesn't exist, fall back to the default.";
97  tip.reset(new tooltip("tooltip_large", message, mouse, source_rect));
98  try
99  {
100  tip->show();
101  }
102  catch(const window_builder_invalid_id&)
103  {
104  ERR_CFG << "Default tooltip doesn't exist, no message shown.";
105  }
106  }
107 }
108 
109 void remove()
110 {
111  tip.reset();
112 }
113 
114 } // namespace tip
115 
116 } // namespace dialogs
Main class to show messages to the user.
Definition: message.hpp:36
The popup class shows windows that are shown non-modal.
Class to show the tips.
Definition: tooltip.cpp:56
tooltip(const std::string &window_id, const t_string &message, const point &mouse, const SDL_Rect &source_rect)
Definition: tooltip.cpp:58
virtual void set_label(const t_string &text)
std::string tooltip
Shown when hovering over an entry in the filter's drop-down list.
Definition: manager.cpp:211
#define REGISTER_WINDOW(id)
Registers a window.
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:79
void remove()
Removes a tip.
Definition: tooltip.cpp:109
static std::unique_ptr< tooltip > tip
Definition: tooltip.cpp:77
Helper struct to signal that get_window_builder failed.
Holds a 2D point.
Definition: point.hpp:25
#define ERR_CFG
Definition: tooltip.cpp:26
static lg::log_domain log_config("config")