]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dragimgg.cpp
Filename correction.
[wxWidgets.git] / src / generic / dragimgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/dragimgg.cpp
3 // Purpose: Generic wxDragImage implementation
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 29/2/2000
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_DRAGIMAGE
28
29 #ifndef WX_PRECOMP
30 #include <stdio.h>
31 #include "wx/window.h"
32 #include "wx/frame.h"
33 #include "wx/dcclient.h"
34 #include "wx/dcscreen.h"
35 #include "wx/dcmemory.h"
36 #include "wx/settings.h"
37 #endif
38
39 #include "wx/log.h"
40 #include "wx/intl.h"
41
42 #define wxUSE_IMAGE_IN_DRAGIMAGE 1
43
44 #if wxUSE_IMAGE_IN_DRAGIMAGE
45 #include "wx/image.h"
46 #endif
47
48 #include "wx/generic/dragimgg.h"
49
50 // ----------------------------------------------------------------------------
51 // macros
52 // ----------------------------------------------------------------------------
53
54 IMPLEMENT_DYNAMIC_CLASS(wxGenericDragImage, wxObject)
55
56 // ============================================================================
57 // implementation
58 // ============================================================================
59
60 // ----------------------------------------------------------------------------
61 // wxGenericDragImage ctors/dtor
62 // ----------------------------------------------------------------------------
63
64 wxGenericDragImage::~wxGenericDragImage()
65 {
66 if (m_windowDC)
67 {
68 delete m_windowDC;
69 }
70 }
71
72 void wxGenericDragImage::Init()
73 {
74 m_isDirty = false;
75 m_isShown = false;
76 m_windowDC = (wxDC*) NULL;
77 m_window = (wxWindow*) NULL;
78 m_fullScreen = false;
79 m_pBackingBitmap = (wxBitmap*) NULL;
80 }
81
82 // Attributes
83 ////////////////////////////////////////////////////////////////////////////
84
85
86 // Operations
87 ////////////////////////////////////////////////////////////////////////////
88
89 // Create a drag image with a virtual image (need to override DoDrawImage, GetImageRect)
90 bool wxGenericDragImage::Create(const wxCursor& cursor)
91 {
92 m_cursor = cursor;
93
94 return true;
95 }
96
97 // Create a drag image from a bitmap and optional cursor
98 bool wxGenericDragImage::Create(const wxBitmap& image, const wxCursor& cursor)
99 {
100 // We don't have to combine the cursor explicitly since we simply show the cursor
101 // as we drag. This currently will only work within one window.
102
103 m_cursor = cursor;
104 m_bitmap = image;
105
106 return true ;
107 }
108
109 // Create a drag image from an icon and optional cursor
110 bool wxGenericDragImage::Create(const wxIcon& image, const wxCursor& cursor)
111 {
112 // We don't have to combine the cursor explicitly since we simply show the cursor
113 // as we drag. This currently will only work within one window.
114
115 m_cursor = cursor;
116 m_icon = image;
117
118 return true ;
119 }
120
121 // Create a drag image from a string and optional cursor
122 bool wxGenericDragImage::Create(const wxString& str, const wxCursor& cursor)
123 {
124 wxFont font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
125
126 long w, h;
127 wxScreenDC dc;
128 dc.SetFont(font);
129 dc.GetTextExtent(str, & w, & h);
130 dc.SetFont(wxNullFont);
131
132 wxMemoryDC dc2;
133
134 // Sometimes GetTextExtent isn't accurate enough, so make it longer
135 wxBitmap bitmap((int) ((w+2) * 1.5), (int) h+2);
136 dc2.SelectObject(bitmap);
137
138 dc2.SetFont(font);
139 dc2.SetBackground(* wxWHITE_BRUSH);
140 dc2.Clear();
141 dc2.SetBackgroundMode(wxTRANSPARENT);
142 dc2.SetTextForeground(* wxLIGHT_GREY);
143 dc2.DrawText(str, 0, 0);
144 dc2.DrawText(str, 1, 0);
145 dc2.DrawText(str, 2, 0);
146 dc2.DrawText(str, 1, 1);
147 dc2.DrawText(str, 2, 1);
148 dc2.DrawText(str, 1, 2);
149 dc2.DrawText(str, 2, 2);
150
151 dc2.SetTextForeground(* wxBLACK);
152 dc2.DrawText(str, 1, 1);
153
154 dc2.SelectObject(wxNullBitmap);
155
156 #if wxUSE_IMAGE_IN_DRAGIMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
157 // Make the bitmap masked
158 wxImage image = bitmap.ConvertToImage();
159 image.SetMaskColour(255, 255, 255);
160 bitmap = wxBitmap(image);
161 #endif
162
163 return Create(bitmap, cursor);
164 }
165
166 #if wxUSE_TREECTRL
167 // Create a drag image for the given tree control item
168 bool wxGenericDragImage::Create(const wxTreeCtrl& treeCtrl, wxTreeItemId& id)
169 {
170 wxString str = treeCtrl.GetItemText(id);
171 return Create(str);
172 }
173 #endif
174
175 #if wxUSE_LISTCTRL
176 // Create a drag image for the given list control item
177 bool wxGenericDragImage::Create(const wxListCtrl& listCtrl, long id)
178 {
179 wxString str = listCtrl.GetItemText(id);
180 return Create(str);
181 }
182 #endif
183
184 // Begin drag
185 bool wxGenericDragImage::BeginDrag(const wxPoint& hotspot,
186 wxWindow* window,
187 bool fullScreen,
188 wxRect* rect)
189 {
190 wxASSERT_MSG( (window != 0), wxT("Window must not be null in BeginDrag."));
191
192 // The image should be offset by this amount
193 m_offset = hotspot;
194 m_window = window;
195 m_fullScreen = fullScreen;
196
197 if (rect)
198 m_boundingRect = * rect;
199
200 m_isDirty = false;
201 m_isDirty = false;
202
203 if (window)
204 {
205 window->CaptureMouse();
206
207 if (m_cursor.Ok())
208 {
209 m_oldCursor = window->GetCursor();
210 window->SetCursor(m_cursor);
211 }
212 }
213
214 // Make a copy of the window so we can repair damage done as the image is
215 // dragged.
216
217 wxSize clientSize;
218 wxPoint pt;
219 if (!m_fullScreen)
220 {
221 clientSize = window->GetClientSize();
222 m_boundingRect.x = 0; m_boundingRect.y = 0;
223 m_boundingRect.width = clientSize.x; m_boundingRect.height = clientSize.y;
224 }
225 else
226 {
227 int w, h;
228 wxDisplaySize(& w, & h);
229 clientSize.x = w; clientSize.y = h;
230 if (rect)
231 {
232 pt.x = m_boundingRect.x; pt.y = m_boundingRect.y;
233 clientSize.x = m_boundingRect.width; clientSize.y = m_boundingRect.height;
234 }
235 else
236 {
237 m_boundingRect.x = 0; m_boundingRect.y = 0;
238 m_boundingRect.width = w; m_boundingRect.height = h;
239 }
240 }
241
242 wxBitmap* backing = (m_pBackingBitmap ? m_pBackingBitmap : (wxBitmap*) & m_backingBitmap);
243
244 if (!backing->Ok() || (backing->GetWidth() < clientSize.x || backing->GetHeight() < clientSize.y))
245 (*backing) = wxBitmap(clientSize.x, clientSize.y);
246
247 if (!m_fullScreen)
248 {
249 m_windowDC = new wxClientDC(window);
250 }
251 else
252 {
253 m_windowDC = new wxScreenDC;
254
255 #if 0
256 // Use m_boundingRect to limit the area considered.
257 ((wxScreenDC*) m_windowDC)->StartDrawingOnTop(rect);
258 #endif
259
260 m_windowDC->SetClippingRegion(m_boundingRect.x, m_boundingRect.y,
261 m_boundingRect.width, m_boundingRect.height);
262 }
263
264 return true;
265 }
266
267 // Begin drag. hotspot is the location of the drag position relative to the upper-left
268 // corner of the image. This is full screen only. fullScreenRect gives the
269 // position of the window on the screen, to restrict the drag to.
270 bool wxGenericDragImage::BeginDrag(const wxPoint& hotspot, wxWindow* window, wxWindow* fullScreenRect)
271 {
272 wxRect rect;
273
274 int x = fullScreenRect->GetPosition().x;
275 int y = fullScreenRect->GetPosition().y;
276
277 wxSize sz = fullScreenRect->GetSize();
278
279 if (fullScreenRect->GetParent() && !fullScreenRect->IsKindOf(CLASSINFO(wxFrame)))
280 fullScreenRect->GetParent()->ClientToScreen(& x, & y);
281
282 rect.x = x; rect.y = y;
283 rect.width = sz.x; rect.height = sz.y;
284
285 return BeginDrag(hotspot, window, true, & rect);
286 }
287
288 // End drag
289 bool wxGenericDragImage::EndDrag()
290 {
291 if (m_window)
292 {
293 #ifdef __WXMSW__
294 // Under Windows we can be pretty sure this test will give
295 // the correct results
296 if (wxWindow::GetCapture() == m_window)
297 #endif
298 m_window->ReleaseMouse();
299
300 if (m_cursor.Ok() && m_oldCursor.Ok())
301 {
302 m_window->SetCursor(m_oldCursor);
303 }
304 }
305
306 if (m_windowDC)
307 {
308 m_windowDC->DestroyClippingRegion();
309 delete m_windowDC;
310 m_windowDC = (wxDC*) NULL;
311 }
312
313 m_repairBitmap = wxNullBitmap;
314
315 return true;
316 }
317
318 // Move the image: call from OnMouseMove. Pt is in window client coordinates if window
319 // is non-NULL, or in screen coordinates if NULL.
320 bool wxGenericDragImage::Move(const wxPoint& pt)
321 {
322 wxASSERT_MSG( (m_windowDC != (wxDC*) NULL), wxT("No window DC in wxGenericDragImage::Move()") );
323
324 wxPoint pt2(pt);
325 if (m_fullScreen)
326 pt2 = m_window->ClientToScreen(pt);
327
328 // Erase at old position, then show at the current position
329 wxPoint oldPos = m_position;
330
331 bool eraseOldImage = (m_isDirty && m_isShown);
332
333 if (m_isShown)
334 RedrawImage(oldPos - m_offset, pt2 - m_offset, eraseOldImage, true);
335
336 m_position = pt2;
337
338 if (m_isShown)
339 m_isDirty = true;
340
341 return true;
342 }
343
344 bool wxGenericDragImage::Show()
345 {
346 wxASSERT_MSG( (m_windowDC != (wxDC*) NULL), wxT("No window DC in wxGenericDragImage::Show()") );
347
348 // Show at the current position
349
350 if (!m_isShown)
351 {
352 // This is where we restore the backing bitmap, in case
353 // something has changed on the window.
354
355 wxBitmap* backing = (m_pBackingBitmap ? m_pBackingBitmap : (wxBitmap*) & m_backingBitmap);
356 wxMemoryDC memDC;
357 memDC.SelectObject(* backing);
358
359 UpdateBackingFromWindow(* m_windowDC, memDC, m_boundingRect, wxRect(0, 0, m_boundingRect.width, m_boundingRect.height));
360
361 //memDC.Blit(0, 0, m_boundingRect.width, m_boundingRect.height, m_windowDC, m_boundingRect.x, m_boundingRect.y);
362 memDC.SelectObject(wxNullBitmap);
363
364 RedrawImage(m_position - m_offset, m_position - m_offset, false, true);
365 }
366
367 m_isShown = true;
368 m_isDirty = true;
369
370 return true;
371 }
372
373 bool wxGenericDragImage::UpdateBackingFromWindow(wxDC& windowDC, wxMemoryDC& destDC,
374 const wxRect& sourceRect, const wxRect& destRect) const
375 {
376 return destDC.Blit(destRect.x, destRect.y, destRect.width, destRect.height, & windowDC,
377 sourceRect.x, sourceRect.y);
378 }
379
380 bool wxGenericDragImage::Hide()
381 {
382 wxASSERT_MSG( (m_windowDC != (wxDC*) NULL), wxT("No window DC in wxGenericDragImage::Hide()") );
383
384 // Repair the old position
385
386 if (m_isShown && m_isDirty)
387 {
388 RedrawImage(m_position - m_offset, m_position - m_offset, true, false);
389 }
390
391 m_isShown = false;
392 m_isDirty = false;
393
394 return true;
395 }
396
397 // More efficient: erase and redraw simultaneously if possible
398 bool wxGenericDragImage::RedrawImage(const wxPoint& oldPos, const wxPoint& newPos,
399 bool eraseOld, bool drawNew)
400 {
401 if (!m_windowDC)
402 return false;
403
404 wxBitmap* backing = (m_pBackingBitmap ? m_pBackingBitmap : (wxBitmap*) & m_backingBitmap);
405 if (!backing->Ok())
406 return false;
407
408 wxRect oldRect(GetImageRect(oldPos));
409 wxRect newRect(GetImageRect(newPos));
410
411 wxRect fullRect;
412
413 // Full rect: the combination of both rects
414 if (eraseOld && drawNew)
415 {
416 int oldRight = oldRect.GetRight();
417 int oldBottom = oldRect.GetBottom();
418 int newRight = newRect.GetRight();
419 int newBottom = newRect.GetBottom();
420
421 wxPoint topLeft = wxPoint(wxMin(oldPos.x, newPos.x), wxMin(oldPos.y, newPos.y));
422 wxPoint bottomRight = wxPoint(wxMax(oldRight, newRight), wxMax(oldBottom, newBottom));
423
424 fullRect.x = topLeft.x; fullRect.y = topLeft.y;
425 fullRect.SetRight(bottomRight.x);
426 fullRect.SetBottom(bottomRight.y);
427 }
428 else if (eraseOld)
429 fullRect = oldRect;
430 else if (drawNew)
431 fullRect = newRect;
432
433 // Make the bitmap bigger than it need be, so we don't
434 // keep reallocating all the time.
435 int excess = 50;
436
437 if (!m_repairBitmap.Ok() || (m_repairBitmap.GetWidth() < fullRect.GetWidth() || m_repairBitmap.GetHeight() < fullRect.GetHeight()))
438 {
439 m_repairBitmap = wxBitmap(fullRect.GetWidth() + excess, fullRect.GetHeight() + excess);
440 }
441
442 wxMemoryDC memDC;
443 memDC.SelectObject(* backing);
444
445 wxMemoryDC memDCTemp;
446 memDCTemp.SelectObject(m_repairBitmap);
447
448 // Draw the backing bitmap onto the repair bitmap.
449 // If full-screen, we may have specified the rect on the
450 // screen that we're using for our backing bitmap.
451 // So subtract this when we're blitting from the backing bitmap
452 // (translate from screen to backing-bitmap coords).
453
454 memDCTemp.Blit(0, 0, fullRect.GetWidth(), fullRect.GetHeight(), & memDC, fullRect.x - m_boundingRect.x, fullRect.y - m_boundingRect.y);
455
456 // If drawing, draw the image onto the mem DC
457 if (drawNew)
458 {
459 wxPoint pos(newPos.x - fullRect.x, newPos.y - fullRect.y) ;
460 DoDrawImage(memDCTemp, pos);
461 }
462
463 // Now blit to the window
464 // Finally, blit the temp mem DC to the window.
465 m_windowDC->Blit(fullRect.x, fullRect.y, fullRect.width, fullRect.height, & memDCTemp, 0, 0);
466
467 memDCTemp.SelectObject(wxNullBitmap);
468 memDC.SelectObject(wxNullBitmap);
469
470 return true;
471 }
472
473 // Override this if you are using a virtual image (drawing your own image)
474 bool wxGenericDragImage::DoDrawImage(wxDC& dc, const wxPoint& pos) const
475 {
476 if (m_bitmap.Ok())
477 {
478 dc.DrawBitmap(m_bitmap, pos.x, pos.y, (m_bitmap.GetMask() != 0));
479 return true;
480 }
481 else if (m_icon.Ok())
482 {
483 dc.DrawIcon(m_icon, pos.x, pos.y);
484 return true;
485 }
486 else
487 return false;
488 }
489
490 // Override this if you are using a virtual image (drawing your own image)
491 wxRect wxGenericDragImage::GetImageRect(const wxPoint& pos) const
492 {
493 if (m_bitmap.Ok())
494 {
495 return wxRect(pos.x, pos.y, m_bitmap.GetWidth(), m_bitmap.GetHeight());
496 }
497 else if (m_icon.Ok())
498 {
499 return wxRect(pos.x, pos.y, m_icon.GetWidth(), m_icon.GetHeight());
500 }
501 else
502 {
503 return wxRect(pos.x, pos.y, 0, 0);
504 }
505 }
506
507 #endif // wxUSE_DRAGIMAGE