gui/dialogs/data_manage.cpp

Go to the documentation of this file.
00001 /* $Id: data_manage.cpp 53111 2012-02-18 17:57:20Z boucman $ */
00002 /*
00003    Copyright (C) 2010 - 2012 by Jody Northup
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/data_manage.hpp"
00019 
00020 #include "foreach.hpp"
00021 #include "formula_string_utils.hpp"
00022 #include "game_config.hpp"
00023 #include "game_preferences.hpp"
00024 #include "gettext.hpp"
00025 #include "gui/auxiliary/log.hpp"
00026 #include "gui/dialogs/field.hpp"
00027 #include "gui/dialogs/game_delete.hpp"
00028 #include "gui/dialogs/helper.hpp"
00029 #include "gui/widgets/button.hpp"
00030 #include "gui/widgets/image.hpp"
00031 #include "gui/widgets/label.hpp"
00032 #ifdef GUI2_EXPERIMENTAL_LISTBOX
00033 #include "gui/widgets/list.hpp"
00034 #else
00035 #include "gui/widgets/listbox.hpp"
00036 #endif
00037 #include "gui/widgets/minimap.hpp"
00038 #include "gui/widgets/settings.hpp"
00039 #include "gui/widgets/text_box.hpp"
00040 #include "gui/widgets/window.hpp"
00041 #include "language.hpp"
00042 #include "preferences_display.hpp"
00043 
00044 #include <cctype>
00045 #include <boost/bind.hpp>
00046 
00047 namespace gui2 {
00048 
00049 
00050 REGISTER_DIALOG(data_manage)
00051 
00052 tdata_manage::tdata_manage(const config& cache_config)
00053     : txtFilter_(register_text("txtFilter", true))
00054     , filename_()
00055     , games_()
00056     , cache_config_(cache_config)
00057     , last_words_()
00058 {
00059 }
00060 
00061 void tdata_manage::pre_show(CVideo& /*video*/, twindow& window)
00062 {
00063     assert(txtFilter_);
00064 
00065     ttext_box* filter = find_widget<ttext_box>(
00066             &window, "txtFilter", false, true);
00067     window.keyboard_capture(filter);
00068     filter->set_text_changed_callback(boost::bind(
00069             &tdata_manage::filter_text_changed, this, _1, _2));
00070 
00071     tlistbox& list = find_widget<tlistbox>(&window, "persist_list", false);
00072     window.keyboard_capture(&list);
00073 
00074 #ifdef GUI2_EXPERIMENTAL_LISTBOX
00075     connect_signal_notify_modified(list, boost::bind(
00076                   &tdata_manage::list_item_clicked
00077                 , this
00078                 , boost::ref(window)));
00079 #else
00080     list.set_callback_value_change(
00081             dialog_callback<tdata_manage, &tdata_manage::list_item_clicked>);
00082 #endif
00083 
00084     {
00085         cursor::setter cur(cursor::WAIT);
00086         games_ = savegame::get_saves_list();
00087     }
00088     fill_game_list(window, games_);
00089 
00090     connect_signal_mouse_left_click(
00091             find_widget<tbutton>(&window, "clear", false)
00092             , boost::bind(
00093                   &tdata_manage::delete_button_callback
00094                 , this
00095                 , boost::ref(window)));
00096 }
00097 
00098 void tdata_manage::fill_game_list(twindow& window
00099         , std::vector<savegame::save_info>& games)
00100 {
00101     tlistbox& list = find_widget<tlistbox>(&window, "persist_list", false);
00102     list.clear();
00103 
00104     foreach(const savegame::save_info game, games) {
00105         std::map<std::string, string_map> data;
00106         string_map item;
00107 
00108         item["label"] = game.name();
00109         data.insert(std::make_pair("filename", item));
00110 
00111         item["label"] = game.format_time_summary();
00112         data.insert(std::make_pair("date", item));
00113 
00114         list.add_row(data);
00115     }
00116 }
00117 
00118 void tdata_manage::list_item_clicked(twindow& /*window*/)
00119 {
00120 }
00121 
00122 bool tdata_manage::filter_text_changed(ttext_* textbox, const std::string& text)
00123 {
00124     twindow& window = *textbox->get_window();
00125 
00126     tlistbox& list = find_widget<tlistbox>(&window, "persist_list", false);
00127 
00128     const std::vector<std::string> words = utils::split(text, ' ');
00129 
00130     if (words == last_words_)
00131         return false;
00132     last_words_ = words;
00133 
00134     std::vector<bool> show_items(list.get_item_count(), true);
00135 
00136     if(!text.empty()) {
00137         for(unsigned int i = 0; i < list.get_item_count(); i++) {
00138             tgrid* row = list.get_row_grid(i);
00139 
00140             tgrid::iterator it = row->begin();
00141             tlabel& filename_label =
00142                 find_widget<tlabel>(*it, "filename", false);
00143 
00144             bool found = false;
00145             foreach (const std::string& word, words){
00146                 found = std::search(filename_label.label().str().begin()
00147                         , filename_label.label().str().end()
00148                         , word.begin(), word.end()
00149                         , chars_equal_insensitive)
00150                     != filename_label.label().str().end();
00151 
00152                 if (! found) {
00153                     // one word doesn't match, we don't reach words.end()
00154                     break;
00155                 }
00156             }
00157 
00158             show_items[i] = found;
00159         }
00160     }
00161 
00162     list.set_row_shown(show_items);
00163 
00164     return false;
00165 }
00166 
00167 void tdata_manage::delete_button_callback(twindow& window)
00168 {
00169     tlistbox& list = find_widget<tlistbox>(&window, "persist_list", false);
00170 
00171     const size_t index = size_t(list.get_selected_row());
00172     if(index < games_.size()) {
00173 
00174         // See if we should ask the user for deletion confirmation
00175         if(preferences::ask_delete_saves()) {
00176             if(!gui2::tgame_delete::execute(window.video())) {
00177                 return;
00178             }
00179         }
00180 
00181         // Delete the file
00182         savegame::delete_game(games_[index].name());
00183 
00184         // Remove it from the list of saves
00185         games_.erase(games_.begin() + index);
00186         list.remove_row(index);
00187 
00188     }
00189 }
00190 
00191 } // namespace gui2
 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