]>
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"
23 #if defined(__WIN95__)
28 #include "wx/window.h"
29 #include "wx/dcclient.h"
30 #include "wx/dcscreen.h"
31 #include "wx/dcmemory.h"
32 #include "wx/settings.h"
38 #include "wx/msw/dragimag.h"
39 #include "wx/msw/private.h"
41 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__)
45 #if !USE_SHARED_LIBRARY
46 IMPLEMENT_DYNAMIC_CLASS(wxDragImage
, wxObject
)
49 wxDragImage::wxDragImage()
54 wxDragImage::~wxDragImage()
57 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
63 ////////////////////////////////////////////////////////////////////////////
67 ////////////////////////////////////////////////////////////////////////////
69 // Create a drag image from a bitmap and optional cursor
70 bool wxDragImage::Create(const wxBitmap
& image
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
73 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
77 bool mask
= TRUE
; // ?
81 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
83 HBITMAP hBitmap1
= (HBITMAP
) image
.GetHBITMAP();
85 if ( image
.GetMask() )
86 hBitmap2
= (HBITMAP
) image
.GetMask()->GetMaskBitmap();
88 int index
= ImageList_Add((HIMAGELIST
) m_hImageList
, hBitmap1
, hBitmap2
);
91 wxLogError(_("Couldn't add an image to the image list."));
94 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
97 return (index
!= -1) ;
100 // Create a drag image from an icon and optional cursor
101 bool wxDragImage::Create(const wxIcon
& image
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
104 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
108 bool mask
= TRUE
; // ?
112 m_hImageList
= (WXHIMAGELIST
) ImageList_Create(image
.GetWidth(), image
.GetHeight(), flags
, 1, 1);
114 HICON hIcon
= (HICON
) image
.GetHICON();
116 int index
= ImageList_AddIcon((HIMAGELIST
) m_hImageList
, hIcon
);
119 wxLogError(_("Couldn't add an image to the image list."));
122 m_cursor
= cursor
; // Can only combine with drag image after calling BeginDrag.
125 return (index
!= -1) ;
128 // Create a drag image from a string and optional cursor
129 bool wxDragImage::Create(const wxString
& str
, const wxCursor
& cursor
, const wxPoint
& hotspot
)
131 wxFont
font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
136 dc
.GetTextExtent(str
, & w
, & h
);
140 wxBitmap
bitmap((int) w
, (int) h
);
141 dc2
.SelectObject(bitmap
);
143 dc2
.SetBackground(* wxWHITE_BRUSH
);
145 dc2
.DrawText(str
, 0, 0);
147 dc2
.SelectObject(wxNullBitmap
);
149 return Create(bitmap
, cursor
, hotspot
);
152 // Create a drag image for the given tree control item
153 bool wxDragImage::Create(const wxTreeCtrl
& treeCtrl
, wxTreeItemId
& id
)
156 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
157 m_hImageList
= (WXHIMAGELIST
) TreeView_CreateDragImage((HWND
) treeCtrl
.GetHWND(), (HTREEITEM
) (WXHTREEITEM
) id
);
161 // Create a drag image for the given list control item
162 bool wxDragImage::Create(const wxListCtrl
& listCtrl
, long id
)
165 ImageList_Destroy((HIMAGELIST
) m_hImageList
);
168 m_hImageList
= (WXHIMAGELIST
) ListView_CreateDragImage((HWND
) listCtrl
.GetHWND(), id
, & pt
);
173 bool wxDragImage::BeginDrag(const wxPoint
& hotspot
, wxWindow
* WXUNUSED(window
))
175 wxASSERT_MSG( (m_hImageList
!= 0), "Image list must not be null in BeginDrag.");
177 bool ret
= (ImageList_BeginDrag((HIMAGELIST
) m_hImageList
, 0, hotspot
.x
, hotspot
.y
) != 0);
179 wxASSERT_MSG( (ret
), "BeginDrag failed.");
186 // First add the cursor to the image list
187 int cursorIndex
= ImageList_AddIcon((HIMAGELIST
) m_hImageList
, (HICON
) m_cursor
.GetHCURSOR());
189 wxASSERT_MSG( (cursorIndex
!= -1), "ImageList_AddIcon failed in BeginDrag.");
191 if (cursorIndex
!= -1)
193 ImageList_SetDragCursorImage((HIMAGELIST
) m_hImageList
, cursorIndex
, m_hotspot
.x
, m_hotspot
.y
);
203 bool wxDragImage::EndDrag(wxWindow
* WXUNUSED(window
))
205 wxASSERT_MSG( (m_hImageList
!= 0), "Image list must not be null in EndDrag.");
214 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
215 // is non-NULL, or in screen coordinates if NULL.
216 bool wxDragImage::Move(const wxPoint
& pt
, wxWindow
* window
)
218 wxASSERT_MSG( (m_hImageList
!= 0), "Image list must not be null in Move.");
220 // TODO: what coordinates are these in: window, client, or screen?
221 bool ret
= (ImageList_DragMove( pt
.x
, pt
.y
) != 0);
228 bool wxDragImage::Show(wxWindow
* window
)
230 wxASSERT_MSG( (m_hImageList
!= 0), "Image list must not be null in Show.");
234 hWnd
= (HWND
) window
->GetHWND();
236 bool ret
= (ImageList_DragEnter( hWnd
, m_position
.x
, m_position
.y
) != 0);
241 bool wxDragImage::Hide(wxWindow
* window
)
243 wxASSERT_MSG( (m_hImageList
!= 0), "Image list must not be null in Hide.");
247 hWnd
= (HWND
) window
->GetHWND();
249 bool ret
= (ImageList_DragLeave( hWnd
) != 0);