forked from humdingerb/quicklaunch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainListView.cpp
477 lines (391 loc) · 9.95 KB
/
MainListView.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Copyright 2010-2017. All rights reserved.
* Distributed under the terms of the MIT license.
*
* Author:
* Humdinger, [email protected]
*/
#include "MainListItem.h"
#include "MainListView.h"
#include "MainWindow.h"
#include "QLFilter.h"
#include "QuickLaunch.h"
#include <Catalog.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "ListView"
class PopUpMenu : public BPopUpMenu {
public:
PopUpMenu(const char* name, BMessenger target);
virtual ~PopUpMenu();
private:
BMessenger fTarget;
};
PopUpMenu::PopUpMenu(const char* name, BMessenger target)
:
BPopUpMenu(name, false, false),
fTarget(target)
{
SetAsyncAutoDestruct(true);
}
PopUpMenu::~PopUpMenu()
{
fTarget.SendMessage(POPCLOSED);
}
MainListView::MainListView()
:
BListView("ResultList"),
fShowingPopUpMenu(false),
fPrimaryButton(false),
fDropRect()
{
}
MainListView::~MainListView()
{
}
#pragma mark -- BListView Overrides --
void
MainListView::Draw(BRect rect)
{
MainWindow* window = dynamic_cast<MainWindow *> (Window());
int letters = window->GetStringLength();
float width, height;
BFont font;
if (IsEmpty()) {
SetLowColor(ui_color(B_CONTROL_BACKGROUND_COLOR));
SetHighColor(ui_color(B_CONTROL_BACKGROUND_COLOR));
FillRect(rect);
QLSettings& settings = my_app->Settings();
BString string;
if (settings.Lock()) {
if (letters == 0)
string = B_TRANSLATE("No favorites yet.");
else if (letters <= settings.GetDelay())
string = B_TRANSLATE("Use '*' as wildcards.");
else
string = B_TRANSLATE("Found no matches.");
settings.Unlock();
}
float strwidth = font.StringWidth(string);
GetPreferredSize(&width, &height);
GetFont(&font);
MovePenTo(width / 2 - strwidth / 2, height / 2 + font.Size() / 2);
SetHighColor(ui_color(B_MENU_SELECTED_BACKGROUND_COLOR));
DrawString(string.String());
} else {
SetHighColor(ui_color(B_CONTROL_BACKGROUND_COLOR));
BRect bounds(Bounds());
BRect itemFrame = ItemFrame(CountItems() - 1);
bounds.top = itemFrame.bottom;
FillRect(bounds);
}
BListView::Draw(rect);
// Only for Favorites == empty search field
if (fDropRect.IsValid() && letters == 0) {
SetHighColor(255, 0, 0, 255);
StrokeRect(fDropRect);
}
}
void
MainListView::FrameResized(float w, float h)
{
BListView::FrameResized(w, h);
for (int32 i = 0; i < CountItems(); i++) {
BListItem* item = ItemAt(i);
item->Update(this, be_plain_font);
}
Invalidate();
}
bool
MainListView::InitiateDrag(BPoint point, int32 dragIndex, bool wasSelected)
{
BPoint pt;
uint32 buttons;
GetMouse(&pt, &buttons);
if ((buttons & B_SECONDARY_MOUSE_BUTTON) != 0)
return false;
MainListItem* sItem = dynamic_cast<MainListItem *>
(ItemAt(CurrentSelection()));
if (sItem == NULL) {
// workaround for a timing problem (see Locale prefs)
sItem = dynamic_cast<MainListItem *> (ItemAt(dragIndex));
Select(dragIndex);
if (sItem == NULL)
return false;
}
entry_ref* ref = NULL;
ref = sItem->Ref();
if (ref == NULL)
return false;
BMessage message;
message.what = B_SIMPLE_DATA;
message.AddRef("refs", ref);
message.AddInt32("index", CurrentSelection());
message.AddBool("isfav", sItem->IsFavorite());
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, true);
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
MainListView::MessageReceived(BMessage* message)
{
QLSettings& settings = my_app->Settings();
switch (message->what) {
case POPCLOSED:
{
fShowingPopUpMenu = false;
break;
}
case ADDFAVORITE:
{
fShowingPopUpMenu = false;
entry_ref* ref = NULL;
MainListItem* item = NULL;
int selection = CurrentSelection();
item = dynamic_cast<MainListItem *> (ItemAt(selection));
if (item->IsFavorite())
break;
item->SetFavorite(true);
if (item)
ref = item->Ref();
if (ref) {
bool duplicate = false;
if (settings.Lock()) {
for (int i = 0; i < settings.fFavoriteList->CountItems(); i++)
{
entry_ref* favorite = static_cast<entry_ref *>
(settings.fFavoriteList->ItemAt(i));
if (*ref == *favorite)
duplicate = true;
}
if (!duplicate)
settings.fFavoriteList->AddItem(ref);
settings.Unlock();
}
}
Invalidate();
break;
}
case REMOVEFAVORITE:
{
fShowingPopUpMenu = false;
entry_ref* ref = NULL;
MainListItem* item = NULL;
int selection = CurrentSelection();
item = dynamic_cast<MainListItem *> (ItemAt(selection));
if (!item->IsFavorite())
break;
MainWindow* window = dynamic_cast<MainWindow *> (Window());
int letters = window->GetStringLength();
if (item) {
ref = item->Ref();
item->SetFavorite(false);
if (letters == 0) { // don't remove from result list
RemoveItem(selection);
Select((selection - 1 < 0) ? 0 : selection - 1);
}
}
if (ref) {
if (settings.Lock()) {
for (int i = 0; i < settings.fFavoriteList->CountItems(); i++)
{
entry_ref* favorite = static_cast<entry_ref *>
(settings.fFavoriteList->ItemAt(i));
if (*ref == *favorite)
settings.fFavoriteList->RemoveItem(i);
}
settings.Unlock();
}
}
Invalidate();
window->ResizeWindow();
break;
}
case ADDIGNORE:
{
fShowingPopUpMenu = false;
entry_ref* ref = NULL;
MainListItem* item = NULL;
int selection = CurrentSelection();
item = dynamic_cast<MainListItem *> (ItemAt(selection));
if (item->IsFavorite())
break;
if (item)
ref = item->Ref();
if (ref) {
BMessenger msgr(my_app->fSetupWindow);
BMessage refMsg(B_REFS_RECEIVED);
refMsg.AddRef("refs",ref);
msgr.SendMessage(&refMsg);
}
break;
}
case OPENLOCATION:
{
fShowingPopUpMenu = false;
BMessenger msgr(Window());
BMessage refMsg(RETURN_CTRL_KEY);
msgr.SendMessage(&refMsg);
break;
}
case B_SIMPLE_DATA:
{
bool fav = false;
if (message->FindBool("isfav", &fav) != B_OK)
break;
if (!fav)
break;
// see if we're dragging a Favorite in a result list
if (my_app->fMainWindow->GetStringLength() > 0)
break;
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);
if (settings.Lock()) {
settings.fFavoriteList->MoveItem(origIndex, dropIndex);
settings.Unlock();
}
break;
}
default:
BView::MessageReceived(message);
break;
}
}
void
MainListView::MouseDown(BPoint position)
{
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) {
Select(IndexOf(position));
_ShowPopUpMenu(ConvertToScreen(position));
fPrimaryButton = false;
Invalidate();
return;
}
if (buttons == B_PRIMARY_MOUSE_BUTTON) {
fCurrentItemIndex = IndexOf(position);
fPrimaryButton = true;
}
BListView::MouseDown(position);
}
void
MainListView::MouseUp(BPoint position)
{
if ((fCurrentItemIndex == IndexOf(position) && fPrimaryButton == true)) {
BMessenger msgr(Window());
BMessage refMsg(SINGLE_CLICK);
msgr.SendMessage(&refMsg);
fPrimaryButton = false;
fCurrentItemIndex = -1;
}
fDropRect = BRect(-1, -1, -1, -1);
Invalidate();
BListView::MouseUp(position);
}
void
MainListView::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 -- Private Methods --
void
MainListView::_ShowPopUpMenu(BPoint screen)
{
if (fShowingPopUpMenu || IsEmpty())
return;
MainListItem* sItem = dynamic_cast<MainListItem *>
(ItemAt(CurrentSelection()));
bool isFav = false;
if (sItem != NULL)
isFav = sItem->IsFavorite();
PopUpMenu* menu = new PopUpMenu("PopUpMenu", this);
BMenuItem* item;
if (isFav) {
item = new BMenuItem(B_TRANSLATE("Remove favorite"),
new BMessage(REMOVEFAVORITE), 'R');
} else {
item = new BMenuItem(B_TRANSLATE("Add to favorites"),
new BMessage(ADDFAVORITE), 'F');
}
menu->AddItem(item);
if (!isFav) {
item = new BMenuItem(B_TRANSLATE("Add to ignore list"),
new BMessage(ADDIGNORE), 'I');
menu->AddItem(item);
}
item = new BMenuItem(B_TRANSLATE("Open containing folder"),
new BMessage(OPENLOCATION));
menu->AddItem(item);
menu->SetTargetForItems(this);
menu->Go(screen, true, true, true);
fShowingPopUpMenu = true;
}