]>
git.saurik.com Git - wxWidgets.git/blob - samples/dragimag/dragimag.cpp
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__)
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("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?
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 wxNode
* node
= m_displayList
.First();
276 DragShape
* shape
= (DragShape
*) node
->Data();
277 if (shape
->IsShown())
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 wxNode
* node
= m_displayList
.First();
301 DragShape
* shape
= (DragShape
*) node
->Data();
305 m_displayList
.Clear();
308 DragShape
* MyCanvas::FindShape(const wxPoint
& pt
) const
310 wxNode
* node
= m_displayList
.First();
313 DragShape
* shape
= (DragShape
*) node
->Data();
314 if (shape
->HitTest(pt
))
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, "wxDragImage sample",
331 wxPoint(20,20), wxSize(470,360) )
333 wxMenu
*file_menu
= new wxMenu();
334 file_menu
->Append( wxID_ABOUT
, "&About...");
335 file_menu
->Append( TEST_USE_SCREEN
, "&Use whole screen for dragging", "Use whole screen", TRUE
);
336 file_menu
->Append( wxID_EXIT
, "E&xit");
338 wxMenuBar
*menu_bar
= new wxMenuBar();
339 menu_bar
->Append(file_menu
, "&File");
341 SetMenuBar( menu_bar
);
344 int widths
[] = { -1, 100 };
345 SetStatusWidths( 2, widths
);
347 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
350 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
355 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
357 (void)wxMessageBox( "wxDragImage demo\n"
358 "Julian Smart (c) 2000",
359 "About wxDragImage Demo", wxICON_INFORMATION
| wxOK
);
362 //-----------------------------------------------------------------------------
364 //-----------------------------------------------------------------------------
366 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
367 EVT_MENU(TEST_USE_SCREEN
, MyApp::OnUseScreen
)
372 // Drag across whole screen
379 wxImage::AddHandler( new wxPNGHandler
);
383 if (image
.LoadFile("backgrnd.png", wxBITMAP_TYPE_PNG
))
385 m_background
= image
.ConvertToBitmap();
388 MyFrame
*frame
= new MyFrame();
390 wxString
rootName("shape0");
393 for (i
= 1; i
< 4; i
++)
396 filename
.Printf("%s%d.png", (const char*) rootName
, i
);
397 if (image
.LoadFile(filename
, wxBITMAP_TYPE_PNG
))
399 DragShape
* newShape
= new DragShape(image
.ConvertToBitmap());
400 newShape
->SetPosition(wxPoint(i
*50, i
*50));
403 newShape
->SetDragMethod(SHAPE_DRAG_TEXT
);
405 newShape
->SetDragMethod(SHAPE_DRAG_ICON
);
407 newShape
->SetDragMethod(SHAPE_DRAG_BITMAP
);
408 frame
->GetCanvas()->GetDisplayList().Append(newShape
);
413 // Under Motif or GTK, this demonstrates that
414 // wxScreenDC only gets the root window content.
415 // We need to be able to copy the overall content
416 // for full-screen dragging to work.
418 wxDisplaySize(& w
, & h
);
419 wxBitmap
bitmap(w
, h
);
423 memDC
.SelectObject(bitmap
);
424 memDC
.Blit(0, 0, w
, h
, & dc
, 0, 0);
425 memDC
.SelectObject(wxNullBitmap
);
426 m_background
= bitmap
;
439 bool MyApp::TileBitmap(const wxRect
& rect
, wxDC
& dc
, wxBitmap
& bitmap
)
441 int w
= bitmap
.GetWidth();
442 int h
= bitmap
.GetHeight();
445 for (i
= rect
.x
; i
< rect
.x
+ rect
.width
; i
+= w
)
447 for (j
= rect
.y
; j
< rect
.y
+ rect
.height
; j
+= h
)
448 dc
.DrawBitmap(bitmap
, i
, j
);
453 void MyApp::OnUseScreen(wxCommandEvent
& event
)
455 m_useScreen
= !m_useScreen
;
460 DragShape::DragShape(const wxBitmap
& bitmap
)
465 m_dragMethod
= SHAPE_DRAG_BITMAP
;
469 DragShape::~DragShape()
473 bool DragShape::HitTest(const wxPoint
& pt
) const
475 wxRect
rect(GetRect());
476 return rect
.Inside(pt
.x
, pt
.y
);
479 bool DragShape::Draw(wxDC
& dc
, int op
)
484 memDC
.SelectObject(m_bitmap
);
486 dc
.Blit(m_pos
.x
, m_pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight(),
487 & memDC
, 0, 0, op
, TRUE
);