Skip to content

Commit bf862b8

Browse files
Update osxgl.m
1 parent efb3129 commit bf862b8

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

src/macosx/osxgl.m

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565
#define MINIMUM_WIDTH 48
6666
#define MINIMUM_HEIGHT 48
6767

68+
/* by MAREK */
69+
#define FILE_URL_SIZE 254
70+
/* end by MAREK */
71+
6872
/* Unsigned integer; data type only avaliable for OS X >= 10.5 */
6973
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1050
7074
typedef unsigned int NSUInteger;
@@ -212,6 +216,9 @@ @interface ALOpenGLView : NSOpenGLView
212216
/* This is passed onto the event functions so we know where the event came from */
213217
ALLEGRO_DISPLAY* dpy_ptr;
214218
}
219+
/* by MAREK */
220+
- (id)initWithFrame:(NSRect)frameRect;
221+
/* end by MAREK */
215222
-(void)setAllegroDisplay: (ALLEGRO_DISPLAY*) ptr;
216223
-(ALLEGRO_DISPLAY*) allegroDisplay;
217224
-(void) reshape;
@@ -292,8 +299,119 @@ void _al_osx_mouse_was_installed(BOOL install) {
292299
});
293300
}
294301

302+
/* by MAREK */
303+
@implementation NSString (Char)
304+
305+
/*
306+
* Convert a NSString to a char pointer
307+
*/
308+
-(char *)toChar{
309+
const char* strUtf8 = [self UTF8String];
310+
size_t len = strlen(strUtf8) + 1;
311+
char *toChar = malloc(len);
312+
memcpy(toChar, strUtf8, len);
313+
return toChar;
314+
}
315+
@end
316+
/* end by MAREK */
317+
295318
@implementation ALOpenGLView
296319

320+
/*by MAREK*/
321+
- (id)initWithFrame:(NSRect)frameRect
322+
{
323+
self = [super initWithFrame:frameRect];
324+
if (self) {
325+
// Register for file URL drops (e.g., images)
326+
[self registerForDraggedTypes:@[NSPasteboardTypeFileURL]];
327+
// Or other types like NSStringPboardType, NSFileContentsPboardType, etc.
328+
}
329+
330+
NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
331+
332+
// NSString *logPath = @"allegro5.log";
333+
// freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr);
334+
335+
return self;
336+
}
337+
338+
// Drag-and-drop methods
339+
340+
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
341+
NSPasteboard *pasteboard = [sender draggingPasteboard];
342+
if ([[pasteboard types] containsObject:NSPasteboardTypeFileURL]) {
343+
return NSDragOperationCopy; // Accept copy operation for files
344+
}
345+
return NSDragOperationNone;
346+
}
347+
348+
- (BOOL)prepareForDragOperation:(id<NSDraggingInfo>)sender {
349+
return YES; // Prepare to accept the drop
350+
}
351+
352+
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
353+
NSPasteboard *pasteboard = [sender draggingPasteboard];
354+
NSArray *classes = @[NSURL.class];
355+
NSDictionary *options = @{NSPasteboardURLReadingFileURLsOnlyKey: @YES};
356+
357+
int n=0;
358+
359+
NSArray<NSURL *> *fileURLs = [pasteboard readObjectsForClasses:classes options:options];
360+
if (fileURLs.count > 0)
361+
{
362+
// Handle multiple dropped files here
363+
364+
for (NSURL *fileURL in fileURLs)
365+
{
366+
NSString *filePath = [fileURL path];
367+
unsigned long ul=[filePath length];
368+
NSLog(@"Dropped file: %@ len:%d", filePath, (int)ul);
369+
const char *cfileURL= [filePath cStringUsingEncoding:NSUTF8StringEncoding];
370+
371+
// Process the file: e.g., load as texture, model, or data into OpenGL
372+
// Example: If it's an image, use NSImage to load and upload to GL texture
373+
// NSImage *image = [[NSImage alloc] initWithContentsOfURL:fileURL];
374+
// Then convert to OpenGL texture...
375+
ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr;
376+
NSWindow *window = dpy->win;
377+
378+
NSPoint mousePos = [window mouseLocationOutsideOfEventStream];
379+
// mousePos.x and mousePos.y now contain the cursor's coordinates relative to the window's bottom-left corner.
380+
381+
NSRect rc = [window frame];
382+
NSRect content = [window contentRectForFrameRect: rc];
383+
content = [self convertRectToBacking: content];
384+
ALLEGRO_EVENT_SOURCE *es = &dpy->parent.es;
385+
386+
_al_event_source_lock(es);
387+
ALLEGRO_EVENT event;
388+
389+
event.drop.type = ALLEGRO_EVENT_DROP;
390+
event.drop.timestamp = al_get_time();
391+
event.drop.x = (int)mousePos.x;
392+
event.drop.y = (int)mousePos.y;
393+
NSLog(@"Event with file (%d/%d) : %@", n+1, (int)fileURLs.count, filePath);
394+
event.drop.text = (char*) cfileURL;
395+
event.drop.is_file = true;
396+
event.drop.row = n;
397+
if ((n+1)<(int)fileURLs.count) event.drop.is_complete = false;
398+
else event.drop.is_complete = true;
399+
_al_event_source_emit_event(es, &event);
400+
_al_event_source_unlock(es);
401+
n++;
402+
}
403+
404+
return YES;
405+
}
406+
return NO;
407+
}
408+
409+
- (void)concludeDragOperation:(id<NSDraggingInfo>)sender {
410+
// Optional: Any cleanup after drop
411+
}
412+
413+
/* end by MAREK*/
414+
297415
-(void) prepareOpenGL
298416
{
299417
[super prepareOpenGL];

0 commit comments

Comments
 (0)