]>
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 m_draggedShape
->SetPosition(m_draggedShape
->GetPosition()
135 + event
.GetPosition() - m_dragStartPos
);
138 m_dragImage
->EndDrag();
143 if (m_currentlyHighlighted
)
145 m_currentlyHighlighted
->Draw(dc
);
147 m_draggedShape
->SetShow(TRUE
);
148 m_draggedShape
->Draw(dc
);
150 m_currentlyHighlighted
= (DragShape
*) NULL
;
152 m_draggedShape
= (DragShape
*) NULL
;
154 else if (event
.Dragging() && m_dragMode
!= TEST_DRAG_NONE
)
156 if (m_dragMode
== TEST_DRAG_START
)
158 // We will start dragging if we've moved beyond a couple of pixels
161 int dx
= abs(event
.GetPosition().x
- m_dragStartPos
.x
);
162 int dy
= abs(event
.GetPosition().y
- m_dragStartPos
.y
);
163 if (dx
<= tolerance
&& dy
<= tolerance
)
167 m_dragMode
= TEST_DRAG_DRAGGING
;
172 // Erase the dragged shape from the canvas
173 m_draggedShape
->SetShow(FALSE
);
175 EraseShape(m_draggedShape
, dc
);
178 switch (m_draggedShape
->GetDragMethod())
180 case SHAPE_DRAG_BITMAP
:
182 m_dragImage
= new wxDragImage(m_draggedShape
->GetBitmap(), wxCursor(wxCURSOR_HAND
));
185 case SHAPE_DRAG_TEXT
:
187 m_dragImage
= new wxDragImage("Dragging some test text", wxCursor(wxCURSOR_HAND
));
190 case SHAPE_DRAG_ICON
:
192 // Can anyone explain why this test is necessary,
193 // to prevent a gcc error?
195 wxIcon
icon(dragicon_xpm
);
197 wxIcon
icon(wxICON(dragicon
));
200 m_dragImage
= new wxDragImage(icon
, wxCursor(wxCURSOR_HAND
));
205 bool fullScreen
= wxGetApp().GetUseScreen();
207 // The offset between the top-left of the shape image and the current shape position
208 wxPoint beginDragHotSpot
= m_dragStartPos
- m_draggedShape
->GetPosition();
210 // Now we do this inside the implementation: always assume
211 // coordinates relative to the capture window (client coordinates)
214 // beginDragHotSpot -= ClientToScreen(wxPoint(0, 0));
216 if (!m_dragImage
->BeginDrag(beginDragHotSpot
, this, fullScreen
))
219 m_dragImage
= (wxDragImage
*) NULL
;
220 m_dragMode
= TEST_DRAG_NONE
;
224 m_dragImage
->Move(event
.GetPosition());
228 else if (m_dragMode
== TEST_DRAG_DRAGGING
)
230 // We're currently dragging. See if we're over another shape.
231 DragShape
* onShape
= FindShape(event
.GetPosition());
233 bool mustUnhighlightOld
= FALSE
;
234 bool mustHighlightNew
= FALSE
;
236 if (m_currentlyHighlighted
)
238 if ((onShape
== (DragShape
*) NULL
) || (m_currentlyHighlighted
!= onShape
))
239 mustUnhighlightOld
= TRUE
;
242 if (onShape
&& (onShape
!= m_currentlyHighlighted
) && onShape
->IsShown())
243 mustHighlightNew
= TRUE
;
245 if (mustUnhighlightOld
|| mustHighlightNew
)
248 // Now with the drag image switched off, we can change the window contents.
250 if (mustUnhighlightOld
)
252 wxClientDC
clientDC(this);
253 m_currentlyHighlighted
->Draw(clientDC
);
254 m_currentlyHighlighted
= (DragShape
*) NULL
;
256 if (mustHighlightNew
)
258 wxClientDC
clientDC(this);
259 m_currentlyHighlighted
= onShape
;
260 m_currentlyHighlighted
->Draw(clientDC
, wxINVERT
);
263 // Move and show the image again
264 m_dragImage
->Move(event
.GetPosition());
266 if (mustUnhighlightOld
|| mustHighlightNew
)
272 void MyCanvas::DrawShapes(wxDC
& dc
)
274 wxNode
* node
= m_displayList
.First();
277 DragShape
* shape
= (DragShape
*) node
->Data();
278 if (shape
->IsShown())
284 void MyCanvas::EraseShape(DragShape
* shape
, wxDC
& dc
)
286 wxSize sz
= GetClientSize();
287 wxRect
rect(0, 0, sz
.x
, sz
.y
);
289 wxRect
rect2(shape
->GetRect());
290 dc
.SetClippingRegion(rect2
.x
, rect2
.y
, rect2
.width
, rect2
.height
);
292 wxGetApp().TileBitmap(rect
, dc
, wxGetApp().GetBackgroundBitmap());
294 dc
.DestroyClippingRegion();
297 void MyCanvas::ClearShapes()
299 wxNode
* node
= m_displayList
.First();
302 DragShape
* shape
= (DragShape
*) node
->Data();
306 m_displayList
.Clear();
309 DragShape
* MyCanvas::FindShape(const wxPoint
& pt
) const
311 wxNode
* node
= m_displayList
.First();
314 DragShape
* shape
= (DragShape
*) node
->Data();
315 if (shape
->HitTest(pt
))
319 return (DragShape
*) NULL
;
324 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
326 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
327 EVT_MENU (wxID_ABOUT
, MyFrame::OnAbout
)
328 EVT_MENU (wxID_EXIT
, MyFrame::OnQuit
)
332 : wxFrame( (wxFrame
*)NULL
, -1, "wxDragImage sample",
333 wxPoint(20,20), wxSize(470,360) )
335 wxMenu
*file_menu
= new wxMenu();
336 file_menu
->Append( wxID_ABOUT
, "&About...");
337 file_menu
->Append( TEST_USE_SCREEN
, "&Use whole screen for dragging", "Use whole screen", TRUE
);
338 file_menu
->Append( wxID_EXIT
, "E&xit");
340 wxMenuBar
*menu_bar
= new wxMenuBar();
341 menu_bar
->Append(file_menu
, "&File");
343 SetMenuBar( menu_bar
);
346 int widths
[] = { -1, 100 };
347 SetStatusWidths( 2, widths
);
349 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
352 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
357 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
359 (void)wxMessageBox( "wxDragImage demo\n"
360 "Julian Smart (c) 2000",
361 "About wxDragImage Demo", 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("backgrnd.png", wxBITMAP_TYPE_PNG
))
387 m_background
= image
.ConvertToBitmap();
391 MyFrame
*frame
= new MyFrame();
393 wxString
rootName("shape0");
396 for (i
= 1; i
< 4; i
++)
399 filename
.Printf("%s%d.png", (const char*) rootName
, i
);
400 if (image
.LoadFile(filename
, wxBITMAP_TYPE_PNG
))
402 DragShape
* newShape
= new DragShape(image
.ConvertToBitmap());
403 newShape
->SetPosition(wxPoint(i
*50, i
*50));
406 newShape
->SetDragMethod(SHAPE_DRAG_TEXT
);
408 newShape
->SetDragMethod(SHAPE_DRAG_ICON
);
410 newShape
->SetDragMethod(SHAPE_DRAG_BITMAP
);
411 frame
->GetCanvas()->GetDisplayList().Append(newShape
);
416 // Under Motif or GTK, this demonstrates that
417 // wxScreenDC only gets the root window content.
418 // We need to be able to copy the overall content
419 // for full-screen dragging to work.
421 wxDisplaySize(& w
, & h
);
422 wxBitmap
bitmap(w
, h
);
426 memDC
.SelectObject(bitmap
);
427 memDC
.Blit(0, 0, w
, h
, & dc
, 0, 0);
428 memDC
.SelectObject(wxNullBitmap
);
429 m_background
= bitmap
;
437 bool MyApp::TileBitmap(const wxRect
& rect
, wxDC
& dc
, wxBitmap
& bitmap
)
439 int w
= bitmap
.GetWidth();
440 int h
= bitmap
.GetHeight();
443 for (i
= rect
.x
; i
< rect
.x
+ rect
.width
; i
+= w
)
445 for (j
= rect
.y
; j
< rect
.y
+ rect
.height
; j
+= h
)
446 dc
.DrawBitmap(bitmap
, i
, j
);
451 void MyApp::OnUseScreen(wxCommandEvent
& event
)
453 m_useScreen
= !m_useScreen
;
458 DragShape::DragShape(const wxBitmap
& bitmap
)
463 m_dragMethod
= SHAPE_DRAG_BITMAP
;
467 DragShape::~DragShape()
471 bool DragShape::HitTest(const wxPoint
& pt
) const
473 wxRect
rect(GetRect());
474 return rect
.Inside(pt
.x
, pt
.y
);
477 bool DragShape::Draw(wxDC
& dc
, int op
)
482 memDC
.SelectObject(m_bitmap
);
484 dc
.Blit(m_pos
.x
, m_pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight(),
485 & memDC
, 0, 0, op
, TRUE
);