The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
filter.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /**
16  * @file
17  * Define the common filters for the @ref gui2::tpane class.
18  */
19 
20 #ifndef GUI_AUXILIARY_FILTER_HPP_INCLUDED
21 #define GUI_AUXILIARY_FILTER_HPP_INCLUDED
22 
23 #include "global.hpp"
24 #include "gui/widgets/text_box.hpp"
25 #include "util.hpp"
28 
29 namespace gui2
30 {
31 
32 template <class T>
33 inline bool sort(const tpane::titem& lhs,
34  const tpane::titem& rhs,
35  const std::string& tag,
36  const bool ascending)
37 {
38  if(ascending) {
39  return lexical_cast<T>(lhs.tags.at(tag))
40  < lexical_cast<T>(rhs.tags.at(tag));
41  } else {
42  return lexical_cast<T>(lhs.tags.at(tag))
43  > lexical_cast<T>(rhs.tags.at(tag));
44  }
45 }
46 
47 /**
48  * A filter testing whether a search string is used in a text.
49  *
50  * The comparison is a lower-case comparison.
51  *
52  * The function is expected to be used as a functor for
53  * @ref gui2::tpane::filter().
54  *
55  * @param item The pane item to search in.
56  * @param tag The tag of the field containing the text to
57  * search in. @note This text field should
58  * already contain lower-cased text only.
59  * @param text_box A text box object whose text is the search
60  * string. The text in the text box will be
61  * converted to lower-cased text before the
62  * comparison is done.
63  *
64  * @returns Whether or not the comparison found a match.
65  */
66 inline bool contains(const tpane::titem& item,
67  const std::string& tag,
68  const ttext_box& text_box)
69 {
70  return item.tags.at(tag).find(utf8::lowercase(text_box.text()))
71  != std::string::npos;
72 }
73 
74 } // namespace gui2
75 
76 #endif
bool sort(const tpane::titem &lhs, const tpane::titem &rhs, const std::string &tag, const bool ascending)
Definition: filter.hpp:33
utf8::string lowercase(const utf8::string &s)
Returns a lowercased version of the string.
Definition: unicode.cpp:53
Class for a single line text area.
Definition: text_box.hpp:118
To lexical_cast(From value)
Lexical cast converts one type to another.
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
bool contains(const tpane::titem &item, const std::string &tag, const ttext_box &text_box)
A filter testing whether a search string is used in a text.
Definition: filter.hpp:66
Templates and utility-routines for strings and numbers.
std::map< std::string, std::string > tags
Definition: pane.hpp:49
const std::string & text() const
Definition: text.hpp:81
GLsizei const GLcharARB ** string
Definition: glew.h:4503