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