]>
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__)
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
) )
89 void MyCanvas::OnEraseBackground(wxEraseEvent
& event
)
91 if (wxGetApp().GetBackgroundBitmap().Ok())
93 wxSize sz
= GetClientSize();
94 wxRect
rect(0, 0, sz
.x
, sz
.y
);
98 wxGetApp().TileBitmap(rect
, *(event
.GetDC()), wxGetApp().GetBackgroundBitmap());
103 wxGetApp().TileBitmap(rect
, dc
, wxGetApp().GetBackgroundBitmap());
107 event
.Skip(); // The official way of doing it
110 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
112 if (event
.LeftDown())
114 DragShape
* shape
= FindShape(event
.GetPosition());
117 // We tentatively start dragging, but wait for
118 // mouse movement before dragging properly.
120 m_dragMode
= TEST_DRAG_START
;
121 m_dragStartPos
= event
.GetPosition();
122 m_draggedShape
= shape
;
125 else if (event
.LeftUp() && m_dragMode
!= TEST_DRAG_NONE
)
129 m_dragMode
= TEST_DRAG_NONE
;
131 if (!m_draggedShape
|| !m_dragImage
)
134 wxPoint
newPos(m_draggedShape
->GetPosition().x
+ (event
.GetPosition().x
- m_dragStartPos
.x
),
135 m_draggedShape
->GetPosition().y
+ (event
.GetPosition().y
- m_dragStartPos
.y
));
137 m_draggedShape
->SetPosition(newPos
);
140 m_dragImage
->EndDrag();
145 if (m_currentlyHighlighted
)
147 m_currentlyHighlighted
->Draw(dc
);
149 m_draggedShape
->SetShow(TRUE
);
150 m_draggedShape
->Draw(dc
);
152 m_currentlyHighlighted
= (DragShape
*) NULL
;
154 m_draggedShape
= (DragShape
*) NULL
;
156 else if (event
.Dragging() && m_dragMode
!= TEST_DRAG_NONE
)
158 if (m_dragMode
== TEST_DRAG_START
)
160 // We will start dragging if we've moved beyond a couple of pixels
163 int dx
= abs(event
.GetPosition().x
- m_dragStartPos
.x
);
164 int dy
= abs(event
.GetPosition().y
- m_dragStartPos
.y
);
165 if (dx
<= tolerance
&& dy
<= tolerance
)
168 wxPoint
newPos(m_draggedShape
->GetPosition().x
+ (event
.GetPosition().x
- m_dragStartPos
.x
),
169 m_draggedShape
->GetPosition().y
+ (event
.GetPosition().y
- m_dragStartPos
.y
));
172 m_dragMode
= TEST_DRAG_DRAGGING
;
177 // Erase the dragged shape from the canvas
178 m_draggedShape
->SetShow(FALSE
);
180 EraseShape(m_draggedShape
, dc
);
183 switch (m_draggedShape
->GetDragMethod())
185 case SHAPE_DRAG_BITMAP
:
187 wxPoint
hotSpot(event
.GetPosition().x
- newPos
.x
, event
.GetPosition().y
- newPos
.y
);
188 m_dragImage
= new wxDragImage(m_draggedShape
->GetBitmap(), wxCursor(wxCURSOR_HAND
), hotSpot
);
191 case SHAPE_DRAG_TEXT
:
193 wxPoint
hotSpot(event
.GetPosition().x
- newPos
.x
, event
.GetPosition().y
- newPos
.y
);
194 m_dragImage
= new wxDragImage("Dragging some test text", wxCursor(wxCURSOR_HAND
), hotSpot
);
197 case SHAPE_DRAG_ICON
:
199 wxPoint
hotSpot(event
.GetPosition().x
- newPos
.x
, event
.GetPosition().y
- newPos
.y
);
201 // Can anyone explain why this test is necessary,
202 // to prevent a gcc error?
204 wxIcon
icon(dragicon_xpm
);
206 wxIcon
icon(wxICON(dragicon
));
209 m_dragImage
= new wxDragImage(icon
, wxCursor(wxCURSOR_HAND
), hotSpot
);
214 bool fullScreen
= FALSE
;
215 if (wxGetApp().GetUseScreen())
217 newPos
= ClientToScreen(newPos
);
224 // This line uses the whole screen...
225 retValue
= m_dragImage
->BeginDrag(wxPoint(0, 0), this, TRUE
);
226 // while this line restricts dragging to the parent frame.
227 // retValue = m_dragImage->BeginDrag(wxPoint(0, 0), this, GetParent());
229 retValue
= m_dragImage
->BeginDrag(wxPoint(0, 0), this);
234 m_dragImage
= (wxDragImage
*) NULL
;
235 m_dragMode
= TEST_DRAG_NONE
;
237 m_dragImage
->Move(newPos
);
240 else if (m_dragMode
== TEST_DRAG_DRAGGING
)
242 // We're currently dragging. See if we're over another shape.
243 DragShape
* onShape
= FindShape(event
.GetPosition());
245 bool mustUnhighlightOld
= FALSE
;
246 bool mustHighlightNew
= FALSE
;
248 if (m_currentlyHighlighted
)
250 if ((onShape
== (DragShape
*) NULL
) || (m_currentlyHighlighted
!= onShape
))
251 mustUnhighlightOld
= TRUE
;
254 if (onShape
&& (onShape
!= m_currentlyHighlighted
) && onShape
->IsShown())
255 mustHighlightNew
= TRUE
;
257 if (mustUnhighlightOld
|| mustHighlightNew
)
260 // Now with the drag image switched off, we can change the window contents.
262 if (mustUnhighlightOld
)
264 wxClientDC
clientDC(this);
265 m_currentlyHighlighted
->Draw(clientDC
);
266 m_currentlyHighlighted
= (DragShape
*) NULL
;
268 if (mustHighlightNew
)
270 wxClientDC
clientDC(this);
271 m_currentlyHighlighted
= onShape
;
272 m_currentlyHighlighted
->Draw(clientDC
, wxINVERT
);
275 wxPoint
newPos(m_draggedShape
->GetPosition().x
+ (event
.GetPosition().x
- m_dragStartPos
.x
),
276 m_draggedShape
->GetPosition().y
+ (event
.GetPosition().y
- m_dragStartPos
.y
));
278 if (wxGetApp().GetUseScreen())
280 newPos
= ClientToScreen(newPos
);
283 // Move and show the image again
284 m_dragImage
->Move(newPos
);
286 if (mustUnhighlightOld
|| mustHighlightNew
)
292 void MyCanvas::DrawShapes(wxDC
& dc
)
294 wxNode
* node
= m_displayList
.First();
297 DragShape
* shape
= (DragShape
*) node
->Data();
298 if (shape
->IsShown())
304 void MyCanvas::EraseShape(DragShape
* shape
, wxDC
& dc
)
306 wxSize sz
= GetClientSize();
307 wxRect
rect(0, 0, sz
.x
, sz
.y
);
309 wxRect
rect2(shape
->GetRect());
310 dc
.SetClippingRegion(rect2
.x
, rect2
.y
, rect2
.width
, rect2
.height
);
312 wxGetApp().TileBitmap(rect
, dc
, wxGetApp().GetBackgroundBitmap());
314 dc
.DestroyClippingRegion();
317 void MyCanvas::ClearShapes()
319 wxNode
* node
= m_displayList
.First();
322 DragShape
* shape
= (DragShape
*) node
->Data();
326 m_displayList
.Clear();
329 DragShape
* MyCanvas::FindShape(const wxPoint
& pt
) const
331 wxNode
* node
= m_displayList
.First();
334 DragShape
* shape
= (DragShape
*) node
->Data();
335 if (shape
->HitTest(pt
))
339 return (DragShape
*) NULL
;
344 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
346 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
347 EVT_MENU (wxID_ABOUT
, MyFrame::OnAbout
)
348 EVT_MENU (wxID_EXIT
, MyFrame::OnQuit
)
352 : wxFrame( (wxFrame
*)NULL
, -1, "wxDragImage sample",
353 wxPoint(20,20), wxSize(470,360) )
355 wxMenu
*file_menu
= new wxMenu();
356 file_menu
->Append( wxID_ABOUT
, "&About...");
357 file_menu
->Append( TEST_USE_SCREEN
, "&Use whole screen for dragging", "Use whole screen", TRUE
);
358 file_menu
->Append( wxID_EXIT
, "E&xit");
360 wxMenuBar
*menu_bar
= new wxMenuBar();
361 menu_bar
->Append(file_menu
, "&File");
363 SetMenuBar( menu_bar
);
366 int widths
[] = { -1, 100 };
367 SetStatusWidths( 2, widths
);
369 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
372 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
377 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
379 (void)wxMessageBox( "wxDragImage demo\n"
380 "Julian Smart (c) 2000",
381 "About wxDragImage Demo", wxICON_INFORMATION
| wxOK
);
384 //-----------------------------------------------------------------------------
386 //-----------------------------------------------------------------------------
388 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
389 EVT_MENU(TEST_USE_SCREEN
, MyApp::OnUseScreen
)
394 // Drag across whole screen
401 wxImage::AddHandler( new wxPNGHandler
);
405 if (image
.LoadFile("backgrnd.png", wxBITMAP_TYPE_PNG
))
407 m_background
= image
.ConvertToBitmap();
411 MyFrame
*frame
= new MyFrame();
413 wxString
rootName("shape0");
416 for (i
= 1; i
< 4; i
++)
419 filename
.Printf("%s%d.png", (const char*) rootName
, i
);
420 if (image
.LoadFile(filename
, wxBITMAP_TYPE_PNG
))
422 DragShape
* newShape
= new DragShape(image
.ConvertToBitmap());
423 newShape
->SetPosition(wxPoint(i
*50, i
*50));
426 newShape
->SetDragMethod(SHAPE_DRAG_TEXT
);
428 newShape
->SetDragMethod(SHAPE_DRAG_ICON
);
430 newShape
->SetDragMethod(SHAPE_DRAG_BITMAP
);
431 frame
->GetCanvas()->GetDisplayList().Append(newShape
);
436 // Under Motif or GTK, this demonstrates that
437 // wxScreenDC only gets the root window content.
438 // We need to be able to copy the overall content
439 // for full-screen dragging to work.
441 wxDisplaySize(& w
, & h
);
442 wxBitmap
bitmap(w
, h
);
446 memDC
.SelectObject(bitmap
);
447 memDC
.Blit(0, 0, w
, h
, & dc
, 0, 0);
448 memDC
.SelectObject(wxNullBitmap
);
449 m_background
= bitmap
;
457 bool MyApp::TileBitmap(const wxRect
& rect
, wxDC
& dc
, wxBitmap
& bitmap
)
459 int w
= bitmap
.GetWidth();
460 int h
= bitmap
.GetHeight();
463 for (i
= rect
.x
; i
< rect
.x
+ rect
.width
; i
+= w
)
465 for (j
= rect
.y
; j
< rect
.y
+ rect
.height
; j
+= h
)
466 dc
.DrawBitmap(bitmap
, i
, j
);
471 void MyApp::OnUseScreen(wxCommandEvent
& event
)
473 m_useScreen
= !m_useScreen
;
478 DragShape::DragShape(const wxBitmap
& bitmap
)
483 m_dragMethod
= SHAPE_DRAG_BITMAP
;
487 DragShape::~DragShape()
491 bool DragShape::HitTest(const wxPoint
& pt
) const
493 wxRect
rect(GetRect());
494 return rect
.Inside(pt
.x
, pt
.y
);
497 bool DragShape::Draw(wxDC
& dc
, int op
)
502 memDC
.SelectObject(m_bitmap
);
504 dc
.Blit(m_pos
.x
, m_pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight(),
505 & memDC
, 0, 0, op
, TRUE
);