Skip to content

Commit

Permalink
FIX: FEAT: fixed crash when user dropped file into window (with `drop…
Browse files Browse the repository at this point in the history
…able` flag)

and fixed crash when calling `show` with `none` value.
It's now possible to receive `drop-file` event with file name in `event/data`.
It's also possible to resolve target drop using `map-event` function.
  • Loading branch information
Oldes committed Feb 26, 2019
1 parent 1831fc2 commit 785ea9f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
12 changes: 11 additions & 1 deletion src/core/n-data.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
***********************************************************************/

#include "sys-core.h"
#include "reb-evtypes.h" // for EVT_DROP_EVENT

#ifdef REMOVED
// Removed because it causes more trouble than the benefits it provides.
Expand Down Expand Up @@ -1057,9 +1058,18 @@ static int Do_Ordinal(REBVAL *ds, REBINT n)
***********************************************************************/
{
REBVAL *val = D_ARG(1);
REBGOB *gob = VAL_EVENT_SER(val);
REBGOB *gob;
REBXYF xy;

//O: handle drop_file this way? Or maybe just don't use EVF_HAS_XY?
//gob = (VAL_EVENT_TYPE(val) == EVT_DROP_FILE) ? Gob_Root : VAL_EVENT_SER(val);
if (GET_FLAG(VAL_EVENT_FLAGS(val), EVF_HAS_DATA)) {
CLR_FLAG(VAL_EVENT_FLAGS(val), EVF_HAS_DATA);
gob = Gob_Root;
} else {
gob = VAL_EVENT_SER(val);
}

if (gob && GET_FLAG(VAL_EVENT_FLAGS(val), EVF_HAS_XY)) {
xy.x = (REBD32)VAL_EVENT_X(val);
xy.y = (REBD32)VAL_EVENT_Y(val);
Expand Down
3 changes: 3 additions & 0 deletions src/core/t-event.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@
case SYM_WINDOW:
case SYM_GOB:
if (IS_EVENT_MODEL(value, EVM_GUI)) {
if (GET_FLAG(VAL_EVENT_FLAGS(value), EVF_HAS_DATA))
goto is_none;
if (VAL_EVENT_SER(value)) {
SET_GOB(val, VAL_EVENT_SER(value));
break;
Expand Down Expand Up @@ -294,6 +296,7 @@

case SYM_DATA:
// Event holds a file string:
if (!GET_FLAG(VAL_EVENT_FLAGS(value), EVF_HAS_DATA)) goto is_none;
if (VAL_EVENT_TYPE(value) != EVT_DROP_FILE) goto is_none;
if (!GET_FLAG(VAL_EVENT_FLAGS(value), EVF_COPIED)) {
void *str = VAL_EVENT_SER(value);
Expand Down
1 change: 1 addition & 0 deletions src/include/reb-event.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ enum {
EVF_DOUBLE, // double click detected
EVF_CONTROL,
EVF_SHIFT,
EVF_HAS_DATA, // drop_file event series contains data instead of gob
};


Expand Down
23 changes: 11 additions & 12 deletions src/os/win32/host-event.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,30 +134,29 @@ static void Add_File_Events(REBGOB *gob, REBINT flags, HDROP drop)
REBINT num;
REBINT len;
REBINT i;
REBCHR* buf;
REBCHR buf[MAX_PATH] = {0};
POINT xy;

//Get the mouse position
DragQueryPoint(drop, &xy);

evt.type = EVT_DROP_FILE;
evt.flags = (u8) (flags | (1<<EVF_HAS_XY));
evt.flags = (u8) (flags | (1<<EVF_HAS_XY) | (1<<EVF_COPIED) | (1<<EVF_HAS_DATA));
evt.model = EVM_GUI;
evt.data = xy.x | xy.y<<16;

// reporting XY as an absolute position, so the target gob can be located using map-event from Gob_Root
evt.data = (xy.x + GOB_X_INT(gob)) | (xy.y + GOB_Y_INT(gob))<<16;

num = DragQueryFile(drop, -1, NULL, 0);

for (i = 0; i < num; i++){
len = DragQueryFile(drop, i, NULL, 0);
buf = OS_Make(len+1);
DragQueryFile(drop, i, buf, len+1);
//Reb_Print("DROP: %s", buf);
buf[len] = 0;
// ?! convert to REBOL format? E.g.: evt.ser = OS_To_REBOL_File(buf, &len);
OS_Free(buf);
// NOTE: originaly the buffer was made using OS_Make and freed when not needed
// but for some reason it was causing a crash! Use GlobalAlloc/GlobalLock?
len = DragQueryFile(drop, i, buf, MAX_PATH-1);
// printf("DROP: %d %ls\n", len, buf);
evt.ser = RL_Encode_UTF8_String(buf, len, TRUE, 0);
if (!RL_Event(&evt)) break; // queue is full
}
DragFinish(drop);
}

static Check_Modifiers(REBINT flags)
Expand Down
7 changes: 4 additions & 3 deletions src/os/win32/host-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -979,9 +979,10 @@ static void Free_Window(REBGOB *gob) {
switch (cmd) {
case CMD_WINDOW_SHOW:
{
REBGOB* gob = (REBGOB*)RXA_SERIES(frm, 1);
OS_Show_Gob(gob);
RXA_TYPE(frm, 1) = RXT_GOB;
if (RXA_TYPE(frm, 1) == RXT_GOB) {
REBGOB* gob = (REBGOB*)RXA_SERIES(frm, 1);
OS_Show_Gob(gob);
}
return RXR_VALUE;
}
case CMD_WINDOW_GUI_METRIC:
Expand Down

0 comments on commit 785ea9f

Please sign in to comment.