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
, -1, _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
->Append( TEST_USE_SCREEN
, _T("&Use whole screen for dragging"), _T("Use whole screen"), TRUE
);
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
);
345 int widths
[] = { -1, 100 };
346 SetStatusWidths( 2, widths
);
348 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
351 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
356 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
358 (void)wxMessageBox( _T("wxDragImage demo\n")
359 _T("Julian Smart (c) 2000"),
360 _T("About wxDragImage Demo"),
361 wxICON_INFORMATION
| wxOK
);
364 //-----------------------------------------------------------------------------
366 //-----------------------------------------------------------------------------
368 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
369 EVT_MENU(TEST_USE_SCREEN
, MyApp::OnUseScreen
)
374 // Drag across whole screen
381 wxImage::AddHandler( new wxPNGHandler
);
385 if (image
.LoadFile(_T("backgrnd.png"), wxBITMAP_TYPE_PNG
))
387 m_background
= wxBitmap(image
);
390 MyFrame
*frame
= new MyFrame();
392 wxString
rootName(_T("shape0"));
395 for (i
= 1; i
< 4; i
++)
398 filename
.Printf(wxT("%s%d.png"), (const wxChar
*)rootName
, i
);
399 /* For some reason under wxX11, the 2nd LoadFile in this loop fails, with
400 a BadMatch inside CreateFromImage (inside ConvertToBitmap). This happens even if you copy
401 the first file over the second file. */
402 if (image
.LoadFile(filename
, wxBITMAP_TYPE_PNG
))
404 DragShape
* newShape
= new DragShape(wxBitmap(image
));
405 newShape
->SetPosition(wxPoint(i
*50, i
*50));
408 newShape
->SetDragMethod(SHAPE_DRAG_TEXT
);
410 newShape
->SetDragMethod(SHAPE_DRAG_ICON
);
412 newShape
->SetDragMethod(SHAPE_DRAG_BITMAP
);
413 frame
->GetCanvas()->GetDisplayList().Append(newShape
);
418 // Under Motif or GTK, this demonstrates that
419 // wxScreenDC only gets the root window content.
420 // We need to be able to copy the overall content
421 // for full-screen dragging to work.
423 wxDisplaySize(& w
, & h
);
424 wxBitmap
bitmap(w
, h
);
428 memDC
.SelectObject(bitmap
);
429 memDC
.Blit(0, 0, w
, h
, & dc
, 0, 0);
430 memDC
.SelectObject(wxNullBitmap
);
431 m_background
= bitmap
;
444 bool MyApp::TileBitmap(const wxRect
& rect
, wxDC
& dc
, wxBitmap
& bitmap
)
446 int w
= bitmap
.GetWidth();
447 int h
= bitmap
.GetHeight();
450 for (i
= rect
.x
; i
< rect
.x
+ rect
.width
; i
+= w
)
452 for (j
= rect
.y
; j
< rect
.y
+ rect
.height
; j
+= h
)
453 dc
.DrawBitmap(bitmap
, i
, j
);
458 void MyApp::OnUseScreen(wxCommandEvent
& WXUNUSED(event
))
460 m_useScreen
= !m_useScreen
;
465 DragShape::DragShape(const wxBitmap
& bitmap
)
470 m_dragMethod
= SHAPE_DRAG_BITMAP
;
474 DragShape::~DragShape()
478 bool DragShape::HitTest(const wxPoint
& pt
) const
480 wxRect
rect(GetRect());
481 return rect
.Inside(pt
.x
, pt
.y
);
484 bool DragShape::Draw(wxDC
& dc
, int op
)
489 memDC
.SelectObject(m_bitmap
);
491 dc
.Blit(m_pos
.x
, m_pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight(),
492 & memDC
, 0, 0, op
, TRUE
);