]>
git.saurik.com Git - wxWidgets.git/blob - samples/dragimag/dragimag.cpp
43a115629fd72d85f28b25ec5c73bc4fe4576c42
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());
104 #if wxUSE_DC_CACHEING
109 event
.Skip(); // The official way of doing it
112 void MyCanvas::OnMouseEvent(wxMouseEvent
& event
)
114 if (event
.LeftDown())
116 DragShape
* shape
= FindShape(event
.GetPosition());
119 // We tentatively start dragging, but wait for
120 // mouse movement before dragging properly.
122 m_dragMode
= TEST_DRAG_START
;
123 m_dragStartPos
= event
.GetPosition();
124 m_draggedShape
= shape
;
127 else if (event
.LeftUp() && m_dragMode
!= TEST_DRAG_NONE
)
131 m_dragMode
= TEST_DRAG_NONE
;
133 if (!m_draggedShape
|| !m_dragImage
)
136 m_draggedShape
->SetPosition(m_draggedShape
->GetPosition()
137 + event
.GetPosition() - m_dragStartPos
);
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
)
169 m_dragMode
= TEST_DRAG_DRAGGING
;
174 // Erase the dragged shape from the canvas
175 m_draggedShape
->SetShow(FALSE
);
177 EraseShape(m_draggedShape
, dc
);
180 switch (m_draggedShape
->GetDragMethod())
182 case SHAPE_DRAG_BITMAP
:
184 m_dragImage
= new wxDragImage(m_draggedShape
->GetBitmap(), wxCursor(wxCURSOR_HAND
));
187 case SHAPE_DRAG_TEXT
:
189 m_dragImage
= new wxDragImage("Dragging some test text", wxCursor(wxCURSOR_HAND
));
192 case SHAPE_DRAG_ICON
:
194 // Can anyone explain why this test is necessary,
195 // to prevent a gcc error?
197 wxIcon
icon(dragicon_xpm
);
199 wxIcon
icon(wxICON(dragicon
));
202 m_dragImage
= new wxDragImage(icon
, wxCursor(wxCURSOR_HAND
));
207 bool fullScreen
= wxGetApp().GetUseScreen();
209 // The offset between the top-left of the shape image and the current shape position
210 wxPoint beginDragHotSpot
= m_dragStartPos
- m_draggedShape
->GetPosition();
212 // Now we do this inside the implementation: always assume
213 // coordinates relative to the capture window (client coordinates)
216 // beginDragHotSpot -= ClientToScreen(wxPoint(0, 0));
218 if (!m_dragImage
->BeginDrag(beginDragHotSpot
, this, fullScreen
))
221 m_dragImage
= (wxDragImage
*) NULL
;
222 m_dragMode
= TEST_DRAG_NONE
;
226 m_dragImage
->Move(event
.GetPosition());
230 else if (m_dragMode
== TEST_DRAG_DRAGGING
)
232 // We're currently dragging. See if we're over another shape.
233 DragShape
* onShape
= FindShape(event
.GetPosition());
235 bool mustUnhighlightOld
= FALSE
;
236 bool mustHighlightNew
= FALSE
;
238 if (m_currentlyHighlighted
)
240 if ((onShape
== (DragShape
*) NULL
) || (m_currentlyHighlighted
!= onShape
))
241 mustUnhighlightOld
= TRUE
;
244 if (onShape
&& (onShape
!= m_currentlyHighlighted
) && onShape
->IsShown())
245 mustHighlightNew
= TRUE
;
247 if (mustUnhighlightOld
|| mustHighlightNew
)
250 // Now with the drag image switched off, we can change the window contents.
252 if (mustUnhighlightOld
)
254 wxClientDC
clientDC(this);
255 m_currentlyHighlighted
->Draw(clientDC
);
256 m_currentlyHighlighted
= (DragShape
*) NULL
;
258 if (mustHighlightNew
)
260 wxClientDC
clientDC(this);
261 m_currentlyHighlighted
= onShape
;
262 m_currentlyHighlighted
->Draw(clientDC
, wxINVERT
);
265 // Move and show the image again
266 m_dragImage
->Move(event
.GetPosition());
268 if (mustUnhighlightOld
|| mustHighlightNew
)
274 void MyCanvas::DrawShapes(wxDC
& dc
)
276 wxNode
* node
= m_displayList
.First();
279 DragShape
* shape
= (DragShape
*) node
->Data();
280 if (shape
->IsShown())
284 #if wxUSE_DC_CACHEING
289 void MyCanvas::EraseShape(DragShape
* shape
, wxDC
& dc
)
291 wxSize sz
= GetClientSize();
292 wxRect
rect(0, 0, sz
.x
, sz
.y
);
294 wxRect
rect2(shape
->GetRect());
295 dc
.SetClippingRegion(rect2
.x
, rect2
.y
, rect2
.width
, rect2
.height
);
297 wxGetApp().TileBitmap(rect
, dc
, wxGetApp().GetBackgroundBitmap());
299 dc
.DestroyClippingRegion();
302 void MyCanvas::ClearShapes()
304 wxNode
* node
= m_displayList
.First();
307 DragShape
* shape
= (DragShape
*) node
->Data();
311 m_displayList
.Clear();
314 DragShape
* MyCanvas::FindShape(const wxPoint
& pt
) const
316 wxNode
* node
= m_displayList
.First();
319 DragShape
* shape
= (DragShape
*) node
->Data();
320 if (shape
->HitTest(pt
))
324 return (DragShape
*) NULL
;
328 IMPLEMENT_DYNAMIC_CLASS( MyFrame
, wxFrame
)
330 BEGIN_EVENT_TABLE(MyFrame
,wxFrame
)
331 EVT_MENU (wxID_ABOUT
, MyFrame::OnAbout
)
332 EVT_MENU (wxID_EXIT
, MyFrame::OnQuit
)
336 : wxFrame( (wxFrame
*)NULL
, -1, "wxDragImage sample",
337 wxPoint(20,20), wxSize(470,360) )
339 wxMenu
*file_menu
= new wxMenu();
340 file_menu
->Append( wxID_ABOUT
, "&About...");
341 file_menu
->Append( TEST_USE_SCREEN
, "&Use whole screen for dragging", "Use whole screen", TRUE
);
342 file_menu
->Append( wxID_EXIT
, "E&xit");
344 wxMenuBar
*menu_bar
= new wxMenuBar();
345 menu_bar
->Append(file_menu
, "&File");
347 SetMenuBar( menu_bar
);
350 int widths
[] = { -1, 100 };
351 SetStatusWidths( 2, widths
);
353 m_canvas
= new MyCanvas( this, -1, wxPoint(0,0), wxSize(10,10) );
356 void MyFrame::OnQuit( wxCommandEvent
&WXUNUSED(event
) )
361 void MyFrame::OnAbout( wxCommandEvent
&WXUNUSED(event
) )
363 (void)wxMessageBox( "wxDragImage demo\n"
364 "Julian Smart (c) 2000",
365 "About wxDragImage Demo", wxICON_INFORMATION
| wxOK
);
368 //-----------------------------------------------------------------------------
370 //-----------------------------------------------------------------------------
372 BEGIN_EVENT_TABLE(MyApp
, wxApp
)
373 EVT_MENU(TEST_USE_SCREEN
, MyApp::OnUseScreen
)
378 // Drag across whole screen
385 wxImage::AddHandler( new wxPNGHandler
);
388 // The DC cache is an efficiency measure to be used
389 // when a lot of masked blitting is done
390 #if wxUSE_DC_CACHEING
391 wxDC::EnableCache(TRUE
);
395 if (image
.LoadFile("backgrnd.png", wxBITMAP_TYPE_PNG
))
397 m_background
= image
.ConvertToBitmap();
400 MyFrame
*frame
= new MyFrame();
402 wxString
rootName("shape0");
405 for (i
= 1; i
< 4; i
++)
408 filename
.Printf("%s%d.png", (const char*) rootName
, i
);
409 if (image
.LoadFile(filename
, wxBITMAP_TYPE_PNG
))
411 DragShape
* newShape
= new DragShape(image
.ConvertToBitmap());
412 newShape
->SetPosition(wxPoint(i
*50, i
*50));
415 newShape
->SetDragMethod(SHAPE_DRAG_TEXT
);
417 newShape
->SetDragMethod(SHAPE_DRAG_ICON
);
419 newShape
->SetDragMethod(SHAPE_DRAG_BITMAP
);
420 frame
->GetCanvas()->GetDisplayList().Append(newShape
);
425 // Under Motif or GTK, this demonstrates that
426 // wxScreenDC only gets the root window content.
427 // We need to be able to copy the overall content
428 // for full-screen dragging to work.
430 wxDisplaySize(& w
, & h
);
431 wxBitmap
bitmap(w
, h
);
435 memDC
.SelectObject(bitmap
);
436 memDC
.Blit(0, 0, w
, h
, & dc
, 0, 0);
437 memDC
.SelectObject(wxNullBitmap
);
438 m_background
= bitmap
;
448 #if wxUSE_DC_CACHEING
454 bool MyApp::TileBitmap(const wxRect
& rect
, wxDC
& dc
, wxBitmap
& bitmap
)
456 int w
= bitmap
.GetWidth();
457 int h
= bitmap
.GetHeight();
460 for (i
= rect
.x
; i
< rect
.x
+ rect
.width
; i
+= w
)
462 for (j
= rect
.y
; j
< rect
.y
+ rect
.height
; j
+= h
)
463 dc
.DrawBitmap(bitmap
, i
, j
);
468 void MyApp::OnUseScreen(wxCommandEvent
& event
)
470 m_useScreen
= !m_useScreen
;
475 DragShape::DragShape(const wxBitmap
& bitmap
)
480 m_dragMethod
= SHAPE_DRAG_BITMAP
;
484 DragShape::~DragShape()
488 bool DragShape::HitTest(const wxPoint
& pt
) const
490 wxRect
rect(GetRect());
491 return rect
.Inside(pt
.x
, pt
.y
);
494 bool DragShape::Draw(wxDC
& dc
, int op
)
499 memDC
.SelectObject(m_bitmap
);
501 dc
.Blit(m_pos
.x
, m_pos
.y
, m_bitmap
.GetWidth(), m_bitmap
.GetHeight(),
502 & memDC
, 0, 0, op
, TRUE
);