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