gui/dialogs/message.cpp

Go to the documentation of this file.
00001 /* $Id: message.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2008 - 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/message.hpp"
00019 
00020 #include "foreach.hpp"
00021 #include "gettext.hpp"
00022 #include "gui/widgets/button.hpp"
00023 #include "gui/widgets/image.hpp"
00024 #include "gui/widgets/label.hpp"
00025 #include "gui/widgets/settings.hpp"
00026 #include "gui/widgets/window.hpp"
00027 #include "log.hpp"
00028 
00029 namespace gui2 {
00030 
00031 REGISTER_DIALOG(message)
00032 
00033 /**
00034  * Helper to implement private functions without modifying the header.
00035  *
00036  * The class is a helper to avoid recompilation and only has static
00037  * functions.
00038  */
00039 struct tmessage_implementation
00040 {
00041     /**
00042      * Initialiazes a button.
00043      *
00044      * @param window              The window that contains the button.
00045      * @param button_status       The button status to modify.
00046      * @param id                  The id of the button.
00047      */
00048     static void
00049     init_button(twindow& window, tmessage::tbutton_status& button_status,
00050             const std::string& id)
00051     {
00052         button_status.button = find_widget<tbutton>(
00053                 &window, id, false, true);
00054         button_status.button->set_visible(button_status.visible);
00055 
00056         if(!button_status.caption.empty()) {
00057             button_status.button->set_label(button_status.caption);
00058         }
00059 
00060         if(button_status.retval != twindow::NONE) {
00061             button_status.button->set_retval(button_status.retval);
00062         }
00063     }
00064 };
00065 
00066 void tmessage::pre_show(CVideo& /*video*/, twindow& window)
00067 {
00068     // ***** Validate the required buttons ***** ***** ***** *****
00069     tmessage_implementation::
00070 			init_button(window, buttons_[left_1], "left_side");
00071     tmessage_implementation::
00072 			init_button(window, buttons_[cancel], "cancel");
00073     tmessage_implementation::
00074 			init_button(window, buttons_[ok] ,"ok");
00075     tmessage_implementation::
00076 			init_button(window, buttons_[right_1], "right_side");
00077 
00078     // ***** ***** ***** ***** Set up the widgets ***** ***** ***** *****
00079     if(!title_.empty()) {
00080         find_widget<tlabel>(&window, "title", false).set_label(title_);
00081     }
00082 
00083     if(!image_.empty()) {
00084         find_widget<timage>(&window, "image", false).set_label(image_);
00085     }
00086 
00087     tcontrol& label = find_widget<tcontrol>(&window, "label", false);
00088     label.set_label(message_);
00089 
00090     // The label might not always be a scroll_label but the capturing
00091     // shouldn't hurt.
00092     window.keyboard_capture(&label);
00093 
00094     // Override the user value, to make sure it's set properly.
00095     window.set_click_dismiss(auto_close_);
00096 }
00097 
00098 void tmessage::post_show(twindow& /*window*/)
00099 {
00100     foreach(tbutton_status& button_status, buttons_) {
00101         button_status.button = NULL;
00102     }
00103 }
00104 
00105 void tmessage::set_button_caption(const tbutton_id button,
00106         const std::string& caption)
00107 {
00108     buttons_[button].caption = caption;
00109     if(buttons_[button].button) {
00110         buttons_[button].button->set_label(caption);
00111     }
00112 }
00113 
00114 void tmessage::set_button_visible(const tbutton_id button,
00115         const twidget::tvisible visible)
00116 {
00117     buttons_[button].visible = visible;
00118     if(buttons_[button].button) {
00119         buttons_[button].button->set_visible(visible);
00120     }
00121 }
00122 
00123 void tmessage::set_button_retval(const tbutton_id button,
00124         const int retval)
00125 {
00126     buttons_[button].retval = retval;
00127     if(buttons_[button].button) {
00128         buttons_[button].button->set_retval(retval);
00129     }
00130 }
00131 
00132 tmessage::tbutton_status::tbutton_status()
00133     : button(NULL)
00134     , caption()
00135     , visible(twidget::INVISIBLE)
00136     , retval(twindow::NONE)
00137 {
00138 }
00139 
00140 void show_message(CVideo& video, const std::string& title,
00141     const std::string& message, const std::string& button_caption,
00142     const bool auto_close)
00143 {
00144     tmessage dlg(title, message, auto_close);
00145     dlg.set_button_caption(tmessage::ok, button_caption);
00146     dlg.show(video);
00147 }
00148 
00149 int show_message(CVideo& video, const std::string& title,
00150     const std::string& message, const tmessage::tbutton_style button_style,
00151     bool /*message_use_markup*/,
00152     bool /*message_title_mode*/)
00153 {
00154     /** @todo implement the markup mode. */
00155     tmessage dlg(title, message, button_style == tmessage::auto_close);
00156 
00157     switch(button_style) {
00158         case tmessage::auto_close :
00159             break;
00160         case tmessage::ok_button :
00161             dlg.set_button_visible(tmessage::ok, twidget::VISIBLE);
00162             dlg.set_button_caption(tmessage::ok, _("OK"));
00163             break;
00164         case tmessage::close_button :
00165             dlg.set_button_visible(tmessage::ok, twidget::VISIBLE);
00166             break;
00167         case tmessage::ok_cancel_buttons :
00168             dlg.set_button_visible(tmessage::ok, twidget::VISIBLE);
00169             dlg.set_button_caption(tmessage::ok, _("OK"));
00170             /* FALL DOWN */
00171         case tmessage::cancel_button :
00172             dlg.set_button_visible(tmessage::cancel, twidget::VISIBLE);
00173             break;
00174         case tmessage::yes_no_buttons :
00175             dlg.set_button_visible(tmessage::ok, twidget::VISIBLE);
00176             dlg.set_button_caption(tmessage::ok, _("Yes"));
00177             dlg.set_button_visible(tmessage::cancel, twidget::VISIBLE);
00178             dlg.set_button_caption(tmessage::cancel, _("No"));
00179             break;
00180     }
00181 
00182     dlg.show(video);
00183     return dlg.get_retval();
00184 }
00185 
00186 void show_error_message(CVideo& video, const std::string& message,
00187     bool message_use_markup)
00188 {
00189     LOG_STREAM(err, lg::general) << message << '\n';
00190     show_message(video, _("Error"), message,
00191             tmessage::ok_button, message_use_markup);
00192 }
00193 
00194 } // namespace gui2
00195 
 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