The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tip.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://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/tip.hpp"
18 
19 #include "gui/dialogs/dialog.hpp"
21 #include "gui/dialogs/popup.hpp"
22 #include "gui/widgets/settings.hpp"
23 #include "gui/widgets/window.hpp"
24 
25 static lg::log_domain log_config("config");
26 #define ERR_CFG LOG_STREAM(warn, log_config)
27 
28 namespace gui2
29 {
30 
31 /*WIKI
32  * @page = GUIWindowDefinitionWML
33  * @order = 2_tip
34  *
35  * == Tip float ==
36  *
37  * Generic window to show a floating tip window. The class has several
38  * subclasses using the same format. For example there will be tooltips and
39  * helptips, both using this class.
40  *
41  * @begin{table}{dialog_widgets}
42  *
43  * label & & control & m &
44  * This text contains the message to show in the tip. $
45  *
46  * @end{table}
47  *
48  * In the canvas of the windows used in this dialog the following variables are
49  * defined:
50  *
51  * @begin{table}{formula}
52  * mouse_x & unsigned & The x coordinate of the mouse pointer when
53  * the window was created. $
54  * mouse_y & unsigned & The y coordinate of the mouse pointer when
55  * the window was created. $
56  * @end{table}
57  */
58 
59 REGISTER_WINDOW(tooltip_large)
60 
61 /**
62  * Class to show the tips.
63  *
64  * At the moment two kinds of tips are known:
65  * * tooltip
66  * * helptip
67  */
68 class ttip : public tpopup
69 {
70 public:
72  {
73  }
74 
76  {
78  }
79 
81  {
82  message_ = message;
83  }
84 
85  void set_mouse(const tpoint& mouse)
86  {
87  mouse_ = mouse;
88  }
89 
90 private:
91  /** The id of the window to use to show the tip. */
93 
94  /** The message to show. */
96 
97  /** The position of the mouse. */
99 
100  /** Inherited from tpopup. */
101  virtual const std::string& window_id() const;
102 
103  /** Inherited from tpopup. */
104  void pre_show(twindow& window);
105 };
106 
107 void ttip::pre_show(twindow& window)
108 {
109  find_widget<tcontrol>(&window, "label", false).set_label(message_);
110 
111  window.set_variable("mouse_x", variant(mouse_.x));
112  window.set_variable("mouse_y", variant(mouse_.y));
113 }
114 
116 {
117  return window_id_;
118 }
119 
120 namespace tip
121 {
122 
123 static ttip& tip()
124 {
125  /*
126  * Allocating a static tip object causes a segmentation fault when Wesnoth
127  * terminates. So instead create an object on the heap and never free it.
128  */
129  static ttip* t = new ttip();
130  return *t;
131 }
132 
133 void show(CVideo& video,
134  const std::string& window_id,
135  const t_string& message,
136  const tpoint& mouse)
137 {
138  /*
139  * For now allow invalid tip names, might turn them to invalid wml messages
140  * later on.
141  */
142  ttip& t = tip();
143  t.set_window_id(window_id);
144  t.set_message(message);
145  t.set_mouse(mouse);
146  try
147  {
148  t.show(video);
149  }
151  {
152  ERR_CFG << "Tip with the requested id '" << window_id
153  << "' doesn't exist, fall back to the default.\n";
154  t.set_window_id("tooltip_large");
155  try
156  {
157  t.show(video);
158  }
160  {
161  ERR_CFG << "Default tooltip doesn't exist, no message shown."
162  << std::endl;
163  }
164  }
165 }
166 
167 void remove()
168 {
169  tip().hide();
170 }
171 
172 } // namespace tip
173 
174 } // namespace gui2
Definition: video.hpp:58
This file contains the window object, this object is a top level container which has the event manage...
GLdouble GLdouble t
Definition: glew.h:1366
#define REGISTER_WINDOW(id)
Registers a window.
Definition: dialog.hpp:43
Helper struct to signal that get_window_builder failed.
Definition: settings.hpp:142
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
virtual const std::string & window_id() const
Inherited from tpopup.
Definition: tip.cpp:115
This file contains the settings handling of the widget library.
void pre_show(twindow &window)
Inherited from tpopup.
Definition: tip.cpp:107
int y
y coordinate.
Definition: point.hpp:34
The popup class shows windows that are shown non-modal.
Definition: popup.hpp:33
ttip()
Definition: tip.cpp:71
static lg::log_domain log_config("config")
void show(CVideo &video, const std::string &window_id, const t_string &message, const tpoint &mouse)
Shows a tip.
Definition: tip.cpp:133
int x
x coordinate.
Definition: point.hpp:31
static ttip & tip()
Definition: tip.cpp:123
tpoint mouse_
The position of the mouse.
Definition: tip.cpp:98
void hide()
Hides the window.
Definition: popup.cpp:57
Holds a 2D point.
Definition: point.hpp:24
The tips of day structure.
Definition: tips.hpp:55
void set_message(const t_string &message)
Definition: tip.cpp:80
void set_mouse(const tpoint &mouse)
Definition: tip.cpp:85
#define ERR_CFG
Definition: tip.cpp:26
t_string message_
The message to show.
Definition: tip.cpp:95
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
static void set_label(twindow &window, const std::string &id, const std::string &label)
Definition: unit_attack.cpp:89
void set_window_id(const std::string &window_id)
Definition: tip.cpp:75
std::string window_id_
The id of the window to use to show the tip.
Definition: tip.cpp:92
GLsizei const GLcharARB ** string
Definition: glew.h:4503