Skip to content

Commit 219c76e

Browse files
committed
Add "Open recent" menu
* Closes #143.
1 parent 1331cb3 commit 219c76e

4 files changed

Lines changed: 65 additions & 19 deletions

File tree

src/editor/EditorWindow.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ EditorWindow::EditorWindow(bool stagger)
103103
.AddItem(B_TRANSLATE("New"), MAINMENU_FILE_NEW, 'N')
104104
.AddSeparator()
105105
.AddItem(B_TRANSLATE("Open" B_UTF8_ELLIPSIS), MAINMENU_FILE_OPEN, 'O')
106+
.AddMenu(B_TRANSLATE("Open recent"))
107+
.AddItem(B_TRANSLATE("<empty>"), MAINMENU_OPEN_RECENT)
108+
.End()
106109
.AddItem(B_TRANSLATE("Reload"), MAINMENU_FILE_RELOAD)
107110
.AddItem(B_TRANSLATE("Save"), MAINMENU_FILE_SAVE, 'S')
108111
.AddItem(B_TRANSLATE("Save as" B_UTF8_ELLIPSIS), MAINMENU_FILE_SAVEAS)
@@ -168,6 +171,9 @@ EditorWindow::EditorWindow(bool stagger)
168171
// When changing this shortcut remember to update one in StatusView as well
169172
AddShortcut('T', B_COMMAND_KEY | B_OPTION_KEY, new BMessage((uint32) OPEN_TERMINAL));
170173

174+
fOpenRecentMenu = fMainMenu->FindItem(MAINMENU_OPEN_RECENT)->Menu();
175+
_PopulateOpenRecentMenu();
176+
171177
fLanguageMenu = fMainMenu->FindItem(MAINMENU_LANGUAGE)->Menu();
172178
_PopulateLanguageMenu();
173179

@@ -919,6 +925,30 @@ EditorWindow::SetOnQuitReplyToMessage(BMessage* message)
919925
}
920926

921927

928+
void
929+
EditorWindow::_PopulateOpenRecentMenu()
930+
{
931+
BMessage refList;
932+
be_roster->GetRecentDocuments(&refList, 10, nullptr, gAppMime);
933+
934+
message_property<B_REF_TYPE> refs(&refList, "refs");
935+
if(refs.size() == 0) {
936+
fOpenRecentMenu->ItemAt(0)->SetEnabled(false);
937+
} else {
938+
// Clear the menu first
939+
int32 count = fOpenRecentMenu->CountItems();
940+
fOpenRecentMenu->RemoveItems(0, count, true);
941+
for(auto ref : refs) {
942+
BPath p(&ref);
943+
BMessage *msg = new BMessage(B_REFS_RECEIVED);
944+
msg->AddRef("refs", &ref);
945+
BMenuItem *menuItem = new BMenuItem(p.Path(), msg);
946+
fOpenRecentMenu->AddItem(menuItem);
947+
}
948+
}
949+
}
950+
951+
922952
void
923953
EditorWindow::_PopulateLanguageMenu()
924954
{

src/editor/EditorWindow.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ enum {
8181
MAINMENU_SEARCH_GOTOLINE = 'msgl',
8282

8383
MAINMENU_LANGUAGE = 'ml00',
84+
MAINMENU_OPEN_RECENT = 'mr00',
8485

8586
TOOLBAR_SPECIAL_SYMBOLS = 'tlss',
8687

@@ -143,6 +144,7 @@ class EditorWindow : public BWindow {
143144
Editor* fEditor;
144145
BFilePanel* fOpenPanel;
145146
BFilePanel* fSavePanel;
147+
BMenu* fOpenRecentMenu;
146148
BMenu* fLanguageMenu;
147149
std::string fCurrentLanguage;
148150
ToolBar* fToolbar;
@@ -163,6 +165,7 @@ class EditorWindow : public BWindow {
163165
std::string fIncrementalSearchTerm;
164166
std::unique_ptr<BMessageFilter> fIncrementalSearchFilter;
165167

168+
void _PopulateOpenRecentMenu();
166169
void _PopulateLanguageMenu();
167170
void _ReloadFile(entry_ref* ref = nullptr);
168171
void _SetLanguage(std::string lang);

src/support/Utils.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,12 @@ KeyDownMessageFilter::Filter(BMessage* message, BHandler** target)
175175

176176

177177
template<>
178-
void
179-
copy_value(const void* source, ssize_t size, std::string* destination)
180-
{
181-
destination->resize(size);
182-
*destination = reinterpret_cast<const char*>(source);
178+
entry_ref
179+
find_value<B_REF_TYPE>(BMessage* message, std::string name, int index) {
180+
entry_ref ref;
181+
status_t status = message->FindRef(name.c_str(), index, &ref);
182+
if(status == B_OK) {
183+
return ref;
184+
}
185+
return entry_ref();
183186
}

src/support/Utils.h

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class BBitmap;
1818
class BCheckBox;
1919
class BRadioButton;
2020

21+
struct entry_ref;
22+
2123

2224
std::string GetFileName(const std::string filename);
2325
std::string GetFileExtension(const std::string filename);
@@ -70,14 +72,30 @@ struct property_type<B_STRING_TYPE>
7072
using type = std::string;
7173
};
7274

73-
74-
template<typename Dest>
75-
void
76-
copy_value(const void* source, ssize_t, Dest* destination)
75+
template<>
76+
struct property_type<B_REF_TYPE>
7777
{
78-
*destination = *reinterpret_cast<const Dest*>(source);
78+
using type = entry_ref;
79+
};
80+
81+
82+
template<type_code BType>
83+
typename property_type<BType>::type
84+
find_value(BMessage* message, std::string name, int index) {
85+
typename property_type<BType>::type typed_data;
86+
ssize_t size;
87+
const void* data;
88+
status_t status = message->FindData(name.c_str(), BType, index, &data, &size);
89+
if(status == B_OK) {
90+
memcpy(data, size, &typed_data);
91+
}
92+
return typename property_type<BType>::type();
7993
}
8094

95+
template<>
96+
entry_ref
97+
find_value<B_REF_TYPE>(BMessage* message, std::string name, int index);
98+
8199

82100
template<type_code BType>
83101
class message_property
@@ -108,15 +126,7 @@ class message_property
108126
return clone;
109127
}
110128
typename property_type<BType>::type operator*() {
111-
ssize_t size;
112-
const void *data;
113-
status_t status = message_->FindData(prop_name_.c_str(), BType, index_, &data, &size);
114-
if(status == B_OK) {
115-
typename property_type<BType>::type value;
116-
copy_value<typename property_type<BType>::type>(data, size, &value);
117-
return value;
118-
}
119-
// FIXME: throw an exception here
129+
return find_value<BType>(message_, prop_name_, index_);
120130
}
121131
};
122132
message_property(BMessage* message, std::string prop_name)

0 commit comments

Comments
 (0)