1/*  This file is part of Cawbird, a Gtk+ linux Twitter client forked from Corebird.
2 *  Copyright (C) 2013 Timm Bäder (Corebird)
3 *
4 *  Cawbird 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 *  Cawbird 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 cawbird.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18[GtkTemplate (ui = "/uk/co/ibboard/cawbird/ui/new-list-entry.ui")]
19class NewListEntry : Gtk.ListBoxRow {
20  [GtkChild]
21  private unowned Gtk.Box create_box;
22  [GtkChild]
23  private unowned Gtk.Grid grid;
24  [GtkChild]
25  private unowned Gtk.Label list_name_label;
26  [GtkChild]
27  private unowned Gtk.Entry list_name_entry;
28  [GtkChild]
29  private unowned Gtk.Revealer revealer;
30  [GtkChild]
31  private unowned Gtk.Button create_list_button;
32
33  public signal void create_activated (string list_name);
34
35
36  construct {
37    list_name_entry.buffer.notify["text"].connect (name_text_changed_cb);
38  }
39
40  public void reveal () {
41    revealer.reveal_child = true;
42    this.activatable = false;
43    list_name_entry.grab_focus ();
44  }
45
46  public void unreveal () {
47    revealer.reveal_child = false;
48    this.activatable = true;
49    list_name_entry.text = "";
50  }
51
52  public override void get_preferred_width (out int min, out int nat) {
53    min = nat = 0;
54    int child_min, child_nat;
55    list_name_label.get_preferred_width (out child_min, out child_nat);
56    min += child_min;
57    nat += child_nat;
58    list_name_entry.get_preferred_width (out child_min, out child_nat);
59    min += child_min;
60    nat += child_nat;
61    create_list_button.get_preferred_width (out child_min, out child_nat);
62    // We can wrap when narrow enough, so ignore the create button for min and only include in natural size (because we'll take the space if possible)
63    nat += child_nat;
64  }
65
66  public override void get_preferred_height_for_width (int width, out int min, out int nat) {
67    int child_min, child_nat;
68    create_box.get_preferred_height_for_width (width, out min, out nat);
69    if (revealer.reveal_child) {
70      list_name_entry.get_preferred_height_for_width (width, out child_min, out child_nat);
71      min += child_min;
72      nat += child_nat;
73      if (width < Cawbird.RESPONSIVE_LIMIT) {
74        create_list_button.get_preferred_height_for_width (width, out child_min, out child_nat);
75        min += child_min;
76        nat += child_nat;
77      }
78    }
79  }
80
81  public override void size_allocate(Gtk.Allocation allocation) {
82    if (allocation.width < Cawbird.RESPONSIVE_LIMIT) {
83      grid.child_set(create_list_button, "left-attach", 0);
84      grid.child_set(create_list_button, "top-attach", 1);
85      grid.child_set(create_list_button, "width", 2);
86    }
87    else {
88      grid.child_set(create_list_button, "left-attach", 2);
89      grid.child_set(create_list_button, "top-attach", 0);
90      grid.child_set(create_list_button, "width", 1);
91    }
92    base.size_allocate(allocation);
93  }
94
95  [GtkCallback]
96  private void create_list_button_clicked_cb () {
97    create_activated (list_name_entry.text);
98  }
99
100  private void name_text_changed_cb () {
101    string name = list_name_entry.text;
102
103    create_list_button.sensitive = false;
104
105    if (name.length == 0 || name.char_count () > 25)
106      return;
107
108    if (name.get_char (0).isdigit())
109      return;
110
111
112    create_list_button.sensitive = true;
113  }
114}
115