1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        dnd.cpp
3 // Purpose:     Drag and drop sample
4 // Author:      Vadim Zeitlin
5 // Modified by:
6 // Created:     04/01/98
7 // RCS-ID:      $Id: dnd.cpp 48566 2007-09-05 12:39:06Z RR $
8 // Copyright:
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11 
12 #include "wx/wxprec.h"
13 
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17 
18 #ifndef WX_PRECOMP
19 #include "wx/wx.h"
20 #endif
21 
22 #include "wx/dnd.h"
23 #include "wx/dataobj.h"
24 #include "wx/image.h"
25 #include "wx/clipbrd.h"
26 #include "wx/colordlg.h"
27 #include "wx/metafile.h"
28 #include "wx/file.h"
29 
30 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
31     #include "../sample.xpm"
32 #if wxUSE_DRAG_AND_DROP
33     #include "dnd_copy.xpm"
34     #include "dnd_move.xpm"
35     #include "dnd_none.xpm"
36 #endif
37 #endif
38 
39 #if wxUSE_DRAG_AND_DROP
40 
41 // ----------------------------------------------------------------------------
42 // Derive two simple classes which just put in the listbox the strings (text or
43 // file names) we drop on them
44 // ----------------------------------------------------------------------------
45 
46 class DnDText : public wxTextDropTarget
47 {
48 public:
DnDText(wxListBox * pOwner)49     DnDText(wxListBox *pOwner) { m_pOwner = pOwner; }
50 
51     virtual bool OnDropText(wxCoord x, wxCoord y, const wxString& text);
52 
53 private:
54     wxListBox *m_pOwner;
55 };
56 
57 class DnDFile : public wxFileDropTarget
58 {
59 public:
DnDFile(wxListBox * pOwner)60     DnDFile(wxListBox *pOwner) { m_pOwner = pOwner; }
61 
62     virtual bool OnDropFiles(wxCoord x, wxCoord y,
63                              const wxArrayString& filenames);
64 
65 private:
66     wxListBox *m_pOwner;
67 };
68 
69 // ----------------------------------------------------------------------------
70 // Define a custom dtop target accepting URLs
71 // ----------------------------------------------------------------------------
72 
73 class URLDropTarget : public wxDropTarget
74 {
75 public:
URLDropTarget()76     URLDropTarget() { SetDataObject(new wxURLDataObject); }
77 
OnDropURL(wxCoord WXUNUSED (x),wxCoord WXUNUSED (y),const wxString & text)78     void OnDropURL(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), const wxString& text)
79     {
80         // of course, a real program would do something more useful here...
81         wxMessageBox(text, _T("wxDnD sample: got URL"),
82                      wxICON_INFORMATION | wxOK);
83     }
84 
85     // URLs can't be moved, only copied
OnDragOver(wxCoord WXUNUSED (x),wxCoord WXUNUSED (y),wxDragResult WXUNUSED (def))86     virtual wxDragResult OnDragOver(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
87                                     wxDragResult WXUNUSED(def))
88         {
89             return wxDragLink;  // At least IE 5.x needs wxDragLink, the
90                                 // other browsers on MSW seem okay with it too.
91         }
92 
93     // translate this to calls to OnDropURL() just for convenience
OnData(wxCoord x,wxCoord y,wxDragResult def)94     virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def)
95     {
96         if ( !GetData() )
97             return wxDragNone;
98 
99         OnDropURL(x, y, ((wxURLDataObject *)m_dataObject)->GetURL());
100 
101         return def;
102     }
103 };
104 
105 #endif // wxUSE_DRAG_AND_DROP
106 
107 // ----------------------------------------------------------------------------
108 // Define a new application type
109 // ----------------------------------------------------------------------------
110 
111 class DnDApp : public wxApp
112 {
113 public:
114     virtual bool OnInit();
115 };
116 
117 IMPLEMENT_APP(DnDApp)
118 
119 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
120 
121 // ----------------------------------------------------------------------------
122 // Define canvas class to show a bitmap
123 // ----------------------------------------------------------------------------
124 
125 class DnDCanvasBitmap : public wxScrolledWindow
126 {
127 public:
DnDCanvasBitmap(wxWindow * parent)128     DnDCanvasBitmap(wxWindow *parent) : wxScrolledWindow(parent) { }
129 
SetBitmap(const wxBitmap & bitmap)130     void SetBitmap(const wxBitmap& bitmap)
131     {
132         m_bitmap = bitmap;
133 
134         SetScrollbars(10, 10,
135                       m_bitmap.GetWidth() / 10, m_bitmap.GetHeight() / 10);
136 
137         Refresh();
138     }
139 
OnPaint(wxPaintEvent & WXUNUSED (event))140     void OnPaint(wxPaintEvent& WXUNUSED(event))
141     {
142         wxPaintDC dc(this);
143 
144         if ( m_bitmap.Ok() )
145         {
146             PrepareDC(dc);
147 
148             dc.DrawBitmap(m_bitmap, 0, 0);
149         }
150     }
151 
152 private:
153     wxBitmap m_bitmap;
154 
155     DECLARE_EVENT_TABLE()
156 };
157 
158 #if wxUSE_METAFILE
159 
160 // and the same thing fo metafiles
161 class DnDCanvasMetafile : public wxScrolledWindow
162 {
163 public:
DnDCanvasMetafile(wxWindow * parent)164     DnDCanvasMetafile(wxWindow *parent) : wxScrolledWindow(parent) { }
165 
SetMetafile(const wxMetafile & metafile)166     void SetMetafile(const wxMetafile& metafile)
167     {
168         m_metafile = metafile;
169 
170         SetScrollbars(10, 10,
171                       m_metafile.GetWidth() / 10, m_metafile.GetHeight() / 10);
172 
173         Refresh();
174     }
175 
OnPaint(wxPaintEvent &)176     void OnPaint(wxPaintEvent&)
177     {
178         wxPaintDC dc(this);
179 
180         if ( m_metafile.Ok() )
181         {
182             PrepareDC(dc);
183 
184             m_metafile.Play(&dc);
185         }
186     }
187 
188 private:
189     wxMetafile m_metafile;
190 
191     DECLARE_EVENT_TABLE()
192 };
193 
194 #endif // wxUSE_METAFILE
195 
196 // ----------------------------------------------------------------------------
197 // Define a new frame type for the main frame
198 // ----------------------------------------------------------------------------
199 
200 class DnDFrame : public wxFrame
201 {
202 public:
203     DnDFrame(wxFrame *frame, const wxChar *title, int x, int y, int w, int h);
204     virtual ~DnDFrame();
205 
206     void OnPaint(wxPaintEvent& event);
207     void OnSize(wxSizeEvent& event);
208     void OnQuit(wxCommandEvent& event);
209     void OnAbout(wxCommandEvent& event);
210     void OnOpenFile(wxCommandEvent& event);
211     void OnDrag(wxCommandEvent& event);
212     void OnDragMoveByDefault(wxCommandEvent& event);
213     void OnDragMoveAllow(wxCommandEvent& event);
214     void OnNewFrame(wxCommandEvent& event);
215     void OnHelp (wxCommandEvent& event);
216 #if wxUSE_LOG
217     void OnLogClear(wxCommandEvent& event);
218 #endif // wxUSE_LOG
219 
220     void OnCopy(wxCommandEvent& event);
221     void OnPaste(wxCommandEvent& event);
222 
223     void OnCopyBitmap(wxCommandEvent& event);
224     void OnPasteBitmap(wxCommandEvent& event);
225 
226 #if wxUSE_METAFILE
227     void OnPasteMetafile(wxCommandEvent& event);
228 #endif // wxUSE_METAFILE
229 
230     void OnCopyFiles(wxCommandEvent& event);
231 
232     void OnLeftDown(wxMouseEvent& event);
233     void OnRightDown(wxMouseEvent& event);
234 
235     void OnUpdateUIMoveByDefault(wxUpdateUIEvent& event);
236 
237     void OnUpdateUIPasteText(wxUpdateUIEvent& event);
238     void OnUpdateUIPasteBitmap(wxUpdateUIEvent& event);
239 
240     DECLARE_EVENT_TABLE()
241 
242 private:
243     // GUI controls
244     wxListBox  *m_ctrlFile,
245                *m_ctrlText;
246 
247 #if wxUSE_LOG
248     wxTextCtrl *m_ctrlLog;
249 
250     wxLog *m_pLog,
251           *m_pLogPrev;
252 #endif // wxUSE_LOG
253 
254     // move the text by default (or copy)?
255     bool m_moveByDefault;
256 
257     // allow moving the text at all?
258     bool m_moveAllow;
259 
260     // the text we drag
261     wxString  m_strText;
262 };
263 
264 // ----------------------------------------------------------------------------
265 // A shape is an example of application-specific data which may be transported
266 // via drag-and-drop or clipboard: in our case, we have different geometric
267 // shapes, each one with its own colour and position
268 // ----------------------------------------------------------------------------
269 
270 #if wxUSE_DRAG_AND_DROP
271 
272 class DnDShape
273 {
274 public:
275     enum Kind
276     {
277         None,
278         Triangle,
279         Rectangle,
280         Ellipse
281     };
282 
DnDShape(const wxPoint & pos,const wxSize & size,const wxColour & col)283     DnDShape(const wxPoint& pos,
284              const wxSize& size,
285              const wxColour& col)
286         : m_pos(pos), m_size(size), m_col(col)
287     {
288     }
289 
290     // this is for debugging - lets us see when exactly an object is freed
291     // (this may be later than you think if it's on the clipboard, for example)
~DnDShape()292     virtual ~DnDShape() { }
293 
294     // the functions used for drag-and-drop: they dump and restore a shape into
295     // some bitwise-copiable data (might use streams too...)
296     // ------------------------------------------------------------------------
297 
298     // restore from buffer
299     static DnDShape *New(const void *buf);
300 
GetDataSize() const301     virtual size_t GetDataSize() const
302     {
303         return sizeof(ShapeDump);
304     }
305 
GetDataHere(void * buf) const306     virtual void GetDataHere(void *buf) const
307     {
308         ShapeDump& dump = *(ShapeDump *)buf;
309         dump.x = m_pos.x;
310         dump.y = m_pos.y;
311         dump.w = m_size.x;
312         dump.h = m_size.y;
313         dump.r = m_col.Red();
314         dump.g = m_col.Green();
315         dump.b = m_col.Blue();
316         dump.k = GetKind();
317     }
318 
319     // accessors
GetPosition() const320     const wxPoint& GetPosition() const { return m_pos; }
GetColour() const321     const wxColour& GetColour() const { return m_col; }
GetSize() const322     const wxSize& GetSize() const { return m_size; }
323 
Move(const wxPoint & pos)324     void Move(const wxPoint& pos) { m_pos = pos; }
325 
326     // to implement in derived classes
327     virtual Kind GetKind() const = 0;
328 
Draw(wxDC & dc)329     virtual void Draw(wxDC& dc)
330     {
331         dc.SetPen(wxPen(m_col, 1, wxSOLID));
332     }
333 
334 protected:
335     //get a point 1 up and 1 left, otherwise the mid-point of a triangle is on the line
GetCentre() const336     wxPoint GetCentre() const
337          { return wxPoint(m_pos.x + m_size.x / 2 - 1, m_pos.y + m_size.y / 2 - 1); }
338 
339     struct ShapeDump
340     {
341         wxCoord x, y,             // position
342                 w, h;             // size
343         int k;                    // kind
344         unsigned char r, g, b;    // colour
345     };
346 
347     wxPoint  m_pos;
348     wxSize   m_size;
349     wxColour m_col;
350 };
351 
352 class DnDTriangularShape : public DnDShape
353 {
354 public:
DnDTriangularShape(const wxPoint & pos,const wxSize & size,const wxColour & col)355     DnDTriangularShape(const wxPoint& pos,
356                        const wxSize& size,
357                        const wxColour& col)
358         : DnDShape(pos, size, col)
359     {
360         wxLogMessage(wxT("DnDTriangularShape is being created"));
361     }
362 
~DnDTriangularShape()363     virtual ~DnDTriangularShape()
364     {
365         wxLogMessage(wxT("DnDTriangularShape is being deleted"));
366     }
367 
GetKind() const368     virtual Kind GetKind() const { return Triangle; }
Draw(wxDC & dc)369     virtual void Draw(wxDC& dc)
370     {
371         DnDShape::Draw(dc);
372 
373         // well, it's a bit difficult to describe a triangle by position and
374         // size, but we're not doing geometry here, do we? ;-)
375         wxPoint p1(m_pos);
376         wxPoint p2(m_pos.x + m_size.x, m_pos.y);
377         wxPoint p3(m_pos.x, m_pos.y + m_size.y);
378 
379         dc.DrawLine(p1, p2);
380         dc.DrawLine(p2, p3);
381         dc.DrawLine(p3, p1);
382 
383         //works in multicolor modes; on GTK (at least) will fail in 16-bit color
384         dc.SetBrush(wxBrush(m_col, wxSOLID));
385         dc.FloodFill(GetCentre(), m_col, wxFLOOD_BORDER);
386     }
387 };
388 
389 class DnDRectangularShape : public DnDShape
390 {
391 public:
DnDRectangularShape(const wxPoint & pos,const wxSize & size,const wxColour & col)392     DnDRectangularShape(const wxPoint& pos,
393                         const wxSize& size,
394                         const wxColour& col)
395         : DnDShape(pos, size, col)
396     {
397         wxLogMessage(wxT("DnDRectangularShape is being created"));
398     }
399 
~DnDRectangularShape()400     virtual ~DnDRectangularShape()
401     {
402         wxLogMessage(wxT("DnDRectangularShape is being deleted"));
403     }
404 
GetKind() const405     virtual Kind GetKind() const { return Rectangle; }
Draw(wxDC & dc)406     virtual void Draw(wxDC& dc)
407     {
408         DnDShape::Draw(dc);
409 
410         wxPoint p1(m_pos);
411         wxPoint p2(p1.x + m_size.x, p1.y);
412         wxPoint p3(p2.x, p2.y + m_size.y);
413         wxPoint p4(p1.x, p3.y);
414 
415         dc.DrawLine(p1, p2);
416         dc.DrawLine(p2, p3);
417         dc.DrawLine(p3, p4);
418         dc.DrawLine(p4, p1);
419 
420         dc.SetBrush(wxBrush(m_col, wxSOLID));
421         dc.FloodFill(GetCentre(), m_col, wxFLOOD_BORDER);
422     }
423 };
424 
425 class DnDEllipticShape : public DnDShape
426 {
427 public:
DnDEllipticShape(const wxPoint & pos,const wxSize & size,const wxColour & col)428     DnDEllipticShape(const wxPoint& pos,
429                      const wxSize& size,
430                      const wxColour& col)
431         : DnDShape(pos, size, col)
432     {
433         wxLogMessage(wxT("DnDEllipticShape is being created"));
434     }
435 
~DnDEllipticShape()436     virtual ~DnDEllipticShape()
437     {
438         wxLogMessage(wxT("DnDEllipticShape is being deleted"));
439     }
440 
GetKind() const441     virtual Kind GetKind() const { return Ellipse; }
Draw(wxDC & dc)442     virtual void Draw(wxDC& dc)
443     {
444         DnDShape::Draw(dc);
445 
446         dc.DrawEllipse(m_pos, m_size);
447 
448         dc.SetBrush(wxBrush(m_col, wxSOLID));
449         dc.FloodFill(GetCentre(), m_col, wxFLOOD_BORDER);
450     }
451 };
452 
453 // ----------------------------------------------------------------------------
454 // A wxDataObject specialisation for the application-specific data
455 // ----------------------------------------------------------------------------
456 
457 static const wxChar *shapeFormatId = wxT("wxShape");
458 
459 class DnDShapeDataObject : public wxDataObject
460 {
461 public:
462     // ctor doesn't copy the pointer, so it shouldn't go away while this object
463     // is alive
DnDShapeDataObject(DnDShape * shape=(DnDShape *)NULL)464     DnDShapeDataObject(DnDShape *shape = (DnDShape *)NULL)
465     {
466         if ( shape )
467         {
468             // we need to copy the shape because the one we're handled may be
469             // deleted while it's still on the clipboard (for example) - and we
470             // reuse the serialisation methods here to copy it
471             void *buf = malloc(shape->DnDShape::GetDataSize());
472             shape->GetDataHere(buf);
473             m_shape = DnDShape::New(buf);
474 
475             free(buf);
476         }
477         else
478         {
479             // nothing to copy
480             m_shape = NULL;
481         }
482 
483         // this string should uniquely identify our format, but is otherwise
484         // arbitrary
485         m_formatShape.SetId(shapeFormatId);
486 
487         // we don't draw the shape to a bitmap until it's really needed (i.e.
488         // we're asked to do so)
489         m_hasBitmap = false;
490 #if wxUSE_METAFILE
491         m_hasMetaFile = false;
492 #endif // wxUSE_METAFILE
493     }
494 
~DnDShapeDataObject()495     virtual ~DnDShapeDataObject() { delete m_shape; }
496 
497     // after a call to this function, the shape is owned by the caller and it
498     // is responsible for deleting it!
499     //
500     // NB: a better solution would be to make DnDShapes ref counted and this
501     //     is what should probably be done in a real life program, otherwise
502     //     the ownership problems become too complicated really fast
GetShape()503     DnDShape *GetShape()
504     {
505         DnDShape *shape = m_shape;
506 
507         m_shape = (DnDShape *)NULL;
508         m_hasBitmap = false;
509 #if wxUSE_METAFILE
510         m_hasMetaFile = false;
511 #endif // wxUSE_METAFILE
512 
513         return shape;
514     }
515 
516     // implement base class pure virtuals
517     // ----------------------------------
518 
GetPreferredFormat(Direction WXUNUSED (dir)) const519     virtual wxDataFormat GetPreferredFormat(Direction WXUNUSED(dir)) const
520     {
521         return m_formatShape;
522     }
523 
GetFormatCount(Direction dir) const524     virtual size_t GetFormatCount(Direction dir) const
525     {
526         // our custom format is supported by both GetData() and SetData()
527         size_t nFormats = 1;
528         if ( dir == Get )
529         {
530             // but the bitmap format(s) are only supported for output
531             nFormats += m_dobjBitmap.GetFormatCount(dir);
532 
533 #if wxUSE_METAFILE
534             nFormats += m_dobjMetaFile.GetFormatCount(dir);
535 #endif // wxUSE_METAFILE
536         }
537 
538         return nFormats;
539     }
540 
GetAllFormats(wxDataFormat * formats,Direction dir) const541     virtual void GetAllFormats(wxDataFormat *formats, Direction dir) const
542     {
543         formats[0] = m_formatShape;
544         if ( dir == Get )
545         {
546             // in Get direction we additionally support bitmaps and metafiles
547             // under Windows
548             m_dobjBitmap.GetAllFormats(&formats[1], dir);
549 
550 #if wxUSE_METAFILE
551             // don't assume that m_dobjBitmap has only 1 format
552             m_dobjMetaFile.GetAllFormats(&formats[1 +
553                     m_dobjBitmap.GetFormatCount(dir)], dir);
554 #endif // wxUSE_METAFILE
555         }
556     }
557 
GetDataSize(const wxDataFormat & format) const558     virtual size_t GetDataSize(const wxDataFormat& format) const
559     {
560         if ( format == m_formatShape )
561         {
562             return m_shape->GetDataSize();
563         }
564 #if wxUSE_METAFILE
565         else if ( m_dobjMetaFile.IsSupported(format) )
566         {
567             if ( !m_hasMetaFile )
568                 CreateMetaFile();
569 
570             return m_dobjMetaFile.GetDataSize(format);
571         }
572 #endif // wxUSE_METAFILE
573         else
574         {
575             wxASSERT_MSG( m_dobjBitmap.IsSupported(format),
576                           wxT("unexpected format") );
577 
578             if ( !m_hasBitmap )
579                 CreateBitmap();
580 
581             return m_dobjBitmap.GetDataSize();
582         }
583     }
584 
GetDataHere(const wxDataFormat & format,void * pBuf) const585     virtual bool GetDataHere(const wxDataFormat& format, void *pBuf) const
586     {
587         if ( format == m_formatShape )
588         {
589             m_shape->GetDataHere(pBuf);
590 
591             return true;
592         }
593 #if wxUSE_METAFILE
594         else if ( m_dobjMetaFile.IsSupported(format) )
595         {
596             if ( !m_hasMetaFile )
597                 CreateMetaFile();
598 
599             return m_dobjMetaFile.GetDataHere(format, pBuf);
600         }
601 #endif // wxUSE_METAFILE
602         else
603         {
604             wxASSERT_MSG( m_dobjBitmap.IsSupported(format),
605                           wxT("unexpected format") );
606 
607             if ( !m_hasBitmap )
608                 CreateBitmap();
609 
610             return m_dobjBitmap.GetDataHere(pBuf);
611         }
612     }
613 
SetData(const wxDataFormat & format,size_t WXUNUSED (len),const void * buf)614     virtual bool SetData(const wxDataFormat& format,
615                          size_t WXUNUSED(len), const void *buf)
616     {
617         wxCHECK_MSG( format == m_formatShape, false,
618                      wxT( "unsupported format") );
619 
620         delete m_shape;
621         m_shape = DnDShape::New(buf);
622 
623         // the shape has changed
624         m_hasBitmap = false;
625 
626 #if wxUSE_METAFILE
627         m_hasMetaFile = false;
628 #endif // wxUSE_METAFILE
629 
630         return true;
631     }
632 
633 private:
634     // creates a bitmap and assigns it to m_dobjBitmap (also sets m_hasBitmap)
635     void CreateBitmap() const;
636 #if wxUSE_METAFILE
637     void CreateMetaFile() const;
638 #endif // wxUSE_METAFILE
639 
640     wxDataFormat        m_formatShape;  // our custom format
641 
642     wxBitmapDataObject  m_dobjBitmap;   // it handles bitmaps
643     bool                m_hasBitmap;    // true if m_dobjBitmap has valid bitmap
644 
645 #if wxUSE_METAFILE
646     wxMetaFileDataObject m_dobjMetaFile;// handles metafiles
647     bool                 m_hasMetaFile; // true if we have valid metafile
648 #endif // wxUSE_METAFILE
649 
650     DnDShape           *m_shape;        // our data
651 };
652 
653 // ----------------------------------------------------------------------------
654 // A dialog to edit shape properties
655 // ----------------------------------------------------------------------------
656 
657 class DnDShapeDialog : public wxDialog
658 {
659 public:
660     DnDShapeDialog(wxFrame *parent, DnDShape *shape);
661 
662     DnDShape *GetShape() const;
663 
664     virtual bool TransferDataToWindow();
665     virtual bool TransferDataFromWindow();
666 
667     void OnColour(wxCommandEvent& event);
668 
669 private:
670     // input
671     DnDShape *m_shape;
672 
673     // output
674     DnDShape::Kind m_shapeKind;
675     wxPoint  m_pos;
676     wxSize   m_size;
677     wxColour m_col;
678 
679     // controls
680     wxRadioBox *m_radio;
681     wxTextCtrl *m_textX,
682                *m_textY,
683                *m_textW,
684                *m_textH;
685 
686     DECLARE_EVENT_TABLE()
687 };
688 
689 // ----------------------------------------------------------------------------
690 // A frame for the shapes which can be drag-and-dropped between frames
691 // ----------------------------------------------------------------------------
692 
693 class DnDShapeFrame : public wxFrame
694 {
695 public:
696     DnDShapeFrame(wxFrame *parent);
697     ~DnDShapeFrame();
698 
699     void SetShape(DnDShape *shape);
SetShape(const wxRegion & region)700     virtual bool SetShape(const wxRegion &region)
701     {
702         return wxFrame::SetShape( region );
703     }
704 
705     // callbacks
706     void OnNewShape(wxCommandEvent& event);
707     void OnEditShape(wxCommandEvent& event);
708     void OnClearShape(wxCommandEvent& event);
709 
710     void OnCopyShape(wxCommandEvent& event);
711     void OnPasteShape(wxCommandEvent& event);
712 
713     void OnUpdateUICopy(wxUpdateUIEvent& event);
714     void OnUpdateUIPaste(wxUpdateUIEvent& event);
715 
716     void OnDrag(wxMouseEvent& event);
717     void OnPaint(wxPaintEvent& event);
718     void OnDrop(wxCoord x, wxCoord y, DnDShape *shape);
719 
720 private:
721     DnDShape *m_shape;
722 
723     static DnDShapeFrame *ms_lastDropTarget;
724 
725     DECLARE_EVENT_TABLE()
726 };
727 
728 // ----------------------------------------------------------------------------
729 // wxDropTarget derivation for DnDShapes
730 // ----------------------------------------------------------------------------
731 
732 class DnDShapeDropTarget : public wxDropTarget
733 {
734 public:
DnDShapeDropTarget(DnDShapeFrame * frame)735     DnDShapeDropTarget(DnDShapeFrame *frame)
736         : wxDropTarget(new DnDShapeDataObject)
737     {
738         m_frame = frame;
739     }
740 
741     // override base class (pure) virtuals
OnEnter(wxCoord x,wxCoord y,wxDragResult def)742     virtual wxDragResult OnEnter(wxCoord x, wxCoord y, wxDragResult def)
743     {
744 #if wxUSE_STATUSBAR
745         m_frame->SetStatusText(_T("Mouse entered the frame"));
746 #endif // wxUSE_STATUSBAR
747         return OnDragOver(x, y, def);
748     }
OnLeave()749     virtual void OnLeave()
750     {
751 #if wxUSE_STATUSBAR
752         m_frame->SetStatusText(_T("Mouse left the frame"));
753 #endif // wxUSE_STATUSBAR
754     }
OnData(wxCoord x,wxCoord y,wxDragResult def)755     virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def)
756     {
757         if ( !GetData() )
758         {
759             wxLogError(wxT("Failed to get drag and drop data"));
760 
761             return wxDragNone;
762         }
763 
764         m_frame->OnDrop(x, y,
765                         ((DnDShapeDataObject *)GetDataObject())->GetShape());
766 
767         return def;
768     }
769 
770 private:
771     DnDShapeFrame *m_frame;
772 };
773 
774 #endif // wxUSE_DRAG_AND_DROP
775 
776 // ----------------------------------------------------------------------------
777 // functions prototypes
778 // ----------------------------------------------------------------------------
779 
780 static void ShowBitmap(const wxBitmap& bitmap);
781 
782 #if wxUSE_METAFILE
783 static void ShowMetaFile(const wxMetaFile& metafile);
784 #endif // wxUSE_METAFILE
785 
786 // ----------------------------------------------------------------------------
787 // IDs for the menu commands
788 // ----------------------------------------------------------------------------
789 
790 enum
791 {
792     Menu_Quit = 1,
793     Menu_Drag,
794     Menu_DragMoveDef,
795     Menu_DragMoveAllow,
796     Menu_NewFrame,
797     Menu_About = 101,
798     Menu_OpenFile,
799     Menu_Help,
800     Menu_Clear,
801     Menu_Copy,
802     Menu_Paste,
803     Menu_CopyBitmap,
804     Menu_PasteBitmap,
805     Menu_PasteMFile,
806     Menu_CopyFiles,
807     Menu_Shape_New = 500,
808     Menu_Shape_Edit,
809     Menu_Shape_Clear,
810     Menu_ShapeClipboard_Copy,
811     Menu_ShapeClipboard_Paste,
812     Button_Colour = 1001
813 };
814 
BEGIN_EVENT_TABLE(DnDFrame,wxFrame)815 BEGIN_EVENT_TABLE(DnDFrame, wxFrame)
816     EVT_MENU(Menu_Quit,       DnDFrame::OnQuit)
817     EVT_MENU(Menu_About,      DnDFrame::OnAbout)
818     EVT_MENU(Menu_OpenFile,      DnDFrame::OnOpenFile)
819     EVT_MENU(Menu_Drag,       DnDFrame::OnDrag)
820     EVT_MENU(Menu_DragMoveDef,  DnDFrame::OnDragMoveByDefault)
821     EVT_MENU(Menu_DragMoveAllow,DnDFrame::OnDragMoveAllow)
822     EVT_MENU(Menu_NewFrame,   DnDFrame::OnNewFrame)
823     EVT_MENU(Menu_Help,       DnDFrame::OnHelp)
824 #if wxUSE_LOG
825     EVT_MENU(Menu_Clear,      DnDFrame::OnLogClear)
826 #endif // wxUSE_LOG
827     EVT_MENU(Menu_Copy,       DnDFrame::OnCopy)
828     EVT_MENU(Menu_Paste,      DnDFrame::OnPaste)
829     EVT_MENU(Menu_CopyBitmap, DnDFrame::OnCopyBitmap)
830     EVT_MENU(Menu_PasteBitmap,DnDFrame::OnPasteBitmap)
831 #if wxUSE_METAFILE
832     EVT_MENU(Menu_PasteMFile, DnDFrame::OnPasteMetafile)
833 #endif // wxUSE_METAFILE
834     EVT_MENU(Menu_CopyFiles,  DnDFrame::OnCopyFiles)
835 
836     EVT_UPDATE_UI(Menu_DragMoveDef, DnDFrame::OnUpdateUIMoveByDefault)
837 
838     EVT_UPDATE_UI(Menu_Paste,       DnDFrame::OnUpdateUIPasteText)
839     EVT_UPDATE_UI(Menu_PasteBitmap, DnDFrame::OnUpdateUIPasteBitmap)
840 
841     EVT_LEFT_DOWN(            DnDFrame::OnLeftDown)
842     EVT_RIGHT_DOWN(           DnDFrame::OnRightDown)
843     EVT_PAINT(                DnDFrame::OnPaint)
844     EVT_SIZE(                 DnDFrame::OnSize)
845 END_EVENT_TABLE()
846 
847 #if wxUSE_DRAG_AND_DROP
848 
849 BEGIN_EVENT_TABLE(DnDShapeFrame, wxFrame)
850     EVT_MENU(Menu_Shape_New,    DnDShapeFrame::OnNewShape)
851     EVT_MENU(Menu_Shape_Edit,   DnDShapeFrame::OnEditShape)
852     EVT_MENU(Menu_Shape_Clear,  DnDShapeFrame::OnClearShape)
853 
854     EVT_MENU(Menu_ShapeClipboard_Copy,  DnDShapeFrame::OnCopyShape)
855     EVT_MENU(Menu_ShapeClipboard_Paste, DnDShapeFrame::OnPasteShape)
856 
857     EVT_UPDATE_UI(Menu_ShapeClipboard_Copy,  DnDShapeFrame::OnUpdateUICopy)
858     EVT_UPDATE_UI(Menu_ShapeClipboard_Paste, DnDShapeFrame::OnUpdateUIPaste)
859 
860     EVT_LEFT_DOWN(DnDShapeFrame::OnDrag)
861 
862     EVT_PAINT(DnDShapeFrame::OnPaint)
863 END_EVENT_TABLE()
864 
865 BEGIN_EVENT_TABLE(DnDShapeDialog, wxDialog)
866     EVT_BUTTON(Button_Colour, DnDShapeDialog::OnColour)
867 END_EVENT_TABLE()
868 
869 #endif // wxUSE_DRAG_AND_DROP
870 
871 BEGIN_EVENT_TABLE(DnDCanvasBitmap, wxScrolledWindow)
872     EVT_PAINT(DnDCanvasBitmap::OnPaint)
873 END_EVENT_TABLE()
874 
875 #if wxUSE_METAFILE
876 BEGIN_EVENT_TABLE(DnDCanvasMetafile, wxScrolledWindow)
877     EVT_PAINT(DnDCanvasMetafile::OnPaint)
878 END_EVENT_TABLE()
879 #endif // wxUSE_METAFILE
880 
881 #endif // wxUSE_DRAG_AND_DROP
882 
883 // ============================================================================
884 // implementation
885 // ============================================================================
886 
887 // `Main program' equivalent, creating windows and returning main app frame
888 bool DnDApp::OnInit()
889 {
890 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
891     // switch on trace messages
892 #if wxUSE_LOG
893 #if defined(__WXGTK__)
894     wxLog::AddTraceMask(_T("clipboard"));
895 #elif defined(__WXMSW__)
896     wxLog::AddTraceMask(wxTRACE_OleCalls);
897 #endif
898 #endif // wxUSE_LOG
899 
900 #if wxUSE_LIBPNG
901     wxImage::AddHandler( new wxPNGHandler );
902 #endif
903 
904     // under X we usually want to use the primary selection by default (which
905     // is shared with other apps)
906     wxTheClipboard->UsePrimarySelection();
907 
908     // create the main frame window
909     DnDFrame *frame = new DnDFrame((wxFrame  *) NULL,
910                                    _T("Drag-and-Drop/Clipboard wxWidgets Sample"),
911                                    10, 100, 750, 540);
912 
913     // activate it
914     frame->Show(true);
915 
916     SetTopWindow(frame);
917 
918     return true;
919 #else
920     wxMessageBox( _T("This sample has to be compiled with wxUSE_DRAG_AND_DROP"), _T("Building error"), wxOK);
921     return false;
922 #endif // wxUSE_DRAG_AND_DROP
923 }
924 
925 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
926 
DnDFrame(wxFrame * frame,const wxChar * title,int x,int y,int w,int h)927 DnDFrame::DnDFrame(wxFrame *frame, const wxChar *title, int x, int y, int w, int h)
928         : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)),
929           m_strText(_T("wxWidgets drag & drop works :-)"))
930 
931 {
932     // frame icon and status bar
933     SetIcon(wxICON(sample));
934 
935 #if wxUSE_STATUSBAR
936     CreateStatusBar();
937 #endif // wxUSE_STATUSBAR
938 
939     // construct menu
940     wxMenu *file_menu = new wxMenu;
941     file_menu->Append(Menu_Drag, _T("&Test drag..."));
942     file_menu->AppendCheckItem(Menu_DragMoveDef, _T("&Move by default"));
943     file_menu->AppendCheckItem(Menu_DragMoveAllow, _T("&Allow moving"));
944     file_menu->AppendSeparator();
945     file_menu->Append(Menu_NewFrame, _T("&New frame\tCtrl-N"));
946     file_menu->AppendSeparator();
947     file_menu->Append(Menu_OpenFile, _T("&Open file..."));
948     file_menu->AppendSeparator();
949     file_menu->Append(Menu_Quit, _T("E&xit\tCtrl-Q"));
950 
951 #if wxUSE_LOG
952     wxMenu *log_menu = new wxMenu;
953     log_menu->Append(Menu_Clear, _T("Clear\tCtrl-L"));
954 #endif // wxUSE_LOG
955 
956     wxMenu *help_menu = new wxMenu;
957     help_menu->Append(Menu_Help, _T("&Help..."));
958     help_menu->AppendSeparator();
959     help_menu->Append(Menu_About, _T("&About"));
960 
961     wxMenu *clip_menu = new wxMenu;
962     clip_menu->Append(Menu_Copy, _T("&Copy text\tCtrl-C"));
963     clip_menu->Append(Menu_Paste, _T("&Paste text\tCtrl-V"));
964     clip_menu->AppendSeparator();
965     clip_menu->Append(Menu_CopyBitmap, _T("Copy &bitmap\tCtrl-Shift-C"));
966     clip_menu->Append(Menu_PasteBitmap, _T("Paste b&itmap\tCtrl-Shift-V"));
967 #if wxUSE_METAFILE
968     clip_menu->AppendSeparator();
969     clip_menu->Append(Menu_PasteMFile, _T("Paste &metafile\tCtrl-M"));
970 #endif // wxUSE_METAFILE
971     clip_menu->AppendSeparator();
972     clip_menu->Append(Menu_CopyFiles, _T("Copy &files\tCtrl-F"));
973 
974     wxMenuBar *menu_bar = new wxMenuBar;
975     menu_bar->Append(file_menu, _T("&File"));
976 #if wxUSE_LOG
977     menu_bar->Append(log_menu,  _T("&Log"));
978 #endif // wxUSE_LOG
979     menu_bar->Append(clip_menu, _T("&Clipboard"));
980     menu_bar->Append(help_menu, _T("&Help"));
981 
982     SetMenuBar(menu_bar);
983 
984     // make a panel with 3 subwindows
985     wxString strFile(_T("Drop files here!")), strText(_T("Drop text on me"));
986 
987     m_ctrlFile  = new wxListBox(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 1, &strFile,
988                                 wxLB_HSCROLL | wxLB_ALWAYS_SB );
989     m_ctrlText  = new wxListBox(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 1, &strText,
990                                 wxLB_HSCROLL | wxLB_ALWAYS_SB );
991 
992 #if wxUSE_LOG
993     m_ctrlLog   = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
994                                  wxTE_MULTILINE | wxTE_READONLY |
995                                  wxSUNKEN_BORDER );
996 
997     // redirect log messages to the text window
998     m_pLog = new wxLogTextCtrl(m_ctrlLog);
999     m_pLogPrev = wxLog::SetActiveTarget(m_pLog);
1000 #endif // wxUSE_LOG
1001 
1002 #if wxUSE_DRAG_AND_DROP
1003     // associate drop targets with the controls
1004     m_ctrlFile->SetDropTarget(new DnDFile(m_ctrlFile));
1005     m_ctrlText->SetDropTarget(new DnDText(m_ctrlText));
1006 #if wxUSE_LOG
1007     m_ctrlLog->SetDropTarget(new URLDropTarget);
1008 #endif // wxUSE_LOG
1009 #endif // wxUSE_DRAG_AND_DROP
1010 
1011     wxBoxSizer *sizer_top = new wxBoxSizer( wxHORIZONTAL );
1012     sizer_top->Add(m_ctrlFile, 1, wxEXPAND );
1013     sizer_top->Add(m_ctrlText, 1, wxEXPAND );
1014 
1015     wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
1016     sizer->Add(sizer_top, 2, wxEXPAND );
1017 #if wxUSE_LOG
1018     sizer->Add(m_ctrlLog, 1, wxEXPAND);
1019     sizer->SetItemMinSize(m_ctrlLog, 450, 100);
1020 #endif // wxUSE_LOG
1021     sizer->AddSpacer(50);
1022 
1023     SetSizer(sizer);
1024     sizer->SetSizeHints( this );
1025 
1026     // copy data by default but allow moving it as well
1027     m_moveByDefault = false;
1028     m_moveAllow = true;
1029     menu_bar->Check(Menu_DragMoveAllow, true);
1030 }
1031 
OnQuit(wxCommandEvent & WXUNUSED (event))1032 void DnDFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
1033 {
1034     Close(true);
1035 }
1036 
OnSize(wxSizeEvent & event)1037 void DnDFrame::OnSize(wxSizeEvent& event)
1038 {
1039     Refresh();
1040 
1041     event.Skip();
1042 }
1043 
OnPaint(wxPaintEvent & WXUNUSED (event))1044 void DnDFrame::OnPaint(wxPaintEvent& WXUNUSED(event))
1045 {
1046     int w = 0;
1047     int h = 0;
1048     GetClientSize( &w, &h );
1049 
1050     wxPaintDC dc(this);
1051     // dc.Clear(); -- this kills wxGTK
1052     dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL, false, _T("charter") ) );
1053     dc.DrawText( _T("Drag text from here!"), 100, h-50 );
1054 }
1055 
OnUpdateUIMoveByDefault(wxUpdateUIEvent & event)1056 void DnDFrame::OnUpdateUIMoveByDefault(wxUpdateUIEvent& event)
1057 {
1058     // only can move by default if moving is allowed at all
1059     event.Enable(m_moveAllow);
1060 }
1061 
OnUpdateUIPasteText(wxUpdateUIEvent & event)1062 void DnDFrame::OnUpdateUIPasteText(wxUpdateUIEvent& event)
1063 {
1064 #ifdef __WXDEBUG__
1065     // too many trace messages if we don't do it - this function is called
1066     // very often
1067     wxLogNull nolog;
1068 #endif
1069 
1070     event.Enable( wxTheClipboard->IsSupported(wxDF_TEXT) );
1071 }
1072 
OnUpdateUIPasteBitmap(wxUpdateUIEvent & event)1073 void DnDFrame::OnUpdateUIPasteBitmap(wxUpdateUIEvent& event)
1074 {
1075 #ifdef __WXDEBUG__
1076     // too many trace messages if we don't do it - this function is called
1077     // very often
1078     wxLogNull nolog;
1079 #endif
1080 
1081     event.Enable( wxTheClipboard->IsSupported(wxDF_BITMAP) );
1082 }
1083 
OnNewFrame(wxCommandEvent & WXUNUSED (event))1084 void DnDFrame::OnNewFrame(wxCommandEvent& WXUNUSED(event))
1085 {
1086 #if wxUSE_DRAG_AND_DROP
1087     (new DnDShapeFrame(this))->Show(true);
1088 
1089     wxLogStatus(this, wxT("Double click the new frame to select a shape for it"));
1090 #endif // wxUSE_DRAG_AND_DROP
1091 }
1092 
OnDrag(wxCommandEvent & WXUNUSED (event))1093 void DnDFrame::OnDrag(wxCommandEvent& WXUNUSED(event))
1094 {
1095 #if wxUSE_DRAG_AND_DROP
1096     wxString strText = wxGetTextFromUser
1097         (
1098             _T("After you enter text in this dialog, press any mouse\n")
1099             _T("button in the bottom (empty) part of the frame and \n")
1100             _T("drag it anywhere - you will be in fact dragging the\n")
1101          _T("text object containing this text"),
1102          _T("Please enter some text"), m_strText, this
1103         );
1104 
1105     m_strText = strText;
1106 #endif // wxUSE_DRAG_AND_DROP
1107 }
1108 
OnDragMoveByDefault(wxCommandEvent & event)1109 void DnDFrame::OnDragMoveByDefault(wxCommandEvent& event)
1110 {
1111     m_moveByDefault = event.IsChecked();
1112 }
1113 
OnDragMoveAllow(wxCommandEvent & event)1114 void DnDFrame::OnDragMoveAllow(wxCommandEvent& event)
1115 {
1116     m_moveAllow = event.IsChecked();
1117 }
1118 
1119 
OnOpenFile(wxCommandEvent & WXUNUSED (event))1120 void DnDFrame::OnOpenFile(wxCommandEvent& WXUNUSED(event))
1121 {
1122     wxFileDialog dialog(this, _T("Open a file"), wxEmptyString, wxEmptyString, _T("Files (*.*)|*.*"), wxFD_MULTIPLE);
1123     if (dialog.ShowModal() == wxID_OK)
1124     {
1125         wxString str;
1126         str.Printf( _T("File opened: %s"), dialog.GetPath().c_str() );
1127         m_ctrlFile->Append( str );
1128     }
1129 }
1130 
OnAbout(wxCommandEvent & WXUNUSED (event))1131 void DnDFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
1132 {
1133     wxMessageBox(_T("Drag-&-Drop Demo\n")
1134                  _T("Please see \"Help|Help...\" for details\n")
1135                  _T("Copyright (c) 1998 Vadim Zeitlin"),
1136                  _T("About wxDnD"),
1137                  wxICON_INFORMATION | wxOK,
1138                  this);
1139 }
1140 
OnHelp(wxCommandEvent &)1141 void DnDFrame::OnHelp(wxCommandEvent& /* event */)
1142 {
1143     wxMessageDialog dialog(this,
1144                            _T("This small program demonstrates drag & drop support in wxWidgets. The program window\n")
1145                            _T("consists of 3 parts: the bottom pane is for debug messages, so that you can see what's\n")
1146                            _T("going on inside. The top part is split into 2 listboxes, the left one accepts files\n")
1147                            _T("and the right one accepts text.\n")
1148                            _T("\n")
1149                            _T("To test wxDropTarget: open wordpad (write.exe), select some text in it and drag it to\n")
1150                            _T("the right listbox (you'll notice the usual visual feedback, i.e. the cursor will change).\n")
1151                            _T("Also, try dragging some files (you can select several at once) from Windows Explorer (or \n")
1152                            _T("File Manager) to the left pane. Hold down Ctrl/Shift keys when you drop text (doesn't \n")
1153                            _T("work with files) and see what changes.\n")
1154                            _T("\n")
1155                            _T("To test wxDropSource: just press any mouse button on the empty zone of the window and drag\n")
1156                            _T("it to wordpad or any other droptarget accepting text (and of course you can just drag it\n")
1157                            _T("to the right pane). Due to a lot of trace messages, the cursor might take some time to \n")
1158                            _T("change, don't release the mouse button until it does. You can change the string being\n")
1159                            _T("dragged in in \"File|Test drag...\" dialog.\n")
1160                            _T("\n")
1161                            _T("\n")
1162                            _T("Please send all questions/bug reports/suggestions &c to \n")
1163                            _T("Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>"),
1164                            _T("wxDnD Help"));
1165 
1166     dialog.ShowModal();
1167 }
1168 
1169 #if wxUSE_LOG
OnLogClear(wxCommandEvent &)1170 void DnDFrame::OnLogClear(wxCommandEvent& /* event */ )
1171 {
1172     m_ctrlLog->Clear();
1173     m_ctrlText->Clear();
1174     m_ctrlFile->Clear();
1175 }
1176 #endif // wxUSE_LOG
1177 
OnLeftDown(wxMouseEvent & WXUNUSED (event))1178 void DnDFrame::OnLeftDown(wxMouseEvent &WXUNUSED(event) )
1179 {
1180 #if wxUSE_DRAG_AND_DROP
1181     if ( !m_strText.empty() )
1182     {
1183         // start drag operation
1184         wxTextDataObject textData(m_strText);
1185         wxDropSource source(textData, this,
1186                             wxDROP_ICON(dnd_copy),
1187                             wxDROP_ICON(dnd_move),
1188                             wxDROP_ICON(dnd_none));
1189 
1190         int flags = 0;
1191         if ( m_moveByDefault )
1192             flags |= wxDrag_DefaultMove;
1193         else if ( m_moveAllow )
1194             flags |= wxDrag_AllowMove;
1195 
1196         wxDragResult result = source.DoDragDrop(flags);
1197 
1198 #if wxUSE_STATUSBAR
1199         const wxChar *pc;
1200         switch ( result )
1201         {
1202             case wxDragError:   pc = _T("Error!");    break;
1203             case wxDragNone:    pc = _T("Nothing");   break;
1204             case wxDragCopy:    pc = _T("Copied");    break;
1205             case wxDragMove:    pc = _T("Moved");     break;
1206             case wxDragCancel:  pc = _T("Cancelled"); break;
1207             default:            pc = _T("Huh?");      break;
1208         }
1209 
1210         SetStatusText(wxString(_T("Drag result: ")) + pc);
1211 #else
1212         wxUnusedVar(result);
1213 #endif // wxUSE_STATUSBAR
1214     }
1215 #endif // wxUSE_DRAG_AND_DROP
1216 }
1217 
OnRightDown(wxMouseEvent & event)1218 void DnDFrame::OnRightDown(wxMouseEvent &event )
1219 {
1220     wxMenu menu(_T("Dnd sample menu"));
1221 
1222     menu.Append(Menu_Drag, _T("&Test drag..."));
1223     menu.AppendSeparator();
1224     menu.Append(Menu_About, _T("&About"));
1225 
1226     PopupMenu( &menu, event.GetX(), event.GetY() );
1227 }
1228 
~DnDFrame()1229 DnDFrame::~DnDFrame()
1230 {
1231 #if wxUSE_LOG
1232     if ( m_pLog != NULL ) {
1233         if ( wxLog::SetActiveTarget(m_pLogPrev) == m_pLog )
1234             delete m_pLog;
1235     }
1236 #endif // wxUSE_LOG
1237 }
1238 
1239 // ---------------------------------------------------------------------------
1240 // bitmap clipboard
1241 // ---------------------------------------------------------------------------
1242 
OnCopyBitmap(wxCommandEvent & WXUNUSED (event))1243 void DnDFrame::OnCopyBitmap(wxCommandEvent& WXUNUSED(event))
1244 {
1245     // PNG support is not always compiled in under Windows, so use BMP there
1246 #if wxUSE_LIBPNG
1247     wxFileDialog dialog(this, _T("Open a PNG file"), wxEmptyString, wxEmptyString, _T("PNG files (*.png)|*.png"), 0);
1248 #else
1249     wxFileDialog dialog(this, _T("Open a BMP file"), wxEmptyString, wxEmptyString, _T("BMP files (*.bmp)|*.bmp"), 0);
1250 #endif
1251 
1252     if (dialog.ShowModal() != wxID_OK)
1253     {
1254         wxLogMessage( _T("Aborted file open") );
1255         return;
1256     }
1257 
1258     if (dialog.GetPath().empty())
1259     {
1260         wxLogMessage( _T("Returned empty string.") );
1261         return;
1262     }
1263 
1264     if (!wxFileExists(dialog.GetPath()))
1265     {
1266         wxLogMessage( _T("File doesn't exist.") );
1267         return;
1268     }
1269 
1270     wxImage image;
1271     image.LoadFile( dialog.GetPath(),
1272 #if wxUSE_LIBPNG
1273                     wxBITMAP_TYPE_PNG
1274 #else
1275                     wxBITMAP_TYPE_BMP
1276 #endif
1277                   );
1278     if (!image.Ok())
1279     {
1280         wxLogError( _T("Invalid image file...") );
1281         return;
1282     }
1283 
1284     wxLogStatus( _T("Decoding image file...") );
1285     wxYield();
1286 
1287     wxBitmap bitmap( image );
1288 
1289     if ( !wxTheClipboard->Open() )
1290     {
1291         wxLogError(_T("Can't open clipboard."));
1292 
1293         return;
1294     }
1295 
1296     wxLogMessage( _T("Creating wxBitmapDataObject...") );
1297     wxYield();
1298 
1299     if ( !wxTheClipboard->AddData(new wxBitmapDataObject(bitmap)) )
1300     {
1301         wxLogError(_T("Can't copy image to the clipboard."));
1302     }
1303     else
1304     {
1305         wxLogMessage(_T("Image has been put on the clipboard.") );
1306         wxLogMessage(_T("You can paste it now and look at it.") );
1307     }
1308 
1309     wxTheClipboard->Close();
1310 }
1311 
OnPasteBitmap(wxCommandEvent & WXUNUSED (event))1312 void DnDFrame::OnPasteBitmap(wxCommandEvent& WXUNUSED(event))
1313 {
1314     if ( !wxTheClipboard->Open() )
1315     {
1316         wxLogError(_T("Can't open clipboard."));
1317 
1318         return;
1319     }
1320 
1321     if ( !wxTheClipboard->IsSupported(wxDF_BITMAP) )
1322     {
1323         wxLogWarning(_T("No bitmap on clipboard"));
1324 
1325         wxTheClipboard->Close();
1326         return;
1327     }
1328 
1329     wxBitmapDataObject data;
1330     if ( !wxTheClipboard->GetData(data) )
1331     {
1332         wxLogError(_T("Can't paste bitmap from the clipboard"));
1333     }
1334     else
1335     {
1336         const wxBitmap& bmp = data.GetBitmap();
1337 
1338         wxLogMessage(_T("Bitmap %dx%d pasted from the clipboard"),
1339                      bmp.GetWidth(), bmp.GetHeight());
1340         ShowBitmap(bmp);
1341     }
1342 
1343     wxTheClipboard->Close();
1344 }
1345 
1346 #if wxUSE_METAFILE
1347 
OnPasteMetafile(wxCommandEvent & WXUNUSED (event))1348 void DnDFrame::OnPasteMetafile(wxCommandEvent& WXUNUSED(event))
1349 {
1350     if ( !wxTheClipboard->Open() )
1351     {
1352         wxLogError(_T("Can't open clipboard."));
1353 
1354         return;
1355     }
1356 
1357     if ( !wxTheClipboard->IsSupported(wxDF_METAFILE) )
1358     {
1359         wxLogWarning(_T("No metafile on clipboard"));
1360     }
1361     else
1362     {
1363         wxMetaFileDataObject data;
1364         if ( !wxTheClipboard->GetData(data) )
1365         {
1366             wxLogError(_T("Can't paste metafile from the clipboard"));
1367         }
1368         else
1369         {
1370             const wxMetaFile& mf = data.GetMetafile();
1371 
1372             wxLogMessage(_T("Metafile %dx%d pasted from the clipboard"),
1373                          mf.GetWidth(), mf.GetHeight());
1374 
1375             ShowMetaFile(mf);
1376         }
1377     }
1378 
1379     wxTheClipboard->Close();
1380 }
1381 
1382 #endif // wxUSE_METAFILE
1383 
1384 // ----------------------------------------------------------------------------
1385 // file clipboard
1386 // ----------------------------------------------------------------------------
1387 
OnCopyFiles(wxCommandEvent & WXUNUSED (event))1388 void DnDFrame::OnCopyFiles(wxCommandEvent& WXUNUSED(event))
1389 {
1390 #ifdef __WXMSW__
1391     wxFileDialog dialog(this, _T("Select a file to copy"), wxEmptyString, wxEmptyString,
1392                          _T("All files (*.*)|*.*"), 0);
1393 
1394     wxArrayString filenames;
1395     while ( dialog.ShowModal() == wxID_OK )
1396     {
1397         filenames.Add(dialog.GetPath());
1398     }
1399 
1400     if ( !filenames.IsEmpty() )
1401     {
1402         wxFileDataObject *dobj = new wxFileDataObject;
1403         size_t count = filenames.GetCount();
1404         for ( size_t n = 0; n < count; n++ )
1405         {
1406             dobj->AddFile(filenames[n]);
1407         }
1408 
1409         wxClipboardLocker locker;
1410         if ( !locker )
1411         {
1412             wxLogError(wxT("Can't open clipboard"));
1413         }
1414         else
1415         {
1416             if ( !wxTheClipboard->AddData(dobj) )
1417             {
1418                 wxLogError(wxT("Can't copy file(s) to the clipboard"));
1419             }
1420             else
1421             {
1422                 wxLogStatus(this, wxT("%d file%s copied to the clipboard"),
1423                             count, count == 1 ? wxEmptyString : wxEmptyString);
1424             }
1425         }
1426     }
1427     else
1428     {
1429         wxLogStatus(this, wxT("Aborted"));
1430     }
1431 #else // !MSW
1432     wxLogError(wxT("Sorry, not implemented"));
1433 #endif // MSW/!MSW
1434 }
1435 
1436 // ---------------------------------------------------------------------------
1437 // text clipboard
1438 // ---------------------------------------------------------------------------
1439 
OnCopy(wxCommandEvent & WXUNUSED (event))1440 void DnDFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
1441 {
1442     if ( !wxTheClipboard->Open() )
1443     {
1444         wxLogError(_T("Can't open clipboard."));
1445 
1446         return;
1447     }
1448 
1449     if ( !wxTheClipboard->AddData(new wxTextDataObject(m_strText)) )
1450     {
1451         wxLogError(_T("Can't copy data to the clipboard"));
1452     }
1453     else
1454     {
1455         wxLogMessage(_T("Text '%s' put on the clipboard"), m_strText.c_str());
1456     }
1457 
1458     wxTheClipboard->Close();
1459 }
1460 
OnPaste(wxCommandEvent & WXUNUSED (event))1461 void DnDFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
1462 {
1463     if ( !wxTheClipboard->Open() )
1464     {
1465         wxLogError(_T("Can't open clipboard."));
1466 
1467         return;
1468     }
1469 
1470     if ( !wxTheClipboard->IsSupported(wxDF_TEXT) )
1471     {
1472         wxLogWarning(_T("No text data on clipboard"));
1473 
1474         wxTheClipboard->Close();
1475         return;
1476     }
1477 
1478     wxTextDataObject text;
1479     if ( !wxTheClipboard->GetData(text) )
1480     {
1481         wxLogError(_T("Can't paste data from the clipboard"));
1482     }
1483     else
1484     {
1485         wxLogMessage(_T("Text '%s' pasted from the clipboard"),
1486                      text.GetText().c_str());
1487     }
1488 
1489     wxTheClipboard->Close();
1490 }
1491 
1492 #if wxUSE_DRAG_AND_DROP
1493 
1494 // ----------------------------------------------------------------------------
1495 // Notifications called by the base class
1496 // ----------------------------------------------------------------------------
1497 
OnDropText(wxCoord,wxCoord,const wxString & text)1498 bool DnDText::OnDropText(wxCoord, wxCoord, const wxString& text)
1499 {
1500     m_pOwner->Append(text);
1501 
1502     return true;
1503 }
1504 
OnDropFiles(wxCoord,wxCoord,const wxArrayString & filenames)1505 bool DnDFile::OnDropFiles(wxCoord, wxCoord, const wxArrayString& filenames)
1506 {
1507     size_t nFiles = filenames.GetCount();
1508     wxString str;
1509     str.Printf( _T("%d files dropped"), (int)nFiles);
1510     m_pOwner->Append(str);
1511     for ( size_t n = 0; n < nFiles; n++ )
1512     {
1513         m_pOwner->Append(filenames[n]);
1514         if (wxFile::Exists(filenames[n]))
1515             m_pOwner->Append(wxT("  This file exists.") );
1516         else
1517             m_pOwner->Append(wxT("  This file doesn't exist.") );
1518 
1519     }
1520 
1521     return true;
1522 }
1523 
1524 // ----------------------------------------------------------------------------
1525 // DnDShapeDialog
1526 // ----------------------------------------------------------------------------
1527 
DnDShapeDialog(wxFrame * parent,DnDShape * shape)1528 DnDShapeDialog::DnDShapeDialog(wxFrame *parent, DnDShape *shape)
1529   :wxDialog( parent, 6001, wxT("Choose Shape"), wxPoint( 10, 10 ),
1530              wxSize( 40, 40 ),
1531              wxDEFAULT_DIALOG_STYLE | wxRAISED_BORDER | wxRESIZE_BORDER )
1532 {
1533     m_shape = shape;
1534     wxBoxSizer* topSizer = new wxBoxSizer( wxVERTICAL );
1535 
1536     // radio box
1537     wxBoxSizer* shapesSizer = new wxBoxSizer( wxHORIZONTAL );
1538     const wxString choices[] = { wxT("None"), wxT("Triangle"),
1539                                  wxT("Rectangle"), wxT("Ellipse") };
1540 
1541     m_radio = new wxRadioBox( this, wxID_ANY, wxT("&Shape"),
1542                               wxDefaultPosition, wxDefaultSize, 4, choices, 4,
1543                               wxRA_SPECIFY_COLS );
1544     shapesSizer->Add( m_radio, 0, wxGROW|wxALL, 5 );
1545     topSizer->Add( shapesSizer, 0, wxALL, 2 );
1546 
1547     // attributes
1548     wxStaticBox* box = new wxStaticBox( this, wxID_ANY, wxT("&Attributes") );
1549     wxStaticBoxSizer* attrSizer = new wxStaticBoxSizer( box, wxHORIZONTAL );
1550     wxFlexGridSizer* xywhSizer = new wxFlexGridSizer( 4, 2 );
1551 
1552     wxStaticText* st;
1553 
1554     st = new wxStaticText( this, wxID_ANY, wxT("Position &X:") );
1555     m_textX = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
1556                               wxSize( 30, 20 ) );
1557     xywhSizer->Add( st, 1, wxGROW|wxALL, 2 );
1558     xywhSizer->Add( m_textX, 1, wxGROW|wxALL, 2 );
1559 
1560     st = new wxStaticText( this, wxID_ANY, wxT("Size &width:") );
1561     m_textW = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
1562                               wxSize( 30, 20 ) );
1563     xywhSizer->Add( st, 1, wxGROW|wxALL, 2 );
1564     xywhSizer->Add( m_textW, 1, wxGROW|wxALL, 2 );
1565 
1566     st = new wxStaticText( this, wxID_ANY, wxT("&Y:") );
1567     m_textY = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
1568                               wxSize( 30, 20 ) );
1569     xywhSizer->Add( st, 1, wxALL|wxALIGN_RIGHT, 2 );
1570     xywhSizer->Add( m_textY, 1, wxGROW|wxALL, 2 );
1571 
1572     st = new wxStaticText( this, wxID_ANY, wxT("&height:") );
1573     m_textH = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
1574                               wxSize( 30, 20 ) );
1575     xywhSizer->Add( st, 1, wxALL|wxALIGN_RIGHT, 2 );
1576     xywhSizer->Add( m_textH, 1, wxGROW|wxALL, 2 );
1577 
1578     wxButton* col = new wxButton( this, Button_Colour, wxT("&Colour...") );
1579     attrSizer->Add( xywhSizer, 1, wxGROW );
1580     attrSizer->Add( col, 0, wxALL|wxALIGN_CENTRE_VERTICAL, 2 );
1581     topSizer->Add( attrSizer, 0, wxGROW|wxALL, 5 );
1582 
1583     // buttons
1584     wxBoxSizer* buttonSizer = new wxBoxSizer( wxHORIZONTAL );
1585     wxButton* bt;
1586     bt = new wxButton( this, wxID_OK, wxT("Ok") );
1587     buttonSizer->Add( bt, 0, wxALL, 2 );
1588     bt = new wxButton( this, wxID_CANCEL, wxT("Cancel") );
1589     buttonSizer->Add( bt, 0, wxALL, 2 );
1590     topSizer->Add( buttonSizer, 0, wxALL|wxALIGN_RIGHT, 2 );
1591 
1592     SetSizer( topSizer );
1593     topSizer->Fit( this );
1594 }
1595 
GetShape() const1596 DnDShape *DnDShapeDialog::GetShape() const
1597 {
1598     switch ( m_shapeKind )
1599     {
1600         default:
1601         case DnDShape::None:      return NULL;
1602         case DnDShape::Triangle:  return new DnDTriangularShape(m_pos, m_size, m_col);
1603         case DnDShape::Rectangle: return new DnDRectangularShape(m_pos, m_size, m_col);
1604         case DnDShape::Ellipse:   return new DnDEllipticShape(m_pos, m_size, m_col);
1605     }
1606 }
1607 
TransferDataToWindow()1608 bool DnDShapeDialog::TransferDataToWindow()
1609 {
1610 
1611     if ( m_shape )
1612     {
1613         m_radio->SetSelection(m_shape->GetKind());
1614         m_pos = m_shape->GetPosition();
1615         m_size = m_shape->GetSize();
1616         m_col = m_shape->GetColour();
1617     }
1618     else
1619     {
1620         m_radio->SetSelection(DnDShape::None);
1621         m_pos = wxPoint(1, 1);
1622         m_size = wxSize(100, 100);
1623     }
1624 
1625     m_textX->SetValue(wxString() << m_pos.x);
1626     m_textY->SetValue(wxString() << m_pos.y);
1627     m_textW->SetValue(wxString() << m_size.x);
1628     m_textH->SetValue(wxString() << m_size.y);
1629 
1630     return true;
1631 }
1632 
TransferDataFromWindow()1633 bool DnDShapeDialog::TransferDataFromWindow()
1634 {
1635     m_shapeKind = (DnDShape::Kind)m_radio->GetSelection();
1636 
1637     m_pos.x = wxAtoi(m_textX->GetValue());
1638     m_pos.y = wxAtoi(m_textY->GetValue());
1639     m_size.x = wxAtoi(m_textW->GetValue());
1640     m_size.y = wxAtoi(m_textH->GetValue());
1641 
1642     if ( !m_pos.x || !m_pos.y || !m_size.x || !m_size.y )
1643     {
1644         wxMessageBox(_T("All sizes and positions should be non null!"),
1645                      _T("Invalid shape"), wxICON_HAND | wxOK, this);
1646 
1647         return false;
1648     }
1649 
1650     return true;
1651 }
1652 
OnColour(wxCommandEvent & WXUNUSED (event))1653 void DnDShapeDialog::OnColour(wxCommandEvent& WXUNUSED(event))
1654 {
1655     wxColourData data;
1656     data.SetChooseFull(true);
1657     for (int i = 0; i < 16; i++)
1658     {
1659         wxColour colour((unsigned char)(i*16), (unsigned char)(i*16), (unsigned char)(i*16));
1660         data.SetCustomColour(i, colour);
1661     }
1662 
1663     wxColourDialog dialog(this, &data);
1664     if ( dialog.ShowModal() == wxID_OK )
1665     {
1666         m_col = dialog.GetColourData().GetColour();
1667     }
1668 }
1669 
1670 // ----------------------------------------------------------------------------
1671 // DnDShapeFrame
1672 // ----------------------------------------------------------------------------
1673 
1674 DnDShapeFrame *DnDShapeFrame::ms_lastDropTarget = NULL;
1675 
DnDShapeFrame(wxFrame * parent)1676 DnDShapeFrame::DnDShapeFrame(wxFrame *parent)
1677              : wxFrame(parent, wxID_ANY, _T("Shape Frame"))
1678 {
1679 #if wxUSE_STATUSBAR
1680     CreateStatusBar();
1681 #endif // wxUSE_STATUSBAR
1682 
1683     wxMenu *menuShape = new wxMenu;
1684     menuShape->Append(Menu_Shape_New, _T("&New default shape\tCtrl-S"));
1685     menuShape->Append(Menu_Shape_Edit, _T("&Edit shape\tCtrl-E"));
1686     menuShape->AppendSeparator();
1687     menuShape->Append(Menu_Shape_Clear, _T("&Clear shape\tCtrl-L"));
1688 
1689     wxMenu *menuClipboard = new wxMenu;
1690     menuClipboard->Append(Menu_ShapeClipboard_Copy, _T("&Copy\tCtrl-C"));
1691     menuClipboard->Append(Menu_ShapeClipboard_Paste, _T("&Paste\tCtrl-V"));
1692 
1693     wxMenuBar *menubar = new wxMenuBar;
1694     menubar->Append(menuShape, _T("&Shape"));
1695     menubar->Append(menuClipboard, _T("&Clipboard"));
1696 
1697     SetMenuBar(menubar);
1698 
1699 #if wxUSE_STATUSBAR
1700     SetStatusText(_T("Press Ctrl-S to create a new shape"));
1701 #endif // wxUSE_STATUSBAR
1702 
1703     SetDropTarget(new DnDShapeDropTarget(this));
1704 
1705     m_shape = NULL;
1706 
1707     SetBackgroundColour(*wxWHITE);
1708 }
1709 
~DnDShapeFrame()1710 DnDShapeFrame::~DnDShapeFrame()
1711 {
1712     if (m_shape)
1713         delete m_shape;
1714 }
1715 
SetShape(DnDShape * shape)1716 void DnDShapeFrame::SetShape(DnDShape *shape)
1717 {
1718     if (m_shape)
1719         delete m_shape;
1720     m_shape = shape;
1721     Refresh();
1722 }
1723 
1724 // callbacks
OnDrag(wxMouseEvent & event)1725 void DnDShapeFrame::OnDrag(wxMouseEvent& event)
1726 {
1727     if ( !m_shape )
1728     {
1729         event.Skip();
1730 
1731         return;
1732     }
1733 
1734     // start drag operation
1735     DnDShapeDataObject shapeData(m_shape);
1736     wxDropSource source(shapeData, this);
1737 
1738     const wxChar *pc = NULL;
1739     switch ( source.DoDragDrop(true) )
1740     {
1741         default:
1742         case wxDragError:
1743             wxLogError(wxT("An error occurred during drag and drop operation"));
1744             break;
1745 
1746         case wxDragNone:
1747 #if wxUSE_STATUSBAR
1748             SetStatusText(_T("Nothing happened"));
1749 #endif // wxUSE_STATUSBAR
1750             break;
1751 
1752         case wxDragCopy:
1753             pc = _T("copied");
1754             break;
1755 
1756         case wxDragMove:
1757             pc = _T("moved");
1758             if ( ms_lastDropTarget != this )
1759             {
1760                 // don't delete the shape if we dropped it on ourselves!
1761                 SetShape(NULL);
1762             }
1763             break;
1764 
1765         case wxDragCancel:
1766 #if wxUSE_STATUSBAR
1767             SetStatusText(_T("Drag and drop operation cancelled"));
1768 #endif // wxUSE_STATUSBAR
1769             break;
1770     }
1771 
1772     if ( pc )
1773     {
1774 #if wxUSE_STATUSBAR
1775         SetStatusText(wxString(_T("Shape successfully ")) + pc);
1776 #endif // wxUSE_STATUSBAR
1777     }
1778     //else: status text already set
1779 }
1780 
OnDrop(wxCoord x,wxCoord y,DnDShape * shape)1781 void DnDShapeFrame::OnDrop(wxCoord x, wxCoord y, DnDShape *shape)
1782 {
1783     ms_lastDropTarget = this;
1784 
1785     wxPoint pt(x, y);
1786 
1787 #if wxUSE_STATUSBAR
1788     wxString s;
1789     s.Printf(wxT("Shape dropped at (%d, %d)"), pt.x, pt.y);
1790     SetStatusText(s);
1791 #endif // wxUSE_STATUSBAR
1792 
1793     shape->Move(pt);
1794     SetShape(shape);
1795 }
1796 
OnEditShape(wxCommandEvent & WXUNUSED (event))1797 void DnDShapeFrame::OnEditShape(wxCommandEvent& WXUNUSED(event))
1798 {
1799     DnDShapeDialog dlg(this, m_shape);
1800     if ( dlg.ShowModal() == wxID_OK )
1801     {
1802         SetShape(dlg.GetShape());
1803 
1804 #if wxUSE_STATUSBAR
1805         if ( m_shape )
1806         {
1807             SetStatusText(_T("You can now drag the shape to another frame"));
1808         }
1809 #endif // wxUSE_STATUSBAR
1810     }
1811 }
1812 
OnNewShape(wxCommandEvent & WXUNUSED (event))1813 void DnDShapeFrame::OnNewShape(wxCommandEvent& WXUNUSED(event))
1814 {
1815     SetShape(new DnDEllipticShape(wxPoint(10, 10), wxSize(80, 60), *wxRED));
1816 
1817 #if wxUSE_STATUSBAR
1818     SetStatusText(_T("You can now drag the shape to another frame"));
1819 #endif // wxUSE_STATUSBAR
1820 }
1821 
OnClearShape(wxCommandEvent & WXUNUSED (event))1822 void DnDShapeFrame::OnClearShape(wxCommandEvent& WXUNUSED(event))
1823 {
1824     SetShape(NULL);
1825 }
1826 
OnCopyShape(wxCommandEvent & WXUNUSED (event))1827 void DnDShapeFrame::OnCopyShape(wxCommandEvent& WXUNUSED(event))
1828 {
1829     if ( m_shape )
1830     {
1831         wxClipboardLocker clipLocker;
1832         if ( !clipLocker )
1833         {
1834             wxLogError(wxT("Can't open the clipboard"));
1835 
1836             return;
1837         }
1838 
1839         wxTheClipboard->AddData(new DnDShapeDataObject(m_shape));
1840     }
1841 }
1842 
OnPasteShape(wxCommandEvent & WXUNUSED (event))1843 void DnDShapeFrame::OnPasteShape(wxCommandEvent& WXUNUSED(event))
1844 {
1845     wxClipboardLocker clipLocker;
1846     if ( !clipLocker )
1847     {
1848         wxLogError(wxT("Can't open the clipboard"));
1849 
1850         return;
1851     }
1852 
1853     DnDShapeDataObject shapeDataObject(NULL);
1854     if ( wxTheClipboard->GetData(shapeDataObject) )
1855     {
1856         SetShape(shapeDataObject.GetShape());
1857     }
1858     else
1859     {
1860         wxLogStatus(wxT("No shape on the clipboard"));
1861     }
1862 }
1863 
OnUpdateUICopy(wxUpdateUIEvent & event)1864 void DnDShapeFrame::OnUpdateUICopy(wxUpdateUIEvent& event)
1865 {
1866     event.Enable( m_shape != NULL );
1867 }
1868 
OnUpdateUIPaste(wxUpdateUIEvent & event)1869 void DnDShapeFrame::OnUpdateUIPaste(wxUpdateUIEvent& event)
1870 {
1871     event.Enable( wxTheClipboard->IsSupported(wxDataFormat(shapeFormatId)) );
1872 }
1873 
OnPaint(wxPaintEvent & event)1874 void DnDShapeFrame::OnPaint(wxPaintEvent& event)
1875 {
1876     if ( m_shape )
1877     {
1878         wxPaintDC dc(this);
1879 
1880         m_shape->Draw(dc);
1881     }
1882     else
1883     {
1884         event.Skip();
1885     }
1886 }
1887 
1888 // ----------------------------------------------------------------------------
1889 // DnDShape
1890 // ----------------------------------------------------------------------------
1891 
New(const void * buf)1892 DnDShape *DnDShape::New(const void *buf)
1893 {
1894     const ShapeDump& dump = *(const ShapeDump *)buf;
1895     switch ( dump.k )
1896     {
1897         case Triangle:
1898             return new DnDTriangularShape(wxPoint(dump.x, dump.y),
1899                                           wxSize(dump.w, dump.h),
1900                                           wxColour(dump.r, dump.g, dump.b));
1901 
1902         case Rectangle:
1903             return new DnDRectangularShape(wxPoint(dump.x, dump.y),
1904                                            wxSize(dump.w, dump.h),
1905                                            wxColour(dump.r, dump.g, dump.b));
1906 
1907         case Ellipse:
1908             return new DnDEllipticShape(wxPoint(dump.x, dump.y),
1909                                         wxSize(dump.w, dump.h),
1910                                         wxColour(dump.r, dump.g, dump.b));
1911 
1912         default:
1913             wxFAIL_MSG(wxT("invalid shape!"));
1914             return NULL;
1915     }
1916 }
1917 
1918 // ----------------------------------------------------------------------------
1919 // DnDShapeDataObject
1920 // ----------------------------------------------------------------------------
1921 
1922 #if wxUSE_METAFILE
1923 
CreateMetaFile() const1924 void DnDShapeDataObject::CreateMetaFile() const
1925 {
1926     wxPoint pos = m_shape->GetPosition();
1927     wxSize size = m_shape->GetSize();
1928 
1929     wxMetaFileDC dcMF(wxEmptyString, pos.x + size.x, pos.y + size.y);
1930 
1931     m_shape->Draw(dcMF);
1932 
1933     wxMetafile *mf = dcMF.Close();
1934 
1935     DnDShapeDataObject *self = (DnDShapeDataObject *)this; // const_cast
1936     self->m_dobjMetaFile.SetMetafile(*mf);
1937     self->m_hasMetaFile = true;
1938 
1939     delete mf;
1940 }
1941 
1942 #endif // wxUSE_METAFILE
1943 
CreateBitmap() const1944 void DnDShapeDataObject::CreateBitmap() const
1945 {
1946     wxPoint pos = m_shape->GetPosition();
1947     wxSize size = m_shape->GetSize();
1948     int x = pos.x + size.x,
1949         y = pos.y + size.y;
1950     wxBitmap bitmap(x, y);
1951     wxMemoryDC dc;
1952     dc.SelectObject(bitmap);
1953     dc.SetBrush(wxBrush(wxT("white"), wxSOLID));
1954     dc.Clear();
1955     m_shape->Draw(dc);
1956     dc.SelectObject(wxNullBitmap);
1957 
1958     DnDShapeDataObject *self = (DnDShapeDataObject *)this; // const_cast
1959     self->m_dobjBitmap.SetBitmap(bitmap);
1960     self->m_hasBitmap = true;
1961 }
1962 
1963 #endif // wxUSE_DRAG_AND_DROP
1964 
1965 // ----------------------------------------------------------------------------
1966 // global functions
1967 // ----------------------------------------------------------------------------
1968 
ShowBitmap(const wxBitmap & bitmap)1969 static void ShowBitmap(const wxBitmap& bitmap)
1970 {
1971     wxFrame *frame = new wxFrame(NULL, wxID_ANY, _T("Bitmap view"));
1972 #if wxUSE_STATUSBAR
1973     frame->CreateStatusBar();
1974 #endif // wxUSE_STATUSBAR
1975     DnDCanvasBitmap *canvas = new DnDCanvasBitmap(frame);
1976     canvas->SetBitmap(bitmap);
1977 
1978     int w = bitmap.GetWidth(),
1979         h = bitmap.GetHeight();
1980 #if wxUSE_STATUSBAR
1981     frame->SetStatusText(wxString::Format(_T("%dx%d"), w, h));
1982 #endif // wxUSE_STATUSBAR
1983 
1984     frame->SetClientSize(w > 100 ? 100 : w, h > 100 ? 100 : h);
1985     frame->Show(true);
1986 }
1987 
1988 #if wxUSE_METAFILE
1989 
ShowMetaFile(const wxMetaFile & metafile)1990 static void ShowMetaFile(const wxMetaFile& metafile)
1991 {
1992     wxFrame *frame = new wxFrame(NULL, wxID_ANY, _T("Metafile view"));
1993     frame->CreateStatusBar();
1994     DnDCanvasMetafile *canvas = new DnDCanvasMetafile(frame);
1995     canvas->SetMetafile(metafile);
1996 
1997     wxSize size = metafile.GetSize();
1998     frame->SetStatusText(wxString::Format(_T("%dx%d"), size.x, size.y));
1999 
2000     frame->SetClientSize(size.x > 100 ? 100 : size.x,
2001                          size.y > 100 ? 100 : size.y);
2002     frame->Show();
2003 }
2004 
2005 #endif // wxUSE_METAFILE
2006 
2007 #endif // wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
2008