gui/dialogs/mp_connect.cpp

Go to the documentation of this file.
00001 /* $Id: mp_connect.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/mp_connect.hpp"
00019 
00020 #include "foreach.hpp"
00021 #include "game_preferences.hpp"
00022 #include "gui/dialogs/field.hpp"
00023 #include "gui/widgets/button.hpp"
00024 #ifdef GUI2_EXPERIMENTAL_LISTBOX
00025 #include "gui/widgets/list.hpp"
00026 #else
00027 #include "gui/widgets/listbox.hpp"
00028 #endif
00029 #include "gui/widgets/settings.hpp"
00030 #include "video.hpp"
00031 
00032 #include <boost/bind.hpp>
00033 
00034 namespace gui2 {
00035 
00036 namespace {
00037 
00038 /*WIKI
00039  * @page = GUIWindowDefinitionWML
00040  * @order = 2_mp_server_list
00041  *
00042  * == Multiplayer server list ==
00043  *
00044  * This shows the dialog with a list of predefined multiplayer servers.
00045  *
00046  * @begin{table}{dialog_widgets}
00047  *
00048  * server_list & & listbox & m &
00049  *         Listbox with the predefined servers to connect to. $
00050  *
00051  * -name & & control & o &
00052  *         Widget which shows the name of the server. $
00053  *
00054  * -address & & control & m &
00055  *         The address/host_name of the server. $
00056  *
00057  * @end{table}
00058  */
00059 
00060 class tmp_server_list : public tdialog
00061 {
00062 public:
00063     tmp_server_list() :
00064         host_name_()
00065     {}
00066 
00067     const std::string& host_name() const { return host_name_; }
00068 
00069 private:
00070     std::string host_name_;
00071 
00072     /** Inherited from tdialog, implemented by REGISTER_DIALOG. */
00073     virtual const std::string& window_id() const;
00074 
00075     /** Inherited from tdialog. */
00076     void pre_show(CVideo& video, twindow& window);
00077 
00078     /** Inherited from tdialog. */
00079     void post_show(twindow& window);
00080 };
00081 
00082 REGISTER_DIALOG(mp_server_list)
00083 
00084 void tmp_server_list::pre_show(CVideo& /*video*/, twindow& window)
00085 {
00086     tlistbox& list = find_widget<tlistbox>(&window, "server_list", false);
00087 
00088     window.keyboard_capture(&list);
00089 
00090     const std::vector<game_config::server_info>&
00091         pref_servers = preferences::server_list();
00092 
00093     foreach(const game_config::server_info& server, pref_servers) {
00094 
00095         std::map<std::string, string_map> data;
00096         string_map item;
00097 
00098         item["label"] = server.name;
00099         data.insert(std::make_pair("name", item));
00100 
00101         item["label"] = server.address;
00102         data.insert(std::make_pair("address", item));
00103 
00104         list.add_row(data);
00105     }
00106 }
00107 
00108 void tmp_server_list::post_show(twindow& window)
00109 {
00110     if(get_retval() == twindow::OK) {
00111 
00112         const tlistbox& list = find_widget<const tlistbox>(
00113                 &window, "server_list", false);
00114 
00115         const tgrid* row = list.get_row_grid(list.get_selected_row());
00116         assert(row);
00117 
00118         host_name_ = find_widget<const tcontrol>(row, "address", false).label();
00119     }
00120 }
00121 
00122 } // namespace
00123 
00124 /*WIKI
00125  * @page = GUIWindowDefinitionWML
00126  * @order = 2_mp_connect
00127  *
00128  * == Multiplayer connect ==
00129  *
00130  * This shows the dialog to the MP server to connect to.
00131  *
00132  * @begin{table}{dialog_widgets}
00133  *
00134  * start_table & & text_box & m &
00135  *         The name of the server to connect to. $
00136  *
00137  * list & & button & o &
00138  *         Shows a dialog with a list of predefined servers to connect to. $
00139  *
00140  * @end{table}
00141  */
00142 
00143 REGISTER_DIALOG(mp_connect)
00144 
00145 static void show_server_list(
00146           CVideo& video
00147         , twindow& window
00148         , tfield_text* host_name)
00149 {
00150     assert(host_name);
00151 
00152     tmp_server_list dlg;
00153     dlg.show(video);
00154 
00155     if(dlg.get_retval() == twindow::OK) {
00156         host_name->set_widget_value(window, dlg.host_name());
00157     }
00158 }
00159 
00160 tmp_connect::tmp_connect()
00161     : host_name_(register_text("host_name"
00162             , true
00163             , preferences::network_host
00164             , preferences::set_network_host
00165             , true))
00166 {
00167 }
00168 
00169 void tmp_connect::pre_show(CVideo& video, twindow& window)
00170 {
00171     assert(host_name_);
00172 
00173     // Set view list callback button.
00174     if(tbutton* button = find_widget<tbutton>(&window, "list", false, false)) {
00175 
00176         connect_signal_mouse_left_click(*button, boost::bind(
00177                   show_server_list
00178                 , boost::ref(video)
00179                 , boost::ref(window)
00180                 , host_name_));
00181     }
00182 
00183 }
00184 
00185 tdialog* tmp_connect::mp_server_list_for_unit_test()
00186 {
00187     return new tmp_server_list();
00188 }
00189 
00190 } // namespace gui2
00191 
 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