City button: fix dialog title
[beifahrer] / src / city-button.vala
1 /* This file is part of Beifahrer.
2  *
3  * Copyright (C) 2010 Philipp Zabel
4  *
5  * Beifahrer is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Beifahrer is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Beifahrer. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using Gtk;
20 using Hildon;
21
22 class CityButton : Hildon.Button {
23         private enum Response {
24                 RADIUS = 1
25         }
26
27         Gtk.Dialog dialog;
28         TouchSelector city_selector;
29         PickerButton radius_button;
30         int active = 0;
31         int radius = 0;
32         bool use_radius;
33
34         public CityButton (SizeType size, ButtonArrangement arrangement, List<City> city_list, bool with_radius = true) {
35                 GLib.Object (arrangement: arrangement, size: size);
36                 set_style (ButtonStyle.PICKER);
37
38                 use_radius = with_radius;
39                 city_selector = new TouchSelectorEntry.text ();
40                 foreach (unowned City city in city_list)
41                         city_selector.append_text (city.name);
42
43                 clicked.connect (on_clicked);
44         }
45
46         public void set_city (string city) {
47                 var model = city_selector.get_model (0);
48                 string _city;
49                 int n = 0;
50
51                 TreeIter iter;
52                 if (model.get_iter_first (out iter)) {
53                         do {
54                                 model.get (iter, 0, out _city);
55                                 if (city == _city) {
56                                         set_active (n);
57                                         return;
58                                 }
59                                 n++;
60                         } while (model.iter_next (ref iter));
61                 }
62         }
63
64         public void set_active (int _active) {
65                 active = _active;
66                 city_selector.set_active (0, active);
67                 update_value ();
68         }
69
70         public int get_active () {
71                 return active;
72         }
73
74         public void set_radius (int _radius) {
75                 radius = _radius;
76                 update_value ();
77         }
78
79         public int get_radius () {
80                 return radius;
81         }
82
83         private void on_clicked () {
84                 dialog = new Gtk.Dialog ();
85                 dialog.set_transient_for (find_parent_window ());
86                 dialog.set_title (get_title ());
87
88                 var content_area = (Box) dialog.get_content_area ();
89                 content_area.pack_start (city_selector, true, true, 0);
90                 city_selector.set_size_request (-1, 5*70);
91
92                 var radius_selector = new TouchSelector.text ();
93                 for (int km = 0; km <= 50; km += 10)
94                         radius_selector.append_text ("%d km".printf (km));
95
96                 if (use_radius) {
97                         radius_button = new PickerButton (SizeType.FINGER_HEIGHT,
98                                                           ButtonArrangement.VERTICAL);
99                         radius_button.set_selector (radius_selector);
100                         radius_button.set_title (_("Radius"));
101                         radius_button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
102                         radius_button.set_active (radius / 10);
103                         dialog.add_action_widget (radius_button, Response.RADIUS);
104
105                         var action_area = (ButtonBox) dialog.get_action_area ();
106                         action_area.set_child_secondary (radius_button, true);
107                 }
108
109                 dialog.add_button (_("Done"), Gtk.ResponseType.OK);
110
111                 dialog.response.connect (on_response);
112
113                 dialog.show_all ();
114         }
115
116         private void on_response (int response_id) {
117                 if (response_id == Gtk.ResponseType.OK) {
118                         active = city_selector.get_active (0);
119                         radius = radius_button.get_active () * 10;
120                         update_value ();
121                 }
122                 if (response_id != Response.RADIUS) {
123                         // Unlink the city_selector so it doesn't get destroyed with the dialog
124                         var content_area = (Box) dialog.get_content_area ();
125                         content_area.remove (city_selector);
126                         dialog.destroy ();
127                         dialog = null;
128                 }
129         }
130
131         private void update_value () {
132                 if (get_radius () == 0)
133                         set_value (city_selector.get_current_text ());
134                 else
135                         set_value ("%s, radius %d km".printf (city_selector.get_current_text (), radius));
136         }
137
138         private Gtk.Window? find_parent_window () {
139                 for (Gtk.Widget p = parent; p != null; p = p.parent)
140                         if (p is Gtk.Window)
141                                 return (Gtk.Window) p;
142                 return null;
143         }
144 }