aboutsummaryrefslogtreecommitdiff
path: root/sokol_app.h
diff options
context:
space:
mode:
authorAndre Weissflog <floooh@gmail.com>2020-10-15 19:06:58 +0200
committerAndre Weissflog <floooh@gmail.com>2020-10-15 19:06:58 +0200
commitffb885a2f75c5ffbced33c7da0eab0f50d824913 (patch)
treeb0b27d40bbc906ee57fc8baa4592751a7d230871 /sokol_app.h
parent497fbd2a37994e2e786be4b746173752b5092e8a (diff)
parent0817b779d308e28e0fe9922667cc945c890f22c0 (diff)
resolve merge conflicts
Diffstat (limited to 'sokol_app.h')
-rw-r--r--sokol_app.h109
1 files changed, 108 insertions, 1 deletions
diff --git a/sokol_app.h b/sokol_app.h
index a007d5a1..d21352f3 100644
--- a/sokol_app.h
+++ b/sokol_app.h
@@ -138,10 +138,12 @@
high-dpi | YES | YES | TODO | YES | YES | YES | TODO | YES
clipboard | YES | YES | TODO | --- | --- | TODO | --- | YES
MSAA | YES | YES | YES | YES | YES | TODO | TODO | YES
+ dropped files | YES | YES | TODO | --- | --- | TODO | TODO | TODO
TODO
====
- Linux:
+ - dropped files
- clipboard support
- UWP:
- clipboard, mouselock, MSAA support
@@ -766,6 +768,7 @@ typedef enum sapp_event_type {
SAPP_EVENTTYPE_UPDATE_CURSOR,
SAPP_EVENTTYPE_QUIT_REQUESTED,
SAPP_EVENTTYPE_CLIPBOARD_PASTED,
+ SAPP_EVENTTYPE_FILE_DROPPED,
_SAPP_EVENTTYPE_NUM,
_SAPP_EVENTTYPE_FORCE_U32 = 0x7FFFFFFF
} sapp_event_type;
@@ -963,6 +966,8 @@ typedef struct sapp_desc {
bool user_cursor; /* if true, user is expected to manage cursor image in SAPP_EVENTTYPE_UPDATE_CURSOR */
bool enable_clipboard; /* enable clipboard access, default is false */
int clipboard_size; /* max size of clipboard content in bytes */
+ int max_dropped_files; /* max number of dropped files to process */
+ int max_dropped_file_path_length; /* max length of a dropped file path */
const char* html5_canvas_name; /* the name (id) of the HTML5 canvas element, default is "canvas" */
bool html5_canvas_resize; /* if true, the HTML5 canvas size is set to sapp_desc.width/height, otherwise canvas size is tracked */
@@ -1028,6 +1033,10 @@ SOKOL_API_DECL void sapp_set_clipboard_string(const char* str);
SOKOL_API_DECL const char* sapp_get_clipboard_string(void);
/* set the window title (only on desktop platforms) */
SOKOL_API_DECL void sapp_set_window_title(const char* str);
+/* gets the total number of dropped files */
+SOKOL_API_DECL int sapp_get_num_dropped_files(void);
+/* gets the dropped file paths */
+SOKOL_API_DECL const char* sapp_get_dropped_file_path(int index);
/* special run-function for SOKOL_NO_ENTRY (in standard mode this is an empty stub) */
SOKOL_API_DECL int sapp_run(const sapp_desc* desc);
@@ -1361,6 +1370,8 @@ inline int sapp_run(const sapp_desc& desc) { return sapp_run(&desc); }
#if defined(_SAPP_MACOS)
@interface _sapp_macos_app_delegate : NSObject<NSApplicationDelegate>
@end
+@interface _sapp_macos_window : NSWindow
+@end
@interface _sapp_macos_window_delegate : NSObject<NSWindowDelegate>
@end
#if defined(SOKOL_METAL)
@@ -1812,6 +1823,12 @@ typedef struct {
sapp_event event;
_sapp_mouse_t mouse;
_sapp_clipboard_t clipboard;
+ // FIXME FIXME FIXME: move this stuff into a nested struct
+ int max_dropped_files;
+ int max_dropped_file_path_length;
+ int num_dropped_files;
+ char* dropped_files;
+ // FIXME FIXME FIXME
#if defined(_SAPP_MACOS)
_sapp_macos_t macos;
#elif defined(_SAPP_IOS)
@@ -2295,6 +2312,8 @@ _SOKOL_PRIVATE sapp_desc _sapp_desc_defaults(const sapp_desc* in_desc) {
desc.swap_interval = _sapp_def(desc.swap_interval, 1);
desc.html5_canvas_name = _sapp_def(desc.html5_canvas_name, "canvas");
desc.clipboard_size = _sapp_def(desc.clipboard_size, 8192);
+ desc.max_dropped_files = _sapp_def(desc.max_dropped_files, 1);
+ desc.max_dropped_file_path_length = _sapp_def(desc.max_dropped_file_path_length, 1024);
desc.window_title = _sapp_def(desc.window_title, "sokol_app");
return desc;
}
@@ -2317,6 +2336,11 @@ _SOKOL_PRIVATE void _sapp_init_state(const sapp_desc* desc) {
_sapp.clipboard.buf_size = _sapp.desc.clipboard_size;
_sapp.clipboard.buffer = (char*) SOKOL_CALLOC(1, _sapp.clipboard.buf_size);
}
+ _sapp.max_dropped_files = _sapp.desc.max_dropped_files;
+ _sapp.max_dropped_file_path_length = _sapp.desc.max_dropped_file_path_length;
+ if (_sapp.max_dropped_files > 0 && _sapp.max_dropped_file_path_length > 0) {
+ _sapp.dropped_files = (char*) SOKOL_CALLOC(1, _sapp.max_dropped_files * _sapp.max_dropped_file_path_length);
+ }
_sapp_strcpy(_sapp.desc.window_title, _sapp.window_title, sizeof(_sapp.window_title));
_sapp.desc.window_title = _sapp.window_title;
_sapp.dpi_scale = 1.0f;
@@ -2329,6 +2353,10 @@ _SOKOL_PRIVATE void _sapp_discard_state(void) {
SOKOL_ASSERT(_sapp.clipboard.buffer);
SOKOL_FREE((void*)_sapp.clipboard.buffer);
}
+ if (_sapp.max_dropped_files > 0 && _sapp.max_dropped_file_path_length > 0) {
+ SOKOL_ASSERT(_sapp.dropped_files);
+ SOKOL_FREE((void*)_sapp.dropped_files);
+ }
_SAPP_CLEAR(_sapp_t, _sapp);
}
@@ -2725,7 +2753,7 @@ _SOKOL_PRIVATE void _sapp_macos_frame(void) {
NSWindowStyleMaskMiniaturizable |
NSWindowStyleMaskResizable;
NSRect window_rect = NSMakeRect(0, 0, _sapp.window_width, _sapp.window_height);
- _sapp.macos.window = [[NSWindow alloc]
+ _sapp.macos.window = [[_sapp_macos_window alloc]
initWithContentRect:window_rect
styleMask:style
backing:NSBackingStoreBuffered
@@ -2875,6 +2903,48 @@ _SOKOL_PRIVATE void _sapp_macos_frame(void) {
}
@end
+@implementation _sapp_macos_window
+- (instancetype)initWithContentRect:(NSRect)contentRect
+ styleMask:(NSWindowStyleMask)style
+ backing:(NSBackingStoreType)backingStoreType
+ defer:(BOOL)flag {
+ if (self = [super initWithContentRect:contentRect styleMask:style backing:backingStoreType defer:flag]) {
+ [self registerForDraggedTypes:[NSArray arrayWithObject:NSPasteboardTypeFileURL]];
+ }
+ return self;
+}
+
+- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
+ return NSDragOperationCopy;
+}
+
+- (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender {
+ return NSDragOperationCopy;
+}
+
+- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
+ NSPasteboard *pboard = [sender draggingPasteboard];
+ if ([pboard.types containsObject:NSPasteboardTypeFileURL]) {
+ memset(_sapp.dropped_files, 0, sizeof(_sapp.max_dropped_files * _sapp.max_dropped_file_path_length));
+ _sapp.num_dropped_files = pboard.pasteboardItems.count > _sapp.max_dropped_files ? _sapp.max_dropped_files : pboard.pasteboardItems.count;
+
+ for (int i = 0; i < _sapp.num_dropped_files; i++) {
+ NSURL *fileUrl = [NSURL fileURLWithPath:[pboard.pasteboardItems[i] stringForType:NSPasteboardTypeFileURL]];
+ _sapp_strcpy(fileUrl.standardizedURL.path.UTF8String, &_sapp.dropped_files[i * _sapp.max_dropped_file_path_length], _sapp.max_dropped_file_path_length);
+ }
+
+ if (_sapp_events_enabled()) {
+ _sapp_init_event(SAPP_EVENTTYPE_FILE_DROPPED);
+ _sapp.desc.event_cb(&_sapp.event);
+ }
+
+ return YES;
+ }
+
+ return NO;
+}
+@end
+
@implementation _sapp_macos_view
#if defined(SOKOL_GLCORE33)
/* NOTE: this is a hack/fix when the initial window size has been clipped by
@@ -5515,6 +5585,33 @@ _SOKOL_PRIVATE LRESULT CALLBACK _sapp_win32_wndproc(HWND hWnd, UINT uMsg, WPARAM
}
*/
break;
+ case WM_DROPFILES:
+ HDROP drop = (HDROP) wParam;
+ int i;
+
+ const int count = DragQueryFileW(drop, 0xffffffff, NULL, 0);
+ memset(_sapp.dropped_files, 0, sizeof(_sapp.max_dropped_files * _sapp.max_dropped_file_path_length));
+ _sapp.num_dropped_files = count > _sapp.max_dropped_files ? _sapp.max_dropped_files : count;
+
+ for (i = 0; i < _sapp.num_dropped_files; i++) {
+ const UINT length = DragQueryFileW(drop, i, NULL, 0) + 1;
+ WCHAR* buffer = (WCHAR*)SOKOL_CALLOC(length, sizeof(WCHAR));
+
+ DragQueryFileW(drop, i, buffer, length);
+
+ const int dst_needed = WideCharToMultiByte(CP_UTF8, 0, buffer, -1, 0, 0, NULL, NULL);
+ int len = dst_needed > _sapp.max_dropped_file_path_length ? _sapp.max_dropped_file_path_length : dst_needed;
+ _sapp_win32_wide_to_utf8(buffer, &_sapp.dropped_files[i * _sapp.max_dropped_file_path_length], len);
+ SOKOL_FREE(buffer);
+ }
+
+ if (_sapp_events_enabled()) {
+ _sapp_init_event(SAPP_EVENTTYPE_FILE_DROPPED);
+ _sapp.desc.event_cb(&_sapp.event);
+ }
+
+ DragFinish(drop);
+ break;
default:
break;
}
@@ -5568,6 +5665,8 @@ _SOKOL_PRIVATE void _sapp_win32_create_window(void) {
_sapp.win32.dc = GetDC(_sapp.win32.hwnd);
SOKOL_ASSERT(_sapp.win32.dc);
_sapp_win32_update_dimensions();
+
+ DragAcceptFiles(_sapp.win32.hwnd, 1);
}
_SOKOL_PRIVATE void _sapp_win32_destroy_window(void) {
@@ -9618,6 +9717,14 @@ SOKOL_API_IMPL void sapp_set_window_title(const char* title) {
#endif
}
+SOKOL_API_IMPL int sapp_get_num_dropped_files(void) {
+ return _sapp.num_dropped_files;
+}
+
+SOKOL_API_IMPL const char* sapp_get_dropped_file_path(int index) {
+ return (const char*) &_sapp.dropped_files[index * _sapp.max_dropped_file_path_length];
+}
+
SOKOL_API_IMPL const void* sapp_metal_get_device(void) {
SOKOL_ASSERT(_sapp.valid);
#if defined(SOKOL_METAL)