Drag and drop protocol for MacOS (so far missing, until now...) #1662
Replies: 5 comments
-
This looks interesting. Are you interested in making a pull request with your changes? It'll make it easier to review. |
Beta Was this translation helpful? Give feedback.
-
Sure, I will do that. Thanks. |
Beta Was this translation helpful? Give feedback.
-
it didn't work, it says: |
Beta Was this translation helpful? Give feedback.
-
Did you push the branch with your changes to github first? It should give you a button to make a pull request then |
Beta Was this translation helpful? Give feedback.
-
well, finally I did it, following the advise i found somewhere in github manual. So I created first the fork, edited the certain file, then i sent request directly to the main branch. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi everyone,
in the file path_to_allegro/docs/src/refman/display.txt we can read:
(quote)
API: al_set_new_display_flags
…
ALLEGRO_DRAG_AND_DROP
: If a display is created with the ALLEGRO_DRAG_AND_DROP flag it will
generate ALLEGRO_EVENT_DROP events when files or text are dropped over
the display.
the X11 backend.
(end of quote)
It's maybe experimental, but works for me very well with Xlib.
I implemented xdnd protocol in Allegro 4 for Linux (xdnd) by my own, and I use dnd on Windows, what is implemented in standard Windows library, in Windows version of my program.
For MacOS, due to disappearance of MacOS proper driver in Allegro 4, I adopted Allegro-Legacy together with Allegro 5, and it works perfectly for me on modern MacOS, both x64 and aa64 (more better).
With one missing part. Xdnd protocol is implemented in Allegro 5 for Xlib, but - as I am informed well - not on MacOS, yet.
I needed that feature badly, so I did by my own, and would like to share my small achievement with all of you, especially maintainers, who could put it into the official code, obviously after reviewing all and revising wha's necessary.
So in the file src/macosx/osxgl.m
before @interface ALOpenGLView : NSOpenGLView …
add interface :
/MAREK/
/*- (void) registerForDraggedTypes:(NSArray<NSString *> ) newTypes;/
/**/
in @interface ALOpenGLView : NSOpenGLView add:
then before @implementation ALOpenGLView add NSString to char pointer converter:
@implementation NSString (Char)
/*
*/
-(char )toChar{
const char strUtf8 = [self UTF8String];
size_t len = strlen(strUtf8) + 1;
char *toChar = malloc(len);
memcpy(toChar, strUtf8, len);
return toChar;
}
@EnD
@implementation ALOpenGLView //before -(void) prepareOpenGL or anywhere, add those methods:
/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 *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
// 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<NSURL *> *fileURLs = [pasteboard readObjectsForClasses:classes options:options];
if (fileURLs.count > 0)
{
// Handle multiple dropped files here
}
return NO;
}
(void)concludeDragOperation:(id)sender {
// Optional: Any cleanup after drop
}
/**/ //by MAREK
anyway, the modified file is here:
https://nextcloud.vurplex.com/s/BYeAoEXSjbz6qn4
Then, wherever you receive event (in my case in a5_display.c in Allegro-Legacy, but obviously it’s different if you use Allegro5 only)
case ALLEGRO_EVENT_DROP: //by MAREK
where in the case of my software the functions:
empty_dropped_fill_buf() is just for emptying internal buffer of file names of files to load
file_dropped_fill_buf(…) is to add file name to the buffer
dropped_fill_buf_activate() is to activate loading after filling the buffer is finished.
Does it work ? Oh yes, absolutely. Here is a short clip of my program running on MacOS Sequoia on Mac mini with M4 proc.
https://nextcloud.vurplex.com/s/9GFBqmbRYD7X3pz
Enjoy.
Marek
Beta Was this translation helpful? Give feedback.
All reactions