~elementaryart/gnome-applets/sezen

« back to all changes in this revision

Viewing changes to sezen/src/search-entry.vala

  • Committer: Michal Hruby
  • Date: 2010-06-29 22:51:11 UTC
  • Revision ID: michal.mhr@gmail.com-20100629225111-dduvj3v85xwu0eed
WIP

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 Michal Hruby <michal.mhr@gmail.com>
 
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 2 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, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 
17
 *
 
18
 * Authored by Michal Hruby <michal.mhr@gmail.com>
 
19
 *
 
20
 */
 
21
 
 
22
using Gtk;
 
23
 
 
24
namespace Sezen
 
25
{
 
26
  public class SearchEntry: Entry
 
27
  {
 
28
    public bool delayed_clear { get; set; default = false; }
 
29
    public signal void search (string text);
 
30
    public signal void clear ();
 
31
 
 
32
    // FIXME: once we properly support gettext
 
33
    //private string default_text = _("Search...");
 
34
    private string default_text = "Search...";
 
35
    private uint search_timeout;
 
36
 
 
37
    public SearchEntry ()
 
38
    {
 
39
    }
 
40
 
 
41
    construct
 
42
    {
 
43
      this.set_text (default_text);
 
44
      this.set_size_request (225, -1);
 
45
      this.changed.connect (this.queue_search);
 
46
      this.focus_in_event.connect (this.focus_in);
 
47
      this.focus_out_event.connect (this.focus_out);
 
48
 
 
49
      this.icon_press.connect (this.icon_pressed);
 
50
 
 
51
      this.set_icon_from_stock (EntryIconPosition.PRIMARY, STOCK_FIND);
 
52
      this.set_icon_from_stock (EntryIconPosition.SECONDARY, STOCK_CLEAR);
 
53
    }
 
54
 
 
55
    private bool focus_in ()
 
56
    {
 
57
      if (this.get_text () == default_text)
 
58
      {
 
59
        this.set_text ("");
 
60
      }
 
61
 
 
62
      return false;
 
63
    }
 
64
 
 
65
    private bool focus_out ()
 
66
    {
 
67
      if (this.get_text () == "")
 
68
      {
 
69
        this.set_text (this.default_text);
 
70
      }
 
71
 
 
72
      return false;
 
73
    }
 
74
 
 
75
    private void icon_pressed (EntryIconPosition pos, Gdk.Event event)
 
76
    {
 
77
      if (pos == EntryIconPosition.SECONDARY &&
 
78
        this.get_text () != default_text)
 
79
      {
 
80
        this.set_text ("");
 
81
      }
 
82
    }
 
83
 
 
84
    private void queue_search ()
 
85
    {
 
86
      if (this.search_timeout != 0)
 
87
      {
 
88
        GLib.Source.remove (this.search_timeout);
 
89
        this.search_timeout = 0;
 
90
      }
 
91
 
 
92
      unowned string t = this.get_text ();
 
93
      if ((t == default_text || t == "") && !delayed_clear)
 
94
      {
 
95
        clear ();
 
96
      }
 
97
      else
 
98
      {
 
99
        this.search_timeout = Timeout.add (500, this.typing_timeout);
 
100
      }
 
101
    }
 
102
 
 
103
    private bool typing_timeout ()
 
104
    {
 
105
      unowned string term = this.get_text ();
 
106
      if (term != default_text && term != "")
 
107
      {
 
108
        search (term);
 
109
      }
 
110
      else if (delayed_clear)
 
111
      {
 
112
        clear ();
 
113
      }
 
114
 
 
115
      this.search_timeout = 0;
 
116
 
 
117
      return false;
 
118
    }
 
119
  }
 
120
}