Added wxUSE_DC_CACHEING and associated code to wxMSW
[wxWidgets.git] / samples / dragimag / dragimag.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dragimag.cpp
3 // Purpose: wxDragImage sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 28/2/2000
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #include "wx/image.h"
24
25 // Under Windows, change this to 1
26 // to use wxGenericDragImage
27
28 #define wxUSE_GENERIC_DRAGIMAGE 1
29
30 #if wxUSE_GENERIC_DRAGIMAGE
31 #include "wx/generic/dragimgg.h"
32 #define wxDragImage wxGenericDragImage
33 #else
34 #include "wx/dragimag.h"
35 #endif
36
37 #include "dragimag.h"
38
39 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
40 #include "mondrian.xpm"
41 #include "dragicon.xpm"
42 #endif
43
44 // main program
45
46 IMPLEMENT_APP(MyApp)
47
48 // MyCanvas
49
50 IMPLEMENT_CLASS(MyCanvas, wxScrolledWindow)
51
52 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
53 EVT_PAINT(MyCanvas::OnPaint)
54 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground)
55 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
56 END_EVENT_TABLE()
57
58 MyCanvas::MyCanvas( wxWindow *parent, wxWindowID id,
59 const wxPoint &pos, const wxSize &size )
60 : wxScrolledWindow( parent, id, pos, size, wxSUNKEN_BORDER )
61 {
62 SetBackgroundColour(* wxWHITE);
63
64 SetCursor(wxCursor(wxCURSOR_ARROW));
65
66 m_dragMode = TEST_DRAG_NONE;
67 m_draggedShape = (DragShape*) NULL;
68 m_dragImage = (wxDragImage*) NULL;
69 m_currentlyHighlighted = (DragShape*) NULL;
70 }
71
72 MyCanvas::~MyCanvas()
73 {
74 ClearShapes();
75
76 if (m_dragImage)
77 delete m_dragImage;
78 }
79
80 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
81 {
82 wxPaintDC dc( this );
83 PrepareDC( dc );
84
85 DrawShapes(dc);
86 }
87
88 void MyCanvas::OnEraseBackground(wxEraseEvent& event)
89 {
90 if (wxGetApp().GetBackgroundBitmap().Ok())
91 {
92 wxSize sz = GetClientSize();
93 wxRect rect(0, 0, sz.x, sz.y);
94
95 if (event.GetDC())
96 {
97 wxGetApp().TileBitmap(rect, *(event.GetDC()), wxGetApp().GetBackgroundBitmap());
98 }
99 else
100 {
101 wxClientDC dc(this);
102 wxGetApp().TileBitmap(rect, dc, wxGetApp().GetBackgroundBitmap());
103 }
104 #if wxUSE_DC_CACHEING
105 wxDC::ClearCache();
106 #endif
107 }
108 else
109 event.Skip(); // The official way of doing it
110 }
111
112 void MyCanvas::OnMouseEvent(wxMouseEvent& event)
113 {
114 if (event.LeftDown())
115 {
116 DragShape* shape = FindShape(event.GetPosition());
117 if (shape)
118 {
119 // We tentatively start dragging, but wait for
120 // mouse movement before dragging properly.
121
122 m_dragMode = TEST_DRAG_START;
123 m_dragStartPos = event.GetPosition();
124 m_draggedShape = shape;
125 }
126 }
127 else if (event.LeftUp() && m_dragMode != TEST_DRAG_NONE)
128 {
129 // Finish dragging
130
131 m_dragMode = TEST_DRAG_NONE;
132
133 if (!m_draggedShape || !m_dragImage)
134 return;
135
136 m_draggedShape->SetPosition(m_draggedShape->GetPosition()
137 + event.GetPosition() - m_dragStartPos);
138
139 m_dragImage->Hide();
140 m_dragImage->EndDrag();
141 delete m_dragImage;
142 m_dragImage = NULL;
143
144 wxClientDC dc(this);
145 if (m_currentlyHighlighted)
146 {
147 m_currentlyHighlighted->Draw(dc);
148 }
149 m_draggedShape->SetShow(TRUE);
150 m_draggedShape->Draw(dc);
151
152 m_currentlyHighlighted = (DragShape*) NULL;
153
154 m_draggedShape = (DragShape*) NULL;
155 }
156 else if (event.Dragging() && m_dragMode != TEST_DRAG_NONE)
157 {
158 if (m_dragMode == TEST_DRAG_START)
159 {
160 // We will start dragging if we've moved beyond a couple of pixels
161
162 int tolerance = 2;
163 int dx = abs(event.GetPosition().x - m_dragStartPos.x);
164 int dy = abs(event.GetPosition().y - m_dragStartPos.y);
165 if (dx <= tolerance && dy <= tolerance)
166 return;
167
168 // Start the drag.
169 m_dragMode = TEST_DRAG_DRAGGING;
170
171 if (m_dragImage)
172 delete m_dragImage;
173
174 // Erase the dragged shape from the canvas
175 m_draggedShape->SetShow(FALSE);
176 wxClientDC dc(this);
177 EraseShape(m_draggedShape, dc);
178 DrawShapes(dc);
179
180 switch (m_draggedShape->GetDragMethod())
181 {
182 case SHAPE_DRAG_BITMAP:
183 {
184 m_dragImage = new wxDragImage(m_draggedShape->GetBitmap(), wxCursor(wxCURSOR_HAND));
185 break;
186 }
187 case SHAPE_DRAG_TEXT:
188 {
189 m_dragImage = new wxDragImage("Dragging some test text", wxCursor(wxCURSOR_HAND));
190 break;
191 }
192 case SHAPE_DRAG_ICON:
193 {
194 // Can anyone explain why this test is necessary,
195 // to prevent a gcc error?
196 #ifdef __WXMOTIF__
197 wxIcon icon(dragicon_xpm);
198 #else
199 wxIcon icon(wxICON(dragicon));
200 #endif
201
202 m_dragImage = new wxDragImage(icon, wxCursor(wxCURSOR_HAND));
203 break;
204 }
205 }
206
207 bool fullScreen = wxGetApp().GetUseScreen();
208
209 // The offset between the top-left of the shape image and the current shape position
210 wxPoint beginDragHotSpot = m_dragStartPos - m_draggedShape->GetPosition();
211
212 // Now we do this inside the implementation: always assume
213 // coordinates relative to the capture window (client coordinates)
214
215 //if (fullScreen)
216 // beginDragHotSpot -= ClientToScreen(wxPoint(0, 0));
217
218 if (!m_dragImage->BeginDrag(beginDragHotSpot, this, fullScreen))
219 {
220 delete m_dragImage;
221 m_dragImage = (wxDragImage*) NULL;
222 m_dragMode = TEST_DRAG_NONE;
223
224 } else
225 {
226 m_dragImage->Move(event.GetPosition());
227 m_dragImage->Show();
228 }
229 }
230 else if (m_dragMode == TEST_DRAG_DRAGGING)
231 {
232 // We're currently dragging. See if we're over another shape.
233 DragShape* onShape = FindShape(event.GetPosition());
234
235 bool mustUnhighlightOld = FALSE;
236 bool mustHighlightNew = FALSE;
237
238 if (m_currentlyHighlighted)
239 {
240 if ((onShape == (DragShape*) NULL) || (m_currentlyHighlighted != onShape))
241 mustUnhighlightOld = TRUE;
242 }
243
244 if (onShape && (onShape != m_currentlyHighlighted) && onShape->IsShown())
245 mustHighlightNew = TRUE;
246
247 if (mustUnhighlightOld || mustHighlightNew)
248 m_dragImage->Hide();
249
250 // Now with the drag image switched off, we can change the window contents.
251
252 if (mustUnhighlightOld)
253 {
254 wxClientDC clientDC(this);
255 m_currentlyHighlighted->Draw(clientDC);
256 m_currentlyHighlighted = (DragShape*) NULL;
257 }
258 if (mustHighlightNew)
259 {
260 wxClientDC clientDC(this);
261 m_currentlyHighlighted = onShape;
262 m_currentlyHighlighted->Draw(clientDC, wxINVERT);
263 }
264
265 // Move and show the image again
266 m_dragImage->Move(event.GetPosition());
267
268 if (mustUnhighlightOld || mustHighlightNew)
269 m_dragImage->Show();
270 }
271 }
272 }
273
274 void MyCanvas::DrawShapes(wxDC& dc)
275 {
276 wxNode* node = m_displayList.First();
277 while (node)
278 {
279 DragShape* shape = (DragShape*) node->Data();
280 if (shape->IsShown())
281 shape->Draw(dc);
282 node = node->Next();
283 }
284 #if wxUSE_DC_CACHEING
285 wxDC::ClearCache();
286 #endif
287 }
288
289 void MyCanvas::EraseShape(DragShape* shape, wxDC& dc)
290 {
291 wxSize sz = GetClientSize();
292 wxRect rect(0, 0, sz.x, sz.y);
293
294 wxRect rect2(shape->GetRect());
295 dc.SetClippingRegion(rect2.x, rect2.y, rect2.width, rect2.height);
296
297 wxGetApp().TileBitmap(rect, dc, wxGetApp().GetBackgroundBitmap());
298
299 dc.DestroyClippingRegion();
300 }
301
302 void MyCanvas::ClearShapes()
303 {
304 wxNode* node = m_displayList.First();
305 while (node)
306 {
307 DragShape* shape = (DragShape*) node->Data();
308 delete shape;
309 node = node->Next();
310 }
311 m_displayList.Clear();
312 }
313
314 DragShape* MyCanvas::FindShape(const wxPoint& pt) const
315 {
316 wxNode* node = m_displayList.First();
317 while (node)
318 {
319 DragShape* shape = (DragShape*) node->Data();
320 if (shape->HitTest(pt))
321 return shape;
322 node = node->Next();
323 }
324 return (DragShape*) NULL;
325 }
326
327 // MyFrame
328 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
329
330 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
331 EVT_MENU (wxID_ABOUT, MyFrame::OnAbout)
332 EVT_MENU (wxID_EXIT, MyFrame::OnQuit)
333 END_EVENT_TABLE()
334
335 MyFrame::MyFrame()
336 : wxFrame( (wxFrame *)NULL, -1, "wxDragImage sample",
337 wxPoint(20,20), wxSize(470,360) )
338 {
339 wxMenu *file_menu = new wxMenu();
340 file_menu->Append( wxID_ABOUT, "&About...");
341 file_menu->Append( TEST_USE_SCREEN, "&Use whole screen for dragging", "Use whole screen", TRUE);
342 file_menu->Append( wxID_EXIT, "E&xit");
343
344 wxMenuBar *menu_bar = new wxMenuBar();
345 menu_bar->Append(file_menu, "&File");
346
347 SetMenuBar( menu_bar );
348
349 CreateStatusBar(2);
350 int widths[] = { -1, 100 };
351 SetStatusWidths( 2, widths );
352
353 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
354 }
355
356 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
357 {
358 Close( TRUE );
359 }
360
361 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
362 {
363 (void)wxMessageBox( "wxDragImage demo\n"
364 "Julian Smart (c) 2000",
365 "About wxDragImage Demo", wxICON_INFORMATION | wxOK );
366 }
367
368 //-----------------------------------------------------------------------------
369 // MyApp
370 //-----------------------------------------------------------------------------
371
372 BEGIN_EVENT_TABLE(MyApp, wxApp)
373 EVT_MENU(TEST_USE_SCREEN, MyApp::OnUseScreen)
374 END_EVENT_TABLE()
375
376 MyApp::MyApp()
377 {
378 // Drag across whole screen
379 m_useScreen = FALSE;
380 }
381
382 bool MyApp::OnInit()
383 {
384 #if wxUSE_LIBPNG
385 wxImage::AddHandler( new wxPNGHandler );
386 #endif
387
388 // The DC cache is an efficiency measure to be used
389 // when a lot of masked blitting is done
390 #if wxUSE_DC_CACHEING
391 wxDC::EnableCache(TRUE);
392 #endif
393
394 wxImage image;
395 if (image.LoadFile("backgrnd.png", wxBITMAP_TYPE_PNG))
396 {
397 m_background = image.ConvertToBitmap();
398 }
399
400 MyFrame *frame = new MyFrame();
401
402 wxString rootName("shape0");
403
404 int i;
405 for (i = 1; i < 4; i++)
406 {
407 wxString filename;
408 filename.Printf("%s%d.png", (const char*) rootName, i);
409 if (image.LoadFile(filename, wxBITMAP_TYPE_PNG))
410 {
411 DragShape* newShape = new DragShape(image.ConvertToBitmap());
412 newShape->SetPosition(wxPoint(i*50, i*50));
413
414 if (i == 2)
415 newShape->SetDragMethod(SHAPE_DRAG_TEXT);
416 else if (i == 3)
417 newShape->SetDragMethod(SHAPE_DRAG_ICON);
418 else
419 newShape->SetDragMethod(SHAPE_DRAG_BITMAP);
420 frame->GetCanvas()->GetDisplayList().Append(newShape);
421 }
422 }
423
424 #if 0
425 // Under Motif or GTK, this demonstrates that
426 // wxScreenDC only gets the root window content.
427 // We need to be able to copy the overall content
428 // for full-screen dragging to work.
429 int w, h;
430 wxDisplaySize(& w, & h);
431 wxBitmap bitmap(w, h);
432
433 wxScreenDC dc;
434 wxMemoryDC memDC;
435 memDC.SelectObject(bitmap);
436 memDC.Blit(0, 0, w, h, & dc, 0, 0);
437 memDC.SelectObject(wxNullBitmap);
438 m_background = bitmap;
439 #endif
440
441 frame->Show( TRUE );
442
443 return TRUE;
444 }
445
446 int MyApp::OnExit()
447 {
448 #if wxUSE_DC_CACHEING
449 wxDC::ClearCache();
450 #endif
451 return 0;
452 }
453
454 bool MyApp::TileBitmap(const wxRect& rect, wxDC& dc, wxBitmap& bitmap)
455 {
456 int w = bitmap.GetWidth();
457 int h = bitmap.GetHeight();
458
459 int i, j;
460 for (i = rect.x; i < rect.x + rect.width; i += w)
461 {
462 for (j = rect.y; j < rect.y + rect.height; j+= h)
463 dc.DrawBitmap(bitmap, i, j);
464 }
465 return TRUE;
466 }
467
468 void MyApp::OnUseScreen(wxCommandEvent& event)
469 {
470 m_useScreen = !m_useScreen;
471 }
472
473 // DragShape
474
475 DragShape::DragShape(const wxBitmap& bitmap)
476 {
477 m_bitmap = bitmap;
478 m_pos.x = 0;
479 m_pos.y = 0;
480 m_dragMethod = SHAPE_DRAG_BITMAP;
481 m_show = TRUE;
482 }
483
484 DragShape::~DragShape()
485 {
486 }
487
488 bool DragShape::HitTest(const wxPoint& pt) const
489 {
490 wxRect rect(GetRect());
491 return rect.Inside(pt.x, pt.y);
492 }
493
494 bool DragShape::Draw(wxDC& dc, int op)
495 {
496 if (m_bitmap.Ok())
497 {
498 wxMemoryDC memDC;
499 memDC.SelectObject(m_bitmap);
500
501 dc.Blit(m_pos.x, m_pos.y, m_bitmap.GetWidth(), m_bitmap.GetHeight(),
502 & memDC, 0, 0, op, TRUE);
503
504 return TRUE;
505 }
506 else
507 return FALSE;
508 }
509