-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainWindow.cpp
190 lines (164 loc) · 4 KB
/
MainWindow.cpp
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
#include "MainWindow.h"
#include <Application.h>
#include <Button.h>
#include <StringView.h>
#include <View.h>
#include <String.h>
#include <be/kernel/OS.h>
#include <unistd.h>
bool clockStart = false;
bool clockReset = false;
BString displayClock;
BString allZeros = "00:00:00.0";
BStringView *clockStringView;
BButton *startButton;
BButton *stopButton;
BButton *resetButton;
enum
{
M_BUTTON_START = 'btn1',
M_BUTTON_STOP = 'btn2',
M_BUTTON_RESET = 'btn3',
M_UPDATE_CLOCK = 'uclk'
};
MainWindow::MainWindow(void)
: BWindow(BRect(100,100,324,200), "StopWatch", B_TITLED_WINDOW,
B_NOT_ZOOMABLE | B_NOT_RESIZABLE |
B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE)
{
BView *mainView = new BView(Bounds(), "mainView", B_FOLLOW_ALL, B_WILL_DRAW);
clockStringView = new BStringView(BRect(10,10,254,55), "clockStringView", NULL);
startButton = new BButton(BRect(9,65,87,90), "startButton", "Start",
new BMessage(M_BUTTON_START));
stopButton = new BButton(BRect(93,65,171,90), "stopButton", "Stop",
new BMessage(M_BUTTON_STOP));
resetButton = new BButton(BRect(177,65,255,90), "resetButton", "Reset",
new BMessage(M_BUTTON_RESET));
mainView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
AddChild(mainView);
mainView->AddChild(clockStringView);
mainView->AddChild(startButton);
mainView->AddChild(stopButton);
mainView->AddChild(resetButton);
BFont font(be_plain_font);
font.SetSize(40.0);
font.SetFamilyAndStyle("DejaVu Sans Mono", "Book");
// font.SetFamilyAndStyle("DSEG7 Classic", "Regular");
clockStringView->SetFont(&font);
clockStringView->SetAlignment(B_ALIGN_CENTER);
clockStringView->SetViewColor(0,0,0);
clockStringView->SetHighColor(255,255,255);
clockStringView->SetText(allZeros.String());
startButton->MakeFocus(true);
stopButton->SetEnabled(false);
stopwatch = new BStopWatch("stopwatch", true);
stopwatch->Reset();
stopwatch->Suspend();
}
void
MainWindow::MessageReceived(BMessage *msg)
{
switch (msg->what)
{
case M_BUTTON_START:
{
Start();
break;
}
case M_BUTTON_STOP:
{
Stop();
break;
}
case M_BUTTON_RESET:
{
Reset();
break;
}
case M_UPDATE_CLOCK:
{
UpdateClock();
break;
}
default:
{
BWindow::MessageReceived(msg);
break;
}
}
}
bool
MainWindow::QuitRequested(void)
{
be_app->PostMessage(B_QUIT_REQUESTED);
return true;
}
int
MainWindow::DefaultButtonState(void)
{
startButton->SetEnabled(true);
startButton->MakeFocus(true);
stopButton->SetEnabled(false);
return(0);
}
void
MainWindow::Start(void) {
message_runner = new BMessageRunner(this, M_UPDATE_CLOCK, 100000, -1);
stopwatch->Resume();
clockStart = true;
clockReset = false;
startButton->SetEnabled(false);
stopButton->SetEnabled(true);
stopButton->MakeFocus(true);
}
void
MainWindow::Stop(void) {
if (message_runner) {
delete message_runner;
message_runner = NULL;
}
stopwatch->Suspend();
clockStart = false;
clockReset = false;
DefaultButtonState();
}
void
MainWindow::Reset() {
if (message_runner) {
delete message_runner;
message_runner = NULL;
}
stopwatch->Reset();
stopwatch->Suspend();
clockStart = false;
clockReset = true;
UpdateClock();
DefaultButtonState();
}
int
MainWindow::UpdateClock(void)
{
int totalSeconds = 0;
int totalMinutes = 0;
int totalHours = 0;
bigtime_t elapsedMicroseconds = stopwatch->ElapsedTime();
totalSeconds = elapsedMicroseconds / 1000000;
totalMinutes = totalSeconds / 60;
totalHours = totalMinutes / 60;
totalHours %= 24;
int H1, H2, M1, M2, S1, S2, TS;
H1 = ((totalHours % 60) / 10);
H2 = ((totalHours % 60) % 10);
M1 = ((totalMinutes % 60) / 10);
M2 = ((totalMinutes % 60) % 10);
S1 = ((totalSeconds % 60) / 10);
S2 = ((totalSeconds % 60) % 10);
TS = ((elapsedMicroseconds % 1000000) / 100000);
displayClock = "";
displayClock << H1 << H2 << ":" << M1 << M2 << ":" << S1 << S2 << "." << TS;
clockStringView->SetText(displayClock.String());
if (clockReset) {
clockStringView->SetText(allZeros.String());
}
return(0);
}