~registry/gcalctool/trunk

« back to all changes in this revision

Viewing changes to src/math-variable-popup.vala

  • Committer: Robert Ancell
  • Date: 2012-10-14 03:31:40 UTC
  • Revision ID: git-v1:12ba2c81b0a81bb3ac776d1034a3c41b3173196a
Port to Vala

https://bugzilla.gnome.org/show_bug.cgi?id=640685

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008-2012 Robert Ancell
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it under
 
5
 * the terms of the GNU General Public License as published by the Free Software
 
6
 * Foundation, either version 2 of the License, or (at your option) any later
 
7
 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
 
8
 * license.
 
9
 */
 
10
 
 
11
public class MathVariablePopup : Gtk.Window
 
12
{
 
13
    private MathEquation equation;
 
14
 
 
15
    private Gtk.Box vbox;
 
16
    private Gtk.Entry variable_name_entry;
 
17
    private Gtk.Button add_variable_button;
 
18
 
 
19
    public MathVariablePopup (MathEquation equation)
 
20
    {
 
21
        this.equation = equation;
 
22
 
 
23
        decorated = false;
 
24
        skip_taskbar_hint = true;
 
25
        border_width = 6;
 
26
 
 
27
        /* Destroy this window when it loses focus */
 
28
        focus_out_event.connect ((event) => { destroy (); return false; });
 
29
 
 
30
        vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 6);
 
31
        vbox.homogeneous = true;
 
32
        add (vbox);
 
33
        vbox.show ();
 
34
 
 
35
        var names = equation.variables.get_names ();
 
36
        for (var i = 0; names[i] != null; i++)
 
37
        {
 
38
            var value = equation.variables.get (names[i]);
 
39
            var entry = make_variable_entry (names[i], value, true);
 
40
            entry.show ();
 
41
            vbox.pack_start (entry, false, true, 0);
 
42
        }
 
43
 
 
44
        var entry = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
 
45
        entry.show ();
 
46
 
 
47
        // TODO: Show greyed "variable name" text to give user a hint how to use
 
48
        variable_name_entry = new Gtk.Entry ();
 
49
        variable_name_entry.key_press_event.connect (variable_name_key_press_cb);
 
50
        variable_name_entry.changed.connect (variable_name_changed_cb);
 
51
        variable_name_entry.activate.connect (add_variable_cb);
 
52
        entry.pack_start (variable_name_entry, true, true, 0);
 
53
        variable_name_entry.show ();
 
54
 
 
55
        add_variable_button = new Gtk.Button ();
 
56
        add_variable_button.sensitive = false;
 
57
        add_variable_button.clicked.connect (add_variable_cb);
 
58
        var image = new Gtk.Image.from_stock (Gtk.Stock.ADD, Gtk.IconSize.BUTTON);
 
59
        add_variable_button.add (image);
 
60
        entry.pack_start (add_variable_button, false, true, 0);
 
61
        image.show ();
 
62
        add_variable_button.show ();
 
63
        vbox.pack_end (entry, false, true, 0);
 
64
 
 
65
        entry = make_variable_entry ("rand", null, false);
 
66
        entry.show ();
 
67
        vbox.pack_end (entry, false, true, 0);
 
68
 
 
69
        entry = make_variable_entry ("ans", equation.answer, false);
 
70
        entry.show ();
 
71
        vbox.pack_end (entry, false, true, 0);
 
72
    }
 
73
 
 
74
    private void insert_variable_cb (Gtk.Widget widget)
 
75
    {
 
76
        var name = widget.get_data<string> ("variable_name");
 
77
        equation.insert (name);
 
78
 
 
79
        widget.get_toplevel ().destroy ();
 
80
    }
 
81
 
 
82
    private bool variable_name_key_press_cb (Gtk.Widget widget, Gdk.EventKey event)
 
83
    {
 
84
        /* Can't have whitespace in names, so replace with underscores */
 
85
        if (event.keyval == Gdk.Key.space || event.keyval == Gdk.Key.KP_Space)
 
86
            event.keyval = Gdk.Key.underscore;
 
87
 
 
88
        return false;
 
89
    }
 
90
 
 
91
    private void variable_name_changed_cb ()
 
92
    {
 
93
        add_variable_button.sensitive = variable_name_entry.get_text () != "";
 
94
    }
 
95
 
 
96
    private void add_variable_cb (Gtk.Widget widget)
 
97
    {
 
98
        var name = variable_name_entry.get_text ();
 
99
        if (name == "")
 
100
            return;
 
101
 
 
102
        var z = equation.number;
 
103
        if (z != null)
 
104
            equation.variables.set (name, z);
 
105
        else if (equation.is_result)
 
106
            equation.variables.set (name, equation.answer);
 
107
        else
 
108
            warning ("Can't add variable %s, the display is not a number", name);
 
109
 
 
110
        widget.get_toplevel ().destroy ();
 
111
    }
 
112
 
 
113
    private void save_variable_cb (Gtk.Widget widget)
 
114
    {
 
115
        var name = widget.get_data<string> ("variable_name");
 
116
        var z = equation.number;
 
117
        if (z != null)
 
118
            equation.variables.set (name, z);
 
119
        else if (equation.is_result)
 
120
            equation.variables.set (name, equation.answer);
 
121
        else
 
122
            warning ("Can't save variable %s, the display is not a number", name);
 
123
 
 
124
        widget.get_toplevel ().destroy ();
 
125
    }
 
126
 
 
127
    private void delete_variable_cb (Gtk.Widget widget)
 
128
    {
 
129
        var name = widget.get_data<string> ("variable_name");
 
130
        equation.variables.delete (name);
 
131
 
 
132
        widget.get_toplevel ().destroy ();
 
133
    }
 
134
 
 
135
    private Gtk.Box make_variable_entry (string name, Number? value, bool writable)
 
136
    {
 
137
        var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
 
138
 
 
139
        string text;
 
140
        if (value != null)
 
141
        {
 
142
            var value_text = equation.serializer.to_string (value);
 
143
            text = "<b>%s</b> = %s".printf (name, value_text);
 
144
        }
 
145
        else
 
146
            text = "<b>%s</b>".printf (name);
 
147
 
 
148
        var button = new Gtk.Button ();
 
149
        button.set_data<string> ("variable_name", name);
 
150
        button.clicked.connect (insert_variable_cb);
 
151
        button.set_relief (Gtk.ReliefStyle.NONE);
 
152
        hbox.pack_start (button, true, true, 0);
 
153
        button.show ();
 
154
 
 
155
        var label = new Gtk.Label (text);
 
156
        label.set_use_markup (true);
 
157
        label.set_alignment (0.0f, 0.5f);
 
158
        button.add (label);
 
159
        label.show ();
 
160
 
 
161
        if (writable)
 
162
        {
 
163
            button = new Gtk.Button ();
 
164
            button.set_data<string> ("variable_name", name);
 
165
            var image = new Gtk.Image.from_stock (Gtk.Stock.SAVE, Gtk.IconSize.BUTTON);
 
166
            button.add (image);
 
167
            hbox.pack_start (button, false, true, 0);
 
168
            button.clicked.connect (save_variable_cb);
 
169
            image.show ();
 
170
            button.show ();
 
171
 
 
172
            button = new Gtk.Button ();
 
173
            button.set_data<string> ("variable_name", name);
 
174
            image = new Gtk.Image.from_stock (Gtk.Stock.DELETE, Gtk.IconSize.BUTTON);
 
175
            button.add (image);
 
176
            hbox.pack_start (button, false, true, 0);
 
177
            button.clicked.connect (delete_variable_cb);
 
178
            image.show ();
 
179
            button.show ();
 
180
        }
 
181
 
 
182
        return hbox;
 
183
    }
 
184
}