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"
34 #include "wx/metafile.h"
35 #endif // wxUSE_METAFILES
37 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
38 #include "mondrian.xpm"
40 #include "dnd_copy.xpm"
41 #include "dnd_move.xpm"
42 #include "dnd_none.xpm"
45 #if wxUSE_DRAG_AND_DROP
47 // ----------------------------------------------------------------------------
48 // Derive two simple classes which just put in the listbox the strings (text or
49 // file names) we drop on them
50 // ----------------------------------------------------------------------------
52 class DnDText
: public wxTextDropTarget
55 DnDText(wxListBox
*pOwner
) { m_pOwner
= pOwner
; }
57 virtual bool OnDropText(wxCoord x
, wxCoord y
, const wxString
& text
);
63 class DnDFile
: public wxFileDropTarget
66 DnDFile(wxListBox
*pOwner
) { m_pOwner
= pOwner
; }
68 virtual bool OnDropFiles(wxCoord x
, wxCoord y
,
69 const wxArrayString
& filenames
);
75 // ----------------------------------------------------------------------------
76 // Define a custom dtop target accepting URLs
77 // ----------------------------------------------------------------------------
79 class URLDropTarget
: public wxDropTarget
82 URLDropTarget() { SetDataObject(new wxURLDataObject
); }
84 void OnDropURL(wxCoord x
, wxCoord y
, const wxString
& text
)
86 // of course, a real program would do something more useful here...
87 wxMessageBox(text
, _T("wxDnD sample: got URL"),
88 wxICON_INFORMATION
| wxOK
);
91 // URLs can't be moved, only copied
92 virtual wxDragResult
OnDragOver(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
95 return wxDragLink
; // At least IE 5.x needs wxDragLink, the
96 // other browsers on MSW seem okay with it too.
99 // translate this to calls to OnDropURL() just for convenience
100 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
105 OnDropURL(x
, y
, ((wxURLDataObject
*)m_dataObject
)->GetURL());
111 #endif // wxUSE_DRAG_AND_DROP
113 // ----------------------------------------------------------------------------
114 // Define a new application type
115 // ----------------------------------------------------------------------------
117 class DnDApp
: public wxApp
120 virtual bool OnInit();
123 IMPLEMENT_APP(DnDApp
);
125 #if wxUSE_DRAG_AND_DROP
127 // ----------------------------------------------------------------------------
128 // Define canvas class to show a bitmap
129 // ----------------------------------------------------------------------------
131 class DnDCanvasBitmap
: public wxScrolledWindow
134 DnDCanvasBitmap(wxWindow
*parent
) : wxScrolledWindow(parent
) { }
136 void SetBitmap(const wxBitmap
& bitmap
)
140 SetScrollbars(10, 10,
141 m_bitmap
.GetWidth() / 10, m_bitmap
.GetHeight() / 10);
146 void OnPaint(wxPaintEvent
& event
)
154 dc
.DrawBitmap(m_bitmap
, 0, 0);
161 DECLARE_EVENT_TABLE()
166 // and the same thing fo metafiles
167 class DnDCanvasMetafile
: public wxScrolledWindow
170 DnDCanvasMetafile(wxWindow
*parent
) : wxScrolledWindow(parent
) { }
172 void SetMetafile(const wxMetafile
& metafile
)
174 m_metafile
= metafile
;
176 SetScrollbars(10, 10,
177 m_metafile
.GetWidth() / 10, m_metafile
.GetHeight() / 10);
182 void OnPaint(wxPaintEvent
& event
)
186 if ( m_metafile
.Ok() )
190 m_metafile
.Play(&dc
);
195 wxMetafile m_metafile
;
197 DECLARE_EVENT_TABLE()
200 #endif // wxUSE_METAFILES
202 // ----------------------------------------------------------------------------
203 // Define a new frame type for the main frame
204 // ----------------------------------------------------------------------------
206 class DnDFrame
: public wxFrame
209 DnDFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
212 void OnPaint(wxPaintEvent
& event
);
213 void OnSize(wxSizeEvent
& event
);
214 void OnQuit(wxCommandEvent
& event
);
215 void OnAbout(wxCommandEvent
& event
);
216 void OnDrag(wxCommandEvent
& event
);
217 void OnDragMoveByDefault(wxCommandEvent
& event
);
218 void OnDragMoveAllow(wxCommandEvent
& event
);
219 void OnNewFrame(wxCommandEvent
& event
);
220 void OnHelp (wxCommandEvent
& event
);
221 void OnLogClear(wxCommandEvent
& event
);
223 void OnCopy(wxCommandEvent
& event
);
224 void OnPaste(wxCommandEvent
& event
);
226 void OnCopyBitmap(wxCommandEvent
& event
);
227 void OnPasteBitmap(wxCommandEvent
& event
);
230 void OnPasteMetafile(wxCommandEvent
& event
);
231 #endif // wxUSE_METAFILES
233 void OnCopyFiles(wxCommandEvent
& event
);
235 void OnLeftDown(wxMouseEvent
& event
);
236 void OnRightDown(wxMouseEvent
& event
);
238 void OnUpdateUIMoveByDefault(wxUpdateUIEvent
& event
);
240 void OnUpdateUIPasteText(wxUpdateUIEvent
& event
);
241 void OnUpdateUIPasteBitmap(wxUpdateUIEvent
& event
);
243 DECLARE_EVENT_TABLE()
247 wxListBox
*m_ctrlFile
,
249 wxTextCtrl
*m_ctrlLog
;
254 // move the text by default (or copy)?
255 bool m_moveByDefault
;
257 // allow moving the text at all?
264 // ----------------------------------------------------------------------------
265 // A shape is an example of application-specific data which may be transported
266 // via drag-and-drop or clipboard: in our case, we have different geometric
267 // shapes, each one with its own colour and position
268 // ----------------------------------------------------------------------------
281 DnDShape(const wxPoint
& pos
,
284 : m_pos(pos
), m_size(size
), m_col(col
)
288 // this is for debugging - lets us see when exactly an object is freed
289 // (this may be later than you think if it's on the clipboard, for example)
290 virtual ~DnDShape() { }
292 // the functions used for drag-and-drop: they dump and restore a shape into
293 // some bitwise-copiable data (might use streams too...)
294 // ------------------------------------------------------------------------
296 // restore from buffer
297 static DnDShape
*New(const void *buf
);
299 virtual size_t GetDataSize() const
301 return sizeof(ShapeDump
);
304 virtual void GetDataHere(void *buf
) const
306 ShapeDump
& dump
= *(ShapeDump
*)buf
;
311 dump
.r
= m_col
.Red();
312 dump
.g
= m_col
.Green();
313 dump
.b
= m_col
.Blue();
318 const wxPoint
& GetPosition() const { return m_pos
; }
319 const wxColour
& GetColour() const { return m_col
; }
320 const wxSize
& GetSize() const { return m_size
; }
322 void Move(const wxPoint
& pos
) { m_pos
= pos
; }
324 // to implement in derived classes
325 virtual Kind
GetKind() const = 0;
327 virtual void Draw(wxDC
& dc
)
329 dc
.SetPen(wxPen(m_col
, 1, wxSOLID
));
333 //get a point 1 up and 1 left, otherwise the mid-point of a triangle is on the line
334 wxPoint
GetCentre() const
335 { return wxPoint(m_pos
.x
+ m_size
.x
/ 2 - 1, m_pos
.y
+ m_size
.y
/ 2 - 1); }
339 int x
, y
, // position
350 class DnDTriangularShape
: public DnDShape
353 DnDTriangularShape(const wxPoint
& pos
,
356 : DnDShape(pos
, size
, col
)
358 wxLogMessage(wxT("DnDTriangularShape is being created"));
361 virtual ~DnDTriangularShape()
363 wxLogMessage(wxT("DnDTriangularShape is being deleted"));
366 virtual Kind
GetKind() const { return Triangle
; }
367 virtual void Draw(wxDC
& dc
)
371 // well, it's a bit difficult to describe a triangle by position and
372 // size, but we're not doing geometry here, do we? ;-)
374 wxPoint
p2(m_pos
.x
+ m_size
.x
, m_pos
.y
);
375 wxPoint
p3(m_pos
.x
, m_pos
.y
+ m_size
.y
);
381 //works in multicolor modes; on GTK (at least) will fail in 16-bit color
382 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
383 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
387 class DnDRectangularShape
: public DnDShape
390 DnDRectangularShape(const wxPoint
& pos
,
393 : DnDShape(pos
, size
, col
)
395 wxLogMessage(wxT("DnDRectangularShape is being created"));
398 virtual ~DnDRectangularShape()
400 wxLogMessage(wxT("DnDRectangularShape is being deleted"));
403 virtual Kind
GetKind() const { return Rectangle
; }
404 virtual void Draw(wxDC
& dc
)
409 wxPoint
p2(p1
.x
+ m_size
.x
, p1
.y
);
410 wxPoint
p3(p2
.x
, p2
.y
+ m_size
.y
);
411 wxPoint
p4(p1
.x
, p3
.y
);
418 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
419 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
423 class DnDEllipticShape
: public DnDShape
426 DnDEllipticShape(const wxPoint
& pos
,
429 : DnDShape(pos
, size
, col
)
431 wxLogMessage(wxT("DnDEllipticShape is being created"));
434 virtual ~DnDEllipticShape()
436 wxLogMessage(wxT("DnDEllipticShape is being deleted"));
439 virtual Kind
GetKind() const { return Ellipse
; }
440 virtual void Draw(wxDC
& dc
)
444 dc
.DrawEllipse(m_pos
, m_size
);
446 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
447 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
451 // ----------------------------------------------------------------------------
452 // A wxDataObject specialisation for the application-specific data
453 // ----------------------------------------------------------------------------
455 static const wxChar
*shapeFormatId
= wxT("wxShape");
457 class DnDShapeDataObject
: public wxDataObject
460 // ctor doesn't copy the pointer, so it shouldn't go away while this object
462 DnDShapeDataObject(DnDShape
*shape
= (DnDShape
*)NULL
)
466 // we need to copy the shape because the one we're handled may be
467 // deleted while it's still on the clipboard (for example) - and we
468 // reuse the serialisation methods here to copy it
469 void *buf
= malloc(shape
->DnDShape::GetDataSize());
470 shape
->GetDataHere(buf
);
471 m_shape
= DnDShape::New(buf
);
481 // this string should uniquely identify our format, but is otherwise
483 m_formatShape
.SetId(shapeFormatId
);
485 // we don't draw the shape to a bitmap until it's really needed (i.e.
486 // we're asked to do so)
489 m_hasMetaFile
= FALSE
;
490 #endif // wxUSE_METAFILES
493 virtual ~DnDShapeDataObject() { delete m_shape
; }
495 // after a call to this function, the shape is owned by the caller and it
496 // is responsible for deleting it!
498 // NB: a better solution would be to make DnDShapes ref counted and this
499 // is what should probably be done in a real life program, otherwise
500 // the ownership problems become too complicated really fast
503 DnDShape
*shape
= m_shape
;
505 m_shape
= (DnDShape
*)NULL
;
508 m_hasMetaFile
= FALSE
;
509 #endif // wxUSE_METAFILES
514 // implement base class pure virtuals
515 // ----------------------------------
517 virtual wxDataFormat
GetPreferredFormat(Direction
WXUNUSED(dir
)) const
519 return m_formatShape
;
522 virtual size_t GetFormatCount(Direction dir
) const
524 // our custom format is supported by both GetData() and SetData()
528 // but the bitmap format(s) are only supported for output
529 nFormats
+= m_dobjBitmap
.GetFormatCount(dir
);
532 nFormats
+= m_dobjMetaFile
.GetFormatCount(dir
);
533 #endif // wxUSE_METAFILES
539 virtual void GetAllFormats(wxDataFormat
*formats
, Direction dir
) const
541 formats
[0] = m_formatShape
;
544 // in Get direction we additionally support bitmaps and metafiles
546 m_dobjBitmap
.GetAllFormats(&formats
[1], dir
);
549 // don't assume that m_dobjBitmap has only 1 format
550 m_dobjMetaFile
.GetAllFormats(&formats
[1 +
551 m_dobjBitmap
.GetFormatCount(dir
)], dir
);
552 #endif // wxUSE_METAFILES
556 virtual size_t GetDataSize(const wxDataFormat
& format
) const
558 if ( format
== m_formatShape
)
560 return m_shape
->GetDataSize();
563 else if ( m_dobjMetaFile
.IsSupported(format
) )
565 if ( !m_hasMetaFile
)
568 return m_dobjMetaFile
.GetDataSize(format
);
570 #endif // wxUSE_METAFILES
573 wxASSERT_MSG( m_dobjBitmap
.IsSupported(format
),
574 wxT("unexpected format") );
579 return m_dobjBitmap
.GetDataSize();
583 virtual bool GetDataHere(const wxDataFormat
& format
, void *pBuf
) const
585 if ( format
== m_formatShape
)
587 m_shape
->GetDataHere(pBuf
);
592 else if ( m_dobjMetaFile
.IsSupported(format
) )
594 if ( !m_hasMetaFile
)
597 return m_dobjMetaFile
.GetDataHere(format
, pBuf
);
599 #endif // wxUSE_METAFILES
602 wxASSERT_MSG( m_dobjBitmap
.IsSupported(format
),
603 wxT("unexpected format") );
608 return m_dobjBitmap
.GetDataHere(pBuf
);
612 virtual bool SetData(const wxDataFormat
& format
,
613 size_t len
, const void *buf
)
615 wxCHECK_MSG( format
== m_formatShape
, FALSE
,
616 wxT( "unsupported format") );
619 m_shape
= DnDShape::New(buf
);
621 // the shape has changed
625 m_hasMetaFile
= FALSE
;
626 #endif // wxUSE_METAFILES
632 // creates a bitmap and assigns it to m_dobjBitmap (also sets m_hasBitmap)
633 void CreateBitmap() const;
635 void CreateMetaFile() const;
636 #endif // wxUSE_METAFILES
638 wxDataFormat m_formatShape
; // our custom format
640 wxBitmapDataObject m_dobjBitmap
; // it handles bitmaps
641 bool m_hasBitmap
; // true if m_dobjBitmap has valid bitmap
644 wxMetaFileDataObject m_dobjMetaFile
;// handles metafiles
645 bool m_hasMetaFile
; // true if we have valid metafile
646 #endif // wxUSE_METAFILES
648 DnDShape
*m_shape
; // our data
651 // ----------------------------------------------------------------------------
652 // A dialog to edit shape properties
653 // ----------------------------------------------------------------------------
655 class DnDShapeDialog
: public wxDialog
658 DnDShapeDialog(wxFrame
*parent
, DnDShape
*shape
);
660 DnDShape
*GetShape() const;
662 virtual bool TransferDataToWindow();
663 virtual bool TransferDataFromWindow();
665 void OnColour(wxCommandEvent
& event
);
672 DnDShape::Kind m_shapeKind
;
684 DECLARE_EVENT_TABLE()
687 // ----------------------------------------------------------------------------
688 // A frame for the shapes which can be drag-and-dropped between frames
689 // ----------------------------------------------------------------------------
691 class DnDShapeFrame
: public wxFrame
694 DnDShapeFrame(wxFrame
*parent
);
697 void SetShape(DnDShape
*shape
);
698 virtual bool SetShape(const wxRegion
®ion
)
700 return wxFrame::SetShape( region
);
704 void OnNewShape(wxCommandEvent
& event
);
705 void OnEditShape(wxCommandEvent
& event
);
706 void OnClearShape(wxCommandEvent
& event
);
708 void OnCopyShape(wxCommandEvent
& event
);
709 void OnPasteShape(wxCommandEvent
& event
);
711 void OnUpdateUICopy(wxUpdateUIEvent
& event
);
712 void OnUpdateUIPaste(wxUpdateUIEvent
& event
);
714 void OnDrag(wxMouseEvent
& event
);
715 void OnPaint(wxPaintEvent
& event
);
716 void OnDrop(wxCoord x
, wxCoord y
, DnDShape
*shape
);
721 static DnDShapeFrame
*ms_lastDropTarget
;
723 DECLARE_EVENT_TABLE()
726 // ----------------------------------------------------------------------------
727 // wxDropTarget derivation for DnDShapes
728 // ----------------------------------------------------------------------------
730 class DnDShapeDropTarget
: public wxDropTarget
733 DnDShapeDropTarget(DnDShapeFrame
*frame
)
734 : wxDropTarget(new DnDShapeDataObject
)
739 // override base class (pure) virtuals
740 virtual wxDragResult
OnEnter(wxCoord x
, wxCoord y
, wxDragResult def
)
741 { m_frame
->SetStatusText(_T("Mouse entered the frame")); return OnDragOver(x
, y
, def
); }
742 virtual void OnLeave()
743 { m_frame
->SetStatusText(_T("Mouse left the frame")); }
744 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
748 wxLogError(wxT("Failed to get drag and drop data"));
753 m_frame
->OnDrop(x
, y
,
754 ((DnDShapeDataObject
*)GetDataObject())->GetShape());
760 DnDShapeFrame
*m_frame
;
763 // ----------------------------------------------------------------------------
764 // functions prototypes
765 // ----------------------------------------------------------------------------
767 static void ShowBitmap(const wxBitmap
& bitmap
);
770 static void ShowMetaFile(const wxMetaFile
& metafile
);
771 #endif // wxUSE_METAFILES
773 // ----------------------------------------------------------------------------
774 // IDs for the menu commands
775 // ----------------------------------------------------------------------------
793 Menu_Shape_New
= 500,
796 Menu_ShapeClipboard_Copy
,
797 Menu_ShapeClipboard_Paste
,
801 BEGIN_EVENT_TABLE(DnDFrame
, wxFrame
)
802 EVT_MENU(Menu_Quit
, DnDFrame::OnQuit
)
803 EVT_MENU(Menu_About
, DnDFrame::OnAbout
)
804 EVT_MENU(Menu_Drag
, DnDFrame::OnDrag
)
805 EVT_MENU(Menu_DragMoveDef
, DnDFrame::OnDragMoveByDefault
)
806 EVT_MENU(Menu_DragMoveAllow
,DnDFrame::OnDragMoveAllow
)
807 EVT_MENU(Menu_NewFrame
, DnDFrame::OnNewFrame
)
808 EVT_MENU(Menu_Help
, DnDFrame::OnHelp
)
809 EVT_MENU(Menu_Clear
, DnDFrame::OnLogClear
)
810 EVT_MENU(Menu_Copy
, DnDFrame::OnCopy
)
811 EVT_MENU(Menu_Paste
, DnDFrame::OnPaste
)
812 EVT_MENU(Menu_CopyBitmap
, DnDFrame::OnCopyBitmap
)
813 EVT_MENU(Menu_PasteBitmap
,DnDFrame::OnPasteBitmap
)
815 EVT_MENU(Menu_PasteMFile
, DnDFrame::OnPasteMetafile
)
816 #endif // wxUSE_METAFILES
817 EVT_MENU(Menu_CopyFiles
, DnDFrame::OnCopyFiles
)
819 EVT_UPDATE_UI(Menu_DragMoveDef
, DnDFrame::OnUpdateUIMoveByDefault
)
821 EVT_UPDATE_UI(Menu_Paste
, DnDFrame::OnUpdateUIPasteText
)
822 EVT_UPDATE_UI(Menu_PasteBitmap
, DnDFrame::OnUpdateUIPasteBitmap
)
824 EVT_LEFT_DOWN( DnDFrame::OnLeftDown
)
825 EVT_RIGHT_DOWN( DnDFrame::OnRightDown
)
826 EVT_PAINT( DnDFrame::OnPaint
)
827 EVT_SIZE( DnDFrame::OnSize
)
830 BEGIN_EVENT_TABLE(DnDShapeFrame
, wxFrame
)
831 EVT_MENU(Menu_Shape_New
, DnDShapeFrame::OnNewShape
)
832 EVT_MENU(Menu_Shape_Edit
, DnDShapeFrame::OnEditShape
)
833 EVT_MENU(Menu_Shape_Clear
, DnDShapeFrame::OnClearShape
)
835 EVT_MENU(Menu_ShapeClipboard_Copy
, DnDShapeFrame::OnCopyShape
)
836 EVT_MENU(Menu_ShapeClipboard_Paste
, DnDShapeFrame::OnPasteShape
)
838 EVT_UPDATE_UI(Menu_ShapeClipboard_Copy
, DnDShapeFrame::OnUpdateUICopy
)
839 EVT_UPDATE_UI(Menu_ShapeClipboard_Paste
, DnDShapeFrame::OnUpdateUIPaste
)
841 EVT_LEFT_DOWN(DnDShapeFrame::OnDrag
)
843 EVT_PAINT(DnDShapeFrame::OnPaint
)
846 BEGIN_EVENT_TABLE(DnDShapeDialog
, wxDialog
)
847 EVT_BUTTON(Button_Colour
, DnDShapeDialog::OnColour
)
850 BEGIN_EVENT_TABLE(DnDCanvasBitmap
, wxScrolledWindow
)
851 EVT_PAINT(DnDCanvasBitmap::OnPaint
)
855 BEGIN_EVENT_TABLE(DnDCanvasMetafile
, wxScrolledWindow
)
856 EVT_PAINT(DnDCanvasMetafile::OnPaint
)
858 #endif // wxUSE_METAFILES
860 #endif // wxUSE_DRAG_AND_DROP
862 // ============================================================================
864 // ============================================================================
866 // `Main program' equivalent, creating windows and returning main app frame
867 bool DnDApp::OnInit()
869 #if wxUSE_DRAG_AND_DROP
870 // switch on trace messages
871 #if defined(__WXGTK__)
872 wxLog::AddTraceMask(_T("clipboard"));
873 #elif defined(__WXMSW__)
874 wxLog::AddTraceMask(wxTRACE_OleCalls
);
878 wxImage::AddHandler( new wxPNGHandler
);
881 // under X we usually want to use the primary selection by default (which
882 // is shared with other apps)
883 wxTheClipboard
->UsePrimarySelection();
885 // create the main frame window
886 DnDFrame
*frame
= new DnDFrame((wxFrame
*) NULL
,
887 _T("Drag-and-Drop/Clipboard wxWindows Sample"),
897 wxMessageBox( _T("This sample has to be compiled with wxUSE_DRAG_AND_DROP"), _T("Building error"), wxOK
);
899 #endif // wxUSE_DRAG_AND_DROP
902 #if wxUSE_DRAG_AND_DROP
904 DnDFrame::DnDFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
)
905 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
)),
906 m_strText(_T("wxWindows drag & drop works :-)"))
909 // frame icon and status bar
910 SetIcon(wxICON(mondrian
));
915 wxMenu
*file_menu
= new wxMenu
;
916 file_menu
->Append(Menu_Drag
, _T("&Test drag..."));
917 file_menu
->AppendCheckItem(Menu_DragMoveDef
, _T("&Move by default"));
918 file_menu
->AppendCheckItem(Menu_DragMoveAllow
, _T("&Allow moving"));
919 file_menu
->AppendSeparator();
920 file_menu
->Append(Menu_NewFrame
, _T("&New frame\tCtrl-N"));
921 file_menu
->AppendSeparator();
922 file_menu
->Append(Menu_Quit
, _T("E&xit\tCtrl-Q"));
924 wxMenu
*log_menu
= new wxMenu
;
925 log_menu
->Append(Menu_Clear
, _T("Clear\tCtrl-L"));
927 wxMenu
*help_menu
= new wxMenu
;
928 help_menu
->Append(Menu_Help
, _T("&Help..."));
929 help_menu
->AppendSeparator();
930 help_menu
->Append(Menu_About
, _T("&About"));
932 wxMenu
*clip_menu
= new wxMenu
;
933 clip_menu
->Append(Menu_Copy
, _T("&Copy text\tCtrl-C"));
934 clip_menu
->Append(Menu_Paste
, _T("&Paste text\tCtrl-V"));
935 clip_menu
->AppendSeparator();
936 clip_menu
->Append(Menu_CopyBitmap
, _T("Copy &bitmap\tCtrl-Shift-C"));
937 clip_menu
->Append(Menu_PasteBitmap
, _T("Paste b&itmap\tCtrl-Shift-V"));
939 clip_menu
->AppendSeparator();
940 clip_menu
->Append(Menu_PasteMFile
, _T("Paste &metafile\tCtrl-M"));
941 #endif // wxUSE_METAFILES
942 clip_menu
->AppendSeparator();
943 clip_menu
->Append(Menu_CopyFiles
, _T("Copy &files\tCtrl-F"));
945 wxMenuBar
*menu_bar
= new wxMenuBar
;
946 menu_bar
->Append(file_menu
, _T("&File"));
947 menu_bar
->Append(log_menu
, _T("&Log"));
948 menu_bar
->Append(clip_menu
, _T("&Clipboard"));
949 menu_bar
->Append(help_menu
, _T("&Help"));
951 SetMenuBar(menu_bar
);
953 // make a panel with 3 subwindows
955 wxSize
size(400, 200);
957 wxString
strFile(_T("Drop files here!")), strText(_T("Drop text on me"));
959 m_ctrlFile
= new wxListBox(this, -1, pos
, size
, 1, &strFile
,
960 wxLB_HSCROLL
| wxLB_ALWAYS_SB
);
961 m_ctrlText
= new wxListBox(this, -1, pos
, size
, 1, &strText
,
962 wxLB_HSCROLL
| wxLB_ALWAYS_SB
);
964 m_ctrlLog
= new wxTextCtrl(this, -1, _T(""), pos
, size
,
965 wxTE_MULTILINE
| wxTE_READONLY
|
968 // redirect log messages to the text window
969 m_pLog
= new wxLogTextCtrl(m_ctrlLog
);
970 m_pLogPrev
= wxLog::SetActiveTarget(m_pLog
);
972 // associate drop targets with the controls
973 m_ctrlFile
->SetDropTarget(new DnDFile(m_ctrlFile
));
974 m_ctrlText
->SetDropTarget(new DnDText(m_ctrlText
));
975 m_ctrlLog
->SetDropTarget(new URLDropTarget
);
977 wxLayoutConstraints
*c
;
980 c
= new wxLayoutConstraints
;
981 c
->left
.SameAs(this, wxLeft
);
982 c
->top
.SameAs(this, wxTop
);
983 c
->right
.PercentOf(this, wxRight
, 50);
984 c
->height
.PercentOf(this, wxHeight
, 30);
985 m_ctrlFile
->SetConstraints(c
);
988 c
= new wxLayoutConstraints
;
989 c
->left
.SameAs (m_ctrlFile
, wxRight
);
990 c
->top
.SameAs (this, wxTop
);
991 c
->right
.SameAs (this, wxRight
);
992 c
->height
.PercentOf(this, wxHeight
, 30);
993 m_ctrlText
->SetConstraints(c
);
995 // Lower text control
996 c
= new wxLayoutConstraints
;
997 c
->left
.SameAs (this, wxLeft
);
998 c
->right
.SameAs (this, wxRight
);
999 c
->height
.PercentOf(this, wxHeight
, 50);
1000 c
->top
.SameAs(m_ctrlText
, wxBottom
);
1001 m_ctrlLog
->SetConstraints(c
);
1003 SetAutoLayout(TRUE
);
1005 // copy data by default but allow moving it as well
1006 m_moveByDefault
= FALSE
;
1008 menu_bar
->Check(Menu_DragMoveAllow
, TRUE
);
1011 void DnDFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
1016 void DnDFrame::OnSize(wxSizeEvent
& event
)
1023 void DnDFrame::OnPaint(wxPaintEvent
& WXUNUSED(event
))
1027 GetClientSize( &w
, &h
);
1030 // dc.Clear(); -- this kills wxGTK
1031 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
, FALSE
, _T("charter") ) );
1032 dc
.DrawText( _T("Drag text from here!"), 100, h
-50 );
1035 void DnDFrame::OnUpdateUIMoveByDefault(wxUpdateUIEvent
& event
)
1037 // only can move by default if moving is allowed at all
1038 event
.Enable(m_moveAllow
);
1041 void DnDFrame::OnUpdateUIPasteText(wxUpdateUIEvent
& event
)
1044 // too many trace messages if we don't do it - this function is called
1049 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
1052 void DnDFrame::OnUpdateUIPasteBitmap(wxUpdateUIEvent
& event
)
1055 // too many trace messages if we don't do it - this function is called
1060 event
.Enable( wxTheClipboard
->IsSupported(wxDF_BITMAP
) );
1063 void DnDFrame::OnNewFrame(wxCommandEvent
& WXUNUSED(event
))
1065 (new DnDShapeFrame(this))->Show(TRUE
);
1067 wxLogStatus(this, wxT("Double click the new frame to select a shape for it"));
1070 void DnDFrame::OnDrag(wxCommandEvent
& WXUNUSED(event
))
1072 wxString strText
= wxGetTextFromUser
1074 _T("After you enter text in this dialog, press any mouse\n")
1075 _T("button in the bottom (empty) part of the frame and \n")
1076 _T("drag it anywhere - you will be in fact dragging the\n")
1077 _T("text object containing this text"),
1078 _T("Please enter some text"), m_strText
, this
1081 m_strText
= strText
;
1084 void DnDFrame::OnDragMoveByDefault(wxCommandEvent
& event
)
1086 m_moveByDefault
= event
.IsChecked();
1089 void DnDFrame::OnDragMoveAllow(wxCommandEvent
& event
)
1091 m_moveAllow
= event
.IsChecked();
1094 void DnDFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
1096 wxMessageBox(_T("Drag-&-Drop Demo\n")
1097 _T("Please see \"Help|Help...\" for details\n")
1098 _T("Copyright (c) 1998 Vadim Zeitlin"),
1100 wxICON_INFORMATION
| wxOK
,
1104 void DnDFrame::OnHelp(wxCommandEvent
& /* event */)
1106 wxMessageDialog
dialog(this,
1107 _T("This small program demonstrates drag & drop support in wxWindows. The program window\n")
1108 _T("consists of 3 parts: the bottom pane is for debug messages, so that you can see what's\n")
1109 _T("going on inside. The top part is split into 2 listboxes, the left one accepts files\n")
1110 _T("and the right one accepts text.\n")
1112 _T("To test wxDropTarget: open wordpad (write.exe), select some text in it and drag it to\n")
1113 _T("the right listbox (you'll notice the usual visual feedback, i.e. the cursor will change).\n")
1114 _T("Also, try dragging some files (you can select several at once) from Windows Explorer (or \n")
1115 _T("File Manager) to the left pane. Hold down Ctrl/Shift keys when you drop text (doesn't \n")
1116 _T("work with files) and see what changes.\n")
1118 _T("To test wxDropSource: just press any mouse button on the empty zone of the window and drag\n")
1119 _T("it to wordpad or any other droptarget accepting text (and of course you can just drag it\n")
1120 _T("to the right pane). Due to a lot of trace messages, the cursor might take some time to \n")
1121 _T("change, don't release the mouse button until it does. You can change the string being\n")
1122 _T("dragged in in \"File|Test drag...\" dialog.\n")
1125 _T("Please send all questions/bug reports/suggestions &c to \n")
1126 _T("Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>"),
1132 void DnDFrame::OnLogClear(wxCommandEvent
& /* event */ )
1135 m_ctrlText
->Clear();
1136 m_ctrlFile
->Clear();
1139 void DnDFrame::OnLeftDown(wxMouseEvent
&WXUNUSED(event
) )
1141 if ( !m_strText
.IsEmpty() )
1143 // start drag operation
1144 wxTextDataObject
textData(m_strText
);
1146 wxFileDataObject textData;
1147 textData.AddFile( "/file1.txt" );
1148 textData.AddFile( "/file2.txt" );
1150 wxDropSource
source(textData
, this,
1151 wxDROP_ICON(dnd_copy
),
1152 wxDROP_ICON(dnd_move
),
1153 wxDROP_ICON(dnd_none
));
1156 if ( m_moveByDefault
)
1157 flags
|= wxDrag_DefaultMove
;
1158 else if ( m_moveAllow
)
1159 flags
|= wxDrag_AllowMove
;
1162 switch ( source
.DoDragDrop(flags
) )
1164 case wxDragError
: pc
= _T("Error!"); break;
1165 case wxDragNone
: pc
= _T("Nothing"); break;
1166 case wxDragCopy
: pc
= _T("Copied"); break;
1167 case wxDragMove
: pc
= _T("Moved"); break;
1168 case wxDragCancel
: pc
= _T("Cancelled"); break;
1169 default: pc
= _T("Huh?"); break;
1172 SetStatusText(wxString(_T("Drag result: ")) + pc
);
1176 void DnDFrame::OnRightDown(wxMouseEvent
&event
)
1178 wxMenu
menu(_T("Dnd sample menu"));
1180 menu
.Append(Menu_Drag
, _T("&Test drag..."));
1181 menu
.AppendSeparator();
1182 menu
.Append(Menu_About
, _T("&About"));
1184 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
1187 DnDFrame::~DnDFrame()
1189 if ( m_pLog
!= NULL
) {
1190 if ( wxLog::SetActiveTarget(m_pLogPrev
) == m_pLog
)
1195 // ---------------------------------------------------------------------------
1197 // ---------------------------------------------------------------------------
1199 void DnDFrame::OnCopyBitmap(wxCommandEvent
& WXUNUSED(event
))
1201 // PNG support is not always compiled in under Windows, so use BMP there
1203 wxFileDialog
dialog(this, _T("Open a PNG file"), _T(""), _T(""), _T("PNG files (*.png)|*.png"), 0);
1205 wxFileDialog
dialog(this, _T("Open a BMP file"), _T(""), _T(""), _T("BMP files (*.bmp)|*.bmp"), 0);
1208 if (dialog
.ShowModal() != wxID_OK
)
1210 wxLogMessage( _T("Aborted file open") );
1214 if (dialog
.GetPath().IsEmpty())
1216 wxLogMessage( _T("Returned empty string.") );
1220 if (!wxFileExists(dialog
.GetPath()))
1222 wxLogMessage( _T("File doesn't exist.") );
1227 image
.LoadFile( dialog
.GetPath(),
1236 wxLogError( _T("Invalid image file...") );
1240 wxLogStatus( _T("Decoding image file...") );
1243 wxBitmap
bitmap( image
);
1245 if ( !wxTheClipboard
->Open() )
1247 wxLogError(_T("Can't open clipboard."));
1252 wxLogMessage( _T("Creating wxBitmapDataObject...") );
1255 if ( !wxTheClipboard
->AddData(new wxBitmapDataObject(bitmap
)) )
1257 wxLogError(_T("Can't copy image to the clipboard."));
1261 wxLogMessage(_T("Image has been put on the clipboard.") );
1262 wxLogMessage(_T("You can paste it now and look at it.") );
1265 wxTheClipboard
->Close();
1268 void DnDFrame::OnPasteBitmap(wxCommandEvent
& WXUNUSED(event
))
1270 if ( !wxTheClipboard
->Open() )
1272 wxLogError(_T("Can't open clipboard."));
1277 if ( !wxTheClipboard
->IsSupported(wxDF_BITMAP
) )
1279 wxLogWarning(_T("No bitmap on clipboard"));
1281 wxTheClipboard
->Close();
1285 wxBitmapDataObject data
;
1286 if ( !wxTheClipboard
->GetData(data
) )
1288 wxLogError(_T("Can't paste bitmap from the clipboard"));
1292 const wxBitmap
& bmp
= data
.GetBitmap();
1294 wxLogMessage(_T("Bitmap %dx%d pasted from the clipboard"),
1295 bmp
.GetWidth(), bmp
.GetHeight());
1299 wxTheClipboard
->Close();
1304 void DnDFrame::OnPasteMetafile(wxCommandEvent
& WXUNUSED(event
))
1306 if ( !wxTheClipboard
->Open() )
1308 wxLogError(_T("Can't open clipboard."));
1313 if ( !wxTheClipboard
->IsSupported(wxDF_METAFILE
) )
1315 wxLogWarning(_T("No metafile on clipboard"));
1319 wxMetaFileDataObject data
;
1320 if ( !wxTheClipboard
->GetData(data
) )
1322 wxLogError(_T("Can't paste metafile from the clipboard"));
1326 const wxMetaFile
& mf
= data
.GetMetafile();
1328 wxLogMessage(_T("Metafile %dx%d pasted from the clipboard"),
1329 mf
.GetWidth(), mf
.GetHeight());
1335 wxTheClipboard
->Close();
1338 #endif // wxUSE_METAFILES
1340 // ----------------------------------------------------------------------------
1342 // ----------------------------------------------------------------------------
1344 void DnDFrame::OnCopyFiles(wxCommandEvent
& WXUNUSED(event
))
1347 wxFileDialog
dialog(this, _T("Select a file to copy"), _T(""), _T(""),
1348 _T("All files (*.*)|*.*"), 0);
1350 wxArrayString filenames
;
1351 while ( dialog
.ShowModal() == wxID_OK
)
1353 filenames
.Add(dialog
.GetPath());
1356 if ( !filenames
.IsEmpty() )
1358 wxFileDataObject
*dobj
= new wxFileDataObject
;
1359 size_t count
= filenames
.GetCount();
1360 for ( size_t n
= 0; n
< count
; n
++ )
1362 dobj
->AddFile(filenames
[n
]);
1365 wxClipboardLocker locker
;
1368 wxLogError(wxT("Can't open clipboard"));
1372 if ( !wxTheClipboard
->AddData(dobj
) )
1374 wxLogError(wxT("Can't copy file(s) to the clipboard"));
1378 wxLogStatus(this, wxT("%d file%s copied to the clipboard"),
1379 count
, count
== 1 ? wxT("") : wxT("s"));
1385 wxLogStatus(this, wxT("Aborted"));
1388 wxLogError(wxT("Sorry, not implemented"));
1392 // ---------------------------------------------------------------------------
1394 // ---------------------------------------------------------------------------
1396 void DnDFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1398 if ( !wxTheClipboard
->Open() )
1400 wxLogError(_T("Can't open clipboard."));
1405 if ( !wxTheClipboard
->AddData(new wxTextDataObject(m_strText
)) )
1407 wxLogError(_T("Can't copy data to the clipboard"));
1411 wxLogMessage(_T("Text '%s' put on the clipboard"), m_strText
.c_str());
1414 wxTheClipboard
->Close();
1417 void DnDFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1419 if ( !wxTheClipboard
->Open() )
1421 wxLogError(_T("Can't open clipboard."));
1426 if ( !wxTheClipboard
->IsSupported(wxDF_TEXT
) )
1428 wxLogWarning(_T("No text data on clipboard"));
1430 wxTheClipboard
->Close();
1434 wxTextDataObject text
;
1435 if ( !wxTheClipboard
->GetData(text
) )
1437 wxLogError(_T("Can't paste data from the clipboard"));
1441 wxLogMessage(_T("Text '%s' pasted from the clipboard"),
1442 text
.GetText().c_str());
1445 wxTheClipboard
->Close();
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, -1, 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, -1, wxT("&Attributes") );
1497 wxStaticBoxSizer
* attrSizer
= new wxStaticBoxSizer( box
, wxHORIZONTAL
);
1498 wxFlexGridSizer
* xywhSizer
= new wxFlexGridSizer( 4, 2 );
1502 st
= new wxStaticText( this, -1, wxT("Position &X:") );
1503 m_textX
= new wxTextCtrl( this, -1, wxEmptyString
, wxDefaultPosition
,
1505 xywhSizer
->Add( st
, 1, wxGROW
|wxALL
, 2 );
1506 xywhSizer
->Add( m_textX
, 1, wxGROW
|wxALL
, 2 );
1508 st
= new wxStaticText( this, -1, wxT("Size &width:") );
1509 m_textW
= new wxTextCtrl( this, -1, wxEmptyString
, wxDefaultPosition
,
1511 xywhSizer
->Add( st
, 1, wxGROW
|wxALL
, 2 );
1512 xywhSizer
->Add( m_textW
, 1, wxGROW
|wxALL
, 2 );
1514 st
= new wxStaticText( this, -1, wxT("&Y:") );
1515 m_textY
= new wxTextCtrl( this, -1, 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, -1, wxT("&height:") );
1521 m_textH
= new wxTextCtrl( this, -1, 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 SetAutoLayout( TRUE
);
1541 SetSizer( topSizer
);
1542 topSizer
->Fit( this );
1545 DnDShape
*DnDShapeDialog::GetShape() const
1547 switch ( m_shapeKind
)
1550 case DnDShape::None
: return NULL
;
1551 case DnDShape::Triangle
: return new DnDTriangularShape(m_pos
, m_size
, m_col
);
1552 case DnDShape::Rectangle
: return new DnDRectangularShape(m_pos
, m_size
, m_col
);
1553 case DnDShape::Ellipse
: return new DnDEllipticShape(m_pos
, m_size
, m_col
);
1557 bool DnDShapeDialog::TransferDataToWindow()
1562 m_radio
->SetSelection(m_shape
->GetKind());
1563 m_pos
= m_shape
->GetPosition();
1564 m_size
= m_shape
->GetSize();
1565 m_col
= m_shape
->GetColour();
1569 m_radio
->SetSelection(DnDShape::None
);
1570 m_pos
= wxPoint(1, 1);
1571 m_size
= wxSize(100, 100);
1574 m_textX
->SetValue(wxString() << m_pos
.x
);
1575 m_textY
->SetValue(wxString() << m_pos
.y
);
1576 m_textW
->SetValue(wxString() << m_size
.x
);
1577 m_textH
->SetValue(wxString() << m_size
.y
);
1582 bool DnDShapeDialog::TransferDataFromWindow()
1584 m_shapeKind
= (DnDShape::Kind
)m_radio
->GetSelection();
1586 m_pos
.x
= wxAtoi(m_textX
->GetValue());
1587 m_pos
.y
= wxAtoi(m_textY
->GetValue());
1588 m_size
.x
= wxAtoi(m_textW
->GetValue());
1589 m_size
.y
= wxAtoi(m_textH
->GetValue());
1591 if ( !m_pos
.x
|| !m_pos
.y
|| !m_size
.x
|| !m_size
.y
)
1593 wxMessageBox(_T("All sizes and positions should be non null!"),
1594 _T("Invalid shape"), wxICON_HAND
| wxOK
, this);
1602 void DnDShapeDialog::OnColour(wxCommandEvent
& WXUNUSED(event
))
1605 data
.SetChooseFull(TRUE
);
1606 for (int i
= 0; i
< 16; i
++)
1608 wxColour
colour(i
*16, i
*16, i
*16);
1609 data
.SetCustomColour(i
, colour
);
1612 wxColourDialog
dialog(this, &data
);
1613 if ( dialog
.ShowModal() == wxID_OK
)
1615 m_col
= dialog
.GetColourData().GetColour();
1619 // ----------------------------------------------------------------------------
1621 // ----------------------------------------------------------------------------
1623 DnDShapeFrame
*DnDShapeFrame::ms_lastDropTarget
= NULL
;
1625 DnDShapeFrame::DnDShapeFrame(wxFrame
*parent
)
1626 : wxFrame(parent
, -1, _T("Shape Frame"),
1627 wxDefaultPosition
, wxSize(250, 150))
1631 wxMenu
*menuShape
= new wxMenu
;
1632 menuShape
->Append(Menu_Shape_New
, _T("&New default shape\tCtrl-S"));
1633 menuShape
->Append(Menu_Shape_Edit
, _T("&Edit shape\tCtrl-E"));
1634 menuShape
->AppendSeparator();
1635 menuShape
->Append(Menu_Shape_Clear
, _T("&Clear shape\tCtrl-L"));
1637 wxMenu
*menuClipboard
= new wxMenu
;
1638 menuClipboard
->Append(Menu_ShapeClipboard_Copy
, _T("&Copy\tCtrl-C"));
1639 menuClipboard
->Append(Menu_ShapeClipboard_Paste
, _T("&Paste\tCtrl-V"));
1641 wxMenuBar
*menubar
= new wxMenuBar
;
1642 menubar
->Append(menuShape
, _T("&Shape"));
1643 menubar
->Append(menuClipboard
, _T("&Clipboard"));
1645 SetMenuBar(menubar
);
1647 SetStatusText(_T("Press Ctrl-S to create a new shape"));
1649 SetDropTarget(new DnDShapeDropTarget(this));
1653 SetBackgroundColour(*wxWHITE
);
1656 DnDShapeFrame::~DnDShapeFrame()
1662 void DnDShapeFrame::SetShape(DnDShape
*shape
)
1671 void DnDShapeFrame::OnDrag(wxMouseEvent
& event
)
1680 // start drag operation
1681 DnDShapeDataObject
shapeData(m_shape
);
1682 wxDropSource
source(shapeData
, this);
1684 const wxChar
*pc
= NULL
;
1685 switch ( source
.DoDragDrop(TRUE
) )
1689 wxLogError(wxT("An error occured during drag and drop operation"));
1693 SetStatusText(_T("Nothing happened"));
1702 if ( ms_lastDropTarget
!= this )
1704 // don't delete the shape if we dropped it on ourselves!
1710 SetStatusText(_T("Drag and drop operation cancelled"));
1716 SetStatusText(wxString(_T("Shape successfully ")) + pc
);
1718 //else: status text already set
1721 void DnDShapeFrame::OnDrop(wxCoord x
, wxCoord y
, DnDShape
*shape
)
1723 ms_lastDropTarget
= this;
1728 s
.Printf(wxT("Shape dropped at (%d, %d)"), pt
.x
, pt
.y
);
1735 void DnDShapeFrame::OnEditShape(wxCommandEvent
& WXUNUSED(event
))
1737 DnDShapeDialog
dlg(this, m_shape
);
1738 if ( dlg
.ShowModal() == wxID_OK
)
1740 SetShape(dlg
.GetShape());
1744 SetStatusText(_T("You can now drag the shape to another frame"));
1749 void DnDShapeFrame::OnNewShape(wxCommandEvent
& WXUNUSED(event
))
1751 SetShape(new DnDEllipticShape(wxPoint(10, 10), wxSize(80, 60), *wxRED
));
1753 SetStatusText(_T("You can now drag the shape to another frame"));
1756 void DnDShapeFrame::OnClearShape(wxCommandEvent
& WXUNUSED(event
))
1761 void DnDShapeFrame::OnCopyShape(wxCommandEvent
& WXUNUSED(event
))
1765 wxClipboardLocker clipLocker
;
1768 wxLogError(wxT("Can't open the clipboard"));
1773 wxTheClipboard
->AddData(new DnDShapeDataObject(m_shape
));
1777 void DnDShapeFrame::OnPasteShape(wxCommandEvent
& WXUNUSED(event
))
1779 wxClipboardLocker clipLocker
;
1782 wxLogError(wxT("Can't open the clipboard"));
1787 DnDShapeDataObject
shapeDataObject(NULL
);
1788 if ( wxTheClipboard
->GetData(shapeDataObject
) )
1790 SetShape(shapeDataObject
.GetShape());
1794 wxLogStatus(wxT("No shape on the clipboard"));
1798 void DnDShapeFrame::OnUpdateUICopy(wxUpdateUIEvent
& event
)
1800 event
.Enable( m_shape
!= NULL
);
1803 void DnDShapeFrame::OnUpdateUIPaste(wxUpdateUIEvent
& event
)
1805 event
.Enable( wxTheClipboard
->IsSupported(wxDataFormat(shapeFormatId
)) );
1808 void DnDShapeFrame::OnPaint(wxPaintEvent
& event
)
1822 // ----------------------------------------------------------------------------
1824 // ----------------------------------------------------------------------------
1826 DnDShape
*DnDShape::New(const void *buf
)
1828 const ShapeDump
& dump
= *(const ShapeDump
*)buf
;
1832 return new DnDTriangularShape(wxPoint(dump
.x
, dump
.y
),
1833 wxSize(dump
.w
, dump
.h
),
1834 wxColour(dump
.r
, dump
.g
, dump
.b
));
1837 return new DnDRectangularShape(wxPoint(dump
.x
, dump
.y
),
1838 wxSize(dump
.w
, dump
.h
),
1839 wxColour(dump
.r
, dump
.g
, dump
.b
));
1842 return new DnDEllipticShape(wxPoint(dump
.x
, dump
.y
),
1843 wxSize(dump
.w
, dump
.h
),
1844 wxColour(dump
.r
, dump
.g
, dump
.b
));
1847 wxFAIL_MSG(wxT("invalid shape!"));
1852 // ----------------------------------------------------------------------------
1853 // DnDShapeDataObject
1854 // ----------------------------------------------------------------------------
1858 void DnDShapeDataObject::CreateMetaFile() const
1860 wxPoint pos
= m_shape
->GetPosition();
1861 wxSize size
= m_shape
->GetSize();
1863 wxMetaFileDC
dcMF(wxEmptyString
, pos
.x
+ size
.x
, pos
.y
+ size
.y
);
1865 m_shape
->Draw(dcMF
);
1867 wxMetafile
*mf
= dcMF
.Close();
1869 DnDShapeDataObject
*self
= (DnDShapeDataObject
*)this; // const_cast
1870 self
->m_dobjMetaFile
.SetMetafile(*mf
);
1871 self
->m_hasMetaFile
= TRUE
;
1876 #endif // wxUSE_METAFILES
1878 void DnDShapeDataObject::CreateBitmap() const
1880 wxPoint pos
= m_shape
->GetPosition();
1881 wxSize size
= m_shape
->GetSize();
1882 int x
= pos
.x
+ size
.x
,
1884 wxBitmap
bitmap(x
, y
);
1886 dc
.SelectObject(bitmap
);
1887 dc
.SetBrush(wxBrush(wxT("white"), wxSOLID
));
1890 dc
.SelectObject(wxNullBitmap
);
1892 DnDShapeDataObject
*self
= (DnDShapeDataObject
*)this; // const_cast
1893 self
->m_dobjBitmap
.SetBitmap(bitmap
);
1894 self
->m_hasBitmap
= TRUE
;
1897 // ----------------------------------------------------------------------------
1899 // ----------------------------------------------------------------------------
1901 static void ShowBitmap(const wxBitmap
& bitmap
)
1903 wxFrame
*frame
= new wxFrame(NULL
, -1, _T("Bitmap view"));
1904 frame
->CreateStatusBar();
1905 DnDCanvasBitmap
*canvas
= new DnDCanvasBitmap(frame
);
1906 canvas
->SetBitmap(bitmap
);
1908 int w
= bitmap
.GetWidth(),
1909 h
= bitmap
.GetHeight();
1910 frame
->SetStatusText(wxString::Format(_T("%dx%d"), w
, h
));
1912 frame
->SetClientSize(w
> 100 ? 100 : w
, h
> 100 ? 100 : h
);
1918 static void ShowMetaFile(const wxMetaFile
& metafile
)
1920 wxFrame
*frame
= new wxFrame(NULL
, -1, _T("Metafile view"));
1921 frame
->CreateStatusBar();
1922 DnDCanvasMetafile
*canvas
= new DnDCanvasMetafile(frame
);
1923 canvas
->SetMetafile(metafile
);
1925 wxSize size
= metafile
.GetSize();
1926 frame
->SetStatusText(wxString::Format(_T("%dx%d"), size
.x
, size
.y
));
1928 frame
->SetClientSize(size
.x
> 100 ? 100 : size
.x
,
1929 size
.y
> 100 ? 100 : size
.y
);
1933 #endif // wxUSE_METAFILES
1935 #endif // wxUSE_DRAG_AND_DROP