~lubuntu-software-center-team/lubuntu-software-center/vala-port

« back to all changes in this revision

Viewing changes to src/Widgets/LscEntries.vala

  • Committer: Stephen Smally
  • Date: 2012-03-04 12:59:13 UTC
  • Revision ID: eco.stefi@fastwebnet.it-20120304125913-bk1iutifwoeoyo0i
Worked a lot!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
//  Copyright © 2011 Avi Romanoff, Allen Lowe, Maxwell Barvian
3
 
// 
4
 
//  This program is free software: you can redistribute it and/or modify
5
 
//  it under the terms of the GNU General Public License as published by
6
 
//  the Free Software Foundation, either version 3 of the License, or
7
 
//  (at your option) any later version.
8
 
// 
9
 
//  This program is distributed in the hope that it will be useful,
10
 
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
//  GNU General Public License for more details.
13
 
// 
14
 
//  You should have received a copy of the GNU General Public License
15
 
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 
// 
17
 
// Code from Granite https://launchpad.net/granite
18
 
//
19
 
 
20
 
using Gtk;
21
 
using Gdk;
22
 
 
23
 
namespace Lsc.Widgets {
24
 
 
25
 
    public class HintedEntry : Gtk.Entry {
26
 
 
27
 
        public string hint_string {
28
 
            get {
29
 
                return placeholder_text;
30
 
            }
31
 
            set {
32
 
                placeholder_text = value;
33
 
            }
34
 
        }
35
 
 
36
 
        public HintedEntry (string hint_string) {
37
 
            this.hint_string = hint_string;
38
 
        }
39
 
    }
40
 
 
41
 
    public class SearchBar : HintedEntry {
42
 
 
43
 
        private bool is_searching = true;
44
 
        private uint timeout_id = 0;
45
 
 
46
 
        /**
47
 
         * This value handles how much time (in ms) should pass
48
 
         * after the user stops typing. By default it is set 
49
 
         * to 300 ms.
50
 
         **/
51
 
        public int pause_delay { get; set; default = 300; }
52
 
 
53
 
        /**
54
 
         * text_changed () signal is emitted after a short delay,
55
 
         * which depends on pause_delay.
56
 
         * If you need a synchronous signal without any delay,
57
 
         * use changed () method.
58
 
         **/
59
 
        public signal void text_changed_pause (string text);
60
 
 
61
 
        public SearchBar (string hint_string) {
62
 
        
63
 
            base (hint_string);
64
 
            
65
 
            set_icon_from_stock (EntryIconPosition.PRIMARY, Stock.FIND);
66
 
            
67
 
            // Signals and callbacks
68
 
            changed.connect (manage_icon);
69
 
            changed.connect_after (on_changed);            
70
 
            focus_in_event.connect (on_focus_in);
71
 
            focus_out_event.connect (on_focus_out);
72
 
            icon_press.connect (on_icon_press);
73
 
        }
74
 
 
75
 
        protected new void hint () {
76
 
            is_searching = false;
77
 
            set_icon_from_stock (Gtk.EntryIconPosition.SECONDARY, null);
78
 
        }
79
 
        
80
 
        private bool on_focus_in () {
81
 
            if (!is_searching) {
82
 
                is_searching = false;
83
 
            }
84
 
            
85
 
            return false;
86
 
        }
87
 
 
88
 
        private bool on_focus_out () {
89
 
            if (get_text () == "") {
90
 
                is_searching = false;
91
 
            }
92
 
            
93
 
            return false;
94
 
        }
95
 
 
96
 
        private void manage_icon () {
97
 
            if (text != "")
98
 
                set_icon_from_gicon (EntryIconPosition.SECONDARY, new ThemedIcon.with_default_fallbacks ("edit-clear-symbolic"));
99
 
            else
100
 
                set_icon_from_stock (EntryIconPosition.SECONDARY, null);
101
 
        }
102
 
 
103
 
        private void on_icon_press (EntryIconPosition position) {
104
 
            if (position == EntryIconPosition.SECONDARY) {
105
 
                is_searching = false;
106
 
                text = "";
107
 
                set_icon_from_stock (position, null);
108
 
                is_searching = true;
109
 
            } else {
110
 
                if (!is_focus) {
111
 
                    is_searching = false;
112
 
                }
113
 
            }
114
 
        }
115
 
 
116
 
        private void on_changed () {
117
 
 
118
 
            if (timeout_id > 0)
119
 
                Source.remove (timeout_id);
120
 
 
121
 
            timeout_id = Timeout.add (pause_delay, emit_text_changed);
122
 
 
123
 
        }
124
 
 
125
 
        private bool emit_text_changed () {
126
 
 
127
 
            var terms = get_text ();
128
 
            text_changed_pause (terms); // Emit signal
129
 
 
130
 
            return Source.remove (timeout_id);
131
 
 
132
 
        }
133
 
 
134
 
    }
135
 
 
136
 
}
137