Author Topic: Drag and drop in Windows  (Read 196 times)

Offline JohnnyB

  • Mc. Print
  • *
  • Posts: 33
    • View Profile
Drag and drop in Windows
« on: 2020-Apr-10 »
Hi all,

has anyone yet done (I know there is a solution for e.g. clipboard) an example of dropping a file into your program window, so you could e.g. automatically load the file?


Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10745
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Drag and drop in Windows
« Reply #1 on: 2020-Apr-14 »
Oh. You need the wm_dropfiles message. Or so. Phew. That's really hard to do, because of the stuff you can drop. You just want the filenames?

Offline JohnnyB

  • Mc. Print
  • *
  • Posts: 33
    • View Profile
Re: Drag and drop in Windows
« Reply #2 on: 2020-Apr-16 »
Thanks for the reply.

Yes, basically I just want to get the file name(s) / paths of something that is dragged into the app window.
« Last Edit: 2020-Apr-16 by JohnnyB »

Online dreamerman

  • Global Moderator
  • Dr. Type
  • *******
  • Posts: 354
    • View Profile
    • my personal website
Re: Drag and drop in Windows
« Reply #3 on: 2020-Apr-18 »
Some workaround is to use SDL2 functions, thanks goes to Schranz0r, get his code from this post: https://www.glbasic.com/forum/index.php?topic=11401.msg100530#msg100530
And add:
in SDL2_window.gbas -> FUNCTION CreateFromGLBasicWindow
after inits add this line:
Code: (glbasic) [Select]
SDL_EventState(SDL_DROPFILE, SDL_ENABLE);in same file add those two functions to SDL2_Window type
Code: (glbasic) [Select]
    FUNCTION EventFileDrop:
INLINE
return events(eventHandle).type == SDL_DROPFILE;
ENDINLINE
    ENDFUNCTION
   
    FUNCTION GetDroppedFile$:
        LOCAL ret$
        INLINE
            char* dropped_filedir;
            dropped_filedir = events(eventHandle).drop.file;
            ret_Str = dropped_filedir;
            SDL_free(dropped_filedir);
        ENDINLINE
        RETURN ret$
    ENDFUNCTION
in main file (SDL_calls.gbas) change PollEvent loop for something like this:
Code: (glbasic) [Select]
WHILE Win.PollEvent()
IF Win.EventKeyDown()
IF KEY(1) THEN Win.CloseWindow() // Close window on ESCAPE-Key
INC counter, 1
ELSEIF Win.EventFileDrop()
LOCAL file$ = Win.GetDroppedFile$()
ENDIF
WEND
It's working, sorry that I doesn't provide clean source, but I'm playing around with other SDL functions so my test project is a complete mess :D
If You need only drop file support maybe there is no need to create SDL window and so on, 'SDL_INIT_EVENTS' could be sufficient to work with drag&drop, not sure.
Check my source code editor for GLBasic - link Update: 20.04.2020

Offline JohnnyB

  • Mc. Print
  • *
  • Posts: 33
    • View Profile
Re: Drag and drop in Windows
« Reply #4 on: 2020-Apr-18 »
That's works perfectly and is very very awesome ;) Thanks!