Updated kn translations
[gcalctool.git] / src / math-variable-popup.vala
blob05849a188c912c0ce21079b3229e0d0afa8b02a4
1 /*
2 * Copyright (C) 2008-2012 Robert Ancell
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 */
11 public class MathVariablePopup : Gtk.Window
13 private MathEquation equation;
15 private Gtk.Box vbox;
16 private Gtk.Entry variable_name_entry;
17 private Gtk.Button add_variable_button;
19 public MathVariablePopup (MathEquation equation)
21 this.equation = equation;
23 decorated = false;
24 skip_taskbar_hint = true;
25 border_width = 6;
27 /* Destroy this window when it loses focus */
28 focus_out_event.connect ((event) => { destroy (); return false; });
30 vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 6);
31 vbox.homogeneous = true;
32 add (vbox);
33 vbox.show ();
35 var names = equation.variables.get_names ();
36 for (var i = 0; names[i] != null; i++)
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);
44 var entry = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
45 entry.show ();
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 ();
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);
65 entry = make_variable_entry ("rand", null, false);
66 entry.show ();
67 vbox.pack_end (entry, false, true, 0);
69 entry = make_variable_entry ("ans", equation.answer, false);
70 entry.show ();
71 vbox.pack_end (entry, false, true, 0);
74 private void insert_variable_cb (Gtk.Widget widget)
76 var name = widget.get_data<string> ("variable_name");
77 equation.insert (name);
79 widget.get_toplevel ().destroy ();
82 private bool variable_name_key_press_cb (Gtk.Widget widget, Gdk.EventKey event)
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;
88 return false;
91 private void variable_name_changed_cb ()
93 add_variable_button.sensitive = variable_name_entry.get_text () != "";
96 private void add_variable_cb (Gtk.Widget widget)
98 var name = variable_name_entry.get_text ();
99 if (name == "")
100 return;
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);
110 widget.get_toplevel ().destroy ();
113 private void save_variable_cb (Gtk.Widget widget)
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);
124 widget.get_toplevel ().destroy ();
127 private void delete_variable_cb (Gtk.Widget widget)
129 var name = widget.get_data<string> ("variable_name");
130 equation.variables.delete (name);
132 widget.get_toplevel ().destroy ();
135 private Gtk.Box make_variable_entry (string name, Number? value, bool writable)
137 var hbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 6);
139 string text;
140 if (value != null)
142 var value_text = equation.serializer.to_string (value);
143 text = "<b>%s</b> = %s".printf (name, value_text);
145 else
146 text = "<b>%s</b>".printf (name);
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 ();
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 ();
161 if (writable)
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 ();
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 ();
182 return hbox;