2
Simple DirectMedia Layer
3
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
5
This software is provided 'as-is', without any express or implied
6
warranty. In no event will the authors be held liable for any damages
7
arising from the use of this software.
9
Permission is granted to anyone to use this software for any purpose,
10
including commercial applications, and to alter it and redistribute it
11
freely, subject to the following restrictions:
13
1. The origin of this software must not be misrepresented; you must not
14
claim that you wrote the original software. If you use this software
15
in a product, an acknowledgment in the product documentation would be
16
appreciated but is not required.
17
2. Altered source versions must be plainly marked as such, and must not be
18
misrepresented as being the original software.
19
3. This notice may not be removed or altered from any source distribution.
21
#include "SDL_internal.h"
23
#include "../SDL_dialog_utils.h"
25
#include "../../core/haiku/SDL_BeApp.h"
37
#include <TypeConstants.h>
39
bool StringEndsWith(const std::string& str, const std::string& end)
41
return str.size() >= end.size() && !str.compare(str.size() - end.size(), end.size(), end);
44
std::vector<std::string> StringSplit(const std::string& str, const std::string& split)
46
std::vector<std::string> result;
50
while ((pos = s.find(split)) != std::string::npos) {
51
result.push_back(s.substr(0, pos));
52
s = s.substr(pos + split.size());
60
class SDLBRefFilter : public BRefFilter
63
SDLBRefFilter(const SDL_DialogFileFilter *filters, int nfilters) :
70
virtual bool Filter(const entry_ref *ref, BNode *node, struct stat_beos *stat, const char *mimeType) override
75
std::string result = path.Path();
82
if (S_ISDIR(info.st_mode))
85
for (int i = 0; i < m_nfilters; i++) {
86
for (const auto& suffix : StringSplit(m_filters[i].pattern, ";")) {
87
if (StringEndsWith(result, std::string(".") + suffix)) {
97
const SDL_DialogFileFilter * const m_filters;
101
class CallbackLooper : public BLooper
104
CallbackLooper(SDL_DialogFileCallback callback, void *userdata) :
105
m_callback(callback),
106
m_userdata(userdata),
121
void SetToBeFreed(BMessenger *messenger, BFilePanel *panel, SDLBRefFilter *filter)
123
m_messenger = messenger;
128
virtual void MessageReceived(BMessage *msg) override
134
const char *filename;
139
case B_REFS_RECEIVED: // Open
140
msg->GetInfo("refs", NULL, &nFiles);
141
for (int i = 0; i < nFiles; i++) {
142
msg->FindRef("refs", i, &file);
144
entry.GetPath(&path);
145
result = path.Path();
146
m_files.push_back(result);
150
case B_SAVE_REQUESTED: // Save
151
msg->FindRef("directory", &file);
153
entry.GetPath(&path);
154
result = path.Path();
156
msg->FindString("name", &filename);
158
m_files.push_back(result);
161
case B_CANCEL: // Whenever the dialog is closed (Cancel but also after Open and Save)
163
nFiles = m_files.size();
164
const char* files[nFiles + 1];
165
for (int i = 0; i < nFiles; i++) {
166
files[i] = m_files[i].c_str();
168
files[nFiles] = NULL;
169
m_callback(m_userdata, files, -1);
177
BHandler::MessageReceived(msg);
183
SDL_DialogFileCallback m_callback;
185
std::vector<std::string> m_files;
187
// Only to free stuff later
188
BMessenger *m_messenger;
190
SDLBRefFilter *m_filter;
193
void ShowDialog(bool save, SDL_DialogFileCallback callback, void *userdata, bool many, bool modal, const SDL_DialogFileFilter *filters, int nfilters, bool folder, const char *location)
195
if (!SDL_InitBeApp()) {
196
char* err = SDL_strdup(SDL_GetError());
197
SDL_SetError("Couldn't init Be app: %s", err);
199
callback(userdata, NULL, -1);
204
const char *msg = validate_filters(filters, nfilters);
207
SDL_SetError("%s", msg);
208
callback(userdata, NULL, -1);
213
if (SDL_GetHint(SDL_HINT_FILE_DIALOG_DRIVER) != NULL) {
214
SDL_SetError("File dialog driver unsupported");
215
callback(userdata, NULL, -1);
219
// No unique_ptr's because they need to survive the end of the function
220
CallbackLooper *looper = new(std::nothrow) CallbackLooper(callback, userdata);
221
BMessenger *messenger = new(std::nothrow) BMessenger(NULL, looper);
222
SDLBRefFilter *filter = new(std::nothrow) SDLBRefFilter(filters, nfilters);
224
if (looper == NULL || messenger == NULL || filter == NULL) {
229
callback(userdata, NULL, -1);
236
entry.SetTo(location);
237
entry.GetRef(&entryref);
240
BFilePanel *panel = new BFilePanel(save ? B_SAVE_PANEL : B_OPEN_PANEL, messenger, location ? &entryref : NULL, folder ? B_DIRECTORY_NODE : B_FILE_NODE, many, NULL, filter, modal);
241
looper->SetToBeFreed(messenger, panel, filter);
246
void SDL_ShowOpenFileDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const SDL_DialogFileFilter *filters, int nfilters, const char *default_location, SDL_bool allow_many)
248
ShowDialog(false, callback, userdata, allow_many == true, !!window, filters, nfilters, false, default_location);
251
void SDL_ShowSaveFileDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const SDL_DialogFileFilter *filters, int nfilters, const char *default_location)
253
ShowDialog(true, callback, userdata, false, !!window, filters, nfilters, false, default_location);
256
void SDL_ShowOpenFolderDialog(SDL_DialogFileCallback callback, void *userdata, SDL_Window *window, const char* default_location, SDL_bool allow_many)
258
ShowDialog(false, callback, userdata, allow_many == true, !!window, NULL, 0, true, default_location);