-
-
Notifications
You must be signed in to change notification settings - Fork 337
/
MainWindow.cpp
634 lines (559 loc) · 16.9 KB
/
MainWindow.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/*
* Copyright 2006 - 2011, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "MainWindow.h"
#include <stdio.h>
#include <Alert.h>
#include <Application.h>
#include <Catalog.h>
#include <GroupLayout.h>
#include <Menu.h>
#include <MenuItem.h>
#include <Messenger.h>
#include <Path.h>
#include <Roster.h>
#include <Screen.h>
#include "support.h"
#include "App.h"
#include "LaunchButton.h"
#include "NamePanel.h"
#include "PadView.h"
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "LaunchBox"
MainWindow::MainWindow(const char* name, BRect frame, bool addDefaultButtons)
:
BWindow(frame, name, B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE
| B_WILL_ACCEPT_FIRST_CLICK | B_NO_WORKSPACE_ACTIVATION
| B_AUTO_UPDATE_SIZE_LIMITS | B_SAME_POSITION_IN_ALL_WORKSPACES,
B_ALL_WORKSPACES),
fSettings(new BMessage('sett')),
fPadView(new PadView("pad view")),
fAutoRaise(false),
fShowOnAllWorkspaces(true)
{
bool buttonsAdded = false;
if (load_settings(fSettings, "main_settings", "LaunchBox") >= B_OK)
buttonsAdded = LoadSettings(fSettings);
if (!buttonsAdded) {
if (addDefaultButtons)
_AddDefaultButtons();
else
_AddEmptyButtons();
}
SetLayout(new BGroupLayout(B_HORIZONTAL));
AddChild(fPadView);
}
MainWindow::MainWindow(const char* name, BRect frame, BMessage* settings)
:
BWindow(frame, name,
B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE
| B_WILL_ACCEPT_FIRST_CLICK | B_NO_WORKSPACE_ACTIVATION
| B_AUTO_UPDATE_SIZE_LIMITS | B_SAME_POSITION_IN_ALL_WORKSPACES,
B_ALL_WORKSPACES),
fSettings(settings),
fPadView(new PadView("pad view")),
fAutoRaise(false),
fShowOnAllWorkspaces(true)
{
if (!LoadSettings(settings))
_AddEmptyButtons();
SetLayout(new BGroupLayout(B_HORIZONTAL));
AddChild(fPadView);
}
MainWindow::~MainWindow()
{
delete fSettings;
}
bool
MainWindow::QuitRequested()
{
int32 padWindowCount = 0;
for (int32 i = 0; BWindow* window = be_app->WindowAt(i); i++) {
if (dynamic_cast<MainWindow*>(window))
padWindowCount++;
}
bool canClose = true;
if (padWindowCount == 1) {
be_app->PostMessage(B_QUIT_REQUESTED);
canClose = false;
} else {
BAlert* alert = new BAlert(B_TRANSLATE("last chance"),
B_TRANSLATE("Really close this pad?\n"
"(The pad will not be remembered.)"),
B_TRANSLATE("Close"), B_TRANSLATE("Cancel"), NULL);
alert->SetShortcut(1, B_ESCAPE);
if (alert->Go() == 1)
canClose = false;
}
return canClose;
}
void
MainWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case MSG_LAUNCH: {
BView* pointer;
if (message->FindPointer("be:source", (void**)&pointer) < B_OK)
break;
LaunchButton* button = dynamic_cast<LaunchButton*>(pointer);
if (button == NULL)
break;
BString errorMessage;
bool launchedByRef = false;
if (button->Ref()) {
BEntry entry(button->Ref(), true);
if (entry.IsDirectory()) {
// open in Tracker
BMessenger messenger("application/x-vnd.Be-TRAK");
if (messenger.IsValid()) {
BMessage trackerMessage(B_REFS_RECEIVED);
trackerMessage.AddRef("refs", button->Ref());
status_t ret = messenger.SendMessage(&trackerMessage);
if (ret < B_OK) {
errorMessage = B_TRANSLATE("Failed to send "
"'open folder' command to Tracker.\n\nError: ");
errorMessage << strerror(ret);
} else
launchedByRef = true;
} else
errorMessage = ("Failed to open folder - is Tracker "
"running?");
} else {
status_t ret = be_roster->Launch(button->Ref());
if (ret < B_OK && ret != B_ALREADY_RUNNING) {
BString errStr(B_TRANSLATE("Failed to launch '%1'.\n"
"\nError:"));
BPath path(button->Ref());
if (path.InitCheck() >= B_OK)
errStr.ReplaceFirst("%1", path.Path());
else
errStr.ReplaceFirst("%1", button->Ref()->name);
errorMessage << errStr.String() << " ";
errorMessage << strerror(ret);
} else
launchedByRef = true;
}
}
if (!launchedByRef && button->AppSignature()) {
status_t ret = be_roster->Launch(button->AppSignature());
if (ret != B_OK && ret != B_ALREADY_RUNNING) {
BString errStr(B_TRANSLATE("\n\nFailed to launch application "
"with signature '%2'.\n\nError:"));
errStr.ReplaceFirst("%2", button->AppSignature());
errorMessage << errStr.String() << " ";
errorMessage << strerror(ret);
} else {
// clear error message on success (might have been
// filled when trying to launch by ref)
errorMessage = "";
}
} else if (!launchedByRef) {
errorMessage = B_TRANSLATE("Failed to launch 'something', "
"error in Pad data.");
}
if (errorMessage.Length() > 0) {
BAlert* alert = new BAlert("error", errorMessage.String(),
B_TRANSLATE("Bummer"), NULL, NULL, B_WIDTH_FROM_WIDEST);
alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE);
alert->Go(NULL);
}
break;
}
case MSG_ADD_SLOT: {
LaunchButton* button;
if (message->FindPointer("be:source", (void**)&button) >= B_OK) {
fPadView->AddButton(new LaunchButton("launch button",
NULL, new BMessage(MSG_LAUNCH)), button);
}
break;
}
case MSG_CLEAR_SLOT: {
LaunchButton* button;
if (message->FindPointer("be:source", (void**)&button) >= B_OK)
button->SetTo((entry_ref*)NULL);
break;
}
case MSG_REMOVE_SLOT: {
LaunchButton* button;
if (message->FindPointer("be:source", (void**)&button) >= B_OK) {
if (fPadView->RemoveButton(button))
delete button;
}
break;
}
case MSG_SET_DESCRIPTION: {
LaunchButton* button;
if (message->FindPointer("be:source", (void**)&button) >= B_OK) {
const char* name;
if (message->FindString("name", &name) >= B_OK) {
// message comes from a previous name panel
button->SetDescription(name);
BRect namePanelFrame;
if (message->FindRect("frame", &namePanelFrame) == B_OK) {
((App*)be_app)->SetNamePanelSize(
namePanelFrame.Size());
}
} else {
// message comes from pad view
entry_ref* ref = button->Ref();
if (ref) {
BString helper(B_TRANSLATE("Description for '%3'"));
helper.ReplaceFirst("%3", ref->name);
// Place the name panel besides the pad, but give it
// the user configured size.
BPoint origin = B_ORIGIN;
BSize size = ((App*)be_app)->NamePanelSize();
NamePanel* panel = new NamePanel(helper.String(),
button->Description(), this, this,
new BMessage(*message), size);
panel->Layout(true);
size = panel->Frame().Size();
BScreen screen(this);
BPoint mousePos;
uint32 buttons;
fPadView->GetMouse(&mousePos, &buttons, false);
fPadView->ConvertToScreen(&mousePos);
if (fPadView->Orientation() == B_HORIZONTAL) {
// Place above or below the pad
origin.x = mousePos.x - size.width / 2;
if (screen.Frame().bottom - Frame().bottom
> size.height + 20) {
origin.y = Frame().bottom + 10;
} else {
origin.y = Frame().top - 10 - size.height;
}
} else {
// Place left or right of the pad
origin.y = mousePos.y - size.height / 2;
if (screen.Frame().right - Frame().right
> size.width + 20) {
origin.x = Frame().right + 10;
} else {
origin.x = Frame().left - 10 - size.width;
}
}
panel->MoveTo(origin);
panel->Show();
}
}
}
break;
}
case MSG_ADD_WINDOW: {
BMessage settings('sett');
SaveSettings(&settings);
message->AddMessage("window", &settings);
be_app->PostMessage(message);
break;
}
case MSG_SHOW_BORDER:
SetLook(B_TITLED_WINDOW_LOOK);
break;
case MSG_HIDE_BORDER:
SetLook(B_BORDERED_WINDOW_LOOK);
break;
case MSG_TOGGLE_AUTORAISE:
ToggleAutoRaise();
break;
case MSG_SHOW_ON_ALL_WORKSPACES:
fShowOnAllWorkspaces = !fShowOnAllWorkspaces;
if (fShowOnAllWorkspaces)
SetWorkspaces(B_ALL_WORKSPACES);
else
SetWorkspaces(1L << current_workspace());
break;
case B_SIMPLE_DATA:
case B_REFS_RECEIVED:
case B_PASTE:
case B_MODIFIERS_CHANGED:
break;
default:
BWindow::MessageReceived(message);
break;
}
}
void
MainWindow::Show()
{
BWindow::Show();
_GetLocation();
}
void
MainWindow::ScreenChanged(BRect frame, color_space format)
{
_AdjustLocation(Frame());
}
void
MainWindow::WorkspaceActivated(int32 workspace, bool active)
{
if (fShowOnAllWorkspaces) {
if (!active)
_GetLocation();
else
_AdjustLocation(Frame());
}
}
void
MainWindow::FrameMoved(BPoint origin)
{
if (IsActive()) {
_GetLocation();
_NotifySettingsChanged();
}
}
void
MainWindow::FrameResized(float width, float height)
{
if (IsActive()) {
_GetLocation();
_NotifySettingsChanged();
}
BWindow::FrameResized(width, height);
}
void
MainWindow::ToggleAutoRaise()
{
fAutoRaise = !fAutoRaise;
if (fAutoRaise)
fPadView->SetEventMask(B_POINTER_EVENTS, B_NO_POINTER_HISTORY);
else
fPadView->SetEventMask(0);
_NotifySettingsChanged();
}
bool
MainWindow::LoadSettings(const BMessage* message)
{
// restore window positioning
BPoint point;
bool useAdjust = false;
if (message->FindPoint("window position", &point) == B_OK) {
fScreenPosition = point;
useAdjust = true;
}
float borderDist;
if (message->FindFloat("border distance", &borderDist) == B_OK) {
fBorderDist = borderDist;
}
// restore window frame
BRect frame;
if (message->FindRect("window frame", &frame) == B_OK) {
if (useAdjust) {
_AdjustLocation(frame);
} else {
make_sure_frame_is_on_screen(frame, this);
MoveTo(frame.LeftTop());
ResizeTo(frame.Width(), frame.Height());
}
}
// restore window look
window_look look;
if (message->FindInt32("window look", (int32*)&look) == B_OK)
SetLook(look);
// restore orientation
int32 orientation;
if (message->FindInt32("orientation", &orientation) == B_OK)
fPadView->SetOrientation((enum orientation)orientation);
// restore icon size
int32 iconSize;
if (message->FindInt32("icon size", &iconSize) == B_OK)
fPadView->SetIconSize(iconSize);
// restore ignore double click
bool ignoreDoubleClick;
if (message->FindBool("ignore double click", &ignoreDoubleClick) == B_OK)
fPadView->SetIgnoreDoubleClick(ignoreDoubleClick);
// restore buttons
const char* path;
bool buttonAdded = false;
for (int32 i = 0; message->FindString("path", i, &path) >= B_OK; i++) {
LaunchButton* button = new LaunchButton("launch button",
NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
BString signature;
if (message->FindString("signature", i, &signature) >= B_OK
&& signature.CountChars() > 0) {
button->SetTo(signature.String(), true);
}
BEntry entry(path, true);
entry_ref ref;
if (entry.Exists() && entry.GetRef(&ref) == B_OK)
button->SetTo(&ref);
const char* text;
if (message->FindString("description", i, &text) >= B_OK)
button->SetDescription(text);
buttonAdded = true;
}
// restore auto raise setting
bool autoRaise;
if (message->FindBool("auto raise", &autoRaise) == B_OK && autoRaise)
ToggleAutoRaise();
// store workspace setting
bool showOnAllWorkspaces;
if (message->FindBool("all workspaces", &showOnAllWorkspaces) == B_OK)
fShowOnAllWorkspaces = showOnAllWorkspaces;
SetWorkspaces(showOnAllWorkspaces
? B_ALL_WORKSPACES : 1L << current_workspace());
if (!fShowOnAllWorkspaces) {
uint32 workspaces;
if (message->FindInt32("workspaces", (int32*)&workspaces) == B_OK)
SetWorkspaces(workspaces);
}
return buttonAdded;
}
void
MainWindow::SaveSettings(BMessage* message)
{
// make sure the positioning info is correct
_GetLocation();
// store window position
if (message->ReplacePoint("window position", fScreenPosition) != B_OK)
message->AddPoint("window position", fScreenPosition);
if (message->ReplaceFloat("border distance", fBorderDist) != B_OK)
message->AddFloat("border distance", fBorderDist);
// store window frame and look
if (message->ReplaceRect("window frame", Frame()) != B_OK)
message->AddRect("window frame", Frame());
if (message->ReplaceInt32("window look", Look()) != B_OK)
message->AddInt32("window look", Look());
// store orientation
if (message->ReplaceInt32("orientation",
(int32)fPadView->Orientation()) != B_OK)
message->AddInt32("orientation", (int32)fPadView->Orientation());
// store icon size
if (message->ReplaceInt32("icon size", fPadView->IconSize()) != B_OK)
message->AddInt32("icon size", fPadView->IconSize());
// store ignore double click
if (message->ReplaceBool("ignore double click",
fPadView->IgnoreDoubleClick()) != B_OK) {
message->AddBool("ignore double click", fPadView->IgnoreDoubleClick());
}
// store buttons
message->RemoveName("path");
message->RemoveName("description");
message->RemoveName("signature");
for (int32 i = 0; LaunchButton* button = fPadView->ButtonAt(i); i++) {
BPath path(button->Ref());
if (path.InitCheck() >= B_OK)
message->AddString("path", path.Path());
else
message->AddString("path", "");
message->AddString("description", button->Description());
if (button->AppSignature())
message->AddString("signature", button->AppSignature());
else
message->AddString("signature", "");
}
// store auto raise setting
if (message->ReplaceBool("auto raise", fAutoRaise) != B_OK)
message->AddBool("auto raise", fAutoRaise);
// store workspace setting
if (message->ReplaceBool("all workspaces", fShowOnAllWorkspaces) != B_OK)
message->AddBool("all workspaces", fShowOnAllWorkspaces);
if (message->ReplaceInt32("workspaces", Workspaces()) != B_OK)
message->AddInt32("workspaces", Workspaces());
}
void
MainWindow::_GetLocation()
{
BRect frame = Frame();
BPoint origin = frame.LeftTop();
BPoint center(origin.x + frame.Width() / 2.0,
origin.y + frame.Height() / 2.0);
BScreen screen(this);
BRect screenFrame = screen.Frame();
fScreenPosition.x = center.x / screenFrame.Width();
fScreenPosition.y = center.y / screenFrame.Height();
if (fabs(0.5 - fScreenPosition.x) > fabs(0.5 - fScreenPosition.y)) {
// nearest to left or right border
if (fScreenPosition.x < 0.5)
fBorderDist = frame.left - screenFrame.left;
else
fBorderDist = screenFrame.right - frame.right;
} else {
// nearest to top or bottom border
if (fScreenPosition.y < 0.5)
fBorderDist = frame.top - screenFrame.top;
else
fBorderDist = screenFrame.bottom - frame.bottom;
}
}
void
MainWindow::_AdjustLocation(BRect frame)
{
BScreen screen(this);
BRect screenFrame = screen.Frame();
BPoint center(fScreenPosition.x * screenFrame.Width(),
fScreenPosition.y * screenFrame.Height());
BPoint frameCenter(frame.left + frame.Width() / 2.0,
frame.top + frame.Height() / 2.0);
frame.OffsetBy(center - frameCenter);
// ignore border dist when distance too large
if (fBorderDist < 10.0) {
// see which border we mean depending on screen position
BPoint offset(0.0, 0.0);
if (fabs(0.5 - fScreenPosition.x) > fabs(0.5 - fScreenPosition.y)) {
// left or right border
if (fScreenPosition.x < 0.5)
offset.x = (screenFrame.left + fBorderDist) - frame.left;
else
offset.x = (screenFrame.right - fBorderDist) - frame.right;
} else {
// top or bottom border
if (fScreenPosition.y < 0.5)
offset.y = (screenFrame.top + fBorderDist) - frame.top;
else
offset.y = (screenFrame.bottom - fBorderDist) - frame.bottom;
}
frame.OffsetBy(offset);
}
make_sure_frame_is_on_screen(frame, this);
MoveTo(frame.LeftTop());
ResizeTo(frame.Width(), frame.Height());
}
void
MainWindow::_AddDefaultButtons()
{
// Mail
LaunchButton* button = new LaunchButton("launch button", NULL,
new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button->SetTo("application/x-vnd.Be-MAIL", true);
// StyledEdit
button = new LaunchButton("launch button", NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button->SetTo("application/x-vnd.Haiku-StyledEdit", true);
// ShowImage
button = new LaunchButton("launch button", NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button->SetTo("application/x-vnd.Haiku-ShowImage", true);
// MediaPlayer
button = new LaunchButton("launch button", NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button->SetTo("application/x-vnd.Haiku-MediaPlayer", true);
// DeskCalc
button = new LaunchButton("launch button", NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button->SetTo("application/x-vnd.Haiku-DeskCalc", true);
// Terminal
button = new LaunchButton("launch button", NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button->SetTo("application/x-vnd.Haiku-Terminal", true);
}
void
MainWindow::_AddEmptyButtons()
{
LaunchButton* button = new LaunchButton("launch button", NULL,
new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button = new LaunchButton("launch button", NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
button = new LaunchButton("launch button", NULL, new BMessage(MSG_LAUNCH));
fPadView->AddButton(button);
}
void
MainWindow::_NotifySettingsChanged()
{
be_app->PostMessage(MSG_SETTINGS_CHANGED);
}