1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDragImage sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
25 // Under Windows, change this to 1
26 // to use wxGenericDragImage
28 #define wxUSE_GENERIC_DRAGIMAGE 1
30 #if wxUSE_GENERIC_DRAGIMAGE
31 #include "wx/generic/dragimgg.h"
32 #define wxDragImage wxGenericDragImage
34 #include "wx/dragimag.h"
39 #if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__) || defined(__WXMGL__) || defined(__WXX11__)
40 #include "mondrian.xpm"
41 #include "dragicon.xpm"
50 IMPLEMENT_CLASS(MyCanvas
, wxScrolledWindow
)
52 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
53 EVT_PAINT(MyCanvas::OnPaint
)
54 EVT_ERASE_BACKGROUND(MyCanvas::OnEraseBackground
)
55 EVT_MOUSE_EVENTS(MyCanvas::OnMouseEvent
)
58 MyCanvas::MyCanvas( wxWindow
*parent
, wxWindowID id
,
59 const wxPoint
&pos
, const wxSize
&size
)
60 : wxScrolledWindow( parent
, id
, pos
, size
, wxSUNKEN_BORDER
)
62 SetBackgroundColour(* wxWHITE
);
64 SetCursor(wxCursor(wxCURSOR_ARROW
));
66 m_dragMode
= TEST_DRAG_NONE
;
67 m_draggedShape
= (DragShape
*) NULL
;
68 m_dragImage
= (wxDragImage
*) NULL
;
69 m_currentlyHighlighted
= (DragShape
*) NULL
;
80 void MyCanvas::OnPaint( wxPaintEvent
&WXUNUSED(event
) )
88 void MyCanvas::OnEraseBackground(wxEraseEvent
& event
)
90 if (wxGetApp().GetBackgroundBitmap().Ok())
92 wxSize sz
= GetClientSize();
93 wxRect
rect(0, 0, sz
.x
, sz
.y
);
97 wxGetApp().TileBitmap(rect
, *(event
.GetDC()), wxGetApp().GetBackgroundBitmap());
102 wxGetApp().TileBitmap(rect
, dc
, wxGetApp().GetBackgroundBitmap());
106 event
.Skip(); // The official way of doing it
109 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
111 if (event
.LeftDown())
113 DragShape
* shape
= FindShape(event
.GetPosition());
116 // We tentatively start dragging, but wait for
117 // mouse movement before dragging properly.
119 m_dragMode
= TEST_DRAG_START
;
120 m_dragStartPos
= event
.GetPosition();
121 m_draggedShape
= shape
;
124 else if (event
.LeftUp() && m_dragMode
!= TEST_DRAG_NONE
)
128 m_dragMode
= TEST_DRAG_NONE
;
130 if (!m_draggedShape
|| !m_dragImage
)
133 m_draggedShape
->SetPosition(m_draggedShape
->GetPosition()
134 + event
.GetPosition() - m_dragStartPos
);
137 m_dragImage
->EndDrag();
141 m_draggedShape
->SetShow(true);
143 m_currentlyHighlighted
= (DragShape
*) NULL
;
145 m_draggedShape
= (DragShape
*) NULL
;
149 else if (event
.Dragging() && m_dragMode
!= TEST_DRAG_NONE
)
151 if (m_dragMode
== TEST_DRAG_START
)
153 // We will start dragging if we've moved beyond a couple of pixels
156 int dx
= abs(event
.GetPosition().x
- m_dragStartPos
.x
);
157 int dy
= abs(event
.GetPosition().y
- m_dragStartPos
.y
);
158 if (dx
<= tolerance
&& dy
<= tolerance
)
162 m_dragMode
= TEST_DRAG_DRAGGING
;
167 // Erase the dragged shape from the canvas
168 m_draggedShape
->SetShow(false);
170 // redraw immediately
174 switch (m_draggedShape
->GetDragMethod())
176 case SHAPE_DRAG_BITMAP
:
178 m_dragImage
= new MyDragImage(this, m_draggedShape
->GetBitmap(), wxCursor(wxCURSOR_HAND
));
181 case SHAPE_DRAG_TEXT
:
183 m_dragImage
= new MyDragImage(this, wxString(wxT("Dragging some test text")), wxCursor(wxCURSOR_HAND
));
186 case SHAPE_DRAG_ICON
:
188 m_dragImage
= new MyDragImage(this, wxICON(dragicon
), wxCursor(wxCURSOR_HAND
));
193 bool fullScreen
= wxGetApp().GetUseScreen();
195 // The offset between the top-left of the shape image and the current shape position
196 wxPoint beginDragHotSpot
= m_dragStartPos
- m_draggedShape
->GetPosition();
198 // Now we do this inside the implementation: always assume
199 // coordinates relative to the capture window (client coordinates)
202 // beginDragHotSpot -= ClientToScreen(wxPoint(0, 0));
204 if (!m_dragImage
->BeginDrag(beginDragHotSpot
, this, fullScreen
))
207 m_dragImage
= (wxDragImage
*) NULL
;
208 m_dragMode
= TEST_DRAG_NONE
;
212 m_dragImage
->Move(event
.GetPosition());
216 else if (m_dragMode
== TEST_DRAG_DRAGGING
)
218 // We're currently dragging. See if we're over another shape.
219 DragShape
* onShape
= FindShape(event
.GetPosition());
221 bool mustUnhighlightOld
= false;
222 bool mustHighlightNew
= false;
224 if (m_currentlyHighlighted
)
226 if ((onShape
== (DragShape
*) NULL
) || (m_currentlyHighlighted
!= onShape
))
227 mustUnhighlightOld
= true;
230 if (onShape
&& (onShape
!= m_currentlyHighlighted
) && onShape
->IsShown())
231 mustHighlightNew
= true;
233 if (mustUnhighlightOld
|| mustHighlightNew
)
236 // Now with the drag image switched off, we can change the window contents.
237 if (mustUnhighlightOld
)
238 m_currentlyHighlighted
= (DragShape
*) NULL
;
240 if (mustHighlightNew
)
241 m_currentlyHighlighted
= onShape
;
243 if (mustUnhighlightOld
|| mustHighlightNew
)
245 Refresh(mustUnhighlightOld
);
249 // Move and show the image again
250 m_dragImage
->Move(event
.GetPosition());
252 if (mustUnhighlightOld
|| mustHighlightNew
)
258 void MyCanvas::DrawShapes(wxDC
& dc
)
260 wxList::compatibility_iterator node
= m_displayList
.GetFirst();
263 DragShape
* shape
= (DragShape
*) node
->GetData();
264 if (shape
->IsShown() && m_draggedShape
!= shape
)
266 shape
->Draw(dc
, (m_currentlyHighlighted
== shape
));
268 node
= node
->GetNext();
272 void MyCanvas::EraseShape(DragShape
* shape
, wxDC
& dc
)
274 wxSize sz
= GetClientSize();
275 wxRect
rect(0, 0, sz
.x
, sz
.y
);
277 wxRect
rect2(shape
->GetRect());
278 dc
.SetClippingRegion(rect2
.x
, rect2
.y
, rect2
.width
, rect2
.height
);
280 wxGetApp().TileBitmap(rect
, dc
, wxGetApp().GetBackgroundBitmap());
282 dc
.DestroyClippingRegion();
285 void MyCanvas::ClearShapes()
287 wxList::compatibility_iterator node
= m_displayList
.GetFirst();
290 DragShape
* shape
= (DragShape
*) node
->GetData();
292 node
= node
->GetNext();
294 m_displayList
.Clear();
297 DragShape
* MyCanvas::FindShape(const wxPoint
& pt
) const
299 wxList::compatibility_iterator node
= m_displayList
.GetFirst();
302 DragShape
* shape
= (DragShape
*) node
->GetData();
303 if (shape
->HitTest(pt
))
305 node
= node
->GetNext();
307 return (DragShape
*) NULL
;
311 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
313 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
314 EVT_MENU (wxID_ABOUT
, MyFrame::OnAbout
)
315 EVT_MENU (wxID_EXIT
, MyFrame::OnQuit
)
319 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, wxT("wxDragImage sample"),
320 wxPoint(20,20), wxSize(470,360) )
322 wxMenu
*file_menu
= new wxMenu();
323 file_menu
->Append( wxID_ABOUT
, wxT("&About..."));
324 file_menu
->AppendCheckItem( TEST_USE_SCREEN
, wxT("&Use whole screen for dragging"), wxT("Use whole screen"));
325 file_menu
->Append( wxID_EXIT
, wxT("E&xit"));
327 wxMenuBar
*menu_bar
= new wxMenuBar();
328 menu_bar
->Append(file_menu
, wxT("&File"));
330 SetIcon(wxICON(mondrian
));
331 SetMenuBar( menu_bar
);
335 int widths
[] = { -1, 100 };
336 SetStatusWidths( 2, widths
);
337 #endif // wxUSE_STATUSBAR
339 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
342 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
347 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
349 (void)wxMessageBox( wxT("wxDragImage demo\n")
350 wxT("Julian Smart (c) 2000"),
351 wxT("About wxDragImage Demo"),
352 wxICON_INFORMATION
| wxOK
);
355 //-----------------------------------------------------------------------------
357 //-----------------------------------------------------------------------------
359 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
360 EVT_MENU(TEST_USE_SCREEN
, MyApp::OnUseScreen
)
365 // Drag across whole screen
371 if ( !wxApp::OnInit() )
375 wxImage::AddHandler( new wxPNGHandler
);
379 if (image
.LoadFile(wxT("backgrnd.png"), wxBITMAP_TYPE_PNG
))
381 m_background
= wxBitmap(image
);
384 MyFrame
*frame
= new MyFrame();
386 wxString
rootName(wxT("shape0"));
388 for (int i
= 1; i
< 4; i
++)
390 /* For some reason under wxX11, the 2nd LoadFile in this loop fails, with
391 a BadMatch inside CreateFromImage (inside ConvertToBitmap). This happens even if you copy
392 the first file over the second file. */
393 if (image
.LoadFile(wxString::Format("%s%d.png", rootName
, i
), wxBITMAP_TYPE_PNG
))
395 DragShape
* newShape
= new DragShape(wxBitmap(image
));
396 newShape
->SetPosition(wxPoint(i
*50, i
*50));
399 newShape
->SetDragMethod(SHAPE_DRAG_TEXT
);
401 newShape
->SetDragMethod(SHAPE_DRAG_ICON
);
403 newShape
->SetDragMethod(SHAPE_DRAG_BITMAP
);
404 frame
->GetCanvas()->GetDisplayList().Append(newShape
);
409 // Under Motif or GTK, this demonstrates that
410 // wxScreenDC only gets the root window content.
411 // We need to be able to copy the overall content
412 // for full-screen dragging to work.
414 wxDisplaySize(& w
, & h
);
415 wxBitmap
bitmap(w
, h
);
419 memDC
.SelectObject(bitmap
);
420 memDC
.Blit(0, 0, w
, h
, & dc
, 0, 0);
421 memDC
.SelectObject(wxNullBitmap
);
422 m_background
= bitmap
;
435 bool MyApp::TileBitmap(const wxRect
& rect
, wxDC
& dc
, wxBitmap
& bitmap
)
437 int w
= bitmap
.GetWidth();
438 int h
= bitmap
.GetHeight();
441 for (i
= rect
.x
; i
< rect
.x
+ rect
.width
; i
+= w
)
443 for (j
= rect
.y
; j
< rect
.y
+ rect
.height
; j
+= h
)
444 dc
.DrawBitmap(bitmap
, i
, j
);
449 void MyApp::OnUseScreen(wxCommandEvent
& WXUNUSED(event
))
451 m_useScreen
= !m_useScreen
;
456 DragShape::DragShape(const wxBitmap
& bitmap
)
461 m_dragMethod
= SHAPE_DRAG_BITMAP
;
465 bool DragShape::HitTest(const wxPoint
& pt
) const
467 wxRect
rect(GetRect());
468 return rect
.Contains(pt
.x
, pt
.y
);
471 bool DragShape::Draw(wxDC
& dc
, bool highlight
)
476 memDC
.SelectObject(m_bitmap
);
478 dc
.Blit(m_pos
.x
, m_pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight(),
479 & memDC
, 0, 0, wxCOPY
, true);
483 dc
.SetPen(*wxWHITE_PEN
);
484 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
485 dc
.DrawRectangle(m_pos
.x
, m_pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight());
496 // On some platforms, notably Mac OS X with Core Graphics, we can't blit from
497 // a window, so we need to draw the background explicitly.
498 bool MyDragImage::UpdateBackingFromWindow(wxDC
& WXUNUSED(windowDC
), wxMemoryDC
& destDC
, const wxRect
& WXUNUSED(sourceRect
),
499 const wxRect
& destRect
) const
501 destDC
.SetClippingRegion(destRect
);
503 if (wxGetApp().GetBackgroundBitmap().Ok())
504 wxGetApp().TileBitmap(destRect
, destDC
, wxGetApp().GetBackgroundBitmap());
506 m_canvas
->DrawShapes(destDC
);