]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/dragimag.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDragImage
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dragimag.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26 #include "wx/msw/private.h"
32 #if defined(__WIN95__)
37 #include "wx/window.h"
38 #include "wx/dcclient.h"
39 #include "wx/dcscreen.h"
40 #include "wx/dcmemory.h"
41 #include "wx/settings.h"
49 #include "wx/msw/dragimag.h"
50 #include "wx/msw/private.h"
52 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 IMPLEMENT_DYNAMIC_CLASS(wxDragImage
, wxObject
)
62 #define GetHimageList() ((HIMAGELIST) m_hImageList)
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
69 // wxDragImage ctors/dtor
70 // ----------------------------------------------------------------------------
72 wxDragImage::wxDragImage()
77 wxDragImage::~wxDragImage()
80 ImageList_Destroy(GetHimageList());
81 #if !wxUSE_SIMPLER_DRAGIMAGE
82 if ( m_hCursorImageList
)
83 ImageList_Destroy((HIMAGELIST
) m_hCursorImageList
);
87 void wxDragImage::Init()
90 #if !wxUSE_SIMPLER_DRAGIMAGE
91 m_hCursorImageList
= 0;
93 m_window
= (wxWindow
*) NULL
;
98 ////////////////////////////////////////////////////////////////////////////
102 ////////////////////////////////////////////////////////////////////////////
104 // Create a drag image from a bitmap and optional cursor
105 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
)
108 ImageList_Destroy(GetHimageList());
112 if (image
.GetDepth() <= 4)
114 else if (image
.GetDepth() <= 8)
116 else if (image
.GetDepth() <= 16)
118 else if (image
.GetDepth() <= 24)
123 bool mask
= (image
.GetMask() != 0);
127 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
132 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
133 index
= ImageList_Add(GetHimageList(), hBitmap1
, 0);
137 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
138 HBITMAP hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
139 HBITMAP hbmpMask
= wxInvertMask(hBitmap2
);
141 index
= ImageList_Add(GetHimageList(), hBitmap1
, hbmpMask
);
142 ::DeleteObject(hbmpMask
);
146 wxLogError(_("Couldn't add an image to the image list."));
148 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
150 return (index
!= -1) ;
153 // Create a drag image from an icon and optional cursor
154 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
)
157 ImageList_Destroy(GetHimageList());
161 if (image
.GetDepth() <= 4)
163 else if (image
.GetDepth() <= 8)
165 else if (image
.GetDepth() <= 16)
167 else if (image
.GetDepth() <= 24)
175 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
177 HICON hIcon
= (HICON
) image
.GetHICON();
179 int index
= ImageList_AddIcon(GetHimageList(), hIcon
);
182 wxLogError(_("Couldn't add an image to the image list."));
185 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
187 return (index
!= -1) ;
190 // Create a drag image from a string and optional cursor
191 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
)
193 wxFont
font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
198 dc
.GetTextExtent(str
, & w
, & h
);
199 dc
.SetFont(wxNullFont
);
203 wxBitmap
bitmap((int) w
+2, (int) h
+2);
204 dc2
.SelectObject(bitmap
);
206 dc2
.SetBackground(* wxWHITE_BRUSH
);
208 dc2
.SetBackgroundMode(wxTRANSPARENT
);
209 dc2
.SetTextForeground(* wxLIGHT_GREY
);
210 dc2
.DrawText(str
, 0, 0);
211 dc2
.DrawText(str
, 1, 0);
212 dc2
.DrawText(str
, 2, 0);
213 dc2
.DrawText(str
, 1, 1);
214 dc2
.DrawText(str
, 2, 1);
215 dc2
.DrawText(str
, 1, 2);
216 dc2
.DrawText(str
, 2, 2);
217 dc2
.SetTextForeground(* wxBLACK
);
218 dc2
.DrawText(str
, 1, 1);
220 dc2
.SelectObject(wxNullBitmap
);
222 // Make the bitmap masked
223 wxImage
image(bitmap
);
224 image
.SetMaskColour(255, 255, 255);
225 bitmap
= image
.ConvertToBitmap();
227 return Create(bitmap
, cursor
);
230 // Create a drag image for the given tree control item
231 bool wxDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
234 ImageList_Destroy(GetHimageList());
235 m_hImageList
= (WXHIMAGELIST
) TreeView_CreateDragImage((HWND
) treeCtrl
.GetHWND(), (HTREEITEM
) (WXHTREEITEM
) id
);
239 // Create a drag image for the given list control item
240 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
243 ImageList_Destroy(GetHimageList());
246 m_hImageList
= (WXHIMAGELIST
) ListView_CreateDragImage((HWND
) listCtrl
.GetHWND(), id
, & pt
);
251 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
, wxRect
* rect
)
253 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in BeginDrag."));
254 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
256 m_fullScreen
= fullScreen
;
258 m_boundingRect
= * rect
;
260 bool ret
= (ImageList_BeginDrag(GetHimageList(), 0, hotspot
.x
, hotspot
.y
) != 0);
261 //bool ret = (ImageList_BeginDrag(GetHimageList(), 0, 0, 0) != 0);
265 wxFAIL_MSG( _T("BeginDrag failed.") );
272 #if wxUSE_SIMPLER_DRAGIMAGE
273 m_oldCursor
= window
->GetCursor();
274 window
->SetCursor(m_cursor
);
276 if (!m_hCursorImageList
)
278 int cxCursor
= GetSystemMetrics(SM_CXCURSOR
);
279 int cyCursor
= GetSystemMetrics(SM_CYCURSOR
);
281 m_hCursorImageList
= (WXHIMAGELIST
) ImageList_Create(cxCursor
, cyCursor
, ILC_MASK
, 1, 1);
284 // See if we can find the cursor hotspot
285 wxPoint
curHotSpot(hotspot
);
289 if (::GetIconInfo((HICON
) (HCURSOR
) m_cursor
.GetHCURSOR(), & iconInfo
) != 0)
291 curHotSpot
.x
-= iconInfo
.xHotspot
;
292 curHotSpot
.y
-= iconInfo
.yHotspot
;
296 msg
.Printf("Hotspot = %d, %d", curHotSpot
.x
, curHotSpot
.y
);
299 // First add the cursor to the image list
300 HCURSOR hCursor
= (HCURSOR
) m_cursor
.GetHCURSOR();
301 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hCursorImageList
, (HICON
) hCursor
);
303 wxASSERT_MSG( (cursorIndex
!= -1), wxT("ImageList_AddIcon failed in BeginDrag."));
305 if (cursorIndex
!= -1)
307 ImageList_SetDragCursorImage((HIMAGELIST
) m_hCursorImageList
, cursorIndex
, curHotSpot
.x
, curHotSpot
.y
);
312 #if !wxUSE_SIMPLER_DRAGIMAGE
319 ::SetCapture(GetHwndOf(window
));
324 // Begin drag. hotspot is the location of the drag position relative to the upper-left
325 // corner of the image. This is full screen only. fullScreenRect gives the
326 // position of the window on the screen, to restrict the drag to.
327 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
331 int x
= fullScreenRect
->GetPosition().x
;
332 int y
= fullScreenRect
->GetPosition().y
;
334 wxSize sz
= fullScreenRect
->GetSize();
336 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
337 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
339 rect
.x
= x
; rect
.y
= y
;
340 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
342 return BeginDrag(hotspot
, window
, TRUE
, & rect
);
346 bool wxDragImage::EndDrag()
348 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in EndDrag."));
352 if ( !::ReleaseCapture() )
354 wxLogLastError(wxT("ReleaseCapture"));
357 #if wxUSE_SIMPLER_DRAGIMAGE
358 if (m_cursor
.Ok() && m_oldCursor
.Ok())
359 m_window
->SetCursor(m_oldCursor
);
364 m_window
= (wxWindow
*) NULL
;
369 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
370 // is non-NULL, or in screen coordinates if NULL.
371 bool wxDragImage::Move(const wxPoint
& pt
)
373 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Move."));
375 // These are in window, not client coordinates.
376 // So need to convert to client coordinates.
378 if (m_window
&& !m_fullScreen
)
381 rect
.left
= 0; rect
.top
= 0;
382 rect
.right
= 0; rect
.bottom
= 0;
383 DWORD style
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_STYLE
);
385 DWORD exStyle
= ::GetWindowLong((HWND
) m_window
->GetHWND(), GWL_EXSTYLE
);
386 ::AdjustWindowRectEx(& rect
, style
, FALSE
, exStyle
);
388 ::AdjustWindowRect(& rect
, style
, FALSE
);
390 // Subtract the (negative) values, i.e. add a small increment
391 pt2
.x
-= rect
.left
; pt2
.y
-= rect
.top
;
393 else if (m_window
&& m_fullScreen
)
395 pt2
= m_window
->ClientToScreen(pt2
);
398 bool ret
= (ImageList_DragMove( pt2
.x
, pt2
.y
) != 0);
405 bool wxDragImage::Show()
407 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Show."));
410 if (m_window
&& !m_fullScreen
)
411 hWnd
= (HWND
) m_window
->GetHWND();
413 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
418 bool wxDragImage::Hide()
420 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Hide."));
423 if (m_window
&& !m_fullScreen
)
424 hWnd
= (HWND
) m_window
->GetHWND();
426 bool ret
= (ImageList_DragLeave( hWnd
) != 0);