bzr branch
http://gegoxaren.bato24.eu/bzr/loggerouter/trunk
1
by Gustav Hartvigsson
* Inital release |
1 |
/*
|
2 |
This file is part of LoggerOuter.
|
|
3 |
||
4 |
LoggerOuter is free software: you can redistribute it and/or modify it under the
|
|
5 |
terms of the GNU Lesser General Public License as published by the Free Software
|
|
6 |
Foundation, either version 3 of the License, or (at your option) any later
|
|
7 |
version.
|
|
8 |
||
9 |
LoggerOuter is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
12 |
License for more details.
|
|
13 |
||
14 |
You should have received a copy of the GNU Lessel General Public License
|
|
15 |
along with LoggerOuter. If not, see <https://www.gnu.org/licenses/>.
|
|
16 |
*/
|
|
17 |
||
18 |
public class LO.Timer : Gtk.Window { |
|
19 |
||
20 |
Gtk.Box layout; |
|
21 |
Gtk.Box button_box; |
|
22 |
Gtk.Label timer_label; |
|
23 |
Gtk.Label text_label; |
|
24 |
Gtk.Button ok_button; |
|
25 |
Gtk.Button close_button; |
|
26 |
|
|
27 |
private int _time; |
|
28 |
public int time { |
|
29 |
construct set { |
|
30 |
this._time = value; |
|
31 |
} |
|
32 |
get { |
|
33 |
return this._time; |
|
34 |
} |
|
35 |
} |
|
36 |
||
37 |
private string _text; |
|
38 |
public string text { |
|
39 |
construct set { |
|
40 |
this._text = value; |
|
41 |
} |
|
42 |
get { |
|
43 |
return _text; |
|
44 |
} |
|
45 |
} |
|
46 |
||
47 |
public delegate void ReturnCallback (ExitCode exit_code); |
|
48 |
ReturnCallback callback; |
|
49 |
||
50 |
construct { |
|
51 |
||
52 |
this.width_request = 100; |
|
53 |
this.height_request = 100; |
|
54 |
this.resizable = false; |
|
55 |
||
56 |
layout = new Gtk.Box (Gtk.Orientation.VERTICAL, 5) { |
|
57 |
hexpand = true, |
|
58 |
vexpand = true, |
|
59 |
homogeneous = false, |
|
60 |
}; |
|
61 |
button_box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0) { |
|
62 |
hexpand = true, |
|
63 |
vexpand = false, |
|
64 |
homogeneous = true, |
|
65 |
}; |
|
66 |
||
67 |
timer_label = new Gtk.Label ("Time Remanining: 0") { |
|
68 |
vexpand = true, |
|
69 |
}; |
|
70 |
||
71 |
text_label = new Gtk.Label (this.text) { |
|
72 |
vexpand = true, |
|
73 |
}; |
|
74 |
||
75 |
layout.append (text_label); |
|
76 |
layout.append (timer_label); |
|
77 |
||
78 |
close_button = new Gtk.Button.with_label ("Cancel"); |
|
79 |
close_button.clicked.connect (() => { |
|
80 |
return_to_caller (ExitCode.CANCEL); |
|
81 |
}); |
|
82 |
button_box.append (close_button); |
|
83 |
||
84 |
ok_button = new Gtk.Button.with_label ("OK"); |
|
85 |
ok_button.clicked.connect (() => { |
|
86 |
return_to_caller (ExitCode.OK); |
|
87 |
}); |
|
88 |
button_box.append (ok_button); |
|
89 |
||
90 |
layout.append (button_box); |
|
91 |
|
|
92 |
this.child = layout; |
|
93 |
this.modal = true; |
|
94 |
} |
|
95 |
||
96 |
public Timer (string text, int time, owned ReturnCallback callback) { |
|
97 |
this.text = text; |
|
98 |
this.time = time; |
|
99 |
this.callback = (owned) callback; |
|
100 |
||
101 |
this.text_label.set_text (this.text); |
|
102 |
this.start_timer (); |
|
103 |
} |
|
104 |
||
105 |
public enum ExitCode { |
|
106 |
INVALID, |
|
107 |
OK, |
|
108 |
CANCEL; |
|
109 |
||
110 |
public string to_string () { |
|
111 |
switch (this) { |
|
112 |
case (INVALID): |
|
113 |
return "INVALID"; |
|
114 |
case (OK): |
|
115 |
return "OK"; |
|
116 |
case (CANCEL): |
|
117 |
return "CANCEL"; |
|
118 |
default: |
|
119 |
assert_not_reached (); |
|
120 |
} |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
private bool label_update () { |
|
125 |
if (_time == 0) { |
|
126 |
return_to_caller (ExitCode.OK); |
|
127 |
return false; |
|
128 |
} |
|
129 |
|
|
130 |
this._time--; |
|
131 |
||
132 |
string text = @"Time Remanining: $(this._time)"; |
|
133 |
this.timer_label.set_text (text); |
|
134 |
||
135 |
||
136 |
return true; |
|
137 |
} |
|
138 |
||
139 |
private void start_timer () { |
|
140 |
label_update (); |
|
141 |
GLib.Timeout.add_seconds (1, label_update); |
|
142 |
} |
|
143 |
||
144 |
private void return_to_caller (ExitCode e) { |
|
145 |
assert (this.callback != null); |
|
146 |
this.callback (e); |
|
147 |
this.destroy (); |
|
148 |
} |
|
149 |
||
150 |
#if TIMER_DEBUG |
|
151 |
||
152 |
static int main (string[] args) { |
|
153 |
Gtk.init (); |
|
154 |
Gtk.Application app = new Gtk.Application ("com.foobar.test", GLib.ApplicationFlags.FLAGS_NONE); |
|
155 |
app.activate.connect (() => { |
|
156 |
LO.Timer t = new LO.Timer ("Shutting down", 30, (e) => { |
|
157 |
print (@"Got: $(e)\n"); |
|
158 |
if (e == LO.Timer.ExitCode.OK) { |
|
159 |
return 0; |
|
160 |
} else { |
|
161 |
return 1; |
|
162 |
} |
|
163 |
}); |
|
164 |
t.present (); |
|
165 |
t.show (); |
|
166 |
app.add_window (t); |
|
167 |
}); |
|
168 |
||
169 |
return app.run (); |
|
170 |
} |
|
171 |
||
172 |
#endif |
|
173 |
||
174 |
}
|
|
175 |