1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Drag and drop sample
4 // Author: Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
22 #if !wxUSE_DRAG_AND_DROP
23 #error This sample requires drag and drop support in the library
26 // under Windows we also support data transfer of metafiles as an extra bonus,
27 // but they're not available under other platforms
36 #include "wx/dirdlg.h"
37 #include "wx/filedlg.h"
39 #include "wx/clipbrd.h"
40 #include "wx/colordlg.h"
44 #include "wx/metafile.h"
47 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
48 #include "mondrian.xpm"
50 #include "dnd_copy.xpm"
51 #include "dnd_move.xpm"
52 #include "dnd_none.xpm"
55 // ----------------------------------------------------------------------------
56 // Derive two simple classes which just put in the listbox the strings (text or
57 // file names) we drop on them
58 // ----------------------------------------------------------------------------
60 class DnDText
: public wxTextDropTarget
63 DnDText(wxListBox
*pOwner
) { m_pOwner
= pOwner
; }
65 virtual bool OnDropText(wxCoord x
, wxCoord y
, const wxString
& text
);
71 class DnDFile
: public wxFileDropTarget
74 DnDFile(wxListBox
*pOwner
) { m_pOwner
= pOwner
; }
76 virtual bool OnDropFiles(wxCoord x
, wxCoord y
,
77 const wxArrayString
& filenames
);
83 // ----------------------------------------------------------------------------
84 // Define a custom dtop target accepting URLs
85 // ----------------------------------------------------------------------------
87 class URLDropTarget
: public wxDropTarget
90 URLDropTarget() { SetDataObject(new wxURLDataObject
); }
92 void OnDropURL(wxCoord x
, wxCoord y
, const wxString
& text
)
94 // of course, a real program would do something more useful here...
95 wxMessageBox(text
, _T("wxDnD sample: got URL"),
96 wxICON_INFORMATION
| wxOK
);
99 // URLs can't be moved, only copied
100 virtual wxDragResult
OnDragOver(wxCoord
WXUNUSED(x
), wxCoord
WXUNUSED(y
),
103 return wxDragLink
; // At least IE 5.x needs wxDragLink, the
104 // other browsers on MSW seem okay with it too.
107 // translate this to calls to OnDropURL() just for convenience
108 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
113 OnDropURL(x
, y
, ((wxURLDataObject
*)m_dataObject
)->GetURL());
119 // ----------------------------------------------------------------------------
120 // Define a new application type
121 // ----------------------------------------------------------------------------
123 class DnDApp
: public wxApp
126 virtual bool OnInit();
129 IMPLEMENT_APP(DnDApp
);
131 // ----------------------------------------------------------------------------
132 // Define canvas class to show a bitmap
133 // ----------------------------------------------------------------------------
135 class DnDCanvasBitmap
: public wxScrolledWindow
138 DnDCanvasBitmap(wxWindow
*parent
) : wxScrolledWindow(parent
) { }
140 void SetBitmap(const wxBitmap
& bitmap
)
144 SetScrollbars(10, 10,
145 m_bitmap
.GetWidth() / 10, m_bitmap
.GetHeight() / 10);
150 void OnPaint(wxPaintEvent
& event
)
158 dc
.DrawBitmap(m_bitmap
, 0, 0);
165 DECLARE_EVENT_TABLE()
170 // and the same thing fo metafiles
171 class DnDCanvasMetafile
: public wxScrolledWindow
174 DnDCanvasMetafile(wxWindow
*parent
) : wxScrolledWindow(parent
) { }
176 void SetMetafile(const wxMetafile
& metafile
)
178 m_metafile
= metafile
;
180 SetScrollbars(10, 10,
181 m_metafile
.GetWidth() / 10, m_metafile
.GetHeight() / 10);
186 void OnPaint(wxPaintEvent
& event
)
190 if ( m_metafile
.Ok() )
194 m_metafile
.Play(&dc
);
199 wxMetafile m_metafile
;
201 DECLARE_EVENT_TABLE()
204 #endif // USE_METAFILES
206 // ----------------------------------------------------------------------------
207 // Define a new frame type for the main frame
208 // ----------------------------------------------------------------------------
210 class DnDFrame
: public wxFrame
213 DnDFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
216 void OnPaint(wxPaintEvent
& event
);
217 void OnSize(wxSizeEvent
& event
);
218 void OnQuit(wxCommandEvent
& event
);
219 void OnAbout(wxCommandEvent
& event
);
220 void OnDrag(wxCommandEvent
& event
);
221 void OnDragMoveByDefault(wxCommandEvent
& event
);
222 void OnDragMoveAllow(wxCommandEvent
& event
);
223 void OnNewFrame(wxCommandEvent
& event
);
224 void OnHelp (wxCommandEvent
& event
);
225 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 // USE_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
,
253 wxTextCtrl
*m_ctrlLog
;
258 // move the text by default (or copy)?
259 bool m_moveByDefault
;
261 // allow moving the text at all?
268 // ----------------------------------------------------------------------------
269 // A shape is an example of application-specific data which may be transported
270 // via drag-and-drop or clipboard: in our case, we have different geometric
271 // shapes, each one with its own colour and position
272 // ----------------------------------------------------------------------------
285 DnDShape(const wxPoint
& pos
,
288 : m_pos(pos
), m_size(size
), m_col(col
)
292 // this is for debugging - lets us see when exactly an object is freed
293 // (this may be later than you think if it's on the clipboard, for example)
294 virtual ~DnDShape() { }
296 // the functions used for drag-and-drop: they dump and restore a shape into
297 // some bitwise-copiable data (might use streams too...)
298 // ------------------------------------------------------------------------
300 // restore from buffer
301 static DnDShape
*New(const void *buf
);
303 virtual size_t GetDataSize() const
305 return sizeof(ShapeDump
);
308 virtual void GetDataHere(void *buf
) const
310 ShapeDump
& dump
= *(ShapeDump
*)buf
;
315 dump
.r
= m_col
.Red();
316 dump
.g
= m_col
.Green();
317 dump
.b
= m_col
.Blue();
322 const wxPoint
& GetPosition() const { return m_pos
; }
323 const wxColour
& GetColour() const { return m_col
; }
324 const wxSize
& GetSize() const { return m_size
; }
326 void Move(const wxPoint
& pos
) { m_pos
= pos
; }
328 // to implement in derived classes
329 virtual Kind
GetKind() const = 0;
331 virtual void Draw(wxDC
& dc
)
333 dc
.SetPen(wxPen(m_col
, 1, wxSOLID
));
337 //get a point 1 up and 1 left, otherwise the mid-point of a triangle is on the line
338 wxPoint
GetCentre() const
339 { return wxPoint(m_pos
.x
+ m_size
.x
/ 2 - 1, m_pos
.y
+ m_size
.y
/ 2 - 1); }
343 int x
, y
, // position
354 class DnDTriangularShape
: public DnDShape
357 DnDTriangularShape(const wxPoint
& pos
,
360 : DnDShape(pos
, size
, col
)
362 wxLogMessage(wxT("DnDTriangularShape is being created"));
365 virtual ~DnDTriangularShape()
367 wxLogMessage(wxT("DnDTriangularShape is being deleted"));
370 virtual Kind
GetKind() const { return Triangle
; }
371 virtual void Draw(wxDC
& dc
)
375 // well, it's a bit difficult to describe a triangle by position and
376 // size, but we're not doing geometry here, do we? ;-)
378 wxPoint
p2(m_pos
.x
+ m_size
.x
, m_pos
.y
);
379 wxPoint
p3(m_pos
.x
, m_pos
.y
+ m_size
.y
);
385 //works in multicolor modes; on GTK (at least) will fail in 16-bit color
386 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
387 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
391 class DnDRectangularShape
: public DnDShape
394 DnDRectangularShape(const wxPoint
& pos
,
397 : DnDShape(pos
, size
, col
)
399 wxLogMessage(wxT("DnDRectangularShape is being created"));
402 virtual ~DnDRectangularShape()
404 wxLogMessage(wxT("DnDRectangularShape is being deleted"));
407 virtual Kind
GetKind() const { return Rectangle
; }
408 virtual void Draw(wxDC
& dc
)
413 wxPoint
p2(p1
.x
+ m_size
.x
, p1
.y
);
414 wxPoint
p3(p2
.x
, p2
.y
+ m_size
.y
);
415 wxPoint
p4(p1
.x
, p3
.y
);
422 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
423 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
427 class DnDEllipticShape
: public DnDShape
430 DnDEllipticShape(const wxPoint
& pos
,
433 : DnDShape(pos
, size
, col
)
435 wxLogMessage(wxT("DnDEllipticShape is being created"));
438 virtual ~DnDEllipticShape()
440 wxLogMessage(wxT("DnDEllipticShape is being deleted"));
443 virtual Kind
GetKind() const { return Ellipse
; }
444 virtual void Draw(wxDC
& dc
)
448 dc
.DrawEllipse(m_pos
, m_size
);
450 dc
.SetBrush(wxBrush(m_col
, wxSOLID
));
451 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
455 // ----------------------------------------------------------------------------
456 // A wxDataObject specialisation for the application-specific data
457 // ----------------------------------------------------------------------------
459 static const wxChar
*shapeFormatId
= wxT("wxShape");
461 class DnDShapeDataObject
: public wxDataObject
464 // ctor doesn't copy the pointer, so it shouldn't go away while this object
466 DnDShapeDataObject(DnDShape
*shape
= (DnDShape
*)NULL
)
470 // we need to copy the shape because the one we're handled may be
471 // deleted while it's still on the clipboard (for example) - and we
472 // reuse the serialisation methods here to copy it
473 void *buf
= malloc(shape
->DnDShape::GetDataSize());
474 shape
->GetDataHere(buf
);
475 m_shape
= DnDShape::New(buf
);
485 // this string should uniquely identify our format, but is otherwise
487 m_formatShape
.SetId(shapeFormatId
);
489 // we don't draw the shape to a bitmap until it's really needed (i.e.
490 // we're asked to do so)
493 m_hasMetaFile
= FALSE
;
497 virtual ~DnDShapeDataObject() { delete m_shape
; }
499 // after a call to this function, the shape is owned by the caller and it
500 // is responsible for deleting it!
502 // NB: a better solution would be to make DnDShapes ref counted and this
503 // is what should probably be done in a real life program, otherwise
504 // the ownership problems become too complicated really fast
507 DnDShape
*shape
= m_shape
;
509 m_shape
= (DnDShape
*)NULL
;
512 m_hasMetaFile
= FALSE
;
518 // implement base class pure virtuals
519 // ----------------------------------
521 virtual wxDataFormat
GetPreferredFormat(Direction
WXUNUSED(dir
)) const
523 return m_formatShape
;
526 virtual size_t GetFormatCount(Direction dir
) const
528 // our custom format is supported by both GetData() and SetData()
532 // but the bitmap format(s) are only supported for output
533 nFormats
+= m_dobjBitmap
.GetFormatCount(dir
);
536 nFormats
+= m_dobjMetaFile
.GetFormatCount(dir
);
543 virtual void GetAllFormats(wxDataFormat
*formats
, Direction dir
) const
545 formats
[0] = m_formatShape
;
548 // in Get direction we additionally support bitmaps and metafiles
550 m_dobjBitmap
.GetAllFormats(&formats
[1], dir
);
553 // don't assume that m_dobjBitmap has only 1 format
554 m_dobjMetaFile
.GetAllFormats(&formats
[1 +
555 m_dobjBitmap
.GetFormatCount(dir
)], dir
);
560 virtual size_t GetDataSize(const wxDataFormat
& format
) const
562 if ( format
== m_formatShape
)
564 return m_shape
->GetDataSize();
567 else if ( m_dobjMetaFile
.IsSupported(format
) )
569 if ( !m_hasMetaFile
)
572 return m_dobjMetaFile
.GetDataSize(format
);
577 wxASSERT_MSG( m_dobjBitmap
.IsSupported(format
),
578 wxT("unexpected format") );
583 return m_dobjBitmap
.GetDataSize();
587 virtual bool GetDataHere(const wxDataFormat
& format
, void *pBuf
) const
589 if ( format
== m_formatShape
)
591 m_shape
->GetDataHere(pBuf
);
596 else if ( m_dobjMetaFile
.IsSupported(format
) )
598 if ( !m_hasMetaFile
)
601 return m_dobjMetaFile
.GetDataHere(format
, pBuf
);
606 wxASSERT_MSG( m_dobjBitmap
.IsSupported(format
),
607 wxT("unexpected format") );
612 return m_dobjBitmap
.GetDataHere(pBuf
);
616 virtual bool SetData(const wxDataFormat
& format
,
617 size_t len
, const void *buf
)
619 wxCHECK_MSG( format
== m_formatShape
, FALSE
,
620 wxT( "unsupported format") );
623 m_shape
= DnDShape::New(buf
);
625 // the shape has changed
629 m_hasMetaFile
= FALSE
;
636 // creates a bitmap and assigns it to m_dobjBitmap (also sets m_hasBitmap)
637 void CreateBitmap() const;
639 void CreateMetaFile() const;
642 wxDataFormat m_formatShape
; // our custom format
644 wxBitmapDataObject m_dobjBitmap
; // it handles bitmaps
645 bool m_hasBitmap
; // true if m_dobjBitmap has valid bitmap
648 wxMetaFileDataObject m_dobjMetaFile
;// handles metafiles
649 bool m_hasMetaFile
; // true if we have valid metafile
652 DnDShape
*m_shape
; // our data
655 // ----------------------------------------------------------------------------
656 // A dialog to edit shape properties
657 // ----------------------------------------------------------------------------
659 class DnDShapeDialog
: public wxDialog
662 DnDShapeDialog(wxFrame
*parent
, DnDShape
*shape
);
664 DnDShape
*GetShape() const;
666 virtual bool TransferDataToWindow();
667 virtual bool TransferDataFromWindow();
669 void OnColour(wxCommandEvent
& event
);
676 DnDShape::Kind m_shapeKind
;
688 DECLARE_EVENT_TABLE()
691 // ----------------------------------------------------------------------------
692 // A frame for the shapes which can be drag-and-dropped between frames
693 // ----------------------------------------------------------------------------
695 class DnDShapeFrame
: public wxFrame
698 DnDShapeFrame(wxFrame
*parent
);
701 void SetShape(DnDShape
*shape
);
704 void OnNewShape(wxCommandEvent
& event
);
705 void OnEditShape(wxCommandEvent
& event
);
706 void OnClearShape(wxCommandEvent
& event
);
708 void OnCopyShape(wxCommandEvent
& event
);
709 void OnPasteShape(wxCommandEvent
& event
);
711 void OnUpdateUICopy(wxUpdateUIEvent
& event
);
712 void OnUpdateUIPaste(wxUpdateUIEvent
& event
);
714 void OnDrag(wxMouseEvent
& event
);
715 void OnPaint(wxPaintEvent
& event
);
716 void OnDrop(wxCoord x
, wxCoord y
, DnDShape
*shape
);
721 static DnDShapeFrame
*ms_lastDropTarget
;
723 DECLARE_EVENT_TABLE()
726 // ----------------------------------------------------------------------------
727 // wxDropTarget derivation for DnDShapes
728 // ----------------------------------------------------------------------------
730 class DnDShapeDropTarget
: public wxDropTarget
733 DnDShapeDropTarget(DnDShapeFrame
*frame
)
734 : wxDropTarget(new DnDShapeDataObject
)
739 // override base class (pure) virtuals
740 virtual wxDragResult
OnEnter(wxCoord x
, wxCoord y
, wxDragResult def
)
741 { m_frame
->SetStatusText(_T("Mouse entered the frame")); return OnDragOver(x
, y
, def
); }
742 virtual void OnLeave()
743 { m_frame
->SetStatusText(_T("Mouse left the frame")); }
744 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
748 wxLogError(wxT("Failed to get drag and drop data"));
753 m_frame
->OnDrop(x
, y
,
754 ((DnDShapeDataObject
*)GetDataObject())->GetShape());
760 DnDShapeFrame
*m_frame
;
763 // ----------------------------------------------------------------------------
764 // functions prototypes
765 // ----------------------------------------------------------------------------
767 static void ShowBitmap(const wxBitmap
& bitmap
);
770 static void ShowMetaFile(const wxMetaFile
& metafile
);
771 #endif // USE_METAFILES
773 // ----------------------------------------------------------------------------
774 // IDs for the menu commands
775 // ----------------------------------------------------------------------------
793 Menu_Shape_New
= 500,
796 Menu_ShapeClipboard_Copy
,
797 Menu_ShapeClipboard_Paste
,
801 BEGIN_EVENT_TABLE(DnDFrame
, wxFrame
)
802 EVT_MENU(Menu_Quit
, DnDFrame::OnQuit
)
803 EVT_MENU(Menu_About
, DnDFrame::OnAbout
)
804 EVT_MENU(Menu_Drag
, DnDFrame::OnDrag
)
805 EVT_MENU(Menu_DragMoveDef
, DnDFrame::OnDragMoveByDefault
)
806 EVT_MENU(Menu_DragMoveAllow
,DnDFrame::OnDragMoveAllow
)
807 EVT_MENU(Menu_NewFrame
, DnDFrame::OnNewFrame
)
808 EVT_MENU(Menu_Help
, DnDFrame::OnHelp
)
809 EVT_MENU(Menu_Clear
, DnDFrame::OnLogClear
)
810 EVT_MENU(Menu_Copy
, DnDFrame::OnCopy
)
811 EVT_MENU(Menu_Paste
, DnDFrame::OnPaste
)
812 EVT_MENU(Menu_CopyBitmap
, DnDFrame::OnCopyBitmap
)
813 EVT_MENU(Menu_PasteBitmap
,DnDFrame::OnPasteBitmap
)
815 EVT_MENU(Menu_PasteMFile
, DnDFrame::OnPasteMetafile
)
816 #endif // USE_METAFILES
817 EVT_MENU(Menu_CopyFiles
, DnDFrame::OnCopyFiles
)
819 EVT_UPDATE_UI(Menu_DragMoveDef
, DnDFrame::OnUpdateUIMoveByDefault
)
821 EVT_UPDATE_UI(Menu_Paste
, DnDFrame::OnUpdateUIPasteText
)
822 EVT_UPDATE_UI(Menu_PasteBitmap
, DnDFrame::OnUpdateUIPasteBitmap
)
824 EVT_LEFT_DOWN( DnDFrame::OnLeftDown
)
825 EVT_RIGHT_DOWN( DnDFrame::OnRightDown
)
826 EVT_PAINT( DnDFrame::OnPaint
)
827 EVT_SIZE( DnDFrame::OnSize
)
830 BEGIN_EVENT_TABLE(DnDShapeFrame
, wxFrame
)
831 EVT_MENU(Menu_Shape_New
, DnDShapeFrame::OnNewShape
)
832 EVT_MENU(Menu_Shape_Edit
, DnDShapeFrame::OnEditShape
)
833 EVT_MENU(Menu_Shape_Clear
, DnDShapeFrame::OnClearShape
)
835 EVT_MENU(Menu_ShapeClipboard_Copy
, DnDShapeFrame::OnCopyShape
)
836 EVT_MENU(Menu_ShapeClipboard_Paste
, DnDShapeFrame::OnPasteShape
)
838 EVT_UPDATE_UI(Menu_ShapeClipboard_Copy
, DnDShapeFrame::OnUpdateUICopy
)
839 EVT_UPDATE_UI(Menu_ShapeClipboard_Paste
, DnDShapeFrame::OnUpdateUIPaste
)
841 EVT_LEFT_DOWN(DnDShapeFrame::OnDrag
)
843 EVT_PAINT(DnDShapeFrame::OnPaint
)
846 BEGIN_EVENT_TABLE(DnDShapeDialog
, wxDialog
)
847 EVT_BUTTON(Button_Colour
, DnDShapeDialog::OnColour
)
850 BEGIN_EVENT_TABLE(DnDCanvasBitmap
, wxScrolledWindow
)
851 EVT_PAINT(DnDCanvasBitmap::OnPaint
)
855 BEGIN_EVENT_TABLE(DnDCanvasMetafile
, wxScrolledWindow
)
856 EVT_PAINT(DnDCanvasMetafile::OnPaint
)
858 #endif // USE_METAFILES
860 // ============================================================================
862 // ============================================================================
864 // `Main program' equivalent, creating windows and returning main app frame
865 bool DnDApp::OnInit()
867 // switch on trace messages
868 #if defined(__WXGTK__)
869 wxLog::AddTraceMask(_T("clipboard"));
870 #elif defined(__WXMSW__)
871 wxLog::AddTraceMask(wxTRACE_OleCalls
);
875 wxImage::AddHandler( new wxPNGHandler
);
878 // under X we usually want to use the primary selection by default (which
879 // is shared with other apps)
880 wxTheClipboard
->UsePrimarySelection();
882 // create the main frame window
883 DnDFrame
*frame
= new DnDFrame((wxFrame
*) NULL
,
884 _T("Drag-and-Drop/Clipboard wxWindows Sample"),
895 DnDFrame::DnDFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
)
896 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
)),
897 m_strText(_T("wxWindows drag & drop works :-)"))
900 // frame icon and status bar
901 SetIcon(wxICON(mondrian
));
906 wxMenu
*file_menu
= new wxMenu
;
907 file_menu
->Append(Menu_Drag
, _T("&Test drag..."));
908 file_menu
->AppendCheckItem(Menu_DragMoveDef
, _T("&Move by default"));
909 file_menu
->AppendCheckItem(Menu_DragMoveAllow
, _T("&Allow moving"));
910 file_menu
->AppendSeparator();
911 file_menu
->Append(Menu_NewFrame
, _T("&New frame\tCtrl-N"));
912 file_menu
->AppendSeparator();
913 file_menu
->Append(Menu_Quit
, _T("E&xit\tCtrl-Q"));
915 wxMenu
*log_menu
= new wxMenu
;
916 log_menu
->Append(Menu_Clear
, _T("Clear\tCtrl-L"));
918 wxMenu
*help_menu
= new wxMenu
;
919 help_menu
->Append(Menu_Help
, _T("&Help..."));
920 help_menu
->AppendSeparator();
921 help_menu
->Append(Menu_About
, _T("&About"));
923 wxMenu
*clip_menu
= new wxMenu
;
924 clip_menu
->Append(Menu_Copy
, _T("&Copy text\tCtrl-C"));
925 clip_menu
->Append(Menu_Paste
, _T("&Paste text\tCtrl-V"));
926 clip_menu
->AppendSeparator();
927 clip_menu
->Append(Menu_CopyBitmap
, _T("Copy &bitmap\tCtrl-Shift-C"));
928 clip_menu
->Append(Menu_PasteBitmap
, _T("Paste b&itmap\tCtrl-Shift-V"));
930 clip_menu
->AppendSeparator();
931 clip_menu
->Append(Menu_PasteMFile
, _T("Paste &metafile\tCtrl-M"));
932 #endif // USE_METAFILES
933 clip_menu
->AppendSeparator();
934 clip_menu
->Append(Menu_CopyFiles
, _T("Copy &files\tCtrl-F"));
936 wxMenuBar
*menu_bar
= new wxMenuBar
;
937 menu_bar
->Append(file_menu
, _T("&File"));
938 menu_bar
->Append(log_menu
, _T("&Log"));
939 menu_bar
->Append(clip_menu
, _T("&Clipboard"));
940 menu_bar
->Append(help_menu
, _T("&Help"));
942 SetMenuBar(menu_bar
);
944 // make a panel with 3 subwindows
946 wxSize
size(400, 200);
948 wxString
strFile(_T("Drop files here!")), strText(_T("Drop text on me"));
950 m_ctrlFile
= new wxListBox(this, -1, pos
, size
, 1, &strFile
,
951 wxLB_HSCROLL
| wxLB_ALWAYS_SB
);
952 m_ctrlText
= new wxListBox(this, -1, pos
, size
, 1, &strText
,
953 wxLB_HSCROLL
| wxLB_ALWAYS_SB
);
955 m_ctrlLog
= new wxTextCtrl(this, -1, _T(""), pos
, size
,
956 wxTE_MULTILINE
| wxTE_READONLY
|
959 // redirect log messages to the text window
960 m_pLog
= new wxLogTextCtrl(m_ctrlLog
);
961 m_pLogPrev
= wxLog::SetActiveTarget(m_pLog
);
963 // associate drop targets with the controls
964 m_ctrlFile
->SetDropTarget(new DnDFile(m_ctrlFile
));
965 m_ctrlText
->SetDropTarget(new DnDText(m_ctrlText
));
966 m_ctrlLog
->SetDropTarget(new URLDropTarget
);
968 wxLayoutConstraints
*c
;
971 c
= new wxLayoutConstraints
;
972 c
->left
.SameAs(this, wxLeft
);
973 c
->top
.SameAs(this, wxTop
);
974 c
->right
.PercentOf(this, wxRight
, 50);
975 c
->height
.PercentOf(this, wxHeight
, 30);
976 m_ctrlFile
->SetConstraints(c
);
979 c
= new wxLayoutConstraints
;
980 c
->left
.SameAs (m_ctrlFile
, wxRight
);
981 c
->top
.SameAs (this, wxTop
);
982 c
->right
.SameAs (this, wxRight
);
983 c
->height
.PercentOf(this, wxHeight
, 30);
984 m_ctrlText
->SetConstraints(c
);
986 // Lower text control
987 c
= new wxLayoutConstraints
;
988 c
->left
.SameAs (this, wxLeft
);
989 c
->right
.SameAs (this, wxRight
);
990 c
->height
.PercentOf(this, wxHeight
, 50);
991 c
->top
.SameAs(m_ctrlText
, wxBottom
);
992 m_ctrlLog
->SetConstraints(c
);
996 // copy data by default but allow moving it as well
997 m_moveByDefault
= FALSE
;
999 menu_bar
->Check(Menu_DragMoveAllow
, TRUE
);
1002 void DnDFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
1007 void DnDFrame::OnSize(wxSizeEvent
& event
)
1014 void DnDFrame::OnPaint(wxPaintEvent
& WXUNUSED(event
))
1018 GetClientSize( &w
, &h
);
1021 // dc.Clear(); -- this kills wxGTK
1022 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
, FALSE
, _T("charter") ) );
1023 dc
.DrawText( _T("Drag text from here!"), 100, h
-50 );
1026 void DnDFrame::OnUpdateUIMoveByDefault(wxUpdateUIEvent
& event
)
1028 // only can move by default if moving is allowed at all
1029 event
.Enable(m_moveAllow
);
1032 void DnDFrame::OnUpdateUIPasteText(wxUpdateUIEvent
& event
)
1035 // too many trace messages if we don't do it - this function is called
1040 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
1043 void DnDFrame::OnUpdateUIPasteBitmap(wxUpdateUIEvent
& event
)
1046 // too many trace messages if we don't do it - this function is called
1051 event
.Enable( wxTheClipboard
->IsSupported(wxDF_BITMAP
) );
1054 void DnDFrame::OnNewFrame(wxCommandEvent
& WXUNUSED(event
))
1056 (new DnDShapeFrame(this))->Show(TRUE
);
1058 wxLogStatus(this, wxT("Double click the new frame to select a shape for it"));
1061 void DnDFrame::OnDrag(wxCommandEvent
& WXUNUSED(event
))
1063 wxString strText
= wxGetTextFromUser
1065 _T("After you enter text in this dialog, press any mouse\n")
1066 _T("button in the bottom (empty) part of the frame and \n")
1067 _T("drag it anywhere - you will be in fact dragging the\n")
1068 _T("text object containing this text"),
1069 _T("Please enter some text"), m_strText
, this
1072 m_strText
= strText
;
1075 void DnDFrame::OnDragMoveByDefault(wxCommandEvent
& event
)
1077 m_moveByDefault
= event
.IsChecked();
1080 void DnDFrame::OnDragMoveAllow(wxCommandEvent
& event
)
1082 m_moveAllow
= event
.IsChecked();
1085 void DnDFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
1087 wxMessageBox(_T("Drag-&-Drop Demo\n")
1088 _T("Please see \"Help|Help...\" for details\n")
1089 _T("Copyright (c) 1998 Vadim Zeitlin"),
1091 wxICON_INFORMATION
| wxOK
,
1095 void DnDFrame::OnHelp(wxCommandEvent
& /* event */)
1097 wxMessageDialog
dialog(this,
1098 _T("This small program demonstrates drag & drop support in wxWindows. The program window\n")
1099 _T("consists of 3 parts: the bottom pane is for debug messages, so that you can see what's\n")
1100 _T("going on inside. The top part is split into 2 listboxes, the left one accepts files\n")
1101 _T("and the right one accepts text.\n")
1103 _T("To test wxDropTarget: open wordpad (write.exe), select some text in it and drag it to\n")
1104 _T("the right listbox (you'll notice the usual visual feedback, i.e. the cursor will change).\n")
1105 _T("Also, try dragging some files (you can select several at once) from Windows Explorer (or \n")
1106 _T("File Manager) to the left pane. Hold down Ctrl/Shift keys when you drop text (doesn't \n")
1107 _T("work with files) and see what changes.\n")
1109 _T("To test wxDropSource: just press any mouse button on the empty zone of the window and drag\n")
1110 _T("it to wordpad or any other droptarget accepting text (and of course you can just drag it\n")
1111 _T("to the right pane). Due to a lot of trace messages, the cursor might take some time to \n")
1112 _T("change, don't release the mouse button until it does. You can change the string being\n")
1113 _T("dragged in in \"File|Test drag...\" dialog.\n")
1116 _T("Please send all questions/bug reports/suggestions &c to \n")
1117 _T("Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>"),
1123 void DnDFrame::OnLogClear(wxCommandEvent
& /* event */ )
1126 m_ctrlText
->Clear();
1127 m_ctrlFile
->Clear();
1130 void DnDFrame::OnLeftDown(wxMouseEvent
&WXUNUSED(event
) )
1132 if ( !m_strText
.IsEmpty() )
1134 // start drag operation
1135 wxTextDataObject
textData(m_strText
);
1137 wxFileDataObject textData;
1138 textData.AddFile( "/file1.txt" );
1139 textData.AddFile( "/file2.txt" );
1141 wxDropSource
source(textData
, this,
1142 wxDROP_ICON(dnd_copy
),
1143 wxDROP_ICON(dnd_move
),
1144 wxDROP_ICON(dnd_none
));
1147 if ( m_moveByDefault
)
1148 flags
|= wxDrag_DefaultMove
;
1149 else if ( m_moveAllow
)
1150 flags
|= wxDrag_AllowMove
;
1153 switch ( source
.DoDragDrop(flags
) )
1155 case wxDragError
: pc
= _T("Error!"); break;
1156 case wxDragNone
: pc
= _T("Nothing"); break;
1157 case wxDragCopy
: pc
= _T("Copied"); break;
1158 case wxDragMove
: pc
= _T("Moved"); break;
1159 case wxDragCancel
: pc
= _T("Cancelled"); break;
1160 default: pc
= _T("Huh?"); break;
1163 SetStatusText(wxString(_T("Drag result: ")) + pc
);
1167 void DnDFrame::OnRightDown(wxMouseEvent
&event
)
1169 wxMenu
menu(_T("Dnd sample menu"));
1171 menu
.Append(Menu_Drag
, _T("&Test drag..."));
1172 menu
.AppendSeparator();
1173 menu
.Append(Menu_About
, _T("&About"));
1175 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
1178 DnDFrame::~DnDFrame()
1180 if ( m_pLog
!= NULL
) {
1181 if ( wxLog::SetActiveTarget(m_pLogPrev
) == m_pLog
)
1186 // ---------------------------------------------------------------------------
1188 // ---------------------------------------------------------------------------
1190 void DnDFrame::OnCopyBitmap(wxCommandEvent
& WXUNUSED(event
))
1192 // PNG support is not always compiled in under Windows, so use BMP there
1194 wxFileDialog
dialog(this, _T("Open a BMP file"), _T(""), _T(""), _T("BMP files (*.bmp)|*.bmp"), 0);
1196 wxFileDialog
dialog(this, _T("Open a PNG file"), _T(""), _T(""), _T("PNG files (*.png)|*.png"), 0);
1199 if (dialog
.ShowModal() != wxID_OK
)
1201 wxLogMessage( _T("Aborted file open") );
1205 if (dialog
.GetPath().IsEmpty())
1207 wxLogMessage( _T("Returned empty string.") );
1211 if (!wxFileExists(dialog
.GetPath()))
1213 wxLogMessage( _T("File doesn't exist.") );
1218 image
.LoadFile( dialog
.GetPath(),
1227 wxLogError( _T("Invalid image file...") );
1231 wxLogStatus( _T("Decoding image file...") );
1234 wxBitmap
bitmap( image
);
1236 if ( !wxTheClipboard
->Open() )
1238 wxLogError(_T("Can't open clipboard."));
1243 wxLogMessage( _T("Creating wxBitmapDataObject...") );
1246 if ( !wxTheClipboard
->AddData(new wxBitmapDataObject(bitmap
)) )
1248 wxLogError(_T("Can't copy image to the clipboard."));
1252 wxLogMessage(_T("Image has been put on the clipboard.") );
1253 wxLogMessage(_T("You can paste it now and look at it.") );
1256 wxTheClipboard
->Close();
1259 void DnDFrame::OnPasteBitmap(wxCommandEvent
& WXUNUSED(event
))
1261 if ( !wxTheClipboard
->Open() )
1263 wxLogError(_T("Can't open clipboard."));
1268 if ( !wxTheClipboard
->IsSupported(wxDF_BITMAP
) )
1270 wxLogWarning(_T("No bitmap on clipboard"));
1272 wxTheClipboard
->Close();
1276 wxBitmapDataObject data
;
1277 if ( !wxTheClipboard
->GetData(data
) )
1279 wxLogError(_T("Can't paste bitmap from the clipboard"));
1283 const wxBitmap
& bmp
= data
.GetBitmap();
1285 wxLogMessage(_T("Bitmap %dx%d pasted from the clipboard"),
1286 bmp
.GetWidth(), bmp
.GetHeight());
1290 wxTheClipboard
->Close();
1293 #ifdef USE_METAFILES
1295 void DnDFrame::OnPasteMetafile(wxCommandEvent
& WXUNUSED(event
))
1297 if ( !wxTheClipboard
->Open() )
1299 wxLogError(_T("Can't open clipboard."));
1304 if ( !wxTheClipboard
->IsSupported(wxDF_METAFILE
) )
1306 wxLogWarning(_T("No metafile on clipboard"));
1310 wxMetaFileDataObject data
;
1311 if ( !wxTheClipboard
->GetData(data
) )
1313 wxLogError(_T("Can't paste metafile from the clipboard"));
1317 const wxMetaFile
& mf
= data
.GetMetafile();
1319 wxLogMessage(_T("Metafile %dx%d pasted from the clipboard"),
1320 mf
.GetWidth(), mf
.GetHeight());
1326 wxTheClipboard
->Close();
1329 #endif // USE_METAFILES
1331 // ----------------------------------------------------------------------------
1333 // ----------------------------------------------------------------------------
1335 void DnDFrame::OnCopyFiles(wxCommandEvent
& WXUNUSED(event
))
1338 wxFileDialog
dialog(this, _T("Select a file to copy"), _T(""), _T(""),
1339 _T("All files (*.*)|*.*"), 0);
1341 wxArrayString filenames
;
1342 while ( dialog
.ShowModal() == wxID_OK
)
1344 filenames
.Add(dialog
.GetPath());
1347 if ( !filenames
.IsEmpty() )
1349 wxFileDataObject
*dobj
= new wxFileDataObject
;
1350 size_t count
= filenames
.GetCount();
1351 for ( size_t n
= 0; n
< count
; n
++ )
1353 dobj
->AddFile(filenames
[n
]);
1356 wxClipboardLocker locker
;
1359 wxLogError(wxT("Can't open clipboard"));
1363 if ( !wxTheClipboard
->AddData(dobj
) )
1365 wxLogError(wxT("Can't copy file(s) to the clipboard"));
1369 wxLogStatus(this, wxT("%d file%s copied to the clipboard"),
1370 count
, count
== 1 ? wxT("") : wxT("s"));
1376 wxLogStatus(this, wxT("Aborted"));
1379 wxLogError(wxT("Sorry, not implemented"));
1383 // ---------------------------------------------------------------------------
1385 // ---------------------------------------------------------------------------
1387 void DnDFrame::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1389 if ( !wxTheClipboard
->Open() )
1391 wxLogError(_T("Can't open clipboard."));
1396 if ( !wxTheClipboard
->AddData(new wxTextDataObject(m_strText
)) )
1398 wxLogError(_T("Can't copy data to the clipboard"));
1402 wxLogMessage(_T("Text '%s' put on the clipboard"), m_strText
.c_str());
1405 wxTheClipboard
->Close();
1408 void DnDFrame::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1410 if ( !wxTheClipboard
->Open() )
1412 wxLogError(_T("Can't open clipboard."));
1417 if ( !wxTheClipboard
->IsSupported(wxDF_TEXT
) )
1419 wxLogWarning(_T("No text data on clipboard"));
1421 wxTheClipboard
->Close();
1425 wxTextDataObject text
;
1426 if ( !wxTheClipboard
->GetData(text
) )
1428 wxLogError(_T("Can't paste data from the clipboard"));
1432 wxLogMessage(_T("Text '%s' pasted from the clipboard"),
1433 text
.GetText().c_str());
1436 wxTheClipboard
->Close();
1439 // ----------------------------------------------------------------------------
1440 // Notifications called by the base class
1441 // ----------------------------------------------------------------------------
1443 bool DnDText::OnDropText(wxCoord
, wxCoord
, const wxString
& text
)
1445 m_pOwner
->Append(text
);
1450 bool DnDFile::OnDropFiles(wxCoord
, wxCoord
, const wxArrayString
& filenames
)
1452 size_t nFiles
= filenames
.GetCount();
1454 str
.Printf( _T("%d files dropped"), (int)nFiles
);
1455 m_pOwner
->Append(str
);
1456 for ( size_t n
= 0; n
< nFiles
; n
++ ) {
1457 m_pOwner
->Append(filenames
[n
]);
1463 // ----------------------------------------------------------------------------
1465 // ----------------------------------------------------------------------------
1467 DnDShapeDialog::DnDShapeDialog(wxFrame
*parent
, DnDShape
*shape
)
1468 :wxDialog( parent
, 6001, wxT("Choose Shape"), wxPoint( 10, 10 ),
1470 wxRAISED_BORDER
|wxCAPTION
|wxTHICK_FRAME
|wxSYSTEM_MENU
)
1473 wxBoxSizer
* topSizer
= new wxBoxSizer( wxVERTICAL
);
1476 wxBoxSizer
* shapesSizer
= new wxBoxSizer( wxHORIZONTAL
);
1477 const wxString choices
[] = { wxT("None"), wxT("Triangle"),
1478 wxT("Rectangle"), wxT("Ellipse") };
1480 m_radio
= new wxRadioBox( this, -1, wxT("&Shape"),
1481 wxDefaultPosition
, wxDefaultSize
, 4, choices
, 4,
1482 wxRA_SPECIFY_COLS
);
1483 shapesSizer
->Add( m_radio
, 0, wxGROW
|wxALL
, 5 );
1484 topSizer
->Add( shapesSizer
, 0, wxALL
, 2 );
1487 wxStaticBox
* box
= new wxStaticBox( this, -1, wxT("&Attributes") );
1488 wxStaticBoxSizer
* attrSizer
= new wxStaticBoxSizer( box
, wxHORIZONTAL
);
1489 wxFlexGridSizer
* xywhSizer
= new wxFlexGridSizer( 4, 2 );
1493 st
= new wxStaticText( this, -1, wxT("Position &X:") );
1494 m_textX
= new wxTextCtrl( this, -1, wxEmptyString
, wxDefaultPosition
,
1496 xywhSizer
->Add( st
, 1, wxGROW
|wxALL
, 2 );
1497 xywhSizer
->Add( m_textX
, 1, wxGROW
|wxALL
, 2 );
1499 st
= new wxStaticText( this, -1, wxT("Size &width:") );
1500 m_textW
= new wxTextCtrl( this, -1, wxEmptyString
, wxDefaultPosition
,
1502 xywhSizer
->Add( st
, 1, wxGROW
|wxALL
, 2 );
1503 xywhSizer
->Add( m_textW
, 1, wxGROW
|wxALL
, 2 );
1505 st
= new wxStaticText( this, -1, wxT("&Y:") );
1506 m_textY
= new wxTextCtrl( this, -1, wxEmptyString
, wxDefaultPosition
,
1508 xywhSizer
->Add( st
, 1, wxALL
|wxALIGN_RIGHT
, 2 );
1509 xywhSizer
->Add( m_textY
, 1, wxGROW
|wxALL
, 2 );
1511 st
= new wxStaticText( this, -1, wxT("&height:") );
1512 m_textH
= new wxTextCtrl( this, -1, wxEmptyString
, wxDefaultPosition
,
1514 xywhSizer
->Add( st
, 1, wxALL
|wxALIGN_RIGHT
, 2 );
1515 xywhSizer
->Add( m_textH
, 1, wxGROW
|wxALL
, 2 );
1517 wxButton
* col
= new wxButton( this, Button_Colour
, wxT("&Colour...") );
1518 attrSizer
->Add( xywhSizer
, 1, wxGROW
);
1519 attrSizer
->Add( col
, 0, wxALL
|wxALIGN_CENTRE_VERTICAL
, 2 );
1520 topSizer
->Add( attrSizer
, 0, wxGROW
|wxALL
, 5 );
1523 wxBoxSizer
* buttonSizer
= new wxBoxSizer( wxHORIZONTAL
);
1525 bt
= new wxButton( this, wxID_OK
, wxT("Ok") );
1526 buttonSizer
->Add( bt
, 0, wxALL
, 2 );
1527 bt
= new wxButton( this, wxID_CANCEL
, wxT("Cancel") );
1528 buttonSizer
->Add( bt
, 0, wxALL
, 2 );
1529 topSizer
->Add( buttonSizer
, 0, wxALL
|wxALIGN_RIGHT
, 2 );
1531 SetAutoLayout( TRUE
);
1532 SetSizer( topSizer
);
1533 topSizer
->Fit( this );
1536 DnDShape
*DnDShapeDialog::GetShape() const
1538 switch ( m_shapeKind
)
1541 case DnDShape::None
: return NULL
;
1542 case DnDShape::Triangle
: return new DnDTriangularShape(m_pos
, m_size
, m_col
);
1543 case DnDShape::Rectangle
: return new DnDRectangularShape(m_pos
, m_size
, m_col
);
1544 case DnDShape::Ellipse
: return new DnDEllipticShape(m_pos
, m_size
, m_col
);
1548 bool DnDShapeDialog::TransferDataToWindow()
1553 m_radio
->SetSelection(m_shape
->GetKind());
1554 m_pos
= m_shape
->GetPosition();
1555 m_size
= m_shape
->GetSize();
1556 m_col
= m_shape
->GetColour();
1560 m_radio
->SetSelection(DnDShape::None
);
1561 m_pos
= wxPoint(1, 1);
1562 m_size
= wxSize(100, 100);
1565 m_textX
->SetValue(wxString() << m_pos
.x
);
1566 m_textY
->SetValue(wxString() << m_pos
.y
);
1567 m_textW
->SetValue(wxString() << m_size
.x
);
1568 m_textH
->SetValue(wxString() << m_size
.y
);
1573 bool DnDShapeDialog::TransferDataFromWindow()
1575 m_shapeKind
= (DnDShape::Kind
)m_radio
->GetSelection();
1577 m_pos
.x
= wxAtoi(m_textX
->GetValue());
1578 m_pos
.y
= wxAtoi(m_textY
->GetValue());
1579 m_size
.x
= wxAtoi(m_textW
->GetValue());
1580 m_size
.y
= wxAtoi(m_textH
->GetValue());
1582 if ( !m_pos
.x
|| !m_pos
.y
|| !m_size
.x
|| !m_size
.y
)
1584 wxMessageBox(_T("All sizes and positions should be non null!"),
1585 _T("Invalid shape"), wxICON_HAND
| wxOK
, this);
1593 void DnDShapeDialog::OnColour(wxCommandEvent
& WXUNUSED(event
))
1596 data
.SetChooseFull(TRUE
);
1597 for (int i
= 0; i
< 16; i
++)
1599 wxColour
colour(i
*16, i
*16, i
*16);
1600 data
.SetCustomColour(i
, colour
);
1603 wxColourDialog
dialog(this, &data
);
1604 if ( dialog
.ShowModal() == wxID_OK
)
1606 m_col
= dialog
.GetColourData().GetColour();
1610 // ----------------------------------------------------------------------------
1612 // ----------------------------------------------------------------------------
1614 DnDShapeFrame
*DnDShapeFrame::ms_lastDropTarget
= NULL
;
1616 DnDShapeFrame::DnDShapeFrame(wxFrame
*parent
)
1617 : wxFrame(parent
, -1, _T("Shape Frame"),
1618 wxDefaultPosition
, wxSize(250, 150))
1622 wxMenu
*menuShape
= new wxMenu
;
1623 menuShape
->Append(Menu_Shape_New
, _T("&New default shape\tCtrl-S"));
1624 menuShape
->Append(Menu_Shape_Edit
, _T("&Edit shape\tCtrl-E"));
1625 menuShape
->AppendSeparator();
1626 menuShape
->Append(Menu_Shape_Clear
, _T("&Clear shape\tCtrl-L"));
1628 wxMenu
*menuClipboard
= new wxMenu
;
1629 menuClipboard
->Append(Menu_ShapeClipboard_Copy
, _T("&Copy\tCtrl-C"));
1630 menuClipboard
->Append(Menu_ShapeClipboard_Paste
, _T("&Paste\tCtrl-V"));
1632 wxMenuBar
*menubar
= new wxMenuBar
;
1633 menubar
->Append(menuShape
, _T("&Shape"));
1634 menubar
->Append(menuClipboard
, _T("&Clipboard"));
1636 SetMenuBar(menubar
);
1638 SetStatusText(_T("Press Ctrl-S to create a new shape"));
1640 SetDropTarget(new DnDShapeDropTarget(this));
1644 SetBackgroundColour(*wxWHITE
);
1647 DnDShapeFrame::~DnDShapeFrame()
1653 void DnDShapeFrame::SetShape(DnDShape
*shape
)
1662 void DnDShapeFrame::OnDrag(wxMouseEvent
& event
)
1671 // start drag operation
1672 DnDShapeDataObject
shapeData(m_shape
);
1673 wxDropSource
source(shapeData
, this);
1675 const wxChar
*pc
= NULL
;
1676 switch ( source
.DoDragDrop(TRUE
) )
1680 wxLogError(wxT("An error occured during drag and drop operation"));
1684 SetStatusText(_T("Nothing happened"));
1693 if ( ms_lastDropTarget
!= this )
1695 // don't delete the shape if we dropped it on ourselves!
1701 SetStatusText(_T("Drag and drop operation cancelled"));
1707 SetStatusText(wxString(_T("Shape successfully ")) + pc
);
1709 //else: status text already set
1712 void DnDShapeFrame::OnDrop(wxCoord x
, wxCoord y
, DnDShape
*shape
)
1714 ms_lastDropTarget
= this;
1719 s
.Printf(wxT("Shape dropped at (%d, %d)"), pt
.x
, pt
.y
);
1726 void DnDShapeFrame::OnEditShape(wxCommandEvent
& event
)
1728 DnDShapeDialog
dlg(this, m_shape
);
1729 if ( dlg
.ShowModal() == wxID_OK
)
1731 SetShape(dlg
.GetShape());
1735 SetStatusText(_T("You can now drag the shape to another frame"));
1740 void DnDShapeFrame::OnNewShape(wxCommandEvent
& event
)
1742 SetShape(new DnDEllipticShape(wxPoint(10, 10), wxSize(80, 60), *wxRED
));
1744 SetStatusText(_T("You can now drag the shape to another frame"));
1747 void DnDShapeFrame::OnClearShape(wxCommandEvent
& event
)
1752 void DnDShapeFrame::OnCopyShape(wxCommandEvent
& event
)
1756 wxClipboardLocker clipLocker
;
1759 wxLogError(wxT("Can't open the clipboard"));
1764 wxTheClipboard
->AddData(new DnDShapeDataObject(m_shape
));
1768 void DnDShapeFrame::OnPasteShape(wxCommandEvent
& event
)
1770 wxClipboardLocker clipLocker
;
1773 wxLogError(wxT("Can't open the clipboard"));
1778 DnDShapeDataObject
shapeDataObject(NULL
);
1779 if ( wxTheClipboard
->GetData(shapeDataObject
) )
1781 SetShape(shapeDataObject
.GetShape());
1785 wxLogStatus(wxT("No shape on the clipboard"));
1789 void DnDShapeFrame::OnUpdateUICopy(wxUpdateUIEvent
& event
)
1791 event
.Enable( m_shape
!= NULL
);
1794 void DnDShapeFrame::OnUpdateUIPaste(wxUpdateUIEvent
& event
)
1796 event
.Enable( wxTheClipboard
->IsSupported(wxDataFormat(shapeFormatId
)) );
1799 void DnDShapeFrame::OnPaint(wxPaintEvent
& event
)
1813 // ----------------------------------------------------------------------------
1815 // ----------------------------------------------------------------------------
1817 DnDShape
*DnDShape::New(const void *buf
)
1819 const ShapeDump
& dump
= *(const ShapeDump
*)buf
;
1823 return new DnDTriangularShape(wxPoint(dump
.x
, dump
.y
),
1824 wxSize(dump
.w
, dump
.h
),
1825 wxColour(dump
.r
, dump
.g
, dump
.b
));
1828 return new DnDRectangularShape(wxPoint(dump
.x
, dump
.y
),
1829 wxSize(dump
.w
, dump
.h
),
1830 wxColour(dump
.r
, dump
.g
, dump
.b
));
1833 return new DnDEllipticShape(wxPoint(dump
.x
, dump
.y
),
1834 wxSize(dump
.w
, dump
.h
),
1835 wxColour(dump
.r
, dump
.g
, dump
.b
));
1838 wxFAIL_MSG(wxT("invalid shape!"));
1843 // ----------------------------------------------------------------------------
1844 // DnDShapeDataObject
1845 // ----------------------------------------------------------------------------
1847 #ifdef USE_METAFILES
1849 void DnDShapeDataObject::CreateMetaFile() const
1851 wxPoint pos
= m_shape
->GetPosition();
1852 wxSize size
= m_shape
->GetSize();
1854 wxMetaFileDC
dcMF(wxEmptyString
, pos
.x
+ size
.x
, pos
.y
+ size
.y
);
1856 m_shape
->Draw(dcMF
);
1858 wxMetafile
*mf
= dcMF
.Close();
1860 DnDShapeDataObject
*self
= (DnDShapeDataObject
*)this; // const_cast
1861 self
->m_dobjMetaFile
.SetMetafile(*mf
);
1862 self
->m_hasMetaFile
= TRUE
;
1869 void DnDShapeDataObject::CreateBitmap() const
1871 wxPoint pos
= m_shape
->GetPosition();
1872 wxSize size
= m_shape
->GetSize();
1873 int x
= pos
.x
+ size
.x
,
1875 wxBitmap
bitmap(x
, y
);
1877 dc
.SelectObject(bitmap
);
1878 dc
.SetBrush(wxBrush(wxT("white"), wxSOLID
));
1881 dc
.SelectObject(wxNullBitmap
);
1883 DnDShapeDataObject
*self
= (DnDShapeDataObject
*)this; // const_cast
1884 self
->m_dobjBitmap
.SetBitmap(bitmap
);
1885 self
->m_hasBitmap
= TRUE
;
1888 // ----------------------------------------------------------------------------
1890 // ----------------------------------------------------------------------------
1892 static void ShowBitmap(const wxBitmap
& bitmap
)
1894 wxFrame
*frame
= new wxFrame(NULL
, -1, _T("Bitmap view"));
1895 frame
->CreateStatusBar();
1896 DnDCanvasBitmap
*canvas
= new DnDCanvasBitmap(frame
);
1897 canvas
->SetBitmap(bitmap
);
1899 int w
= bitmap
.GetWidth(),
1900 h
= bitmap
.GetHeight();
1901 frame
->SetStatusText(wxString::Format(_T("%dx%d"), w
, h
));
1903 frame
->SetClientSize(w
> 100 ? 100 : w
, h
> 100 ? 100 : h
);
1907 #ifdef USE_METAFILES
1909 static void ShowMetaFile(const wxMetaFile
& metafile
)
1911 wxFrame
*frame
= new wxFrame(NULL
, -1, _T("Metafile view"));
1912 frame
->CreateStatusBar();
1913 DnDCanvasMetafile
*canvas
= new DnDCanvasMetafile(frame
);
1914 canvas
->SetMetafile(metafile
);
1916 wxSize size
= metafile
.GetSize();
1917 frame
->SetStatusText(wxString::Format(_T("%dx%d"), size
.x
, size
.y
));
1919 frame
->SetClientSize(size
.x
> 100 ? 100 : size
.x
,
1920 size
.y
> 100 ? 100 : size
.y
);
1924 #endif // USE_METAFILES