moonshot-window: Manually create the viewport to control the border
[moonshot-ui.git] / src / moonshot-window.vala
1 using Gtk;
2
3 class MainWindow : Window
4 {
5     private const int WINDOW_WIDTH = 400;
6     private const int WINDOW_HEIGHT = 500;
7
8     private Entry search_entry;
9     private VBox vbox_rigth;
10     private VBox custom_vbox;
11
12     private Entry username_entry;
13     private Entry password_entry;
14
15     private enum Columns
16     {
17         IDCARD_COL,
18         LOGO_COL,
19         NAME_COL,
20         N_COLUMNS
21     }
22
23     public MainWindow()
24     {
25         this.title = "Moonshoot";
26         this.position = WindowPosition.CENTER;
27         set_default_size (WINDOW_WIDTH, WINDOW_HEIGHT);
28
29         build_ui();
30         connect_signals();
31     }
32
33     private void search_entry_icon_press_cb (EntryIconPosition pos, Gdk.Event event)
34     {
35         if (pos == EntryIconPosition.PRIMARY)
36         {
37             print ("Search entry icon pressed\n");
38         }
39         else
40         {
41             this.search_entry.set_text ("");
42         }
43     }
44
45     private void search_entry_text_changed_cb ()
46     {
47         var has_text = this.search_entry.get_text_length () > 0;
48         this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, has_text);
49         this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, has_text);
50
51         this.vbox_rigth.set_visible (false);
52         this.resize (WINDOW_WIDTH, WINDOW_HEIGHT);
53     }
54
55     private bool search_entry_key_press_event_cb (Gdk.EventKey e)
56     {
57         if(Gdk.keyval_name(e.keyval) == "Escape")
58            this.search_entry.set_text("");
59
60         // Continue processing this event, since the
61         // text entry functionality needs to see it too.
62         return false;
63     }
64
65     private IdCard get_id_card ()
66     {
67         var id_card = new IdCard ();
68         id_card.issuer = "University";
69         id_card.username = "username";
70         id_card.password = "password";
71
72         return id_card;
73     }
74
75     private void show_details (IdCard id_card)
76     {
77        this.vbox_rigth.set_visible (!vbox_rigth.get_visible ());
78
79        if (this.vbox_rigth.get_visible () == false)
80        {
81            this.resize (WINDOW_WIDTH, WINDOW_HEIGHT);
82        }
83        else
84        {
85            this.username_entry.set_text (id_card.username);
86            this.password_entry.set_text (id_card.password);
87        }
88     }
89
90     private void details_button_clicked_cb ()
91     {
92        var id_card = get_id_card ();
93        show_details (id_card);
94     }
95
96     private void set_idcard_color (IdCardWidget id_card_widget)
97     {
98         var color = Gdk.Color ();
99         color.red = 65535;
100         color.green = 65535;
101         color.blue = 65535;
102         var state = id_card_widget.get_state ();
103         id_card_widget.event_box.modify_bg (state, color);
104     }
105
106     private void add_identity (AddIdentityDialog dialog)
107     {
108         var id_card_widget = new IdCardWidget ();
109         var id_card = new IdCard ();
110
111         id_card.issuer = dialog.issuer;
112         id_card.username = dialog.username;
113         id_card.password = dialog.password;
114         id_card_widget.id_card = id_card;
115
116         set_idcard_color (id_card_widget);
117
118         this.custom_vbox.pack_start (id_card_widget, false, false);
119
120         id_card_widget.details_button.clicked.connect (details_button_clicked_cb);
121         id_card_widget.delete_button.clicked.connect (remove_identity_cb);
122     }
123
124     private void add_identity_cb ()
125     {
126         var dialog = new AddIdentityDialog ();
127         var result = dialog.run ();
128
129         switch (result) {
130         case ResponseType.OK:
131             add_identity (dialog);
132             break;
133         default:
134             break;
135         }
136         dialog.destroy ();
137     }
138
139     private void remove_identity ()
140     {
141     }
142
143     private void remove_identity_cb ()
144     {
145         var dialog = new MessageDialog (null,
146                                         DialogFlags.DESTROY_WITH_PARENT,
147                                         MessageType.INFO,
148                                         Gtk.ButtonsType.YES_NO,
149                                         _("Are you sure you want to delete this ID Card?"));
150         var result = dialog.run ();
151         switch (result) {
152         case ResponseType.YES:
153             remove_identity ();
154             break;
155         default:
156             break;
157         }
158         dialog.destroy ();
159     }
160
161     private void label_make_bold (Label label)
162     {
163         var font_desc = new Pango.FontDescription ();
164
165         font_desc.set_weight (Pango.Weight.BOLD);
166
167         /* This will only affect the weight of the font, the rest is
168          * from the current state of the widget, which comes from the
169          * theme or user prefs, since the font desc only has the
170          * weight flag turned on.
171          */
172         label.modify_font (font_desc);
173     }
174
175     private void build_ui()
176     {
177         this.search_entry = new Entry();
178
179         this.search_entry.set_icon_from_icon_name (EntryIconPosition.PRIMARY,
180                                                    "edit-find-symbolic");
181         this.search_entry.set_icon_sensitive (EntryIconPosition.PRIMARY, false);
182         this.search_entry.set_icon_tooltip_text (EntryIconPosition.PRIMARY,
183                                                  _("Search identity or service"));
184
185         this.search_entry.set_icon_from_icon_name (EntryIconPosition.SECONDARY,
186                                                    "edit-clear-symbolic");
187         this.search_entry.set_icon_sensitive (EntryIconPosition.SECONDARY, false);
188         this.search_entry.set_icon_tooltip_text (EntryIconPosition.SECONDARY,
189                                                  _("Clear the current search"));
190
191         this.search_entry.icon_press.connect (search_entry_icon_press_cb);
192         this.search_entry.notify["text"].connect (search_entry_text_changed_cb);
193         this.search_entry.key_press_event.connect(search_entry_key_press_event_cb);
194
195         this.custom_vbox = new VBox (false, 6);
196
197         var viewport = new Viewport (null, null);
198         viewport.set_border_width (6);
199         viewport.set_shadow_type (ShadowType.NONE);
200         viewport.add (custom_vbox);
201         var scroll = new ScrolledWindow (null, null);
202         scroll.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC);
203         scroll.set_shadow_type (ShadowType.IN);
204         scroll.add_with_viewport (viewport);
205
206         var button_add = new ToolButton (null, null);
207         button_add.set_icon_name ("list-add-symbolic");
208         button_add.clicked.connect (add_identity_cb);
209         var button_toolbar = new Toolbar ();
210         button_toolbar.insert (button_add, 0);
211
212         var vbox_left = new VBox (false, 0);
213         vbox_left.pack_start (search_entry, false, false, 6);
214         vbox_left.pack_start (scroll, true, true, 0);
215         vbox_left.pack_start (button_toolbar, false, false, 0);
216         vbox_left.set_size_request (WINDOW_WIDTH, 0);
217
218         var login_vbox_title = new Label (_("Login: "));
219         label_make_bold (login_vbox_title);
220         login_vbox_title.set_alignment (0, (float) 0.5);
221         var username_label = new Label (_("Username:"));
222         username_label.set_alignment (1, (float) 0.5);
223         this.username_entry = new Entry ();
224         var password_label = new Label (_("Password:"));
225         password_label.set_alignment (1, (float) 0.5);
226         this.password_entry = new Entry ();
227         password_entry.set_invisible_char ('*');
228         password_entry.set_visibility (false);
229         var remember_checkbutton = new CheckButton.with_label (_("Remember password"));
230         var login_table = new Table (3, 3, false);
231         login_table.set_col_spacings (10);
232         login_table.set_row_spacings (10);
233         login_table.attach_defaults (username_label, 0, 1, 0, 1);
234         login_table.attach_defaults (username_entry, 1, 2, 0, 1);
235         login_table.attach_defaults (password_label, 0, 1, 1, 2);
236         login_table.attach_defaults (password_entry, 1, 2, 1, 2);
237         login_table.attach_defaults (remember_checkbutton,  1, 2, 2, 3);
238         var login_vbox_alignment = new Alignment (0, 0, 0, 0);
239         login_vbox_alignment.set_padding (0, 0, 12, 0);
240         login_vbox_alignment.add (login_table);
241         var login_vbox = new VBox (false, 6);
242         login_vbox.pack_start (login_vbox_title, false, true, 0);
243         login_vbox.pack_start (login_vbox_alignment, false, true, 0);
244
245         var services_vbox_title = new Label (_("Services:"));
246         label_make_bold (services_vbox_title);
247         services_vbox_title.set_alignment (0, (float) 0.5);
248         var email_label = new Label (_("Email"));
249         var email_remove_button = new Button.from_stock (Stock.REMOVE);
250         var im_label = new Label (_("IM"));
251         var im_remove_button = new Button.from_stock (Stock.REMOVE);
252         var services_table = new Table (2, 2, false);
253         services_table.set_col_spacings (10);
254         services_table.set_row_spacings (10);
255         services_table.attach_defaults (email_label, 0, 1, 0, 1);
256         services_table.attach_defaults (email_remove_button, 1, 2, 0, 1);
257         services_table.attach_defaults (im_label, 0, 1, 1, 2);
258         services_table.attach_defaults (im_remove_button, 1, 2, 1, 2);
259         var services_vbox_alignment = new Alignment (0, 0, 0, 0);
260         services_vbox_alignment.set_padding (0, 0, 12, 0);
261         services_vbox_alignment.add (services_table);
262         var services_vbox = new VBox (false, 6);
263         services_vbox.pack_start (services_vbox_title, false, true, 0);
264         services_vbox.pack_start (services_vbox_alignment, false, true, 0);
265
266         this.vbox_rigth = new VBox (false, 18);
267         vbox_rigth.pack_start (login_vbox, false, true, 0);
268         vbox_rigth.pack_start (services_vbox, false, true, 0);
269
270         var hbox = new HBox (false, 12);
271         hbox.pack_start (vbox_left, false, false, 0);
272         hbox.pack_start (vbox_rigth, false, false, 0);
273
274         var main_vbox = new VBox (false, 12);
275         main_vbox.pack_start (hbox, true, true, 0);
276         main_vbox.set_border_width (12);
277         add (main_vbox);
278
279         main_vbox.show_all();
280         this.vbox_rigth.hide ();
281     }
282
283     private void connect_signals()
284     {
285         this.destroy.connect (Gtk.main_quit);
286     }
287
288     public static int main(string[] args)
289     {
290         Gtk.init(ref args);
291
292         Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
293         Intl.bind_textdomain_codeset (Config.GETTEXT_PACKAGE, "UTF-8");
294         Intl.textdomain (Config.GETTEXT_PACKAGE);
295
296         var window = new MainWindow();
297         window.show ();
298
299         Gtk.main();
300
301         return 0;
302     }
303 }