Skip to content

Commit cf803c5

Browse files
augiedoggieKapiX
authored andcommitted
EditorWindow: give the toolbar 'open' button a recent files dropdown
1 parent d414ec9 commit cf803c5

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/editor/EditorWindow.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ EditorWindow::EditorWindow(bool stagger)
160160
AddShortcut('T', B_COMMAND_KEY | B_OPTION_KEY, new BMessage((uint32) OPEN_TERMINAL));
161161

162162
fOpenRecentMenu = fMainMenu->FindItem(MAINMENU_OPEN_RECENT)->Menu();
163-
_PopulateOpenRecentMenu();
163+
_PopulateOpenRecentMenu(fOpenRecentMenu);
164164

165165
fLanguageMenu = fMainMenu->FindItem(MAINMENU_LANGUAGE)->Menu();
166166
_PopulateLanguageMenu();
@@ -187,6 +187,11 @@ EditorWindow::EditorWindow(bool stagger)
187187
B_TRANSLATE("New"), "new document");
188188
fToolbar->AddAction(MAINMENU_FILE_OPEN,
189189
B_TRANSLATE("Open" B_UTF8_ELLIPSIS), "open");
190+
BButton* openButton = fToolbar->FindButton(MAINMENU_FILE_OPEN);
191+
if(openButton != nullptr) {
192+
openButton->SetBehavior(BButton::B_POP_UP_BEHAVIOR);
193+
openButton->SetPopUpMessage(new BMessage(TOOLBAR_OPEN_RECENT));
194+
}
190195
fToolbar->AddAction(MAINMENU_FILE_RELOAD,
191196
B_TRANSLATE("Reload"), "reload");
192197
fToolbar->SetActionEnabled(MAINMENU_FILE_RELOAD, false);
@@ -666,6 +671,21 @@ EditorWindow::MessageReceived(BMessage* message)
666671
case MAINMENU_LANGUAGE: {
667672
_SetLanguage(message->GetString("lang", "text"));
668673
} break;
674+
case TOOLBAR_OPEN_RECENT: {
675+
BButton* button = fToolbar->FindButton(MAINMENU_FILE_OPEN);
676+
if(button == nullptr) {
677+
return;
678+
}
679+
BPopUpMenu* menu = new BPopUpMenu("RecentsPopUp", false, false);
680+
_PopulateOpenRecentMenu(menu);
681+
menu->SetTargetForItems(this);
682+
BPoint menuPoint(ConvertToScreen(fToolbar->ConvertToParent(button->Frame().LeftBottom())));
683+
menuPoint.x += 2;
684+
menuPoint.y += 1;
685+
button->SetValue(1);
686+
menu->Go(menuPoint, true);
687+
button->SetValue(0);
688+
} break;
669689
case TOOLBAR_SPECIAL_SYMBOLS: {
670690
bool pressed = fPreferences->fWhiteSpaceVisible && fPreferences->fEOLVisible;
671691
if(pressed == true) {
@@ -910,24 +930,24 @@ EditorWindow::SetOnQuitReplyToMessage(BMessage* message)
910930

911931

912932
void
913-
EditorWindow::_PopulateOpenRecentMenu()
933+
EditorWindow::_PopulateOpenRecentMenu(BMenu* menu)
914934
{
915935
BMessage refList;
916936
be_roster->GetRecentDocuments(&refList, 10, nullptr, gAppMime);
917937

918938
message_property<B_REF_TYPE> refs(&refList, "refs");
919939
if(refs.size() == 0) {
920-
fOpenRecentMenu->ItemAt(0)->SetEnabled(false);
940+
menu->ItemAt(0)->SetEnabled(false);
921941
} else {
922942
// Clear the menu first
923-
int32 count = fOpenRecentMenu->CountItems();
924-
fOpenRecentMenu->RemoveItems(0, count, true);
943+
int32 count = menu->CountItems();
944+
menu->RemoveItems(0, count, true);
925945
for(auto ref : refs) {
926946
BPath p(&ref);
927947
BMessage *msg = new BMessage(B_REFS_RECEIVED);
928948
msg->AddRef("refs", &ref);
929949
BMenuItem *menuItem = new BMenuItem(p.Path(), msg);
930-
fOpenRecentMenu->AddItem(menuItem);
950+
menu->AddItem(menuItem);
931951
}
932952
}
933953
}

src/editor/EditorWindow.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ enum {
8383
MAINMENU_LANGUAGE = 'ml00',
8484
MAINMENU_OPEN_RECENT = 'mr00',
8585

86+
TOOLBAR_OPEN_RECENT = 'tlor',
8687
TOOLBAR_SPECIAL_SYMBOLS = 'tlss',
8788

8889
FILE_OPEN = 'flop',
@@ -162,7 +163,7 @@ class EditorWindow : public BWindow {
162163
static Preferences* fPreferences;
163164
FilePreferences fFilePreferences;
164165

165-
void _PopulateOpenRecentMenu();
166+
void _PopulateOpenRecentMenu(BMenu* menu);
166167
void _PopulateLanguageMenu();
167168
void _ReloadFile(entry_ref* ref = nullptr);
168169
void _SetLanguage(std::string lang);

0 commit comments

Comments
 (0)