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
30 #include "wx/dirdlg.h"
31 #include "wx/filedlg.h"
33 #include "wx/clipbrd.h"
34 #include "wx/colordlg.h"
35 #include "wx/resource.h"
37 #if defined(__WXGTK__) || defined(__WXMOTIF__)
38 #include "mondrian.xpm"
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
) { m_pOwner
= pOwner
; }
62 virtual bool OnDropFiles(wxCoord x
, wxCoord y
,
63 const wxArrayString
& filenames
);
69 // ----------------------------------------------------------------------------
70 // Define a new application type
71 // ----------------------------------------------------------------------------
73 class DnDApp
: public wxApp
76 virtual bool OnInit();
79 IMPLEMENT_APP(DnDApp
);
81 // ----------------------------------------------------------------------------
82 // Define a new frame type for the main frame
83 // ----------------------------------------------------------------------------
85 class DnDFrame
: public wxFrame
88 DnDFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
);
91 void OnPaint(wxPaintEvent
& event
);
92 void OnQuit (wxCommandEvent
& event
);
93 void OnAbout(wxCommandEvent
& event
);
94 void OnDrag (wxCommandEvent
& event
);
95 void OnNewFrame(wxCommandEvent
& event
);
96 void OnHelp (wxCommandEvent
& event
);
97 void OnLogClear(wxCommandEvent
& event
);
99 void OnCopy(wxCommandEvent
& event
);
100 void OnPaste(wxCommandEvent
& event
);
102 void OnCopyBitmap(wxCommandEvent
& event
);
103 void OnPasteBitmap(wxCommandEvent
& event
);
105 void OnCopyFiles(wxCommandEvent
& event
);
107 void OnLeftDown(wxMouseEvent
& event
);
108 void OnRightDown(wxMouseEvent
& event
);
110 void OnUpdateUIPasteText(wxUpdateUIEvent
& event
);
111 void OnUpdateUIPasteBitmap(wxUpdateUIEvent
& event
);
113 DECLARE_EVENT_TABLE()
116 wxListBox
*m_ctrlFile
,
118 wxTextCtrl
*m_ctrlLog
;
127 // ----------------------------------------------------------------------------
128 // A shape is an example of application-specific data which may be transported
129 // via drag-and-drop or clipboard: in our case, we have different geometric
130 // shapes, each one with its own colour and position
131 // ----------------------------------------------------------------------------
144 DnDShape(const wxPoint
& pos
,
147 : m_pos(pos
), m_size(size
), m_col(col
)
151 // this is for debugging - lets us see when exactly an object is freed
152 // (this may be later than you think if it's on the clipboard, for example)
153 virtual ~DnDShape() { }
155 // the functions used for drag-and-drop: they dump and restore a shape into
156 // some bitwise-copiable data (might use streams too...)
157 // ------------------------------------------------------------------------
159 // restore from buffer
160 static DnDShape
*New(const void *buf
);
162 virtual size_t GetDataSize() const
164 return sizeof(ShapeDump
);
167 virtual void GetDataHere(void *buf
) const
169 ShapeDump
& dump
= *(ShapeDump
*)buf
;
174 dump
.r
= m_col
.Red();
175 dump
.g
= m_col
.Green();
176 dump
.b
= m_col
.Blue();
181 const wxPoint
& GetPosition() const { return m_pos
; }
182 const wxColour
& GetColour() const { return m_col
; }
183 const wxSize
& GetSize() const { return m_size
; }
185 void Move(const wxPoint
& pos
) { m_pos
= pos
; }
187 // to implement in derived classes
188 virtual Kind
GetKind() const = 0;
190 virtual void Draw(wxDC
& dc
)
192 dc
.SetPen(wxPen(m_col
, 1, wxSOLID
));
196 wxPoint
GetCentre() const
197 { return wxPoint(m_pos
.x
+ m_size
.x
/ 2, m_pos
.y
+ m_size
.y
/ 2); }
201 int x
, y
, // position
212 class DnDTriangularShape
: public DnDShape
215 DnDTriangularShape(const wxPoint
& pos
,
218 : DnDShape(pos
, size
, col
)
220 wxLogMessage("DnDTriangularShape is being created");
223 virtual ~DnDTriangularShape()
225 wxLogMessage("DnDTriangularShape is being deleted");
228 virtual Kind
GetKind() const { return Triangle
; }
229 virtual void Draw(wxDC
& dc
)
233 // well, it's a bit difficult to describe a triangle by position and
234 // size, but we're not doing geometry here, do we? ;-)
236 wxPoint
p2(m_pos
.x
+ m_size
.x
, m_pos
.y
);
237 wxPoint
p3(m_pos
.x
, m_pos
.y
+ m_size
.y
);
244 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
249 class DnDRectangularShape
: public DnDShape
252 DnDRectangularShape(const wxPoint
& pos
,
255 : DnDShape(pos
, size
, col
)
257 wxLogMessage("DnDRectangularShape is being created");
260 virtual ~DnDRectangularShape()
262 wxLogMessage("DnDRectangularShape is being deleted");
265 virtual Kind
GetKind() const { return Rectangle
; }
266 virtual void Draw(wxDC
& dc
)
271 wxPoint
p2(p1
.x
+ m_size
.x
, p1
.y
);
272 wxPoint
p3(p2
.x
, p2
.y
+ m_size
.y
);
273 wxPoint
p4(p1
.x
, p3
.y
);
281 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
286 class DnDEllipticShape
: public DnDShape
289 DnDEllipticShape(const wxPoint
& pos
,
292 : DnDShape(pos
, size
, col
)
294 wxLogMessage("DnDEllipticShape is being created");
297 virtual ~DnDEllipticShape()
299 wxLogMessage("DnDEllipticShape is being deleted");
302 virtual Kind
GetKind() const { return Ellipse
; }
303 virtual void Draw(wxDC
& dc
)
307 dc
.DrawEllipse(m_pos
, m_size
);
310 dc
.FloodFill(GetCentre(), m_col
, wxFLOOD_BORDER
);
315 // ----------------------------------------------------------------------------
316 // A wxDataObject specialisation for the application-specific data
317 // ----------------------------------------------------------------------------
319 static const char *shapeFormatId
= "wxShape";
321 class DnDShapeDataObject
: public wxDataObject
324 // ctor doesn't copy the pointer, so it shouldn't go away while this object
326 DnDShapeDataObject(DnDShape
*shape
= (DnDShape
*)NULL
)
330 // we need to copy the shape because the one we're handled may be
331 // deleted while it's still on the clipboard (for example) - and we
332 // reuse the serialisation methods here to copy it
333 void *buf
= malloc(shape
->DnDShape
::GetDataSize());
334 shape
->GetDataHere(buf
);
335 m_shape
= DnDShape
::New(buf
);
345 // this string should uniquely identify our format, but is otherwise
347 m_formatShape
.SetId(shapeFormatId
);
349 // we don't draw the shape to a bitmap until it's really needed (i.e.
350 // we're asked to do so)
354 virtual ~DnDShapeDataObject() { delete m_shape
; }
356 // after a call to this function, the shape is owned by the caller and it
357 // is responsible for deleting it!
359 // NB: a better solution would be to make DnDShapes ref counted and this
360 // is what should probably be done in a real life program, otherwise
361 // the ownership problems become too complicated really fast
364 DnDShape
*shape
= m_shape
;
366 m_shape
= (DnDShape
*)NULL
;
372 // implement base class pure virtuals
373 // ----------------------------------
375 virtual wxDataFormat
GetPreferredFormat(Direction
WXUNUSED(dir
)) const
377 return m_formatShape
;
380 virtual size_t GetFormatCount(Direction dir
) const
382 // our custom format is supported by both GetData() and SetData()
386 // but the bitmap format(s) are only supported for output
387 nFormats
+= m_dataobj
.GetFormatCount(dir
);
393 virtual void GetAllFormats(wxDataFormat
*formats
, Direction dir
) const
395 formats
[0] = m_formatShape
;
398 m_dataobj
.GetAllFormats(&formats
[1], dir
);
402 virtual size_t GetDataSize(const wxDataFormat
& format
) const
404 if ( format
== m_formatShape
)
406 return m_shape
->GetDataSize();
413 return m_dataobj
.GetDataSize();
417 virtual bool GetDataHere(const wxDataFormat
& format
, void *pBuf
) const
419 if ( format
== m_formatShape
)
421 m_shape
->GetDataHere(pBuf
);
430 return m_dataobj
.GetDataHere(pBuf
);
434 virtual bool SetData(const wxDataFormat
& format
,
435 size_t len
, const void *buf
)
437 wxCHECK_MSG( format
== m_formatShape
, FALSE
, "unsupported format" );
440 m_shape
= DnDShape
::New(buf
);
442 // the shape has changed
449 // creates a bitmap and assigns it to m_dataobj (also sets m_hasBitmap)
450 void CreateBitmap() const;
452 wxDataFormat m_formatShape
; // our custom format
454 wxBitmapDataObject m_dataobj
; // it handles bitmaps
455 bool m_hasBitmap
; // true if m_dataobj has valid bitmap
457 DnDShape
*m_shape
; // our data
460 // ----------------------------------------------------------------------------
461 // A dialog to edit shape properties
462 // ----------------------------------------------------------------------------
464 class DnDShapeDialog
: public wxDialog
467 DnDShapeDialog(wxFrame
*parent
, DnDShape
*shape
);
469 DnDShape
*GetShape() const;
471 virtual bool TransferDataToWindow();
472 virtual bool TransferDataFromWindow();
474 void OnColour(wxCommandEvent
& event
);
481 DnDShape
::Kind m_shapeKind
;
493 DECLARE_EVENT_TABLE()
496 // ----------------------------------------------------------------------------
497 // A frame for the shapes which can be drag-and-dropped between frames
498 // ----------------------------------------------------------------------------
500 class DnDShapeFrame
: public wxFrame
503 DnDShapeFrame(wxFrame
*parent
);
506 void SetShape(DnDShape
*shape
);
509 void OnNewShape(wxCommandEvent
& event
);
510 void OnEditShape(wxCommandEvent
& event
);
511 void OnClearShape(wxCommandEvent
& event
);
513 void OnCopyShape(wxCommandEvent
& event
);
514 void OnPasteShape(wxCommandEvent
& event
);
516 void OnUpdateUICopy(wxUpdateUIEvent
& event
);
517 void OnUpdateUIPaste(wxUpdateUIEvent
& event
);
519 void OnDrag(wxMouseEvent
& event
);
520 void OnPaint(wxPaintEvent
& event
);
521 void OnDrop(wxCoord x
, wxCoord y
, DnDShape
*shape
);
526 static DnDShapeFrame
*ms_lastDropTarget
;
528 DECLARE_EVENT_TABLE()
531 // ----------------------------------------------------------------------------
532 // wxDropTarget derivation for DnDShapes
533 // ----------------------------------------------------------------------------
535 class DnDShapeDropTarget
: public wxDropTarget
538 DnDShapeDropTarget(DnDShapeFrame
*frame
)
539 : wxDropTarget(new DnDShapeDataObject
)
544 // override base class (pure) virtuals
545 virtual void OnEnter()
546 { m_frame
->SetStatusText("Mouse entered the frame"); }
547 virtual void OnLeave()
548 { m_frame
->SetStatusText("Mouse left the frame"); }
549 virtual wxDragResult
OnData(wxCoord x
, wxCoord y
, wxDragResult def
)
553 wxLogError("Failed to get drag and drop data");
558 m_frame
->OnDrop(x
, y
,
559 ((DnDShapeDataObject
*)GetDataObject())->GetShape());
565 DnDShapeFrame
*m_frame
;
568 // ----------------------------------------------------------------------------
569 // IDs for the menu commands
570 // ----------------------------------------------------------------------------
585 Menu_Shape_New
= 500,
588 Menu_ShapeClipboard_Copy
,
589 Menu_ShapeClipboard_Paste
,
593 BEGIN_EVENT_TABLE(DnDFrame
, wxFrame
)
594 EVT_MENU(Menu_Quit
, DnDFrame
::OnQuit
)
595 EVT_MENU(Menu_About
, DnDFrame
::OnAbout
)
596 EVT_MENU(Menu_Drag
, DnDFrame
::OnDrag
)
597 EVT_MENU(Menu_NewFrame
, DnDFrame
::OnNewFrame
)
598 EVT_MENU(Menu_Help
, DnDFrame
::OnHelp
)
599 EVT_MENU(Menu_Clear
, DnDFrame
::OnLogClear
)
600 EVT_MENU(Menu_Copy
, DnDFrame
::OnCopy
)
601 EVT_MENU(Menu_Paste
, DnDFrame
::OnPaste
)
602 EVT_MENU(Menu_CopyBitmap
, DnDFrame
::OnCopyBitmap
)
603 EVT_MENU(Menu_PasteBitmap
,DnDFrame
::OnPasteBitmap
)
604 EVT_MENU(Menu_CopyFiles
, DnDFrame
::OnCopyFiles
)
606 EVT_UPDATE_UI(Menu_Paste
, DnDFrame
::OnUpdateUIPasteText
)
607 EVT_UPDATE_UI(Menu_PasteBitmap
, DnDFrame
::OnUpdateUIPasteBitmap
)
609 EVT_LEFT_DOWN( DnDFrame
::OnLeftDown
)
610 EVT_RIGHT_DOWN( DnDFrame
::OnRightDown
)
611 EVT_PAINT( DnDFrame
::OnPaint
)
614 BEGIN_EVENT_TABLE(DnDShapeFrame
, wxFrame
)
615 EVT_MENU(Menu_Shape_New
, DnDShapeFrame
::OnNewShape
)
616 EVT_MENU(Menu_Shape_Edit
, DnDShapeFrame
::OnEditShape
)
617 EVT_MENU(Menu_Shape_Clear
, DnDShapeFrame
::OnClearShape
)
619 EVT_MENU(Menu_ShapeClipboard_Copy
, DnDShapeFrame
::OnCopyShape
)
620 EVT_MENU(Menu_ShapeClipboard_Paste
, DnDShapeFrame
::OnPasteShape
)
622 EVT_UPDATE_UI(Menu_ShapeClipboard_Copy
, DnDShapeFrame
::OnUpdateUICopy
)
623 EVT_UPDATE_UI(Menu_ShapeClipboard_Paste
, DnDShapeFrame
::OnUpdateUIPaste
)
625 EVT_LEFT_DOWN(DnDShapeFrame
::OnDrag
)
627 EVT_PAINT(DnDShapeFrame
::OnPaint
)
630 BEGIN_EVENT_TABLE(DnDShapeDialog
, wxDialog
)
631 EVT_BUTTON(Button_Colour
, DnDShapeDialog
::OnColour
)
634 // ============================================================================
636 // ============================================================================
638 // `Main program' equivalent, creating windows and returning main app frame
639 bool DnDApp
::OnInit()
641 // load our ressources
645 pathList
.Add("./Debug");
646 pathList
.Add("./Release");
649 wxString path
= pathList
.FindValidPath("dnd.wxr");
652 wxLogError("Can't find the resource file dnd.wxr in the current "
653 "directory, aborting.");
658 wxDefaultResourceTable
->ParseResourceFile(path
);
661 wxImage
::AddHandler( new wxPNGHandler
);
664 // create the main frame window
665 DnDFrame
*frame
= new DnDFrame((wxFrame
*) NULL
,
666 "Drag-and-Drop/Clipboard wxWindows Sample",
677 DnDFrame
::DnDFrame(wxFrame
*frame
, char *title
, int x
, int y
, int w
, int h
)
678 : wxFrame(frame
, -1, title
, wxPoint(x
, y
), wxSize(w
, h
)),
679 m_strText("wxWindows drag & drop works :-)")
682 // frame icon and status bar
683 SetIcon(wxICON(mondrian
));
688 wxMenu
*file_menu
= new wxMenu
;
689 file_menu
->Append(Menu_Drag
, "&Test drag...");
690 file_menu
->AppendSeparator();
691 file_menu
->Append(Menu_NewFrame
, "&New frame\tCtrl-N");
692 file_menu
->AppendSeparator();
693 file_menu
->Append(Menu_Quit
, "E&xit");
695 wxMenu
*log_menu
= new wxMenu
;
696 log_menu
->Append(Menu_Clear
, "Clear\tCtrl-L");
698 wxMenu
*help_menu
= new wxMenu
;
699 help_menu
->Append(Menu_Help
, "&Help...");
700 help_menu
->AppendSeparator();
701 help_menu
->Append(Menu_About
, "&About");
703 wxMenu
*clip_menu
= new wxMenu
;
704 clip_menu
->Append(Menu_Copy
, "&Copy text\tCtrl+C");
705 clip_menu
->Append(Menu_Paste
, "&Paste text\tCtrl+V");
706 clip_menu
->AppendSeparator();
707 clip_menu
->Append(Menu_CopyBitmap
, "&Copy bitmap\tAlt+C");
708 clip_menu
->Append(Menu_PasteBitmap
, "&Paste bitmap\tAlt+V");
709 clip_menu
->AppendSeparator();
710 clip_menu
->Append(Menu_CopyFiles
, "&Copy files\tCtrl+F");
712 wxMenuBar
*menu_bar
= new wxMenuBar
;
713 menu_bar
->Append(file_menu
, "&File");
714 menu_bar
->Append(log_menu
, "&Log");
715 menu_bar
->Append(clip_menu
, "&Clipboard");
716 menu_bar
->Append(help_menu
, "&Help");
718 SetMenuBar(menu_bar
);
720 // make a panel with 3 subwindows
722 wxSize
size(400, 200);
724 wxString
strFile("Drop files here!"), strText("Drop text on me");
726 m_ctrlFile
= new wxListBox(this, -1, pos
, size
, 1, &strFile
,
727 wxLB_HSCROLL
| wxLB_ALWAYS_SB
);
728 m_ctrlText
= new wxListBox(this, -1, pos
, size
, 1, &strText
,
729 wxLB_HSCROLL
| wxLB_ALWAYS_SB
);
731 m_ctrlLog
= new wxTextCtrl(this, -1, "", pos
, size
,
732 wxTE_MULTILINE
| wxTE_READONLY
|
736 // redirect log messages to the text window and switch on OLE messages
738 wxLog
::AddTraceMask(wxTRACE_OleCalls
);
740 m_pLog
= new wxLogTextCtrl(m_ctrlLog
);
741 m_pLogPrev
= wxLog
::SetActiveTarget(m_pLog
);
743 // associate drop targets with 2 text controls
744 m_ctrlFile
->SetDropTarget(new DnDFile(m_ctrlFile
));
745 m_ctrlText
->SetDropTarget(new DnDText(m_ctrlText
));
747 wxLayoutConstraints
*c
;
750 c
= new wxLayoutConstraints
;
751 c
->left
.SameAs(this, wxLeft
);
752 c
->top
.SameAs(this, wxTop
);
753 c
->right
.PercentOf(this, wxRight
, 50);
754 c
->height
.PercentOf(this, wxHeight
, 30);
755 m_ctrlFile
->SetConstraints(c
);
758 c
= new wxLayoutConstraints
;
759 c
->left
.SameAs (m_ctrlFile
, wxRight
);
760 c
->top
.SameAs (this, wxTop
);
761 c
->right
.SameAs (this, wxRight
);
762 c
->height
.PercentOf(this, wxHeight
, 30);
763 m_ctrlText
->SetConstraints(c
);
765 // Lower text control
766 c
= new wxLayoutConstraints
;
767 c
->left
.SameAs (this, wxLeft
);
768 c
->right
.SameAs (this, wxRight
);
769 c
->height
.PercentOf(this, wxHeight
, 50);
770 c
->top
.SameAs(m_ctrlText
, wxBottom
);
771 m_ctrlLog
->SetConstraints(c
);
776 void DnDFrame
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
781 void DnDFrame
::OnPaint(wxPaintEvent
& WXUNUSED(event
))
785 GetClientSize( &w
, &h
);
788 dc
.SetFont( wxFont( 24, wxDECORATIVE
, wxNORMAL
, wxNORMAL
, FALSE
, "charter" ) );
789 dc
.DrawText( "Drag text from here!", 20, h
-50 );
793 // 4/5 is 80% taken by other windows, 20 is arbitrary margin
794 dc
.DrawBitmap(m_bitmap
,
795 w
- m_bitmap
.GetWidth() - 20,
800 void DnDFrame
::OnUpdateUIPasteText(wxUpdateUIEvent
& event
)
802 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
805 void DnDFrame
::OnUpdateUIPasteBitmap(wxUpdateUIEvent
& event
)
807 event
.Enable( wxTheClipboard
->IsSupported(wxDF_BITMAP
) );
810 void DnDFrame
::OnNewFrame(wxCommandEvent
& WXUNUSED(event
))
812 (new DnDShapeFrame(this))->Show(TRUE
);
814 wxLogStatus(this, "Double click the new frame to select a shape for it");
817 void DnDFrame
::OnDrag(wxCommandEvent
& WXUNUSED(event
))
819 wxString strText
= wxGetTextFromUser
821 "After you enter text in this dialog, press any mouse\n"
822 "button in the bottom (empty) part of the frame and \n"
823 "drag it anywhere - you will be in fact dragging the\n"
824 "text object containing this text",
825 "Please enter some text", m_strText
, this
831 void DnDFrame
::OnAbout(wxCommandEvent
& WXUNUSED(event
))
833 wxMessageBox("Drag-&-Drop Demo\n"
834 "Please see \"Help|Help...\" for details\n"
835 "Copyright (c) 1998 Vadim Zeitlin",
837 wxICON_INFORMATION
| wxOK
,
841 void DnDFrame
::OnHelp(wxCommandEvent
& /* event */)
843 wxMessageDialog
dialog(this,
844 "This small program demonstrates drag & drop support in wxWindows. The program window\n"
845 "consists of 3 parts: the bottom pane is for debug messages, so that you can see what's\n"
846 "going on inside. The top part is split into 2 listboxes, the left one accepts files\n"
847 "and the right one accepts text.\n"
849 "To test wxDropTarget: open wordpad (write.exe), select some text in it and drag it to\n"
850 "the right listbox (you'll notice the usual visual feedback, i.e. the cursor will change).\n"
851 "Also, try dragging some files (you can select several at once) from Windows Explorer (or \n"
852 "File Manager) to the left pane. Hold down Ctrl/Shift keys when you drop text (doesn't \n"
853 "work with files) and see what changes.\n"
855 "To test wxDropSource: just press any mouse button on the empty zone of the window and drag\n"
856 "it to wordpad or any other droptarget accepting text (and of course you can just drag it\n"
857 "to the right pane). Due to a lot of trace messages, the cursor might take some time to \n"
858 "change, don't release the mouse button until it does. You can change the string being\n"
859 "dragged in in \"File|Test drag...\" dialog.\n"
862 "Please send all questions/bug reports/suggestions &c to \n"
863 "Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>",
869 void DnDFrame
::OnLogClear(wxCommandEvent
& /* event */ )
876 void DnDFrame
::OnLeftDown(wxMouseEvent
&WXUNUSED(event
) )
878 if ( !m_strText
.IsEmpty() )
880 // start drag operation
881 wxTextDataObject
textData(m_strText
);
883 wxFileDataObject textData;
884 textData.AddFile( "/file1.txt" );
885 textData.AddFile( "/file2.txt" );
887 wxDropSource
source(textData
, this
889 ,wxCURSOR_PENCIL
, // for copy
890 wxCURSOR_SPRAYCAN
, // for move
891 wxCURSOR_QUESTION_ARROW
// for nothing
897 switch ( source
.DoDragDrop(TRUE
) )
899 case wxDragError
: pc
= "Error!"; break;
900 case wxDragNone
: pc
= "Nothing"; break;
901 case wxDragCopy
: pc
= "Copied"; break;
902 case wxDragMove
: pc
= "Moved"; break;
903 case wxDragCancel
: pc
= "Cancelled"; break;
904 default: pc
= "Huh?"; break;
907 SetStatusText(wxString("Drag result: ") + pc
);
911 void DnDFrame
::OnRightDown(wxMouseEvent
&event
)
913 wxMenu
menu("Dnd sample menu");
915 menu
.Append(Menu_Drag
, "&Test drag...");
916 menu
.AppendSeparator();
917 menu
.Append(Menu_About
, "&About");
919 PopupMenu( &menu
, event
.GetX(), event
.GetY() );
922 DnDFrame
::~DnDFrame()
924 if ( m_pLog
!= NULL
) {
925 if ( wxLog
::SetActiveTarget(m_pLogPrev
) == m_pLog
)
930 // ---------------------------------------------------------------------------
932 // ---------------------------------------------------------------------------
934 void DnDFrame
::OnCopyBitmap(wxCommandEvent
& WXUNUSED(event
))
936 // PNG support is not always compiled in under Windows, so use BMP there
938 wxFileDialog
dialog(this, "Open a BMP file", "", "", "BMP files (*.bmp)|*.bmp", 0);
940 wxFileDialog
dialog(this, "Open a PNG file", "", "", "PNG files (*.png)|*.png", 0);
943 if (dialog
.ShowModal() != wxID_OK
)
945 wxLogMessage( _T("Aborted file open") );
949 if (dialog
.GetPath().IsEmpty())
951 wxLogMessage( _T("Returned empty string.") );
955 if (!wxFileExists(dialog
.GetPath()))
957 wxLogMessage( _T("File doesn't exist.") );
962 image
.LoadFile( dialog
.GetPath(),
971 wxLogError( _T("Invalid image file...") );
975 wxLogStatus( _T("Decoding image file...") );
978 wxBitmap
bitmap( image
.ConvertToBitmap() );
980 if ( !wxTheClipboard
->Open() )
982 wxLogError(_T("Can't open clipboard."));
987 wxLogMessage( _T("Creating wxBitmapDataObject...") );
990 if ( !wxTheClipboard
->AddData(new wxBitmapDataObject(bitmap
)) )
992 wxLogError(_T("Can't copy image to the clipboard."));
996 wxLogMessage(_T("Image has been put on the clipboard.") );
997 wxLogMessage(_T("You can paste it now and look at it.") );
1000 wxTheClipboard
->Close();
1003 void DnDFrame
::OnPasteBitmap(wxCommandEvent
& WXUNUSED(event
))
1005 if ( !wxTheClipboard
->Open() )
1007 wxLogError(_T("Can't open clipboard."));
1012 if ( !wxTheClipboard
->IsSupported(wxDF_BITMAP
) )
1014 wxLogWarning(_T("No bitmap on clipboard"));
1016 wxTheClipboard
->Close();
1020 wxBitmapDataObject data
;
1021 if ( !wxTheClipboard
->GetData(data
) )
1023 wxLogError(_T("Can't paste bitmap from the clipboard"));
1027 wxLogMessage(_T("Bitmap pasted from the clipboard") );
1028 m_bitmap
= data
.GetBitmap();
1032 wxTheClipboard
->Close();
1035 // ----------------------------------------------------------------------------
1037 // ----------------------------------------------------------------------------
1039 void DnDFrame
::OnCopyFiles(wxCommandEvent
& WXUNUSED(event
))
1042 wxFileDataObject
*dobj
= new wxFileDataObject
;
1044 wxFileDialog
dialog(this, "Select a file to copy", "", "",
1045 "All files (*.*)|*.*", 0);
1047 if ( dialog
.ShowModal() == wxID_OK
)
1049 wxString filename
= dialog
.GetPath();
1050 dobj
->AddFile(filename
);
1052 wxClipboardLocker locker
;
1055 wxLogError("Can't open clipboard");
1059 if ( !wxTheClipboard
->AddData(dobj
) )
1061 wxLogError("Can't copy file to the clipboard");
1065 wxLogStatus(this, "File '%s' copied to the clipboard",
1072 wxLogStatus(this, "Aborted");
1075 wxLogError("Sorry, not implemented");
1079 // ---------------------------------------------------------------------------
1081 // ---------------------------------------------------------------------------
1083 void DnDFrame
::OnCopy(wxCommandEvent
& WXUNUSED(event
))
1085 if ( !wxTheClipboard
->Open() )
1087 wxLogError(_T("Can't open clipboard."));
1092 if ( !wxTheClipboard
->AddData(new wxTextDataObject(m_strText
)) )
1094 wxLogError(_T("Can't copy data to the clipboard"));
1098 wxLogMessage(_T("Text '%s' put on the clipboard"), m_strText
.c_str());
1101 wxTheClipboard
->Close();
1104 void DnDFrame
::OnPaste(wxCommandEvent
& WXUNUSED(event
))
1106 if ( !wxTheClipboard
->Open() )
1108 wxLogError(_T("Can't open clipboard."));
1113 if ( !wxTheClipboard
->IsSupported(wxDF_TEXT
) )
1115 wxLogWarning(_T("No text data on clipboard"));
1117 wxTheClipboard
->Close();
1121 wxTextDataObject text
;
1122 if ( !wxTheClipboard
->GetData(text
) )
1124 wxLogError(_T("Can't paste data from the clipboard"));
1128 wxLogMessage(_T("Text '%s' pasted from the clipboard"),
1129 text
.GetText().c_str());
1132 wxTheClipboard
->Close();
1135 // ----------------------------------------------------------------------------
1136 // Notifications called by the base class
1137 // ----------------------------------------------------------------------------
1139 bool DnDText
::OnDropText(wxCoord
, wxCoord
, const wxString
& text
)
1141 m_pOwner
->Append(text
);
1146 bool DnDFile
::OnDropFiles(wxCoord
, wxCoord
, const wxArrayString
& filenames
)
1148 size_t nFiles
= filenames
.GetCount();
1150 str
.Printf( _T("%d files dropped"), nFiles
);
1151 m_pOwner
->Append(str
);
1152 for ( size_t n
= 0; n
< nFiles
; n
++ ) {
1153 m_pOwner
->Append(filenames
[n
]);
1159 // ----------------------------------------------------------------------------
1161 // ----------------------------------------------------------------------------
1163 DnDShapeDialog
::DnDShapeDialog(wxFrame
*parent
, DnDShape
*shape
)
1167 LoadFromResource(parent
, "dialogShape");
1169 m_textX
= (wxTextCtrl
*)wxFindWindowByName("textX", this);
1170 m_textY
= (wxTextCtrl
*)wxFindWindowByName("textY", this);
1171 m_textW
= (wxTextCtrl
*)wxFindWindowByName("textW", this);
1172 m_textH
= (wxTextCtrl
*)wxFindWindowByName("textH", this);
1174 m_radio
= (wxRadioBox
*)wxFindWindowByName("radio", this);
1177 DnDShape
*DnDShapeDialog
::GetShape() const
1179 switch ( m_shapeKind
)
1182 case DnDShape
::None
: return NULL
;
1183 case DnDShape
::Triangle
: return new DnDTriangularShape(m_pos
, m_size
, m_col
);
1184 case DnDShape
::Rectangle
: return new DnDRectangularShape(m_pos
, m_size
, m_col
);
1185 case DnDShape
::Ellipse
: return new DnDEllipticShape(m_pos
, m_size
, m_col
);
1189 bool DnDShapeDialog
::TransferDataToWindow()
1194 m_radio
->SetSelection(m_shape
->GetKind());
1195 m_pos
= m_shape
->GetPosition();
1196 m_size
= m_shape
->GetSize();
1197 m_col
= m_shape
->GetColour();
1201 m_radio
->SetSelection(DnDShape
::None
);
1202 m_pos
= wxPoint(1, 1);
1203 m_size
= wxSize(100, 100);
1206 m_textX
->SetValue(wxString() << m_pos
.x
);
1207 m_textY
->SetValue(wxString() << m_pos
.y
);
1208 m_textW
->SetValue(wxString() << m_size
.x
);
1209 m_textH
->SetValue(wxString() << m_size
.y
);
1214 bool DnDShapeDialog
::TransferDataFromWindow()
1216 m_shapeKind
= (DnDShape
::Kind
)m_radio
->GetSelection();
1218 m_pos
.x
= atoi(m_textX
->GetValue());
1219 m_pos
.y
= atoi(m_textY
->GetValue());
1220 m_size
.x
= atoi(m_textW
->GetValue());
1221 m_size
.y
= atoi(m_textH
->GetValue());
1223 if ( !m_pos
.x
|| !m_pos
.y
|| !m_size
.x
|| !m_size
.y
)
1225 wxMessageBox("All sizes and positions should be non null!",
1226 "Invalid shape", wxICON_HAND
| wxOK
, this);
1234 void DnDShapeDialog
::OnColour(wxCommandEvent
& WXUNUSED(event
))
1237 data
.SetChooseFull(TRUE
);
1238 for (int i
= 0; i
< 16; i
++)
1240 wxColour
colour(i
*16, i
*16, i
*16);
1241 data
.SetCustomColour(i
, colour
);
1244 wxColourDialog
dialog(this, &data
);
1245 if ( dialog
.ShowModal() == wxID_OK
)
1247 m_col
= dialog
.GetColourData().GetColour();
1251 // ----------------------------------------------------------------------------
1253 // ----------------------------------------------------------------------------
1255 DnDShapeFrame
*DnDShapeFrame
::ms_lastDropTarget
= NULL
;
1257 DnDShapeFrame
::DnDShapeFrame(wxFrame
*parent
)
1258 : wxFrame(parent
, -1, "Shape Frame",
1259 wxDefaultPosition
, wxSize(250, 150))
1263 wxMenu
*menuShape
= new wxMenu
;
1264 menuShape
->Append(Menu_Shape_New
, "&New default shape\tCtrl-S");
1265 menuShape
->Append(Menu_Shape_Edit
, "&Edit shape\tCtrl-E");
1266 menuShape
->AppendSeparator();
1267 menuShape
->Append(Menu_Shape_Clear
, "&Clear shape\tDel");
1269 wxMenu
*menuClipboard
= new wxMenu
;
1270 menuClipboard
->Append(Menu_ShapeClipboard_Copy
, "&Copy\tCtrl-C");
1271 menuClipboard
->Append(Menu_ShapeClipboard_Paste
, "&Paste\tCtrl-V");
1273 wxMenuBar
*menubar
= new wxMenuBar
;
1274 menubar
->Append(menuShape
, "&Shape");
1275 menubar
->Append(menuClipboard
, "&Clipboard");
1277 SetMenuBar(menubar
);
1279 SetStatusText("Press Ctrl-S to create a new shape");
1281 SetDropTarget(new DnDShapeDropTarget(this));
1285 SetBackgroundColour(*wxWHITE
);
1288 DnDShapeFrame
::~DnDShapeFrame()
1294 void DnDShapeFrame
::SetShape(DnDShape
*shape
)
1303 void DnDShapeFrame
::OnDrag(wxMouseEvent
& event
)
1312 // start drag operation
1313 DnDShapeDataObject
shapeData(m_shape
);
1314 wxDropSource
source(shapeData
, this);
1316 const char *pc
= NULL
;
1317 switch ( source
.DoDragDrop(TRUE
) )
1321 wxLogError("An error occured during drag and drop operation");
1325 SetStatusText("Nothing happened");
1334 if ( ms_lastDropTarget
!= this )
1336 // don't delete the shape if we dropped it on ourselves!
1342 SetStatusText("Drag and drop operation cancelled");
1348 SetStatusText(wxString("Shape successfully ") + pc
);
1350 //else: status text already set
1353 void DnDShapeFrame
::OnDrop(wxCoord x
, wxCoord y
, DnDShape
*shape
)
1355 ms_lastDropTarget
= this;
1360 s
.Printf("Shape dropped at (%ld, %ld)", pt
.x
, pt
.y
);
1367 void DnDShapeFrame
::OnEditShape(wxCommandEvent
& event
)
1369 DnDShapeDialog
dlg(this, m_shape
);
1370 if ( dlg
.ShowModal() == wxID_OK
)
1372 SetShape(dlg
.GetShape());
1376 SetStatusText("You can now drag the shape to another frame");
1381 void DnDShapeFrame
::OnNewShape(wxCommandEvent
& event
)
1383 SetShape(new DnDEllipticShape(wxPoint(10, 10), wxSize(80, 60), *wxRED
));
1385 SetStatusText("You can now drag the shape to another frame");
1388 void DnDShapeFrame
::OnClearShape(wxCommandEvent
& event
)
1393 void DnDShapeFrame
::OnCopyShape(wxCommandEvent
& event
)
1397 wxClipboardLocker clipLocker
;
1400 wxLogError("Can't open the clipboard");
1405 wxTheClipboard
->AddData(new DnDShapeDataObject(m_shape
));
1409 void DnDShapeFrame
::OnPasteShape(wxCommandEvent
& event
)
1411 wxClipboardLocker clipLocker
;
1414 wxLogError("Can't open the clipboard");
1419 DnDShapeDataObject
shapeDataObject(NULL
);
1420 if ( wxTheClipboard
->GetData(shapeDataObject
) )
1422 SetShape(shapeDataObject
.GetShape());
1426 wxLogStatus("No shape on the clipboard");
1430 void DnDShapeFrame
::OnUpdateUICopy(wxUpdateUIEvent
& event
)
1432 event
.Enable( m_shape
!= NULL
);
1435 void DnDShapeFrame
::OnUpdateUIPaste(wxUpdateUIEvent
& event
)
1437 event
.Enable( wxTheClipboard
->IsSupported(wxDataFormat(shapeFormatId
)) );
1440 void DnDShapeFrame
::OnPaint(wxPaintEvent
& event
)
1454 // ----------------------------------------------------------------------------
1456 // ----------------------------------------------------------------------------
1458 DnDShape
*DnDShape
::New(const void *buf
)
1460 const ShapeDump
& dump
= *(const ShapeDump
*)buf
;
1464 return new DnDTriangularShape(wxPoint(dump
.x
, dump
.y
),
1465 wxSize(dump
.w
, dump
.h
),
1466 wxColour(dump
.r
, dump
.g
, dump
.b
));
1469 return new DnDRectangularShape(wxPoint(dump
.x
, dump
.y
),
1470 wxSize(dump
.w
, dump
.h
),
1471 wxColour(dump
.r
, dump
.g
, dump
.b
));
1474 return new DnDEllipticShape(wxPoint(dump
.x
, dump
.y
),
1475 wxSize(dump
.w
, dump
.h
),
1476 wxColour(dump
.r
, dump
.g
, dump
.b
));
1479 wxFAIL_MSG("invalid shape!");
1484 // ----------------------------------------------------------------------------
1485 // DnDShapeDataObject
1486 // ----------------------------------------------------------------------------
1488 void DnDShapeDataObject
::CreateBitmap() const
1490 wxPoint pos
= m_shape
->GetPosition();
1491 wxSize size
= m_shape
->GetSize();
1492 int x
= pos
.x
+ size
.x
,
1494 wxBitmap
bitmap(x
, y
);
1496 dc
.SelectObject(bitmap
);
1497 dc
.SetBrush(wxBrush("white", wxSOLID
));
1500 dc
.SelectObject(wxNullBitmap
);
1502 DnDShapeDataObject
*self
= (DnDShapeDataObject
*)this; // const_cast
1503 self
->m_dataobj
.SetBitmap(bitmap
);
1504 self
->m_hasBitmap
= TRUE
;