Repository navigation
Expand file tree
/
Copy pathFavView.cpp
More file actions
298 lines (244 loc) · 5.83 KB
/
Copy pathFavView.cpp
File metadata and controls
298 lines (244 loc) · 5.83 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
289
290
291
292
293
294
295
296
297
298
/*
* Copyright 2015-2018. All rights reserved.
* Distributed under the terms of the MIT license.
*
* Author:
* Humdinger, [email protected]
*/
#include <Bitmap.h>
#include <Catalog.h>
#include <ControlLook.h>
#include "App.h"
#include "FavItem.h"
#include "FavView.h"
#include "Constants.h"
#include "ContextPopUp.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "ClipList"
FavView::FavView(const char* name)
:
BListView(name),
fDropRect()
{
}
FavView::~FavView()
{
}
void
FavView::AttachedToWindow()
{
SetFlags(Flags() | B_FULL_UPDATE_ON_RESIZE | B_NAVIGABLE);
BListView::AttachedToWindow();
}
void
FavView::Draw(BRect rect)
{
SetHighColor(ui_color(B_CONTROL_BACKGROUND_COLOR));
BRect bounds(Bounds());
BRect itemFrame = ItemFrame(CountItems() - 1);
bounds.top = itemFrame.bottom;
FillRect(bounds);
BListView::Draw(rect);
if (fDropRect.IsValid()) {
SetHighColor(255, 0, 0, 255);
StrokeRect(fDropRect);
}
}
bool
FavView::InitiateDrag(BPoint point, int32 dragIndex, bool wasSelected)
{
FavItem* sItem = dynamic_cast<FavItem *> (ItemAt(CurrentSelection()));
if (sItem == NULL) {
// workaround for a timing problem (see Locale prefs)
sItem = dynamic_cast<FavItem *> (ItemAt(dragIndex));
Select(dragIndex);
if (sItem == NULL)
return false;
}
BString string(sItem->GetClip());
BMessage message(B_SIMPLE_DATA);
message.AddData("text/plain", B_MIME_TYPE, string, string.Length());
int32 index = CurrentSelection();
message.AddInt32("index", index);
message.AddInt32("clipdinger_command", FAV_DRAGGED);
BRect dragRect(0.0f, 0.0f, Bounds().Width(), sItem->Height());
BBitmap* dragBitmap = new BBitmap(dragRect, B_RGB32, true);
if (dragBitmap->IsValid()) {
BView* view = new BView(dragBitmap->Bounds(), "helper", B_FOLLOW_NONE,
B_WILL_DRAW);
dragBitmap->AddChild(view);
dragBitmap->Lock();
sItem->DrawItem(view, dragRect);
view->SetHighColor(0, 0, 0, 255);
view->StrokeRect(view->Bounds());
view->Sync();
dragBitmap->Unlock();
} else {
delete dragBitmap;
dragBitmap = NULL;
}
if (dragBitmap != NULL)
DragMessage(&message, dragBitmap, B_OP_ALPHA, BPoint(0.0, 0.0));
else
DragMessage(&message, dragRect.OffsetToCopy(point), this);
return true;
}
void
FavView::MakeFocus(bool focused)
{
BListView::MakeFocus(focused);
// only signal FavView is focused when gaining focus
if (focused)
my_app->fMainWindow->SetHistoryActiveFlag(!focused);
}
void
FavView::MessageReceived(BMessage* message)
{
switch (message->what) {
case FAV_DRAGGED:
{
int32 origIndex;
int32 dropIndex;
BPoint dropPoint;
if (message->FindInt32("index", &origIndex) != B_OK)
origIndex = CountItems() - 1; // new Fav added at the bottom
dropPoint = message->DropPoint();
dropIndex = IndexOf(ConvertFromScreen(dropPoint));
if (dropIndex > origIndex)
dropIndex = dropIndex - 1;
if (dropIndex < 0)
dropIndex = CountItems() - 1; // move to bottom
MoveItem(origIndex, dropIndex);
Select(dropIndex);
RenumberFKeys();
break;
}
case POPCLOSED:
{
fShowingPopUpMenu = false;
break;
}
default:
{
BListView::MessageReceived(message);
break;
}
}
}
void
FavView::KeyDown(const char* bytes, int32 numBytes)
{
switch (bytes[0]) {
case B_DELETE:
{
BMessage message(DELETE);
Looper()->PostMessage(&message);
break;
}
case B_LEFT_ARROW:
{
if (my_app->fMainWindow->Lock())
my_app->fMainWindow->fHistory->MakeFocus(true);
my_app->fMainWindow->Unlock();
break;
}
default:
{
BListView::KeyDown(bytes, numBytes);
break;
}
}
}
void
FavView::MouseDown(BPoint position)
{
MakeFocus(true);
BRect bounds(Bounds());
BRect itemFrame = ItemFrame(CountItems() - 1);
bounds.top = itemFrame.bottom;
if (bounds.Contains(position))
return;
uint32 buttons = 0;
if (Window() != NULL && Window()->CurrentMessage() != NULL)
buttons = Window()->CurrentMessage()->FindInt32("buttons");
if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0) {
Select(IndexOf(position));
_ShowPopUpMenu(ConvertToScreen(position));
return;
}
BListView::MouseDown(position);
}
void
FavView::MouseUp(BPoint position)
{
fDropRect = BRect(-1, -1, -1, -1);
Invalidate();
BListView::MouseUp(position);
}
void
FavView::MouseMoved(BPoint where, uint32 transit, const BMessage* dragMessage)
{
if (dragMessage != NULL) {
switch (transit) {
case B_ENTERED_VIEW:
case B_INSIDE_VIEW:
{
int32 index = IndexOf(where);
if (index < 0)
index = CountItems();
fDropRect = ItemFrame(index);
if (fDropRect.IsValid()) {
fDropRect.top = fDropRect.top -1;
fDropRect.bottom = fDropRect.top + 1;
} else {
fDropRect = ItemFrame(index - 1);
if (fDropRect.IsValid())
fDropRect.top = fDropRect.bottom - 1;
else {
// empty view, show indicator at top
fDropRect = Bounds();
fDropRect.bottom = fDropRect.top + 1;
}
}
Invalidate();
break;
}
case B_EXITED_VIEW:
{
fDropRect = BRect(-1, -1, -1, -1);
Invalidate();
break;
}
}
}
BListView::MouseMoved(where, transit, dragMessage);
}
// #pragma mark - Member Functions
void
FavView::RenumberFKeys()
{
for (int32 i = 0; i < CountItems(); i++) {
FavItem* item = dynamic_cast<FavItem *>(ItemAt(i));
item->SetFavNumber(i);
}
}
void
FavView::_ShowPopUpMenu(BPoint screen)
{
if (fShowingPopUpMenu)
return;
ContextPopUp* menu = new ContextPopUp("PopUpMenu", this);
BMessage* msg = NULL;
msg = new BMessage(PASTE_SPRUNGE);
BMenuItem* item = new BMenuItem(B_TRANSLATE("Paste online"), msg, 'P');
menu->AddItem(item);
msg = new BMessage(EDIT_TITLE);
item = new BMenuItem(B_TRANSLATE("Edit title"), msg, 'E');
menu->AddItem(item);
msg = new BMessage(DELETE);
item = new BMenuItem(B_TRANSLATE("Remove"), msg);
menu->AddItem(item);
menu->SetTargetForItems(Looper());
menu->Go(screen, true, true, true);
fShowingPopUpMenu = true;
}