diff --git a/src/preferences/backgrounds/BackgroundsView.cpp b/src/preferences/backgrounds/BackgroundsView.cpp index 4f49b40c6a..0435d1332e 100644 --- a/src/preferences/backgrounds/BackgroundsView.cpp +++ b/src/preferences/backgrounds/BackgroundsView.cpp @@ -73,6 +73,7 @@ BackgroundsView::BackgroundsView() fCurrent(NULL), fCurrentInfo(NULL), fLastImageIndex(-1), + fRecentFoldersLimit(10), fPathList(1, true), fImageList(1, true), fFoundPositionSetting(false) @@ -147,8 +148,6 @@ BackgroundsView::BackgroundsView() B_TRANSLATE("Current workspace"), new BMessage(kMsgCurrentWorkspace))); menuItem->SetMarked(true); - fLastWorkspaceIndex = - fWorkspaceMenu->IndexOf(fWorkspaceMenu->FindMarked()); fWorkspaceMenu->AddSeparatorItem(); fWorkspaceMenu->AddItem(new BMenuItem(B_TRANSLATE("Default folder"), new BMessage(kMsgDefaultFolder))); @@ -352,8 +351,6 @@ BackgroundsView::MessageReceived(BMessage* message) case kMsgCurrentWorkspace: case kMsgAllWorkspaces: fImageMenu->FindItem(kMsgNoImage)->SetLabel(B_TRANSLATE("None")); - fLastWorkspaceIndex = fWorkspaceMenu->IndexOf( - fWorkspaceMenu->FindMarked()); if (fCurrent && fCurrent->IsDesktop()) { _UpdateButtons(); } else { @@ -364,8 +361,6 @@ BackgroundsView::MessageReceived(BMessage* message) case kMsgDefaultFolder: fImageMenu->FindItem(kMsgNoImage)->SetLabel(B_TRANSLATE("None")); - fLastWorkspaceIndex = fWorkspaceMenu->IndexOf( - fWorkspaceMenu->FindMarked()); _SetDesktop(false); _LoadDefaultFolder(); break; @@ -388,10 +383,6 @@ BackgroundsView::MessageReceived(BMessage* message) _FindImageItem(fLastImageIndex)->SetMarked(true); else fImageMenu->ItemAt(0)->SetMarked(true); - } else if (pointer == fFolderPanel) { - if (fLastWorkspaceIndex >= 0) - fWorkspaceMenu->ItemAt(fLastWorkspaceIndex) - ->SetMarked(true); } break; } @@ -405,15 +396,16 @@ BackgroundsView::MessageReceived(BMessage* message) break; case kMsgFolderSelected: + { fImageMenu->FindItem(kMsgNoImage)->SetLabel(B_TRANSLATE("Default")); - fLastWorkspaceIndex = fWorkspaceMenu->IndexOf( - fWorkspaceMenu->FindMarked()); _SetDesktop(false); - - _LoadRecentFolder(*fPathList.ItemAt(fWorkspaceMenu->IndexOf( - fWorkspaceMenu->FindMarked()) - 6)); + BString folderPathStr; + if (message->FindString("folderPath", &folderPathStr) == B_OK) { + BPath folderPath(folderPathStr); + _LoadRecentFolder(folderPath); + } break; - + } case kMsgApplySettings: { _Save(); @@ -830,27 +822,21 @@ BackgroundsView::_LoadSettings() PRINT_OBJECT(fSettings); - BString string; - if (fSettings.FindString("paneldir", &string) == B_OK) - fPanel->SetPanelDirectory(string.String()); + BString settingStr; + if (fSettings.FindString("paneldir", &settingStr) == B_OK) + fPanel->SetPanelDirectory(settingStr.String()); - if (fSettings.FindString("folderpaneldir", &string) == B_OK) - fFolderPanel->SetPanelDirectory(string.String()); + if (fSettings.FindString("folderpaneldir", &settingStr) == B_OK) + fFolderPanel->SetPanelDirectory(settingStr.String()); int32 index = 0; - while (fSettings.FindString("recentfolder", index, &string) == B_OK) { - if (index == 0) - fWorkspaceMenu->AddSeparatorItem(); - - path.SetTo(string.String()); - int32 i = _AddPath(path); - BString s; - s << B_TRANSLATE("Folder: ") << path.Leaf(); - BMenuItem* item = new BMenuItem(s.String(), - new BMessage(kMsgFolderSelected)); - fWorkspaceMenu->AddItem(item, -i - 1 + 6); + while (fSettings.FindString("recentfolder", index, &settingStr) == B_OK) { + path.SetTo(settingStr.String()); + _AddRecentFolder(path); index++; } + + fWorkspaceMenu->ItemAt(1)->SetMarked(true); fWorkspaceMenu->SetTargetForItems(this); PRINT(("Settings Loaded\n")); @@ -1043,46 +1029,59 @@ BackgroundsView::RefsReceived(BMessage* message) BMessenger(this).SendMessage(kMsgCurrentWorkspace); break; } - BMenuItem* item; - int32 index = _AddPath(path); - if (index >= 0) { - item = fWorkspaceMenu->ItemAt(index + 6); - fLastWorkspaceIndex = index + 6; - } else { - if (fWorkspaceMenu->CountItems() <= 5) - fWorkspaceMenu->AddSeparatorItem(); - BString s; - s << B_TRANSLATE("Folder: ") << path.Leaf(); - item = new BMenuItem(s.String(), - new BMessage(kMsgFolderSelected)); - fWorkspaceMenu->AddItem(item, -index - 1 + 6); - item->SetTarget(this); - fLastWorkspaceIndex = -index - 1 + 6; - } - item->SetMarked(true); - BMessenger(this).SendMessage(kMsgFolderSelected); + // Add the newly selected path as a recent folder, + // removing the oldest entry if needed + _AddRecentFolder(path, true); } } } -int32 -BackgroundsView::_AddPath(BPath path) +static BPath* +FindPath(BPath* currentPath, void* newPath) { - int32 count = fPathList.CountItems(); - int32 index = 0; - for (; index < count; index++) { - BPath* p = fPathList.ItemAt(index); - int c = BString(p->Path()).ICompare(path.Path()); - if (c == 0) - return index; + BPath* pathToCheck = static_cast(newPath); + int compare = ICompare(currentPath->Path(), pathToCheck->Path()); + return compare == 0 ? currentPath : NULL; +} - if (c > 0) - break; + +void +BackgroundsView::_AddRecentFolder(BPath path, bool notifyApp = false) +{ + BPath* currentPath = fPathList.EachElement(FindPath, &path); + BMenuItem* item; + BMessage* folderSelectedMsg = new BMessage(kMsgFolderSelected); + folderSelectedMsg->AddString("folderPath", path.Path()); + + if (currentPath == NULL) { + // "All Workspaces", "Current Workspace", "--", "Default folder", + // "Other folder...", If only these 5 exist, + // we need a new separator to add path specific recent folders. + if (fWorkspaceMenu->CountItems() <= 5) + fWorkspaceMenu->AddSeparatorItem(); + // Maxed out the number of recent folders, remove the oldest entry + if (fPathList.CountItems() == fRecentFoldersLimit) { + fPathList.RemoveItemAt(0); + fWorkspaceMenu->RemoveItem(6); + } + // Add the new recent folder + BString folderMenuText(B_TRANSLATE("Folder: %path")); + folderMenuText.ReplaceFirst("%path", path.Leaf()); + item = new BMenuItem(folderMenuText.String(), folderSelectedMsg); + fWorkspaceMenu->AddItem(item); + fPathList.AddItem(new BPath(path)); + item->SetTarget(this); + } else { + int32 itemIndex = fPathList.IndexOf(currentPath); + item = fWorkspaceMenu->ItemAt(itemIndex + 6); } - fPathList.AddItem(new BPath(path), index); - return -index - 1; + + item->SetMarked(true); + + if (notifyApp) + BMessenger(this).SendMessage(folderSelectedMsg); } diff --git a/src/preferences/backgrounds/BackgroundsView.h b/src/preferences/backgrounds/BackgroundsView.h index 4df303f248..4acb9fce81 100644 --- a/src/preferences/backgrounds/BackgroundsView.h +++ b/src/preferences/backgrounds/BackgroundsView.h @@ -130,7 +130,8 @@ protected: void _UpdatePreview(); void _UpdateButtons(); void _SetDesktop(bool isDesktop); - int32 _AddPath(BPath path); + void _AddRecentFolder(BPath path, + bool notifyApp = false); static int32 _NotifyThread(void* data); @@ -159,7 +160,7 @@ protected: entry_ref fCurrentRef; int32 fLastImageIndex; - int32 fLastWorkspaceIndex; + int32 fRecentFoldersLimit; BMessage fSettings; BObjectList fPathList;