~victored/+junk/huddle

« back to all changes in this revision

Viewing changes to src/main-window.vala

  • Committer: Victor Eduardo
  • Date: 2012-10-09 05:17:40 UTC
  • Revision ID: victor@elementaryos.org-20121009051740-vg6nik7mqjy8mwns
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
 
2
/**
 
3
 * Copyright (C) 2012 Victor Eduardo <victoreduardm@gmail.com>
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 *
 
10
 * See the file COPYING for the full license text.
 
11
 */
 
12
 
 
13
public class Huddle.MainWindow : Gtk.Window {
 
14
    private const int MIN_WIDTH = 750;
 
15
    private const int MIN_HEIGHT = 440;
 
16
 
 
17
    public enum State {
 
18
        NORMAL,
 
19
        MAXIMIZED
 
20
    }
 
21
 
 
22
    private Granite.Widgets.SidebarPaned contacts_pane = new Granite.Widgets.SidebarPaned ();
 
23
 
 
24
    private State state;
 
25
    private int width;
 
26
    private int height;
 
27
 
 
28
    public MainWindow (App application) {
 
29
        title = App.instance.program_name;
 
30
 
 
31
        set_application (application);
 
32
        set_icon (Icons.APP.render_at_size (48));
 
33
 
 
34
        set_geometry ();
 
35
        add_widgets ();
 
36
    }
 
37
 
 
38
    private void set_geometry () {
 
39
        width_request = MIN_WIDTH;
 
40
        height_request = MIN_HEIGHT;
 
41
        window_position = Gtk.WindowPosition.CENTER;
 
42
 
 
43
        width = (int) Settings.SavedState.instance.window_width;
 
44
        height = (int) Settings.SavedState.instance.window_height;
 
45
        set_default_size (width, height);
 
46
 
 
47
        state = (State) Settings.SavedState.instance.window_state;
 
48
        switch (state) {
 
49
            case State.MAXIMIZED:
 
50
                maximize ();
 
51
                break;
 
52
            default:
 
53
                break;
 
54
        }
 
55
    }
 
56
 
 
57
    private void add_widgets () {
 
58
        contacts_pane.position = (int) Settings.SavedState.instance.sidebar_width;
 
59
        add (contacts_pane);
 
60
    }
 
61
 
 
62
    public void show_alert (string title, string message) {
 
63
        var dialog = new Gtk.MessageDialog (this, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR,
 
64
                                            Gtk.ButtonsType.OK, title);
 
65
 
 
66
        dialog.title = App.instance.program_name;
 
67
        dialog.secondary_text = message;
 
68
        dialog.secondary_use_markup = true;
 
69
 
 
70
        dialog.run ();
 
71
        dialog.destroy ();
 
72
    }
 
73
 
 
74
    /**
 
75
     * Called when the user tries to close the window.
 
76
     */
 
77
    public override bool delete_event (Gdk.EventAny event) {
 
78
        if (Settings.Main.instance.hide_on_close) {
 
79
            hide ();
 
80
            return true;
 
81
        }
 
82
 
 
83
        return false; // Let the window be destroyed
 
84
    }
 
85
 
 
86
    public override void destroy () {
 
87
        Settings.SavedState.instance.sidebar_width = contacts_pane.position;
 
88
        Settings.SavedState.instance.window_state = state;
 
89
        Settings.SavedState.instance.window_width = width;
 
90
        Settings.SavedState.instance.window_height = height;
 
91
 
 
92
        base.destroy ();
 
93
    }
 
94
 
 
95
    public override bool configure_event (Gdk.EventConfigure event) {
 
96
        bool window_maximized = (get_window ().get_state () == Gdk.WindowState.MAXIMIZED);
 
97
 
 
98
        if (!window_maximized)
 
99
            get_size (out width, out height);
 
100
 
 
101
        state = window_maximized ? State.MAXIMIZED : State.NORMAL;
 
102
 
 
103
        return base.configure_event (event);
 
104
    }
 
105
}