The Battle for Wesnoth  1.19.2+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  * At the moment two kinds of tips are known:
35  * * tooltip
36  * * helptip
37  *
38  * Generic window to show a floating tip window.
39  * The class has several subclasses using the same format.
40  */
41 class tooltip : public modeless_dialog
42 {
43 public:
44  tooltip(const std::string& window_id, const t_string& message,
45  const point& mouse, const SDL_Rect& source_rect)
46  : modeless_dialog(window_id)
47  {
48  find_widget<styled_widget>(this, "label", false).set_label(message);
49 
50  set_variable("mouse_x", wfl::variant(mouse.x));
51  set_variable("mouse_y", wfl::variant(mouse.y));
52 
53  set_variable("source_x", wfl::variant(source_rect.x));
54  set_variable("source_y", wfl::variant(source_rect.y));
55  set_variable("source_w", wfl::variant(source_rect.w));
56  set_variable("source_h", wfl::variant(source_rect.h));
57  }
58 };
59 
60 namespace tip
61 {
62 
63 static std::unique_ptr<tooltip> tip;
64 
65 void show(const std::string& window_id,
66  const t_string& message,
67  const point& mouse,
68  const SDL_Rect& source_rect)
69 {
70  /*
71  * For now allow invalid tip names, might turn them to invalid wml messages
72  * later on.
73  */
74  tip.reset(new tooltip(window_id, message, mouse, source_rect));
75  try
76  {
77  tip->show();
78  }
79  catch(const window_builder_invalid_id&)
80  {
81  ERR_CFG << "Tip with the requested id '" << window_id
82  << "' doesn't exist, fall back to the default.";
83  tip.reset(new tooltip("tooltip_large", message, mouse, source_rect));
84  try
85  {
86  tip->show();
87  }
88  catch(const window_builder_invalid_id&)
89  {
90  ERR_CFG << "Default tooltip doesn't exist, no message shown.";
91  }
92  }
93 }
94 
95 void remove()
96 {
97  tip.reset();
98 }
99 
100 } // namespace tip
101 
102 } // namespace dialogs
Main class to show messages to the user.
Definition: message.hpp:36
The popup class shows windows that are shown non-modal.
At the moment two kinds of tips are known:
Definition: tooltip.cpp:42
tooltip(const std::string &window_id, const t_string &message, const point &mouse, const SDL_Rect &source_rect)
Definition: tooltip.cpp:44
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:209
#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:65
void remove()
Removes a tip.
Definition: tooltip.cpp:95
static std::unique_ptr< tooltip > tip
Definition: tooltip.cpp:63
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")