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