diff --git a/src/macosx/osxgl.m b/src/macosx/osxgl.m index 0a9147cf9..b3be0ea62 100644 --- a/src/macosx/osxgl.m +++ b/src/macosx/osxgl.m @@ -15,7 +15,6 @@ * See readme.txt for copyright information. */ - #include "allegro5/allegro.h" #include "allegro5/allegro_opengl.h" #include "allegro5/internal/aintern.h" @@ -27,6 +26,7 @@ #include "allegro5/platform/aintosx.h" #include "./osxgl.h" #include "allegro5/allegro_osx.h" + #ifndef ALLEGRO_MACOSX #error something is wrong with the makefile #endif @@ -65,6 +65,12 @@ #define MINIMUM_WIDTH 48 #define MINIMUM_HEIGHT 48 + +/* by MAREK */ +static char **cfileURLs; +int cfileURLs_n=0; +/* */ //by MAREK + /* Unsigned integer; data type only avaliable for OS X >= 10.5 */ #if MAC_OS_X_VERSION_MIN_REQUIRED < 1050 typedef unsigned int NSUInteger; @@ -212,6 +218,9 @@ @interface ALOpenGLView : NSOpenGLView /* This is passed onto the event functions so we know where the event came from */ ALLEGRO_DISPLAY* dpy_ptr; } +/* by MAREK* / +- (id)initWithFrame:(NSRect)frameRect; +/* */ //by MAREK -(void)setAllegroDisplay: (ALLEGRO_DISPLAY*) ptr; -(ALLEGRO_DISPLAY*) allegroDisplay; -(void) reshape; @@ -294,6 +303,114 @@ void _al_osx_mouse_was_installed(BOOL install) { @implementation ALOpenGLView +/* by MAREK */ +- (id)initWithFrame:(NSRect)frameRect +{ + self = [super initWithFrame:frameRect]; + if (self) { + // Register for file URL drops (e.g., images) + [self registerForDraggedTypes:@[NSPasteboardTypeFileURL]]; + // Or other types like NSStringPboardType, NSFileContentsPboardType, etc. + } + + // NSString *logPath = @"allegro5.log"; + // freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr); + + return self; +} + +// Drag-and-drop methods + +- (NSDragOperation)draggingEntered:(id)sender { + NSPasteboard *pasteboard = [sender draggingPasteboard]; + if ([[pasteboard types] containsObject:NSPasteboardTypeFileURL]) { + return NSDragOperationCopy; // Accept copy operation for files + } + return NSDragOperationNone; +} + +- (BOOL)prepareForDragOperation:(id)sender { + return YES; // Prepare to accept the drop +} + +- (BOOL)performDragOperation:(id)sender { + NSPasteboard *pasteboard = [sender draggingPasteboard]; + NSArray *classes = @[NSURL.class]; + NSDictionary *options = @{NSPasteboardURLReadingFileURLsOnlyKey: @YES}; + + int n=0; + + NSArray *fileURLs = [pasteboard readObjectsForClasses:classes options:options]; + if (fileURLs.count > 0) + { + // Handle multiple dropped files here + + //freeing filenames + if (cfileURLs_n>0) + { + for (int ni=0; niwin; + + NSPoint mousePos = [window mouseLocationOutsideOfEventStream]; + // mousePos.x and mousePos.y now contain the cursor's coordinates relative to the window's bottom-left corner. + + NSRect rc = [window frame]; + NSRect content = [window contentRectForFrameRect: rc]; + content = [self convertRectToBacking: content]; + ALLEGRO_EVENT_SOURCE *es = &dpy->parent.es; + + _al_event_source_lock(es); + ALLEGRO_EVENT event; + + event.drop.type = ALLEGRO_EVENT_DROP; + event.drop.timestamp = al_get_time(); + event.drop.x = (int)mousePos.x; + event.drop.y = (int)mousePos.y; + NSLog(@"Event with file (%d/%d) : %@", n+1, (int)fileURLs.count, filePath); + //alocating memory for filename + cfileURLs[n]=malloc(strlen(cfileURL)+1); + //copying + strncpy(cfileURLs[n], cfileURL, strlen(cfileURL)+1); + //pointer will be unique and not destructed until next drop series + event.drop.text = cfileURLs[n]; + event.drop.is_file = true; + event.drop.row = n; + if ((n+1)<(int)fileURLs.count) event.drop.is_complete = false; + else event.drop.is_complete = true; + _al_event_source_emit_event(es, &event); + _al_event_source_unlock(es); + n++; + } + + return YES; + } + return NO; +} + +- (void)concludeDragOperation:(id)sender { + // Optional: Any cleanup after drop +} + +/**/ //by MAREK + -(void) prepareOpenGL { [super prepareOpenGL]; @@ -1646,6 +1763,10 @@ static void init_halt_events(ALLEGRO_DISPLAY_OSX_WIN *dpy) return; } + + ////// + //view->registerForDraggedTypes:@[NSPasteboardTypeFileURL]]; + ////// dpy->view = view; /* Hook up the view to its display */ [view setAllegroDisplay: &dpy->parent]; @@ -1876,6 +1997,16 @@ static void destroy_display(ALLEGRO_DISPLAY* d) al_free(d->vertex_cache); al_free(d); [pool drain]; + + /* by MAREK */ + //freeing filenames + if (cfileURLs_n>0) + { + for (int ni=0; ni