1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDragImage sample
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx/wx.h".
12 #include "wx/wxprec.h"
24 // Under Windows, change this to 1
25 // to use wxGenericDragImage
27 #define wxUSE_GENERIC_DRAGIMAGE 1
29 #if wxUSE_GENERIC_DRAGIMAGE
30 #include "wx/generic/dragimgg.h"
31 #define wxDragImage wxGenericDragImage
33 #include "wx/dragimag.h"
38 #ifndef wxHAS_IMAGES_IN_RESOURCES
39 #include "../sample.xpm"
40 #include "dragicon.xpm"
49 IMPLEMENT_CLASS(MyCanvas
, wxScrolledWindow
)
51 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
52 EVT_PAINT(MyCanvas::OnPaint
)
53 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground
)
54 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
57 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
58 const wxPoint
&pos
, const wxSize
&size
)
59 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
61 SetBackgroundColour(* wxWHITE
);
63 SetCursor(wxCursor(wxCURSOR_ARROW
));
65 m_dragMode
= TEST_DRAG_NONE
;
66 m_draggedShape
= (DragShape
*) NULL
;
67 m_dragImage
= (wxDragImage
*) NULL
;
68 m_currentlyHighlighted
= (DragShape
*) NULL
;
79 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
87 void MyCanvas::OnEraseBackground(wxEraseEvent
& event
)
89 if (wxGetApp().GetBackgroundBitmap().IsOk())
91 wxSize sz
= GetClientSize();
92 wxRect
rect(0, 0, sz
.x
, sz
.y
);
96 wxGetApp().TileBitmap(rect
, *(event
.GetDC()), wxGetApp().GetBackgroundBitmap());
101 wxGetApp().TileBitmap(rect
, dc
, wxGetApp().GetBackgroundBitmap());
105 event
.Skip(); // The official way of doing it
108 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
110 if (event
.LeftDown())
112 DragShape
* shape
= FindShape(event
.GetPosition());
115 // We tentatively start dragging, but wait for
116 // mouse movement before dragging properly.
118 m_dragMode
= TEST_DRAG_START
;
119 m_dragStartPos
= event
.GetPosition();
120 m_draggedShape
= shape
;
123 else if (event
.LeftUp() && m_dragMode
!= TEST_DRAG_NONE
)
127 m_dragMode
= TEST_DRAG_NONE
;
129 if (!m_draggedShape
|| !m_dragImage
)
132 m_draggedShape
->SetPosition(m_draggedShape
->GetPosition()
133 + event
.GetPosition() - m_dragStartPos
);
136 m_dragImage
->EndDrag();
137 wxDELETE(m_dragImage
);
139 m_draggedShape
->SetShow(true);
141 m_currentlyHighlighted
= (DragShape
*) NULL
;
143 m_draggedShape
= (DragShape
*) NULL
;
147 else if (event
.Dragging() && m_dragMode
!= TEST_DRAG_NONE
)
149 if (m_dragMode
== TEST_DRAG_START
)
151 // We will start dragging if we've moved beyond a couple of pixels
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
)
160 m_dragMode
= TEST_DRAG_DRAGGING
;
165 // Erase the dragged shape from the canvas
166 m_draggedShape
->SetShow(false);
168 // redraw immediately
172 switch (m_draggedShape
->GetDragMethod())
174 case SHAPE_DRAG_BITMAP
:
176 m_dragImage
= new MyDragImage(this, m_draggedShape
->GetBitmap(), wxCursor(wxCURSOR_HAND
));
179 case SHAPE_DRAG_TEXT
:
181 m_dragImage
= new MyDragImage(this, wxString(wxT("Dragging some test text")), wxCursor(wxCURSOR_HAND
));
184 case SHAPE_DRAG_ICON
:
186 m_dragImage
= new MyDragImage(this, wxICON(dragicon
), wxCursor(wxCURSOR_HAND
));
191 bool fullScreen
= wxGetApp().GetUseScreen();
193 // The offset between the top-left of the shape image and the current shape position
194 wxPoint beginDragHotSpot
= m_dragStartPos
- m_draggedShape
->GetPosition();
196 // Now we do this inside the implementation: always assume
197 // coordinates relative to the capture window (client coordinates)
200 // beginDragHotSpot -= ClientToScreen(wxPoint(0, 0));
202 if (!m_dragImage
->BeginDrag(beginDragHotSpot
, this, fullScreen
))
204 wxDELETE(m_dragImage
);
205 m_dragMode
= TEST_DRAG_NONE
;
209 m_dragImage
->Move(event
.GetPosition());
213 else if (m_dragMode
== TEST_DRAG_DRAGGING
)
215 // We're currently dragging. See if we're over another shape.
216 DragShape
* onShape
= FindShape(event
.GetPosition());
218 bool mustUnhighlightOld
= false;
219 bool mustHighlightNew
= false;
221 if (m_currentlyHighlighted
)
223 if ((onShape
== (DragShape
*) NULL
) || (m_currentlyHighlighted
!= onShape
))
224 mustUnhighlightOld
= true;
227 if (onShape
&& (onShape
!= m_currentlyHighlighted
) && onShape
->IsShown())
228 mustHighlightNew
= true;
230 if (mustUnhighlightOld
|| mustHighlightNew
)
233 // Now with the drag image switched off, we can change the window contents.
234 if (mustUnhighlightOld
)
235 m_currentlyHighlighted
= (DragShape
*) NULL
;
237 if (mustHighlightNew
)
238 m_currentlyHighlighted
= onShape
;
240 if (mustUnhighlightOld
|| mustHighlightNew
)
242 Refresh(mustUnhighlightOld
);
246 // Move and show the image again
247 m_dragImage
->Move(event
.GetPosition());
249 if (mustUnhighlightOld
|| mustHighlightNew
)
255 void MyCanvas::DrawShapes(wxDC
& dc
)
257 wxList::compatibility_iterator node
= m_displayList
.GetFirst();
260 DragShape
* shape
= (DragShape
*) node
->GetData();
261 if (shape
->IsShown() && m_draggedShape
!= shape
)
263 shape
->Draw(dc
, (m_currentlyHighlighted
== shape
));
265 node
= node
->GetNext();
269 void MyCanvas::EraseShape(DragShape
* shape
, wxDC
& dc
)
271 wxSize sz
= GetClientSize();
272 wxRect
rect(0, 0, sz
.x
, sz
.y
);
274 wxRect
rect2(shape
->GetRect());
275 dc
.SetClippingRegion(rect2
.x
, rect2
.y
, rect2
.width
, rect2
.height
);
277 wxGetApp().TileBitmap(rect
, dc
, wxGetApp().GetBackgroundBitmap());
279 dc
.DestroyClippingRegion();
282 void MyCanvas::ClearShapes()
284 wxList::compatibility_iterator node
= m_displayList
.GetFirst();
287 DragShape
* shape
= (DragShape
*) node
->GetData();
289 node
= node
->GetNext();
291 m_displayList
.Clear();
294 DragShape
* MyCanvas::FindShape(const wxPoint
& pt
) const
296 wxList::compatibility_iterator node
= m_displayList
.GetFirst();
299 DragShape
* shape
= (DragShape
*) node
->GetData();
300 if (shape
->HitTest(pt
))
302 node
= node
->GetNext();
304 return (DragShape
*) NULL
;
308 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
310 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
311 EVT_MENU (wxID_ABOUT
, MyFrame::OnAbout
)
312 EVT_MENU (wxID_EXIT
, MyFrame::OnQuit
)
316 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, wxT("wxDragImage sample"),
317 wxPoint(20,20), wxSize(470,360) )
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"));
324 wxMenuBar
*menu_bar
= new wxMenuBar();
325 menu_bar
->Append(file_menu
, wxT("&File"));
327 SetIcon(wxICON(sample
));
328 SetMenuBar( menu_bar
);
332 int widths
[] = { -1, 100 };
333 SetStatusWidths( 2, widths
);
334 #endif // wxUSE_STATUSBAR
336 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
339 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
344 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
346 (void)wxMessageBox( wxT("wxDragImage demo\n")
347 wxT("Julian Smart (c) 2000"),
348 wxT("About wxDragImage Demo"),
349 wxICON_INFORMATION
| wxOK
);
352 //-----------------------------------------------------------------------------
354 //-----------------------------------------------------------------------------
356 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
357 EVT_MENU(TEST_USE_SCREEN
, MyApp::OnUseScreen
)
362 // Drag across whole screen
368 if ( !wxApp::OnInit() )
372 wxImage::AddHandler( new wxPNGHandler
);
376 if (image
.LoadFile(wxT("backgrnd.png"), wxBITMAP_TYPE_PNG
))
378 m_background
= wxBitmap(image
);
381 MyFrame
*frame
= new MyFrame();
383 wxString
rootName(wxT("shape0"));
385 for (int i
= 1; i
< 4; i
++)
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
))
392 DragShape
* newShape
= new DragShape(wxBitmap(image
));
393 newShape
->SetPosition(wxPoint(i
*50, i
*50));
396 newShape
->SetDragMethod(SHAPE_DRAG_TEXT
);
398 newShape
->SetDragMethod(SHAPE_DRAG_ICON
);
400 newShape
->SetDragMethod(SHAPE_DRAG_BITMAP
);
401 frame
->GetCanvas()->GetDisplayList().Append(newShape
);
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.
411 wxDisplaySize(& w
, & h
);
412 wxBitmap
bitmap(w
, h
);
416 memDC
.SelectObject(bitmap
);
417 memDC
.Blit(0, 0, w
, h
, & dc
, 0, 0);
418 memDC
.SelectObject(wxNullBitmap
);
419 m_background
= bitmap
;
432 bool MyApp::TileBitmap(const wxRect
& rect
, wxDC
& dc
, wxBitmap
& bitmap
)
434 int w
= bitmap
.GetWidth();
435 int h
= bitmap
.GetHeight();
438 for (i
= rect
.x
; i
< rect
.x
+ rect
.width
; i
+= w
)
440 for (j
= rect
.y
; j
< rect
.y
+ rect
.height
; j
+= h
)
441 dc
.DrawBitmap(bitmap
, i
, j
);
446 void MyApp::OnUseScreen(wxCommandEvent
& WXUNUSED(event
))
448 m_useScreen
= !m_useScreen
;
453 DragShape::DragShape(const wxBitmap
& bitmap
)
458 m_dragMethod
= SHAPE_DRAG_BITMAP
;
462 bool DragShape::HitTest(const wxPoint
& pt
) const
464 wxRect
rect(GetRect());
465 return rect
.Contains(pt
.x
, pt
.y
);
468 bool DragShape::Draw(wxDC
& dc
, bool highlight
)
473 memDC
.SelectObject(m_bitmap
);
475 dc
.Blit(m_pos
.x
, m_pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight(),
476 & memDC
, 0, 0, wxCOPY
, true);
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());
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
498 destDC
.SetClippingRegion(destRect
);
500 if (wxGetApp().GetBackgroundBitmap().IsOk())
501 wxGetApp().TileBitmap(destRect
, destDC
, wxGetApp().GetBackgroundBitmap());
503 m_canvas
->DrawShapes(destDC
);