~codygarver/switchboard/fix-1031195

« back to all changes in this revision

Viewing changes to Switchboard/ElementaryEntry.vala

  • Committer: Avi Romanoff
  • Date: 2011-06-23 05:26:15 UTC
  • Revision ID: avi@elementaryos.org-20110623052615-vd2nmnbcitrjrnml
Make consts all caps, move source into it's own directory, utilize constants more, bump version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***
 
2
BEGIN LICENSE
 
3
Copyright (C) 2011 Avi Romanoff <aviromanoff@gmail.com>
 
4
This program is free software: you can redistribute it and/or modify it 
 
5
under the terms of the GNU Lesser General Public License version 2.1, as published 
 
6
by the Free Software Foundation.
 
7
 
 
8
This program is distributed in the hope that it will be useful, but 
 
9
WITHOUT ANY WARRANTY; without even the implied warranties of 
 
10
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
11
PURPOSE.\  See the GNU General Public License for more details.
 
12
 
 
13
You should have received a copy of the GNU General Public License along 
 
14
with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
END LICENSE
 
16
***/
 
17
 
 
18
namespace ElementaryWidgets {
 
19
 
 
20
 
 
21
public class ElementaryEntry : Gtk.Entry {
 
22
 
 
23
    public string hint_string;
 
24
 
 
25
    public ElementaryEntry (string hint_string) {
 
26
    
 
27
        this.hint_string = hint_string;
 
28
        
 
29
        this.hint ();
 
30
        this.focus_in_event.connect (on_focus_in);
 
31
        this.focus_out_event.connect (on_focus_out);
 
32
    
 
33
    }
 
34
 
 
35
    private bool on_focus_in () {
 
36
    
 
37
        if (get_text () == "") {
 
38
            unhint ();
 
39
        }
 
40
        return false;
 
41
    
 
42
    }
 
43
    
 
44
    private bool on_focus_out () {
 
45
    
 
46
        if (get_text () == "") {
 
47
            hint ();
 
48
        }
 
49
        return false;
 
50
    
 
51
    }
 
52
    
 
53
    protected void hint () {
 
54
 
 
55
        this.text = this.hint_string;
 
56
        grey_out ();
 
57
    
 
58
    }
 
59
 
 
60
    protected void unhint () {
 
61
    
 
62
        this.text = "";
 
63
        reset_font ();
 
64
    
 
65
    }
 
66
    
 
67
    
 
68
    private void grey_out () {
 
69
    
 
70
        var color = Gdk.Color ();
 
71
        Gdk.Color.parse ("#999", out color);
 
72
        this.modify_text (Gtk.StateType.NORMAL, color);
 
73
        this.modify_font (Pango.FontDescription.from_string ("italic"));
 
74
    
 
75
    }
 
76
    
 
77
    private void reset_font () {
 
78
    
 
79
        var color = Gdk.Color ();
 
80
        Gdk.Color.parse ("#444", out color);
 
81
        this.modify_text (Gtk.StateType.NORMAL, color);
 
82
        this.modify_font (Pango.FontDescription.from_string ("normal"));
 
83
    
 
84
    }
 
85
    
 
86
    protected new string get_text () {
 
87
    
 
88
        text = this.text;
 
89
        if (text == this.hint_string) {
 
90
            return "";
 
91
        }
 
92
        else {
 
93
            return text;
 
94
        }
 
95
    
 
96
    }
 
97
}
 
98
 
 
99
public class ElementarySearchEntry : ElementaryEntry {
 
100
 
 
101
    bool is_searching;
 
102
 
 
103
    public ElementarySearchEntry (string hint_string) {
 
104
    
 
105
        base(hint_string);
 
106
        this.set_icon_from_stock(Gtk.EntryIconPosition.PRIMARY, "gtk-find");
 
107
        this.changed.connect (manage_icon);
 
108
        this.focus_in_event.connect (on_focus_in);
 
109
        this.focus_out_event.connect (on_focus_out);
 
110
        this.icon_press.connect (icon_pressed);
 
111
        setup_clear_icon ();
 
112
        this.is_searching = true;
 
113
        
 
114
    }
 
115
 
 
116
    private void setup_clear_icon () {
 
117
    
 
118
        var stock_item = Gtk.StockItem ();
 
119
        stock_item.stock_id = "edit-clear-symbolic";
 
120
        stock_item.label = null;
 
121
        stock_item.modifier = 0;
 
122
        stock_item.keyval = 0;
 
123
        stock_item.translation_domain = Gtk.Stock.CLEAR;
 
124
        var factory = new Gtk.IconFactory ();
 
125
        var icon_set = new Gtk.IconSet ();
 
126
        var icon_source = new Gtk.IconSource ();
 
127
        icon_source.set_icon_name (Gtk.Stock.CLEAR);
 
128
        icon_set.add_source (icon_source);
 
129
        icon_source.set_icon_name ("edit-clear-symbolic");
 
130
        icon_set.add_source (icon_source);
 
131
        factory.add ("edit-clear-symbolic", icon_set);
 
132
        Gtk.Stock.add ({stock_item});
 
133
        factory.add_default ();
 
134
        
 
135
    }
 
136
 
 
137
    private new void hint () {
 
138
    
 
139
        this.is_searching = false;
 
140
        this.set_icon_from_stock (Gtk.EntryIconPosition.SECONDARY, null);
 
141
        base.hint ();
 
142
        
 
143
    }
 
144
    
 
145
    private new bool on_focus_in () {
 
146
    
 
147
        if (!this.is_searching) {
 
148
            this.unhint ();
 
149
            this.is_searching = false;
 
150
        }
 
151
        return false;
 
152
    
 
153
    }
 
154
 
 
155
    private new bool on_focus_out () {
 
156
        
 
157
        if (this.get_text() == "") {
 
158
            this.hint ();
 
159
            this.is_searching = false;
 
160
        }
 
161
        return false;
 
162
    
 
163
    }
 
164
 
 
165
    private void manage_icon () {
 
166
 
 
167
        if (this.text != "") {
 
168
            this.set_icon_from_stock (Gtk.EntryIconPosition.SECONDARY, "edit-clear-symbolic");
 
169
        }
 
170
        else {
 
171
            this.set_icon_from_stock (Gtk.EntryIconPosition.SECONDARY, null);
 
172
        }
 
173
        
 
174
    }
 
175
 
 
176
    private void icon_pressed (Gtk.EntryIconPosition icon_position) {
 
177
    
 
178
        if (icon_position == Gtk.EntryIconPosition.SECONDARY) {
 
179
            this.is_searching = false;
 
180
            this.text = "";
 
181
            this.set_icon_from_stock(Gtk.EntryIconPosition.SECONDARY, null);
 
182
            this.is_searching = true;
 
183
        }
 
184
        else {
 
185
            if (!this.is_focus) {
 
186
                this.is_searching = false;
 
187
                this.hint ();
 
188
            }
 
189
        }
 
190
        
 
191
    }
 
192
}
 
193
 
 
194
 
 
195
}