]> git.saurik.com Git - wxWidgets.git/blob - samples/dragimag/dragimag.cpp
Added setters for table row and column count
[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 // 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
19 #include "wx/wx.h"
20 #endif
21
22 #include "wx/image.h"
23
24 // Under Windows, change this to 1
25 // to use wxGenericDragImage
26
27 #define wxUSE_GENERIC_DRAGIMAGE 1
28
29 #if wxUSE_GENERIC_DRAGIMAGE
30 #include "wx/generic/dragimgg.h"
31 #define wxDragImage wxGenericDragImage
32 #else
33 #include "wx/dragimag.h"
34 #endif
35
36 #include "dragimag.h"
37
38 #ifndef wxHAS_IMAGES_IN_RESOURCES
39 #include "../sample.xpm"
40 #include "dragicon.xpm"
41 #endif
42
43 // main program
44
45 IMPLEMENT_APP(MyApp)
46
47 // MyCanvas
48
49 IMPLEMENT_CLASS(MyCanvas, wxScrolledWindow)
50
51 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
52 EVT_PAINT(MyCanvas::OnPaint)
53 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground)
54 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent)
55 END_EVENT_TABLE()
56
57 MyCanvas::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
71 MyCanvas::~MyCanvas()
72 {
73 ClearShapes();
74
75 if (m_dragImage)
76 delete m_dragImage;
77 }
78
79 void MyCanvas::OnPaint( wxPaintEvent &WXUNUSED(event) )
80 {
81 wxPaintDC dc( this );
82 PrepareDC( dc );
83
84 DrawShapes(dc);
85 }
86
87 void MyCanvas::OnEraseBackground(wxEraseEvent& event)
88 {
89 if (wxGetApp().GetBackgroundBitmap().IsOk())
90 {
91 wxSize sz = GetClientSize();
92 wxRect rect(0, 0, sz.x, sz.y);
93
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
108 void 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
132 m_draggedShape->SetPosition(m_draggedShape->GetPosition()
133 + event.GetPosition() - m_dragStartPos);
134
135 m_dragImage->Hide();
136 m_dragImage->EndDrag();
137 wxDELETE(m_dragImage);
138
139 m_draggedShape->SetShow(true);
140
141 m_currentlyHighlighted = (DragShape*) NULL;
142
143 m_draggedShape = (DragShape*) NULL;
144
145 Refresh(true);
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
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
166 m_draggedShape->SetShow(false);
167
168 // redraw immediately
169 Refresh(true);
170 Update();
171
172 switch (m_draggedShape->GetDragMethod())
173 {
174 case SHAPE_DRAG_BITMAP:
175 {
176 m_dragImage = new MyDragImage(this, m_draggedShape->GetBitmap(), wxCursor(wxCURSOR_HAND));
177 break;
178 }
179 case SHAPE_DRAG_TEXT:
180 {
181 m_dragImage = new MyDragImage(this, wxString(wxT("Dragging some test text")), wxCursor(wxCURSOR_HAND));
182 break;
183 }
184 case SHAPE_DRAG_ICON:
185 {
186 m_dragImage = new MyDragImage(this, wxICON(dragicon), wxCursor(wxCURSOR_HAND));
187 break;
188 }
189 }
190
191 bool fullScreen = wxGetApp().GetUseScreen();
192
193 // The offset between the top-left of the shape image and the current shape position
194 wxPoint beginDragHotSpot = m_dragStartPos - m_draggedShape->GetPosition();
195
196 // Now we do this inside the implementation: always assume
197 // coordinates relative to the capture window (client coordinates)
198
199 //if (fullScreen)
200 // beginDragHotSpot -= ClientToScreen(wxPoint(0, 0));
201
202 if (!m_dragImage->BeginDrag(beginDragHotSpot, this, fullScreen))
203 {
204 wxDELETE(m_dragImage);
205 m_dragMode = TEST_DRAG_NONE;
206
207 } else
208 {
209 m_dragImage->Move(event.GetPosition());
210 m_dragImage->Show();
211 }
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
218 bool mustUnhighlightOld = false;
219 bool mustHighlightNew = false;
220
221 if (m_currentlyHighlighted)
222 {
223 if ((onShape == (DragShape*) NULL) || (m_currentlyHighlighted != onShape))
224 mustUnhighlightOld = true;
225 }
226
227 if (onShape && (onShape != m_currentlyHighlighted) && onShape->IsShown())
228 mustHighlightNew = true;
229
230 if (mustUnhighlightOld || mustHighlightNew)
231 m_dragImage->Hide();
232
233 // Now with the drag image switched off, we can change the window contents.
234 if (mustUnhighlightOld)
235 m_currentlyHighlighted = (DragShape*) NULL;
236
237 if (mustHighlightNew)
238 m_currentlyHighlighted = onShape;
239
240 if (mustUnhighlightOld || mustHighlightNew)
241 {
242 Refresh(mustUnhighlightOld);
243 Update();
244 }
245
246 // Move and show the image again
247 m_dragImage->Move(event.GetPosition());
248
249 if (mustUnhighlightOld || mustHighlightNew)
250 m_dragImage->Show();
251 }
252 }
253 }
254
255 void MyCanvas::DrawShapes(wxDC& dc)
256 {
257 wxList::compatibility_iterator node = m_displayList.GetFirst();
258 while (node)
259 {
260 DragShape* shape = (DragShape*) node->GetData();
261 if (shape->IsShown() && m_draggedShape != shape)
262 {
263 shape->Draw(dc, (m_currentlyHighlighted == shape));
264 }
265 node = node->GetNext();
266 }
267 }
268
269 void 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);
276
277 wxGetApp().TileBitmap(rect, dc, wxGetApp().GetBackgroundBitmap());
278
279 dc.DestroyClippingRegion();
280 }
281
282 void MyCanvas::ClearShapes()
283 {
284 wxList::compatibility_iterator node = m_displayList.GetFirst();
285 while (node)
286 {
287 DragShape* shape = (DragShape*) node->GetData();
288 delete shape;
289 node = node->GetNext();
290 }
291 m_displayList.Clear();
292 }
293
294 DragShape* MyCanvas::FindShape(const wxPoint& pt) const
295 {
296 wxList::compatibility_iterator node = m_displayList.GetFirst();
297 while (node)
298 {
299 DragShape* shape = (DragShape*) node->GetData();
300 if (shape->HitTest(pt))
301 return shape;
302 node = node->GetNext();
303 }
304 return (DragShape*) NULL;
305 }
306
307 // MyFrame
308 IMPLEMENT_DYNAMIC_CLASS( MyFrame, wxFrame )
309
310 BEGIN_EVENT_TABLE(MyFrame,wxFrame)
311 EVT_MENU (wxID_ABOUT, MyFrame::OnAbout)
312 EVT_MENU (wxID_EXIT, MyFrame::OnQuit)
313 END_EVENT_TABLE()
314
315 MyFrame::MyFrame()
316 : wxFrame( (wxFrame *)NULL, wxID_ANY, wxT("wxDragImage sample"),
317 wxPoint(20,20), wxSize(470,360) )
318 {
319 wxMenu *file_menu = new wxMenu();
320 file_menu->Append( wxID_ABOUT, wxT("&About"));
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"));
323
324 wxMenuBar *menu_bar = new wxMenuBar();
325 menu_bar->Append(file_menu, wxT("&File"));
326
327 SetIcon(wxICON(sample));
328 SetMenuBar( menu_bar );
329
330 #if wxUSE_STATUSBAR
331 CreateStatusBar(2);
332 int widths[] = { -1, 100 };
333 SetStatusWidths( 2, widths );
334 #endif // wxUSE_STATUSBAR
335
336 m_canvas = new MyCanvas( this, wxID_ANY, wxPoint(0,0), wxSize(10,10) );
337 }
338
339 void MyFrame::OnQuit( wxCommandEvent &WXUNUSED(event) )
340 {
341 Close( true );
342 }
343
344 void MyFrame::OnAbout( wxCommandEvent &WXUNUSED(event) )
345 {
346 (void)wxMessageBox( wxT("wxDragImage demo\n")
347 wxT("Julian Smart (c) 2000"),
348 wxT("About wxDragImage Demo"),
349 wxICON_INFORMATION | wxOK );
350 }
351
352 //-----------------------------------------------------------------------------
353 // MyApp
354 //-----------------------------------------------------------------------------
355
356 BEGIN_EVENT_TABLE(MyApp, wxApp)
357 EVT_MENU(TEST_USE_SCREEN, MyApp::OnUseScreen)
358 END_EVENT_TABLE()
359
360 MyApp::MyApp()
361 {
362 // Drag across whole screen
363 m_useScreen = false;
364 }
365
366 bool MyApp::OnInit()
367 {
368 if ( !wxApp::OnInit() )
369 return false;
370
371 #if wxUSE_LIBPNG
372 wxImage::AddHandler( new wxPNGHandler );
373 #endif
374
375 wxImage image;
376 if (image.LoadFile(wxT("backgrnd.png"), wxBITMAP_TYPE_PNG))
377 {
378 m_background = wxBitmap(image);
379 }
380
381 MyFrame *frame = new MyFrame();
382
383 wxString rootName(wxT("shape0"));
384
385 for (int i = 1; i < 4; i++)
386 {
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. */
390 if (image.LoadFile(wxString::Format("%s%d.png", rootName, i), wxBITMAP_TYPE_PNG))
391 {
392 DragShape* newShape = new DragShape(wxBitmap(image));
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
422 frame->Show( true );
423
424 return true;
425 }
426
427 int MyApp::OnExit()
428 {
429 return 0;
430 }
431
432 bool MyApp::TileBitmap(const wxRect& rect, wxDC& dc, wxBitmap& bitmap)
433 {
434 int w = bitmap.GetWidth();
435 int h = bitmap.GetHeight();
436
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 }
443 return true;
444 }
445
446 void MyApp::OnUseScreen(wxCommandEvent& WXUNUSED(event))
447 {
448 m_useScreen = !m_useScreen;
449 }
450
451 // DragShape
452
453 DragShape::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;
459 m_show = true;
460 }
461
462 bool DragShape::HitTest(const wxPoint& pt) const
463 {
464 wxRect rect(GetRect());
465 return rect.Contains(pt.x, pt.y);
466 }
467
468 bool DragShape::Draw(wxDC& dc, bool highlight)
469 {
470 if (m_bitmap.IsOk())
471 {
472 wxMemoryDC memDC;
473 memDC.SelectObject(m_bitmap);
474
475 dc.Blit(m_pos.x, m_pos.y, m_bitmap.GetWidth(), m_bitmap.GetHeight(),
476 & memDC, 0, 0, wxCOPY, true);
477
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 }
484
485 return true;
486 }
487 else
488 return false;
489 }
490
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.
495 bool MyDragImage::UpdateBackingFromWindow(wxDC& WXUNUSED(windowDC), wxMemoryDC& destDC, const wxRect& WXUNUSED(sourceRect),
496 const wxRect& destRect) const
497 {
498 destDC.SetClippingRegion(destRect);
499
500 if (wxGetApp().GetBackgroundBitmap().IsOk())
501 wxGetApp().TileBitmap(destRect, destDC, wxGetApp().GetBackgroundBitmap());
502
503 m_canvas->DrawShapes(destDC);
504 return true;
505 }
506