Repository navigation
Expand file tree
/
Copy pathReplView.cpp
More file actions
288 lines (236 loc) · 6.12 KB
/
Copy pathReplView.cpp
File metadata and controls
288 lines (236 loc) · 6.12 KB
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
/*
* Copyright 2016. All rights reserved.
* Distributed under the terms of the MIT license.
*
* Author:
* Humdinger, humdingerb@gmail.com
*/
#include <stdio.h>
#include <Alert.h>
#include <Application.h>
#include <Catalog.h>
#include <Clipboard.h>
#include <ControlLook.h>
#include <Dragger.h>
#include <Font.h>
#include <LayoutBuilder.h>
#include <Roster.h>
#include <Size.h>
#include <String.h>
#include <View.h>
#include "Constants.h"
#include "ReplView.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "ClipboardMonitor"
ReplView::ReplView()
:
BView(B_TRANSLATE("Clipboard monitor"), B_WILL_DRAW | B_FULL_UPDATE_ON_RESIZE),
fReplicated(false)
{
fContentsView = new BStringView("contents", "");
fContentsView->SetAlignment(B_ALIGN_CENTER);
fContentsView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
_SetColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), B_DARKEN_1_TINT));
// Dragger
BRect rect(Bounds());
rect.left = rect.right - 7;
rect.top = rect.bottom - 7;
BDragger* dragger = new BDragger(rect, this,
B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM);
dragger->SetExplicitMinSize(BSize(7, 7));
dragger->SetExplicitMaxSize(BSize(7, 7));
dragger->SetExplicitAlignment(BAlignment(B_ALIGN_RIGHT, B_ALIGN_BOTTOM));
// Layout
font_height fheight;
fContentsView->GetFontHeight(&fheight);
SetExplicitMinSize(BSize(10.0,
(fheight.ascent + fheight.descent + fheight.leading) * 1.5));
SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
static const float spacing = be_control_look->DefaultLabelSpacing();
BLayoutBuilder::Group<>(this, B_HORIZONTAL)
.SetInsets(spacing, spacing / 2, spacing, spacing / 2.5)
.Add(fContentsView)
.Add(dragger, 0.01)
.End();
}
ReplView::ReplView(BMessage* archive)
:
BView(archive),
fReplicated(true)
{
fContentsView = dynamic_cast<BStringView *> (FindView("contents"));
fContentsView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
}
ReplView::~ReplView()
{
}
BArchivable*
ReplView::Instantiate(BMessage* data)
{
if (!validate_instantiation(data, "Clipboard monitor"))
return NULL;
return new ReplView(data);
}
status_t
ReplView::Archive(BMessage* archive, bool deep) const
{
BView::Archive(archive, deep);
archive->AddString("add_on", kApplicationSignature);
archive->AddString("class", "Clipboard monitor");
archive->PrintToStream();
return B_OK;
}
void
ReplView::AttachedToWindow()
{
BView::AttachedToWindow();
fCurrentClip = _GetClipboard().String();
TruncateClip(Bounds().Width());
// status_t error =
be_clipboard->StartWatching(this);
fContentsView->AddFilter(new BMessageFilter(B_MOUSE_DOWN,
&ReplView::_MessageFilter));
}
void
ReplView::MouseDown(BPoint point)
{
BMessage* msg = Window()->CurrentMessage();
_LaunchClipdinger(msg);
}
void
ReplView::MessageReceived(BMessage* msg)
{
if (msg->WasDropped()) {
rgb_color* color;
ssize_t size;
if (msg->FindData("RGBColor", B_RGB_COLOR_TYPE,
(const void **)&color, &size) == B_OK) {
_SetColor(*color);
return;
}
}
switch (msg->what)
{
case B_CLIPBOARD_CHANGED:
{
fCurrentClip = _GetClipboard().String();
if (fCurrentClip.Length() == 0)
break;
TruncateClip(Bounds().Width());
break;
}
case B_ABOUT_REQUESTED:
{
BString text = B_TRANSLATE_COMMENT(
"Clipdinger %version%\n"
"\twritten by Humdinger\n"
"\tCopyright %years%\n\n"
"Clipdinger provides a history of clippings of the system "
"clipboard and lets you create favorites.\n\n"
"This is its clipboard monitor that can be replicated to "
"the Desktop to always show its current contents.",
"Don't change the variables %years% and %version%.");
text.ReplaceAll("%years%", kCopyright);
text.ReplaceAll("%version%", kVersion);
BAlert* alert = new BAlert("about", text.String(),
B_TRANSLATE("Thank you"));
BTextView* view = alert->TextView();
BFont font;
view->SetStylable(true);
view->GetFont(&font);
font.SetSize(font.Size() + 4);
font.SetFace(B_BOLD_FACE);
view->SetFontAndColor(0, 11, &font);
alert->Go();
}
default:
{
BView::MessageReceived(msg);
break;
}
}
}
// #pragma mark - Member Functions
void
ReplView::TruncateClip(float width)
{
static const float spacing = be_control_look->DefaultLabelSpacing();
BString string(fCurrentClip);
TruncateString(&string, B_TRUNCATE_END, width - spacing * 8);
fContentsView->SetText(string);
}
BString
ReplView::_GetClipboard()
{
const char* text;
ssize_t textLen;
BMessage* clipboard = (BMessage *)NULL;
if (be_clipboard->Lock()) {
if ((clipboard = be_clipboard->Data()))
clipboard->FindData("text/plain", B_MIME_TYPE,
(const void **)&text, &textLen);
be_clipboard->Unlock();
}
if (text == NULL) {
text = B_TRANSLATE("-= No text in clipboard =-");
textLen = strlen(text);
}
BString clip(text, textLen);
return clip;
}
void
ReplView::_LaunchClipdinger(BMessage* msg)
{
uint32 buttons = msg->FindInt32("buttons");
int32 clicks = msg->FindInt32("clicks");
if (buttons == B_PRIMARY_MOUSE_BUTTON && clicks >= 2) {
team_id team;
team = be_roster->TeamFor(kApplicationSignature);
if (team < 0) {
be_roster->Launch(kApplicationSignature);
while (be_roster->TeamFor(kApplicationSignature) < 0)
snooze(100000);
}
team = be_roster->TeamFor(kApplicationSignature);
BMessenger messenger(kApplicationSignature, team);
if (messenger.IsValid()) {
BMessage message(ACTIVATE);
messenger.SendMessage(&message);
}
}
}
filter_result
ReplView::_MessageFilter(BMessage* msg, BHandler** target, BMessageFilter* filter)
{
ReplView* view = (ReplView *)(*target);
switch (msg->what)
{
case B_MOUSE_DOWN:
{
view->_LaunchClipdinger(msg);
return B_SKIP_MESSAGE;
}
default:
return B_DISPATCH_MESSAGE;
}
}
void
ReplView::_SetColor(rgb_color color)
{
SetViewColor(color);
fContentsView->SetViewColor(color);
float thresh = color.red + (color.green * 1.25f) + (color.blue * 0.45f);
if (thresh >= 360) {
color.red = 0;
color.green = 0;
color.blue = 0;
} else {
color.red = 255;
color.green = 255;
color.blue = 255;
}
fContentsView->SetHighColor(color);
fContentsView->SetLowColor(color);
fContentsView->Invalidate();
Invalidate();
}