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