Skip to content

Commit

Permalink
Implements Drag/Drop of files onto a program's window (Windows-only).
Browse files Browse the repository at this point in the history
New statement:
    _ACCEPTFILEDROP [{ON|OFF}]
    Enables a program to accept files being dropped from a folder.

New functions:
    _TOTALDROPPEDFILES
    Returns the number of files that have been received via drag/drop.

    _DROPPEDFILE$
    Returns the list of files that have been dropped. The function sequentially returns the file list and decreases _TOTALDROPPEDFILES with every read.
  • Loading branch information
FellippeHeitor committed Jul 31, 2018
1 parent 708e05e commit c83b15e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
55 changes: 55 additions & 0 deletions internal/c/libqb.cpp
Expand Up @@ -235,6 +235,8 @@ void sub__delay(double seconds);
void *generic_window_handle=NULL;
#ifdef QB64_WINDOWS
HWND window_handle=NULL;
HDROP hdrop=NULL;
int32 totalDroppedFiles=0;
#endif
//...

Expand Down Expand Up @@ -327,6 +329,7 @@ extern "C" int qb64_custom_event(int event,int v1,int v2,int v3,int v4,int v5,in
#define QB64_EVENT_CLOSE 1
#define QB64_EVENT_KEY 2
#define QB64_EVENT_RELATIVE_MOUSE_MOVEMENT 3
#define QB64_EVENT_FILE_DROP 4
#define QB64_EVENT_KEY_PAUSE 1000

static int32 image_qbicon16_handle;
Expand Down Expand Up @@ -26502,6 +26505,49 @@ void sub__echo(qbs *message){

}//echo

void sub__filedrop(int32 on_off=NULL) {
#ifdef QB64_WINDOWS
if ((on_off==NULL)||(on_off==1))
DragAcceptFiles((HWND)func__handle(), TRUE);
if ((on_off==2))
DragAcceptFiles((HWND)func__handle(), FALSE);
#endif
}

int32 func__totaldroppedfiles() {
#ifdef QB64_WINDOWS
return totalDroppedFiles;
#endif
return 0;
}

qbs *func__droppedfile() {
#ifdef QB64_WINDOWS
static int32 index=-1;
static char szNextFile[MAX_PATH];

if (totalDroppedFiles > 0) {
totalDroppedFiles--;
index++;
if ( DragQueryFile ( hdrop, index, szNextFile, MAX_PATH ) > 0 ) {
if (totalDroppedFiles==0) {
DragFinish(hdrop);
index=-1;
}
return qbs_new_txt(szNextFile);
} else {
goto reset;
}
} else {
reset:
DragFinish(hdrop);
totalDroppedFiles=0;
index=-1;
}
#endif
return qbs_new_txt("");
}

// 0 1 2 0 1 2
void sub__resize(int32 on_off, int32 stretch_smooth){

Expand Down Expand Up @@ -29617,6 +29663,15 @@ extern "C" int qb64_custom_event(int event,int v1,int v2,int v3,int v4,int v5,in
return NULL;
}//QB64_EVENT_RELATIVE_MOUSE_MOVEMENT

if (event==QB64_EVENT_FILE_DROP){
#ifdef QB64_WINDOWS
if (totalDroppedFiles > 0) DragFinish(hdrop);

hdrop=(HDROP)p1;
totalDroppedFiles = DragQueryFile ( hdrop, -1, NULL, 0 );
#endif
return NULL;
}

return -1;//Unknown command (use for debugging purposes only)
}//qb64_custom_event
Expand Down
5 changes: 5 additions & 0 deletions internal/c/parts/core/src/freeglut_main.c
Expand Up @@ -53,6 +53,7 @@ int qb64_custom_event(int event,int v1,int v2,int v3,int v4,int v5,int v6,int v7
#define QB64_EVENT_CLOSE 1
#define QB64_EVENT_KEY 2
#define QB64_EVENT_RELATIVE_MOUSE_MOVEMENT 3
#define QB64_EVENT_FILE_DROP 4

#define QBK 200000
#define VK 100000
Expand Down Expand Up @@ -2235,6 +2236,10 @@ LRESULT CALLBACK fgWindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,

break;

case WM_DROPFILES:
qb64_custom_event(QB64_EVENT_FILE_DROP,0,0,0,0,0,0,0,0,wParam,NULL);
break;

case WM_DESTROY:
/*
* The window already got destroyed, so don't bother with it.
Expand Down
3 changes: 3 additions & 0 deletions internal/c/qbx.cpp
Expand Up @@ -99,6 +99,9 @@ uint8**out_data,int32*out_x,int32 *out_y,int32*out_x_pre_increment,int32*out_x_p

extern void sub__title(qbs *title);
extern void sub__echo(qbs *message);
extern void sub__filedrop(int32 on_off=NULL);
extern int32 func__totaldroppedfiles();
extern qbs *func__droppedfile();

extern void sub__glrender(int32 method);
extern void sub__displayorder(int32 method1,int32 method2,int32 method3,int32 method4);
Expand Down
26 changes: 26 additions & 0 deletions source/subs_functions/subs_functions.bas
Expand Up @@ -898,6 +898,32 @@ id.arg = MKL$(STRINGTYPE - ISPOINTER)
id.NoCloud = 1
regid

clearid
id.n = "_ACCEPTFILEDROP"
id.subfunc = 2
id.callname = "sub__filedrop"
id.args = 1
id.arg = MKL$(LONGTYPE - ISPOINTER)
id.specialformat = "[{ON|OFF}]"
id.NoCloud = 1
regid

clearid
id.n = "_TOTALDROPPEDFILES"
id.subfunc = 1
id.callname = "func__totaldroppedfiles"
id.ret = LONGTYPE - ISPOINTER
regid

clearid
id.n = "_DROPPEDFILE"
id.musthave = "$"
id.subfunc = 1
id.callname = "func__droppedfile"
id.ret = STRINGTYPE - ISPOINTER
id.NoCloud = 1
regid

clearid
id.n = "CLEAR"
id.subfunc = 2
Expand Down

1 comment on commit c83b15e

@FellippeHeitor
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SCREEN _NEWIMAGE(128, 25, 0)

_ACCEPTFILEDROP
PRINT "Drag files from a folder and drop them in this window..."

DO
    IF _TOTALDROPPEDFILES THEN
        FOR i = 1 TO _TOTALDROPPEDFILES
            a$ = _DROPPEDFILE$
            COLOR 15
            PRINT i,
            IF _FILEEXISTS(a$) THEN
                COLOR 2: PRINT "file",
            ELSE
                IF _DIREXISTS(a$) THEN
                    COLOR 3: PRINT "folder",
                ELSE
                    COLOR 4: PRINT "not found", 'highly unlikely, but who knows?
                END IF
            END IF
            COLOR 15
            PRINT a$
        NEXT
    END IF

    _LIMIT 30
LOOP

Please sign in to comment.