Use a enum for the columns of the ListStore
[moonshot-ui.git] / src / moonshot-window.vala
1 using Gtk;
2
3 class MainWindow : Window
4 {
5
6     private Entry search_entry;
7     private TreeView identities_list;
8
9     private enum Columns
10     {
11         NAME_COL,
12         N_COLUMNS
13     }
14
15     public MainWindow()
16     {
17         this.title = "Moonshoot";
18         this.position = WindowPosition.CENTER;
19         set_default_size (400, 300);
20
21         build_ui();
22         connect_signals();
23     }
24
25     private void search_entry_icon_press_cb ()
26     {
27         print ("Search entry icon pressed\n");
28     }
29
30     private void search_entry_text_changed_cb ()
31     {
32         var has_text = this.search_entry.get_text_length () > 0;
33         this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, has_text);
34     }
35
36     private void setup_identities_list ()
37     {
38         var listmodel = new ListStore (Columns.N_COLUMNS, typeof (string));
39         this.identities_list.set_model (listmodel);
40
41         var cell = new CellRendererText ();
42         cell.set ("foreground_set", true);
43
44         this.identities_list.insert_column_with_attributes (-1, "Identities", cell,
45                                                             "text", Columns.NAME_COL);
46     }
47
48     private void add_identity_cb ()
49     {
50         ListStore listmodel;
51         TreeIter iter;
52
53         listmodel = (ListStore) this.identities_list.get_model ();
54
55         listmodel.append (out iter);
56         listmodel.set (iter,
57                        Columns.NAME_COL, "Identity 1");
58     }
59
60     private void remove_identity_cb ()
61     {
62         TreeModel model;
63         TreeIter iter;
64
65         var selection = identities_list.get_selection ();
66
67         if (selection.get_selected (out model, out iter))
68         {
69             ((ListStore) model).remove (iter);
70         }
71     }
72
73     private void label_make_bold (Label label)
74     {
75         var font_desc = new Pango.FontDescription ();
76
77         font_desc.set_weight (Pango.Weight.BOLD);
78
79         /* This will only affect the weight of the font, the rest is
80          * from the current state of the widget, which comes from the
81          * theme or user prefs, since the font desc only has the
82          * weight flag turned on.
83          */
84         label.modify_font (font_desc);
85     }
86
87     private void build_ui()
88     {
89         var toolbar = new Toolbar ();
90         var open_button = new ToolButton (null, "Open"); //.from_stock (Stock.OPEN);
91         open_button.is_important = true;
92         toolbar.add (open_button);
93         //open_button.clicked.connect (on_open_clicked);
94
95         this.search_entry = new Entry();
96         this.search_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY, "system-search");
97         this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, false);
98         this.search_entry.set_icon_tooltip_text (EntryIconPosition.SECONDARY,
99                                                  "Search identity or service");
100         this.search_entry.icon_press.connect (search_entry_icon_press_cb);
101         this.search_entry.notify["text"].connect (search_entry_text_changed_cb);
102
103         this.identities_list = new TreeView ();
104         this.identities_list.set_headers_visible (false);
105         setup_identities_list ();
106
107         var scroll = new ScrolledWindow (null, null);
108         scroll.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
109         scroll.set_shadow_type (ShadowType.IN);
110         scroll.add (this.identities_list);
111
112         var button_add = new ToolButton (null, null);
113         button_add.set_icon_name ("gtk-add");
114         button_add.clicked.connect (add_identity_cb);
115         var button_remove = new ToolButton (null, null);
116         button_remove.set_icon_name ("gtk-remove");
117         button_remove.clicked.connect (remove_identity_cb);
118         var button_toolbar = new Toolbar ();
119         button_toolbar.insert (button_add, 0);
120         button_toolbar.insert (button_remove, 1);
121
122         var vbox_left = new VBox (false, 0);
123         vbox_left.pack_start (search_entry, false, false, 6);
124         vbox_left.pack_start (scroll, true, true, 0);
125         vbox_left.pack_start (button_toolbar, false, false, 0);
126
127         var login_vbox_title = new Label ("Login: ");
128         label_make_bold (login_vbox_title);
129         login_vbox_title.set_alignment (0, 0);
130         var username_label = new Label ("Username:");
131         var username_entry = new Entry ();
132         var password_label = new Label ("Password:");
133         var password_entry = new Entry ();
134         var remember_checkbutton = new CheckButton.with_label ("Remember password");
135         var login_table = new Table (3, 3, false);
136         login_table.set_col_spacings (6);
137         login_table.attach_defaults (username_label, 0, 1, 0, 1);
138         login_table.attach_defaults (username_entry, 1, 2, 0, 1);
139         login_table.attach_defaults (password_label, 0, 1, 1, 2);
140         login_table.attach_defaults (password_entry, 1, 2, 1, 2);
141         login_table.attach_defaults (remember_checkbutton,  0, 2, 2, 3);
142         var login_vbox_alignment = new Alignment (0, 0, 0, 0);
143         login_vbox_alignment.set_padding (0, 0, 12, 0);
144         login_vbox_alignment.add (login_table);
145         var login_vbox = new VBox (false, 6);
146         login_vbox.pack_start (login_vbox_title, false, true, 0);
147         login_vbox.pack_start (login_vbox_alignment, false, true, 0);
148
149         var services_vbox_title = new Label ("Services:");
150         label_make_bold (services_vbox_title);
151         services_vbox_title.set_alignment (0, 0);
152         var email_label = new Label ("Email");
153         var email_remove_button = new Button.from_stock (Stock.REMOVE);
154         var im_label = new Label ("IM");
155         var im_remove_button = new Button.from_stock (Stock.REMOVE);
156         var services_table = new Table (2, 2, false);
157         services_table.set_col_spacings (6);
158         services_table.attach_defaults (email_label, 0, 1, 0, 1);
159         services_table.attach_defaults (email_remove_button, 1, 2, 0, 1);
160         services_table.attach_defaults (im_label, 0, 1, 1, 2);
161         services_table.attach_defaults (im_remove_button, 1, 2, 1, 2);
162         var services_vbox_alignment = new Alignment (0, 0, 0, 0);
163         services_vbox_alignment.set_padding (0, 0, 12, 0);
164         services_vbox_alignment.add (services_table);
165         var services_vbox = new VBox (false, 6);
166         services_vbox.pack_start (services_vbox_title, false, true, 0);
167         services_vbox.pack_start (services_vbox_alignment, false, true, 0);
168
169         var vbox_rigth = new VBox (false, 18);
170         vbox_rigth.pack_start (login_vbox, false, true, 0);
171         vbox_rigth.pack_start (services_vbox, false, true, 0);
172
173         var hbox = new HBox (false, 12);
174         hbox.pack_start (vbox_left, true, true, 0);
175         hbox.pack_start (vbox_rigth, true, true, 0);
176
177         var send_button = new Button.with_label ("Send");
178         var hbox_send_button = new HBox (false, 0);
179         hbox_send_button.pack_end (send_button, false, false, 0);
180
181         var main_vbox = new VBox (false, 12);
182         main_vbox.pack_start (toolbar, false, true, 0);
183         main_vbox.pack_start (hbox, true, true, 0);
184         main_vbox.pack_start (hbox_send_button, false, false, 0);
185         main_vbox.set_border_width (12);
186         add (main_vbox);
187     }
188
189     private void connect_signals()
190     {
191         this.destroy.connect (Gtk.main_quit);
192     }
193
194     public static int main(string[] args)
195     {
196         Gtk.init(ref args);
197
198         Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
199         Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
200         Intl.textdomain (Config.GETTEXT_PACKAGE);
201
202         var window = new MainWindow();
203         window.show_all();
204
205         Gtk.main();
206
207         return 0;
208     }
209 }