1/*
2* Copyright (c) 2011-2013 Yorba Foundation
3*               2018 elementary LLC. (https://elementary.io)
4*
5* This program is free software; you can redistribute it and/or
6* modify it under the terms of the GNU Lesser General Public
7* License as published by the Free Software Foundation; either
8* version 2.1 of the License, or (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13* General Public License for more details.
14*
15* You should have received a copy of the GNU General Public
16* License along with this program; if not, write to the
17* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18* Boston, MA 02110-1301 USA
19*/
20
21public class SearchRowText : SearchRow {
22    private Gtk.ComboBoxText text_context;
23    private Gtk.Entry entry;
24
25    public SearchRowText (SearchRowContainer parent) {
26        Object (parent: parent);
27
28        // Ordering must correspond with SearchConditionText.Context
29        text_context = new Gtk.ComboBoxText ();
30        text_context.append_text (_("contains"));
31        text_context.append_text (_("is exactly"));
32        text_context.append_text (_("starts with"));
33        text_context.append_text (_("ends with"));
34        text_context.append_text (_("does not contain"));
35        text_context.append_text (_("is not set"));
36        text_context.set_active (0);
37        text_context.changed.connect (on_changed);
38
39        entry = new Gtk.Entry ();
40        entry.set_width_chars (25);
41        entry.set_activates_default (true);
42        entry.changed.connect (on_changed);
43
44        add (text_context);
45        add (entry);
46        show_all ();
47    }
48
49    ~SearchRowText () {
50        text_context.changed.disconnect (on_changed);
51        entry.changed.disconnect (on_changed);
52    }
53
54    public override SearchCondition get_search_condition () {
55        SearchCondition.SearchType type = parent.get_search_type ();
56        string text = entry.get_text ();
57        SearchConditionText.Context context = get_text_context ();
58        SearchConditionText c = new SearchConditionText (type, text, context);
59        return c;
60    }
61
62    public override void populate (SearchCondition sc) {
63        SearchConditionText? text = sc as SearchConditionText;
64        assert (text != null);
65        text_context.set_active (text.context);
66        entry.set_text (text.text);
67        on_changed ();
68    }
69
70    public override bool is_complete () {
71        return entry.text.chomp () != "" || get_text_context () == SearchConditionText.Context.IS_NOT_SET;
72    }
73
74    private SearchConditionText.Context get_text_context () {
75        return (SearchConditionText.Context) text_context.get_active ();
76    }
77
78    private void on_changed () {
79        if (get_text_context () == SearchConditionText.Context.IS_NOT_SET) {
80            entry.hide ();
81        } else {
82            entry.show ();
83        }
84
85        parent.changed (parent);
86    }
87}
88