forked from humdingerb/quicklaunch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeskButton.cpp
More file actions
239 lines (189 loc) · 4.93 KB
/
DeskButton.cpp
File metadata and controls
239 lines (189 loc) · 4.93 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
/*
* Copyright 2003-2007, Haiku. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Jérôme Duval
* Jonas Sundström
*
* Simplified by Humdinger, 2017
*/
#include "DeskButton.h"
#include "QuickLaunch.h"
#include <Bitmap.h>
#include <Catalog.h>
#include <Message.h>
#include <NodeInfo.h>
#include <Path.h>
#include <Roster.h>
#include <image.h>
#define OPEN_REF 'opre'
// from QuickLaunch.cpp
extern const char* kApplicationSignature;
extern status_t our_image(image_info& image);
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Application"
DeskButton::DeskButton(BRect frame, entry_ref* ref, const char* name,
uint32 resizeMask, uint32 flags)
:
BView(frame, name, resizeMask, flags),
fRef(*ref)
{
fIcon = new BBitmap(BRect(0, 0, 15, 15), B_RGBA32);
BNodeInfo::GetTrackerIcon(&fRef, fIcon, B_MINI_ICON);
}
DeskButton::DeskButton(BMessage* message)
:
BView(message)
{
message->FindRef("ref", &fRef);
fIcon = new BBitmap(BRect(0, 0, 15, 15), B_RGBA32);
BNodeInfo::GetTrackerIcon(&fRef, fIcon, B_MINI_ICON);
}
DeskButton::DeskButton()
:
BView(BRect(0, 0, 15, 15), "QuickLaunch", B_FOLLOW_NONE, B_WILL_DRAW)
{
image_info info;
if (our_image(info) == B_OK
&& get_ref_for_path(info.name, &fRef) == B_OK) {
fIcon = new BBitmap(BRect(0, 0, 15, 15), B_RGBA32);
BNodeInfo::GetTrackerIcon(&fRef, fIcon, B_MINI_ICON);
}
}
DeskButton::~DeskButton()
{
delete fIcon;
}
// archiving overrides
DeskButton*
DeskButton::Instantiate(BMessage* data)
{
if (!validate_instantiation(data, "DeskButton"))
return NULL;
return new DeskButton(data);
}
void
DeskButton::AboutRequested()
{
BString text = B_TRANSLATE_COMMENT(
"QuickLaunch %version%\n"
"\twritten by Humdinger\n"
"\tCopyright %years%\n\n"
"QuickLaunch quickly starts any installed application. "
"Just enter the first few letters of its name and choose "
"from a list of all found programs.\n",
"Don't change the variables %years% and %version%.");
text.ReplaceAll("%version%", kVersion);
text.ReplaceAll("%years%", kCopyright);
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();
}
status_t
DeskButton::Archive(BMessage* data, bool deep) const
{
BView::Archive(data, deep);
data->AddRef("ref", &fRef);
data->AddString("add_on", kApplicationSignature);
return B_NO_ERROR;
}
void
DeskButton::AttachedToWindow()
{
BView* parent = Parent();
if (parent)
SetViewColor(parent->ViewColor());
BView::AttachedToWindow();
}
void
DeskButton::Draw(BRect rect)
{
BView::Draw(rect);
SetDrawingMode(B_OP_ALPHA);
SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
DrawBitmap(fIcon);
}
void
DeskButton::MessageReceived(BMessage* message)
{
switch (message->what) {
case B_ABOUT_REQUESTED:
{
AboutRequested();
break;
}
case OPEN_REF:
{
entry_ref ref;
message->FindRef("refs", &ref);
be_roster->Launch(&ref);
break;
}
default:
BView::MessageReceived(message);
break;
}
}
void
DeskButton::MouseDown(BPoint point)
{
uint32 mouseButtons = 0;
if (Window()->CurrentMessage() != NULL)
mouseButtons = Window()->CurrentMessage()->FindInt32("buttons");
BPoint where = ConvertToScreen(point);
if (mouseButtons & B_SECONDARY_MOUSE_BUTTON) {
_GetFavoriteList();
BPopUpMenu* menu = new BPopUpMenu("", false, false);
menu->SetFont(be_plain_font);
if (!fFavoriteList->IsEmpty()) {
for (int i = 0; i < fFavoriteList->CountItems(); i++)
{
entry_ref* favorite = static_cast<entry_ref *>
(fFavoriteList->ItemAt(i));
BMessage* message = new BMessage(OPEN_REF);
message->AddRef("refs", favorite);
menu->AddItem(new BMenuItem(favorite->name, message));
}
menu->AddSeparatorItem();
}
BMessage* message = new BMessage(OPEN_REF);
message->AddRef("refs", &fRef);
menu->AddItem(new BMenuItem(B_TRANSLATE("Open QuickLaunch"), message));
menu->AddItem(new BMenuItem(B_TRANSLATE("About QuickLaunch"),
new BMessage(B_ABOUT_REQUESTED)));
menu->SetTargetForItems(this);
menu->Go(where, true, true, BRect(where - BPoint(4, 4),
where + BPoint(4, 4)));
delete fFavoriteList;
} else if (mouseButtons & B_PRIMARY_MOUSE_BUTTON)
be_roster->Launch(&fRef);
}
void
DeskButton::_GetFavoriteList()
{
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
return;
path.Append("QuickLaunch_settings");
BFile file(path.Path(), B_READ_ONLY);
fFavoriteList = new BList();
BMessage settings;
if (file.InitCheck() == B_OK && settings.Unflatten(&file) == B_OK) {
int32 i = 0;
BString itemText;
while (settings.FindString("favorite", i++, &itemText) == B_OK) {
entry_ref favorite;
status_t err = get_ref_for_path(itemText.String(), &favorite);
if (err == B_OK)
fFavoriteList->AddItem(new entry_ref(favorite));
}
}
}