]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dragimag.cpp
1. some fixes for the problems reported by BoundsChecker
[wxWidgets.git] / src / msw / dragimag.cpp
CommitLineData
7cf83330
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: dragimag.cpp
3// Purpose: wxDragImage
4// Author: Julian Smart
5// Modified by:
6// Created: 08/04/99
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "dragimag.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
ff0ea71c 18#include "wx/msw/private.h"
7cf83330
JS
19
20#ifdef __BORLANDC__
21#pragma hdrstop
22#endif
23
24#if defined(__WIN95__)
25
26#ifndef WX_PRECOMP
27#include <stdio.h>
28#include "wx/setup.h"
29#include "wx/window.h"
30#include "wx/dcclient.h"
2662e49e
RR
31#include "wx/dcscreen.h"
32#include "wx/dcmemory.h"
33#include "wx/settings.h"
7cf83330
JS
34#endif
35
36#include "wx/log.h"
37#include "wx/intl.h"
38
39#include "wx/msw/dragimag.h"
40#include "wx/msw/private.h"
41
65fd5cb0 42#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
7cf83330
JS
43#include <commctrl.h>
44#endif
45
7cf83330 46IMPLEMENT_DYNAMIC_CLASS(wxDragImage, wxObject)
7cf83330
JS
47
48wxDragImage::wxDragImage()
49{
50 m_hImageList = 0;
51}
52
53wxDragImage::~wxDragImage()
54{
55 if ( m_hImageList )
56 ImageList_Destroy((HIMAGELIST) m_hImageList);
57 m_hImageList = 0;
58}
59
60
61// Attributes
62////////////////////////////////////////////////////////////////////////////
63
64
65// Operations
66////////////////////////////////////////////////////////////////////////////
67
68// Create a drag image from a bitmap and optional cursor
69bool wxDragImage::Create(const wxBitmap& image, const wxCursor& cursor, const wxPoint& hotspot)
70{
71 if ( m_hImageList )
72 ImageList_Destroy((HIMAGELIST) m_hImageList);
73 m_hImageList = 0;
74
75 UINT flags = 0;
76 bool mask = TRUE; // ?
77 if ( mask )
78 flags |= ILC_MASK;
79
80 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
81
82 HBITMAP hBitmap1 = (HBITMAP) image.GetHBITMAP();
83 HBITMAP hBitmap2 = 0;
84 if ( image.GetMask() )
85 hBitmap2 = (HBITMAP) image.GetMask()->GetMaskBitmap();
86
87 int index = ImageList_Add((HIMAGELIST) m_hImageList, hBitmap1, hBitmap2);
88 if ( index == -1 )
89 {
90 wxLogError(_("Couldn't add an image to the image list."));
91 }
92
93 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
94 m_hotspot = hotspot;
95
96 return (index != -1) ;
97}
98
99// Create a drag image from an icon and optional cursor
100bool wxDragImage::Create(const wxIcon& image, const wxCursor& cursor, const wxPoint& hotspot)
101{
102 if ( m_hImageList )
103 ImageList_Destroy((HIMAGELIST) m_hImageList);
104 m_hImageList = 0;
105
106 UINT flags = 0;
107 bool mask = TRUE; // ?
108 if ( mask )
109 flags |= ILC_MASK;
110
111 m_hImageList = (WXHIMAGELIST) ImageList_Create(image.GetWidth(), image.GetHeight(), flags, 1, 1);
112
113 HICON hIcon = (HICON) image.GetHICON();
114
115 int index = ImageList_AddIcon((HIMAGELIST) m_hImageList, hIcon);
116 if ( index == -1 )
117 {
118 wxLogError(_("Couldn't add an image to the image list."));
119 }
120
121 m_cursor = cursor; // Can only combine with drag image after calling BeginDrag.
122 m_hotspot = hotspot;
123
124 return (index != -1) ;
125}
126
127// Create a drag image from a string and optional cursor
128bool wxDragImage::Create(const wxString& str, const wxCursor& cursor, const wxPoint& hotspot)
129{
130 wxFont font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
131
132 long w, h;
133 wxScreenDC dc;
134 dc.SetFont(font);
135 dc.GetTextExtent(str, & w, & h);
136
137 wxMemoryDC dc2;
138 dc2.SetFont(font);
139 wxBitmap bitmap((int) w, (int) h);
140 dc2.SelectObject(bitmap);
141
142 dc2.SetBackground(* wxWHITE_BRUSH);
143 dc2.Clear();
144 dc2.DrawText(str, 0, 0);
145
146 dc2.SelectObject(wxNullBitmap);
147
148 return Create(bitmap, cursor, hotspot);
149}
150
151// Create a drag image for the given tree control item
152bool wxDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
153{
154 if ( m_hImageList )
155 ImageList_Destroy((HIMAGELIST) m_hImageList);
156 m_hImageList = (WXHIMAGELIST) TreeView_CreateDragImage((HWND) treeCtrl.GetHWND(), (HTREEITEM) (WXHTREEITEM) id);
157 return TRUE;
158}
159
160// Create a drag image for the given list control item
161bool wxDragImage::Create(const wxListCtrl& listCtrl, long id)
162{
163 if ( m_hImageList )
164 ImageList_Destroy((HIMAGELIST) m_hImageList);
165 POINT pt;
166 pt.x = 0; pt.y = 0;
167 m_hImageList = (WXHIMAGELIST) ListView_CreateDragImage((HWND) listCtrl.GetHWND(), id, & pt);
168 return TRUE;
169}
170
171// Begin drag
172bool wxDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* WXUNUSED(window))
173{
223d09f6 174 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in BeginDrag."));
7cf83330
JS
175
176 bool ret = (ImageList_BeginDrag((HIMAGELIST) m_hImageList, 0, hotspot.x, hotspot.y) != 0);
177
223d09f6 178 wxASSERT_MSG( (ret), wxT("BeginDrag failed."));
7cf83330
JS
179
180 if (!ret)
181 return FALSE;
182
183 if (m_cursor.Ok())
184 {
185 // First add the cursor to the image list
186 int cursorIndex = ImageList_AddIcon((HIMAGELIST) m_hImageList, (HICON) m_cursor.GetHCURSOR());
187
223d09f6 188 wxASSERT_MSG( (cursorIndex != -1), wxT("ImageList_AddIcon failed in BeginDrag."));
7cf83330
JS
189
190 if (cursorIndex != -1)
191 {
192 ImageList_SetDragCursorImage((HIMAGELIST) m_hImageList, cursorIndex, m_hotspot.x, m_hotspot.y);
193 }
194 }
195
196 ::ShowCursor(FALSE);
197
198 return TRUE;
199}
200
201// End drag
202bool wxDragImage::EndDrag(wxWindow* WXUNUSED(window))
203{
223d09f6 204 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in EndDrag."));
7cf83330
JS
205
206 ImageList_EndDrag();
207
208 ::ShowCursor(TRUE);
209
210 return TRUE;
211}
212
213// Move the image: call from OnMouseMove. Pt is in window client coordinates if window
214// is non-NULL, or in screen coordinates if NULL.
215bool wxDragImage::Move(const wxPoint& pt, wxWindow* window)
216{
223d09f6 217 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Move."));
7cf83330
JS
218
219 // TODO: what coordinates are these in: window, client, or screen?
220 bool ret = (ImageList_DragMove( pt.x, pt.y ) != 0);
221
222 m_position = pt;
223
224 return ret;
225}
226
227bool wxDragImage::Show(wxWindow* window)
228{
223d09f6 229 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Show."));
7cf83330
JS
230
231 HWND hWnd = 0;
232 if (window)
233 hWnd = (HWND) window->GetHWND();
234
235 bool ret = (ImageList_DragEnter( hWnd, m_position.x, m_position.y ) != 0);
236
237 return ret;
238}
239
240bool wxDragImage::Hide(wxWindow* window)
241{
223d09f6 242 wxASSERT_MSG( (m_hImageList != 0), wxT("Image list must not be null in Hide."));
7cf83330
JS
243
244 HWND hWnd = 0;
245 if (window)
246 hWnd = (HWND) window->GetHWND();
247
248 bool ret = (ImageList_DragLeave( hWnd ) != 0);
249
250 return ret;
251}
252
253#endif
254 // __WIN95__
255