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()); }
// set_window_pos void set_window_pos( BWindow* window, BRect frame ) { // sanity checks: make sure window is not too big/small // and that it's not off-screen float minWidth, maxWidth, minHeight, maxHeight; window->GetSizeLimits( &minWidth, &maxWidth, &minHeight, &maxHeight ); make_sure_frame_is_within_limits( frame, minWidth, minHeight, maxWidth, maxHeight ); if ( make_sure_frame_is_on_screen( frame ) ) { window->MoveTo( frame.LeftTop() ); window->ResizeTo( frame.Width(), frame.Height() ); } }
BRect App::_GetNextWindowFrame(bool singlePackageMode) { BRect frame; const char* frameName; if (singlePackageMode) { frame = BRect(50.0, 50.0, 649.0, 349.0); frameName = "small window frame"; } else { frame = BRect(50.0, 50.0, 749.0, 549.0); frameName = "window frame"; } BMessage settings; if (_LoadSettings(settings)) { BRect windowFrame; if (settings.FindRect(frameName, &windowFrame) == B_OK) frame = windowFrame; } make_sure_frame_is_on_screen(frame); return frame; }
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::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 = "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) { errorMessage = "Failed to launch '"; BPath path(button->Ref()); if (path.InitCheck() >= B_OK) errorMessage << path.Path(); else errorMessage << button->Ref()->name; errorMessage << "'.\n\nError: "; 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) { errorMessage = "Failed to launch application with " "signature '"; errorMessage << button->AppSignature() << "'.\n\nError: "; errorMessage << strerror(ret); } else { // clear error message on success (might have been // filled when trying to launch by ref) errorMessage = ""; } } else if (!launchedByRef) { errorMessage = "Failed to launch 'something', error in " "Pad data."; } if (errorMessage.Length() > 0) { BAlert* alert = new BAlert("error", errorMessage.String(), "Bummer", NULL, NULL, B_WIDTH_FROM_WIDEST); 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", fLastID++, 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); message->FindRect("frame", &fNamePanelFrame); } else { // message comes from pad view entry_ref* ref = button->Ref(); if (ref) { BString helper("Description for '"); helper << ref->name << "'"; make_sure_frame_is_on_screen(fNamePanelFrame, this); new NamePanel(helper.String(), button->Description(), this, this, new BMessage(*message), fNamePanelFrame); } } } 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; case B_ABOUT_REQUESTED: be_app->PostMessage(message); break; default: BWindow::MessageReceived(message); break; } }