]>
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"
47 #include "wx/msw/dragimag.h"
48 #include "wx/msw/private.h"
50 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 IMPLEMENT_DYNAMIC_CLASS(wxDragImage
, wxObject
)
60 #define GetHimageList() ((HIMAGELIST) m_hImageList)
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
67 // wxDragImage ctors/dtor
68 // ----------------------------------------------------------------------------
70 wxDragImage::wxDragImage()
75 wxDragImage::~wxDragImage()
78 ImageList_Destroy(GetHimageList());
83 ////////////////////////////////////////////////////////////////////////////
87 ////////////////////////////////////////////////////////////////////////////
89 // Create a drag image from a bitmap and optional cursor
90 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
93 ImageList_Destroy(GetHimageList());
97 bool mask
= TRUE
; // ?
101 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
103 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
104 HBITMAP hBitmap2
= 0;
105 if ( image
.GetMask() )
106 hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
108 int index
= ImageList_Add(GetHimageList(), hBitmap1
, hBitmap2
);
111 wxLogError(_("Couldn't add an image to the image list."));
114 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
117 return (index
!= -1) ;
120 // Create a drag image from an icon and optional cursor
121 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
124 ImageList_Destroy(GetHimageList());
128 bool mask
= TRUE
; // ?
132 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
134 HICON hIcon
= (HICON
) image
.GetHICON();
136 int index
= ImageList_AddIcon(GetHimageList(), hIcon
);
139 wxLogError(_("Couldn't add an image to the image list."));
142 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
145 return (index
!= -1) ;
148 // Create a drag image from a string and optional cursor
149 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
151 wxFont
font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
156 dc
.GetTextExtent(str
, & w
, & h
);
160 wxBitmap
bitmap((int) w
, (int) h
);
161 dc2
.SelectObject(bitmap
);
163 dc2
.SetBackground(* wxWHITE_BRUSH
);
165 dc2
.DrawText(str
, 0, 0);
167 dc2
.SelectObject(wxNullBitmap
);
169 return Create(bitmap
, cursor
, hotspot
);
172 // Create a drag image for the given tree control item
173 bool wxDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
176 ImageList_Destroy(GetHimageList());
177 m_hImageList
= (WXHIMAGELIST
) TreeView_CreateDragImage((HWND
) treeCtrl
.GetHWND(), (HTREEITEM
) (WXHTREEITEM
) id
);
181 // Create a drag image for the given list control item
182 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
185 ImageList_Destroy(GetHimageList());
188 m_hImageList
= (WXHIMAGELIST
) ListView_CreateDragImage((HWND
) listCtrl
.GetHWND(), id
, & pt
);
193 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* window
)
195 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in BeginDrag."));
197 bool ret
= (ImageList_BeginDrag(GetHimageList(), 0, hotspot
.x
, hotspot
.y
) != 0);
201 wxFAIL_MSG( _T("BeginDrag failed.") );
208 // First add the cursor to the image list
209 int cursorIndex
= ImageList_AddIcon(GetHimageList(), (HICON
) m_cursor
.GetHCURSOR());
211 wxASSERT_MSG( (cursorIndex
!= -1), wxT("ImageList_AddIcon failed in BeginDrag."));
213 if (cursorIndex
!= -1)
215 ImageList_SetDragCursorImage(GetHimageList(), cursorIndex
, m_hotspot
.x
, m_hotspot
.y
);
220 ::SetCapture(GetHwndOf(window
));
226 bool wxDragImage::EndDrag(wxWindow
* WXUNUSED(window
))
228 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in EndDrag."));
232 if ( !::ReleaseCapture() )
234 wxLogLastError("ReleaseCapture");
242 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
243 // is non-NULL, or in screen coordinates if NULL.
244 bool wxDragImage::Move(const wxPoint
& pt
, wxWindow
* window
)
246 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Move."));
248 // TODO: what coordinates are these in: window, client, or screen?
249 bool ret
= (ImageList_DragMove( pt
.x
, pt
.y
) != 0);
256 bool wxDragImage::Show(wxWindow
* window
)
258 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Show."));
262 hWnd
= (HWND
) window
->GetHWND();
264 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
269 bool wxDragImage::Hide(wxWindow
* window
)
271 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Hide."));
275 hWnd
= (HWND
) window
->GetHWND();
277 bool ret
= (ImageList_DragLeave( hWnd
) != 0);