1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
|
/*
* Copyright 2018–2019 elementary, Inc. (https://elementary.io)
* Copyright 2011–2013 Maxwell Barvian <maxwell@elementaryos.org>
* Copyright 2011–2013 Corentin Noël <tintou@noel.tf>
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
namespace Granite.Widgets {
/**
* This widget allows users to easily pick a time.
*/
public class TimePicker : Gtk.Entry {
/**
* Sent when the time got changed
*/
public signal void time_changed ();
/**
* Format used in 12h mode
*/
public string format_12 { get; construct; }
/**
* Format used in 24h mode
*/
public string format_24 { get; construct; }
private GLib.DateTime _time = null;
/**
* Current time
*/
public GLib.DateTime time {
get {
if (_time == null) {
time = new GLib.DateTime.now_local ();
}
return _time;
}
set {
_time = value;
changing_time = true;
if (_time.get_hour () >= 12) {
am_pm_modebutton.set_active (1);
} else {
am_pm_modebutton.set_active (0);
}
update_text (true);
changing_time = false;
}
}
private bool changing_time = false;
private string old_string = "";
private Gtk.Popover popover;
private Gtk.SpinButton hours_spinbutton;
private Gtk.SpinButton minutes_spinbutton;
private ModeButton am_pm_modebutton;
/**
* Creates a new TimePicker.
*
* @param format_12 The desired custom 12h format. For example "%l:%M %p".
* @param format_24 The desired custom 24h format. For example "%H:%M".
*/
public TimePicker.with_format (string format_12, string format_24) {
Object (format_12: format_12, format_24: format_24);
}
static construct {
Granite.init ();
}
construct {
if (format_12 == null) {
format_12 = Granite.DateTime.get_default_time_format (true);
}
if (format_24 == null) {
format_24 = Granite.DateTime.get_default_time_format (false);
}
max_length = 8;
secondary_icon_gicon = new ThemedIcon.with_default_fallbacks ("appointment-symbolic");
icon_release.connect (on_icon_press);
am_pm_modebutton = new ModeButton () {
hexpand = true,
orientation = Gtk.Orientation.VERTICAL,
no_show_all = true
};
/// TRANSLATORS: this will only show up when 12-hours clock is in use
am_pm_modebutton.append_text (_("AM"));
/// TRANSLATORS: this will only show up when 12-hours clock is in use
am_pm_modebutton.append_text (_("PM"));
am_pm_modebutton.mode_changed.connect (mode => {
if (changing_time) {
return;
}
if (am_pm_modebutton.selected == 0) {
time = _time.add_hours (-12);
time_changed ();
} else if (am_pm_modebutton.selected == 1) {
time = _time.add_hours (12);
time_changed ();
} else {
assert_not_reached ();
}
update_text (true);
});
if (Granite.DateTime.is_clock_format_12h ()) {
hours_spinbutton = new Gtk.SpinButton.with_range (1, 12, 1);
} else {
hours_spinbutton = new Gtk.SpinButton.with_range (0, 23, 1);
}
hours_spinbutton.orientation = Gtk.Orientation.VERTICAL;
hours_spinbutton.wrap = true;
hours_spinbutton.value_changed.connect (() => update_time (true));
minutes_spinbutton = new Gtk.SpinButton.with_range (0, 59, 1);
minutes_spinbutton.orientation = Gtk.Orientation.VERTICAL;
minutes_spinbutton.wrap = true;
minutes_spinbutton.value_changed.connect (() => update_time (false));
// If the spinbutton value is less than 10, append zero in front of value. '6' becomes '06'
minutes_spinbutton.output.connect (() => {
var val = minutes_spinbutton.get_value ();
if (val < 10) {
minutes_spinbutton.set_text ("0" + val.to_string ());
return true;
}
return false;
});
/// TRANSLATORS: separates hours from minutes.
var separation_label = new Gtk.Label (_(":"));
var pop_grid = new Gtk.Grid ();
pop_grid.column_spacing = 6;
pop_grid.row_spacing = 6;
pop_grid.attach (hours_spinbutton, 0, 0, 1, 1);
pop_grid.attach (separation_label, 1, 0, 1, 1);
pop_grid.attach (minutes_spinbutton, 2, 0, 1, 1);
pop_grid.attach (am_pm_modebutton, 3, 0, 1, 1);
pop_grid.margin = 6;
popover = new Gtk.Popover (this);
popover.position = Gtk.PositionType.BOTTOM;
popover.add (pop_grid);
// Connecting to events allowing manual changes
add_events (Gdk.EventMask.FOCUS_CHANGE_MASK | Gdk.EventMask.SCROLL_MASK);
focus_out_event.connect (() => {
is_unfocused ();
return false;
});
scroll_event.connect ((event) => {
switch (event.direction) {
case Gdk.ScrollDirection.UP:
case Gdk.ScrollDirection.RIGHT:
_time = _time.add_minutes (1);
break;
case Gdk.ScrollDirection.DOWN:
case Gdk.ScrollDirection.LEFT:
_time = _time.add_minutes (-1);
break;
default:
break;
}
update_text ();
return false;
});
activate.connect (is_unfocused);
update_text ();
}
private void update_time (bool is_hour) {
if (changing_time) {
return;
}
if (is_hour) {
var new_hour = hours_spinbutton.get_value_as_int () - time.get_hour ();
if (Granite.DateTime.is_clock_format_12h ()) {
if (hours_spinbutton.get_value_as_int () == 12 && am_pm_modebutton.selected == 0) {
_time = _time.add_hours (-_time.get_hour ());
} else if (hours_spinbutton.get_value_as_int () < 12 && am_pm_modebutton.selected == 0) {
_time = _time.add_hours (new_hour);
} else if (hours_spinbutton.get_value_as_int () == 12 && am_pm_modebutton.selected == 1) {
_time = _time.add_hours (-_time.get_hour () + 12);
} else if (hours_spinbutton.get_value_as_int () < 12 && am_pm_modebutton.selected == 1) {
_time = _time.add_hours (new_hour + 12);
if (time.get_hour () <= 12) {
_time = _time.add_hours (12);
}
}
} else {
_time = _time.add_hours (new_hour);
}
} else {
_time = time.add_minutes (minutes_spinbutton.get_value_as_int () - time.get_minute ());
}
update_text ();
}
private void on_icon_press (Gtk.EntryIconPosition position, Gdk.Event event) {
// If the mode is changed from 12h to 24h or visa versa, the entry updates on icon press
update_text ();
changing_time = true;
if (Granite.DateTime.is_clock_format_12h () && time.get_hour () > 12) {
hours_spinbutton.set_value (time.get_hour () - 12);
} else {
hours_spinbutton.set_value (time.get_hour ());
}
if (Granite.DateTime.is_clock_format_12h ()) {
am_pm_modebutton.no_show_all = false;
am_pm_modebutton.show_all ();
if (time.get_hour () > 12) {
hours_spinbutton.set_value (time.get_hour () - 12);
} else if (time.get_hour () == 0) {
hours_spinbutton.set_value (12);
} else {
hours_spinbutton.set_value (time.get_hour ());
}
// Make sure that bounds are set correctly
hours_spinbutton.set_range (1, 12);
} else {
am_pm_modebutton.no_show_all = true;
am_pm_modebutton.hide ();
hours_spinbutton.set_value (time.get_hour ());
hours_spinbutton.set_range (0, 23);
}
minutes_spinbutton.set_value (time.get_minute ());
changing_time = false;
popover.pointing_to = get_icon_area (Gtk.EntryIconPosition.SECONDARY);
popover.show_all ();
}
[Version (deprecated = true, deprecated_since = "5.2.0")]
protected virtual void position_dropdown (out int x, out int y) {
x = -1;
y = -1;
}
private void is_unfocused () {
if (!popover.visible && old_string.collate (text) != 0) {
old_string = text;
parse_time (text.dup ());
}
}
private void parse_time (string timestr) {
string current = "";
bool is_hours = true;
bool is_suffix = false;
bool has_suffix = false;
int? hour = null;
int? minute = null;
foreach (var c in timestr.down ().to_utf8 ()) {
if (c.isdigit ()) {
current = "%s%c".printf (current, c);
} else {
if (!is_suffix) {
if (current != "") {
if (is_hours) {
is_hours = false;
hour = int.parse (current);
current = "";
} else {
minute = int.parse (current);
current = "";
}
}
if (c.to_string ().contains ("a") || c.to_string ().contains ("p")) {
is_suffix = true;
current = "%s%c".printf (current, c);
}
}
if (c.to_string ().contains ("m") && is_suffix) {
if (hour == null) {
return;
} else if (minute == null) {
minute = 0;
}
// We can imagine that some will try to set it to "19:00 am"
if (current.contains ("a") || hour >= 12) {
time = time.add_hours (hour - time.get_hour ());
} else {
time = time.add_hours (hour + 12 - time.get_hour ());
}
if (current.contains ("a") && hour == 12) {
time = time.add_hours (-12);
}
time = time.add_minutes (minute - time.get_minute ());
has_suffix = true;
}
}
}
if (is_hours == false && is_suffix == false && current != "") {
minute = int.parse (current);
}
if (hour == null) {
if (current.length < 3) {
hour = int.parse (current);
minute = 0;
} else if (current.length == 4) {
hour = int.parse (current.slice (0, 2));
minute = int.parse (current.slice (2, 4));
if (hour > 23 || minute > 59) {
hour = null;
minute = null;
}
}
}
if (hour == null || minute == null) {
update_text ();
return;
}
if (has_suffix == false) {
time = time.add_hours (hour - time.get_hour ());
time = time.add_minutes (minute - time.get_minute ());
}
update_text ();
}
private void update_text (bool no_signal = false) {
if (Granite.DateTime.is_clock_format_12h ()) {
set_text (time.format (format_12));
} else {
set_text (time.format (format_24));
}
old_string = text;
if (no_signal == false) {
time_changed ();
}
}
}
}
|