gui/dialogs/dialog.cpp

Go to the documentation of this file.
00001 /* $Id: dialog.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/dialog.hpp"
00019 
00020 #include "foreach.hpp"
00021 #include "gui/dialogs/field.hpp"
00022 #include "gui/widgets/integer_selector.hpp"
00023 #include "video.hpp"
00024 
00025 namespace gui2 {
00026 
00027 tdialog::~tdialog()
00028 {
00029     foreach(tfield_* field, fields_) {
00030         delete field;
00031     }
00032 }
00033 
00034 bool tdialog::show(CVideo& video, const unsigned auto_close_time)
00035 {
00036     if(video.faked()) {
00037         return false;
00038     }
00039 
00040     std::auto_ptr<twindow> window(build_window(video));
00041     assert(window.get());
00042 
00043     post_build(video, *window);
00044 
00045     window->set_owner(this);
00046 
00047     init_fields(*window);
00048 
00049     pre_show(video, *window);
00050 
00051     retval_ = window->show(restore_, auto_close_time);
00052 
00053     /*
00054      * It can happen that when two clicks follow eachother fast that the event
00055      * handling code in events.cpp generates a DOUBLE_CLICK_EVENT. For some
00056      * reason it can happen that this event gets pushed in the queue when the
00057      * window is shown, but processed after the window is closed. This causes
00058      * the next window to get this pending event.
00059      *
00060      * This caused a bug where double clicking in the campaign selection dialog
00061      * directly selected a difficulty level and started the campaign. In order
00062      * to avoid that problem, filter all pending DOUBLE_CLICK_EVENT events after
00063      * the window is closed.
00064      */
00065     events::discard(SDL_EVENTMASK(DOUBLE_CLICK_EVENT));
00066 
00067     finalize_fields(*window, (retval_ ==  twindow::OK || always_save_fields_));
00068 
00069     post_show(*window);
00070 
00071     return retval_ == twindow::OK;
00072 }
00073 
00074 tfield_bool* tdialog::register_bool(
00075           const std::string& id
00076         , const bool mandatory
00077         , bool (*callback_load_value) ()
00078         , void (*callback_save_value) (const bool value)
00079         , void (*callback_change) (twidget* widget))
00080 {
00081     tfield_bool* field =  new tfield_bool(
00082               id
00083             , mandatory
00084             , callback_load_value
00085             , callback_save_value, callback_change);
00086 
00087     fields_.push_back(field);
00088     return field;
00089 }
00090 
00091 tfield_bool* tdialog::register_bool(const std::string& id
00092         , const bool mandatory
00093         , bool& linked_variable
00094         , void (*callback_change) (twidget* widget))
00095 {
00096     tfield_bool* field =  new tfield_bool(
00097               id
00098             , mandatory
00099             , linked_variable
00100             , callback_change);
00101 
00102     fields_.push_back(field);
00103     return field;
00104 }
00105 
00106 tfield_integer* tdialog::register_integer(
00107           const std::string& id
00108         , const bool mandatory
00109         , int (*callback_load_value) ()
00110         , void (*callback_save_value) (const int value))
00111 {
00112     tfield_integer* field =  new tfield_integer(
00113               id
00114             , mandatory
00115             , callback_load_value
00116             , callback_save_value);
00117 
00118     fields_.push_back(field);
00119     return field;
00120 }
00121 
00122 tfield_integer* tdialog::register_integer(const std::string& id
00123         , const bool mandatory
00124         , int& linked_variable)
00125 {
00126     tfield_integer* field =  new tfield_integer(
00127               id
00128             , mandatory
00129             , linked_variable);
00130 
00131     fields_.push_back(field);
00132     return field;
00133 }
00134 
00135 tfield_text* tdialog::register_text(
00136           const std::string& id
00137         , const bool mandatory
00138         , std::string (*callback_load_value) ()
00139         , void (*callback_save_value) (const std::string& value)
00140         , const bool capture_focus)
00141 {
00142     tfield_text* field =  new tfield_text(
00143               id
00144             , mandatory
00145             , callback_load_value
00146             , callback_save_value);
00147 
00148     if(capture_focus) {
00149         focus_ = id;
00150     }
00151 
00152     fields_.push_back(field);
00153     return field;
00154 }
00155 
00156 tfield_text* tdialog::register_text(const std::string& id
00157         , const bool mandatory
00158         , std::string& linked_variable
00159         , const bool capture_focus)
00160 {
00161     tfield_text* field =  new tfield_text(
00162               id
00163             , mandatory
00164             , linked_variable);
00165 
00166     if(capture_focus) {
00167         focus_ = id;
00168     }
00169 
00170     fields_.push_back(field);
00171     return field;
00172 }
00173 
00174 tfield_label* tdialog::register_label(const std::string& id
00175         , const bool mandatory
00176         , const std::string& text
00177         , const bool use_markup)
00178 {
00179     tfield_label* field =  new tfield_label(
00180               id
00181             , mandatory
00182             , text
00183             , use_markup);
00184 
00185     fields_.push_back(field);
00186     return field;
00187 }
00188 
00189 twindow* tdialog::build_window(CVideo& video) const
00190 {
00191     return build(video, window_id());
00192 }
00193 
00194 void tdialog::init_fields(twindow& window)
00195 {
00196     foreach(tfield_* field, fields_) {
00197         field->attach_to_window(window);
00198         field->widget_init(window);
00199     }
00200 
00201     if(!focus_.empty()) {
00202         if(twidget* widget = window.find(focus_, false)) {
00203             window.keyboard_capture(widget);
00204         }
00205     }
00206 }
00207 
00208 void tdialog::finalize_fields(twindow& window, const bool save_fields)
00209 {
00210     foreach(tfield_* field, fields_) {
00211         if(save_fields) {
00212             field->widget_finalize(window);
00213         }
00214         field->detach_from_window();
00215     }
00216 }
00217 
00218 } // namespace gui2
00219 
00220 
00221 /*WIKI
00222  * @page = GUIWindowDefinitionWML
00223  * @order = 1
00224  *
00225  * {{Autogenerated}}
00226  *
00227  * = Window definition =
00228  *
00229  * The window definition define how the windows shown in the dialog look.
00230  */
00231 
00232 /*WIKI
00233  * @page = GUIWindowDefinitionWML
00234  * @order = ZZZZZZ_footer
00235  *
00236  * [[Category: WML Reference]]
00237  * [[Category: GUI WML Reference]]
00238  */
00239 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:42 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs