]> git.saurik.com Git - wxWidgets.git/blame - samples/dragimag/dragimag.cpp
No real changes, just simplify wxBookCtrlBase unit test a little.
[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
e7092398 39#ifndef wxHAS_IMAGES_IN_RESOURCES
3cb332c1
VZ
40 #include "../sample.xpm"
41 #include "dragicon.xpm"
68be9f09
JS
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{
a1b806b9 90 if (wxGetApp().GetBackgroundBitmap().IsOk())
68be9f09
JS
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();
5276b0a5 138 wxDELETE(m_dragImage);
68be9f09 139
07850a49 140 m_draggedShape->SetShow(true);
68be9f09
JS
141
142 m_currentlyHighlighted = (DragShape*) NULL;
143
144 m_draggedShape = (DragShape*) NULL;
ce00f59b 145
aa7a6a0e 146 Refresh(true);
68be9f09
JS
147 }
148 else if (event.Dragging() && m_dragMode != TEST_DRAG_NONE)
149 {
150 if (m_dragMode == TEST_DRAG_START)
151 {
152 // We will start dragging if we've moved beyond a couple of pixels
153
154 int tolerance = 2;
155 int dx = abs(event.GetPosition().x - m_dragStartPos.x);
156 int dy = abs(event.GetPosition().y - m_dragStartPos.y);
157 if (dx <= tolerance && dy <= tolerance)
158 return;
159
68be9f09
JS
160 // Start the drag.
161 m_dragMode = TEST_DRAG_DRAGGING;
162
163 if (m_dragImage)
164 delete m_dragImage;
165
166 // Erase the dragged shape from the canvas
07850a49 167 m_draggedShape->SetShow(false);
aa7a6a0e
JS
168
169 // redraw immediately
170 Refresh(true);
171 Update();
68be9f09
JS
172
173 switch (m_draggedShape->GetDragMethod())
174 {
175 case SHAPE_DRAG_BITMAP:
176 {
aa7a6a0e 177 m_dragImage = new MyDragImage(this, m_draggedShape->GetBitmap(), wxCursor(wxCURSOR_HAND));
68be9f09
JS
178 break;
179 }
180 case SHAPE_DRAG_TEXT:
181 {
9a83f860 182 m_dragImage = new MyDragImage(this, wxString(wxT("Dragging some test text")), wxCursor(wxCURSOR_HAND));
68be9f09
JS
183 break;
184 }
185 case SHAPE_DRAG_ICON:
186 {
aa7a6a0e 187 m_dragImage = new MyDragImage(this, wxICON(dragicon), wxCursor(wxCURSOR_HAND));
68be9f09
JS
188 break;
189 }
190 }
191
aa2d25a5 192 bool fullScreen = wxGetApp().GetUseScreen();
68be9f09 193
aa2d25a5
JS
194 // The offset between the top-left of the shape image and the current shape position
195 wxPoint beginDragHotSpot = m_dragStartPos - m_draggedShape->GetPosition();
925e9792 196
aa2d25a5
JS
197 // Now we do this inside the implementation: always assume
198 // coordinates relative to the capture window (client coordinates)
68be9f09 199
aa2d25a5
JS
200 //if (fullScreen)
201 // beginDragHotSpot -= ClientToScreen(wxPoint(0, 0));
925e9792 202
aa2d25a5 203 if (!m_dragImage->BeginDrag(beginDragHotSpot, this, fullScreen))
68be9f09 204 {
5276b0a5 205 wxDELETE(m_dragImage);
68be9f09 206 m_dragMode = TEST_DRAG_NONE;
925e9792 207
aa2d25a5
JS
208 } else
209 {
210 m_dragImage->Move(event.GetPosition());
211 m_dragImage->Show();
68be9f09 212 }
68be9f09
JS
213 }
214 else if (m_dragMode == TEST_DRAG_DRAGGING)
215 {
216 // We're currently dragging. See if we're over another shape.
217 DragShape* onShape = FindShape(event.GetPosition());
218
07850a49
WS
219 bool mustUnhighlightOld = false;
220 bool mustHighlightNew = false;
68be9f09
JS
221
222 if (m_currentlyHighlighted)
223 {
224 if ((onShape == (DragShape*) NULL) || (m_currentlyHighlighted != onShape))
07850a49 225 mustUnhighlightOld = true;
68be9f09
JS
226 }
227
228 if (onShape && (onShape != m_currentlyHighlighted) && onShape->IsShown())
07850a49 229 mustHighlightNew = true;
68be9f09
JS
230
231 if (mustUnhighlightOld || mustHighlightNew)
232 m_dragImage->Hide();
ce00f59b 233
68be9f09 234 // Now with the drag image switched off, we can change the window contents.
68be9f09 235 if (mustUnhighlightOld)
68be9f09 236 m_currentlyHighlighted = (DragShape*) NULL;
aa7a6a0e 237
68be9f09 238 if (mustHighlightNew)
68be9f09 239 m_currentlyHighlighted = onShape;
aa7a6a0e
JS
240
241 if (mustUnhighlightOld || mustHighlightNew)
242 {
243 Refresh(mustUnhighlightOld);
244 Update();
68be9f09
JS
245 }
246
68be9f09 247 // Move and show the image again
aa2d25a5 248 m_dragImage->Move(event.GetPosition());
68be9f09
JS
249
250 if (mustUnhighlightOld || mustHighlightNew)
251 m_dragImage->Show();
252 }
253 }
254}
255
256void MyCanvas::DrawShapes(wxDC& dc)
257{
ccd7d9e5 258 wxList::compatibility_iterator node = m_displayList.GetFirst();
68be9f09
JS
259 while (node)
260 {
b1d4dd7a 261 DragShape* shape = (DragShape*) node->GetData();
aa7a6a0e
JS
262 if (shape->IsShown() && m_draggedShape != shape)
263 {
264 shape->Draw(dc, (m_currentlyHighlighted == shape));
265 }
b1d4dd7a 266 node = node->GetNext();
68be9f09
JS
267 }
268}
269
270void MyCanvas::EraseShape(DragShape* shape, wxDC& dc)
271{
272 wxSize sz = GetClientSize();
273 wxRect rect(0, 0, sz.x, sz.y);
274
275 wxRect rect2(shape->GetRect());
276 dc.SetClippingRegion(rect2.x, rect2.y, rect2.width, rect2.height);
925e9792 277
68be9f09
JS
278 wxGetApp().TileBitmap(rect, dc, wxGetApp().GetBackgroundBitmap());
279
280 dc.DestroyClippingRegion();
281}
282
283void MyCanvas::ClearShapes()
284{
ccd7d9e5 285 wxList::compatibility_iterator node = m_displayList.GetFirst();
68be9f09
JS
286 while (node)
287 {
b1d4dd7a 288 DragShape* shape = (DragShape*) node->GetData();
68be9f09 289 delete shape;
b1d4dd7a 290 node = node->GetNext();
68be9f09
JS
291 }
292 m_displayList.Clear();
293}
294
295DragShape* MyCanvas::FindShape(const wxPoint& pt) const
296{
ccd7d9e5 297 wxList::compatibility_iterator node = m_displayList.GetFirst();
68be9f09
JS
298 while (node)
299 {
b1d4dd7a 300 DragShape* shape = (DragShape*) node->GetData();
68be9f09
JS
301 if (shape->HitTest(pt))
302 return shape;
b1d4dd7a 303 node = node->GetNext();
68be9f09
JS
304 }
305 return (DragShape*) NULL;
306}
307
308// MyFrame
68be9f09
JS
309IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
310
311BEGIN_EVENT_TABLE(MyFrame,wxFrame)
312 EVT_MENU (wxID_ABOUT, MyFrame::OnAbout)
313 EVT_MENU (wxID_EXIT, MyFrame::OnQuit)
314END_EVENT_TABLE()
315
316MyFrame::MyFrame()
9a83f860 317: wxFrame( (wxFrame *)NULL, wxID_ANY, wxT("wxDragImage sample"),
1cfa5d8e 318 wxPoint(20,20), wxSize(470,360) )
68be9f09 319{
1cfa5d8e 320 wxMenu *file_menu = new wxMenu();
2d143b66 321 file_menu->Append( wxID_ABOUT, wxT("&About"));
9a83f860
VZ
322 file_menu->AppendCheckItem( TEST_USE_SCREEN, wxT("&Use whole screen for dragging"), wxT("Use whole screen"));
323 file_menu->Append( wxID_EXIT, wxT("E&xit"));
925e9792 324
1cfa5d8e 325 wxMenuBar *menu_bar = new wxMenuBar();
9a83f860 326 menu_bar->Append(file_menu, wxT("&File"));
d8d18184 327
3cb332c1 328 SetIcon(wxICON(sample));
1cfa5d8e 329 SetMenuBar( menu_bar );
925e9792 330
8520f137 331#if wxUSE_STATUSBAR
1cfa5d8e
JS
332 CreateStatusBar(2);
333 int widths[] = { -1, 100 };
334 SetStatusWidths( 2, widths );
8520f137 335#endif // wxUSE_STATUSBAR
925e9792 336
07850a49 337 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) );
68be9f09
JS
338}
339
340void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
341{
07850a49 342 Close( true );
68be9f09
JS
343}
344
345void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
346{
9a83f860
VZ
347 (void)wxMessageBox( wxT("wxDragImage demo\n")
348 wxT("Julian Smart (c) 2000"),
349 wxT("About wxDragImage Demo"),
1cfa5d8e 350 wxICON_INFORMATION | wxOK );
68be9f09
JS
351}
352
353//-----------------------------------------------------------------------------
354// MyApp
355//-----------------------------------------------------------------------------
356
357BEGIN_EVENT_TABLE(MyApp, wxApp)
358 EVT_MENU(TEST_USE_SCREEN, MyApp::OnUseScreen)
359END_EVENT_TABLE()
360
361MyApp::MyApp()
362{
363 // Drag across whole screen
07850a49 364 m_useScreen = false;
68be9f09
JS
365}
366
367bool MyApp::OnInit()
368{
45e6e6f8
VZ
369 if ( !wxApp::OnInit() )
370 return false;
371
68be9f09
JS
372#if wxUSE_LIBPNG
373 wxImage::AddHandler( new wxPNGHandler );
374#endif
375
376 wxImage image;
9a83f860 377 if (image.LoadFile(wxT("backgrnd.png"), wxBITMAP_TYPE_PNG))
68be9f09 378 {
368d59f0 379 m_background = wxBitmap(image);
68be9f09
JS
380 }
381
68be9f09
JS
382 MyFrame *frame = new MyFrame();
383
9a83f860 384 wxString rootName(wxT("shape0"));
68be9f09 385
1606607a 386 for (int i = 1; i < 4; i++)
68be9f09 387 {
2f6c54eb
VZ
388 /* For some reason under wxX11, the 2nd LoadFile in this loop fails, with
389 a BadMatch inside CreateFromImage (inside ConvertToBitmap). This happens even if you copy
390 the first file over the second file. */
1606607a 391 if (image.LoadFile(wxString::Format("%s%d.png", rootName, i), wxBITMAP_TYPE_PNG))
68be9f09 392 {
368d59f0 393 DragShape* newShape = new DragShape(wxBitmap(image));
68be9f09
JS
394 newShape->SetPosition(wxPoint(i*50, i*50));
395
396 if (i == 2)
397 newShape->SetDragMethod(SHAPE_DRAG_TEXT);
398 else if (i == 3)
399 newShape->SetDragMethod(SHAPE_DRAG_ICON);
400 else
401 newShape->SetDragMethod(SHAPE_DRAG_BITMAP);
402 frame->GetCanvas()->GetDisplayList().Append(newShape);
403 }
404 }
405
406#if 0
407 // Under Motif or GTK, this demonstrates that
408 // wxScreenDC only gets the root window content.
409 // We need to be able to copy the overall content
410 // for full-screen dragging to work.
411 int w, h;
412 wxDisplaySize(& w, & h);
413 wxBitmap bitmap(w, h);
414
415 wxScreenDC dc;
416 wxMemoryDC memDC;
417 memDC.SelectObject(bitmap);
418 memDC.Blit(0, 0, w, h, & dc, 0, 0);
419 memDC.SelectObject(wxNullBitmap);
420 m_background = bitmap;
421#endif
422
07850a49 423 frame->Show( true );
68be9f09 424
07850a49 425 return true;
68be9f09
JS
426}
427
0cbff120
JS
428int MyApp::OnExit()
429{
0cbff120
JS
430 return 0;
431}
432
68be9f09
JS
433bool MyApp::TileBitmap(const wxRect& rect, wxDC& dc, wxBitmap& bitmap)
434{
435 int w = bitmap.GetWidth();
436 int h = bitmap.GetHeight();
925e9792 437
68be9f09
JS
438 int i, j;
439 for (i = rect.x; i < rect.x + rect.width; i += w)
440 {
441 for (j = rect.y; j < rect.y + rect.height; j+= h)
442 dc.DrawBitmap(bitmap, i, j);
443 }
07850a49 444 return true;
68be9f09
JS
445}
446
87728739 447void MyApp::OnUseScreen(wxCommandEvent& WXUNUSED(event))
68be9f09
JS
448{
449 m_useScreen = !m_useScreen;
450}
451
452// DragShape
453
454DragShape::DragShape(const wxBitmap& bitmap)
455{
456 m_bitmap = bitmap;
457 m_pos.x = 0;
458 m_pos.y = 0;
459 m_dragMethod = SHAPE_DRAG_BITMAP;
07850a49 460 m_show = true;
68be9f09
JS
461}
462
68be9f09
JS
463bool DragShape::HitTest(const wxPoint& pt) const
464{
465 wxRect rect(GetRect());
50dd4ca9 466 return rect.Contains(pt.x, pt.y);
68be9f09
JS
467}
468
aa7a6a0e 469bool DragShape::Draw(wxDC& dc, bool highlight)
68be9f09 470{
a1b806b9 471 if (m_bitmap.IsOk())
68be9f09
JS
472 {
473 wxMemoryDC memDC;
474 memDC.SelectObject(m_bitmap);
925e9792 475
68be9f09 476 dc.Blit(m_pos.x, m_pos.y, m_bitmap.GetWidth(), m_bitmap.GetHeight(),
aa7a6a0e 477 & memDC, 0, 0, wxCOPY, true);
ce00f59b 478
aa7a6a0e
JS
479 if (highlight)
480 {
481 dc.SetPen(*wxWHITE_PEN);
482 dc.SetBrush(*wxTRANSPARENT_BRUSH);
483 dc.DrawRectangle(m_pos.x, m_pos.y, m_bitmap.GetWidth(), m_bitmap.GetHeight());
484 }
68be9f09 485
07850a49 486 return true;
68be9f09
JS
487 }
488 else
07850a49 489 return false;
68be9f09
JS
490}
491
aa7a6a0e
JS
492// MyDragImage
493
494// On some platforms, notably Mac OS X with Core Graphics, we can't blit from
495// a window, so we need to draw the background explicitly.
496bool MyDragImage::UpdateBackingFromWindow(wxDC& WXUNUSED(windowDC), wxMemoryDC& destDC, const wxRect& WXUNUSED(sourceRect),
497 const wxRect& destRect) const
498{
499 destDC.SetClippingRegion(destRect);
500
a1b806b9 501 if (wxGetApp().GetBackgroundBitmap().IsOk())
aa7a6a0e
JS
502 wxGetApp().TileBitmap(destRect, destDC, wxGetApp().GetBackgroundBitmap());
503
504 m_canvas->DrawShapes(destDC);
505 return true;
506}
507