]> git.saurik.com Git - wxWidgets.git/blame - samples/dragimag/dragimag.cpp
Restored the accidentally deleted "return true;" at the end of the Show method from...
[wxWidgets.git] / samples / dragimag / dragimag.cpp
CommitLineData
68be9f09 1/////////////////////////////////////////////////////////////////////////////
11e2dfd3 2// Name: dragimag.cpp
68be9f09
JS
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
11e2dfd3 20#include "wx/wx.h"
68be9f09
JS
21#endif
22
11e2dfd3 23#include "wx/image.h"
68be9f09
JS
24
25// Under Windows, change this to 1
26// to use wxGenericDragImage
27
aa2d25a5 28#define wxUSE_GENERIC_DRAGIMAGE 1
68be9f09
JS
29
30#if wxUSE_GENERIC_DRAGIMAGE
11e2dfd3 31#include "wx/generic/dragimgg.h"
68be9f09
JS
32#define wxDragImage wxGenericDragImage
33#else
11e2dfd3 34#include "wx/dragimag.h"
68be9f09
JS
35#endif
36
11e2dfd3 37#include "dragimag.h"
68be9f09 38
e334d0ea 39#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
68be9f09
JS
40#include "mondrian.xpm"
41#include "dragicon.xpm"
42#endif
43
44// main program
45
46IMPLEMENT_APP(MyApp)
47
48// MyCanvas
49
50IMPLEMENT_CLASS(MyCanvas, wxScrolledWindow)
51
52BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
53 EVT_PAINT(MyCanvas::OnPaint)
54 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground)
55 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
56END_EVENT_TABLE()
57
58MyCanvas::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
72MyCanvas::~MyCanvas()
73{
74 ClearShapes();
75
76 if (m_dragImage)
77 delete m_dragImage;
78}
79
80void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
81{
82 wxPaintDC dc( this );
83 PrepareDC( dc );
84
85 DrawShapes(dc);
68be9f09
JS
86}
87
88void MyCanvas::OnEraseBackground(wxEraseEvent& event)
89{
90 if (wxGetApp().GetBackgroundBitmap().Ok())
91 {
92 wxSize sz = GetClientSize();
93 wxRect rect(0, 0, sz.x, sz.y);
925e9792 94
68be9f09
JS
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 }
105 else
106 event.Skip(); // The official way of doing it
107}
108
109void MyCanvas::OnMouseEvent(wxMouseEvent& event)
110{
111 if (event.LeftDown())
112 {
113 DragShape* shape = FindShape(event.GetPosition());
114 if (shape)
115 {
116 // We tentatively start dragging, but wait for
117 // mouse movement before dragging properly.
118
119 m_dragMode = TEST_DRAG_START;
120 m_dragStartPos = event.GetPosition();
121 m_draggedShape = shape;
122 }
123 }
124 else if (event.LeftUp() && m_dragMode != TEST_DRAG_NONE)
125 {
126 // Finish dragging
127
128 m_dragMode = TEST_DRAG_NONE;
129
130 if (!m_draggedShape || !m_dragImage)
131 return;
132
aa2d25a5
JS
133 m_draggedShape->SetPosition(m_draggedShape->GetPosition()
134 + event.GetPosition() - m_dragStartPos);
68be9f09
JS
135
136 m_dragImage->Hide();
137 m_dragImage->EndDrag();
138 delete m_dragImage;
139 m_dragImage = NULL;
140
141 wxClientDC dc(this);
142 if (m_currentlyHighlighted)
143 {
144 m_currentlyHighlighted->Draw(dc);
145 }
07850a49 146 m_draggedShape->SetShow(true);
68be9f09
JS
147 m_draggedShape->Draw(dc);
148
149 m_currentlyHighlighted = (DragShape*) NULL;
150
151 m_draggedShape = (DragShape*) NULL;
152 }
153 else if (event.Dragging() && m_dragMode != TEST_DRAG_NONE)
154 {
155 if (m_dragMode == TEST_DRAG_START)
156 {
157 // We will start dragging if we've moved beyond a couple of pixels
158
159 int tolerance = 2;
160 int dx = abs(event.GetPosition().x - m_dragStartPos.x);
161 int dy = abs(event.GetPosition().y - m_dragStartPos.y);
162 if (dx <= tolerance && dy <= tolerance)
163 return;
164
68be9f09
JS
165 // Start the drag.
166 m_dragMode = TEST_DRAG_DRAGGING;
167
168 if (m_dragImage)
169 delete m_dragImage;
170
171 // Erase the dragged shape from the canvas
07850a49 172 m_draggedShape->SetShow(false);
68be9f09
JS
173 wxClientDC dc(this);
174 EraseShape(m_draggedShape, dc);
175 DrawShapes(dc);
176
177 switch (m_draggedShape->GetDragMethod())
178 {
179 case SHAPE_DRAG_BITMAP:
180 {
aa2d25a5 181 m_dragImage = new wxDragImage(m_draggedShape->GetBitmap(), wxCursor(wxCURSOR_HAND));
68be9f09
JS
182 break;
183 }
184 case SHAPE_DRAG_TEXT:
185 {
ab1ca7b3 186 m_dragImage = new wxDragImage(wxString(_T("Dragging some test text")), wxCursor(wxCURSOR_HAND));
68be9f09
JS
187 break;
188 }
189 case SHAPE_DRAG_ICON:
190 {
78d93bf6 191 m_dragImage = new wxDragImage(wxICON(dragicon), wxCursor(wxCURSOR_HAND));
68be9f09
JS
192 break;
193 }
194 }
195
aa2d25a5 196 bool fullScreen = wxGetApp().GetUseScreen();
68be9f09 197
aa2d25a5
JS
198 // The offset between the top-left of the shape image and the current shape position
199 wxPoint beginDragHotSpot = m_dragStartPos - m_draggedShape->GetPosition();
925e9792 200
aa2d25a5
JS
201 // Now we do this inside the implementation: always assume
202 // coordinates relative to the capture window (client coordinates)
68be9f09 203
aa2d25a5
JS
204 //if (fullScreen)
205 // beginDragHotSpot -= ClientToScreen(wxPoint(0, 0));
925e9792 206
aa2d25a5 207 if (!m_dragImage->BeginDrag(beginDragHotSpot, this, fullScreen))
68be9f09
JS
208 {
209 delete m_dragImage;
210 m_dragImage = (wxDragImage*) NULL;
211 m_dragMode = TEST_DRAG_NONE;
925e9792 212
aa2d25a5
JS
213 } else
214 {
215 m_dragImage->Move(event.GetPosition());
216 m_dragImage->Show();
68be9f09 217 }
68be9f09
JS
218 }
219 else if (m_dragMode == TEST_DRAG_DRAGGING)
220 {
221 // We're currently dragging. See if we're over another shape.
222 DragShape* onShape = FindShape(event.GetPosition());
223
07850a49
WS
224 bool mustUnhighlightOld = false;
225 bool mustHighlightNew = false;
68be9f09
JS
226
227 if (m_currentlyHighlighted)
228 {
229 if ((onShape == (DragShape*) NULL) || (m_currentlyHighlighted != onShape))
07850a49 230 mustUnhighlightOld = true;
68be9f09
JS
231 }
232
233 if (onShape && (onShape != m_currentlyHighlighted) && onShape->IsShown())
07850a49 234 mustHighlightNew = true;
68be9f09
JS
235
236 if (mustUnhighlightOld || mustHighlightNew)
237 m_dragImage->Hide();
238
239 // Now with the drag image switched off, we can change the window contents.
240
241 if (mustUnhighlightOld)
242 {
243 wxClientDC clientDC(this);
244 m_currentlyHighlighted->Draw(clientDC);
245 m_currentlyHighlighted = (DragShape*) NULL;
246 }
247 if (mustHighlightNew)
248 {
249 wxClientDC clientDC(this);
250 m_currentlyHighlighted = onShape;
251 m_currentlyHighlighted->Draw(clientDC, wxINVERT);
252 }
253
68be9f09 254 // Move and show the image again
aa2d25a5 255 m_dragImage->Move(event.GetPosition());
68be9f09
JS
256
257 if (mustUnhighlightOld || mustHighlightNew)
258 m_dragImage->Show();
259 }
260 }
261}
262
263void MyCanvas::DrawShapes(wxDC& dc)
264{
ccd7d9e5 265 wxList::compatibility_iterator node = m_displayList.GetFirst();
68be9f09
JS
266 while (node)
267 {
b1d4dd7a 268 DragShape* shape = (DragShape*) node->GetData();
68be9f09
JS
269 if (shape->IsShown())
270 shape->Draw(dc);
b1d4dd7a 271 node = node->GetNext();
68be9f09
JS
272 }
273}
274
275void MyCanvas::EraseShape(DragShape* shape, wxDC& dc)
276{
277 wxSize sz = GetClientSize();
278 wxRect rect(0, 0, sz.x, sz.y);
279
280 wxRect rect2(shape->GetRect());
281 dc.SetClippingRegion(rect2.x, rect2.y, rect2.width, rect2.height);
925e9792 282
68be9f09
JS
283 wxGetApp().TileBitmap(rect, dc, wxGetApp().GetBackgroundBitmap());
284
285 dc.DestroyClippingRegion();
286}
287
288void MyCanvas::ClearShapes()
289{
ccd7d9e5 290 wxList::compatibility_iterator node = m_displayList.GetFirst();
68be9f09
JS
291 while (node)
292 {
b1d4dd7a 293 DragShape* shape = (DragShape*) node->GetData();
68be9f09 294 delete shape;
b1d4dd7a 295 node = node->GetNext();
68be9f09
JS
296 }
297 m_displayList.Clear();
298}
299
300DragShape* MyCanvas::FindShape(const wxPoint& pt) const
301{
ccd7d9e5 302 wxList::compatibility_iterator node = m_displayList.GetFirst();
68be9f09
JS
303 while (node)
304 {
b1d4dd7a 305 DragShape* shape = (DragShape*) node->GetData();
68be9f09
JS
306 if (shape->HitTest(pt))
307 return shape;
b1d4dd7a 308 node = node->GetNext();
68be9f09
JS
309 }
310 return (DragShape*) NULL;
311}
312
313// MyFrame
68be9f09
JS
314IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
315
316BEGIN_EVENT_TABLE(MyFrame,wxFrame)
317 EVT_MENU (wxID_ABOUT, MyFrame::OnAbout)
318 EVT_MENU (wxID_EXIT, MyFrame::OnQuit)
319END_EVENT_TABLE()
320
321MyFrame::MyFrame()
07850a49 322: wxFrame( (wxFrame *)NULL, wxID_ANY, _T("wxDragImage sample"),
1cfa5d8e 323 wxPoint(20,20), wxSize(470,360) )
68be9f09 324{
1cfa5d8e
JS
325 wxMenu *file_menu = new wxMenu();
326 file_menu->Append( wxID_ABOUT, _T("&About..."));
2153bf89 327 file_menu->AppendCheckItem( TEST_USE_SCREEN, _T("&Use whole screen for dragging"), _T("Use whole screen"));
1cfa5d8e 328 file_menu->Append( wxID_EXIT, _T("E&xit"));
925e9792 329
1cfa5d8e
JS
330 wxMenuBar *menu_bar = new wxMenuBar();
331 menu_bar->Append(file_menu, _T("&File"));
d8d18184
MB
332
333 SetIcon(wxICON(mondrian));
1cfa5d8e 334 SetMenuBar( menu_bar );
925e9792 335
8520f137 336#if wxUSE_STATUSBAR
1cfa5d8e
JS
337 CreateStatusBar(2);
338 int widths[] = { -1, 100 };
339 SetStatusWidths( 2, widths );
8520f137 340#endif // wxUSE_STATUSBAR
925e9792 341
07850a49 342 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) );
68be9f09
JS
343}
344
345void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
346{
07850a49 347 Close( true );
68be9f09
JS
348}
349
350void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
351{
ab1ca7b3 352 (void)wxMessageBox( _T("wxDragImage demo\n")
1cfa5d8e 353 _T("Julian Smart (c) 2000"),
925e9792 354 _T("About wxDragImage Demo"),
1cfa5d8e 355 wxICON_INFORMATION | wxOK );
68be9f09
JS
356}
357
358//-----------------------------------------------------------------------------
359// MyApp
360//-----------------------------------------------------------------------------
361
362BEGIN_EVENT_TABLE(MyApp, wxApp)
363 EVT_MENU(TEST_USE_SCREEN, MyApp::OnUseScreen)
364END_EVENT_TABLE()
365
366MyApp::MyApp()
367{
368 // Drag across whole screen
07850a49 369 m_useScreen = false;
68be9f09
JS
370}
371
372bool MyApp::OnInit()
373{
374#if wxUSE_LIBPNG
375 wxImage::AddHandler( new wxPNGHandler );
376#endif
377
378 wxImage image;
ab1ca7b3 379 if (image.LoadFile(_T("backgrnd.png"), wxBITMAP_TYPE_PNG))
68be9f09 380 {
368d59f0 381 m_background = wxBitmap(image);
68be9f09
JS
382 }
383
68be9f09
JS
384 MyFrame *frame = new MyFrame();
385
ab1ca7b3 386 wxString rootName(_T("shape0"));
68be9f09
JS
387
388 int i;
389 for (i = 1; i < 4; i++)
390 {
391 wxString filename;
4693b20c 392 filename.Printf(wxT("%s%d.png"), (const wxChar*)rootName, i);
2f6c54eb
VZ
393 /* For some reason under wxX11, the 2nd LoadFile in this loop fails, with
394 a BadMatch inside CreateFromImage (inside ConvertToBitmap). This happens even if you copy
395 the first file over the second file. */
68be9f09
JS
396 if (image.LoadFile(filename, wxBITMAP_TYPE_PNG))
397 {
368d59f0 398 DragShape* newShape = new DragShape(wxBitmap(image));
68be9f09
JS
399 newShape->SetPosition(wxPoint(i*50, i*50));
400
401 if (i == 2)
402 newShape->SetDragMethod(SHAPE_DRAG_TEXT);
403 else if (i == 3)
404 newShape->SetDragMethod(SHAPE_DRAG_ICON);
405 else
406 newShape->SetDragMethod(SHAPE_DRAG_BITMAP);
407 frame->GetCanvas()->GetDisplayList().Append(newShape);
408 }
409 }
410
411#if 0
412 // Under Motif or GTK, this demonstrates that
413 // wxScreenDC only gets the root window content.
414 // We need to be able to copy the overall content
415 // for full-screen dragging to work.
416 int w, h;
417 wxDisplaySize(& w, & h);
418 wxBitmap bitmap(w, h);
419
420 wxScreenDC dc;
421 wxMemoryDC memDC;
422 memDC.SelectObject(bitmap);
423 memDC.Blit(0, 0, w, h, & dc, 0, 0);
424 memDC.SelectObject(wxNullBitmap);
425 m_background = bitmap;
426#endif
427
07850a49 428 frame->Show( true );
68be9f09 429
07850a49 430 return true;
68be9f09
JS
431}
432
0cbff120
JS
433int MyApp::OnExit()
434{
0cbff120
JS
435 return 0;
436}
437
68be9f09
JS
438bool MyApp::TileBitmap(const wxRect& rect, wxDC& dc, wxBitmap& bitmap)
439{
440 int w = bitmap.GetWidth();
441 int h = bitmap.GetHeight();
925e9792 442
68be9f09
JS
443 int i, j;
444 for (i = rect.x; i < rect.x + rect.width; i += w)
445 {
446 for (j = rect.y; j < rect.y + rect.height; j+= h)
447 dc.DrawBitmap(bitmap, i, j);
448 }
07850a49 449 return true;
68be9f09
JS
450}
451
87728739 452void MyApp::OnUseScreen(wxCommandEvent& WXUNUSED(event))
68be9f09
JS
453{
454 m_useScreen = !m_useScreen;
455}
456
457// DragShape
458
459DragShape::DragShape(const wxBitmap& bitmap)
460{
461 m_bitmap = bitmap;
462 m_pos.x = 0;
463 m_pos.y = 0;
464 m_dragMethod = SHAPE_DRAG_BITMAP;
07850a49 465 m_show = true;
68be9f09
JS
466}
467
68be9f09
JS
468bool DragShape::HitTest(const wxPoint& pt) const
469{
470 wxRect rect(GetRect());
471 return rect.Inside(pt.x, pt.y);
472}
473
474bool DragShape::Draw(wxDC& dc, int op)
475{
476 if (m_bitmap.Ok())
477 {
478 wxMemoryDC memDC;
479 memDC.SelectObject(m_bitmap);
925e9792 480
68be9f09 481 dc.Blit(m_pos.x, m_pos.y, m_bitmap.GetWidth(), m_bitmap.GetHeight(),
07850a49 482 & memDC, 0, 0, op, true);
68be9f09 483
07850a49 484 return true;
68be9f09
JS
485 }
486 else
07850a49 487 return false;
68be9f09
JS
488}
489