1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Drag and drop sample
4 // Author: Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
24 #include "wx/clipbrd.h"
25 #include "wx/colordlg.h"
26 #include "wx/metafile.h"
28 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
29 #include "../sample.xpm"
30 #if wxUSE_DRAG_AND_DROP
31 #include "dnd_copy.xpm"
32 #include "dnd_move.xpm"
33 #include "dnd_none.xpm"
37 #if wxUSE_DRAG_AND_DROP
39 // ----------------------------------------------------------------------------
40 // Derive two simple classes which just put in the listbox the strings (text or
41 // file names) we drop on them
42 // ----------------------------------------------------------------------------
44 class DnDText
: public wxTextDropTarget
47 DnDText(wxListBox
*pOwner
) { m_pOwner
= pOwner
; }
49 virtual bool OnDropText(wxCoord x
, wxCoord y
, const wxString
& text
);
55 class DnDFile
: public wxFileDropTarget
58 DnDFile(wxListBox
*pOwner
) { m_pOwner
= pOwner
; }
60 virtual bool OnDropFiles(wxCoord x
, wxCoord y
,
61 const wxArrayString
& filenames
);
67 // ----------------------------------------------------------------------------
68 // Define a custom dtop target accepting URLs
69 // ----------------------------------------------------------------------------
71 class URLDropTarget
: public wxDropTarget
74 URLDropTarget() { SetDataObject(new wxURLDataObject
); }
76 void OnDropURL(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
), const wxString
& text
)
78 // of course, a real program would do something more useful here...
79 wxMessageBox(text
, _T("wxDnD sample: got URL"),
80 wxICON_INFORMATION
| wxOK
);
83 // URLs can't be moved, only copied
84 virtual wxDragResult
OnDragOver(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
85 wxDragResult
WXUNUSED(def
))
87 return wxDragLink
; // At least IE 5.x needs wxDragLink, the
88 // other browsers on MSW seem okay with it too.
91 // translate this to calls to OnDropURL() just for convenience
92 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
97 OnDropURL(x
, y
, ((wxURLDataObject
*)m_dataObject
)->GetURL());
103 #endif // wxUSE_DRAG_AND_DROP
105 // ----------------------------------------------------------------------------
106 // Define a new application type
107 // ----------------------------------------------------------------------------
109 class DnDApp
: public wxApp
112 virtual bool OnInit();
115 IMPLEMENT_APP(DnDApp
);
117 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
119 // ----------------------------------------------------------------------------
120 // Define canvas class to show a bitmap
121 // ----------------------------------------------------------------------------
123 class DnDCanvasBitmap
: public wxScrolledWindow
126 DnDCanvasBitmap(wxWindow
*parent
) : wxScrolledWindow(parent
) { }
128 void SetBitmap(const wxBitmap
& bitmap
)
132 SetScrollbars(10, 10,
133 m_bitmap
.GetWidth() / 10, m_bitmap
.GetHeight() / 10);
138 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
146 dc
.DrawBitmap(m_bitmap
, 0, 0);
153 DECLARE_EVENT_TABLE()
158 // and the same thing fo metafiles
159 class DnDCanvasMetafile
: public wxScrolledWindow
162 DnDCanvasMetafile(wxWindow
*parent
) : wxScrolledWindow(parent
) { }
164 void SetMetafile(const wxMetafile
& metafile
)
166 m_metafile
= metafile
;
168 SetScrollbars(10, 10,
169 m_metafile
.GetWidth() / 10, m_metafile
.GetHeight() / 10);
174 void OnPaint(wxPaintEvent
&)
178 if ( m_metafile
.Ok() )
182 m_metafile
.Play(&dc
);
187 wxMetafile m_metafile
;
189 DECLARE_EVENT_TABLE()
192 #endif // wxUSE_METAFILE
194 // ----------------------------------------------------------------------------
195 // Define a new frame type for the main frame
196 // ----------------------------------------------------------------------------
198 class DnDFrame
: public wxFrame
201 DnDFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
204 void OnPaint(wxPaintEvent
& event
);
205 void OnSize(wxSizeEvent
& event
);
206 void OnQuit(wxCommandEvent
& event
);
207 void OnAbout(wxCommandEvent
& event
);
208 void OnDrag(wxCommandEvent
& event
);
209 void OnDragMoveByDefault(wxCommandEvent
& event
);
210 void OnDragMoveAllow(wxCommandEvent
& event
);
211 void OnNewFrame(wxCommandEvent
& event
);
212 void OnHelp (wxCommandEvent
& event
);
214 void OnLogClear(wxCommandEvent
& event
);
217 void OnCopy(wxCommandEvent
& event
);
218 void OnPaste(wxCommandEvent
& event
);
220 void OnCopyBitmap(wxCommandEvent
& event
);
221 void OnPasteBitmap(wxCommandEvent
& event
);
224 void OnPasteMetafile(wxCommandEvent
& event
);
225 #endif // wxUSE_METAFILE
227 void OnCopyFiles(wxCommandEvent
& event
);
229 void OnLeftDown(wxMouseEvent
& event
);
230 void OnRightDown(wxMouseEvent
& event
);
232 void OnUpdateUIMoveByDefault(wxUpdateUIEvent
& event
);
234 void OnUpdateUIPasteText(wxUpdateUIEvent
& event
);
235 void OnUpdateUIPasteBitmap(wxUpdateUIEvent
& event
);
237 DECLARE_EVENT_TABLE()
241 wxListBox
*m_ctrlFile
,
245 wxTextCtrl
*m_ctrlLog
;
251 // move the text by default (or copy)?
252 bool m_moveByDefault
;
254 // allow moving the text at all?
261 // ----------------------------------------------------------------------------
262 // A shape is an example of application-specific data which may be transported
263 // via drag-and-drop or clipboard: in our case, we have different geometric
264 // shapes, each one with its own colour and position
265 // ----------------------------------------------------------------------------
267 #if wxUSE_DRAG_AND_DROP
280 DnDShape(const wxPoint
& pos
,
283 : m_pos(pos
), m_size(size
), m_col(col
)
287 // this is for debugging - lets us see when exactly an object is freed
288 // (this may be later than you think if it's on the clipboard, for example)
289 virtual ~DnDShape() { }
291 // the functions used for drag-and-drop: they dump and restore a shape into
292 // some bitwise-copiable data (might use streams too...)
293 // ------------------------------------------------------------------------
295 // restore from buffer
296 static DnDShape
*New(const void *buf
);
298 virtual size_t GetDataSize() const
300 return sizeof(ShapeDump
);
303 virtual void GetDataHere(void *buf
) const
305 ShapeDump
& dump
= *(ShapeDump
*)buf
;
310 dump
.r
= m_col
.Red();
311 dump
.g
= m_col
.Green();
312 dump
.b
= m_col
.Blue();
317 const wxPoint
& GetPosition() const { return m_pos
; }
318 const wxColour
& GetColour() const { return m_col
; }
319 const wxSize
& GetSize() const { return m_size
; }
321 void Move(const wxPoint
& pos
) { m_pos
= pos
; }
323 // to implement in derived classes
324 virtual Kind
GetKind() const = 0;
326 virtual void Draw(wxDC
& dc
)
328 dc
.SetPen(wxPen(m_col
, 1, wxSOLID
));
332 //get a point 1 up and 1 left, otherwise the mid-point of a triangle is on the line
333 wxPoint
GetCentre() const
334 { return wxPoint(m_pos
.x
+ m_size
.x
/ 2 - 1, m_pos
.y
+ m_size
.y
/ 2 - 1); }
338 wxCoord x
, y
, // position
341 unsigned char r
, g
, b
; // colour
349 class DnDTriangularShape
: public DnDShape
352 DnDTriangularShape(const wxPoint
& pos
,
355 : DnDShape(pos
, size
, col
)
357 wxLogMessage(wxT("DnDTriangularShape is being created"));
360 virtual ~DnDTriangularShape()
362 wxLogMessage(wxT("DnDTriangularShape is being deleted"));
365 virtual Kind
GetKind() const { return Triangle
; }
366 virtual void Draw(wxDC
& dc
)
370 // well, it's a bit difficult to describe a triangle by position and
371 // size, but we're not doing geometry here, do we? ;-)
373 wxPoint
p2(m_pos
.x
+ m_size
.x
, m_pos
.y
);
374 wxPoint
p3(m_pos
.x
, m_pos
.y
+ m_size
.y
);
380 //works in multicolor modes; on GTK (at least) will fail in 16-bit color
381 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
382 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
386 class DnDRectangularShape
: public DnDShape
389 DnDRectangularShape(const wxPoint
& pos
,
392 : DnDShape(pos
, size
, col
)
394 wxLogMessage(wxT("DnDRectangularShape is being created"));
397 virtual ~DnDRectangularShape()
399 wxLogMessage(wxT("DnDRectangularShape is being deleted"));
402 virtual Kind
GetKind() const { return Rectangle
; }
403 virtual void Draw(wxDC
& dc
)
408 wxPoint
p2(p1
.x
+ m_size
.x
, p1
.y
);
409 wxPoint
p3(p2
.x
, p2
.y
+ m_size
.y
);
410 wxPoint
p4(p1
.x
, p3
.y
);
417 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
418 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
422 class DnDEllipticShape
: public DnDShape
425 DnDEllipticShape(const wxPoint
& pos
,
428 : DnDShape(pos
, size
, col
)
430 wxLogMessage(wxT("DnDEllipticShape is being created"));
433 virtual ~DnDEllipticShape()
435 wxLogMessage(wxT("DnDEllipticShape is being deleted"));
438 virtual Kind
GetKind() const { return Ellipse
; }
439 virtual void Draw(wxDC
& dc
)
443 dc
.DrawEllipse(m_pos
, m_size
);
445 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
446 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
450 // ----------------------------------------------------------------------------
451 // A wxDataObject specialisation for the application-specific data
452 // ----------------------------------------------------------------------------
454 static const wxChar
*shapeFormatId
= wxT("wxShape");
456 class DnDShapeDataObject
: public wxDataObject
459 // ctor doesn't copy the pointer, so it shouldn't go away while this object
461 DnDShapeDataObject(DnDShape
*shape
= (DnDShape
*)NULL
)
465 // we need to copy the shape because the one we're handled may be
466 // deleted while it's still on the clipboard (for example) - and we
467 // reuse the serialisation methods here to copy it
468 void *buf
= malloc(shape
->DnDShape
::GetDataSize());
469 shape
->GetDataHere(buf
);
470 m_shape
= DnDShape
::New(buf
);
480 // this string should uniquely identify our format, but is otherwise
482 m_formatShape
.SetId(shapeFormatId
);
484 // we don't draw the shape to a bitmap until it's really needed (i.e.
485 // we're asked to do so)
488 m_hasMetaFile
= false;
489 #endif // wxUSE_METAFILE
492 virtual ~DnDShapeDataObject() { delete m_shape
; }
494 // after a call to this function, the shape is owned by the caller and it
495 // is responsible for deleting it!
497 // NB: a better solution would be to make DnDShapes ref counted and this
498 // is what should probably be done in a real life program, otherwise
499 // the ownership problems become too complicated really fast
502 DnDShape
*shape
= m_shape
;
504 m_shape
= (DnDShape
*)NULL
;
507 m_hasMetaFile
= false;
508 #endif // wxUSE_METAFILE
513 // implement base class pure virtuals
514 // ----------------------------------
516 virtual wxDataFormat
GetPreferredFormat(Direction
WXUNUSED(dir
)) const
518 return m_formatShape
;
521 virtual size_t GetFormatCount(Direction dir
) const
523 // our custom format is supported by both GetData() and SetData()
527 // but the bitmap format(s) are only supported for output
528 nFormats
+= m_dobjBitmap
.GetFormatCount(dir
);
531 nFormats
+= m_dobjMetaFile
.GetFormatCount(dir
);
532 #endif // wxUSE_METAFILE
538 virtual void GetAllFormats(wxDataFormat
*formats
, Direction dir
) const
540 formats
[0] = m_formatShape
;
543 // in Get direction we additionally support bitmaps and metafiles
545 m_dobjBitmap
.GetAllFormats(&formats
[1], dir
);
548 // don't assume that m_dobjBitmap has only 1 format
549 m_dobjMetaFile
.GetAllFormats(&formats
[1 +
550 m_dobjBitmap
.GetFormatCount(dir
)], dir
);
551 #endif // wxUSE_METAFILE
555 virtual size_t GetDataSize(const wxDataFormat
& format
) const
557 if ( format
== m_formatShape
)
559 return m_shape
->GetDataSize();
562 else if ( m_dobjMetaFile
.IsSupported(format
) )
564 if ( !m_hasMetaFile
)
567 return m_dobjMetaFile
.GetDataSize(format
);
569 #endif // wxUSE_METAFILE
572 wxASSERT_MSG( m_dobjBitmap
.IsSupported(format
),
573 wxT("unexpected format") );
578 return m_dobjBitmap
.GetDataSize();
582 virtual bool GetDataHere(const wxDataFormat
& format
, void *pBuf
) const
584 if ( format
== m_formatShape
)
586 m_shape
->GetDataHere(pBuf
);
591 else if ( m_dobjMetaFile
.IsSupported(format
) )
593 if ( !m_hasMetaFile
)
596 return m_dobjMetaFile
.GetDataHere(format
, pBuf
);
598 #endif // wxUSE_METAFILE
601 wxASSERT_MSG( m_dobjBitmap
.IsSupported(format
),
602 wxT("unexpected format") );
607 return m_dobjBitmap
.GetDataHere(pBuf
);
611 virtual bool SetData(const wxDataFormat
& format
,
612 size_t WXUNUSED(len
), const void *buf
)
614 wxCHECK_MSG( format
== m_formatShape
, false,
615 wxT( "unsupported format") );
618 m_shape
= DnDShape
::New(buf
);
620 // the shape has changed
624 m_hasMetaFile
= false;
625 #endif // wxUSE_METAFILE
631 // creates a bitmap and assigns it to m_dobjBitmap (also sets m_hasBitmap)
632 void CreateBitmap() const;
634 void CreateMetaFile() const;
635 #endif // wxUSE_METAFILE
637 wxDataFormat m_formatShape
; // our custom format
639 wxBitmapDataObject m_dobjBitmap
; // it handles bitmaps
640 bool m_hasBitmap
; // true if m_dobjBitmap has valid bitmap
643 wxMetaFileDataObject m_dobjMetaFile
;// handles metafiles
644 bool m_hasMetaFile
; // true if we have valid metafile
645 #endif // wxUSE_METAFILE
647 DnDShape
*m_shape
; // our data
650 // ----------------------------------------------------------------------------
651 // A dialog to edit shape properties
652 // ----------------------------------------------------------------------------
654 class DnDShapeDialog
: public wxDialog
657 DnDShapeDialog(wxFrame
*parent
, DnDShape
*shape
);
659 DnDShape
*GetShape() const;
661 virtual bool TransferDataToWindow();
662 virtual bool TransferDataFromWindow();
664 void OnColour(wxCommandEvent
& event
);
671 DnDShape
::Kind m_shapeKind
;
683 DECLARE_EVENT_TABLE()
686 // ----------------------------------------------------------------------------
687 // A frame for the shapes which can be drag-and-dropped between frames
688 // ----------------------------------------------------------------------------
690 class DnDShapeFrame
: public wxFrame
693 DnDShapeFrame(wxFrame
*parent
);
696 void SetShape(DnDShape
*shape
);
697 virtual bool SetShape(const wxRegion
®ion
)
699 return wxFrame
::SetShape( region
);
703 void OnNewShape(wxCommandEvent
& event
);
704 void OnEditShape(wxCommandEvent
& event
);
705 void OnClearShape(wxCommandEvent
& event
);
707 void OnCopyShape(wxCommandEvent
& event
);
708 void OnPasteShape(wxCommandEvent
& event
);
710 void OnUpdateUICopy(wxUpdateUIEvent
& event
);
711 void OnUpdateUIPaste(wxUpdateUIEvent
& event
);
713 void OnDrag(wxMouseEvent
& event
);
714 void OnPaint(wxPaintEvent
& event
);
715 void OnDrop(wxCoord x
, wxCoord y
, DnDShape
*shape
);
720 static DnDShapeFrame
*ms_lastDropTarget
;
722 DECLARE_EVENT_TABLE()
725 // ----------------------------------------------------------------------------
726 // wxDropTarget derivation for DnDShapes
727 // ----------------------------------------------------------------------------
729 class DnDShapeDropTarget
: public wxDropTarget
732 DnDShapeDropTarget(DnDShapeFrame
*frame
)
733 : wxDropTarget(new DnDShapeDataObject
)
738 // override base class (pure) virtuals
739 virtual wxDragResult
OnEnter(wxCoord x
, wxCoord y
, wxDragResult def
)
742 m_frame
->SetStatusText(_T("Mouse entered the frame"));
743 #endif // wxUSE_STATUSBAR
744 return OnDragOver(x
, y
, def
);
746 virtual void OnLeave()
749 m_frame
->SetStatusText(_T("Mouse left the frame"));
750 #endif // wxUSE_STATUSBAR
752 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
756 wxLogError(wxT("Failed to get drag and drop data"));
761 m_frame
->OnDrop(x
, y
,
762 ((DnDShapeDataObject
*)GetDataObject())->GetShape());
768 DnDShapeFrame
*m_frame
;
771 #endif // wxUSE_DRAG_AND_DROP
773 // ----------------------------------------------------------------------------
774 // functions prototypes
775 // ----------------------------------------------------------------------------
777 static void ShowBitmap(const wxBitmap
& bitmap
);
780 static void ShowMetaFile(const wxMetaFile
& metafile
);
781 #endif // wxUSE_METAFILE
783 // ----------------------------------------------------------------------------
784 // IDs for the menu commands
785 // ----------------------------------------------------------------------------
803 Menu_Shape_New
= 500,
806 Menu_ShapeClipboard_Copy
,
807 Menu_ShapeClipboard_Paste
,
811 BEGIN_EVENT_TABLE(DnDFrame
, wxFrame
)
812 EVT_MENU(Menu_Quit
, DnDFrame
::OnQuit
)
813 EVT_MENU(Menu_About
, DnDFrame
::OnAbout
)
814 EVT_MENU(Menu_Drag
, DnDFrame
::OnDrag
)
815 EVT_MENU(Menu_DragMoveDef
, DnDFrame
::OnDragMoveByDefault
)
816 EVT_MENU(Menu_DragMoveAllow
,DnDFrame
::OnDragMoveAllow
)
817 EVT_MENU(Menu_NewFrame
, DnDFrame
::OnNewFrame
)
818 EVT_MENU(Menu_Help
, DnDFrame
::OnHelp
)
820 EVT_MENU(Menu_Clear
, DnDFrame
::OnLogClear
)
822 EVT_MENU(Menu_Copy
, DnDFrame
::OnCopy
)
823 EVT_MENU(Menu_Paste
, DnDFrame
::OnPaste
)
824 EVT_MENU(Menu_CopyBitmap
, DnDFrame
::OnCopyBitmap
)
825 EVT_MENU(Menu_PasteBitmap
,DnDFrame
::OnPasteBitmap
)
827 EVT_MENU(Menu_PasteMFile
, DnDFrame
::OnPasteMetafile
)
828 #endif // wxUSE_METAFILE
829 EVT_MENU(Menu_CopyFiles
, DnDFrame
::OnCopyFiles
)
831 EVT_UPDATE_UI(Menu_DragMoveDef
, DnDFrame
::OnUpdateUIMoveByDefault
)
833 EVT_UPDATE_UI(Menu_Paste
, DnDFrame
::OnUpdateUIPasteText
)
834 EVT_UPDATE_UI(Menu_PasteBitmap
, DnDFrame
::OnUpdateUIPasteBitmap
)
836 EVT_LEFT_DOWN( DnDFrame
::OnLeftDown
)
837 EVT_RIGHT_DOWN( DnDFrame
::OnRightDown
)
838 EVT_PAINT( DnDFrame
::OnPaint
)
839 EVT_SIZE( DnDFrame
::OnSize
)
842 #if wxUSE_DRAG_AND_DROP
844 BEGIN_EVENT_TABLE(DnDShapeFrame
, wxFrame
)
845 EVT_MENU(Menu_Shape_New
, DnDShapeFrame
::OnNewShape
)
846 EVT_MENU(Menu_Shape_Edit
, DnDShapeFrame
::OnEditShape
)
847 EVT_MENU(Menu_Shape_Clear
, DnDShapeFrame
::OnClearShape
)
849 EVT_MENU(Menu_ShapeClipboard_Copy
, DnDShapeFrame
::OnCopyShape
)
850 EVT_MENU(Menu_ShapeClipboard_Paste
, DnDShapeFrame
::OnPasteShape
)
852 EVT_UPDATE_UI(Menu_ShapeClipboard_Copy
, DnDShapeFrame
::OnUpdateUICopy
)
853 EVT_UPDATE_UI(Menu_ShapeClipboard_Paste
, DnDShapeFrame
::OnUpdateUIPaste
)
855 EVT_LEFT_DOWN(DnDShapeFrame
::OnDrag
)
857 EVT_PAINT(DnDShapeFrame
::OnPaint
)
860 BEGIN_EVENT_TABLE(DnDShapeDialog
, wxDialog
)
861 EVT_BUTTON(Button_Colour
, DnDShapeDialog
::OnColour
)
864 #endif // wxUSE_DRAG_AND_DROP
866 BEGIN_EVENT_TABLE(DnDCanvasBitmap
, wxScrolledWindow
)
867 EVT_PAINT(DnDCanvasBitmap
::OnPaint
)
871 BEGIN_EVENT_TABLE(DnDCanvasMetafile
, wxScrolledWindow
)
872 EVT_PAINT(DnDCanvasMetafile
::OnPaint
)
874 #endif // wxUSE_METAFILE
876 #endif // wxUSE_DRAG_AND_DROP
878 // ============================================================================
880 // ============================================================================
882 // `Main program' equivalent, creating windows and returning main app frame
883 bool DnDApp
::OnInit()
885 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
886 // switch on trace messages
888 #if defined(__WXGTK__)
889 wxLog
::AddTraceMask(_T("clipboard"));
890 #elif defined(__WXMSW__)
891 wxLog
::AddTraceMask(wxTRACE_OleCalls
);
896 wxImage
::AddHandler( new wxPNGHandler
);
899 // under X we usually want to use the primary selection by default (which
900 // is shared with other apps)
901 wxTheClipboard
->UsePrimarySelection();
903 // create the main frame window
904 DnDFrame
*frame
= new DnDFrame((wxFrame
*) NULL
,
905 _T("Drag-and-Drop/Clipboard wxWidgets Sample"),
915 wxMessageBox( _T("This sample has to be compiled with wxUSE_DRAG_AND_DROP"), _T("Building error"), wxOK
);
917 #endif // wxUSE_DRAG_AND_DROP
920 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
922 DnDFrame
::DnDFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
)
923 : wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
)),
924 m_strText(_T("wxWidgets drag & drop works :-)"))
927 // frame icon and status bar
928 SetIcon(wxICON(sample
));
932 #endif // wxUSE_STATUSBAR
935 wxMenu
*file_menu
= new wxMenu
;
936 file_menu
->Append(Menu_Drag
, _T("&Test drag..."));
937 file_menu
->AppendCheckItem(Menu_DragMoveDef
, _T("&Move by default"));
938 file_menu
->AppendCheckItem(Menu_DragMoveAllow
, _T("&Allow moving"));
939 file_menu
->AppendSeparator();
940 file_menu
->Append(Menu_NewFrame
, _T("&New frame\tCtrl-N"));
941 file_menu
->AppendSeparator();
942 file_menu
->Append(Menu_Quit
, _T("E&xit\tCtrl-Q"));
945 wxMenu
*log_menu
= new wxMenu
;
946 log_menu
->Append(Menu_Clear
, _T("Clear\tCtrl-L"));
949 wxMenu
*help_menu
= new wxMenu
;
950 help_menu
->Append(Menu_Help
, _T("&Help..."));
951 help_menu
->AppendSeparator();
952 help_menu
->Append(Menu_About
, _T("&About"));
954 wxMenu
*clip_menu
= new wxMenu
;
955 clip_menu
->Append(Menu_Copy
, _T("&Copy text\tCtrl-C"));
956 clip_menu
->Append(Menu_Paste
, _T("&Paste text\tCtrl-V"));
957 clip_menu
->AppendSeparator();
958 clip_menu
->Append(Menu_CopyBitmap
, _T("Copy &bitmap\tCtrl-Shift-C"));
959 clip_menu
->Append(Menu_PasteBitmap
, _T("Paste b&itmap\tCtrl-Shift-V"));
961 clip_menu
->AppendSeparator();
962 clip_menu
->Append(Menu_PasteMFile
, _T("Paste &metafile\tCtrl-M"));
963 #endif // wxUSE_METAFILE
964 clip_menu
->AppendSeparator();
965 clip_menu
->Append(Menu_CopyFiles
, _T("Copy &files\tCtrl-F"));
967 wxMenuBar
*menu_bar
= new wxMenuBar
;
968 menu_bar
->Append(file_menu
, _T("&File"));
970 menu_bar
->Append(log_menu
, _T("&Log"));
972 menu_bar
->Append(clip_menu
, _T("&Clipboard"));
973 menu_bar
->Append(help_menu
, _T("&Help"));
975 SetMenuBar(menu_bar
);
977 // make a panel with 3 subwindows
978 wxString
strFile(_T("Drop files here!")), strText(_T("Drop text on me"));
980 m_ctrlFile
= new wxListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, 1, &strFile
,
981 wxLB_HSCROLL
| wxLB_ALWAYS_SB
);
982 m_ctrlText
= new wxListBox(this, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, 1, &strText
,
983 wxLB_HSCROLL
| wxLB_ALWAYS_SB
);
986 m_ctrlLog
= new wxTextCtrl(this, wxID_ANY
, _T(""), wxDefaultPosition
, wxDefaultSize
,
987 wxTE_MULTILINE
| wxTE_READONLY
|
990 // redirect log messages to the text window
991 m_pLog
= new wxLogTextCtrl(m_ctrlLog
);
992 m_pLogPrev
= wxLog
::SetActiveTarget(m_pLog
);
995 #if wxUSE_DRAG_AND_DROP
996 // associate drop targets with the controls
997 m_ctrlFile
->SetDropTarget(new DnDFile(m_ctrlFile
));
998 m_ctrlText
->SetDropTarget(new DnDText(m_ctrlText
));
1000 m_ctrlLog
->SetDropTarget(new URLDropTarget
);
1002 #endif // wxUSE_DRAG_AND_DROP
1004 wxBoxSizer
*sizer_top
= new wxBoxSizer( wxHORIZONTAL
);
1005 sizer_top
->Add(m_ctrlFile
, 1, wxEXPAND
);
1006 sizer_top
->Add(m_ctrlText
, 1, wxEXPAND
);
1008 wxBoxSizer
*sizer
= new wxBoxSizer( wxVERTICAL
);
1009 sizer
->Add(sizer_top
, 1, wxEXPAND
);
1011 sizer
->Add(m_ctrlLog
, 2, wxEXPAND
);
1012 sizer
->SetItemMinSize(m_ctrlLog
, 450, 0);
1014 sizer
->AddSpacer(50);
1017 sizer
->SetSizeHints( this );
1019 // copy data by default but allow moving it as well
1020 m_moveByDefault
= false;
1022 menu_bar
->Check(Menu_DragMoveAllow
, true);
1025 void DnDFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
1030 void DnDFrame
::OnSize(wxSizeEvent
& event
)
1037 void DnDFrame
::OnPaint(wxPaintEvent
& WXUNUSED(event
))
1041 GetClientSize( &w
, &h
);
1044 // dc.Clear(); -- this kills wxGTK
1045 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
, false, _T("charter") ) );
1046 dc
.DrawText( _T("Drag text from here!"), 100, h
-50 );
1049 void DnDFrame
::OnUpdateUIMoveByDefault(wxUpdateUIEvent
& event
)
1051 // only can move by default if moving is allowed at all
1052 event
.Enable(m_moveAllow
);
1055 void DnDFrame
::OnUpdateUIPasteText(wxUpdateUIEvent
& event
)
1058 // too many trace messages if we don't do it - this function is called
1063 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
1066 void DnDFrame
::OnUpdateUIPasteBitmap(wxUpdateUIEvent
& event
)
1069 // too many trace messages if we don't do it - this function is called
1074 event
.Enable( wxTheClipboard
->IsSupported(wxDF_BITMAP
) );
1077 void DnDFrame
::OnNewFrame(wxCommandEvent
& WXUNUSED(event
))
1079 #if wxUSE_DRAG_AND_DROP
1080 (new DnDShapeFrame(this))->Show(true);
1082 wxLogStatus(this, wxT("Double click the new frame to select a shape for it"));
1083 #endif // wxUSE_DRAG_AND_DROP
1086 void DnDFrame
::OnDrag(wxCommandEvent
& WXUNUSED(event
))
1088 #if wxUSE_DRAG_AND_DROP
1089 wxString strText
= wxGetTextFromUser
1091 _T("After you enter text in this dialog, press any mouse\n")
1092 _T("button in the bottom (empty) part of the frame and \n")
1093 _T("drag it anywhere - you will be in fact dragging the\n")
1094 _T("text object containing this text"),
1095 _T("Please enter some text"), m_strText
, this
1098 m_strText
= strText
;
1099 #endif // wxUSE_DRAG_AND_DROP
1102 void DnDFrame
::OnDragMoveByDefault(wxCommandEvent
& event
)
1104 m_moveByDefault
= event
.IsChecked();
1107 void DnDFrame
::OnDragMoveAllow(wxCommandEvent
& event
)
1109 m_moveAllow
= event
.IsChecked();
1112 void DnDFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
1114 wxMessageBox(_T("Drag-&-Drop Demo\n")
1115 _T("Please see \"Help|Help...\" for details\n")
1116 _T("Copyright (c) 1998 Vadim Zeitlin"),
1118 wxICON_INFORMATION
| wxOK
,
1122 void DnDFrame
::OnHelp(wxCommandEvent
& /* event */)
1124 wxMessageDialog
dialog(this,
1125 _T("This small program demonstrates drag & drop support in wxWidgets. The program window\n")
1126 _T("consists of 3 parts: the bottom pane is for debug messages, so that you can see what's\n")
1127 _T("going on inside. The top part is split into 2 listboxes, the left one accepts files\n")
1128 _T("and the right one accepts text.\n")
1130 _T("To test wxDropTarget: open wordpad (write.exe), select some text in it and drag it to\n")
1131 _T("the right listbox (you'll notice the usual visual feedback, i.e. the cursor will change).\n")
1132 _T("Also, try dragging some files (you can select several at once) from Windows Explorer (or \n")
1133 _T("File Manager) to the left pane. Hold down Ctrl/Shift keys when you drop text (doesn't \n")
1134 _T("work with files) and see what changes.\n")
1136 _T("To test wxDropSource: just press any mouse button on the empty zone of the window and drag\n")
1137 _T("it to wordpad or any other droptarget accepting text (and of course you can just drag it\n")
1138 _T("to the right pane). Due to a lot of trace messages, the cursor might take some time to \n")
1139 _T("change, don't release the mouse button until it does. You can change the string being\n")
1140 _T("dragged in in \"File|Test drag...\" dialog.\n")
1143 _T("Please send all questions/bug reports/suggestions &c to \n")
1144 _T("Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>"),
1151 void DnDFrame
::OnLogClear(wxCommandEvent
& /* event */ )
1154 m_ctrlText
->Clear();
1155 m_ctrlFile
->Clear();
1159 void DnDFrame
::OnLeftDown(wxMouseEvent
&WXUNUSED(event
) )
1161 #if wxUSE_DRAG_AND_DROP
1162 if ( !m_strText
.IsEmpty() )
1164 // start drag operation
1165 wxTextDataObject
textData(m_strText
);
1167 wxFileDataObject textData;
1168 textData.AddFile( "/file1.txt" );
1169 textData.AddFile( "/file2.txt" );
1171 wxDropSource
source(textData
, this,
1172 wxDROP_ICON(dnd_copy
),
1173 wxDROP_ICON(dnd_move
),
1174 wxDROP_ICON(dnd_none
));
1177 if ( m_moveByDefault
)
1178 flags
|= wxDrag_DefaultMove
;
1179 else if ( m_moveAllow
)
1180 flags
|= wxDrag_AllowMove
;
1182 wxDragResult result
= source
.DoDragDrop(flags
);
1188 case wxDragError
: pc
= _T("Error!"); break;
1189 case wxDragNone
: pc
= _T("Nothing"); break;
1190 case wxDragCopy
: pc
= _T("Copied"); break;
1191 case wxDragMove
: pc
= _T("Moved"); break;
1192 case wxDragCancel
: pc
= _T("Cancelled"); break;
1193 default: pc
= _T("Huh?"); break;
1196 SetStatusText(wxString(_T("Drag result: ")) + pc
);
1198 wxUnusedVar(result
);
1199 #endif // wxUSE_STATUSBAR
1201 #endif // wxUSE_DRAG_AND_DROP
1204 void DnDFrame
::OnRightDown(wxMouseEvent
&event
)
1206 wxMenu
menu(_T("Dnd sample menu"));
1208 menu
.Append(Menu_Drag
, _T("&Test drag..."));
1209 menu
.AppendSeparator();
1210 menu
.Append(Menu_About
, _T("&About"));
1212 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
1215 DnDFrame
::~DnDFrame()
1218 if ( m_pLog
!= NULL
) {
1219 if ( wxLog
::SetActiveTarget(m_pLogPrev
) == m_pLog
)
1225 // ---------------------------------------------------------------------------
1227 // ---------------------------------------------------------------------------
1229 void DnDFrame
::OnCopyBitmap(wxCommandEvent
& WXUNUSED(event
))
1231 // PNG support is not always compiled in under Windows, so use BMP there
1233 wxFileDialog
dialog(this, _T("Open a PNG file"), _T(""), _T(""), _T("PNG files (*.png)|*.png"), 0);
1235 wxFileDialog
dialog(this, _T("Open a BMP file"), _T(""), _T(""), _T("BMP files (*.bmp)|*.bmp"), 0);
1238 if (dialog
.ShowModal() != wxID_OK
)
1240 wxLogMessage( _T("Aborted file open") );
1244 if (dialog
.GetPath().IsEmpty())
1246 wxLogMessage( _T("Returned empty string.") );
1250 if (!wxFileExists(dialog
.GetPath()))
1252 wxLogMessage( _T("File doesn't exist.") );
1257 image
.LoadFile( dialog
.GetPath(),
1266 wxLogError( _T("Invalid image file...") );
1270 wxLogStatus( _T("Decoding image file...") );
1273 wxBitmap
bitmap( image
);
1275 if ( !wxTheClipboard
->Open() )
1277 wxLogError(_T("Can't open clipboard."));
1282 wxLogMessage( _T("Creating wxBitmapDataObject...") );
1285 if ( !wxTheClipboard
->AddData(new wxBitmapDataObject(bitmap
)) )
1287 wxLogError(_T("Can't copy image to the clipboard."));
1291 wxLogMessage(_T("Image has been put on the clipboard.") );
1292 wxLogMessage(_T("You can paste it now and look at it.") );
1295 wxTheClipboard
->Close();
1298 void DnDFrame
::OnPasteBitmap(wxCommandEvent
& WXUNUSED(event
))
1300 if ( !wxTheClipboard
->Open() )
1302 wxLogError(_T("Can't open clipboard."));
1307 if ( !wxTheClipboard
->IsSupported(wxDF_BITMAP
) )
1309 wxLogWarning(_T("No bitmap on clipboard"));
1311 wxTheClipboard
->Close();
1315 wxBitmapDataObject data
;
1316 if ( !wxTheClipboard
->GetData(data
) )
1318 wxLogError(_T("Can't paste bitmap from the clipboard"));
1322 const wxBitmap
& bmp
= data
.GetBitmap();
1324 wxLogMessage(_T("Bitmap %dx%d pasted from the clipboard"),
1325 bmp
.GetWidth(), bmp
.GetHeight());
1329 wxTheClipboard
->Close();
1334 void DnDFrame
::OnPasteMetafile(wxCommandEvent
& WXUNUSED(event
))
1336 if ( !wxTheClipboard
->Open() )
1338 wxLogError(_T("Can't open clipboard."));
1343 if ( !wxTheClipboard
->IsSupported(wxDF_METAFILE
) )
1345 wxLogWarning(_T("No metafile on clipboard"));
1349 wxMetaFileDataObject data
;
1350 if ( !wxTheClipboard
->GetData(data
) )
1352 wxLogError(_T("Can't paste metafile from the clipboard"));
1356 const wxMetaFile
& mf
= data
.GetMetafile();
1358 wxLogMessage(_T("Metafile %dx%d pasted from the clipboard"),
1359 mf
.GetWidth(), mf
.GetHeight());
1365 wxTheClipboard
->Close();
1368 #endif // wxUSE_METAFILE
1370 // ----------------------------------------------------------------------------
1372 // ----------------------------------------------------------------------------
1374 void DnDFrame
::OnCopyFiles(wxCommandEvent
& WXUNUSED(event
))
1377 wxFileDialog
dialog(this, _T("Select a file to copy"), _T(""), _T(""),
1378 _T("All files (*.*)|*.*"), 0);
1380 wxArrayString filenames
;
1381 while ( dialog
.ShowModal() == wxID_OK
)
1383 filenames
.Add(dialog
.GetPath());
1386 if ( !filenames
.IsEmpty() )
1388 wxFileDataObject
*dobj
= new wxFileDataObject
;
1389 size_t count
= filenames
.GetCount();
1390 for ( size_t n
= 0; n
< count
; n
++ )
1392 dobj
->AddFile(filenames
[n
]);
1395 wxClipboardLocker locker
;
1398 wxLogError(wxT("Can't open clipboard"));
1402 if ( !wxTheClipboard
->AddData(dobj
) )
1404 wxLogError(wxT("Can't copy file(s) to the clipboard"));
1408 wxLogStatus(this, wxT("%d file%s copied to the clipboard"),
1409 count
, count
== 1 ?
wxT("") : wxT("s"));
1415 wxLogStatus(this, wxT("Aborted"));
1418 wxLogError(wxT("Sorry, not implemented"));
1422 // ---------------------------------------------------------------------------
1424 // ---------------------------------------------------------------------------
1426 void DnDFrame
::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1428 if ( !wxTheClipboard
->Open() )
1430 wxLogError(_T("Can't open clipboard."));
1435 if ( !wxTheClipboard
->AddData(new wxTextDataObject(m_strText
)) )
1437 wxLogError(_T("Can't copy data to the clipboard"));
1441 wxLogMessage(_T("Text '%s' put on the clipboard"), m_strText
.c_str());
1444 wxTheClipboard
->Close();
1447 void DnDFrame
::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1449 if ( !wxTheClipboard
->Open() )
1451 wxLogError(_T("Can't open clipboard."));
1456 if ( !wxTheClipboard
->IsSupported(wxDF_TEXT
) )
1458 wxLogWarning(_T("No text data on clipboard"));
1460 wxTheClipboard
->Close();
1464 wxTextDataObject text
;
1465 if ( !wxTheClipboard
->GetData(text
) )
1467 wxLogError(_T("Can't paste data from the clipboard"));
1471 wxLogMessage(_T("Text '%s' pasted from the clipboard"),
1472 text
.GetText().c_str());
1475 wxTheClipboard
->Close();
1478 #if wxUSE_DRAG_AND_DROP
1480 // ----------------------------------------------------------------------------
1481 // Notifications called by the base class
1482 // ----------------------------------------------------------------------------
1484 bool DnDText
::OnDropText(wxCoord
, wxCoord
, const wxString
& text
)
1486 m_pOwner
->Append(text
);
1491 bool DnDFile
::OnDropFiles(wxCoord
, wxCoord
, const wxArrayString
& filenames
)
1493 size_t nFiles
= filenames
.GetCount();
1495 str
.Printf( _T("%d files dropped"), (int)nFiles
);
1496 m_pOwner
->Append(str
);
1497 for ( size_t n
= 0; n
< nFiles
; n
++ ) {
1498 m_pOwner
->Append(filenames
[n
]);
1504 // ----------------------------------------------------------------------------
1506 // ----------------------------------------------------------------------------
1508 DnDShapeDialog
::DnDShapeDialog(wxFrame
*parent
, DnDShape
*shape
)
1509 :wxDialog( parent
, 6001, wxT("Choose Shape"), wxPoint( 10, 10 ),
1511 wxRAISED_BORDER
|wxCAPTION
|wxTHICK_FRAME
|wxSYSTEM_MENU
)
1514 wxBoxSizer
* topSizer
= new wxBoxSizer( wxVERTICAL
);
1517 wxBoxSizer
* shapesSizer
= new wxBoxSizer( wxHORIZONTAL
);
1518 const wxString choices
[] = { wxT("None"), wxT("Triangle"),
1519 wxT("Rectangle"), wxT("Ellipse") };
1521 m_radio
= new wxRadioBox( this, wxID_ANY
, wxT("&Shape"),
1522 wxDefaultPosition
, wxDefaultSize
, 4, choices
, 4,
1523 wxRA_SPECIFY_COLS
);
1524 shapesSizer
->Add( m_radio
, 0, wxGROW
|wxALL
, 5 );
1525 topSizer
->Add( shapesSizer
, 0, wxALL
, 2 );
1528 wxStaticBox
* box
= new wxStaticBox( this, wxID_ANY
, wxT("&Attributes") );
1529 wxStaticBoxSizer
* attrSizer
= new wxStaticBoxSizer( box
, wxHORIZONTAL
);
1530 wxFlexGridSizer
* xywhSizer
= new wxFlexGridSizer( 4, 2 );
1534 st
= new wxStaticText( this, wxID_ANY
, wxT("Position &X:") );
1535 m_textX
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1537 xywhSizer
->Add( st
, 1, wxGROW
|wxALL
, 2 );
1538 xywhSizer
->Add( m_textX
, 1, wxGROW
|wxALL
, 2 );
1540 st
= new wxStaticText( this, wxID_ANY
, wxT("Size &width:") );
1541 m_textW
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1543 xywhSizer
->Add( st
, 1, wxGROW
|wxALL
, 2 );
1544 xywhSizer
->Add( m_textW
, 1, wxGROW
|wxALL
, 2 );
1546 st
= new wxStaticText( this, wxID_ANY
, wxT("&Y:") );
1547 m_textY
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1549 xywhSizer
->Add( st
, 1, wxALL
|wxALIGN_RIGHT
, 2 );
1550 xywhSizer
->Add( m_textY
, 1, wxGROW
|wxALL
, 2 );
1552 st
= new wxStaticText( this, wxID_ANY
, wxT("&height:") );
1553 m_textH
= new wxTextCtrl( this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
,
1555 xywhSizer
->Add( st
, 1, wxALL
|wxALIGN_RIGHT
, 2 );
1556 xywhSizer
->Add( m_textH
, 1, wxGROW
|wxALL
, 2 );
1558 wxButton
* col
= new wxButton( this, Button_Colour
, wxT("&Colour...") );
1559 attrSizer
->Add( xywhSizer
, 1, wxGROW
);
1560 attrSizer
->Add( col
, 0, wxALL
|wxALIGN_CENTRE_VERTICAL
, 2 );
1561 topSizer
->Add( attrSizer
, 0, wxGROW
|wxALL
, 5 );
1564 wxBoxSizer
* buttonSizer
= new wxBoxSizer( wxHORIZONTAL
);
1566 bt
= new wxButton( this, wxID_OK
, wxT("Ok") );
1567 buttonSizer
->Add( bt
, 0, wxALL
, 2 );
1568 bt
= new wxButton( this, wxID_CANCEL
, wxT("Cancel") );
1569 buttonSizer
->Add( bt
, 0, wxALL
, 2 );
1570 topSizer
->Add( buttonSizer
, 0, wxALL
|wxALIGN_RIGHT
, 2 );
1572 SetSizer( topSizer
);
1573 topSizer
->Fit( this );
1576 DnDShape
*DnDShapeDialog
::GetShape() const
1578 switch ( m_shapeKind
)
1581 case DnDShape
::None
: return NULL
;
1582 case DnDShape
::Triangle
: return new DnDTriangularShape(m_pos
, m_size
, m_col
);
1583 case DnDShape
::Rectangle
: return new DnDRectangularShape(m_pos
, m_size
, m_col
);
1584 case DnDShape
::Ellipse
: return new DnDEllipticShape(m_pos
, m_size
, m_col
);
1588 bool DnDShapeDialog
::TransferDataToWindow()
1593 m_radio
->SetSelection(m_shape
->GetKind());
1594 m_pos
= m_shape
->GetPosition();
1595 m_size
= m_shape
->GetSize();
1596 m_col
= m_shape
->GetColour();
1600 m_radio
->SetSelection(DnDShape
::None
);
1601 m_pos
= wxPoint(1, 1);
1602 m_size
= wxSize(100, 100);
1605 m_textX
->SetValue(wxString() << m_pos
.x
);
1606 m_textY
->SetValue(wxString() << m_pos
.y
);
1607 m_textW
->SetValue(wxString() << m_size
.x
);
1608 m_textH
->SetValue(wxString() << m_size
.y
);
1613 bool DnDShapeDialog
::TransferDataFromWindow()
1615 m_shapeKind
= (DnDShape
::Kind
)m_radio
->GetSelection();
1617 m_pos
.x
= wxAtoi(m_textX
->GetValue());
1618 m_pos
.y
= wxAtoi(m_textY
->GetValue());
1619 m_size
.x
= wxAtoi(m_textW
->GetValue());
1620 m_size
.y
= wxAtoi(m_textH
->GetValue());
1622 if ( !m_pos
.x
|| !m_pos
.y
|| !m_size
.x
|| !m_size
.y
)
1624 wxMessageBox(_T("All sizes and positions should be non null!"),
1625 _T("Invalid shape"), wxICON_HAND
| wxOK
, this);
1633 void DnDShapeDialog
::OnColour(wxCommandEvent
& WXUNUSED(event
))
1636 data
.SetChooseFull(true);
1637 for (int i
= 0; i
< 16; i
++)
1639 wxColour
colour((unsigned char)(i
*16), (unsigned char)(i
*16), (unsigned char)(i
*16));
1640 data
.SetCustomColour(i
, colour
);
1643 wxColourDialog
dialog(this, &data
);
1644 if ( dialog
.ShowModal() == wxID_OK
)
1646 m_col
= dialog
.GetColourData().GetColour();
1650 // ----------------------------------------------------------------------------
1652 // ----------------------------------------------------------------------------
1654 DnDShapeFrame
*DnDShapeFrame
::ms_lastDropTarget
= NULL
;
1656 DnDShapeFrame
::DnDShapeFrame(wxFrame
*parent
)
1657 : wxFrame(parent
, wxID_ANY
, _T("Shape Frame"))
1661 #endif // wxUSE_STATUSBAR
1663 wxMenu
*menuShape
= new wxMenu
;
1664 menuShape
->Append(Menu_Shape_New
, _T("&New default shape\tCtrl-S"));
1665 menuShape
->Append(Menu_Shape_Edit
, _T("&Edit shape\tCtrl-E"));
1666 menuShape
->AppendSeparator();
1667 menuShape
->Append(Menu_Shape_Clear
, _T("&Clear shape\tCtrl-L"));
1669 wxMenu
*menuClipboard
= new wxMenu
;
1670 menuClipboard
->Append(Menu_ShapeClipboard_Copy
, _T("&Copy\tCtrl-C"));
1671 menuClipboard
->Append(Menu_ShapeClipboard_Paste
, _T("&Paste\tCtrl-V"));
1673 wxMenuBar
*menubar
= new wxMenuBar
;
1674 menubar
->Append(menuShape
, _T("&Shape"));
1675 menubar
->Append(menuClipboard
, _T("&Clipboard"));
1677 SetMenuBar(menubar
);
1680 SetStatusText(_T("Press Ctrl-S to create a new shape"));
1681 #endif // wxUSE_STATUSBAR
1683 SetDropTarget(new DnDShapeDropTarget(this));
1687 SetBackgroundColour(*wxWHITE
);
1690 DnDShapeFrame
::~DnDShapeFrame()
1696 void DnDShapeFrame
::SetShape(DnDShape
*shape
)
1705 void DnDShapeFrame
::OnDrag(wxMouseEvent
& event
)
1714 // start drag operation
1715 DnDShapeDataObject
shapeData(m_shape
);
1716 wxDropSource
source(shapeData
, this);
1718 const wxChar
*pc
= NULL
;
1719 switch ( source
.DoDragDrop(true) )
1723 wxLogError(wxT("An error occurred during drag and drop operation"));
1728 SetStatusText(_T("Nothing happened"));
1729 #endif // wxUSE_STATUSBAR
1738 if ( ms_lastDropTarget
!= this )
1740 // don't delete the shape if we dropped it on ourselves!
1747 SetStatusText(_T("Drag and drop operation cancelled"));
1748 #endif // wxUSE_STATUSBAR
1755 SetStatusText(wxString(_T("Shape successfully ")) + pc
);
1756 #endif // wxUSE_STATUSBAR
1758 //else: status text already set
1761 void DnDShapeFrame
::OnDrop(wxCoord x
, wxCoord y
, DnDShape
*shape
)
1763 ms_lastDropTarget
= this;
1769 s
.Printf(wxT("Shape dropped at (%d, %d)"), pt
.x
, pt
.y
);
1771 #endif // wxUSE_STATUSBAR
1777 void DnDShapeFrame
::OnEditShape(wxCommandEvent
& WXUNUSED(event
))
1779 DnDShapeDialog
dlg(this, m_shape
);
1780 if ( dlg
.ShowModal() == wxID_OK
)
1782 SetShape(dlg
.GetShape());
1787 SetStatusText(_T("You can now drag the shape to another frame"));
1789 #endif // wxUSE_STATUSBAR
1793 void DnDShapeFrame
::OnNewShape(wxCommandEvent
& WXUNUSED(event
))
1795 SetShape(new DnDEllipticShape(wxPoint(10, 10), wxSize(80, 60), *wxRED
));
1798 SetStatusText(_T("You can now drag the shape to another frame"));
1799 #endif // wxUSE_STATUSBAR
1802 void DnDShapeFrame
::OnClearShape(wxCommandEvent
& WXUNUSED(event
))
1807 void DnDShapeFrame
::OnCopyShape(wxCommandEvent
& WXUNUSED(event
))
1811 wxClipboardLocker clipLocker
;
1814 wxLogError(wxT("Can't open the clipboard"));
1819 wxTheClipboard
->AddData(new DnDShapeDataObject(m_shape
));
1823 void DnDShapeFrame
::OnPasteShape(wxCommandEvent
& WXUNUSED(event
))
1825 wxClipboardLocker clipLocker
;
1828 wxLogError(wxT("Can't open the clipboard"));
1833 DnDShapeDataObject
shapeDataObject(NULL
);
1834 if ( wxTheClipboard
->GetData(shapeDataObject
) )
1836 SetShape(shapeDataObject
.GetShape());
1840 wxLogStatus(wxT("No shape on the clipboard"));
1844 void DnDShapeFrame
::OnUpdateUICopy(wxUpdateUIEvent
& event
)
1846 event
.Enable( m_shape
!= NULL
);
1849 void DnDShapeFrame
::OnUpdateUIPaste(wxUpdateUIEvent
& event
)
1851 event
.Enable( wxTheClipboard
->IsSupported(wxDataFormat(shapeFormatId
)) );
1854 void DnDShapeFrame
::OnPaint(wxPaintEvent
& event
)
1868 // ----------------------------------------------------------------------------
1870 // ----------------------------------------------------------------------------
1872 DnDShape
*DnDShape
::New(const void *buf
)
1874 const ShapeDump
& dump
= *(const ShapeDump
*)buf
;
1878 return new DnDTriangularShape(wxPoint(dump
.x
, dump
.y
),
1879 wxSize(dump
.w
, dump
.h
),
1880 wxColour(dump
.r
, dump
.g
, dump
.b
));
1883 return new DnDRectangularShape(wxPoint(dump
.x
, dump
.y
),
1884 wxSize(dump
.w
, dump
.h
),
1885 wxColour(dump
.r
, dump
.g
, dump
.b
));
1888 return new DnDEllipticShape(wxPoint(dump
.x
, dump
.y
),
1889 wxSize(dump
.w
, dump
.h
),
1890 wxColour(dump
.r
, dump
.g
, dump
.b
));
1893 wxFAIL_MSG(wxT("invalid shape!"));
1898 // ----------------------------------------------------------------------------
1899 // DnDShapeDataObject
1900 // ----------------------------------------------------------------------------
1904 void DnDShapeDataObject
::CreateMetaFile() const
1906 wxPoint pos
= m_shape
->GetPosition();
1907 wxSize size
= m_shape
->GetSize();
1909 wxMetaFileDC
dcMF(wxEmptyString
, pos
.x
+ size
.x
, pos
.y
+ size
.y
);
1911 m_shape
->Draw(dcMF
);
1913 wxMetafile
*mf
= dcMF
.Close();
1915 DnDShapeDataObject
*self
= (DnDShapeDataObject
*)this; // const_cast
1916 self
->m_dobjMetaFile
.SetMetafile(*mf
);
1917 self
->m_hasMetaFile
= true;
1922 #endif // wxUSE_METAFILE
1924 void DnDShapeDataObject
::CreateBitmap() const
1926 wxPoint pos
= m_shape
->GetPosition();
1927 wxSize size
= m_shape
->GetSize();
1928 int x
= pos
.x
+ size
.x
,
1930 wxBitmap
bitmap(x
, y
);
1932 dc
.SelectObject(bitmap
);
1933 dc
.SetBrush(wxBrush(wxT("white"), wxSOLID
));
1936 dc
.SelectObject(wxNullBitmap
);
1938 DnDShapeDataObject
*self
= (DnDShapeDataObject
*)this; // const_cast
1939 self
->m_dobjBitmap
.SetBitmap(bitmap
);
1940 self
->m_hasBitmap
= true;
1943 #endif // wxUSE_DRAG_AND_DROP
1945 // ----------------------------------------------------------------------------
1947 // ----------------------------------------------------------------------------
1949 static void ShowBitmap(const wxBitmap
& bitmap
)
1951 wxFrame
*frame
= new wxFrame(NULL
, wxID_ANY
, _T("Bitmap view"));
1953 frame
->CreateStatusBar();
1954 #endif // wxUSE_STATUSBAR
1955 DnDCanvasBitmap
*canvas
= new DnDCanvasBitmap(frame
);
1956 canvas
->SetBitmap(bitmap
);
1958 int w
= bitmap
.GetWidth(),
1959 h
= bitmap
.GetHeight();
1961 frame
->SetStatusText(wxString
::Format(_T("%dx%d"), w
, h
));
1962 #endif // wxUSE_STATUSBAR
1964 frame
->SetClientSize(w
> 100 ?
100 : w
, h
> 100 ?
100 : h
);
1970 static void ShowMetaFile(const wxMetaFile
& metafile
)
1972 wxFrame
*frame
= new wxFrame(NULL
, wxID_ANY
, _T("Metafile view"));
1973 frame
->CreateStatusBar();
1974 DnDCanvasMetafile
*canvas
= new DnDCanvasMetafile(frame
);
1975 canvas
->SetMetafile(metafile
);
1977 wxSize size
= metafile
.GetSize();
1978 frame
->SetStatusText(wxString
::Format(_T("%dx%d"), size
.x
, size
.y
));
1980 frame
->SetClientSize(size
.x
> 100 ?
100 : size
.x
,
1981 size
.y
> 100 ?
100 : size
.y
);
1985 #endif // wxUSE_METAFILE
1987 #endif // wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD