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
);
224 void OnLogClear(wxCommandEvent
& event
);
227 void OnCopy(wxCommandEvent
& event
);
228 void OnPaste(wxCommandEvent
& event
);
230 void OnCopyBitmap(wxCommandEvent
& event
);
231 void OnPasteBitmap(wxCommandEvent
& event
);
234 void OnPasteMetafile(wxCommandEvent
& event
);
235 #endif // wxUSE_METAFILES
237 void OnCopyFiles(wxCommandEvent
& event
);
239 void OnLeftDown(wxMouseEvent
& event
);
240 void OnRightDown(wxMouseEvent
& event
);
242 void OnUpdateUIMoveByDefault(wxUpdateUIEvent
& event
);
244 void OnUpdateUIPasteText(wxUpdateUIEvent
& event
);
245 void OnUpdateUIPasteBitmap(wxUpdateUIEvent
& event
);
247 DECLARE_EVENT_TABLE()
251 wxListBox
*m_ctrlFile
,
255 wxTextCtrl
*m_ctrlLog
;
261 // move the text by default (or copy)?
262 bool m_moveByDefault
;
264 // allow moving the text at all?
271 // ----------------------------------------------------------------------------
272 // A shape is an example of application-specific data which may be transported
273 // via drag-and-drop or clipboard: in our case, we have different geometric
274 // shapes, each one with its own colour and position
275 // ----------------------------------------------------------------------------
277 #if wxUSE_DRAG_AND_DROP
290 DnDShape(const wxPoint
& pos
,
293 : m_pos(pos
), m_size(size
), m_col(col
)
297 // this is for debugging - lets us see when exactly an object is freed
298 // (this may be later than you think if it's on the clipboard, for example)
299 virtual ~DnDShape() { }
301 // the functions used for drag-and-drop: they dump and restore a shape into
302 // some bitwise-copiable data (might use streams too...)
303 // ------------------------------------------------------------------------
305 // restore from buffer
306 static DnDShape
*New(const void *buf
);
308 virtual size_t GetDataSize() const
310 return sizeof(ShapeDump
);
313 virtual void GetDataHere(void *buf
) const
315 ShapeDump
& dump
= *(ShapeDump
*)buf
;
320 dump
.r
= m_col
.Red();
321 dump
.g
= m_col
.Green();
322 dump
.b
= m_col
.Blue();
327 const wxPoint
& GetPosition() const { return m_pos
; }
328 const wxColour
& GetColour() const { return m_col
; }
329 const wxSize
& GetSize() const { return m_size
; }
331 void Move(const wxPoint
& pos
) { m_pos
= pos
; }
333 // to implement in derived classes
334 virtual Kind
GetKind() const = 0;
336 virtual void Draw(wxDC
& dc
)
338 dc
.SetPen(wxPen(m_col
, 1, wxSOLID
));
342 //get a point 1 up and 1 left, otherwise the mid-point of a triangle is on the line
343 wxPoint
GetCentre() const
344 { return wxPoint(m_pos
.x
+ m_size
.x
/ 2 - 1, m_pos
.y
+ m_size
.y
/ 2 - 1); }
348 int x
, y
, // position
359 class DnDTriangularShape
: public DnDShape
362 DnDTriangularShape(const wxPoint
& pos
,
365 : DnDShape(pos
, size
, col
)
367 wxLogMessage(wxT("DnDTriangularShape is being created"));
370 virtual ~DnDTriangularShape()
372 wxLogMessage(wxT("DnDTriangularShape is being deleted"));
375 virtual Kind
GetKind() const { return Triangle
; }
376 virtual void Draw(wxDC
& dc
)
380 // well, it's a bit difficult to describe a triangle by position and
381 // size, but we're not doing geometry here, do we? ;-)
383 wxPoint
p2(m_pos
.x
+ m_size
.x
, m_pos
.y
);
384 wxPoint
p3(m_pos
.x
, m_pos
.y
+ m_size
.y
);
390 //works in multicolor modes; on GTK (at least) will fail in 16-bit color
391 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
392 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
396 class DnDRectangularShape
: public DnDShape
399 DnDRectangularShape(const wxPoint
& pos
,
402 : DnDShape(pos
, size
, col
)
404 wxLogMessage(wxT("DnDRectangularShape is being created"));
407 virtual ~DnDRectangularShape()
409 wxLogMessage(wxT("DnDRectangularShape is being deleted"));
412 virtual Kind
GetKind() const { return Rectangle
; }
413 virtual void Draw(wxDC
& dc
)
418 wxPoint
p2(p1
.x
+ m_size
.x
, p1
.y
);
419 wxPoint
p3(p2
.x
, p2
.y
+ m_size
.y
);
420 wxPoint
p4(p1
.x
, p3
.y
);
427 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
428 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
432 class DnDEllipticShape
: public DnDShape
435 DnDEllipticShape(const wxPoint
& pos
,
438 : DnDShape(pos
, size
, col
)
440 wxLogMessage(wxT("DnDEllipticShape is being created"));
443 virtual ~DnDEllipticShape()
445 wxLogMessage(wxT("DnDEllipticShape is being deleted"));
448 virtual Kind
GetKind() const { return Ellipse
; }
449 virtual void Draw(wxDC
& dc
)
453 dc
.DrawEllipse(m_pos
, m_size
);
455 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
456 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
460 // ----------------------------------------------------------------------------
461 // A wxDataObject specialisation for the application-specific data
462 // ----------------------------------------------------------------------------
464 static const wxChar
*shapeFormatId
= wxT("wxShape");
466 class DnDShapeDataObject
: public wxDataObject
469 // ctor doesn't copy the pointer, so it shouldn't go away while this object
471 DnDShapeDataObject(DnDShape
*shape
= (DnDShape
*)NULL
)
475 // we need to copy the shape because the one we're handled may be
476 // deleted while it's still on the clipboard (for example) - and we
477 // reuse the serialisation methods here to copy it
478 void *buf
= malloc(shape
->DnDShape::GetDataSize());
479 shape
->GetDataHere(buf
);
480 m_shape
= DnDShape::New(buf
);
490 // this string should uniquely identify our format, but is otherwise
492 m_formatShape
.SetId(shapeFormatId
);
494 // we don't draw the shape to a bitmap until it's really needed (i.e.
495 // we're asked to do so)
498 m_hasMetaFile
= false;
499 #endif // wxUSE_METAFILES
502 virtual ~DnDShapeDataObject() { delete m_shape
; }
504 // after a call to this function, the shape is owned by the caller and it
505 // is responsible for deleting it!
507 // NB: a better solution would be to make DnDShapes ref counted and this
508 // is what should probably be done in a real life program, otherwise
509 // the ownership problems become too complicated really fast
512 DnDShape
*shape
= m_shape
;
514 m_shape
= (DnDShape
*)NULL
;
517 m_hasMetaFile
= false;
518 #endif // wxUSE_METAFILES
523 // implement base class pure virtuals
524 // ----------------------------------
526 virtual wxDataFormat
GetPreferredFormat(Direction
WXUNUSED(dir
)) const
528 return m_formatShape
;
531 virtual size_t GetFormatCount(Direction dir
) const
533 // our custom format is supported by both GetData() and SetData()
537 // but the bitmap format(s) are only supported for output
538 nFormats
+= m_dobjBitmap
.GetFormatCount(dir
);
541 nFormats
+= m_dobjMetaFile
.GetFormatCount(dir
);
542 #endif // wxUSE_METAFILES
548 virtual void GetAllFormats(wxDataFormat
*formats
, Direction dir
) const
550 formats
[0] = m_formatShape
;
553 // in Get direction we additionally support bitmaps and metafiles
555 m_dobjBitmap
.GetAllFormats(&formats
[1], dir
);
558 // don't assume that m_dobjBitmap has only 1 format
559 m_dobjMetaFile
.GetAllFormats(&formats
[1 +
560 m_dobjBitmap
.GetFormatCount(dir
)], dir
);
561 #endif // wxUSE_METAFILES
565 virtual size_t GetDataSize(const wxDataFormat
& format
) const
567 if ( format
== m_formatShape
)
569 return m_shape
->GetDataSize();
572 else if ( m_dobjMetaFile
.IsSupported(format
) )
574 if ( !m_hasMetaFile
)
577 return m_dobjMetaFile
.GetDataSize(format
);
579 #endif // wxUSE_METAFILES
582 wxASSERT_MSG( m_dobjBitmap
.IsSupported(format
),
583 wxT("unexpected format") );
588 return m_dobjBitmap
.GetDataSize();
592 virtual bool GetDataHere(const wxDataFormat
& format
, void *pBuf
) const
594 if ( format
== m_formatShape
)
596 m_shape
->GetDataHere(pBuf
);
601 else if ( m_dobjMetaFile
.IsSupported(format
) )
603 if ( !m_hasMetaFile
)
606 return m_dobjMetaFile
.GetDataHere(format
, pBuf
);
608 #endif // wxUSE_METAFILES
611 wxASSERT_MSG( m_dobjBitmap
.IsSupported(format
),
612 wxT("unexpected format") );
617 return m_dobjBitmap
.GetDataHere(pBuf
);
621 virtual bool SetData(const wxDataFormat
& format
,
622 size_t WXUNUSED(len
), const void *buf
)
624 wxCHECK_MSG( format
== m_formatShape
, false,
625 wxT( "unsupported format") );
628 m_shape
= DnDShape::New(buf
);
630 // the shape has changed
634 m_hasMetaFile
= false;
635 #endif // wxUSE_METAFILES
641 // creates a bitmap and assigns it to m_dobjBitmap (also sets m_hasBitmap)
642 void CreateBitmap() const;
644 void CreateMetaFile() const;
645 #endif // wxUSE_METAFILES
647 wxDataFormat m_formatShape
; // our custom format
649 wxBitmapDataObject m_dobjBitmap
; // it handles bitmaps
650 bool m_hasBitmap
; // true if m_dobjBitmap has valid bitmap
653 wxMetaFileDataObject m_dobjMetaFile
;// handles metafiles
654 bool m_hasMetaFile
; // true if we have valid metafile
655 #endif // wxUSE_METAFILES
657 DnDShape
*m_shape
; // our data
660 // ----------------------------------------------------------------------------
661 // A dialog to edit shape properties
662 // ----------------------------------------------------------------------------
664 class DnDShapeDialog
: public wxDialog
667 DnDShapeDialog(wxFrame
*parent
, DnDShape
*shape
);
669 DnDShape
*GetShape() const;
671 virtual bool TransferDataToWindow();
672 virtual bool TransferDataFromWindow();
674 void OnColour(wxCommandEvent
& event
);
681 DnDShape::Kind m_shapeKind
;
693 DECLARE_EVENT_TABLE()
696 // ----------------------------------------------------------------------------
697 // A frame for the shapes which can be drag-and-dropped between frames
698 // ----------------------------------------------------------------------------
700 class DnDShapeFrame
: public wxFrame
703 DnDShapeFrame(wxFrame
*parent
);
706 void SetShape(DnDShape
*shape
);
707 virtual bool SetShape(const wxRegion
®ion
)
709 return wxFrame::SetShape( region
);
713 void OnNewShape(wxCommandEvent
& event
);
714 void OnEditShape(wxCommandEvent
& event
);
715 void OnClearShape(wxCommandEvent
& event
);
717 void OnCopyShape(wxCommandEvent
& event
);
718 void OnPasteShape(wxCommandEvent
& event
);
720 void OnUpdateUICopy(wxUpdateUIEvent
& event
);
721 void OnUpdateUIPaste(wxUpdateUIEvent
& event
);
723 void OnDrag(wxMouseEvent
& event
);
724 void OnPaint(wxPaintEvent
& event
);
725 void OnDrop(wxCoord x
, wxCoord y
, DnDShape
*shape
);
730 static DnDShapeFrame
*ms_lastDropTarget
;
732 DECLARE_EVENT_TABLE()
735 // ----------------------------------------------------------------------------
736 // wxDropTarget derivation for DnDShapes
737 // ----------------------------------------------------------------------------
739 class DnDShapeDropTarget
: public wxDropTarget
742 DnDShapeDropTarget(DnDShapeFrame
*frame
)
743 : wxDropTarget(new DnDShapeDataObject
)
748 // override base class (pure) virtuals
749 virtual wxDragResult
OnEnter(wxCoord x
, wxCoord y
, wxDragResult def
)
752 m_frame
->SetStatusText(_T("Mouse entered the frame"));
753 #endif // wxUSE_STATUSBAR
754 return OnDragOver(x
, y
, def
);
756 virtual void OnLeave()
759 m_frame
->SetStatusText(_T("Mouse left the frame"));
760 #endif // wxUSE_STATUSBAR
762 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
766 wxLogError(wxT("Failed to get drag and drop data"));
771 m_frame
->OnDrop(x
, y
,
772 ((DnDShapeDataObject
*)GetDataObject())->GetShape());
778 DnDShapeFrame
*m_frame
;
781 #endif // wxUSE_DRAG_AND_DROP
783 // ----------------------------------------------------------------------------
784 // functions prototypes
785 // ----------------------------------------------------------------------------
787 static void ShowBitmap(const wxBitmap
& bitmap
);
790 static void ShowMetaFile(const wxMetaFile
& metafile
);
791 #endif // wxUSE_METAFILES
793 // ----------------------------------------------------------------------------
794 // IDs for the menu commands
795 // ----------------------------------------------------------------------------
813 Menu_Shape_New
= 500,
816 Menu_ShapeClipboard_Copy
,
817 Menu_ShapeClipboard_Paste
,
821 BEGIN_EVENT_TABLE(DnDFrame
, wxFrame
)
822 EVT_MENU(Menu_Quit
, DnDFrame::OnQuit
)
823 EVT_MENU(Menu_About
, DnDFrame::OnAbout
)
824 EVT_MENU(Menu_Drag
, DnDFrame::OnDrag
)
825 EVT_MENU(Menu_DragMoveDef
, DnDFrame::OnDragMoveByDefault
)
826 EVT_MENU(Menu_DragMoveAllow
,DnDFrame::OnDragMoveAllow
)
827 EVT_MENU(Menu_NewFrame
, DnDFrame::OnNewFrame
)
828 EVT_MENU(Menu_Help
, DnDFrame::OnHelp
)
830 EVT_MENU(Menu_Clear
, DnDFrame::OnLogClear
)
832 EVT_MENU(Menu_Copy
, DnDFrame::OnCopy
)
833 EVT_MENU(Menu_Paste
, DnDFrame::OnPaste
)
834 EVT_MENU(Menu_CopyBitmap
, DnDFrame::OnCopyBitmap
)
835 EVT_MENU(Menu_PasteBitmap
,DnDFrame::OnPasteBitmap
)
837 EVT_MENU(Menu_PasteMFile
, DnDFrame::OnPasteMetafile
)
838 #endif // wxUSE_METAFILES
839 EVT_MENU(Menu_CopyFiles
, DnDFrame::OnCopyFiles
)
841 EVT_UPDATE_UI(Menu_DragMoveDef
, DnDFrame::OnUpdateUIMoveByDefault
)
843 EVT_UPDATE_UI(Menu_Paste
, DnDFrame::OnUpdateUIPasteText
)
844 EVT_UPDATE_UI(Menu_PasteBitmap
, DnDFrame::OnUpdateUIPasteBitmap
)
846 EVT_LEFT_DOWN( DnDFrame::OnLeftDown
)
847 EVT_RIGHT_DOWN( DnDFrame::OnRightDown
)
848 EVT_PAINT( DnDFrame::OnPaint
)
849 EVT_SIZE( DnDFrame::OnSize
)
852 #if wxUSE_DRAG_AND_DROP
854 BEGIN_EVENT_TABLE(DnDShapeFrame
, wxFrame
)
855 EVT_MENU(Menu_Shape_New
, DnDShapeFrame::OnNewShape
)
856 EVT_MENU(Menu_Shape_Edit
, DnDShapeFrame::OnEditShape
)
857 EVT_MENU(Menu_Shape_Clear
, DnDShapeFrame::OnClearShape
)
859 EVT_MENU(Menu_ShapeClipboard_Copy
, DnDShapeFrame::OnCopyShape
)
860 EVT_MENU(Menu_ShapeClipboard_Paste
, DnDShapeFrame::OnPasteShape
)
862 EVT_UPDATE_UI(Menu_ShapeClipboard_Copy
, DnDShapeFrame::OnUpdateUICopy
)
863 EVT_UPDATE_UI(Menu_ShapeClipboard_Paste
, DnDShapeFrame::OnUpdateUIPaste
)
865 EVT_LEFT_DOWN(DnDShapeFrame::OnDrag
)
867 EVT_PAINT(DnDShapeFrame::OnPaint
)
870 BEGIN_EVENT_TABLE(DnDShapeDialog
, wxDialog
)
871 EVT_BUTTON(Button_Colour
, DnDShapeDialog::OnColour
)
874 #endif // wxUSE_DRAG_AND_DROP
876 BEGIN_EVENT_TABLE(DnDCanvasBitmap
, wxScrolledWindow
)
877 EVT_PAINT(DnDCanvasBitmap::OnPaint
)
881 BEGIN_EVENT_TABLE(DnDCanvasMetafile
, wxScrolledWindow
)
882 EVT_PAINT(DnDCanvasMetafile::OnPaint
)
884 #endif // wxUSE_METAFILES
886 #endif // wxUSE_DRAG_AND_DROP
888 // ============================================================================
890 // ============================================================================
892 // `Main program' equivalent, creating windows and returning main app frame
893 bool DnDApp::OnInit()
895 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
896 // switch on trace messages
898 #if defined(__WXGTK__)
899 wxLog::AddTraceMask(_T("clipboard"));
900 #elif defined(__WXMSW__)
901 wxLog::AddTraceMask(wxTRACE_OleCalls
);
906 wxImage::AddHandler( new wxPNGHandler
);
909 // under X we usually want to use the primary selection by default (which
910 // is shared with other apps)
911 wxTheClipboard
->UsePrimarySelection();
913 // create the main frame window
914 DnDFrame
*frame
= new DnDFrame((wxFrame
*) NULL
,
915 _T("Drag-and-Drop/Clipboard wxWidgets Sample"),
925 wxMessageBox( _T("This sample has to be compiled with wxUSE_DRAG_AND_DROP"), _T("Building error"), wxOK
);
927 #endif // wxUSE_DRAG_AND_DROP
930 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
932 DnDFrame::DnDFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
)
933 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
)),
934 m_strText(_T("wxWidgets drag & drop works :-)"))
937 // frame icon and status bar
938 SetIcon(wxICON(mondrian
));
942 #endif // wxUSE_STATUSBAR
945 wxMenu
*file_menu
= new wxMenu
;
946 file_menu
->Append(Menu_Drag
, _T("&Test drag..."));
947 file_menu
->AppendCheckItem(Menu_DragMoveDef
, _T("&Move by default"));
948 file_menu
->AppendCheckItem(Menu_DragMoveAllow
, _T("&Allow moving"));
949 file_menu
->AppendSeparator();
950 file_menu
->Append(Menu_NewFrame
, _T("&New frame\tCtrl-N"));
951 file_menu
->AppendSeparator();
952 file_menu
->Append(Menu_Quit
, _T("E&xit\tCtrl-Q"));
955 wxMenu
*log_menu
= new wxMenu
;
956 log_menu
->Append(Menu_Clear
, _T("Clear\tCtrl-L"));
959 wxMenu
*help_menu
= new wxMenu
;
960 help_menu
->Append(Menu_Help
, _T("&Help..."));
961 help_menu
->AppendSeparator();
962 help_menu
->Append(Menu_About
, _T("&About"));
964 wxMenu
*clip_menu
= new wxMenu
;
965 clip_menu
->Append(Menu_Copy
, _T("&Copy text\tCtrl-C"));
966 clip_menu
->Append(Menu_Paste
, _T("&Paste text\tCtrl-V"));
967 clip_menu
->AppendSeparator();
968 clip_menu
->Append(Menu_CopyBitmap
, _T("Copy &bitmap\tCtrl-Shift-C"));
969 clip_menu
->Append(Menu_PasteBitmap
, _T("Paste b&itmap\tCtrl-Shift-V"));
971 clip_menu
->AppendSeparator();
972 clip_menu
->Append(Menu_PasteMFile
, _T("Paste &metafile\tCtrl-M"));
973 #endif // wxUSE_METAFILES
974 clip_menu
->AppendSeparator();
975 clip_menu
->Append(Menu_CopyFiles
, _T("Copy &files\tCtrl-F"));
977 wxMenuBar
*menu_bar
= new wxMenuBar
;
978 menu_bar
->Append(file_menu
, _T("&File"));
980 menu_bar
->Append(log_menu
, _T("&Log"));
982 menu_bar
->Append(clip_menu
, _T("&Clipboard"));
983 menu_bar
->Append(help_menu
, _T("&Help"));
985 SetMenuBar(menu_bar
);
987 // make a panel with 3 subwindows
988 wxString
strFile(_T("Drop files here!")), strText(_T("Drop text on me"));
990 m_ctrlFile
= new wxListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, 1, &strFile
,
991 wxLB_HSCROLL
| wxLB_ALWAYS_SB
);
992 m_ctrlText
= new wxListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, 1, &strText
,
993 wxLB_HSCROLL
| wxLB_ALWAYS_SB
);
996 m_ctrlLog
= new wxTextCtrl(this, wxID_ANY
, _T(""), wxDefaultPosition
, wxDefaultSize
,
997 wxTE_MULTILINE
| wxTE_READONLY
|
1000 // redirect log messages to the text window
1001 m_pLog
= new wxLogTextCtrl(m_ctrlLog
);
1002 m_pLogPrev
= wxLog::SetActiveTarget(m_pLog
);
1005 #if wxUSE_DRAG_AND_DROP
1006 // associate drop targets with the controls
1007 m_ctrlFile
->SetDropTarget(new DnDFile(m_ctrlFile
));
1008 m_ctrlText
->SetDropTarget(new DnDText(m_ctrlText
));
1010 m_ctrlLog
->SetDropTarget(new URLDropTarget
);
1012 #endif // wxUSE_DRAG_AND_DROP
1014 wxBoxSizer
*m_sizer_top
= new wxBoxSizer( wxHORIZONTAL
);
1015 m_sizer_top
->Add(m_ctrlFile
, 1, wxEXPAND
);
1016 m_sizer_top
->Add(m_ctrlText
, 1, wxEXPAND
);
1018 wxBoxSizer
*m_sizer
= new wxBoxSizer( wxVERTICAL
);
1019 m_sizer
->Add(m_sizer_top
, 1, wxEXPAND
);
1021 m_sizer
->Add(m_ctrlLog
, 1, wxEXPAND
);
1025 SetSizer( m_sizer
);
1026 m_sizer
->SetSizeHints( this );
1028 // copy data by default but allow moving it as well
1029 m_moveByDefault
= false;
1031 menu_bar
->Check(Menu_DragMoveAllow
, true);
1034 void DnDFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
1039 void DnDFrame::OnSize(wxSizeEvent
& event
)
1046 void DnDFrame::OnPaint(wxPaintEvent
& WXUNUSED(event
))
1050 GetClientSize( &w
, &h
);
1053 // dc.Clear(); -- this kills wxGTK
1054 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
, false, _T("charter") ) );
1055 dc
.DrawText( _T("Drag text from here!"), 100, h
-50 );
1058 void DnDFrame::OnUpdateUIMoveByDefault(wxUpdateUIEvent
& event
)
1060 // only can move by default if moving is allowed at all
1061 event
.Enable(m_moveAllow
);
1064 void DnDFrame::OnUpdateUIPasteText(wxUpdateUIEvent
& event
)
1067 // too many trace messages if we don't do it - this function is called
1072 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
1075 void DnDFrame::OnUpdateUIPasteBitmap(wxUpdateUIEvent
& event
)
1078 // too many trace messages if we don't do it - this function is called
1083 event
.Enable( wxTheClipboard
->IsSupported(wxDF_BITMAP
) );
1086 void DnDFrame::OnNewFrame(wxCommandEvent
& WXUNUSED(event
))
1088 #if wxUSE_DRAG_AND_DROP
1089 (new DnDShapeFrame(this))->Show(true);
1091 wxLogStatus(this, wxT("Double click the new frame to select a shape for it"));
1092 #endif // wxUSE_DRAG_AND_DROP
1095 void DnDFrame::OnDrag(wxCommandEvent
& WXUNUSED(event
))
1097 #if wxUSE_DRAG_AND_DROP
1098 wxString strText
= wxGetTextFromUser
1100 _T("After you enter text in this dialog, press any mouse\n")
1101 _T("button in the bottom (empty) part of the frame and \n")
1102 _T("drag it anywhere - you will be in fact dragging the\n")
1103 _T("text object containing this text"),
1104 _T("Please enter some text"), m_strText
, this
1107 m_strText
= strText
;
1108 #endif // wxUSE_DRAG_AND_DROP
1111 void DnDFrame::OnDragMoveByDefault(wxCommandEvent
& event
)
1113 m_moveByDefault
= event
.IsChecked();
1116 void DnDFrame::OnDragMoveAllow(wxCommandEvent
& event
)
1118 m_moveAllow
= event
.IsChecked();
1121 void DnDFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
1123 wxMessageBox(_T("Drag-&-Drop Demo\n")
1124 _T("Please see \"Help|Help...\" for details\n")
1125 _T("Copyright (c) 1998 Vadim Zeitlin"),
1127 wxICON_INFORMATION
| wxOK
,
1131 void DnDFrame::OnHelp(wxCommandEvent
& /* event */)
1133 wxMessageDialog
dialog(this,
1134 _T("This small program demonstrates drag & drop support in wxWidgets. The program window\n")
1135 _T("consists of 3 parts: the bottom pane is for debug messages, so that you can see what's\n")
1136 _T("going on inside. The top part is split into 2 listboxes, the left one accepts files\n")
1137 _T("and the right one accepts text.\n")
1139 _T("To test wxDropTarget: open wordpad (write.exe), select some text in it and drag it to\n")
1140 _T("the right listbox (you'll notice the usual visual feedback, i.e. the cursor will change).\n")
1141 _T("Also, try dragging some files (you can select several at once) from Windows Explorer (or \n")
1142 _T("File Manager) to the left pane. Hold down Ctrl/Shift keys when you drop text (doesn't \n")
1143 _T("work with files) and see what changes.\n")
1145 _T("To test wxDropSource: just press any mouse button on the empty zone of the window and drag\n")
1146 _T("it to wordpad or any other droptarget accepting text (and of course you can just drag it\n")
1147 _T("to the right pane). Due to a lot of trace messages, the cursor might take some time to \n")
1148 _T("change, don't release the mouse button until it does. You can change the string being\n")
1149 _T("dragged in in \"File|Test drag...\" dialog.\n")
1152 _T("Please send all questions/bug reports/suggestions &c to \n")
1153 _T("Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>"),
1160 void DnDFrame::OnLogClear(wxCommandEvent
& /* event */ )
1163 m_ctrlText
->Clear();
1164 m_ctrlFile
->Clear();
1168 void DnDFrame::OnLeftDown(wxMouseEvent
&WXUNUSED(event
) )
1170 #if wxUSE_DRAG_AND_DROP
1171 if ( !m_strText
.IsEmpty() )
1173 // start drag operation
1174 wxTextDataObject
textData(m_strText
);
1176 wxFileDataObject textData;
1177 textData.AddFile( "/file1.txt" );
1178 textData.AddFile( "/file2.txt" );
1180 wxDropSource
source(textData
, this,
1181 wxDROP_ICON(dnd_copy
),
1182 wxDROP_ICON(dnd_move
),
1183 wxDROP_ICON(dnd_none
));
1186 if ( m_moveByDefault
)
1187 flags
|= wxDrag_DefaultMove
;
1188 else if ( m_moveAllow
)
1189 flags
|= wxDrag_AllowMove
;
1191 wxDragResult result
= source
.DoDragDrop(flags
);
1197 case wxDragError
: pc
= _T("Error!"); break;
1198 case wxDragNone
: pc
= _T("Nothing"); break;
1199 case wxDragCopy
: pc
= _T("Copied"); break;
1200 case wxDragMove
: pc
= _T("Moved"); break;
1201 case wxDragCancel
: pc
= _T("Cancelled"); break;
1202 default: pc
= _T("Huh?"); break;
1205 SetStatusText(wxString(_T("Drag result: ")) + pc
);
1207 wxUnusedVar(result
);
1208 #endif // wxUSE_STATUSBAR
1210 #endif // wxUSE_DRAG_AND_DROP
1213 void DnDFrame::OnRightDown(wxMouseEvent
&event
)
1215 wxMenu
menu(_T("Dnd sample menu"));
1217 menu
.Append(Menu_Drag
, _T("&Test drag..."));
1218 menu
.AppendSeparator();
1219 menu
.Append(Menu_About
, _T("&About"));
1221 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
1224 DnDFrame::~DnDFrame()
1227 if ( m_pLog
!= NULL
) {
1228 if ( wxLog::SetActiveTarget(m_pLogPrev
) == m_pLog
)
1234 // ---------------------------------------------------------------------------
1236 // ---------------------------------------------------------------------------
1238 void DnDFrame::OnCopyBitmap(wxCommandEvent
& WXUNUSED(event
))
1240 // PNG support is not always compiled in under Windows, so use BMP there
1242 wxFileDialog
dialog(this, _T("Open a PNG file"), _T(""), _T(""), _T("PNG files (*.png)|*.png"), 0);
1244 wxFileDialog
dialog(this, _T("Open a BMP file"), _T(""), _T(""), _T("BMP files (*.bmp)|*.bmp"), 0);
1247 if (dialog
.ShowModal() != wxID_OK
)
1249 wxLogMessage( _T("Aborted file open") );
1253 if (dialog
.GetPath().IsEmpty())
1255 wxLogMessage( _T("Returned empty string.") );
1259 if (!wxFileExists(dialog
.GetPath()))
1261 wxLogMessage( _T("File doesn't exist.") );
1266 image
.LoadFile( dialog
.GetPath(),
1275 wxLogError( _T("Invalid image file...") );
1279 wxLogStatus( _T("Decoding image file...") );
1282 wxBitmap
bitmap( image
);
1284 if ( !wxTheClipboard
->Open() )
1286 wxLogError(_T("Can't open clipboard."));
1291 wxLogMessage( _T("Creating wxBitmapDataObject...") );
1294 if ( !wxTheClipboard
->AddData(new wxBitmapDataObject(bitmap
)) )
1296 wxLogError(_T("Can't copy image to the clipboard."));
1300 wxLogMessage(_T("Image has been put on the clipboard.") );
1301 wxLogMessage(_T("You can paste it now and look at it.") );
1304 wxTheClipboard
->Close();
1307 void DnDFrame::OnPasteBitmap(wxCommandEvent
& WXUNUSED(event
))
1309 if ( !wxTheClipboard
->Open() )
1311 wxLogError(_T("Can't open clipboard."));
1316 if ( !wxTheClipboard
->IsSupported(wxDF_BITMAP
) )
1318 wxLogWarning(_T("No bitmap on clipboard"));
1320 wxTheClipboard
->Close();
1324 wxBitmapDataObject data
;
1325 if ( !wxTheClipboard
->GetData(data
) )
1327 wxLogError(_T("Can't paste bitmap from the clipboard"));
1331 const wxBitmap
& bmp
= data
.GetBitmap();
1333 wxLogMessage(_T("Bitmap %dx%d pasted from the clipboard"),
1334 bmp
.GetWidth(), bmp
.GetHeight());
1338 wxTheClipboard
->Close();
1343 void DnDFrame::OnPasteMetafile(wxCommandEvent
& WXUNUSED(event
))
1345 if ( !wxTheClipboard
->Open() )
1347 wxLogError(_T("Can't open clipboard."));
1352 if ( !wxTheClipboard
->IsSupported(wxDF_METAFILE
) )
1354 wxLogWarning(_T("No metafile on clipboard"));
1358 wxMetaFileDataObject data
;
1359 if ( !wxTheClipboard
->GetData(data
) )
1361 wxLogError(_T("Can't paste metafile from the clipboard"));
1365 const wxMetaFile
& mf
= data
.GetMetafile();
1367 wxLogMessage(_T("Metafile %dx%d pasted from the clipboard"),
1368 mf
.GetWidth(), mf
.GetHeight());
1374 wxTheClipboard
->Close();
1377 #endif // wxUSE_METAFILES
1379 // ----------------------------------------------------------------------------
1381 // ----------------------------------------------------------------------------
1383 void DnDFrame::OnCopyFiles(wxCommandEvent
& WXUNUSED(event
))
1386 wxFileDialog
dialog(this, _T("Select a file to copy"), _T(""), _T(""),
1387 _T("All files (*.*)|*.*"), 0);
1389 wxArrayString filenames
;
1390 while ( dialog
.ShowModal() == wxID_OK
)
1392 filenames
.Add(dialog
.GetPath());
1395 if ( !filenames
.IsEmpty() )
1397 wxFileDataObject
*dobj
= new wxFileDataObject
;
1398 size_t count
= filenames
.GetCount();
1399 for ( size_t n
= 0; n
< count
; n
++ )
1401 dobj
->AddFile(filenames
[n
]);
1404 wxClipboardLocker locker
;
1407 wxLogError(wxT("Can't open clipboard"));
1411 if ( !wxTheClipboard
->AddData(dobj
) )
1413 wxLogError(wxT("Can't copy file(s) to the clipboard"));
1417 wxLogStatus(this, wxT("%d file%s copied to the clipboard"),
1418 count
, count
== 1 ? wxT("") : wxT("s"));
1424 wxLogStatus(this, wxT("Aborted"));
1427 wxLogError(wxT("Sorry, not implemented"));
1431 // ---------------------------------------------------------------------------
1433 // ---------------------------------------------------------------------------
1435 void DnDFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1437 if ( !wxTheClipboard
->Open() )
1439 wxLogError(_T("Can't open clipboard."));
1444 if ( !wxTheClipboard
->AddData(new wxTextDataObject(m_strText
)) )
1446 wxLogError(_T("Can't copy data to the clipboard"));
1450 wxLogMessage(_T("Text '%s' put on the clipboard"), m_strText
.c_str());
1453 wxTheClipboard
->Close();
1456 void DnDFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1458 if ( !wxTheClipboard
->Open() )
1460 wxLogError(_T("Can't open clipboard."));
1465 if ( !wxTheClipboard
->IsSupported(wxDF_TEXT
) )
1467 wxLogWarning(_T("No text data on clipboard"));
1469 wxTheClipboard
->Close();
1473 wxTextDataObject text
;
1474 if ( !wxTheClipboard
->GetData(text
) )
1476 wxLogError(_T("Can't paste data from the clipboard"));
1480 wxLogMessage(_T("Text '%s' pasted from the clipboard"),
1481 text
.GetText().c_str());
1484 wxTheClipboard
->Close();
1487 #if wxUSE_DRAG_AND_DROP
1489 // ----------------------------------------------------------------------------
1490 // Notifications called by the base class
1491 // ----------------------------------------------------------------------------
1493 bool DnDText::OnDropText(wxCoord
, wxCoord
, const wxString
& text
)
1495 m_pOwner
->Append(text
);
1500 bool DnDFile::OnDropFiles(wxCoord
, wxCoord
, const wxArrayString
& filenames
)
1502 size_t nFiles
= filenames
.GetCount();
1504 str
.Printf( _T("%d files dropped"), (int)nFiles
);
1505 m_pOwner
->Append(str
);
1506 for ( size_t n
= 0; n
< nFiles
; n
++ ) {
1507 m_pOwner
->Append(filenames
[n
]);
1513 // ----------------------------------------------------------------------------
1515 // ----------------------------------------------------------------------------
1517 DnDShapeDialog::DnDShapeDialog(wxFrame
*parent
, DnDShape
*shape
)
1518 :wxDialog( parent
, 6001, wxT("Choose Shape"), wxPoint( 10, 10 ),
1520 wxRAISED_BORDER
|wxCAPTION
|wxTHICK_FRAME
|wxSYSTEM_MENU
)
1523 wxBoxSizer
* topSizer
= new wxBoxSizer( wxVERTICAL
);
1526 wxBoxSizer
* shapesSizer
= new wxBoxSizer( wxHORIZONTAL
);
1527 const wxString choices
[] = { wxT("None"), wxT("Triangle"),
1528 wxT("Rectangle"), wxT("Ellipse") };
1530 m_radio
= new wxRadioBox( this, wxID_ANY
, wxT("&Shape"),
1531 wxDefaultPosition
, wxDefaultSize
, 4, choices
, 4,
1532 wxRA_SPECIFY_COLS
);
1533 shapesSizer
->Add( m_radio
, 0, wxGROW
|wxALL
, 5 );
1534 topSizer
->Add( shapesSizer
, 0, wxALL
, 2 );
1537 wxStaticBox
* box
= new wxStaticBox( this, wxID_ANY
, wxT("&Attributes") );
1538 wxStaticBoxSizer
* attrSizer
= new wxStaticBoxSizer( box
, wxHORIZONTAL
);
1539 wxFlexGridSizer
* xywhSizer
= new wxFlexGridSizer( 4, 2 );
1543 st
= new wxStaticText( this, wxID_ANY
, wxT("Position &X:") );
1544 m_textX
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1546 xywhSizer
->Add( st
, 1, wxGROW
|wxALL
, 2 );
1547 xywhSizer
->Add( m_textX
, 1, wxGROW
|wxALL
, 2 );
1549 st
= new wxStaticText( this, wxID_ANY
, wxT("Size &width:") );
1550 m_textW
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1552 xywhSizer
->Add( st
, 1, wxGROW
|wxALL
, 2 );
1553 xywhSizer
->Add( m_textW
, 1, wxGROW
|wxALL
, 2 );
1555 st
= new wxStaticText( this, wxID_ANY
, wxT("&Y:") );
1556 m_textY
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1558 xywhSizer
->Add( st
, 1, wxALL
|wxALIGN_RIGHT
, 2 );
1559 xywhSizer
->Add( m_textY
, 1, wxGROW
|wxALL
, 2 );
1561 st
= new wxStaticText( this, wxID_ANY
, wxT("&height:") );
1562 m_textH
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1564 xywhSizer
->Add( st
, 1, wxALL
|wxALIGN_RIGHT
, 2 );
1565 xywhSizer
->Add( m_textH
, 1, wxGROW
|wxALL
, 2 );
1567 wxButton
* col
= new wxButton( this, Button_Colour
, wxT("&Colour...") );
1568 attrSizer
->Add( xywhSizer
, 1, wxGROW
);
1569 attrSizer
->Add( col
, 0, wxALL
|wxALIGN_CENTRE_VERTICAL
, 2 );
1570 topSizer
->Add( attrSizer
, 0, wxGROW
|wxALL
, 5 );
1573 wxBoxSizer
* buttonSizer
= new wxBoxSizer( wxHORIZONTAL
);
1575 bt
= new wxButton( this, wxID_OK
, wxT("Ok") );
1576 buttonSizer
->Add( bt
, 0, wxALL
, 2 );
1577 bt
= new wxButton( this, wxID_CANCEL
, wxT("Cancel") );
1578 buttonSizer
->Add( bt
, 0, wxALL
, 2 );
1579 topSizer
->Add( buttonSizer
, 0, wxALL
|wxALIGN_RIGHT
, 2 );
1581 SetSizer( topSizer
);
1582 topSizer
->Fit( this );
1585 DnDShape
*DnDShapeDialog::GetShape() const
1587 switch ( m_shapeKind
)
1590 case DnDShape::None
: return NULL
;
1591 case DnDShape::Triangle
: return new DnDTriangularShape(m_pos
, m_size
, m_col
);
1592 case DnDShape::Rectangle
: return new DnDRectangularShape(m_pos
, m_size
, m_col
);
1593 case DnDShape::Ellipse
: return new DnDEllipticShape(m_pos
, m_size
, m_col
);
1597 bool DnDShapeDialog::TransferDataToWindow()
1602 m_radio
->SetSelection(m_shape
->GetKind());
1603 m_pos
= m_shape
->GetPosition();
1604 m_size
= m_shape
->GetSize();
1605 m_col
= m_shape
->GetColour();
1609 m_radio
->SetSelection(DnDShape::None
);
1610 m_pos
= wxPoint(1, 1);
1611 m_size
= wxSize(100, 100);
1614 m_textX
->SetValue(wxString() << m_pos
.x
);
1615 m_textY
->SetValue(wxString() << m_pos
.y
);
1616 m_textW
->SetValue(wxString() << m_size
.x
);
1617 m_textH
->SetValue(wxString() << m_size
.y
);
1622 bool DnDShapeDialog::TransferDataFromWindow()
1624 m_shapeKind
= (DnDShape::Kind
)m_radio
->GetSelection();
1626 m_pos
.x
= wxAtoi(m_textX
->GetValue());
1627 m_pos
.y
= wxAtoi(m_textY
->GetValue());
1628 m_size
.x
= wxAtoi(m_textW
->GetValue());
1629 m_size
.y
= wxAtoi(m_textH
->GetValue());
1631 if ( !m_pos
.x
|| !m_pos
.y
|| !m_size
.x
|| !m_size
.y
)
1633 wxMessageBox(_T("All sizes and positions should be non null!"),
1634 _T("Invalid shape"), wxICON_HAND
| wxOK
, this);
1642 void DnDShapeDialog::OnColour(wxCommandEvent
& WXUNUSED(event
))
1645 data
.SetChooseFull(true);
1646 for (int i
= 0; i
< 16; i
++)
1648 wxColour
colour(i
*16, i
*16, i
*16);
1649 data
.SetCustomColour(i
, colour
);
1652 wxColourDialog
dialog(this, &data
);
1653 if ( dialog
.ShowModal() == wxID_OK
)
1655 m_col
= dialog
.GetColourData().GetColour();
1659 // ----------------------------------------------------------------------------
1661 // ----------------------------------------------------------------------------
1663 DnDShapeFrame
*DnDShapeFrame::ms_lastDropTarget
= NULL
;
1665 DnDShapeFrame::DnDShapeFrame(wxFrame
*parent
)
1666 : wxFrame(parent
, wxID_ANY
, _T("Shape Frame"))
1670 #endif // wxUSE_STATUSBAR
1672 wxMenu
*menuShape
= new wxMenu
;
1673 menuShape
->Append(Menu_Shape_New
, _T("&New default shape\tCtrl-S"));
1674 menuShape
->Append(Menu_Shape_Edit
, _T("&Edit shape\tCtrl-E"));
1675 menuShape
->AppendSeparator();
1676 menuShape
->Append(Menu_Shape_Clear
, _T("&Clear shape\tCtrl-L"));
1678 wxMenu
*menuClipboard
= new wxMenu
;
1679 menuClipboard
->Append(Menu_ShapeClipboard_Copy
, _T("&Copy\tCtrl-C"));
1680 menuClipboard
->Append(Menu_ShapeClipboard_Paste
, _T("&Paste\tCtrl-V"));
1682 wxMenuBar
*menubar
= new wxMenuBar
;
1683 menubar
->Append(menuShape
, _T("&Shape"));
1684 menubar
->Append(menuClipboard
, _T("&Clipboard"));
1686 SetMenuBar(menubar
);
1689 SetStatusText(_T("Press Ctrl-S to create a new shape"));
1690 #endif // wxUSE_STATUSBAR
1692 SetDropTarget(new DnDShapeDropTarget(this));
1696 SetBackgroundColour(*wxWHITE
);
1699 DnDShapeFrame::~DnDShapeFrame()
1705 void DnDShapeFrame::SetShape(DnDShape
*shape
)
1714 void DnDShapeFrame::OnDrag(wxMouseEvent
& event
)
1723 // start drag operation
1724 DnDShapeDataObject
shapeData(m_shape
);
1725 wxDropSource
source(shapeData
, this);
1727 const wxChar
*pc
= NULL
;
1728 switch ( source
.DoDragDrop(true) )
1732 wxLogError(wxT("An error occured during drag and drop operation"));
1737 SetStatusText(_T("Nothing happened"));
1738 #endif // wxUSE_STATUSBAR
1747 if ( ms_lastDropTarget
!= this )
1749 // don't delete the shape if we dropped it on ourselves!
1756 SetStatusText(_T("Drag and drop operation cancelled"));
1757 #endif // wxUSE_STATUSBAR
1764 SetStatusText(wxString(_T("Shape successfully ")) + pc
);
1765 #endif // wxUSE_STATUSBAR
1767 //else: status text already set
1770 void DnDShapeFrame::OnDrop(wxCoord x
, wxCoord y
, DnDShape
*shape
)
1772 ms_lastDropTarget
= this;
1778 s
.Printf(wxT("Shape dropped at (%d, %d)"), pt
.x
, pt
.y
);
1780 #endif // wxUSE_STATUSBAR
1786 void DnDShapeFrame::OnEditShape(wxCommandEvent
& WXUNUSED(event
))
1788 DnDShapeDialog
dlg(this, m_shape
);
1789 if ( dlg
.ShowModal() == wxID_OK
)
1791 SetShape(dlg
.GetShape());
1796 SetStatusText(_T("You can now drag the shape to another frame"));
1798 #endif // wxUSE_STATUSBAR
1802 void DnDShapeFrame::OnNewShape(wxCommandEvent
& WXUNUSED(event
))
1804 SetShape(new DnDEllipticShape(wxPoint(10, 10), wxSize(80, 60), *wxRED
));
1807 SetStatusText(_T("You can now drag the shape to another frame"));
1808 #endif // wxUSE_STATUSBAR
1811 void DnDShapeFrame::OnClearShape(wxCommandEvent
& WXUNUSED(event
))
1816 void DnDShapeFrame::OnCopyShape(wxCommandEvent
& WXUNUSED(event
))
1820 wxClipboardLocker clipLocker
;
1823 wxLogError(wxT("Can't open the clipboard"));
1828 wxTheClipboard
->AddData(new DnDShapeDataObject(m_shape
));
1832 void DnDShapeFrame::OnPasteShape(wxCommandEvent
& WXUNUSED(event
))
1834 wxClipboardLocker clipLocker
;
1837 wxLogError(wxT("Can't open the clipboard"));
1842 DnDShapeDataObject
shapeDataObject(NULL
);
1843 if ( wxTheClipboard
->GetData(shapeDataObject
) )
1845 SetShape(shapeDataObject
.GetShape());
1849 wxLogStatus(wxT("No shape on the clipboard"));
1853 void DnDShapeFrame::OnUpdateUICopy(wxUpdateUIEvent
& event
)
1855 event
.Enable( m_shape
!= NULL
);
1858 void DnDShapeFrame::OnUpdateUIPaste(wxUpdateUIEvent
& event
)
1860 event
.Enable( wxTheClipboard
->IsSupported(wxDataFormat(shapeFormatId
)) );
1863 void DnDShapeFrame::OnPaint(wxPaintEvent
& event
)
1877 // ----------------------------------------------------------------------------
1879 // ----------------------------------------------------------------------------
1881 DnDShape
*DnDShape::New(const void *buf
)
1883 const ShapeDump
& dump
= *(const ShapeDump
*)buf
;
1887 return new DnDTriangularShape(wxPoint(dump
.x
, dump
.y
),
1888 wxSize(dump
.w
, dump
.h
),
1889 wxColour(dump
.r
, dump
.g
, dump
.b
));
1892 return new DnDRectangularShape(wxPoint(dump
.x
, dump
.y
),
1893 wxSize(dump
.w
, dump
.h
),
1894 wxColour(dump
.r
, dump
.g
, dump
.b
));
1897 return new DnDEllipticShape(wxPoint(dump
.x
, dump
.y
),
1898 wxSize(dump
.w
, dump
.h
),
1899 wxColour(dump
.r
, dump
.g
, dump
.b
));
1902 wxFAIL_MSG(wxT("invalid shape!"));
1907 // ----------------------------------------------------------------------------
1908 // DnDShapeDataObject
1909 // ----------------------------------------------------------------------------
1913 void DnDShapeDataObject::CreateMetaFile() const
1915 wxPoint pos
= m_shape
->GetPosition();
1916 wxSize size
= m_shape
->GetSize();
1918 wxMetaFileDC
dcMF(wxEmptyString
, pos
.x
+ size
.x
, pos
.y
+ size
.y
);
1920 m_shape
->Draw(dcMF
);
1922 wxMetafile
*mf
= dcMF
.Close();
1924 DnDShapeDataObject
*self
= (DnDShapeDataObject
*)this; // const_cast
1925 self
->m_dobjMetaFile
.SetMetafile(*mf
);
1926 self
->m_hasMetaFile
= true;
1931 #endif // wxUSE_METAFILES
1933 void DnDShapeDataObject::CreateBitmap() const
1935 wxPoint pos
= m_shape
->GetPosition();
1936 wxSize size
= m_shape
->GetSize();
1937 int x
= pos
.x
+ size
.x
,
1939 wxBitmap
bitmap(x
, y
);
1941 dc
.SelectObject(bitmap
);
1942 dc
.SetBrush(wxBrush(wxT("white"), wxSOLID
));
1945 dc
.SelectObject(wxNullBitmap
);
1947 DnDShapeDataObject
*self
= (DnDShapeDataObject
*)this; // const_cast
1948 self
->m_dobjBitmap
.SetBitmap(bitmap
);
1949 self
->m_hasBitmap
= true;
1952 #endif // wxUSE_DRAG_AND_DROP
1954 // ----------------------------------------------------------------------------
1956 // ----------------------------------------------------------------------------
1958 static void ShowBitmap(const wxBitmap
& bitmap
)
1960 wxFrame
*frame
= new wxFrame(NULL
, wxID_ANY
, _T("Bitmap view"));
1962 frame
->CreateStatusBar();
1963 #endif // wxUSE_STATUSBAR
1964 DnDCanvasBitmap
*canvas
= new DnDCanvasBitmap(frame
);
1965 canvas
->SetBitmap(bitmap
);
1967 int w
= bitmap
.GetWidth(),
1968 h
= bitmap
.GetHeight();
1970 frame
->SetStatusText(wxString::Format(_T("%dx%d"), w
, h
));
1971 #endif // wxUSE_STATUSBAR
1973 frame
->SetClientSize(w
> 100 ? 100 : w
, h
> 100 ? 100 : h
);
1979 static void ShowMetaFile(const wxMetaFile
& metafile
)
1981 wxFrame
*frame
= new wxFrame(NULL
, wxID_ANY
, _T("Metafile view"));
1982 frame
->CreateStatusBar();
1983 DnDCanvasMetafile
*canvas
= new DnDCanvasMetafile(frame
);
1984 canvas
->SetMetafile(metafile
);
1986 wxSize size
= metafile
.GetSize();
1987 frame
->SetStatusText(wxString::Format(_T("%dx%d"), size
.x
, size
.y
));
1989 frame
->SetClientSize(size
.x
> 100 ? 100 : size
.x
,
1990 size
.y
> 100 ? 100 : size
.y
);
1994 #endif // wxUSE_METAFILES
1996 #endif // wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD