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();
142 if (m_currentlyHighlighted
)
144 m_currentlyHighlighted
->Draw(dc
);
146 m_draggedShape
->SetShow(true);
147 m_draggedShape
->Draw(dc
);
149 m_currentlyHighlighted
= (DragShape
*) NULL
;
151 m_draggedShape
= (DragShape
*) NULL
;
153 else if (event
.Dragging() && m_dragMode
!= TEST_DRAG_NONE
)
155 if (m_dragMode
== TEST_DRAG_START
)
157 // We will start dragging if we've moved beyond a couple of pixels
160 int dx
= abs(event
.GetPosition().x
- m_dragStartPos
.x
);
161 int dy
= abs(event
.GetPosition().y
- m_dragStartPos
.y
);
162 if (dx
<= tolerance
&& dy
<= tolerance
)
166 m_dragMode
= TEST_DRAG_DRAGGING
;
171 // Erase the dragged shape from the canvas
172 m_draggedShape
->SetShow(false);
174 EraseShape(m_draggedShape
, dc
);
177 switch (m_draggedShape
->GetDragMethod())
179 case SHAPE_DRAG_BITMAP
:
181 m_dragImage
= new wxDragImage(m_draggedShape
->GetBitmap(), wxCursor(wxCURSOR_HAND
));
184 case SHAPE_DRAG_TEXT
:
186 m_dragImage
= new wxDragImage(wxString(_T("Dragging some test text")), wxCursor(wxCURSOR_HAND
));
189 case SHAPE_DRAG_ICON
:
191 // Can anyone explain why this test is necessary,
192 // to prevent a gcc error?
193 #if defined(__WXMOTIF__) || defined(__WXX11__)
194 wxIcon
icon(dragicon_xpm
);
196 wxIcon
icon(wxICON(dragicon
));
199 m_dragImage
= new wxDragImage(icon
, wxCursor(wxCURSOR_HAND
));
204 bool fullScreen
= wxGetApp().GetUseScreen();
206 // The offset between the top-left of the shape image and the current shape position
207 wxPoint beginDragHotSpot
= m_dragStartPos
- m_draggedShape
->GetPosition();
209 // Now we do this inside the implementation: always assume
210 // coordinates relative to the capture window (client coordinates)
213 // beginDragHotSpot -= ClientToScreen(wxPoint(0, 0));
215 if (!m_dragImage
->BeginDrag(beginDragHotSpot
, this, fullScreen
))
218 m_dragImage
= (wxDragImage
*) NULL
;
219 m_dragMode
= TEST_DRAG_NONE
;
223 m_dragImage
->Move(event
.GetPosition());
227 else if (m_dragMode
== TEST_DRAG_DRAGGING
)
229 // We're currently dragging. See if we're over another shape.
230 DragShape
* onShape
= FindShape(event
.GetPosition());
232 bool mustUnhighlightOld
= false;
233 bool mustHighlightNew
= false;
235 if (m_currentlyHighlighted
)
237 if ((onShape
== (DragShape
*) NULL
) || (m_currentlyHighlighted
!= onShape
))
238 mustUnhighlightOld
= true;
241 if (onShape
&& (onShape
!= m_currentlyHighlighted
) && onShape
->IsShown())
242 mustHighlightNew
= true;
244 if (mustUnhighlightOld
|| mustHighlightNew
)
247 // Now with the drag image switched off, we can change the window contents.
249 if (mustUnhighlightOld
)
251 wxClientDC
clientDC(this);
252 m_currentlyHighlighted
->Draw(clientDC
);
253 m_currentlyHighlighted
= (DragShape
*) NULL
;
255 if (mustHighlightNew
)
257 wxClientDC
clientDC(this);
258 m_currentlyHighlighted
= onShape
;
259 m_currentlyHighlighted
->Draw(clientDC
, wxINVERT
);
262 // Move and show the image again
263 m_dragImage
->Move(event
.GetPosition());
265 if (mustUnhighlightOld
|| mustHighlightNew
)
271 void MyCanvas::DrawShapes(wxDC
& dc
)
273 wxList::compatibility_iterator node
= m_displayList
.GetFirst();
276 DragShape
* shape
= (DragShape
*) node
->GetData();
277 if (shape
->IsShown())
279 node
= node
->GetNext();
283 void MyCanvas::EraseShape(DragShape
* shape
, wxDC
& dc
)
285 wxSize sz
= GetClientSize();
286 wxRect
rect(0, 0, sz
.x
, sz
.y
);
288 wxRect
rect2(shape
->GetRect());
289 dc
.SetClippingRegion(rect2
.x
, rect2
.y
, rect2
.width
, rect2
.height
);
291 wxGetApp().TileBitmap(rect
, dc
, wxGetApp().GetBackgroundBitmap());
293 dc
.DestroyClippingRegion();
296 void MyCanvas::ClearShapes()
298 wxList::compatibility_iterator node
= m_displayList
.GetFirst();
301 DragShape
* shape
= (DragShape
*) node
->GetData();
303 node
= node
->GetNext();
305 m_displayList
.Clear();
308 DragShape
* MyCanvas::FindShape(const wxPoint
& pt
) const
310 wxList::compatibility_iterator node
= m_displayList
.GetFirst();
313 DragShape
* shape
= (DragShape
*) node
->GetData();
314 if (shape
->HitTest(pt
))
316 node
= node
->GetNext();
318 return (DragShape
*) NULL
;
322 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
324 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
325 EVT_MENU (wxID_ABOUT
, MyFrame::OnAbout
)
326 EVT_MENU (wxID_EXIT
, MyFrame::OnQuit
)
330 : wxFrame( (wxFrame
*)NULL
, wxID_ANY
, _T("wxDragImage sample"),
331 wxPoint(20,20), wxSize(470,360) )
333 wxMenu
*file_menu
= new wxMenu();
334 file_menu
->Append( wxID_ABOUT
, _T("&About..."));
335 file_menu
->AppendCheckItem( TEST_USE_SCREEN
, _T("&Use whole screen for dragging"), _T("Use whole screen"));
336 file_menu
->Append( wxID_EXIT
, _T("E&xit"));
338 wxMenuBar
*menu_bar
= new wxMenuBar();
339 menu_bar
->Append(file_menu
, _T("&File"));
341 SetIcon(wxICON(mondrian
));
342 SetMenuBar( menu_bar
);
346 int widths
[] = { -1, 100 };
347 SetStatusWidths( 2, widths
);
348 #endif // wxUSE_STATUSBAR
350 m_canvas
= new MyCanvas( this, wxID_ANY
, wxPoint(0,0), wxSize(10,10) );
353 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
358 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
360 (void)wxMessageBox( _T("wxDragImage demo\n")
361 _T("Julian Smart (c) 2000"),
362 _T("About wxDragImage Demo"),
363 wxICON_INFORMATION
| wxOK
);
366 //-----------------------------------------------------------------------------
368 //-----------------------------------------------------------------------------
370 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
371 EVT_MENU(TEST_USE_SCREEN
, MyApp::OnUseScreen
)
376 // Drag across whole screen
383 wxImage::AddHandler( new wxPNGHandler
);
387 if (image
.LoadFile(_T("backgrnd.png"), wxBITMAP_TYPE_PNG
))
389 m_background
= wxBitmap(image
);
392 MyFrame
*frame
= new MyFrame();
394 wxString
rootName(_T("shape0"));
397 for (i
= 1; i
< 4; i
++)
400 filename
.Printf(wxT("%s%d.png"), (const wxChar
*)rootName
, i
);
401 /* For some reason under wxX11, the 2nd LoadFile in this loop fails, with
402 a BadMatch inside CreateFromImage (inside ConvertToBitmap). This happens even if you copy
403 the first file over the second file. */
404 if (image
.LoadFile(filename
, wxBITMAP_TYPE_PNG
))
406 DragShape
* newShape
= new DragShape(wxBitmap(image
));
407 newShape
->SetPosition(wxPoint(i
*50, i
*50));
410 newShape
->SetDragMethod(SHAPE_DRAG_TEXT
);
412 newShape
->SetDragMethod(SHAPE_DRAG_ICON
);
414 newShape
->SetDragMethod(SHAPE_DRAG_BITMAP
);
415 frame
->GetCanvas()->GetDisplayList().Append(newShape
);
420 // Under Motif or GTK, this demonstrates that
421 // wxScreenDC only gets the root window content.
422 // We need to be able to copy the overall content
423 // for full-screen dragging to work.
425 wxDisplaySize(& w
, & h
);
426 wxBitmap
bitmap(w
, h
);
430 memDC
.SelectObject(bitmap
);
431 memDC
.Blit(0, 0, w
, h
, & dc
, 0, 0);
432 memDC
.SelectObject(wxNullBitmap
);
433 m_background
= bitmap
;
446 bool MyApp::TileBitmap(const wxRect
& rect
, wxDC
& dc
, wxBitmap
& bitmap
)
448 int w
= bitmap
.GetWidth();
449 int h
= bitmap
.GetHeight();
452 for (i
= rect
.x
; i
< rect
.x
+ rect
.width
; i
+= w
)
454 for (j
= rect
.y
; j
< rect
.y
+ rect
.height
; j
+= h
)
455 dc
.DrawBitmap(bitmap
, i
, j
);
460 void MyApp::OnUseScreen(wxCommandEvent
& WXUNUSED(event
))
462 m_useScreen
= !m_useScreen
;
467 DragShape::DragShape(const wxBitmap
& bitmap
)
472 m_dragMethod
= SHAPE_DRAG_BITMAP
;
476 bool DragShape::HitTest(const wxPoint
& pt
) const
478 wxRect
rect(GetRect());
479 return rect
.Inside(pt
.x
, pt
.y
);
482 bool DragShape::Draw(wxDC
& dc
, int op
)
487 memDC
.SelectObject(m_bitmap
);
489 dc
.Blit(m_pos
.x
, m_pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight(),
490 & memDC
, 0, 0, op
, true);