]>
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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dragimag.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18 #include "wx/msw/private.h"
24 #if defined(__WIN95__)
29 #include "wx/window.h"
30 #include "wx/dcclient.h"
31 #include "wx/dcscreen.h"
32 #include "wx/dcmemory.h"
33 #include "wx/settings.h"
39 #include "wx/msw/dragimag.h"
40 #include "wx/msw/private.h"
42 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
46 IMPLEMENT_DYNAMIC_CLASS(wxDragImage
, wxObject
)
48 wxDragImage::wxDragImage()
53 wxDragImage::~wxDragImage()
56 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
62 ////////////////////////////////////////////////////////////////////////////
66 ////////////////////////////////////////////////////////////////////////////
68 // Create a drag image from a bitmap and optional cursor
69 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
72 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
76 bool mask
= TRUE
; // ?
80 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
82 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
84 if ( image
.GetMask() )
85 hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
87 int index
= ImageList_Add((HIMAGELIST
) m_hImageList
, hBitmap1
, hBitmap2
);
90 wxLogError(_("Couldn't add an image to the image list."));
93 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
96 return (index
!= -1) ;
99 // Create a drag image from an icon and optional cursor
100 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
103 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
107 bool mask
= TRUE
; // ?
111 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
113 HICON hIcon
= (HICON
) image
.GetHICON();
115 int index
= ImageList_AddIcon((HIMAGELIST
) m_hImageList
, hIcon
);
118 wxLogError(_("Couldn't add an image to the image list."));
121 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
124 return (index
!= -1) ;
127 // Create a drag image from a string and optional cursor
128 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
130 wxFont
font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
135 dc
.GetTextExtent(str
, & w
, & h
);
139 wxBitmap
bitmap((int) w
, (int) h
);
140 dc2
.SelectObject(bitmap
);
142 dc2
.SetBackground(* wxWHITE_BRUSH
);
144 dc2
.DrawText(str
, 0, 0);
146 dc2
.SelectObject(wxNullBitmap
);
148 return Create(bitmap
, cursor
, hotspot
);
151 // Create a drag image for the given tree control item
152 bool wxDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
155 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
156 m_hImageList
= (WXHIMAGELIST
) TreeView_CreateDragImage((HWND
) treeCtrl
.GetHWND(), (HTREEITEM
) (WXHTREEITEM
) id
);
160 // Create a drag image for the given list control item
161 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
164 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
167 m_hImageList
= (WXHIMAGELIST
) ListView_CreateDragImage((HWND
) listCtrl
.GetHWND(), id
, & pt
);
172 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* WXUNUSED(window
))
174 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in BeginDrag."));
176 bool ret
= (ImageList_BeginDrag((HIMAGELIST
) m_hImageList
, 0, hotspot
.x
, hotspot
.y
) != 0);
178 wxASSERT_MSG( (ret
), wxT("BeginDrag failed."));
185 // First add the cursor to the image list
186 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hImageList
, (HICON
) m_cursor
.GetHCURSOR());
188 wxASSERT_MSG( (cursorIndex
!= -1), wxT("ImageList_AddIcon failed in BeginDrag."));
190 if (cursorIndex
!= -1)
192 ImageList_SetDragCursorImage((HIMAGELIST
) m_hImageList
, cursorIndex
, m_hotspot
.x
, m_hotspot
.y
);
202 bool wxDragImage::EndDrag(wxWindow
* WXUNUSED(window
))
204 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in EndDrag."));
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.
215 bool wxDragImage::Move(const wxPoint
& pt
, wxWindow
* window
)
217 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Move."));
219 // TODO: what coordinates are these in: window, client, or screen?
220 bool ret
= (ImageList_DragMove( pt
.x
, pt
.y
) != 0);
227 bool wxDragImage::Show(wxWindow
* window
)
229 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Show."));
233 hWnd
= (HWND
) window
->GetHWND();
235 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
240 bool wxDragImage::Hide(wxWindow
* window
)
242 wxASSERT_MSG( (m_hImageList
!= 0), wxT("Image list must not be null in Hide."));
246 hWnd
= (HWND
) window
->GetHWND();
248 bool ret
= (ImageList_DragLeave( hWnd
) != 0);