-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeskbarControlView.cpp
250 lines (197 loc) · 4.73 KB
/
DeskbarControlView.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
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
#include "BSCApp.h"
#include "Controller.h"
#include "ControllerObserver.h"
#include "DeskbarControlView.h"
#include "messages.h"
#include <Bitmap.h>
#include <Entry.h>
#include <MenuItem.h>
#include <Message.h>
#include <NodeInfo.h>
#include <PopUpMenu.h>
#include <Roster.h>
#include <stdio.h>
#include <string.h>
enum {
BSC_STOP,
BSC_START,
BSC_PAUSE_RESUME
};
class BSCMenuItem : public BMenuItem {
public:
BSCMenuItem(uint32 action, BMessage *message);
virtual void DrawContent();
private:
const char *ActionToString(uint32 action);
uint32 fAction;
BBitmap *fMenuIcon;
};
const static char* kControllerMessengerName = "controller_messenger";
DeskbarControlView::DeskbarControlView(BRect rect, const char *name)
:
BView(rect, name, B_FOLLOW_LEFT|B_FOLLOW_TOP, B_WILL_DRAW)
{
InitData();
fAppMessenger = BMessenger(kAppSignature);
fControllerMessenger = BMessenger(gControllerLooper);
}
DeskbarControlView::DeskbarControlView(BMessage *data)
:
BView(data)
{
InitData();
fAppMessenger = BMessenger(kAppSignature);
data->FindMessenger(kControllerMessengerName, &fControllerMessenger);
}
DeskbarControlView::~DeskbarControlView()
{
delete fBitmap;
delete fRecordingBitmap;
}
DeskbarControlView*
DeskbarControlView::Instantiate(BMessage *archive)
{
if (!validate_instantiation(archive, "DeskbarControlView"))
return NULL;
return new DeskbarControlView(archive);
}
status_t
DeskbarControlView::Archive(BMessage *message, bool deep) const
{
status_t status = BView::Archive(message, deep);
if (status != B_OK)
return status;
status = message->AddString("add_on", kAppSignature);
if (status != B_OK)
return status;
status = message->AddMessenger(kControllerMessengerName,
fControllerMessenger);
return status;
}
void
DeskbarControlView::AttachedToWindow()
{
SetViewColor(Parent()->ViewColor());
if (LockLooper()) {
StartWatching(fControllerMessenger, kMsgControllerCaptureStarted);
StartWatching(fControllerMessenger, kMsgControllerCaptureFinished);
UnlockLooper();
}
}
void
DeskbarControlView::DetachedFromWindow()
{
if (LockLooper()) {
StopWatching(fControllerMessenger, kMsgControllerCaptureStarted);
StopWatching(fControllerMessenger, kMsgControllerCaptureFinished);
UnlockLooper();
}
}
void
DeskbarControlView::Draw(BRect rect)
{
SetDrawingMode(B_OP_OVER);
DrawBitmap(fBitmap);
if (fRecording) {
SetDrawingMode(B_OP_COPY);
SetHighColor(250, 0, 0);
FillRect(BRect(2, 12, 5, 15));
}
}
void
DeskbarControlView::MessageReceived(BMessage *message)
{
switch (message->what) {
case kMsgGUIToggleCapture:
case kPauseResumeCapture:
if (fAppMessenger.IsValid())
fAppMessenger.SendMessage(message->what);
break;
case B_OBSERVER_NOTICE_CHANGE:
{
int32 code;
message->FindInt32("be:observe_change_what", &code);
switch (code) {
case kMsgControllerCaptureStarted:
fRecording = true;
Invalidate();
break;
case kMsgControllerCaptureFinished:
fRecording = false;
Invalidate();
break;
default:
break;
}
}
default:
BView::MessageReceived(message);
break;
}
}
void
DeskbarControlView::MouseDown(BPoint where)
{
BPopUpMenu *menu = new BPopUpMenu("menu");
BMessage *menuMessage = new BMessage(kMsgGUIToggleCapture);
if (fRecording) {
menu->AddItem(new BSCMenuItem(BSC_STOP, menuMessage));
menu->AddItem(new BSCMenuItem(BSC_PAUSE_RESUME,
new BMessage(kPauseResumeCapture)));
} else
menu->AddItem(new BSCMenuItem(BSC_START, menuMessage));
menu->SetTargetForItems(this);
ConvertToScreen(&where);
menu->Go(where, true, false, true);
}
void
DeskbarControlView::InitData()
{
fBitmap = NULL;
fRecordingBitmap = NULL;
fRecording = false;
app_info info;
be_roster->GetAppInfo(kAppSignature, &info);
fBitmap = new BBitmap(BRect(0, 0, 15, 15), B_COLOR_8_BIT);
BNodeInfo::GetTrackerIcon(&info.ref, fBitmap, B_MINI_ICON);
}
BSCMenuItem::BSCMenuItem(uint32 action, BMessage *message)
:
BMenuItem(ActionToString(action), message),
fAction(action),
fMenuIcon(NULL)
{
}
void
BSCMenuItem::DrawContent()
{
Menu()->SetFontSize(10);
if (fAction == BSC_START) {
BPoint drawPoint(ContentLocation());
drawPoint.x += 20;
Menu()->MovePenTo(drawPoint);
BMenuItem::DrawContent();
BRect imageRect;
imageRect.SetLeftTop(ContentLocation());
imageRect.top += 2;
imageRect.right = imageRect.left + 10;
imageRect.bottom = Frame().bottom - 2;
Menu()->SetHighColor(248, 0, 0);
Menu()->FillEllipse(imageRect);
} else
BMenuItem::DrawContent();
}
const char *
BSCMenuItem::ActionToString(uint32 action)
{
switch (action) {
case BSC_START:
return "Start recording";
case BSC_STOP:
return "Stop recording";
case BSC_PAUSE_RESUME:
return "Pause/Resume";
default:
return "No Action";
}
}