summaryrefslogtreecommitdiff
path: root/src/contacts-type-combo.vala
blob: d9186be89acc225b81fd434a9a5bf780c0d7ea65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
 * Copyright (C) 2011 Alexander Larsson <alexl@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using Gtk;
using Gee;
using Folks;

public class Contacts.TypeCombo : Grid  {
  private unowned TypeSet type_set;
  private ComboBox combo;
  private Entry entry;
  private TreeIter last_active;
  private bool custom_mode;
  private bool in_manual_change;
  public bool modified;

  public signal void changed ();

  public TypeCombo (TypeSet type_set) {
    this.type_set = type_set;

    combo = new ComboBox.with_model (type_set.store);
    combo.set_halign (Align.FILL);
    combo.set_hexpand (true);
    this.add (combo);

    var renderer = new CellRendererText ();
    combo.pack_start (renderer, true);
    combo.set_attributes (renderer,
                          "text", 0);
    combo.set_row_separator_func ( (model, iter) => {
        string? s;
        model.get (iter, 0, out s);
        return s == null;
      });

    entry = new Entry ();
    entry.set_halign (Align.FILL);
    entry.set_hexpand (true);
    // Make the default entry small so we don't unnecessarily
    // expand the labels (it'll be expanded a bit anyway)
    entry.width_chars = 4;

    this.add (entry);

    combo.set_no_show_all (true);
    entry.set_no_show_all (true);

    combo.show ();

    combo.changed.connect (combo_changed);
    entry.focus_out_event.connect (entry_focus_out_event);
    entry.activate.connect (entry_activate);
    entry.key_release_event.connect (entry_key_release);
  }

  private void finish_custom () {
    if (!custom_mode)
      return;

    custom_mode = false;
    var text = entry.get_text ();

    if (text != "") {
      TreeIter iter;
      type_set.add_custom_label (text, out iter);

      last_active = iter;
      combo.set_active_iter (iter);
    } else {
      combo.set_active_iter (last_active);
    }

    combo.show ();
    entry.hide ();
  }

  private void entry_activate () {
    finish_custom ();
  }

  private bool entry_key_release (Gdk.EventKey event) {
    if (event.keyval == Gdk.Key.Escape) {
      entry.set_text ("");
      finish_custom ();
    }
    return true;
  }

  private bool entry_focus_out_event (Gdk.EventFocus event) {
    finish_custom ();
    return false;
  }

  private void combo_changed (ComboBox combo) {
    if (in_manual_change)
      return;

    modified = true;
    TreeIter iter;
    if (combo.get_active_iter (out iter)) {
      if (type_set.is_custom (iter)) {
        custom_mode = true;
        entry.show ();
        entry.grab_focus ();
        combo.hide ();
      } else {
        last_active = iter;
        this.changed ();
      }
    }
  }

  private void set_from_iter (TreeIter iter) {
    in_manual_change = true;
    last_active = iter;
    combo.set_active_iter (iter);
    in_manual_change = false;
    modified = false;
  }

  public void set_active (AbstractFieldDetails details) {
    TreeIter iter;
    type_set.lookup_type (details, out iter);
    set_from_iter (iter);
  }

  public void set_to (string type) {
    TreeIter iter;
    type_set.lookup_type_by_string (type, out iter);
    set_from_iter (iter);
  }

  public void update_details (AbstractFieldDetails details) {
    TreeIter iter;
    combo.get_active_iter (out iter);
    type_set.update_details (details, iter);
  }
}