1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Drag and drop sample
4 // Author: Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
26 #include "wx/dirdlg.h"
27 #include "wx/filedlg.h"
29 #include "wx/clipbrd.h"
30 #include "wx/colordlg.h"
32 #include "wx/dataobj.h"
35 #include "wx/metafile.h"
36 #endif // wxUSE_METAFILES
38 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
39 #include "mondrian.xpm"
40 #if wxUSE_DRAG_AND_DROP
41 #include "dnd_copy.xpm"
42 #include "dnd_move.xpm"
43 #include "dnd_none.xpm"
47 #if wxUSE_DRAG_AND_DROP
49 // ----------------------------------------------------------------------------
50 // Derive two simple classes which just put in the listbox the strings (text or
51 // file names) we drop on them
52 // ----------------------------------------------------------------------------
54 class DnDText
: public wxTextDropTarget
57 DnDText(wxListBox
*pOwner
) { m_pOwner
= pOwner
; }
59 virtual bool OnDropText(wxCoord x
, wxCoord y
, const wxString
& text
);
65 class DnDFile
: public wxFileDropTarget
68 DnDFile(wxListBox
*pOwner
) { m_pOwner
= pOwner
; }
70 virtual bool OnDropFiles(wxCoord x
, wxCoord y
,
71 const wxArrayString
& filenames
);
77 // ----------------------------------------------------------------------------
78 // Define a custom dtop target accepting URLs
79 // ----------------------------------------------------------------------------
81 class URLDropTarget
: public wxDropTarget
84 URLDropTarget() { SetDataObject(new wxURLDataObject
); }
86 void OnDropURL(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), const wxString
& text
)
88 // of course, a real program would do something more useful here...
89 wxMessageBox(text
, _T("wxDnD sample: got URL"),
90 wxICON_INFORMATION
| wxOK
);
93 // URLs can't be moved, only copied
94 virtual wxDragResult
OnDragOver(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
95 wxDragResult
WXUNUSED(def
))
97 return wxDragLink
; // At least IE 5.x needs wxDragLink, the
98 // other browsers on MSW seem okay with it too.
101 // translate this to calls to OnDropURL() just for convenience
102 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
107 OnDropURL(x
, y
, ((wxURLDataObject
*)m_dataObject
)->GetURL());
113 #endif // wxUSE_DRAG_AND_DROP
115 // ----------------------------------------------------------------------------
116 // Define a new application type
117 // ----------------------------------------------------------------------------
119 class DnDApp
: public wxApp
122 virtual bool OnInit();
125 IMPLEMENT_APP(DnDApp
);
127 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
129 // ----------------------------------------------------------------------------
130 // Define canvas class to show a bitmap
131 // ----------------------------------------------------------------------------
133 class DnDCanvasBitmap
: public wxScrolledWindow
136 DnDCanvasBitmap(wxWindow
*parent
) : wxScrolledWindow(parent
) { }
138 void SetBitmap(const wxBitmap
& bitmap
)
142 SetScrollbars(10, 10,
143 m_bitmap
.GetWidth() / 10, m_bitmap
.GetHeight() / 10);
148 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
156 dc
.DrawBitmap(m_bitmap
, 0, 0);
163 DECLARE_EVENT_TABLE()
168 // and the same thing fo metafiles
169 class DnDCanvasMetafile
: public wxScrolledWindow
172 DnDCanvasMetafile(wxWindow
*parent
) : wxScrolledWindow(parent
) { }
174 void SetMetafile(const wxMetafile
& metafile
)
176 m_metafile
= metafile
;
178 SetScrollbars(10, 10,
179 m_metafile
.GetWidth() / 10, m_metafile
.GetHeight() / 10);
184 void OnPaint(wxPaintEvent
& event
)
188 if ( m_metafile
.Ok() )
192 m_metafile
.Play(&dc
);
197 wxMetafile m_metafile
;
199 DECLARE_EVENT_TABLE()
202 #endif // wxUSE_METAFILES
204 // ----------------------------------------------------------------------------
205 // Define a new frame type for the main frame
206 // ----------------------------------------------------------------------------
208 class DnDFrame
: public wxFrame
211 DnDFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
214 void OnPaint(wxPaintEvent
& event
);
215 void OnSize(wxSizeEvent
& event
);
216 void OnQuit(wxCommandEvent
& event
);
217 void OnAbout(wxCommandEvent
& event
);
218 void OnDrag(wxCommandEvent
& event
);
219 void OnDragMoveByDefault(wxCommandEvent
& event
);
220 void OnDragMoveAllow(wxCommandEvent
& event
);
221 void OnNewFrame(wxCommandEvent
& event
);
222 void OnHelp (wxCommandEvent
& event
);
223 void OnLogClear(wxCommandEvent
& event
);
225 void OnCopy(wxCommandEvent
& event
);
226 void OnPaste(wxCommandEvent
& event
);
228 void OnCopyBitmap(wxCommandEvent
& event
);
229 void OnPasteBitmap(wxCommandEvent
& event
);
232 void OnPasteMetafile(wxCommandEvent
& event
);
233 #endif // wxUSE_METAFILES
235 void OnCopyFiles(wxCommandEvent
& event
);
237 void OnLeftDown(wxMouseEvent
& event
);
238 void OnRightDown(wxMouseEvent
& event
);
240 void OnUpdateUIMoveByDefault(wxUpdateUIEvent
& event
);
242 void OnUpdateUIPasteText(wxUpdateUIEvent
& event
);
243 void OnUpdateUIPasteBitmap(wxUpdateUIEvent
& event
);
245 DECLARE_EVENT_TABLE()
249 wxListBox
*m_ctrlFile
,
251 wxTextCtrl
*m_ctrlLog
;
256 // move the text by default (or copy)?
257 bool m_moveByDefault
;
259 // allow moving the text at all?
266 // ----------------------------------------------------------------------------
267 // A shape is an example of application-specific data which may be transported
268 // via drag-and-drop or clipboard: in our case, we have different geometric
269 // shapes, each one with its own colour and position
270 // ----------------------------------------------------------------------------
272 #if wxUSE_DRAG_AND_DROP
285 DnDShape(const wxPoint
& pos
,
288 : m_pos(pos
), m_size(size
), m_col(col
)
292 // this is for debugging - lets us see when exactly an object is freed
293 // (this may be later than you think if it's on the clipboard, for example)
294 virtual ~DnDShape() { }
296 // the functions used for drag-and-drop: they dump and restore a shape into
297 // some bitwise-copiable data (might use streams too...)
298 // ------------------------------------------------------------------------
300 // restore from buffer
301 static DnDShape
*New(const void *buf
);
303 virtual size_t GetDataSize() const
305 return sizeof(ShapeDump
);
308 virtual void GetDataHere(void *buf
) const
310 ShapeDump
& dump
= *(ShapeDump
*)buf
;
315 dump
.r
= m_col
.Red();
316 dump
.g
= m_col
.Green();
317 dump
.b
= m_col
.Blue();
322 const wxPoint
& GetPosition() const { return m_pos
; }
323 const wxColour
& GetColour() const { return m_col
; }
324 const wxSize
& GetSize() const { return m_size
; }
326 void Move(const wxPoint
& pos
) { m_pos
= pos
; }
328 // to implement in derived classes
329 virtual Kind
GetKind() const = 0;
331 virtual void Draw(wxDC
& dc
)
333 dc
.SetPen(wxPen(m_col
, 1, wxSOLID
));
337 //get a point 1 up and 1 left, otherwise the mid-point of a triangle is on the line
338 wxPoint
GetCentre() const
339 { return wxPoint(m_pos
.x
+ m_size
.x
/ 2 - 1, m_pos
.y
+ m_size
.y
/ 2 - 1); }
343 int x
, y
, // position
354 class DnDTriangularShape
: public DnDShape
357 DnDTriangularShape(const wxPoint
& pos
,
360 : DnDShape(pos
, size
, col
)
362 wxLogMessage(wxT("DnDTriangularShape is being created"));
365 virtual ~DnDTriangularShape()
367 wxLogMessage(wxT("DnDTriangularShape is being deleted"));
370 virtual Kind
GetKind() const { return Triangle
; }
371 virtual void Draw(wxDC
& dc
)
375 // well, it's a bit difficult to describe a triangle by position and
376 // size, but we're not doing geometry here, do we? ;-)
378 wxPoint
p2(m_pos
.x
+ m_size
.x
, m_pos
.y
);
379 wxPoint
p3(m_pos
.x
, m_pos
.y
+ m_size
.y
);
385 //works in multicolor modes; on GTK (at least) will fail in 16-bit color
386 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
387 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
391 class DnDRectangularShape
: public DnDShape
394 DnDRectangularShape(const wxPoint
& pos
,
397 : DnDShape(pos
, size
, col
)
399 wxLogMessage(wxT("DnDRectangularShape is being created"));
402 virtual ~DnDRectangularShape()
404 wxLogMessage(wxT("DnDRectangularShape is being deleted"));
407 virtual Kind
GetKind() const { return Rectangle
; }
408 virtual void Draw(wxDC
& dc
)
413 wxPoint
p2(p1
.x
+ m_size
.x
, p1
.y
);
414 wxPoint
p3(p2
.x
, p2
.y
+ m_size
.y
);
415 wxPoint
p4(p1
.x
, p3
.y
);
422 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
423 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
427 class DnDEllipticShape
: public DnDShape
430 DnDEllipticShape(const wxPoint
& pos
,
433 : DnDShape(pos
, size
, col
)
435 wxLogMessage(wxT("DnDEllipticShape is being created"));
438 virtual ~DnDEllipticShape()
440 wxLogMessage(wxT("DnDEllipticShape is being deleted"));
443 virtual Kind
GetKind() const { return Ellipse
; }
444 virtual void Draw(wxDC
& dc
)
448 dc
.DrawEllipse(m_pos
, m_size
);
450 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
451 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
455 // ----------------------------------------------------------------------------
456 // A wxDataObject specialisation for the application-specific data
457 // ----------------------------------------------------------------------------
459 static const wxChar
*shapeFormatId
= wxT("wxShape");
461 class DnDShapeDataObject
: public wxDataObject
464 // ctor doesn't copy the pointer, so it shouldn't go away while this object
466 DnDShapeDataObject(DnDShape
*shape
= (DnDShape
*)NULL
)
470 // we need to copy the shape because the one we're handled may be
471 // deleted while it's still on the clipboard (for example) - and we
472 // reuse the serialisation methods here to copy it
473 void *buf
= malloc(shape
->DnDShape::GetDataSize());
474 shape
->GetDataHere(buf
);
475 m_shape
= DnDShape::New(buf
);
485 // this string should uniquely identify our format, but is otherwise
487 m_formatShape
.SetId(shapeFormatId
);
489 // we don't draw the shape to a bitmap until it's really needed (i.e.
490 // we're asked to do so)
493 m_hasMetaFile
= false;
494 #endif // wxUSE_METAFILES
497 virtual ~DnDShapeDataObject() { delete m_shape
; }
499 // after a call to this function, the shape is owned by the caller and it
500 // is responsible for deleting it!
502 // NB: a better solution would be to make DnDShapes ref counted and this
503 // is what should probably be done in a real life program, otherwise
504 // the ownership problems become too complicated really fast
507 DnDShape
*shape
= m_shape
;
509 m_shape
= (DnDShape
*)NULL
;
512 m_hasMetaFile
= false;
513 #endif // wxUSE_METAFILES
518 // implement base class pure virtuals
519 // ----------------------------------
521 virtual wxDataFormat
GetPreferredFormat(Direction
WXUNUSED(dir
)) const
523 return m_formatShape
;
526 virtual size_t GetFormatCount(Direction dir
) const
528 // our custom format is supported by both GetData() and SetData()
532 // but the bitmap format(s) are only supported for output
533 nFormats
+= m_dobjBitmap
.GetFormatCount(dir
);
536 nFormats
+= m_dobjMetaFile
.GetFormatCount(dir
);
537 #endif // wxUSE_METAFILES
543 virtual void GetAllFormats(wxDataFormat
*formats
, Direction dir
) const
545 formats
[0] = m_formatShape
;
548 // in Get direction we additionally support bitmaps and metafiles
550 m_dobjBitmap
.GetAllFormats(&formats
[1], dir
);
553 // don't assume that m_dobjBitmap has only 1 format
554 m_dobjMetaFile
.GetAllFormats(&formats
[1 +
555 m_dobjBitmap
.GetFormatCount(dir
)], dir
);
556 #endif // wxUSE_METAFILES
560 virtual size_t GetDataSize(const wxDataFormat
& format
) const
562 if ( format
== m_formatShape
)
564 return m_shape
->GetDataSize();
567 else if ( m_dobjMetaFile
.IsSupported(format
) )
569 if ( !m_hasMetaFile
)
572 return m_dobjMetaFile
.GetDataSize(format
);
574 #endif // wxUSE_METAFILES
577 wxASSERT_MSG( m_dobjBitmap
.IsSupported(format
),
578 wxT("unexpected format") );
583 return m_dobjBitmap
.GetDataSize();
587 virtual bool GetDataHere(const wxDataFormat
& format
, void *pBuf
) const
589 if ( format
== m_formatShape
)
591 m_shape
->GetDataHere(pBuf
);
596 else if ( m_dobjMetaFile
.IsSupported(format
) )
598 if ( !m_hasMetaFile
)
601 return m_dobjMetaFile
.GetDataHere(format
, pBuf
);
603 #endif // wxUSE_METAFILES
606 wxASSERT_MSG( m_dobjBitmap
.IsSupported(format
),
607 wxT("unexpected format") );
612 return m_dobjBitmap
.GetDataHere(pBuf
);
616 virtual bool SetData(const wxDataFormat
& format
,
617 size_t WXUNUSED(len
), const void *buf
)
619 wxCHECK_MSG( format
== m_formatShape
, false,
620 wxT( "unsupported format") );
623 m_shape
= DnDShape::New(buf
);
625 // the shape has changed
629 m_hasMetaFile
= false;
630 #endif // wxUSE_METAFILES
636 // creates a bitmap and assigns it to m_dobjBitmap (also sets m_hasBitmap)
637 void CreateBitmap() const;
639 void CreateMetaFile() const;
640 #endif // wxUSE_METAFILES
642 wxDataFormat m_formatShape
; // our custom format
644 wxBitmapDataObject m_dobjBitmap
; // it handles bitmaps
645 bool m_hasBitmap
; // true if m_dobjBitmap has valid bitmap
648 wxMetaFileDataObject m_dobjMetaFile
;// handles metafiles
649 bool m_hasMetaFile
; // true if we have valid metafile
650 #endif // wxUSE_METAFILES
652 DnDShape
*m_shape
; // our data
655 // ----------------------------------------------------------------------------
656 // A dialog to edit shape properties
657 // ----------------------------------------------------------------------------
659 class DnDShapeDialog
: public wxDialog
662 DnDShapeDialog(wxFrame
*parent
, DnDShape
*shape
);
664 DnDShape
*GetShape() const;
666 virtual bool TransferDataToWindow();
667 virtual bool TransferDataFromWindow();
669 void OnColour(wxCommandEvent
& event
);
676 DnDShape::Kind m_shapeKind
;
688 DECLARE_EVENT_TABLE()
691 // ----------------------------------------------------------------------------
692 // A frame for the shapes which can be drag-and-dropped between frames
693 // ----------------------------------------------------------------------------
695 class DnDShapeFrame
: public wxFrame
698 DnDShapeFrame(wxFrame
*parent
);
701 void SetShape(DnDShape
*shape
);
702 virtual bool SetShape(const wxRegion
®ion
)
704 return wxFrame::SetShape( region
);
708 void OnNewShape(wxCommandEvent
& event
);
709 void OnEditShape(wxCommandEvent
& event
);
710 void OnClearShape(wxCommandEvent
& event
);
712 void OnCopyShape(wxCommandEvent
& event
);
713 void OnPasteShape(wxCommandEvent
& event
);
715 void OnUpdateUICopy(wxUpdateUIEvent
& event
);
716 void OnUpdateUIPaste(wxUpdateUIEvent
& event
);
718 void OnDrag(wxMouseEvent
& event
);
719 void OnPaint(wxPaintEvent
& event
);
720 void OnDrop(wxCoord x
, wxCoord y
, DnDShape
*shape
);
725 static DnDShapeFrame
*ms_lastDropTarget
;
727 DECLARE_EVENT_TABLE()
730 // ----------------------------------------------------------------------------
731 // wxDropTarget derivation for DnDShapes
732 // ----------------------------------------------------------------------------
734 class DnDShapeDropTarget
: public wxDropTarget
737 DnDShapeDropTarget(DnDShapeFrame
*frame
)
738 : wxDropTarget(new DnDShapeDataObject
)
743 // override base class (pure) virtuals
744 virtual wxDragResult
OnEnter(wxCoord x
, wxCoord y
, wxDragResult def
)
745 { m_frame
->SetStatusText(_T("Mouse entered the frame")); return OnDragOver(x
, y
, def
); }
746 virtual void OnLeave()
747 { m_frame
->SetStatusText(_T("Mouse left the frame")); }
748 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
752 wxLogError(wxT("Failed to get drag and drop data"));
757 m_frame
->OnDrop(x
, y
,
758 ((DnDShapeDataObject
*)GetDataObject())->GetShape());
764 DnDShapeFrame
*m_frame
;
767 #endif // wxUSE_DRAG_AND_DROP
769 // ----------------------------------------------------------------------------
770 // functions prototypes
771 // ----------------------------------------------------------------------------
773 static void ShowBitmap(const wxBitmap
& bitmap
);
776 static void ShowMetaFile(const wxMetaFile
& metafile
);
777 #endif // wxUSE_METAFILES
779 // ----------------------------------------------------------------------------
780 // IDs for the menu commands
781 // ----------------------------------------------------------------------------
799 Menu_Shape_New
= 500,
802 Menu_ShapeClipboard_Copy
,
803 Menu_ShapeClipboard_Paste
,
807 BEGIN_EVENT_TABLE(DnDFrame
, wxFrame
)
808 EVT_MENU(Menu_Quit
, DnDFrame::OnQuit
)
809 EVT_MENU(Menu_About
, DnDFrame::OnAbout
)
810 EVT_MENU(Menu_Drag
, DnDFrame::OnDrag
)
811 EVT_MENU(Menu_DragMoveDef
, DnDFrame::OnDragMoveByDefault
)
812 EVT_MENU(Menu_DragMoveAllow
,DnDFrame::OnDragMoveAllow
)
813 EVT_MENU(Menu_NewFrame
, DnDFrame::OnNewFrame
)
814 EVT_MENU(Menu_Help
, DnDFrame::OnHelp
)
815 EVT_MENU(Menu_Clear
, DnDFrame::OnLogClear
)
816 EVT_MENU(Menu_Copy
, DnDFrame::OnCopy
)
817 EVT_MENU(Menu_Paste
, DnDFrame::OnPaste
)
818 EVT_MENU(Menu_CopyBitmap
, DnDFrame::OnCopyBitmap
)
819 EVT_MENU(Menu_PasteBitmap
,DnDFrame::OnPasteBitmap
)
821 EVT_MENU(Menu_PasteMFile
, DnDFrame::OnPasteMetafile
)
822 #endif // wxUSE_METAFILES
823 EVT_MENU(Menu_CopyFiles
, DnDFrame::OnCopyFiles
)
825 EVT_UPDATE_UI(Menu_DragMoveDef
, DnDFrame::OnUpdateUIMoveByDefault
)
827 EVT_UPDATE_UI(Menu_Paste
, DnDFrame::OnUpdateUIPasteText
)
828 EVT_UPDATE_UI(Menu_PasteBitmap
, DnDFrame::OnUpdateUIPasteBitmap
)
830 EVT_LEFT_DOWN( DnDFrame::OnLeftDown
)
831 EVT_RIGHT_DOWN( DnDFrame::OnRightDown
)
832 EVT_PAINT( DnDFrame::OnPaint
)
833 EVT_SIZE( DnDFrame::OnSize
)
836 #if wxUSE_DRAG_AND_DROP
838 BEGIN_EVENT_TABLE(DnDShapeFrame
, wxFrame
)
839 EVT_MENU(Menu_Shape_New
, DnDShapeFrame::OnNewShape
)
840 EVT_MENU(Menu_Shape_Edit
, DnDShapeFrame::OnEditShape
)
841 EVT_MENU(Menu_Shape_Clear
, DnDShapeFrame::OnClearShape
)
843 EVT_MENU(Menu_ShapeClipboard_Copy
, DnDShapeFrame::OnCopyShape
)
844 EVT_MENU(Menu_ShapeClipboard_Paste
, DnDShapeFrame::OnPasteShape
)
846 EVT_UPDATE_UI(Menu_ShapeClipboard_Copy
, DnDShapeFrame::OnUpdateUICopy
)
847 EVT_UPDATE_UI(Menu_ShapeClipboard_Paste
, DnDShapeFrame::OnUpdateUIPaste
)
849 EVT_LEFT_DOWN(DnDShapeFrame::OnDrag
)
851 EVT_PAINT(DnDShapeFrame::OnPaint
)
854 BEGIN_EVENT_TABLE(DnDShapeDialog
, wxDialog
)
855 EVT_BUTTON(Button_Colour
, DnDShapeDialog::OnColour
)
858 #endif // wxUSE_DRAG_AND_DROP
860 BEGIN_EVENT_TABLE(DnDCanvasBitmap
, wxScrolledWindow
)
861 EVT_PAINT(DnDCanvasBitmap::OnPaint
)
865 BEGIN_EVENT_TABLE(DnDCanvasMetafile
, wxScrolledWindow
)
866 EVT_PAINT(DnDCanvasMetafile::OnPaint
)
868 #endif // wxUSE_METAFILES
870 #endif // wxUSE_DRAG_AND_DROP
872 // ============================================================================
874 // ============================================================================
876 // `Main program' equivalent, creating windows and returning main app frame
877 bool DnDApp::OnInit()
879 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
880 // switch on trace messages
881 #if defined(__WXGTK__)
882 wxLog::AddTraceMask(_T("clipboard"));
883 #elif defined(__WXMSW__)
884 wxLog::AddTraceMask(wxTRACE_OleCalls
);
888 wxImage::AddHandler( new wxPNGHandler
);
891 // under X we usually want to use the primary selection by default (which
892 // is shared with other apps)
893 wxTheClipboard
->UsePrimarySelection();
895 // create the main frame window
896 DnDFrame
*frame
= new DnDFrame((wxFrame
*) NULL
,
897 _T("Drag-and-Drop/Clipboard wxWidgets Sample"),
907 wxMessageBox( _T("This sample has to be compiled with wxUSE_DRAG_AND_DROP"), _T("Building error"), wxOK
);
909 #endif // wxUSE_DRAG_AND_DROP
912 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
914 DnDFrame::DnDFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
)
915 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
)),
916 m_strText(_T("wxWidgets drag & drop works :-)"))
919 // frame icon and status bar
920 SetIcon(wxICON(mondrian
));
925 wxMenu
*file_menu
= new wxMenu
;
926 file_menu
->Append(Menu_Drag
, _T("&Test drag..."));
927 file_menu
->AppendCheckItem(Menu_DragMoveDef
, _T("&Move by default"));
928 file_menu
->AppendCheckItem(Menu_DragMoveAllow
, _T("&Allow moving"));
929 file_menu
->AppendSeparator();
930 file_menu
->Append(Menu_NewFrame
, _T("&New frame\tCtrl-N"));
931 file_menu
->AppendSeparator();
932 file_menu
->Append(Menu_Quit
, _T("E&xit\tCtrl-Q"));
934 wxMenu
*log_menu
= new wxMenu
;
935 log_menu
->Append(Menu_Clear
, _T("Clear\tCtrl-L"));
937 wxMenu
*help_menu
= new wxMenu
;
938 help_menu
->Append(Menu_Help
, _T("&Help..."));
939 help_menu
->AppendSeparator();
940 help_menu
->Append(Menu_About
, _T("&About"));
942 wxMenu
*clip_menu
= new wxMenu
;
943 clip_menu
->Append(Menu_Copy
, _T("&Copy text\tCtrl-C"));
944 clip_menu
->Append(Menu_Paste
, _T("&Paste text\tCtrl-V"));
945 clip_menu
->AppendSeparator();
946 clip_menu
->Append(Menu_CopyBitmap
, _T("Copy &bitmap\tCtrl-Shift-C"));
947 clip_menu
->Append(Menu_PasteBitmap
, _T("Paste b&itmap\tCtrl-Shift-V"));
949 clip_menu
->AppendSeparator();
950 clip_menu
->Append(Menu_PasteMFile
, _T("Paste &metafile\tCtrl-M"));
951 #endif // wxUSE_METAFILES
952 clip_menu
->AppendSeparator();
953 clip_menu
->Append(Menu_CopyFiles
, _T("Copy &files\tCtrl-F"));
955 wxMenuBar
*menu_bar
= new wxMenuBar
;
956 menu_bar
->Append(file_menu
, _T("&File"));
957 menu_bar
->Append(log_menu
, _T("&Log"));
958 menu_bar
->Append(clip_menu
, _T("&Clipboard"));
959 menu_bar
->Append(help_menu
, _T("&Help"));
961 SetMenuBar(menu_bar
);
963 // make a panel with 3 subwindows
964 wxString
strFile(_T("Drop files here!")), strText(_T("Drop text on me"));
966 m_ctrlFile
= new wxListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, 1, &strFile
,
967 wxLB_HSCROLL
| wxLB_ALWAYS_SB
);
968 m_ctrlText
= new wxListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, 1, &strText
,
969 wxLB_HSCROLL
| wxLB_ALWAYS_SB
);
971 m_ctrlLog
= new wxTextCtrl(this, wxID_ANY
, _T(""), wxDefaultPosition
, wxDefaultSize
,
972 wxTE_MULTILINE
| wxTE_READONLY
|
975 // redirect log messages to the text window
976 m_pLog
= new wxLogTextCtrl(m_ctrlLog
);
977 m_pLogPrev
= wxLog::SetActiveTarget(m_pLog
);
979 #if wxUSE_DRAG_AND_DROP
980 // associate drop targets with the controls
981 m_ctrlFile
->SetDropTarget(new DnDFile(m_ctrlFile
));
982 m_ctrlText
->SetDropTarget(new DnDText(m_ctrlText
));
983 m_ctrlLog
->SetDropTarget(new URLDropTarget
);
984 #endif // wxUSE_DRAG_AND_DROP
986 wxBoxSizer
*m_sizer_top
= new wxBoxSizer( wxHORIZONTAL
);
987 m_sizer_top
->Add(m_ctrlFile
, 1, wxEXPAND
);
988 m_sizer_top
->Add(m_ctrlText
, 1, wxEXPAND
);
990 wxBoxSizer
*m_sizer
= new wxBoxSizer( wxVERTICAL
);
991 m_sizer
->Add(m_sizer_top
, 1, wxEXPAND
);
992 m_sizer
->Add(m_ctrlLog
, 1, wxEXPAND
| wxBOTTOM
, 50);
995 m_sizer
->SetSizeHints( this );
997 // copy data by default but allow moving it as well
998 m_moveByDefault
= false;
1000 menu_bar
->Check(Menu_DragMoveAllow
, true);
1003 void DnDFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
1008 void DnDFrame::OnSize(wxSizeEvent
& event
)
1015 void DnDFrame::OnPaint(wxPaintEvent
& WXUNUSED(event
))
1019 GetClientSize( &w
, &h
);
1022 // dc.Clear(); -- this kills wxGTK
1023 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
, false, _T("charter") ) );
1024 dc
.DrawText( _T("Drag text from here!"), 100, h
-50 );
1027 void DnDFrame::OnUpdateUIMoveByDefault(wxUpdateUIEvent
& event
)
1029 // only can move by default if moving is allowed at all
1030 event
.Enable(m_moveAllow
);
1033 void DnDFrame::OnUpdateUIPasteText(wxUpdateUIEvent
& event
)
1036 // too many trace messages if we don't do it - this function is called
1041 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
1044 void DnDFrame::OnUpdateUIPasteBitmap(wxUpdateUIEvent
& event
)
1047 // too many trace messages if we don't do it - this function is called
1052 event
.Enable( wxTheClipboard
->IsSupported(wxDF_BITMAP
) );
1055 void DnDFrame::OnNewFrame(wxCommandEvent
& WXUNUSED(event
))
1057 #if wxUSE_DRAG_AND_DROP
1058 (new DnDShapeFrame(this))->Show(true);
1060 wxLogStatus(this, wxT("Double click the new frame to select a shape for it"));
1061 #endif // wxUSE_DRAG_AND_DROP
1064 void DnDFrame::OnDrag(wxCommandEvent
& WXUNUSED(event
))
1066 #if wxUSE_DRAG_AND_DROP
1067 wxString strText
= wxGetTextFromUser
1069 _T("After you enter text in this dialog, press any mouse\n")
1070 _T("button in the bottom (empty) part of the frame and \n")
1071 _T("drag it anywhere - you will be in fact dragging the\n")
1072 _T("text object containing this text"),
1073 _T("Please enter some text"), m_strText
, this
1076 m_strText
= strText
;
1077 #endif // wxUSE_DRAG_AND_DROP
1080 void DnDFrame::OnDragMoveByDefault(wxCommandEvent
& event
)
1082 m_moveByDefault
= event
.IsChecked();
1085 void DnDFrame::OnDragMoveAllow(wxCommandEvent
& event
)
1087 m_moveAllow
= event
.IsChecked();
1090 void DnDFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
1092 wxMessageBox(_T("Drag-&-Drop Demo\n")
1093 _T("Please see \"Help|Help...\" for details\n")
1094 _T("Copyright (c) 1998 Vadim Zeitlin"),
1096 wxICON_INFORMATION
| wxOK
,
1100 void DnDFrame::OnHelp(wxCommandEvent
& /* event */)
1102 wxMessageDialog
dialog(this,
1103 _T("This small program demonstrates drag & drop support in wxWidgets. The program window\n")
1104 _T("consists of 3 parts: the bottom pane is for debug messages, so that you can see what's\n")
1105 _T("going on inside. The top part is split into 2 listboxes, the left one accepts files\n")
1106 _T("and the right one accepts text.\n")
1108 _T("To test wxDropTarget: open wordpad (write.exe), select some text in it and drag it to\n")
1109 _T("the right listbox (you'll notice the usual visual feedback, i.e. the cursor will change).\n")
1110 _T("Also, try dragging some files (you can select several at once) from Windows Explorer (or \n")
1111 _T("File Manager) to the left pane. Hold down Ctrl/Shift keys when you drop text (doesn't \n")
1112 _T("work with files) and see what changes.\n")
1114 _T("To test wxDropSource: just press any mouse button on the empty zone of the window and drag\n")
1115 _T("it to wordpad or any other droptarget accepting text (and of course you can just drag it\n")
1116 _T("to the right pane). Due to a lot of trace messages, the cursor might take some time to \n")
1117 _T("change, don't release the mouse button until it does. You can change the string being\n")
1118 _T("dragged in in \"File|Test drag...\" dialog.\n")
1121 _T("Please send all questions/bug reports/suggestions &c to \n")
1122 _T("Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>"),
1128 void DnDFrame::OnLogClear(wxCommandEvent
& /* event */ )
1131 m_ctrlText
->Clear();
1132 m_ctrlFile
->Clear();
1135 void DnDFrame::OnLeftDown(wxMouseEvent
&WXUNUSED(event
) )
1137 #if wxUSE_DRAG_AND_DROP
1138 if ( !m_strText
.IsEmpty() )
1140 // start drag operation
1141 wxTextDataObject
textData(m_strText
);
1143 wxFileDataObject textData;
1144 textData.AddFile( "/file1.txt" );
1145 textData.AddFile( "/file2.txt" );
1147 wxDropSource
source(textData
, this,
1148 wxDROP_ICON(dnd_copy
),
1149 wxDROP_ICON(dnd_move
),
1150 wxDROP_ICON(dnd_none
));
1153 if ( m_moveByDefault
)
1154 flags
|= wxDrag_DefaultMove
;
1155 else if ( m_moveAllow
)
1156 flags
|= wxDrag_AllowMove
;
1159 switch ( source
.DoDragDrop(flags
) )
1161 case wxDragError
: pc
= _T("Error!"); break;
1162 case wxDragNone
: pc
= _T("Nothing"); break;
1163 case wxDragCopy
: pc
= _T("Copied"); break;
1164 case wxDragMove
: pc
= _T("Moved"); break;
1165 case wxDragCancel
: pc
= _T("Cancelled"); break;
1166 default: pc
= _T("Huh?"); break;
1169 SetStatusText(wxString(_T("Drag result: ")) + pc
);
1171 #endif // wxUSE_DRAG_AND_DROP
1174 void DnDFrame::OnRightDown(wxMouseEvent
&event
)
1176 wxMenu
menu(_T("Dnd sample menu"));
1178 menu
.Append(Menu_Drag
, _T("&Test drag..."));
1179 menu
.AppendSeparator();
1180 menu
.Append(Menu_About
, _T("&About"));
1182 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
1185 DnDFrame::~DnDFrame()
1187 if ( m_pLog
!= NULL
) {
1188 if ( wxLog::SetActiveTarget(m_pLogPrev
) == m_pLog
)
1193 // ---------------------------------------------------------------------------
1195 // ---------------------------------------------------------------------------
1197 void DnDFrame::OnCopyBitmap(wxCommandEvent
& WXUNUSED(event
))
1199 // PNG support is not always compiled in under Windows, so use BMP there
1201 wxFileDialog
dialog(this, _T("Open a PNG file"), _T(""), _T(""), _T("PNG files (*.png)|*.png"), 0);
1203 wxFileDialog
dialog(this, _T("Open a BMP file"), _T(""), _T(""), _T("BMP files (*.bmp)|*.bmp"), 0);
1206 if (dialog
.ShowModal() != wxID_OK
)
1208 wxLogMessage( _T("Aborted file open") );
1212 if (dialog
.GetPath().IsEmpty())
1214 wxLogMessage( _T("Returned empty string.") );
1218 if (!wxFileExists(dialog
.GetPath()))
1220 wxLogMessage( _T("File doesn't exist.") );
1225 image
.LoadFile( dialog
.GetPath(),
1234 wxLogError( _T("Invalid image file...") );
1238 wxLogStatus( _T("Decoding image file...") );
1241 wxBitmap
bitmap( image
);
1243 if ( !wxTheClipboard
->Open() )
1245 wxLogError(_T("Can't open clipboard."));
1250 wxLogMessage( _T("Creating wxBitmapDataObject...") );
1253 if ( !wxTheClipboard
->AddData(new wxBitmapDataObject(bitmap
)) )
1255 wxLogError(_T("Can't copy image to the clipboard."));
1259 wxLogMessage(_T("Image has been put on the clipboard.") );
1260 wxLogMessage(_T("You can paste it now and look at it.") );
1263 wxTheClipboard
->Close();
1266 void DnDFrame::OnPasteBitmap(wxCommandEvent
& WXUNUSED(event
))
1268 if ( !wxTheClipboard
->Open() )
1270 wxLogError(_T("Can't open clipboard."));
1275 if ( !wxTheClipboard
->IsSupported(wxDF_BITMAP
) )
1277 wxLogWarning(_T("No bitmap on clipboard"));
1279 wxTheClipboard
->Close();
1283 wxBitmapDataObject data
;
1284 if ( !wxTheClipboard
->GetData(data
) )
1286 wxLogError(_T("Can't paste bitmap from the clipboard"));
1290 const wxBitmap
& bmp
= data
.GetBitmap();
1292 wxLogMessage(_T("Bitmap %dx%d pasted from the clipboard"),
1293 bmp
.GetWidth(), bmp
.GetHeight());
1297 wxTheClipboard
->Close();
1302 void DnDFrame::OnPasteMetafile(wxCommandEvent
& WXUNUSED(event
))
1304 if ( !wxTheClipboard
->Open() )
1306 wxLogError(_T("Can't open clipboard."));
1311 if ( !wxTheClipboard
->IsSupported(wxDF_METAFILE
) )
1313 wxLogWarning(_T("No metafile on clipboard"));
1317 wxMetaFileDataObject data
;
1318 if ( !wxTheClipboard
->GetData(data
) )
1320 wxLogError(_T("Can't paste metafile from the clipboard"));
1324 const wxMetaFile
& mf
= data
.GetMetafile();
1326 wxLogMessage(_T("Metafile %dx%d pasted from the clipboard"),
1327 mf
.GetWidth(), mf
.GetHeight());
1333 wxTheClipboard
->Close();
1336 #endif // wxUSE_METAFILES
1338 // ----------------------------------------------------------------------------
1340 // ----------------------------------------------------------------------------
1342 void DnDFrame::OnCopyFiles(wxCommandEvent
& WXUNUSED(event
))
1345 wxFileDialog
dialog(this, _T("Select a file to copy"), _T(""), _T(""),
1346 _T("All files (*.*)|*.*"), 0);
1348 wxArrayString filenames
;
1349 while ( dialog
.ShowModal() == wxID_OK
)
1351 filenames
.Add(dialog
.GetPath());
1354 if ( !filenames
.IsEmpty() )
1356 wxFileDataObject
*dobj
= new wxFileDataObject
;
1357 size_t count
= filenames
.GetCount();
1358 for ( size_t n
= 0; n
< count
; n
++ )
1360 dobj
->AddFile(filenames
[n
]);
1363 wxClipboardLocker locker
;
1366 wxLogError(wxT("Can't open clipboard"));
1370 if ( !wxTheClipboard
->AddData(dobj
) )
1372 wxLogError(wxT("Can't copy file(s) to the clipboard"));
1376 wxLogStatus(this, wxT("%d file%s copied to the clipboard"),
1377 count
, count
== 1 ? wxT("") : wxT("s"));
1383 wxLogStatus(this, wxT("Aborted"));
1386 wxLogError(wxT("Sorry, not implemented"));
1390 // ---------------------------------------------------------------------------
1392 // ---------------------------------------------------------------------------
1394 void DnDFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1396 if ( !wxTheClipboard
->Open() )
1398 wxLogError(_T("Can't open clipboard."));
1403 if ( !wxTheClipboard
->AddData(new wxTextDataObject(m_strText
)) )
1405 wxLogError(_T("Can't copy data to the clipboard"));
1409 wxLogMessage(_T("Text '%s' put on the clipboard"), m_strText
.c_str());
1412 wxTheClipboard
->Close();
1415 void DnDFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1417 if ( !wxTheClipboard
->Open() )
1419 wxLogError(_T("Can't open clipboard."));
1424 if ( !wxTheClipboard
->IsSupported(wxDF_TEXT
) )
1426 wxLogWarning(_T("No text data on clipboard"));
1428 wxTheClipboard
->Close();
1432 wxTextDataObject text
;
1433 if ( !wxTheClipboard
->GetData(text
) )
1435 wxLogError(_T("Can't paste data from the clipboard"));
1439 wxLogMessage(_T("Text '%s' pasted from the clipboard"),
1440 text
.GetText().c_str());
1443 wxTheClipboard
->Close();
1446 #if wxUSE_DRAG_AND_DROP
1448 // ----------------------------------------------------------------------------
1449 // Notifications called by the base class
1450 // ----------------------------------------------------------------------------
1452 bool DnDText::OnDropText(wxCoord
, wxCoord
, const wxString
& text
)
1454 m_pOwner
->Append(text
);
1459 bool DnDFile::OnDropFiles(wxCoord
, wxCoord
, const wxArrayString
& filenames
)
1461 size_t nFiles
= filenames
.GetCount();
1463 str
.Printf( _T("%d files dropped"), (int)nFiles
);
1464 m_pOwner
->Append(str
);
1465 for ( size_t n
= 0; n
< nFiles
; n
++ ) {
1466 m_pOwner
->Append(filenames
[n
]);
1472 // ----------------------------------------------------------------------------
1474 // ----------------------------------------------------------------------------
1476 DnDShapeDialog::DnDShapeDialog(wxFrame
*parent
, DnDShape
*shape
)
1477 :wxDialog( parent
, 6001, wxT("Choose Shape"), wxPoint( 10, 10 ),
1479 wxRAISED_BORDER
|wxCAPTION
|wxTHICK_FRAME
|wxSYSTEM_MENU
)
1482 wxBoxSizer
* topSizer
= new wxBoxSizer( wxVERTICAL
);
1485 wxBoxSizer
* shapesSizer
= new wxBoxSizer( wxHORIZONTAL
);
1486 const wxString choices
[] = { wxT("None"), wxT("Triangle"),
1487 wxT("Rectangle"), wxT("Ellipse") };
1489 m_radio
= new wxRadioBox( this, wxID_ANY
, wxT("&Shape"),
1490 wxDefaultPosition
, wxDefaultSize
, 4, choices
, 4,
1491 wxRA_SPECIFY_COLS
);
1492 shapesSizer
->Add( m_radio
, 0, wxGROW
|wxALL
, 5 );
1493 topSizer
->Add( shapesSizer
, 0, wxALL
, 2 );
1496 wxStaticBox
* box
= new wxStaticBox( this, wxID_ANY
, wxT("&Attributes") );
1497 wxStaticBoxSizer
* attrSizer
= new wxStaticBoxSizer( box
, wxHORIZONTAL
);
1498 wxFlexGridSizer
* xywhSizer
= new wxFlexGridSizer( 4, 2 );
1502 st
= new wxStaticText( this, wxID_ANY
, wxT("Position &X:") );
1503 m_textX
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1505 xywhSizer
->Add( st
, 1, wxGROW
|wxALL
, 2 );
1506 xywhSizer
->Add( m_textX
, 1, wxGROW
|wxALL
, 2 );
1508 st
= new wxStaticText( this, wxID_ANY
, wxT("Size &width:") );
1509 m_textW
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1511 xywhSizer
->Add( st
, 1, wxGROW
|wxALL
, 2 );
1512 xywhSizer
->Add( m_textW
, 1, wxGROW
|wxALL
, 2 );
1514 st
= new wxStaticText( this, wxID_ANY
, wxT("&Y:") );
1515 m_textY
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1517 xywhSizer
->Add( st
, 1, wxALL
|wxALIGN_RIGHT
, 2 );
1518 xywhSizer
->Add( m_textY
, 1, wxGROW
|wxALL
, 2 );
1520 st
= new wxStaticText( this, wxID_ANY
, wxT("&height:") );
1521 m_textH
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1523 xywhSizer
->Add( st
, 1, wxALL
|wxALIGN_RIGHT
, 2 );
1524 xywhSizer
->Add( m_textH
, 1, wxGROW
|wxALL
, 2 );
1526 wxButton
* col
= new wxButton( this, Button_Colour
, wxT("&Colour...") );
1527 attrSizer
->Add( xywhSizer
, 1, wxGROW
);
1528 attrSizer
->Add( col
, 0, wxALL
|wxALIGN_CENTRE_VERTICAL
, 2 );
1529 topSizer
->Add( attrSizer
, 0, wxGROW
|wxALL
, 5 );
1532 wxBoxSizer
* buttonSizer
= new wxBoxSizer( wxHORIZONTAL
);
1534 bt
= new wxButton( this, wxID_OK
, wxT("Ok") );
1535 buttonSizer
->Add( bt
, 0, wxALL
, 2 );
1536 bt
= new wxButton( this, wxID_CANCEL
, wxT("Cancel") );
1537 buttonSizer
->Add( bt
, 0, wxALL
, 2 );
1538 topSizer
->Add( buttonSizer
, 0, wxALL
|wxALIGN_RIGHT
, 2 );
1540 SetSizer( topSizer
);
1541 topSizer
->Fit( this );
1544 DnDShape
*DnDShapeDialog::GetShape() const
1546 switch ( m_shapeKind
)
1549 case DnDShape::None
: return NULL
;
1550 case DnDShape::Triangle
: return new DnDTriangularShape(m_pos
, m_size
, m_col
);
1551 case DnDShape::Rectangle
: return new DnDRectangularShape(m_pos
, m_size
, m_col
);
1552 case DnDShape::Ellipse
: return new DnDEllipticShape(m_pos
, m_size
, m_col
);
1556 bool DnDShapeDialog::TransferDataToWindow()
1561 m_radio
->SetSelection(m_shape
->GetKind());
1562 m_pos
= m_shape
->GetPosition();
1563 m_size
= m_shape
->GetSize();
1564 m_col
= m_shape
->GetColour();
1568 m_radio
->SetSelection(DnDShape::None
);
1569 m_pos
= wxPoint(1, 1);
1570 m_size
= wxSize(100, 100);
1573 m_textX
->SetValue(wxString() << m_pos
.x
);
1574 m_textY
->SetValue(wxString() << m_pos
.y
);
1575 m_textW
->SetValue(wxString() << m_size
.x
);
1576 m_textH
->SetValue(wxString() << m_size
.y
);
1581 bool DnDShapeDialog::TransferDataFromWindow()
1583 m_shapeKind
= (DnDShape::Kind
)m_radio
->GetSelection();
1585 m_pos
.x
= wxAtoi(m_textX
->GetValue());
1586 m_pos
.y
= wxAtoi(m_textY
->GetValue());
1587 m_size
.x
= wxAtoi(m_textW
->GetValue());
1588 m_size
.y
= wxAtoi(m_textH
->GetValue());
1590 if ( !m_pos
.x
|| !m_pos
.y
|| !m_size
.x
|| !m_size
.y
)
1592 wxMessageBox(_T("All sizes and positions should be non null!"),
1593 _T("Invalid shape"), wxICON_HAND
| wxOK
, this);
1601 void DnDShapeDialog::OnColour(wxCommandEvent
& WXUNUSED(event
))
1604 data
.SetChooseFull(true);
1605 for (int i
= 0; i
< 16; i
++)
1607 wxColour
colour(i
*16, i
*16, i
*16);
1608 data
.SetCustomColour(i
, colour
);
1611 wxColourDialog
dialog(this, &data
);
1612 if ( dialog
.ShowModal() == wxID_OK
)
1614 m_col
= dialog
.GetColourData().GetColour();
1618 // ----------------------------------------------------------------------------
1620 // ----------------------------------------------------------------------------
1622 DnDShapeFrame
*DnDShapeFrame::ms_lastDropTarget
= NULL
;
1624 DnDShapeFrame::DnDShapeFrame(wxFrame
*parent
)
1625 : wxFrame(parent
, wxID_ANY
, _T("Shape Frame"),
1626 wxDefaultPosition
, wxSize(250, 150))
1630 wxMenu
*menuShape
= new wxMenu
;
1631 menuShape
->Append(Menu_Shape_New
, _T("&New default shape\tCtrl-S"));
1632 menuShape
->Append(Menu_Shape_Edit
, _T("&Edit shape\tCtrl-E"));
1633 menuShape
->AppendSeparator();
1634 menuShape
->Append(Menu_Shape_Clear
, _T("&Clear shape\tCtrl-L"));
1636 wxMenu
*menuClipboard
= new wxMenu
;
1637 menuClipboard
->Append(Menu_ShapeClipboard_Copy
, _T("&Copy\tCtrl-C"));
1638 menuClipboard
->Append(Menu_ShapeClipboard_Paste
, _T("&Paste\tCtrl-V"));
1640 wxMenuBar
*menubar
= new wxMenuBar
;
1641 menubar
->Append(menuShape
, _T("&Shape"));
1642 menubar
->Append(menuClipboard
, _T("&Clipboard"));
1644 SetMenuBar(menubar
);
1646 SetStatusText(_T("Press Ctrl-S to create a new shape"));
1648 SetDropTarget(new DnDShapeDropTarget(this));
1652 SetBackgroundColour(*wxWHITE
);
1655 DnDShapeFrame::~DnDShapeFrame()
1661 void DnDShapeFrame::SetShape(DnDShape
*shape
)
1670 void DnDShapeFrame::OnDrag(wxMouseEvent
& event
)
1679 // start drag operation
1680 DnDShapeDataObject
shapeData(m_shape
);
1681 wxDropSource
source(shapeData
, this);
1683 const wxChar
*pc
= NULL
;
1684 switch ( source
.DoDragDrop(true) )
1688 wxLogError(wxT("An error occured during drag and drop operation"));
1692 SetStatusText(_T("Nothing happened"));
1701 if ( ms_lastDropTarget
!= this )
1703 // don't delete the shape if we dropped it on ourselves!
1709 SetStatusText(_T("Drag and drop operation cancelled"));
1715 SetStatusText(wxString(_T("Shape successfully ")) + pc
);
1717 //else: status text already set
1720 void DnDShapeFrame::OnDrop(wxCoord x
, wxCoord y
, DnDShape
*shape
)
1722 ms_lastDropTarget
= this;
1727 s
.Printf(wxT("Shape dropped at (%d, %d)"), pt
.x
, pt
.y
);
1734 void DnDShapeFrame::OnEditShape(wxCommandEvent
& WXUNUSED(event
))
1736 DnDShapeDialog
dlg(this, m_shape
);
1737 if ( dlg
.ShowModal() == wxID_OK
)
1739 SetShape(dlg
.GetShape());
1743 SetStatusText(_T("You can now drag the shape to another frame"));
1748 void DnDShapeFrame::OnNewShape(wxCommandEvent
& WXUNUSED(event
))
1750 SetShape(new DnDEllipticShape(wxPoint(10, 10), wxSize(80, 60), *wxRED
));
1752 SetStatusText(_T("You can now drag the shape to another frame"));
1755 void DnDShapeFrame::OnClearShape(wxCommandEvent
& WXUNUSED(event
))
1760 void DnDShapeFrame::OnCopyShape(wxCommandEvent
& WXUNUSED(event
))
1764 wxClipboardLocker clipLocker
;
1767 wxLogError(wxT("Can't open the clipboard"));
1772 wxTheClipboard
->AddData(new DnDShapeDataObject(m_shape
));
1776 void DnDShapeFrame::OnPasteShape(wxCommandEvent
& WXUNUSED(event
))
1778 wxClipboardLocker clipLocker
;
1781 wxLogError(wxT("Can't open the clipboard"));
1786 DnDShapeDataObject
shapeDataObject(NULL
);
1787 if ( wxTheClipboard
->GetData(shapeDataObject
) )
1789 SetShape(shapeDataObject
.GetShape());
1793 wxLogStatus(wxT("No shape on the clipboard"));
1797 void DnDShapeFrame::OnUpdateUICopy(wxUpdateUIEvent
& event
)
1799 event
.Enable( m_shape
!= NULL
);
1802 void DnDShapeFrame::OnUpdateUIPaste(wxUpdateUIEvent
& event
)
1804 event
.Enable( wxTheClipboard
->IsSupported(wxDataFormat(shapeFormatId
)) );
1807 void DnDShapeFrame::OnPaint(wxPaintEvent
& event
)
1821 // ----------------------------------------------------------------------------
1823 // ----------------------------------------------------------------------------
1825 DnDShape
*DnDShape::New(const void *buf
)
1827 const ShapeDump
& dump
= *(const ShapeDump
*)buf
;
1831 return new DnDTriangularShape(wxPoint(dump
.x
, dump
.y
),
1832 wxSize(dump
.w
, dump
.h
),
1833 wxColour(dump
.r
, dump
.g
, dump
.b
));
1836 return new DnDRectangularShape(wxPoint(dump
.x
, dump
.y
),
1837 wxSize(dump
.w
, dump
.h
),
1838 wxColour(dump
.r
, dump
.g
, dump
.b
));
1841 return new DnDEllipticShape(wxPoint(dump
.x
, dump
.y
),
1842 wxSize(dump
.w
, dump
.h
),
1843 wxColour(dump
.r
, dump
.g
, dump
.b
));
1846 wxFAIL_MSG(wxT("invalid shape!"));
1851 // ----------------------------------------------------------------------------
1852 // DnDShapeDataObject
1853 // ----------------------------------------------------------------------------
1857 void DnDShapeDataObject::CreateMetaFile() const
1859 wxPoint pos
= m_shape
->GetPosition();
1860 wxSize size
= m_shape
->GetSize();
1862 wxMetaFileDC
dcMF(wxEmptyString
, pos
.x
+ size
.x
, pos
.y
+ size
.y
);
1864 m_shape
->Draw(dcMF
);
1866 wxMetafile
*mf
= dcMF
.Close();
1868 DnDShapeDataObject
*self
= (DnDShapeDataObject
*)this; // const_cast
1869 self
->m_dobjMetaFile
.SetMetafile(*mf
);
1870 self
->m_hasMetaFile
= true;
1875 #endif // wxUSE_METAFILES
1877 void DnDShapeDataObject::CreateBitmap() const
1879 wxPoint pos
= m_shape
->GetPosition();
1880 wxSize size
= m_shape
->GetSize();
1881 int x
= pos
.x
+ size
.x
,
1883 wxBitmap
bitmap(x
, y
);
1885 dc
.SelectObject(bitmap
);
1886 dc
.SetBrush(wxBrush(wxT("white"), wxSOLID
));
1889 dc
.SelectObject(wxNullBitmap
);
1891 DnDShapeDataObject
*self
= (DnDShapeDataObject
*)this; // const_cast
1892 self
->m_dobjBitmap
.SetBitmap(bitmap
);
1893 self
->m_hasBitmap
= true;
1896 #endif // wxUSE_DRAG_AND_DROP
1898 // ----------------------------------------------------------------------------
1900 // ----------------------------------------------------------------------------
1902 static void ShowBitmap(const wxBitmap
& bitmap
)
1904 wxFrame
*frame
= new wxFrame(NULL
, wxID_ANY
, _T("Bitmap view"));
1905 frame
->CreateStatusBar();
1906 DnDCanvasBitmap
*canvas
= new DnDCanvasBitmap(frame
);
1907 canvas
->SetBitmap(bitmap
);
1909 int w
= bitmap
.GetWidth(),
1910 h
= bitmap
.GetHeight();
1911 frame
->SetStatusText(wxString::Format(_T("%dx%d"), w
, h
));
1913 frame
->SetClientSize(w
> 100 ? 100 : w
, h
> 100 ? 100 : h
);
1919 static void ShowMetaFile(const wxMetaFile
& metafile
)
1921 wxFrame
*frame
= new wxFrame(NULL
, wxID_ANY
, _T("Metafile view"));
1922 frame
->CreateStatusBar();
1923 DnDCanvasMetafile
*canvas
= new DnDCanvasMetafile(frame
);
1924 canvas
->SetMetafile(metafile
);
1926 wxSize size
= metafile
.GetSize();
1927 frame
->SetStatusText(wxString::Format(_T("%dx%d"), size
.x
, size
.y
));
1929 frame
->SetClientSize(size
.x
> 100 ? 100 : size
.x
,
1930 size
.y
> 100 ? 100 : size
.y
);
1934 #endif // wxUSE_METAFILES
1936 #endif // wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD