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(__WXMSW__) && !defined(__WXPM__)
40 #include "../sample.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();
138 wxDELETE(m_dragImage
);
140 m_draggedShape
->SetShow(true);
142 m_currentlyHighlighted
= (DragShape
*) NULL
;
144 m_draggedShape
= (DragShape
*) NULL
;
148 else if (event
.Dragging() && m_dragMode
!= TEST_DRAG_NONE
)
150 if (m_dragMode
== TEST_DRAG_START
)
152 // We will start dragging if we've moved beyond a couple of pixels
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
)
161 m_dragMode
= TEST_DRAG_DRAGGING
;
166 // Erase the dragged shape from the canvas
167 m_draggedShape
->SetShow(false);
169 // redraw immediately
173 switch (m_draggedShape
->GetDragMethod())
175 case SHAPE_DRAG_BITMAP
:
177 m_dragImage
= new MyDragImage(this, m_draggedShape
->GetBitmap(), wxCursor(wxCURSOR_HAND
));
180 case SHAPE_DRAG_TEXT
:
182 m_dragImage
= new MyDragImage(this, wxString(wxT("Dragging some test text")), wxCursor(wxCURSOR_HAND
));
185 case SHAPE_DRAG_ICON
:
187 m_dragImage
= new MyDragImage(this, wxICON(dragicon
), wxCursor(wxCURSOR_HAND
));
192 bool fullScreen
= wxGetApp().GetUseScreen();
194 // The offset between the top-left of the shape image and the current shape position
195 wxPoint beginDragHotSpot
= m_dragStartPos
- m_draggedShape
->GetPosition();
197 // Now we do this inside the implementation: always assume
198 // coordinates relative to the capture window (client coordinates)
201 // beginDragHotSpot -= ClientToScreen(wxPoint(0, 0));
203 if (!m_dragImage
->BeginDrag(beginDragHotSpot
, this, fullScreen
))
205 wxDELETE(m_dragImage
);
206 m_dragMode
= TEST_DRAG_NONE
;
210 m_dragImage
->Move(event
.GetPosition());
214 else if (m_dragMode
== TEST_DRAG_DRAGGING
)
216 // We're currently dragging. See if we're over another shape.
217 DragShape
* onShape
= FindShape(event
.GetPosition());
219 bool mustUnhighlightOld
= false;
220 bool mustHighlightNew
= false;
222 if (m_currentlyHighlighted
)
224 if ((onShape
== (DragShape
*) NULL
) || (m_currentlyHighlighted
!= onShape
))
225 mustUnhighlightOld
= true;
228 if (onShape
&& (onShape
!= m_currentlyHighlighted
) && onShape
->IsShown())
229 mustHighlightNew
= true;
231 if (mustUnhighlightOld
|| mustHighlightNew
)
234 // Now with the drag image switched off, we can change the window contents.
235 if (mustUnhighlightOld
)
236 m_currentlyHighlighted
= (DragShape
*) NULL
;
238 if (mustHighlightNew
)
239 m_currentlyHighlighted
= onShape
;
241 if (mustUnhighlightOld
|| mustHighlightNew
)
243 Refresh(mustUnhighlightOld
);
247 // Move and show the image again
248 m_dragImage
->Move(event
.GetPosition());
250 if (mustUnhighlightOld
|| mustHighlightNew
)
256 void MyCanvas::DrawShapes(wxDC
& dc
)
258 wxList::compatibility_iterator node
= m_displayList
.GetFirst();
261 DragShape
* shape
= (DragShape
*) node
->GetData();
262 if (shape
->IsShown() && m_draggedShape
!= shape
)
264 shape
->Draw(dc
, (m_currentlyHighlighted
== shape
));
266 node
= node
->GetNext();
270 void MyCanvas::EraseShape(DragShape
* shape
, wxDC
& dc
)
272 wxSize sz
= GetClientSize();
273 wxRect
rect(0, 0, sz
.x
, sz
.y
);
275 wxRect
rect2(shape
->GetRect());
276 dc
.SetClippingRegion(rect2
.x
, rect2
.y
, rect2
.width
, rect2
.height
);
278 wxGetApp().TileBitmap(rect
, dc
, wxGetApp().GetBackgroundBitmap());
280 dc
.DestroyClippingRegion();
283 void MyCanvas::ClearShapes()
285 wxList::compatibility_iterator node
= m_displayList
.GetFirst();
288 DragShape
* shape
= (DragShape
*) node
->GetData();
290 node
= node
->GetNext();
292 m_displayList
.Clear();
295 DragShape
* MyCanvas::FindShape(const wxPoint
& pt
) const
297 wxList::compatibility_iterator node
= m_displayList
.GetFirst();
300 DragShape
* shape
= (DragShape
*) node
->GetData();
301 if (shape
->HitTest(pt
))
303 node
= node
->GetNext();
305 return (DragShape
*) NULL
;
309 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
311 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
312 EVT_MENU (wxID_ABOUT
, MyFrame::OnAbout
)
313 EVT_MENU (wxID_EXIT
, MyFrame::OnQuit
)
317 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, wxT("wxDragImage sample"),
318 wxPoint(20,20), wxSize(470,360) )
320 wxMenu
*file_menu
= new wxMenu();
321 file_menu
->Append( wxID_ABOUT
, wxT("&About..."));
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"));
325 wxMenuBar
*menu_bar
= new wxMenuBar();
326 menu_bar
->Append(file_menu
, wxT("&File"));
328 SetIcon(wxICON(sample
));
329 SetMenuBar( menu_bar
);
333 int widths
[] = { -1, 100 };
334 SetStatusWidths( 2, widths
);
335 #endif // wxUSE_STATUSBAR
337 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
340 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
345 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
347 (void)wxMessageBox( wxT("wxDragImage demo\n")
348 wxT("Julian Smart (c) 2000"),
349 wxT("About wxDragImage Demo"),
350 wxICON_INFORMATION
| wxOK
);
353 //-----------------------------------------------------------------------------
355 //-----------------------------------------------------------------------------
357 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
358 EVT_MENU(TEST_USE_SCREEN
, MyApp::OnUseScreen
)
363 // Drag across whole screen
369 if ( !wxApp::OnInit() )
373 wxImage::AddHandler( new wxPNGHandler
);
377 if (image
.LoadFile(wxT("backgrnd.png"), wxBITMAP_TYPE_PNG
))
379 m_background
= wxBitmap(image
);
382 MyFrame
*frame
= new MyFrame();
384 wxString
rootName(wxT("shape0"));
386 for (int i
= 1; i
< 4; i
++)
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. */
391 if (image
.LoadFile(wxString::Format("%s%d.png", rootName
, i
), wxBITMAP_TYPE_PNG
))
393 DragShape
* newShape
= new DragShape(wxBitmap(image
));
394 newShape
->SetPosition(wxPoint(i
*50, i
*50));
397 newShape
->SetDragMethod(SHAPE_DRAG_TEXT
);
399 newShape
->SetDragMethod(SHAPE_DRAG_ICON
);
401 newShape
->SetDragMethod(SHAPE_DRAG_BITMAP
);
402 frame
->GetCanvas()->GetDisplayList().Append(newShape
);
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.
412 wxDisplaySize(& w
, & h
);
413 wxBitmap
bitmap(w
, h
);
417 memDC
.SelectObject(bitmap
);
418 memDC
.Blit(0, 0, w
, h
, & dc
, 0, 0);
419 memDC
.SelectObject(wxNullBitmap
);
420 m_background
= bitmap
;
433 bool MyApp::TileBitmap(const wxRect
& rect
, wxDC
& dc
, wxBitmap
& bitmap
)
435 int w
= bitmap
.GetWidth();
436 int h
= bitmap
.GetHeight();
439 for (i
= rect
.x
; i
< rect
.x
+ rect
.width
; i
+= w
)
441 for (j
= rect
.y
; j
< rect
.y
+ rect
.height
; j
+= h
)
442 dc
.DrawBitmap(bitmap
, i
, j
);
447 void MyApp::OnUseScreen(wxCommandEvent
& WXUNUSED(event
))
449 m_useScreen
= !m_useScreen
;
454 DragShape::DragShape(const wxBitmap
& bitmap
)
459 m_dragMethod
= SHAPE_DRAG_BITMAP
;
463 bool DragShape::HitTest(const wxPoint
& pt
) const
465 wxRect
rect(GetRect());
466 return rect
.Contains(pt
.x
, pt
.y
);
469 bool DragShape::Draw(wxDC
& dc
, bool highlight
)
474 memDC
.SelectObject(m_bitmap
);
476 dc
.Blit(m_pos
.x
, m_pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight(),
477 & memDC
, 0, 0, wxCOPY
, true);
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());
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.
496 bool MyDragImage::UpdateBackingFromWindow(wxDC
& WXUNUSED(windowDC
), wxMemoryDC
& destDC
, const wxRect
& WXUNUSED(sourceRect
),
497 const wxRect
& destRect
) const
499 destDC
.SetClippingRegion(destRect
);
501 if (wxGetApp().GetBackgroundBitmap().Ok())
502 wxGetApp().TileBitmap(destRect
, destDC
, wxGetApp().GetBackgroundBitmap());
504 m_canvas
->DrawShapes(destDC
);