The Battle for Wesnoth  1.13.10+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 - 2017 by Mark de Wever <koraq@xs4all.nl>
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::pane class.
18  */
19 
20 #pragma once
21 
22 #include "gui/widgets/text_box.hpp"
23 #include "lexical_cast.hpp"
26 
27 namespace gui2
28 {
29 
30 template <class T>
31 inline bool sort(const pane::item& lhs,
32  const pane::item& rhs,
33  const std::string& tag,
34  const bool ascending)
35 {
36  if(ascending) {
37  return lexical_cast<T>(lhs.tags.at(tag))
38  < lexical_cast<T>(rhs.tags.at(tag));
39  } else {
40  return lexical_cast<T>(lhs.tags.at(tag))
41  > lexical_cast<T>(rhs.tags.at(tag));
42  }
43 }
44 
45 /**
46  * A filter testing whether a search string is used in a text.
47  *
48  * The comparison is a lower-case comparison.
49  *
50  * The function is expected to be used as a functor for
51  * @ref gui2::pane::filter().
52  *
53  * @param item The pane item to search in.
54  * @param tag The tag of the field containing the text to
55  * search in. @note This text field should
56  * already contain lower-cased text only.
57  * @param text_box A text box object whose text is the search
58  * string. The text in the text box will be
59  * converted to lower-cased text before the
60  * comparison is done.
61  *
62  * @returns Whether or not the comparison found a match.
63  */
64 inline bool contains(const pane::item& item,
65  const std::string& tag,
66  const text_box& text_box)
67 {
68  return item.tags.at(tag).find(utf8::lowercase(text_box.text()))
69  != std::string::npos;
70 }
71 
72 } // namespace gui2
std::vector< char_t > string
std::map< std::string, std::string > tags
Definition: pane.hpp:46
bool contains(const pane::item &item, const std::string &tag, const text_box &text_box)
A filter testing whether a search string is used in a text.
Definition: filter.hpp:64
New lexcical_cast header.
utf8::string lowercase(const utf8::string &s)
Returns a lowercased version of the string.
Definition: unicode.cpp:51
To lexical_cast(From value)
Lexical cast converts one type to another.
Class for a single line text area.
Definition: text_box.hpp:121
Generic file dialog.
Definition: field-fwd.hpp:22
const std::string & text() const
bool sort(const pane::item &lhs, const pane::item &rhs, const std::string &tag, const bool ascending)
Definition: filter.hpp:31