summaryrefslogtreecommitdiff
path: root/legacy/ecore/src/lib/ecore_win32/ecore_win32_dnd.c
blob: 6ce9367ece0963702bb9df618f982dd0f29cd12d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <windows.h>

#include "Ecore_Win32.h"
#include "ecore_win32_private.h"


static int _ecore_win32_dnd_init_count = 0;

static HANDLE DataToHandle(const char *data, int size)
{
   char *ptr;
   ptr = (char *)GlobalAlloc(GMEM_FIXED, size);
   memcpy(ptr, data, size);
   return ptr;
}


int
ecore_win32_dnd_init()
{
   if (_ecore_win32_dnd_init_count > 0)
     {
	_ecore_win32_dnd_init_count++;
	return _ecore_win32_dnd_init_count;
     }

   if (OleInitialize(NULL) != S_OK)
     return 0;

   _ecore_win32_dnd_init_count++;

   return _ecore_win32_dnd_init_count;
}

int ecore_win32_dnd_shutdown()
{
   _ecore_win32_dnd_init_count--;
   if (_ecore_win32_dnd_init_count > 0) return _ecore_win32_dnd_init_count;

   OleUninitialize();

   if (_ecore_win32_dnd_init_count < 0) _ecore_win32_dnd_init_count = 0;

   return _ecore_win32_dnd_init_count;
}

int ecore_win32_dnd_begin(const char *data,
                          int         size)
{
   if (data == NULL)
      return 0;
   if (size < 0)
      size = strlen(data) + 1;

   IDataObject *pDataObject = NULL;
   IDropSource *pDropSource = NULL;

   FORMATETC fmtetc = { CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
   STGMEDIUM stgmed = { TYMED_HGLOBAL, { 0 }, 0 };

   stgmed.hGlobal = DataToHandle(data, size);

   int res = 0;

   // create the data object
   pDataObject = (IDataObject *)_ecore_win32_dnd_data_object_new((void *)&fmtetc,
                                                                 (void *)&stgmed,
                                                                 1);
   pDropSource = (IDropSource *)_ecore_win32_dnd_drop_source_new();

   if (pDataObject && pDropSource)
   {
      DWORD dwResult;
      DWORD dwEffect = DROPEFFECT_COPY;

      // do the drag-drop!
      dwResult = DoDragDrop(pDataObject, pDropSource, DROPEFFECT_COPY, &dwEffect);

      // finished. Check the return values to see if we need to do anything else
      if (dwResult == DRAGDROP_S_DROP)
      {
         //printf(">>> \"%s\" Dropped <<<\n", str);
         if(dwEffect == DROPEFFECT_MOVE)
         {
            // remove the data we just dropped from active document
         }
      }
      //else if (dwResult == DRAGDROP_S_CANCEL)
      //   printf("DND cancelled\n");
      //else
      //   printf("DND error\n");

      res = 1;
   }

   _ecore_win32_dnd_data_object_free(pDataObject);
   _ecore_win32_dnd_drop_source_free(pDropSource);

   // cleanup
   ReleaseStgMedium(&stgmed);
   return (int)res;
}

int ecore_win32_dnd_register_drop_target(Ecore_Win32_Window                 *window,
                                         Ecore_Win32_Dnd_DropTarget_Callback callback)
{
   if (window == NULL)
      return 0;

   struct _Ecore_Win32_Window *wnd = (struct _Ecore_Win32_Window *)window;
   wnd->dnd_drop_target = _ecore_win32_dnd_register_drop_window(wnd->window,
                                                                callback,
                                                                (void *)wnd);
   return (int)(wnd->dnd_drop_target != NULL);
}

void ecore_win32_dnd_unregister_drop_target(Ecore_Win32_Window *window)
{
   if (window == NULL)
      return;

   struct _Ecore_Win32_Window *wnd = (struct _Ecore_Win32_Window *)window;
   if (wnd->dnd_drop_target != NULL)
      _ecore_win32_dnd_unregister_drop_window(wnd->window, wnd->dnd_drop_target);
}