]> git.saurik.com Git - wxWidgets.git/blob - samples/dragimag/dragimag.cpp
little tweaks
[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
89 void MyCanvas::OnEraseBackground(wxEraseEvent& event)
90 {
91 if (wxGetApp().GetBackgroundBitmap().Ok())
92 {
93 wxSize sz = GetClientSize();
94 wxRect rect(0, 0, sz.x, sz.y);
95
96 if (event.GetDC())
97 {
98 wxGetApp().TileBitmap(rect, *(event.GetDC()), wxGetApp().GetBackgroundBitmap());
99 }
100 else
101 {
102 wxClientDC dc(this);
103 wxGetApp().TileBitmap(rect, dc, wxGetApp().GetBackgroundBitmap());
104 }
105 }
106 else
107 event.Skip(); // The official way of doing it
108 }
109
110 void MyCanvas::OnMouseEvent(wxMouseEvent& event)
111 {
112 if (event.LeftDown())
113 {
114 DragShape* shape = FindShape(event.GetPosition());
115 if (shape)
116 {
117 // We tentatively start dragging, but wait for
118 // mouse movement before dragging properly.
119
120 m_dragMode = TEST_DRAG_START;
121 m_dragStartPos = event.GetPosition();
122 m_draggedShape = shape;
123 }
124 }
125 else if (event.LeftUp() && m_dragMode != TEST_DRAG_NONE)
126 {
127 // Finish dragging
128
129 m_dragMode = TEST_DRAG_NONE;
130
131 if (!m_draggedShape || !m_dragImage)
132 return;
133
134 m_draggedShape->SetPosition(m_draggedShape->GetPosition()
135 + event.GetPosition() - m_dragStartPos);
136
137 m_dragImage->Hide();
138 m_dragImage->EndDrag();
139 delete m_dragImage;
140 m_dragImage = NULL;
141
142 wxClientDC dc(this);
143 if (m_currentlyHighlighted)
144 {
145 m_currentlyHighlighted->Draw(dc);
146 }
147 m_draggedShape->SetShow(TRUE);
148 m_draggedShape->Draw(dc);
149
150 m_currentlyHighlighted = (DragShape*) NULL;
151
152 m_draggedShape = (DragShape*) NULL;
153 }
154 else if (event.Dragging() && m_dragMode != TEST_DRAG_NONE)
155 {
156 if (m_dragMode == TEST_DRAG_START)
157 {
158 // We will start dragging if we've moved beyond a couple of pixels
159
160 int tolerance = 2;
161 int dx = abs(event.GetPosition().x - m_dragStartPos.x);
162 int dy = abs(event.GetPosition().y - m_dragStartPos.y);
163 if (dx <= tolerance && dy <= tolerance)
164 return;
165
166 // Start the drag.
167 m_dragMode = TEST_DRAG_DRAGGING;
168
169 if (m_dragImage)
170 delete m_dragImage;
171
172 // Erase the dragged shape from the canvas
173 m_draggedShape->SetShow(FALSE);
174 wxClientDC dc(this);
175 EraseShape(m_draggedShape, dc);
176 DrawShapes(dc);
177
178 switch (m_draggedShape->GetDragMethod())
179 {
180 case SHAPE_DRAG_BITMAP:
181 {
182 m_dragImage = new wxDragImage(m_draggedShape->GetBitmap(), wxCursor(wxCURSOR_HAND));
183 break;
184 }
185 case SHAPE_DRAG_TEXT:
186 {
187 m_dragImage = new wxDragImage("Dragging some test text", wxCursor(wxCURSOR_HAND));
188 break;
189 }
190 case SHAPE_DRAG_ICON:
191 {
192 // Can anyone explain why this test is necessary,
193 // to prevent a gcc error?
194 #ifdef __WXMOTIF__
195 wxIcon icon(dragicon_xpm);
196 #else
197 wxIcon icon(wxICON(dragicon));
198 #endif
199
200 m_dragImage = new wxDragImage(icon, wxCursor(wxCURSOR_HAND));
201 break;
202 }
203 }
204
205 bool fullScreen = wxGetApp().GetUseScreen();
206
207 // The offset between the top-left of the shape image and the current shape position
208 wxPoint beginDragHotSpot = m_dragStartPos - m_draggedShape->GetPosition();
209
210 // Now we do this inside the implementation: always assume
211 // coordinates relative to the capture window (client coordinates)
212
213 //if (fullScreen)
214 // beginDragHotSpot -= ClientToScreen(wxPoint(0, 0));
215
216 if (!m_dragImage->BeginDrag(beginDragHotSpot, this, fullScreen))
217 {
218 delete m_dragImage;
219 m_dragImage = (wxDragImage*) NULL;
220 m_dragMode = TEST_DRAG_NONE;
221
222 } else
223 {
224 m_dragImage->Move(event.GetPosition());
225 m_dragImage->Show();
226 }
227 }
228 else if (m_dragMode == TEST_DRAG_DRAGGING)
229 {
230 // We're currently dragging. See if we're over another shape.
231 DragShape* onShape = FindShape(event.GetPosition());
232
233 bool mustUnhighlightOld = FALSE;
234 bool mustHighlightNew = FALSE;
235
236 if (m_currentlyHighlighted)
237 {
238 if ((onShape == (DragShape*) NULL) || (m_currentlyHighlighted != onShape))
239 mustUnhighlightOld = TRUE;
240 }
241
242 if (onShape && (onShape != m_currentlyHighlighted) && onShape->IsShown())
243 mustHighlightNew = TRUE;
244
245 if (mustUnhighlightOld || mustHighlightNew)
246 m_dragImage->Hide();
247
248 // Now with the drag image switched off, we can change the window contents.
249
250 if (mustUnhighlightOld)
251 {
252 wxClientDC clientDC(this);
253 m_currentlyHighlighted->Draw(clientDC);
254 m_currentlyHighlighted = (DragShape*) NULL;
255 }
256 if (mustHighlightNew)
257 {
258 wxClientDC clientDC(this);
259 m_currentlyHighlighted = onShape;
260 m_currentlyHighlighted->Draw(clientDC, wxINVERT);
261 }
262
263 // Move and show the image again
264 m_dragImage->Move(event.GetPosition());
265
266 if (mustUnhighlightOld || mustHighlightNew)
267 m_dragImage->Show();
268 }
269 }
270 }
271
272 void MyCanvas::DrawShapes(wxDC& dc)
273 {
274 wxNode* node = m_displayList.First();
275 while (node)
276 {
277 DragShape* shape = (DragShape*) node->Data();
278 if (shape->IsShown())
279 shape->Draw(dc);
280 node = node->Next();
281 }
282 }
283
284 void MyCanvas::EraseShape(DragShape* shape, wxDC& dc)
285 {
286 wxSize sz = GetClientSize();
287 wxRect rect(0, 0, sz.x, sz.y);
288
289 wxRect rect2(shape->GetRect());
290 dc.SetClippingRegion(rect2.x, rect2.y, rect2.width, rect2.height);
291
292 wxGetApp().TileBitmap(rect, dc, wxGetApp().GetBackgroundBitmap());
293
294 dc.DestroyClippingRegion();
295 }
296
297 void MyCanvas::ClearShapes()
298 {
299 wxNode* node = m_displayList.First();
300 while (node)
301 {
302 DragShape* shape = (DragShape*) node->Data();
303 delete shape;
304 node = node->Next();
305 }
306 m_displayList.Clear();
307 }
308
309 DragShape* MyCanvas::FindShape(const wxPoint& pt) const
310 {
311 wxNode* node = m_displayList.First();
312 while (node)
313 {
314 DragShape* shape = (DragShape*) node->Data();
315 if (shape->HitTest(pt))
316 return shape;
317 node = node->Next();
318 }
319 return (DragShape*) NULL;
320 }
321
322 // MyFrame
323
324 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
325
326 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
327 EVT_MENU (wxID_ABOUT, MyFrame::OnAbout)
328 EVT_MENU (wxID_EXIT, MyFrame::OnQuit)
329 END_EVENT_TABLE()
330
331 MyFrame::MyFrame()
332 : wxFrame( (wxFrame *)NULL, -1, "wxDragImage sample",
333 wxPoint(20,20), wxSize(470,360) )
334 {
335 wxMenu *file_menu = new wxMenu();
336 file_menu->Append( wxID_ABOUT, "&About...");
337 file_menu->Append( TEST_USE_SCREEN, "&Use whole screen for dragging", "Use whole screen", TRUE);
338 file_menu->Append( wxID_EXIT, "E&xit");
339
340 wxMenuBar *menu_bar = new wxMenuBar();
341 menu_bar->Append(file_menu, "&File");
342
343 SetMenuBar( menu_bar );
344
345 CreateStatusBar(2);
346 int widths[] = { -1, 100 };
347 SetStatusWidths( 2, widths );
348
349 m_canvas = new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
350 }
351
352 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
353 {
354 Close( TRUE );
355 }
356
357 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
358 {
359 (void)wxMessageBox( "wxDragImage demo\n"
360 "Julian Smart (c) 2000",
361 "About wxDragImage Demo", wxICON_INFORMATION | wxOK );
362 }
363
364 //-----------------------------------------------------------------------------
365 // MyApp
366 //-----------------------------------------------------------------------------
367
368 BEGIN_EVENT_TABLE(MyApp, wxApp)
369 EVT_MENU(TEST_USE_SCREEN, MyApp::OnUseScreen)
370 END_EVENT_TABLE()
371
372 MyApp::MyApp()
373 {
374 // Drag across whole screen
375 m_useScreen = FALSE;
376 }
377
378 bool MyApp::OnInit()
379 {
380 #if wxUSE_LIBPNG
381 wxImage::AddHandler( new wxPNGHandler );
382 #endif
383
384 wxImage image;
385 if (image.LoadFile("backgrnd.png", wxBITMAP_TYPE_PNG))
386 {
387 m_background = image.ConvertToBitmap();
388 }
389
390
391 MyFrame *frame = new MyFrame();
392
393 wxString rootName("shape0");
394
395 int i;
396 for (i = 1; i < 4; i++)
397 {
398 wxString filename;
399 filename.Printf("%s%d.png", (const char*) rootName, i);
400 if (image.LoadFile(filename, wxBITMAP_TYPE_PNG))
401 {
402 DragShape* newShape = new DragShape(image.ConvertToBitmap());
403 newShape->SetPosition(wxPoint(i*50, i*50));
404
405 if (i == 2)
406 newShape->SetDragMethod(SHAPE_DRAG_TEXT);
407 else if (i == 3)
408 newShape->SetDragMethod(SHAPE_DRAG_ICON);
409 else
410 newShape->SetDragMethod(SHAPE_DRAG_BITMAP);
411 frame->GetCanvas()->GetDisplayList().Append(newShape);
412 }
413 }
414
415 #if 0
416 // Under Motif or GTK, this demonstrates that
417 // wxScreenDC only gets the root window content.
418 // We need to be able to copy the overall content
419 // for full-screen dragging to work.
420 int w, h;
421 wxDisplaySize(& w, & h);
422 wxBitmap bitmap(w, h);
423
424 wxScreenDC dc;
425 wxMemoryDC memDC;
426 memDC.SelectObject(bitmap);
427 memDC.Blit(0, 0, w, h, & dc, 0, 0);
428 memDC.SelectObject(wxNullBitmap);
429 m_background = bitmap;
430 #endif
431
432 frame->Show( TRUE );
433
434 return TRUE;
435 }
436
437 bool MyApp::TileBitmap(const wxRect& rect, wxDC& dc, wxBitmap& bitmap)
438 {
439 int w = bitmap.GetWidth();
440 int h = bitmap.GetHeight();
441
442 int i, j;
443 for (i = rect.x; i < rect.x + rect.width; i += w)
444 {
445 for (j = rect.y; j < rect.y + rect.height; j+= h)
446 dc.DrawBitmap(bitmap, i, j);
447 }
448 return TRUE;
449 }
450
451 void MyApp::OnUseScreen(wxCommandEvent& event)
452 {
453 m_useScreen = !m_useScreen;
454 }
455
456 // DragShape
457
458 DragShape::DragShape(const wxBitmap& bitmap)
459 {
460 m_bitmap = bitmap;
461 m_pos.x = 0;
462 m_pos.y = 0;
463 m_dragMethod = SHAPE_DRAG_BITMAP;
464 m_show = TRUE;
465 }
466
467 DragShape::~DragShape()
468 {
469 }
470
471 bool DragShape::HitTest(const wxPoint& pt) const
472 {
473 wxRect rect(GetRect());
474 return rect.Inside(pt.x, pt.y);
475 }
476
477 bool DragShape::Draw(wxDC& dc, int op)
478 {
479 if (m_bitmap.Ok())
480 {
481 wxMemoryDC memDC;
482 memDC.SelectObject(m_bitmap);
483
484 dc.Blit(m_pos.x, m_pos.y, m_bitmap.GetWidth(), m_bitmap.GetHeight(),
485 & memDC, 0, 0, op, TRUE);
486
487 return TRUE;
488 }
489 else
490 return FALSE;
491 }
492