~gwibber-committers/gwibber/libgwibber-0.0

« back to all changes in this revision

Viewing changes to libgwibber-gtk/entry.vala

  • Committer: Ken VanDine
  • Date: 2010-06-04 02:12:26 UTC
  • Revision ID: ken.vandine@canonical.com-20100604021226-l9195zr1w8x0hpa3
initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 Neil Jagdish Patel
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License
 
6
 * version 3.0 as published by the Free Software Foundation.
 
7
 *
 
8
 * This library is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License version 3.0 for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public
 
14
 * License along with this library. If not, see
 
15
 * <http://www.gnu.org/licenses/>.
 
16
 *
 
17
 * Authored by Neil Jagdish Patel <neil.patel@canonical.com>
 
18
 */
 
19
 
 
20
namespace Gwibber
 
21
{
 
22
    public class Entry : Gtk.VBox
 
23
    {
 
24
        private Service service;
 
25
 
 
26
        private InputTextView text_view;
 
27
 
 
28
        public Entry ()
 
29
        {
 
30
            Object ();
 
31
        }
 
32
 
 
33
        construct
 
34
        {
 
35
            this.service = new Service ();
 
36
 
 
37
            this.text_view = new InputTextView (this.service);
 
38
            this.add (this.text_view);
 
39
            this.text_view.show ();
 
40
        }
 
41
    }
 
42
 
 
43
    public class InputTextView : Gtk.TextView
 
44
    {
 
45
        public Service service { get; construct; }
 
46
 
 
47
        private Gdk.Color base_color;
 
48
        private Gdk.Color error_color;
 
49
 
 
50
        bool last_was_shortened = false;
 
51
 
 
52
        public InputTextView (Service service)
 
53
        {
 
54
            Object (service:service,
 
55
                    border_width:10,
 
56
                    accepts_tab:true,
 
57
                    editable:true,
 
58
                    cursor_visible:true,
 
59
                    wrap_mode:Gtk.WrapMode.WORD_CHAR,
 
60
                    left_margin:2,
 
61
                    right_margin:2,
 
62
                    pixels_above_lines:2,
 
63
                    pixels_below_lines:2);
 
64
        }
 
65
 
 
66
        construct
 
67
        {
 
68
            this.base_color = this.get_style ().base[Gtk.StateType.NORMAL];
 
69
            Gdk.Color.parse ("indianred", out this.error_color);
 
70
 
 
71
            this.get_buffer ().changed.connect (this.on_text_changed);
 
72
            this.get_buffer ().insert_text.connect (this.on_text_inserted);
 
73
 
 
74
            //this.set_sensitive (this.service.is_connected ());
 
75
        }
 
76
 
 
77
        private void on_text_changed ()
 
78
        {
 
79
            var chars = this.get_buffer ().get_char_count ();
 
80
 
 
81
            this.modify_base (Gtk.StateType.NORMAL,
 
82
                        chars > 140 ? this.error_color : this.base_color);
 
83
        }
 
84
 
 
85
        private void on_text_inserted (Gtk.TextIter iter, string text, int len)
 
86
        {
 
87
            if (this.last_was_shortened == false
 
88
                && len > 30
 
89
                && text != null
 
90
                && text[0:4] == "http")
 
91
            {
 
92
                var buf = this.get_buffer ();
 
93
                Signal.stop_emission_by_name (buf, "insert-text") ;
 
94
                var shrt = this.service.shorten (text);
 
95
                this.last_was_shortened = true;
 
96
                buf.insert (iter, shrt, -1);
 
97
            }
 
98
            else
 
99
            {
 
100
                this.last_was_shortened = false;
 
101
            }
 
102
        }
 
103
    }
 
104
}
 
105