]>
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 ( m_hCursorImageList
)
82 ImageList_Destroy((HIMAGELIST
) m_hCursorImageList
);
85 void wxDragImage::Init()
88 m_hCursorImageList
= 0;
89 m_window
= (wxWindow
*) NULL
;
94 ////////////////////////////////////////////////////////////////////////////
98 ////////////////////////////////////////////////////////////////////////////
100 // Create a drag image from a bitmap and optional cursor
101 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
104 ImageList_Destroy(GetHimageList());
108 if (image
.GetDepth() <= 4)
110 else if (image
.GetDepth() <= 8)
112 else if (image
.GetDepth() <= 16)
114 else if (image
.GetDepth() <= 24)
119 bool mask
= (image
.GetMask() != 0);
123 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
128 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
129 index
= ImageList_Add(GetHimageList(), hBitmap1
, 0);
133 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
134 HBITMAP hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
135 HBITMAP hbmpMask
= wxInvertMask(hBitmap2
);
137 index
= ImageList_Add(GetHimageList(), hBitmap1
, hbmpMask
);
138 ::DeleteObject(hbmpMask
);
142 wxLogError(_("Couldn't add an image to the image list."));
144 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
147 return (index
!= -1) ;
150 // Create a drag image from an icon and optional cursor
151 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
154 ImageList_Destroy(GetHimageList());
158 if (image
.GetDepth() <= 4)
160 else if (image
.GetDepth() <= 8)
162 else if (image
.GetDepth() <= 16)
164 else if (image
.GetDepth() <= 24)
172 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
174 HICON hIcon
= (HICON
) image
.GetHICON();
176 int index
= ImageList_AddIcon(GetHimageList(), hIcon
);
179 wxLogError(_("Couldn't add an image to the image list."));
182 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
185 return (index
!= -1) ;
188 // Create a drag image from a string and optional cursor
189 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
191 wxFont
font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
196 dc
.GetTextExtent(str
, & w
, & h
);
197 dc
.SetFont(wxNullFont
);
201 wxBitmap
bitmap((int) w
+2, (int) h
+2);
202 dc2
.SelectObject(bitmap
);
204 dc2
.SetBackground(* wxWHITE_BRUSH
);
206 dc2
.SetBackgroundMode(wxTRANSPARENT
);
207 dc2
.SetTextForeground(* wxLIGHT_GREY
);
208 dc2
.DrawText(str
, 0, 0);
209 dc2
.DrawText(str
, 1, 0);
210 dc2
.DrawText(str
, 2, 0);
211 dc2
.DrawText(str
, 1, 1);
212 dc2
.DrawText(str
, 2, 1);
213 dc2
.DrawText(str
, 1, 2);
214 dc2
.DrawText(str
, 2, 2);
215 dc2
.SetTextForeground(* wxBLACK
);
216 dc2
.DrawText(str
, 1, 1);
218 dc2
.SelectObject(wxNullBitmap
);
220 // Make the bitmap masked
221 wxImage
image(bitmap
);
222 image
.SetMaskColour(255, 255, 255);
223 bitmap
= image
.ConvertToBitmap();
225 return Create(bitmap
, cursor
, hotspot
);
228 // Create a drag image for the given tree control item
229 bool wxDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
232 ImageList_Destroy(GetHimageList());
233 m_hImageList
= (WXHIMAGELIST
) TreeView_CreateDragImage((HWND
) treeCtrl
.GetHWND(), (HTREEITEM
) (WXHTREEITEM
) id
);
237 // Create a drag image for the given list control item
238 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
241 ImageList_Destroy(GetHimageList());
244 m_hImageList
= (WXHIMAGELIST
) ListView_CreateDragImage((HWND
) listCtrl
.GetHWND(), id
, & pt
);
249 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, bool fullScreen
, wxRect
* rect
)
251 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in BeginDrag."));
252 wxASSERT_MSG( (window
!= 0), wxT("Window must not be null in BeginDrag."));
254 m_fullScreen
= fullScreen
;
256 m_boundingRect
= * rect
;
258 bool ret
= (ImageList_BeginDrag(GetHimageList(), 0, hotspot
.x
, hotspot
.y
) != 0);
262 wxFAIL_MSG( _T("BeginDrag failed.") );
269 if (!m_hCursorImageList
)
271 int cxCursor
= GetSystemMetrics(SM_CXCURSOR
);
272 int cyCursor
= GetSystemMetrics(SM_CYCURSOR
);
274 m_hCursorImageList
= (WXHIMAGELIST
) ImageList_Create(cxCursor
, cyCursor
, ILC_MASK
, 1, 1);
277 // First add the cursor to the image list
278 HCURSOR hCursor
= (HCURSOR
) m_cursor
.GetHCURSOR();
279 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hCursorImageList
, (HICON
) hCursor
);
281 wxASSERT_MSG( (cursorIndex
!= -1), wxT("ImageList_AddIcon failed in BeginDrag."));
283 if (cursorIndex
!= -1)
285 ImageList_SetDragCursorImage((HIMAGELIST
) m_hCursorImageList
, cursorIndex
, m_hotspot
.x
, m_hotspot
.y
);
292 ::SetCapture(GetHwndOf(window
));
297 // Begin drag. hotspot is the location of the drag position relative to the upper-left
298 // corner of the image. This is full screen only. fullScreenRect gives the
299 // position of the window on the screen, to restrict the drag to.
300 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
, wxWindow
* fullScreenRect
)
304 int x
= fullScreenRect
->GetPosition().x
;
305 int y
= fullScreenRect
->GetPosition().y
;
307 wxSize sz
= fullScreenRect
->GetSize();
309 if (fullScreenRect
->GetParent() && !fullScreenRect
->IsKindOf(CLASSINFO(wxFrame
)))
310 fullScreenRect
->GetParent()->ClientToScreen(& x
, & y
);
312 rect
.x
= x
; rect
.y
= y
;
313 rect
.width
= sz
.x
; rect
.height
= sz
.y
;
315 return BeginDrag(hotspot
, window
, TRUE
, & rect
);
319 bool wxDragImage::EndDrag()
321 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in EndDrag."));
325 if ( !::ReleaseCapture() )
327 wxLogLastError(wxT("ReleaseCapture"));
331 m_window
= (wxWindow
*) NULL
;
336 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
337 // is non-NULL, or in screen coordinates if NULL.
338 bool wxDragImage::Move(const wxPoint
& pt
)
340 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Move."));
342 // TODO: what coordinates are these in: window, client, or screen?
343 bool ret
= (ImageList_DragMove( pt
.x
, pt
.y
) != 0);
350 bool wxDragImage::Show()
352 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Show."));
355 if (m_window
&& !m_fullScreen
)
356 hWnd
= (HWND
) m_window
->GetHWND();
358 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
363 bool wxDragImage::Hide()
365 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Hide."));
368 if (m_window
&& !m_fullScreen
)
369 hWnd
= (HWND
) m_window
->GetHWND();
371 bool ret
= (ImageList_DragLeave( hWnd
) != 0);