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