TimeChooser.vala 3.25 KB
Newer Older
1
/*
2
 * This file is part of GNOME Break Timer.
3
 * 
4
 * GNOME Break Timer is free software: you can redistribute it and/or modify
5 6 7 8
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
9
 * GNOME Break Timer is distributed in the hope that it will be useful,
10 11 12 13 14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
15
 * along with GNOME Break Timer.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23 24 25
 */

public class TimeChooser : Gtk.ComboBox {
	private Gtk.ListStore list_store;
	
	private Gtk.TreeIter other_item;
	private Gtk.TreeIter? custom_item;
	
	private const int OPTION_OTHER = -1;
	
26
	public int time_seconds { get; set; }
27
	
28
	public signal void time_selected (int time);
29
	
30
	public TimeChooser (int[] options) {
31
		Object ();
32
		
33
		this.list_store = new Gtk.ListStore (3, typeof (string), typeof (string), typeof (int));
34
		
35 36
		this.set_model (this.list_store);
		this.set_id_column (1);
37
		
38 39 40
		Gtk.CellRendererText cell = new Gtk.CellRendererText ();
		this.pack_start (cell, true);
		this.set_attributes (cell, "text", null);
41 42
		
		foreach (int time in options) {
43 44
			string label = NaturalTime.instance.get_label_for_seconds (time);
			this.add_option (label, time);
45
		}
46
		// this.other_item = this.add_option ( _("Other…"), OPTION_OTHER);
47 48
		this.custom_item = null;
		
49
		this.changed.connect (this.on_changed);
50
		
51 52
		this.notify["time-seconds"].connect ( (s, p) => {
			this.set_time (this.time_seconds);
53 54 55
		});
	}
	
56 57
	public bool set_time (int seconds) {
		string id = seconds.to_string ();
58
		
59
		bool option_exists = this.set_active_id (id);
60 61 62
		
		if (!option_exists) {
			if (seconds > 0) {
63 64
				Gtk.TreeIter new_option = this.add_custom_option (seconds);
				this.set_active_iter (new_option);
65 66 67 68 69 70 71 72 73
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	}
	
74
	public int get_time () {
75 76 77
		return this.time_seconds;
	}
	
78 79
	private Gtk.TreeIter add_option (string label, int seconds) {
		string id = seconds.to_string ();
80 81
		
		Gtk.TreeIter iter;
82 83
		this.list_store.append (out iter);
		this.list_store.set (iter, 0, label, 1, id, 2, seconds, -1);
84 85 86 87
		
		return iter;
	}
	
88 89 90
	private Gtk.TreeIter add_custom_option (int seconds) {
		string label = NaturalTime.instance.get_label_for_seconds (seconds);
		string id = seconds.to_string ();
91 92
		
		if (this.custom_item == null) {
93 94
			this.list_store.append (out this.custom_item);
			this.list_store.set (this.custom_item, 0, label, 1, id, 2, seconds, -1);
95 96
			return this.custom_item;
		} else {
97
			this.list_store.set (this.custom_item, 0, label, 1, id, 2, seconds, -1);
98 99 100 101
			return this.custom_item;
		}
	}
	
102 103
	private void on_changed () {
		if (this.get_active () < 0) {
104 105 106 107
			return;
		}
		
		Gtk.TreeIter iter;
108
		this.get_active_iter (out iter);
109 110
		
		int val;
111
		this.list_store.get (iter, 2, out val);
112
		if (val == OPTION_OTHER) {
113
			this.start_custom_input ();
114 115
		} else if (val > 0) {
			this.time_seconds = val;
116
			this.time_selected (val);
117 118 119
		}
	}
	
120
	private void start_custom_input () {
121
		GLib.warning("Custom time input is not implemented");
122
	}
123
}