1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Drag and drop sample
4 // Author: Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
23 #include "wx/dataobj.h"
25 #include "wx/clipbrd.h"
26 #include "wx/colordlg.h"
27 #include "wx/metafile.h"
28 #include "wx/dirctrl.h"
30 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
31 #include "../sample.xpm"
32 #if wxUSE_DRAG_AND_DROP
33 #include "dnd_copy.xpm"
34 #include "dnd_move.xpm"
35 #include "dnd_none.xpm"
39 #if wxUSE_DRAG_AND_DROP
41 // ----------------------------------------------------------------------------
42 // Derive two simple classes which just put in the listbox the strings (text or
43 // file names) we drop on them
44 // ----------------------------------------------------------------------------
46 class DnDText
: public wxTextDropTarget
49 DnDText(wxListBox
*pOwner
) { m_pOwner
= pOwner
; }
51 virtual bool OnDropText(wxCoord x
, wxCoord y
, const wxString
& text
);
57 class DnDFile
: public wxFileDropTarget
60 DnDFile(wxListBox
*pOwner
= NULL
) { m_pOwner
= pOwner
; }
62 virtual bool OnDropFiles(wxCoord x
, wxCoord y
,
63 const wxArrayString
& filenames
);
69 // ----------------------------------------------------------------------------
70 // Define a custom dtop target accepting URLs
71 // ----------------------------------------------------------------------------
73 class URLDropTarget
: public wxDropTarget
76 URLDropTarget() { SetDataObject(new wxURLDataObject
); }
78 void OnDropURL(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), const wxString
& text
)
80 // of course, a real program would do something more useful here...
81 wxMessageBox(text
, _T("wxDnD sample: got URL"),
82 wxICON_INFORMATION
| wxOK
);
85 // URLs can't be moved, only copied
86 virtual wxDragResult
OnDragOver(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
87 wxDragResult
WXUNUSED(def
))
89 return wxDragLink
; // At least IE 5.x needs wxDragLink, the
90 // other browsers on MSW seem okay with it too.
93 // translate this to calls to OnDropURL() just for convenience
94 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
99 OnDropURL(x
, y
, ((wxURLDataObject
*)m_dataObject
)->GetURL());
105 #endif // wxUSE_DRAG_AND_DROP
107 // ----------------------------------------------------------------------------
108 // Define a new application type
109 // ----------------------------------------------------------------------------
111 class DnDApp
: public wxApp
114 virtual bool OnInit();
117 IMPLEMENT_APP(DnDApp
)
119 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
121 // ----------------------------------------------------------------------------
122 // Define canvas class to show a bitmap
123 // ----------------------------------------------------------------------------
125 class DnDCanvasBitmap
: public wxScrolledWindow
128 DnDCanvasBitmap(wxWindow
*parent
) : wxScrolledWindow(parent
) { }
130 void SetBitmap(const wxBitmap
& bitmap
)
134 SetScrollbars(10, 10,
135 m_bitmap
.GetWidth() / 10, m_bitmap
.GetHeight() / 10);
140 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
148 dc
.DrawBitmap(m_bitmap
, 0, 0);
155 DECLARE_EVENT_TABLE()
160 // and the same thing fo metafiles
161 class DnDCanvasMetafile
: public wxScrolledWindow
164 DnDCanvasMetafile(wxWindow
*parent
) : wxScrolledWindow(parent
) { }
166 void SetMetafile(const wxMetafile
& metafile
)
168 m_metafile
= metafile
;
170 SetScrollbars(10, 10,
171 m_metafile
.GetWidth() / 10, m_metafile
.GetHeight() / 10);
176 void OnPaint(wxPaintEvent
&)
180 if ( m_metafile
.Ok() )
184 m_metafile
.Play(&dc
);
189 wxMetafile m_metafile
;
191 DECLARE_EVENT_TABLE()
194 #endif // wxUSE_METAFILE
196 // ----------------------------------------------------------------------------
197 // Define a new frame type for the main frame
198 // ----------------------------------------------------------------------------
200 class DnDFrame
: public wxFrame
206 void OnPaint(wxPaintEvent
& event
);
207 void OnSize(wxSizeEvent
& event
);
208 void OnQuit(wxCommandEvent
& event
);
209 void OnAbout(wxCommandEvent
& event
);
210 void OnDrag(wxCommandEvent
& event
);
211 void OnDragMoveByDefault(wxCommandEvent
& event
);
212 void OnDragMoveAllow(wxCommandEvent
& event
);
213 void OnNewFrame(wxCommandEvent
& event
);
214 void OnHelp (wxCommandEvent
& event
);
216 void OnLogClear(wxCommandEvent
& event
);
219 void OnCopy(wxCommandEvent
& event
);
220 void OnPaste(wxCommandEvent
& event
);
222 void OnCopyBitmap(wxCommandEvent
& event
);
223 void OnPasteBitmap(wxCommandEvent
& event
);
226 void OnPasteMetafile(wxCommandEvent
& event
);
227 #endif // wxUSE_METAFILE
229 void OnCopyFiles(wxCommandEvent
& event
);
231 void OnUsePrimary(wxCommandEvent
& event
);
233 void OnLeftDown(wxMouseEvent
& event
);
234 void OnRightDown(wxMouseEvent
& event
);
236 void OnBeginDrag(wxTreeEvent
& event
);
238 void OnUpdateUIMoveByDefault(wxUpdateUIEvent
& event
);
240 void OnUpdateUIPasteText(wxUpdateUIEvent
& event
);
241 void OnUpdateUIPasteBitmap(wxUpdateUIEvent
& event
);
244 // show the result of a dnd operation in the status bar
245 void LogDragResult(wxDragResult result
);
249 wxListBox
*m_ctrlFile
,
251 wxGenericDirCtrl
*m_ctrlDir
;
254 wxTextCtrl
*m_ctrlLog
;
260 // move the text by default (or copy)?
261 bool m_moveByDefault
;
263 // allow moving the text at all?
270 DECLARE_EVENT_TABLE()
273 // ----------------------------------------------------------------------------
274 // A shape is an example of application-specific data which may be transported
275 // via drag-and-drop or clipboard: in our case, we have different geometric
276 // shapes, each one with its own colour and position
277 // ----------------------------------------------------------------------------
279 #if wxUSE_DRAG_AND_DROP
292 DnDShape(const wxPoint
& pos
,
295 : m_pos(pos
), m_size(size
), m_col(col
)
299 // this is for debugging - lets us see when exactly an object is freed
300 // (this may be later than you think if it's on the clipboard, for example)
301 virtual ~DnDShape() { }
303 // the functions used for drag-and-drop: they dump and restore a shape into
304 // some bitwise-copiable data (might use streams too...)
305 // ------------------------------------------------------------------------
307 // restore from buffer
308 static DnDShape
*New(const void *buf
);
310 virtual size_t GetDataSize() const
312 return sizeof(ShapeDump
);
315 virtual void GetDataHere(void *buf
) const
317 ShapeDump
& dump
= *(ShapeDump
*)buf
;
322 dump
.r
= m_col
.Red();
323 dump
.g
= m_col
.Green();
324 dump
.b
= m_col
.Blue();
329 const wxPoint
& GetPosition() const { return m_pos
; }
330 const wxColour
& GetColour() const { return m_col
; }
331 const wxSize
& GetSize() const { return m_size
; }
333 void Move(const wxPoint
& pos
) { m_pos
= pos
; }
335 // to implement in derived classes
336 virtual Kind
GetKind() const = 0;
338 virtual void Draw(wxDC
& dc
)
340 dc
.SetPen(wxPen(m_col
, 1, wxSOLID
));
344 //get a point 1 up and 1 left, otherwise the mid-point of a triangle is on the line
345 wxPoint
GetCentre() const
346 { return wxPoint(m_pos
.x
+ m_size
.x
/ 2 - 1, m_pos
.y
+ m_size
.y
/ 2 - 1); }
350 wxCoord x
, y
, // position
353 unsigned char r
, g
, b
; // colour
361 class DnDTriangularShape
: public DnDShape
364 DnDTriangularShape(const wxPoint
& pos
,
367 : DnDShape(pos
, size
, col
)
369 wxLogMessage(wxT("DnDTriangularShape is being created"));
372 virtual ~DnDTriangularShape()
374 wxLogMessage(wxT("DnDTriangularShape is being deleted"));
377 virtual Kind
GetKind() const { return Triangle
; }
378 virtual void Draw(wxDC
& dc
)
382 // well, it's a bit difficult to describe a triangle by position and
383 // size, but we're not doing geometry here, do we? ;-)
385 wxPoint
p2(m_pos
.x
+ m_size
.x
, m_pos
.y
);
386 wxPoint
p3(m_pos
.x
, m_pos
.y
+ m_size
.y
);
392 //works in multicolor modes; on GTK (at least) will fail in 16-bit color
393 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
394 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
398 class DnDRectangularShape
: public DnDShape
401 DnDRectangularShape(const wxPoint
& pos
,
404 : DnDShape(pos
, size
, col
)
406 wxLogMessage(wxT("DnDRectangularShape is being created"));
409 virtual ~DnDRectangularShape()
411 wxLogMessage(wxT("DnDRectangularShape is being deleted"));
414 virtual Kind
GetKind() const { return Rectangle
; }
415 virtual void Draw(wxDC
& dc
)
420 wxPoint
p2(p1
.x
+ m_size
.x
, p1
.y
);
421 wxPoint
p3(p2
.x
, p2
.y
+ m_size
.y
);
422 wxPoint
p4(p1
.x
, p3
.y
);
429 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
430 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
434 class DnDEllipticShape
: public DnDShape
437 DnDEllipticShape(const wxPoint
& pos
,
440 : DnDShape(pos
, size
, col
)
442 wxLogMessage(wxT("DnDEllipticShape is being created"));
445 virtual ~DnDEllipticShape()
447 wxLogMessage(wxT("DnDEllipticShape is being deleted"));
450 virtual Kind
GetKind() const { return Ellipse
; }
451 virtual void Draw(wxDC
& dc
)
455 dc
.DrawEllipse(m_pos
, m_size
);
457 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
458 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
462 // ----------------------------------------------------------------------------
463 // A wxDataObject specialisation for the application-specific data
464 // ----------------------------------------------------------------------------
466 static const wxChar
*shapeFormatId
= wxT("wxShape");
468 class DnDShapeDataObject
: public wxDataObject
471 // ctor doesn't copy the pointer, so it shouldn't go away while this object
473 DnDShapeDataObject(DnDShape
*shape
= (DnDShape
*)NULL
)
477 // we need to copy the shape because the one we're handled may be
478 // deleted while it's still on the clipboard (for example) - and we
479 // reuse the serialisation methods here to copy it
480 void *buf
= malloc(shape
->DnDShape::GetDataSize());
481 shape
->GetDataHere(buf
);
482 m_shape
= DnDShape::New(buf
);
492 // this string should uniquely identify our format, but is otherwise
494 m_formatShape
.SetId(shapeFormatId
);
496 // we don't draw the shape to a bitmap until it's really needed (i.e.
497 // we're asked to do so)
500 m_hasMetaFile
= false;
501 #endif // wxUSE_METAFILE
504 virtual ~DnDShapeDataObject() { delete m_shape
; }
506 // after a call to this function, the shape is owned by the caller and it
507 // is responsible for deleting it!
509 // NB: a better solution would be to make DnDShapes ref counted and this
510 // is what should probably be done in a real life program, otherwise
511 // the ownership problems become too complicated really fast
514 DnDShape
*shape
= m_shape
;
516 m_shape
= (DnDShape
*)NULL
;
519 m_hasMetaFile
= false;
520 #endif // wxUSE_METAFILE
525 // implement base class pure virtuals
526 // ----------------------------------
528 virtual wxDataFormat
GetPreferredFormat(Direction
WXUNUSED(dir
)) const
530 return m_formatShape
;
533 virtual size_t GetFormatCount(Direction dir
) const
535 // our custom format is supported by both GetData() and SetData()
539 // but the bitmap format(s) are only supported for output
540 nFormats
+= m_dobjBitmap
.GetFormatCount(dir
);
543 nFormats
+= m_dobjMetaFile
.GetFormatCount(dir
);
544 #endif // wxUSE_METAFILE
550 virtual void GetAllFormats(wxDataFormat
*formats
, Direction dir
) const
552 formats
[0] = m_formatShape
;
555 // in Get direction we additionally support bitmaps and metafiles
557 m_dobjBitmap
.GetAllFormats(&formats
[1], dir
);
560 // don't assume that m_dobjBitmap has only 1 format
561 m_dobjMetaFile
.GetAllFormats(&formats
[1 +
562 m_dobjBitmap
.GetFormatCount(dir
)], dir
);
563 #endif // wxUSE_METAFILE
567 virtual size_t GetDataSize(const wxDataFormat
& format
) const
569 if ( format
== m_formatShape
)
571 return m_shape
->GetDataSize();
574 else if ( m_dobjMetaFile
.IsSupported(format
) )
576 if ( !m_hasMetaFile
)
579 return m_dobjMetaFile
.GetDataSize(format
);
581 #endif // wxUSE_METAFILE
584 wxASSERT_MSG( m_dobjBitmap
.IsSupported(format
),
585 wxT("unexpected format") );
590 return m_dobjBitmap
.GetDataSize();
594 virtual bool GetDataHere(const wxDataFormat
& format
, void *pBuf
) const
596 if ( format
== m_formatShape
)
598 m_shape
->GetDataHere(pBuf
);
603 else if ( m_dobjMetaFile
.IsSupported(format
) )
605 if ( !m_hasMetaFile
)
608 return m_dobjMetaFile
.GetDataHere(format
, pBuf
);
610 #endif // wxUSE_METAFILE
613 wxASSERT_MSG( m_dobjBitmap
.IsSupported(format
),
614 wxT("unexpected format") );
619 return m_dobjBitmap
.GetDataHere(pBuf
);
623 virtual bool SetData(const wxDataFormat
& format
,
624 size_t WXUNUSED(len
), const void *buf
)
626 wxCHECK_MSG( format
== m_formatShape
, false,
627 wxT( "unsupported format") );
630 m_shape
= DnDShape::New(buf
);
632 // the shape has changed
636 m_hasMetaFile
= false;
637 #endif // wxUSE_METAFILE
643 // creates a bitmap and assigns it to m_dobjBitmap (also sets m_hasBitmap)
644 void CreateBitmap() const;
646 void CreateMetaFile() const;
647 #endif // wxUSE_METAFILE
649 wxDataFormat m_formatShape
; // our custom format
651 wxBitmapDataObject m_dobjBitmap
; // it handles bitmaps
652 bool m_hasBitmap
; // true if m_dobjBitmap has valid bitmap
655 wxMetaFileDataObject m_dobjMetaFile
;// handles metafiles
656 bool m_hasMetaFile
; // true if we have valid metafile
657 #endif // wxUSE_METAFILE
659 DnDShape
*m_shape
; // our data
662 // ----------------------------------------------------------------------------
663 // A dialog to edit shape properties
664 // ----------------------------------------------------------------------------
666 class DnDShapeDialog
: public wxDialog
669 DnDShapeDialog(wxFrame
*parent
, DnDShape
*shape
);
671 DnDShape
*GetShape() const;
673 virtual bool TransferDataToWindow();
674 virtual bool TransferDataFromWindow();
676 void OnColour(wxCommandEvent
& event
);
683 DnDShape::Kind m_shapeKind
;
695 DECLARE_EVENT_TABLE()
698 // ----------------------------------------------------------------------------
699 // A frame for the shapes which can be drag-and-dropped between frames
700 // ----------------------------------------------------------------------------
702 class DnDShapeFrame
: public wxFrame
705 DnDShapeFrame(wxFrame
*parent
);
708 void SetShape(DnDShape
*shape
);
709 virtual bool SetShape(const wxRegion
®ion
)
711 return wxFrame::SetShape( region
);
715 void OnNewShape(wxCommandEvent
& event
);
716 void OnEditShape(wxCommandEvent
& event
);
717 void OnClearShape(wxCommandEvent
& event
);
719 void OnCopyShape(wxCommandEvent
& event
);
720 void OnPasteShape(wxCommandEvent
& event
);
722 void OnUpdateUICopy(wxUpdateUIEvent
& event
);
723 void OnUpdateUIPaste(wxUpdateUIEvent
& event
);
725 void OnDrag(wxMouseEvent
& event
);
726 void OnPaint(wxPaintEvent
& event
);
727 void OnDrop(wxCoord x
, wxCoord y
, DnDShape
*shape
);
732 static DnDShapeFrame
*ms_lastDropTarget
;
734 DECLARE_EVENT_TABLE()
737 // ----------------------------------------------------------------------------
738 // wxDropTarget derivation for DnDShapes
739 // ----------------------------------------------------------------------------
741 class DnDShapeDropTarget
: public wxDropTarget
744 DnDShapeDropTarget(DnDShapeFrame
*frame
)
745 : wxDropTarget(new DnDShapeDataObject
)
750 // override base class (pure) virtuals
751 virtual wxDragResult
OnEnter(wxCoord x
, wxCoord y
, wxDragResult def
)
754 m_frame
->SetStatusText(_T("Mouse entered the frame"));
755 #endif // wxUSE_STATUSBAR
756 return OnDragOver(x
, y
, def
);
758 virtual void OnLeave()
761 m_frame
->SetStatusText(_T("Mouse left the frame"));
762 #endif // wxUSE_STATUSBAR
764 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
768 wxLogError(wxT("Failed to get drag and drop data"));
773 m_frame
->OnDrop(x
, y
,
774 ((DnDShapeDataObject
*)GetDataObject())->GetShape());
780 DnDShapeFrame
*m_frame
;
783 #endif // wxUSE_DRAG_AND_DROP
785 // ----------------------------------------------------------------------------
786 // functions prototypes
787 // ----------------------------------------------------------------------------
789 static void ShowBitmap(const wxBitmap
& bitmap
);
792 static void ShowMetaFile(const wxMetaFile
& metafile
);
793 #endif // wxUSE_METAFILE
795 // ----------------------------------------------------------------------------
796 // IDs for the menu commands
797 // ----------------------------------------------------------------------------
816 Menu_Shape_New
= 500,
819 Menu_ShapeClipboard_Copy
,
820 Menu_ShapeClipboard_Paste
,
824 BEGIN_EVENT_TABLE(DnDFrame
, wxFrame
)
825 EVT_MENU(Menu_Quit
, DnDFrame::OnQuit
)
826 EVT_MENU(Menu_About
, DnDFrame::OnAbout
)
827 EVT_MENU(Menu_Drag
, DnDFrame::OnDrag
)
828 EVT_MENU(Menu_DragMoveDef
, DnDFrame::OnDragMoveByDefault
)
829 EVT_MENU(Menu_DragMoveAllow
,DnDFrame::OnDragMoveAllow
)
830 EVT_MENU(Menu_NewFrame
, DnDFrame::OnNewFrame
)
831 EVT_MENU(Menu_Help
, DnDFrame::OnHelp
)
833 EVT_MENU(Menu_Clear
, DnDFrame::OnLogClear
)
835 EVT_MENU(Menu_Copy
, DnDFrame::OnCopy
)
836 EVT_MENU(Menu_Paste
, DnDFrame::OnPaste
)
837 EVT_MENU(Menu_CopyBitmap
, DnDFrame::OnCopyBitmap
)
838 EVT_MENU(Menu_PasteBitmap
,DnDFrame::OnPasteBitmap
)
840 EVT_MENU(Menu_PasteMFile
, DnDFrame::OnPasteMetafile
)
841 #endif // wxUSE_METAFILE
842 EVT_MENU(Menu_CopyFiles
, DnDFrame::OnCopyFiles
)
843 EVT_MENU(Menu_UsePrimary
, DnDFrame::OnUsePrimary
)
845 EVT_UPDATE_UI(Menu_DragMoveDef
, DnDFrame::OnUpdateUIMoveByDefault
)
847 EVT_UPDATE_UI(Menu_Paste
, DnDFrame::OnUpdateUIPasteText
)
848 EVT_UPDATE_UI(Menu_PasteBitmap
, DnDFrame::OnUpdateUIPasteBitmap
)
850 EVT_LEFT_DOWN( DnDFrame::OnLeftDown
)
851 EVT_RIGHT_DOWN( DnDFrame::OnRightDown
)
852 EVT_PAINT( DnDFrame::OnPaint
)
853 EVT_SIZE( DnDFrame::OnSize
)
856 #if wxUSE_DRAG_AND_DROP
858 BEGIN_EVENT_TABLE(DnDShapeFrame
, wxFrame
)
859 EVT_MENU(Menu_Shape_New
, DnDShapeFrame::OnNewShape
)
860 EVT_MENU(Menu_Shape_Edit
, DnDShapeFrame::OnEditShape
)
861 EVT_MENU(Menu_Shape_Clear
, DnDShapeFrame::OnClearShape
)
863 EVT_MENU(Menu_ShapeClipboard_Copy
, DnDShapeFrame::OnCopyShape
)
864 EVT_MENU(Menu_ShapeClipboard_Paste
, DnDShapeFrame::OnPasteShape
)
866 EVT_UPDATE_UI(Menu_ShapeClipboard_Copy
, DnDShapeFrame::OnUpdateUICopy
)
867 EVT_UPDATE_UI(Menu_ShapeClipboard_Paste
, DnDShapeFrame::OnUpdateUIPaste
)
869 EVT_LEFT_DOWN(DnDShapeFrame::OnDrag
)
871 EVT_PAINT(DnDShapeFrame::OnPaint
)
874 BEGIN_EVENT_TABLE(DnDShapeDialog
, wxDialog
)
875 EVT_BUTTON(Button_Colour
, DnDShapeDialog::OnColour
)
878 #endif // wxUSE_DRAG_AND_DROP
880 BEGIN_EVENT_TABLE(DnDCanvasBitmap
, wxScrolledWindow
)
881 EVT_PAINT(DnDCanvasBitmap::OnPaint
)
885 BEGIN_EVENT_TABLE(DnDCanvasMetafile
, wxScrolledWindow
)
886 EVT_PAINT(DnDCanvasMetafile::OnPaint
)
888 #endif // wxUSE_METAFILE
890 #endif // wxUSE_DRAG_AND_DROP
892 // ============================================================================
894 // ============================================================================
896 // `Main program' equivalent, creating windows and returning main app frame
897 bool DnDApp::OnInit()
899 if ( !wxApp::OnInit() )
902 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
903 // switch on trace messages
905 #if defined(__WXGTK__)
906 wxLog::AddTraceMask(_T("clipboard"));
907 #elif defined(__WXMSW__)
908 wxLog::AddTraceMask(wxTRACE_OleCalls
);
913 wxImage::AddHandler( new wxPNGHandler
);
916 // create the main frame window
921 wxMessageBox( _T("This sample has to be compiled with wxUSE_DRAG_AND_DROP"), _T("Building error"), wxOK
);
923 #endif // wxUSE_DRAG_AND_DROP
926 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
929 : wxFrame(NULL
, wxID_ANY
, _T("Drag-and-Drop/Clipboard wxWidgets Sample"),
931 m_strText(_T("wxWidgets drag & drop works :-)"))
934 // frame icon and status bar
935 SetIcon(wxICON(sample
));
939 #endif // wxUSE_STATUSBAR
942 wxMenu
*file_menu
= new wxMenu
;
943 file_menu
->Append(Menu_Drag
, _T("&Test drag..."));
944 file_menu
->AppendCheckItem(Menu_DragMoveDef
, _T("&Move by default"));
945 file_menu
->AppendCheckItem(Menu_DragMoveAllow
, _T("&Allow moving"));
946 file_menu
->AppendSeparator();
947 file_menu
->Append(Menu_NewFrame
, _T("&New frame\tCtrl-N"));
948 file_menu
->AppendSeparator();
949 file_menu
->Append(Menu_Quit
, _T("E&xit\tCtrl-Q"));
952 wxMenu
*log_menu
= new wxMenu
;
953 log_menu
->Append(Menu_Clear
, _T("Clear\tCtrl-L"));
956 wxMenu
*help_menu
= new wxMenu
;
957 help_menu
->Append(Menu_Help
, _T("&Help..."));
958 help_menu
->AppendSeparator();
959 help_menu
->Append(Menu_About
, _T("&About"));
961 wxMenu
*clip_menu
= new wxMenu
;
962 clip_menu
->Append(Menu_Copy
, _T("&Copy text\tCtrl-C"));
963 clip_menu
->Append(Menu_Paste
, _T("&Paste text\tCtrl-V"));
964 clip_menu
->AppendSeparator();
965 clip_menu
->Append(Menu_CopyBitmap
, _T("Copy &bitmap\tCtrl-Shift-C"));
966 clip_menu
->Append(Menu_PasteBitmap
, _T("Paste b&itmap\tCtrl-Shift-V"));
968 clip_menu
->AppendSeparator();
969 clip_menu
->Append(Menu_PasteMFile
, _T("Paste &metafile\tCtrl-M"));
970 #endif // wxUSE_METAFILE
971 clip_menu
->AppendSeparator();
972 clip_menu
->Append(Menu_CopyFiles
, _T("Copy &files\tCtrl-F"));
973 clip_menu
->AppendSeparator();
974 clip_menu
->AppendCheckItem(Menu_UsePrimary
, _T("Use &primary selection\tCtrl-P"));
976 wxMenuBar
*menu_bar
= new wxMenuBar
;
977 menu_bar
->Append(file_menu
, _T("&File"));
979 menu_bar
->Append(log_menu
, _T("&Log"));
981 menu_bar
->Append(clip_menu
, _T("&Clipboard"));
982 menu_bar
->Append(help_menu
, _T("&Help"));
984 SetMenuBar(menu_bar
);
986 // create the child controls
987 SetBackgroundColour(*wxWHITE
); // labels read better on this background
989 wxString
strFile(_T("Drop files here!")), strText(_T("Drop text on me"));
991 m_ctrlFile
= new wxListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, 1, &strFile
,
992 wxLB_HSCROLL
| wxLB_ALWAYS_SB
);
993 m_ctrlText
= new wxListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, 1, &strText
,
994 wxLB_HSCROLL
| wxLB_ALWAYS_SB
);
995 m_ctrlDir
= new wxGenericDirCtrl(this);
998 m_ctrlLog
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
,
999 wxDefaultPosition
, wxDefaultSize
,
1000 wxTE_MULTILINE
| wxTE_READONLY
|
1003 // redirect log messages to the text window
1004 m_pLog
= new wxLogTextCtrl(m_ctrlLog
);
1005 m_pLogPrev
= wxLog::SetActiveTarget(m_pLog
);
1008 #if wxUSE_DRAG_AND_DROP
1009 // associate drop targets with the controls
1010 m_ctrlFile
->SetDropTarget(new DnDFile(m_ctrlFile
));
1011 m_ctrlText
->SetDropTarget(new DnDText(m_ctrlText
));
1016 wxEVT_COMMAND_TREE_BEGIN_DRAG
,
1017 wxTreeEventHandler(DnDFrame::OnBeginDrag
),
1023 m_ctrlLog
->SetDropTarget(new URLDropTarget
);
1025 #endif // wxUSE_DRAG_AND_DROP
1027 wxBoxSizer
*sizer_top
= new wxBoxSizer( wxHORIZONTAL
);
1028 sizer_top
->Add(m_ctrlFile
, 1, wxEXPAND
);
1029 sizer_top
->Add(m_ctrlText
, 1, wxEXPAND
);
1031 wxBoxSizer
*sizerDirCtrl
= new wxBoxSizer(wxVERTICAL
);
1032 sizerDirCtrl
->Add(new wxStaticText(this, wxID_ANY
, "Drag files from here"),
1033 wxSizerFlags().Centre().Border());
1034 sizerDirCtrl
->Add(m_ctrlDir
, wxSizerFlags(1).Expand());
1035 sizer_top
->Add(sizerDirCtrl
, 1, wxEXPAND
);
1037 // make all columns of reasonable minimal size
1038 for ( unsigned n
= 0; n
< sizer_top
->GetChildren().size(); n
++ )
1039 sizer_top
->SetItemMinSize(n
, 200, 300);
1041 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
1042 sizer
->Add(sizer_top
, 1, wxEXPAND
);
1044 sizer
->Add(m_ctrlLog
, 2, wxEXPAND
);
1045 sizer
->SetItemMinSize(m_ctrlLog
, 450, 200);
1047 sizer
->AddSpacer(50);
1049 // copy data by default but allow moving it as well
1050 m_moveByDefault
= false;
1052 menu_bar
->Check(Menu_DragMoveAllow
, true);
1054 // set the correct size and show the frame
1055 SetSizerAndFit(sizer
);
1059 void DnDFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
1064 void DnDFrame::OnSize(wxSizeEvent
& event
)
1071 void DnDFrame::OnPaint(wxPaintEvent
& WXUNUSED(event
))
1075 GetClientSize( &w
, &h
);
1078 // dc.Clear(); -- this kills wxGTK
1079 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
, false, _T("charter") ) );
1080 dc
.DrawText( _T("Drag text from here!"), 100, h
-50 );
1083 void DnDFrame::OnUpdateUIMoveByDefault(wxUpdateUIEvent
& event
)
1085 // only can move by default if moving is allowed at all
1086 event
.Enable(m_moveAllow
);
1089 void DnDFrame::OnUpdateUIPasteText(wxUpdateUIEvent
& event
)
1092 // too many trace messages if we don't do it - this function is called
1097 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
1100 void DnDFrame::OnUpdateUIPasteBitmap(wxUpdateUIEvent
& event
)
1103 // too many trace messages if we don't do it - this function is called
1108 event
.Enable( wxTheClipboard
->IsSupported(wxDF_BITMAP
) );
1111 void DnDFrame::OnNewFrame(wxCommandEvent
& WXUNUSED(event
))
1113 #if wxUSE_DRAG_AND_DROP
1114 (new DnDShapeFrame(this))->Show(true);
1116 wxLogStatus(this, wxT("Double click the new frame to select a shape for it"));
1117 #endif // wxUSE_DRAG_AND_DROP
1120 void DnDFrame::OnDrag(wxCommandEvent
& WXUNUSED(event
))
1122 #if wxUSE_DRAG_AND_DROP
1123 wxString strText
= wxGetTextFromUser
1125 _T("After you enter text in this dialog, press any mouse\n")
1126 _T("button in the bottom (empty) part of the frame and \n")
1127 _T("drag it anywhere - you will be in fact dragging the\n")
1128 _T("text object containing this text"),
1129 _T("Please enter some text"), m_strText
, this
1132 m_strText
= strText
;
1133 #endif // wxUSE_DRAG_AND_DROP
1136 void DnDFrame::OnDragMoveByDefault(wxCommandEvent
& event
)
1138 m_moveByDefault
= event
.IsChecked();
1141 void DnDFrame::OnDragMoveAllow(wxCommandEvent
& event
)
1143 m_moveAllow
= event
.IsChecked();
1146 void DnDFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
1148 wxMessageBox(_T("Drag-&-Drop Demo\n")
1149 _T("Please see \"Help|Help...\" for details\n")
1150 _T("Copyright (c) 1998 Vadim Zeitlin"),
1152 wxICON_INFORMATION
| wxOK
,
1156 void DnDFrame::OnHelp(wxCommandEvent
& /* event */)
1158 wxMessageDialog
dialog(this,
1159 _T("This small program demonstrates drag & drop support in wxWidgets. The program window\n")
1160 _T("consists of 3 parts: the bottom pane is for debug messages, so that you can see what's\n")
1161 _T("going on inside. The top part is split into 2 listboxes, the left one accepts files\n")
1162 _T("and the right one accepts text.\n")
1164 _T("To test wxDropTarget: open wordpad (write.exe), select some text in it and drag it to\n")
1165 _T("the right listbox (you'll notice the usual visual feedback, i.e. the cursor will change).\n")
1166 _T("Also, try dragging some files (you can select several at once) from Windows Explorer (or \n")
1167 _T("File Manager) to the left pane. Hold down Ctrl/Shift keys when you drop text (doesn't \n")
1168 _T("work with files) and see what changes.\n")
1170 _T("To test wxDropSource: just press any mouse button on the empty zone of the window and drag\n")
1171 _T("it to wordpad or any other droptarget accepting text (and of course you can just drag it\n")
1172 _T("to the right pane). Due to a lot of trace messages, the cursor might take some time to \n")
1173 _T("change, don't release the mouse button until it does. You can change the string being\n")
1174 _T("dragged in in \"File|Test drag...\" dialog.\n")
1177 _T("Please send all questions/bug reports/suggestions &c to \n")
1178 _T("Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>"),
1185 void DnDFrame::OnLogClear(wxCommandEvent
& /* event */ )
1188 m_ctrlText
->Clear();
1189 m_ctrlFile
->Clear();
1193 void DnDFrame::LogDragResult(wxDragResult result
)
1199 case wxDragError
: pc
= _T("Error!"); break;
1200 case wxDragNone
: pc
= _T("Nothing"); break;
1201 case wxDragCopy
: pc
= _T("Copied"); break;
1202 case wxDragMove
: pc
= _T("Moved"); break;
1203 case wxDragCancel
: pc
= _T("Cancelled"); break;
1204 default: pc
= _T("Huh?"); break;
1207 SetStatusText(wxString(_T("Drag result: ")) + pc
);
1209 wxUnusedVar(result
);
1210 #endif // wxUSE_STATUSBAR
1213 void DnDFrame::OnLeftDown(wxMouseEvent
&WXUNUSED(event
) )
1215 #if wxUSE_DRAG_AND_DROP
1216 if ( !m_strText
.empty() )
1218 // start drag operation
1219 wxTextDataObject
textData(m_strText
);
1220 wxDropSource
source(textData
, this,
1221 wxDROP_ICON(dnd_copy
),
1222 wxDROP_ICON(dnd_move
),
1223 wxDROP_ICON(dnd_none
));
1226 if ( m_moveByDefault
)
1227 flags
|= wxDrag_DefaultMove
;
1228 else if ( m_moveAllow
)
1229 flags
|= wxDrag_AllowMove
;
1231 LogDragResult(source
.DoDragDrop(flags
));
1233 #endif // wxUSE_DRAG_AND_DROP
1236 void DnDFrame::OnRightDown(wxMouseEvent
&event
)
1238 wxMenu
menu(_T("Dnd sample menu"));
1240 menu
.Append(Menu_Drag
, _T("&Test drag..."));
1241 menu
.AppendSeparator();
1242 menu
.Append(Menu_About
, _T("&About"));
1244 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
1247 DnDFrame::~DnDFrame()
1250 if ( m_pLog
!= NULL
) {
1251 if ( wxLog::SetActiveTarget(m_pLogPrev
) == m_pLog
)
1257 void DnDFrame::OnUsePrimary(wxCommandEvent
& event
)
1259 const bool usePrimary
= event
.IsChecked();
1260 wxTheClipboard
->UsePrimarySelection(usePrimary
);
1262 wxLogStatus(_T("Now using %s selection"), usePrimary
? _T("primary")
1266 void DnDFrame::OnBeginDrag(wxTreeEvent
& WXUNUSED(event
))
1268 wxFileDataObject data
;
1269 data
.AddFile(m_ctrlDir
->GetPath());
1271 wxDropSource
dragSource(this);
1272 dragSource
.SetData(data
);
1274 LogDragResult(dragSource
.DoDragDrop());
1277 // ---------------------------------------------------------------------------
1279 // ---------------------------------------------------------------------------
1281 void DnDFrame::OnCopyBitmap(wxCommandEvent
& WXUNUSED(event
))
1283 // PNG support is not always compiled in under Windows, so use BMP there
1285 wxFileDialog
dialog(this, _T("Open a PNG file"), wxEmptyString
, wxEmptyString
, _T("PNG files (*.png)|*.png"), 0);
1287 wxFileDialog
dialog(this, _T("Open a BMP file"), wxEmptyString
, wxEmptyString
, _T("BMP files (*.bmp)|*.bmp"), 0);
1290 if (dialog
.ShowModal() != wxID_OK
)
1292 wxLogMessage( _T("Aborted file open") );
1296 if (dialog
.GetPath().empty())
1298 wxLogMessage( _T("Returned empty string.") );
1302 if (!wxFileExists(dialog
.GetPath()))
1304 wxLogMessage( _T("File doesn't exist.") );
1309 image
.LoadFile( dialog
.GetPath(),
1318 wxLogError( _T("Invalid image file...") );
1322 wxLogStatus( _T("Decoding image file...") );
1325 wxBitmap
bitmap( image
);
1327 if ( !wxTheClipboard
->Open() )
1329 wxLogError(_T("Can't open clipboard."));
1334 wxLogMessage( _T("Creating wxBitmapDataObject...") );
1337 if ( !wxTheClipboard
->AddData(new wxBitmapDataObject(bitmap
)) )
1339 wxLogError(_T("Can't copy image to the clipboard."));
1343 wxLogMessage(_T("Image has been put on the clipboard.") );
1344 wxLogMessage(_T("You can paste it now and look at it.") );
1347 wxTheClipboard
->Close();
1350 void DnDFrame::OnPasteBitmap(wxCommandEvent
& WXUNUSED(event
))
1352 if ( !wxTheClipboard
->Open() )
1354 wxLogError(_T("Can't open clipboard."));
1359 if ( !wxTheClipboard
->IsSupported(wxDF_BITMAP
) )
1361 wxLogWarning(_T("No bitmap on clipboard"));
1363 wxTheClipboard
->Close();
1367 wxBitmapDataObject data
;
1368 if ( !wxTheClipboard
->GetData(data
) )
1370 wxLogError(_T("Can't paste bitmap from the clipboard"));
1374 const wxBitmap
& bmp
= data
.GetBitmap();
1376 wxLogMessage(_T("Bitmap %dx%d pasted from the clipboard"),
1377 bmp
.GetWidth(), bmp
.GetHeight());
1381 wxTheClipboard
->Close();
1386 void DnDFrame::OnPasteMetafile(wxCommandEvent
& WXUNUSED(event
))
1388 if ( !wxTheClipboard
->Open() )
1390 wxLogError(_T("Can't open clipboard."));
1395 if ( !wxTheClipboard
->IsSupported(wxDF_METAFILE
) )
1397 wxLogWarning(_T("No metafile on clipboard"));
1401 wxMetaFileDataObject data
;
1402 if ( !wxTheClipboard
->GetData(data
) )
1404 wxLogError(_T("Can't paste metafile from the clipboard"));
1408 const wxMetaFile
& mf
= data
.GetMetafile();
1410 wxLogMessage(_T("Metafile %dx%d pasted from the clipboard"),
1411 mf
.GetWidth(), mf
.GetHeight());
1417 wxTheClipboard
->Close();
1420 #endif // wxUSE_METAFILE
1422 // ----------------------------------------------------------------------------
1424 // ----------------------------------------------------------------------------
1426 void DnDFrame::OnCopyFiles(wxCommandEvent
& WXUNUSED(event
))
1429 wxFileDialog
dialog(this, _T("Select a file to copy"), wxEmptyString
, wxEmptyString
,
1430 _T("All files (*.*)|*.*"), 0);
1432 wxArrayString filenames
;
1433 while ( dialog
.ShowModal() == wxID_OK
)
1435 filenames
.Add(dialog
.GetPath());
1438 if ( !filenames
.IsEmpty() )
1440 wxFileDataObject
*dobj
= new wxFileDataObject
;
1441 size_t count
= filenames
.GetCount();
1442 for ( size_t n
= 0; n
< count
; n
++ )
1444 dobj
->AddFile(filenames
[n
]);
1447 wxClipboardLocker locker
;
1450 wxLogError(wxT("Can't open clipboard"));
1454 if ( !wxTheClipboard
->AddData(dobj
) )
1456 wxLogError(wxT("Can't copy file(s) to the clipboard"));
1460 wxLogStatus(this, wxT("%d file%s copied to the clipboard"),
1461 count
, count
== 1 ? wxEmptyString
: wxEmptyString
);
1467 wxLogStatus(this, wxT("Aborted"));
1470 wxLogError(wxT("Sorry, not implemented"));
1474 // ---------------------------------------------------------------------------
1476 // ---------------------------------------------------------------------------
1478 void DnDFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1480 if ( !wxTheClipboard
->Open() )
1482 wxLogError(_T("Can't open clipboard."));
1487 if ( !wxTheClipboard
->AddData(new wxTextDataObject(m_strText
)) )
1489 wxLogError(_T("Can't copy data to the clipboard"));
1493 wxLogMessage(_T("Text '%s' put on the clipboard"), m_strText
.c_str());
1496 wxTheClipboard
->Close();
1499 void DnDFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1501 if ( !wxTheClipboard
->Open() )
1503 wxLogError(_T("Can't open clipboard."));
1508 if ( !wxTheClipboard
->IsSupported(wxDF_TEXT
) )
1510 wxLogWarning(_T("No text data on clipboard"));
1512 wxTheClipboard
->Close();
1516 wxTextDataObject text
;
1517 if ( !wxTheClipboard
->GetData(text
) )
1519 wxLogError(_T("Can't paste data from the clipboard"));
1523 wxLogMessage(_T("Text '%s' pasted from the clipboard"),
1524 text
.GetText().c_str());
1527 wxTheClipboard
->Close();
1530 #if wxUSE_DRAG_AND_DROP
1532 // ----------------------------------------------------------------------------
1533 // Notifications called by the base class
1534 // ----------------------------------------------------------------------------
1536 bool DnDText::OnDropText(wxCoord
, wxCoord
, const wxString
& text
)
1538 m_pOwner
->Append(text
);
1543 bool DnDFile::OnDropFiles(wxCoord
, wxCoord
, const wxArrayString
& filenames
)
1545 size_t nFiles
= filenames
.GetCount();
1547 str
.Printf( _T("%d files dropped"), (int)nFiles
);
1549 if (m_pOwner
!= NULL
)
1551 m_pOwner
->Append(str
);
1552 for ( size_t n
= 0; n
< nFiles
; n
++ )
1553 m_pOwner
->Append(filenames
[n
]);
1559 // ----------------------------------------------------------------------------
1561 // ----------------------------------------------------------------------------
1563 DnDShapeDialog::DnDShapeDialog(wxFrame
*parent
, DnDShape
*shape
)
1564 :wxDialog( parent
, 6001, wxT("Choose Shape"), wxPoint( 10, 10 ),
1566 wxDEFAULT_DIALOG_STYLE
| wxRAISED_BORDER
| wxRESIZE_BORDER
)
1569 wxBoxSizer
* topSizer
= new wxBoxSizer( wxVERTICAL
);
1572 wxBoxSizer
* shapesSizer
= new wxBoxSizer( wxHORIZONTAL
);
1573 const wxString choices
[] = { wxT("None"), wxT("Triangle"),
1574 wxT("Rectangle"), wxT("Ellipse") };
1576 m_radio
= new wxRadioBox( this, wxID_ANY
, wxT("&Shape"),
1577 wxDefaultPosition
, wxDefaultSize
, 4, choices
, 4,
1578 wxRA_SPECIFY_COLS
);
1579 shapesSizer
->Add( m_radio
, 0, wxGROW
|wxALL
, 5 );
1580 topSizer
->Add( shapesSizer
, 0, wxALL
, 2 );
1583 wxStaticBox
* box
= new wxStaticBox( this, wxID_ANY
, wxT("&Attributes") );
1584 wxStaticBoxSizer
* attrSizer
= new wxStaticBoxSizer( box
, wxHORIZONTAL
);
1585 wxFlexGridSizer
* xywhSizer
= new wxFlexGridSizer( 4, 2 );
1589 st
= new wxStaticText( this, wxID_ANY
, wxT("Position &X:") );
1590 m_textX
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1592 xywhSizer
->Add( st
, 1, wxGROW
|wxALL
, 2 );
1593 xywhSizer
->Add( m_textX
, 1, wxGROW
|wxALL
, 2 );
1595 st
= new wxStaticText( this, wxID_ANY
, wxT("Size &width:") );
1596 m_textW
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1598 xywhSizer
->Add( st
, 1, wxGROW
|wxALL
, 2 );
1599 xywhSizer
->Add( m_textW
, 1, wxGROW
|wxALL
, 2 );
1601 st
= new wxStaticText( this, wxID_ANY
, wxT("&Y:") );
1602 m_textY
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1604 xywhSizer
->Add( st
, 1, wxALL
|wxALIGN_RIGHT
, 2 );
1605 xywhSizer
->Add( m_textY
, 1, wxGROW
|wxALL
, 2 );
1607 st
= new wxStaticText( this, wxID_ANY
, wxT("&height:") );
1608 m_textH
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1610 xywhSizer
->Add( st
, 1, wxALL
|wxALIGN_RIGHT
, 2 );
1611 xywhSizer
->Add( m_textH
, 1, wxGROW
|wxALL
, 2 );
1613 wxButton
* col
= new wxButton( this, Button_Colour
, wxT("&Colour...") );
1614 attrSizer
->Add( xywhSizer
, 1, wxGROW
);
1615 attrSizer
->Add( col
, 0, wxALL
|wxALIGN_CENTRE_VERTICAL
, 2 );
1616 topSizer
->Add( attrSizer
, 0, wxGROW
|wxALL
, 5 );
1619 wxBoxSizer
* buttonSizer
= new wxBoxSizer( wxHORIZONTAL
);
1621 bt
= new wxButton( this, wxID_OK
, wxT("Ok") );
1622 buttonSizer
->Add( bt
, 0, wxALL
, 2 );
1623 bt
= new wxButton( this, wxID_CANCEL
, wxT("Cancel") );
1624 buttonSizer
->Add( bt
, 0, wxALL
, 2 );
1625 topSizer
->Add( buttonSizer
, 0, wxALL
|wxALIGN_RIGHT
, 2 );
1627 SetSizer( topSizer
);
1628 topSizer
->Fit( this );
1631 DnDShape
*DnDShapeDialog::GetShape() const
1633 switch ( m_shapeKind
)
1636 case DnDShape::None
: return NULL
;
1637 case DnDShape::Triangle
: return new DnDTriangularShape(m_pos
, m_size
, m_col
);
1638 case DnDShape::Rectangle
: return new DnDRectangularShape(m_pos
, m_size
, m_col
);
1639 case DnDShape::Ellipse
: return new DnDEllipticShape(m_pos
, m_size
, m_col
);
1643 bool DnDShapeDialog::TransferDataToWindow()
1648 m_radio
->SetSelection(m_shape
->GetKind());
1649 m_pos
= m_shape
->GetPosition();
1650 m_size
= m_shape
->GetSize();
1651 m_col
= m_shape
->GetColour();
1655 m_radio
->SetSelection(DnDShape::None
);
1656 m_pos
= wxPoint(1, 1);
1657 m_size
= wxSize(100, 100);
1660 m_textX
->SetValue(wxString() << m_pos
.x
);
1661 m_textY
->SetValue(wxString() << m_pos
.y
);
1662 m_textW
->SetValue(wxString() << m_size
.x
);
1663 m_textH
->SetValue(wxString() << m_size
.y
);
1668 bool DnDShapeDialog::TransferDataFromWindow()
1670 m_shapeKind
= (DnDShape::Kind
)m_radio
->GetSelection();
1672 m_pos
.x
= wxAtoi(m_textX
->GetValue());
1673 m_pos
.y
= wxAtoi(m_textY
->GetValue());
1674 m_size
.x
= wxAtoi(m_textW
->GetValue());
1675 m_size
.y
= wxAtoi(m_textH
->GetValue());
1677 if ( !m_pos
.x
|| !m_pos
.y
|| !m_size
.x
|| !m_size
.y
)
1679 wxMessageBox(_T("All sizes and positions should be non null!"),
1680 _T("Invalid shape"), wxICON_HAND
| wxOK
, this);
1688 void DnDShapeDialog::OnColour(wxCommandEvent
& WXUNUSED(event
))
1691 data
.SetChooseFull(true);
1692 for (int i
= 0; i
< 16; i
++)
1694 wxColour
colour((unsigned char)(i
*16), (unsigned char)(i
*16), (unsigned char)(i
*16));
1695 data
.SetCustomColour(i
, colour
);
1698 wxColourDialog
dialog(this, &data
);
1699 if ( dialog
.ShowModal() == wxID_OK
)
1701 m_col
= dialog
.GetColourData().GetColour();
1705 // ----------------------------------------------------------------------------
1707 // ----------------------------------------------------------------------------
1709 DnDShapeFrame
*DnDShapeFrame::ms_lastDropTarget
= NULL
;
1711 DnDShapeFrame::DnDShapeFrame(wxFrame
*parent
)
1712 : wxFrame(parent
, wxID_ANY
, _T("Shape Frame"))
1716 #endif // wxUSE_STATUSBAR
1718 wxMenu
*menuShape
= new wxMenu
;
1719 menuShape
->Append(Menu_Shape_New
, _T("&New default shape\tCtrl-S"));
1720 menuShape
->Append(Menu_Shape_Edit
, _T("&Edit shape\tCtrl-E"));
1721 menuShape
->AppendSeparator();
1722 menuShape
->Append(Menu_Shape_Clear
, _T("&Clear shape\tCtrl-L"));
1724 wxMenu
*menuClipboard
= new wxMenu
;
1725 menuClipboard
->Append(Menu_ShapeClipboard_Copy
, _T("&Copy\tCtrl-C"));
1726 menuClipboard
->Append(Menu_ShapeClipboard_Paste
, _T("&Paste\tCtrl-V"));
1728 wxMenuBar
*menubar
= new wxMenuBar
;
1729 menubar
->Append(menuShape
, _T("&Shape"));
1730 menubar
->Append(menuClipboard
, _T("&Clipboard"));
1732 SetMenuBar(menubar
);
1735 SetStatusText(_T("Press Ctrl-S to create a new shape"));
1736 #endif // wxUSE_STATUSBAR
1738 SetDropTarget(new DnDShapeDropTarget(this));
1742 SetBackgroundColour(*wxWHITE
);
1745 DnDShapeFrame::~DnDShapeFrame()
1751 void DnDShapeFrame::SetShape(DnDShape
*shape
)
1760 void DnDShapeFrame::OnDrag(wxMouseEvent
& event
)
1769 // start drag operation
1770 DnDShapeDataObject
shapeData(m_shape
);
1771 wxDropSource
source(shapeData
, this);
1773 const wxChar
*pc
= NULL
;
1774 switch ( source
.DoDragDrop(true) )
1778 wxLogError(wxT("An error occurred during drag and drop operation"));
1783 SetStatusText(_T("Nothing happened"));
1784 #endif // wxUSE_STATUSBAR
1793 if ( ms_lastDropTarget
!= this )
1795 // don't delete the shape if we dropped it on ourselves!
1802 SetStatusText(_T("Drag and drop operation cancelled"));
1803 #endif // wxUSE_STATUSBAR
1810 SetStatusText(wxString(_T("Shape successfully ")) + pc
);
1811 #endif // wxUSE_STATUSBAR
1813 //else: status text already set
1816 void DnDShapeFrame::OnDrop(wxCoord x
, wxCoord y
, DnDShape
*shape
)
1818 ms_lastDropTarget
= this;
1824 s
.Printf(wxT("Shape dropped at (%d, %d)"), pt
.x
, pt
.y
);
1826 #endif // wxUSE_STATUSBAR
1832 void DnDShapeFrame::OnEditShape(wxCommandEvent
& WXUNUSED(event
))
1834 DnDShapeDialog
dlg(this, m_shape
);
1835 if ( dlg
.ShowModal() == wxID_OK
)
1837 SetShape(dlg
.GetShape());
1842 SetStatusText(_T("You can now drag the shape to another frame"));
1844 #endif // wxUSE_STATUSBAR
1848 void DnDShapeFrame::OnNewShape(wxCommandEvent
& WXUNUSED(event
))
1850 SetShape(new DnDEllipticShape(wxPoint(10, 10), wxSize(80, 60), *wxRED
));
1853 SetStatusText(_T("You can now drag the shape to another frame"));
1854 #endif // wxUSE_STATUSBAR
1857 void DnDShapeFrame::OnClearShape(wxCommandEvent
& WXUNUSED(event
))
1862 void DnDShapeFrame::OnCopyShape(wxCommandEvent
& WXUNUSED(event
))
1866 wxClipboardLocker clipLocker
;
1869 wxLogError(wxT("Can't open the clipboard"));
1874 wxTheClipboard
->AddData(new DnDShapeDataObject(m_shape
));
1878 void DnDShapeFrame::OnPasteShape(wxCommandEvent
& WXUNUSED(event
))
1880 wxClipboardLocker clipLocker
;
1883 wxLogError(wxT("Can't open the clipboard"));
1888 DnDShapeDataObject
shapeDataObject(NULL
);
1889 if ( wxTheClipboard
->GetData(shapeDataObject
) )
1891 SetShape(shapeDataObject
.GetShape());
1895 wxLogStatus(wxT("No shape on the clipboard"));
1899 void DnDShapeFrame::OnUpdateUICopy(wxUpdateUIEvent
& event
)
1901 event
.Enable( m_shape
!= NULL
);
1904 void DnDShapeFrame::OnUpdateUIPaste(wxUpdateUIEvent
& event
)
1906 event
.Enable( wxTheClipboard
->IsSupported(wxDataFormat(shapeFormatId
)) );
1909 void DnDShapeFrame::OnPaint(wxPaintEvent
& event
)
1923 // ----------------------------------------------------------------------------
1925 // ----------------------------------------------------------------------------
1927 DnDShape
*DnDShape::New(const void *buf
)
1929 const ShapeDump
& dump
= *(const ShapeDump
*)buf
;
1933 return new DnDTriangularShape(wxPoint(dump
.x
, dump
.y
),
1934 wxSize(dump
.w
, dump
.h
),
1935 wxColour(dump
.r
, dump
.g
, dump
.b
));
1938 return new DnDRectangularShape(wxPoint(dump
.x
, dump
.y
),
1939 wxSize(dump
.w
, dump
.h
),
1940 wxColour(dump
.r
, dump
.g
, dump
.b
));
1943 return new DnDEllipticShape(wxPoint(dump
.x
, dump
.y
),
1944 wxSize(dump
.w
, dump
.h
),
1945 wxColour(dump
.r
, dump
.g
, dump
.b
));
1948 wxFAIL_MSG(wxT("invalid shape!"));
1953 // ----------------------------------------------------------------------------
1954 // DnDShapeDataObject
1955 // ----------------------------------------------------------------------------
1959 void DnDShapeDataObject::CreateMetaFile() const
1961 wxPoint pos
= m_shape
->GetPosition();
1962 wxSize size
= m_shape
->GetSize();
1964 wxMetaFileDC
dcMF(wxEmptyString
, pos
.x
+ size
.x
, pos
.y
+ size
.y
);
1966 m_shape
->Draw(dcMF
);
1968 wxMetafile
*mf
= dcMF
.Close();
1970 DnDShapeDataObject
*self
= (DnDShapeDataObject
*)this; // const_cast
1971 self
->m_dobjMetaFile
.SetMetafile(*mf
);
1972 self
->m_hasMetaFile
= true;
1977 #endif // wxUSE_METAFILE
1979 void DnDShapeDataObject::CreateBitmap() const
1981 wxPoint pos
= m_shape
->GetPosition();
1982 wxSize size
= m_shape
->GetSize();
1983 int x
= pos
.x
+ size
.x
,
1985 wxBitmap
bitmap(x
, y
);
1987 dc
.SelectObject(bitmap
);
1988 dc
.SetBrush(wxBrush(wxT("white"), wxSOLID
));
1991 dc
.SelectObject(wxNullBitmap
);
1993 DnDShapeDataObject
*self
= (DnDShapeDataObject
*)this; // const_cast
1994 self
->m_dobjBitmap
.SetBitmap(bitmap
);
1995 self
->m_hasBitmap
= true;
1998 #endif // wxUSE_DRAG_AND_DROP
2000 // ----------------------------------------------------------------------------
2002 // ----------------------------------------------------------------------------
2004 static void ShowBitmap(const wxBitmap
& bitmap
)
2006 wxFrame
*frame
= new wxFrame(NULL
, wxID_ANY
, _T("Bitmap view"));
2008 frame
->CreateStatusBar();
2009 #endif // wxUSE_STATUSBAR
2010 DnDCanvasBitmap
*canvas
= new DnDCanvasBitmap(frame
);
2011 canvas
->SetBitmap(bitmap
);
2013 int w
= bitmap
.GetWidth(),
2014 h
= bitmap
.GetHeight();
2016 frame
->SetStatusText(wxString::Format(_T("%dx%d"), w
, h
));
2017 #endif // wxUSE_STATUSBAR
2019 frame
->SetClientSize(w
> 100 ? 100 : w
, h
> 100 ? 100 : h
);
2025 static void ShowMetaFile(const wxMetaFile
& metafile
)
2027 wxFrame
*frame
= new wxFrame(NULL
, wxID_ANY
, _T("Metafile view"));
2028 frame
->CreateStatusBar();
2029 DnDCanvasMetafile
*canvas
= new DnDCanvasMetafile(frame
);
2030 canvas
->SetMetafile(metafile
);
2032 wxSize size
= metafile
.GetSize();
2033 frame
->SetStatusText(wxString::Format(_T("%dx%d"), size
.x
, size
.y
));
2035 frame
->SetClientSize(size
.x
> 100 ? 100 : size
.x
,
2036 size
.y
> 100 ? 100 : size
.y
);
2040 #endif // wxUSE_METAFILE
2042 #endif // wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD