gui/dialogs/tip.cpp

Go to the documentation of this file.
00001 /* $Id: tip.cpp 52869 2012-02-03 20:18:33Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2011 - 2012 by Mark de Wever <koraq@xs4all.nl>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #define GETTEXT_DOMAIN "wesnoth-lib"
00017 
00018 #include "gui/dialogs/tip.hpp"
00019 
00020 #include "gui/dialogs/dialog.hpp"
00021 #include "gui/dialogs/popup.hpp"
00022 #include "gui/widgets/settings.hpp"
00023 #include "gui/widgets/window.hpp"
00024 
00025 static lg::log_domain log_config("config");
00026 #define ERR_CFG LOG_STREAM(warn , log_config)
00027 
00028 namespace gui2 {
00029 
00030 /*WIKI
00031  * @page = GUIWindowDefinitionWML
00032  * @order = 2_tip
00033  *
00034  * == Tip float ==
00035  *
00036  * Generic window to show a floating tip window. The class has several
00037  * subclasses using the same format. For example there will be tooltips and
00038  * helptips, both using this class.
00039  *
00040  * @begin{table}{dialog_widgets}
00041  *
00042  * label & & control & m &
00043  *         This text contains the message to show in the tip. $
00044  *
00045  * @end{table}
00046  *
00047  * In the canvas of the windows used in this dialog the following variables are
00048  * defined:
00049  *
00050  * @begin{table}{formula}
00051  *     mouse_x & unsigned &          The x coordinate of the mouse pointer when
00052  *                                   the window was created. $
00053  *     mouse_y & unsigned &          The y coordinate of the mouse pointer when
00054  *                                   the window was created. $
00055  * @end{table}
00056  */
00057 
00058 REGISTER_WINDOW(tooltip_large)
00059 
00060 /**
00061  * Class to show the tips.
00062  *
00063  * At the moment two kinds of tips are known:
00064  * * tooltip
00065  * * helptip
00066  */
00067 class ttip
00068     : public tpopup
00069 {
00070 public:
00071 
00072     ttip()
00073         : tpopup()
00074         , window_id_()
00075         , message_()
00076         , mouse_(tpoint(0, 0))
00077     {
00078     }
00079 
00080     void set_window_id(const std::string& window_id)
00081     {
00082         window_id_ = window_id;
00083     }
00084 
00085     void set_message(const t_string& message)
00086     {
00087         message_ = message;
00088     }
00089 
00090     void set_mouse(const tpoint& mouse)
00091     {
00092         mouse_ = mouse;
00093     }
00094 
00095 private:
00096 
00097     /** The id of the window to use to show the tip. */
00098     std::string window_id_;
00099 
00100     /** The message to show. */
00101     t_string message_;
00102 
00103     /** The position of the mouse. */
00104     tpoint mouse_;
00105 
00106     /** Inherited from tpopup. */
00107     virtual const std::string& window_id() const;
00108 
00109     /** Inherited from tpopup. */
00110     void pre_show(CVideo& video, twindow& window);
00111 
00112 };
00113 
00114 void ttip::pre_show(CVideo& /*video*/, twindow& window)
00115 {
00116     find_widget<tcontrol>(&window, "label", false).set_label(message_);
00117 
00118     window.set_variable("mouse_x", variant(mouse_.x));
00119     window.set_variable("mouse_y", variant(mouse_.y));
00120 }
00121 
00122 const std::string& ttip::window_id() const
00123 {
00124     return window_id_;
00125 }
00126 
00127 namespace tip {
00128 
00129 static ttip& tip()
00130 {
00131     /*
00132      * Allocating a static tip object causes a segmentation fault when Wesnoth
00133      * termines. So instead create an object on the heap and never free it.
00134      */
00135     static ttip *t = new ttip();
00136     return *t;
00137 }
00138 
00139 void show(CVideo& video
00140         , const std::string& window_id
00141         , const t_string& message
00142         , const tpoint& mouse)
00143 {
00144     /*
00145      * For now allow invalid tip names, might turn them to invalid wml messages
00146      * later on.
00147      */
00148     ttip& t = tip();
00149     t.set_window_id(window_id);
00150     t.set_message(message);
00151     t.set_mouse(mouse);
00152     try {
00153         t.show(video);
00154     } catch(twindow_builder_invalid_id&) {
00155         ERR_CFG << "Tip with the requested id '" << window_id
00156                 << "' doesn't exist, fall back to the default.\n";
00157         t.set_window_id("tooltip_large");
00158         try {
00159             t.show(video);
00160         } catch(twindow_builder_invalid_id&) {
00161             ERR_CFG << "Default tooltip doesn't exist, no message shown.\n";
00162         }
00163     }
00164 }
00165 
00166 void remove()
00167 {
00168     tip().hide();
00169 }
00170 
00171 } // namespace tip
00172 
00173 } // namespace gui2
00174 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:58 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs