]> git.saurik.com Git - wxWidgets.git/blob - samples/dnd/dnd.cpp
added a menu command to switch between clipboard and primary selection
[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/dataobj.h"
24 #include "wx/image.h"
25 #include "wx/clipbrd.h"
26 #include "wx/colordlg.h"
27 #include "wx/metafile.h"
28
29 #if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
30 #include "../sample.xpm"
31 #if wxUSE_DRAG_AND_DROP
32 #include "dnd_copy.xpm"
33 #include "dnd_move.xpm"
34 #include "dnd_none.xpm"
35 #endif
36 #endif
37
38 #if wxUSE_DRAG_AND_DROP
39
40 // ----------------------------------------------------------------------------
41 // Derive two simple classes which just put in the listbox the strings (text or
42 // file names) we drop on them
43 // ----------------------------------------------------------------------------
44
45 class DnDText : public wxTextDropTarget
46 {
47 public:
48 DnDText(wxListBox *pOwner) { m_pOwner = pOwner; }
49
50 virtual bool OnDropText(wxCoord x, wxCoord y, const wxString& text);
51
52 private:
53 wxListBox *m_pOwner;
54 };
55
56 class DnDFile : public wxFileDropTarget
57 {
58 public:
59 DnDFile(wxListBox *pOwner) { m_pOwner = pOwner; }
60
61 virtual bool OnDropFiles(wxCoord x, wxCoord y,
62 const wxArrayString& filenames);
63
64 private:
65 wxListBox *m_pOwner;
66 };
67
68 // ----------------------------------------------------------------------------
69 // Define a custom dtop target accepting URLs
70 // ----------------------------------------------------------------------------
71
72 class URLDropTarget : public wxDropTarget
73 {
74 public:
75 URLDropTarget() { SetDataObject(new wxURLDataObject); }
76
77 void OnDropURL(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y), const wxString& text)
78 {
79 // of course, a real program would do something more useful here...
80 wxMessageBox(text, _T("wxDnD sample: got URL"),
81 wxICON_INFORMATION | wxOK);
82 }
83
84 // URLs can't be moved, only copied
85 virtual wxDragResult OnDragOver(wxCoord WXUNUSED(x), wxCoord WXUNUSED(y),
86 wxDragResult WXUNUSED(def))
87 {
88 return wxDragLink; // At least IE 5.x needs wxDragLink, the
89 // other browsers on MSW seem okay with it too.
90 }
91
92 // translate this to calls to OnDropURL() just for convenience
93 virtual wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def)
94 {
95 if ( !GetData() )
96 return wxDragNone;
97
98 OnDropURL(x, y, ((wxURLDataObject *)m_dataObject)->GetURL());
99
100 return def;
101 }
102 };
103
104 #endif // wxUSE_DRAG_AND_DROP
105
106 // ----------------------------------------------------------------------------
107 // Define a new application type
108 // ----------------------------------------------------------------------------
109
110 class DnDApp : public wxApp
111 {
112 public:
113 virtual bool OnInit();
114 };
115
116 IMPLEMENT_APP(DnDApp)
117
118 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
119
120 // ----------------------------------------------------------------------------
121 // Define canvas class to show a bitmap
122 // ----------------------------------------------------------------------------
123
124 class DnDCanvasBitmap : public wxScrolledWindow
125 {
126 public:
127 DnDCanvasBitmap(wxWindow *parent) : wxScrolledWindow(parent) { }
128
129 void SetBitmap(const wxBitmap& bitmap)
130 {
131 m_bitmap = bitmap;
132
133 SetScrollbars(10, 10,
134 m_bitmap.GetWidth() / 10, m_bitmap.GetHeight() / 10);
135
136 Refresh();
137 }
138
139 void OnPaint(wxPaintEvent& WXUNUSED(event))
140 {
141 wxPaintDC dc(this);
142
143 if ( m_bitmap.Ok() )
144 {
145 PrepareDC(dc);
146
147 dc.DrawBitmap(m_bitmap, 0, 0);
148 }
149 }
150
151 private:
152 wxBitmap m_bitmap;
153
154 DECLARE_EVENT_TABLE()
155 };
156
157 #if wxUSE_METAFILE
158
159 // and the same thing fo metafiles
160 class DnDCanvasMetafile : public wxScrolledWindow
161 {
162 public:
163 DnDCanvasMetafile(wxWindow *parent) : wxScrolledWindow(parent) { }
164
165 void SetMetafile(const wxMetafile& metafile)
166 {
167 m_metafile = metafile;
168
169 SetScrollbars(10, 10,
170 m_metafile.GetWidth() / 10, m_metafile.GetHeight() / 10);
171
172 Refresh();
173 }
174
175 void OnPaint(wxPaintEvent&)
176 {
177 wxPaintDC dc(this);
178
179 if ( m_metafile.Ok() )
180 {
181 PrepareDC(dc);
182
183 m_metafile.Play(&dc);
184 }
185 }
186
187 private:
188 wxMetafile m_metafile;
189
190 DECLARE_EVENT_TABLE()
191 };
192
193 #endif // wxUSE_METAFILE
194
195 // ----------------------------------------------------------------------------
196 // Define a new frame type for the main frame
197 // ----------------------------------------------------------------------------
198
199 class DnDFrame : public wxFrame
200 {
201 public:
202 DnDFrame(wxFrame *frame, const wxChar *title, int x, int y, int w, int h);
203 virtual ~DnDFrame();
204
205 void OnPaint(wxPaintEvent& event);
206 void OnSize(wxSizeEvent& event);
207 void OnQuit(wxCommandEvent& event);
208 void OnAbout(wxCommandEvent& event);
209 void OnDrag(wxCommandEvent& event);
210 void OnDragMoveByDefault(wxCommandEvent& event);
211 void OnDragMoveAllow(wxCommandEvent& event);
212 void OnNewFrame(wxCommandEvent& event);
213 void OnHelp (wxCommandEvent& event);
214 #if wxUSE_LOG
215 void OnLogClear(wxCommandEvent& event);
216 #endif // wxUSE_LOG
217
218 void OnCopy(wxCommandEvent& event);
219 void OnPaste(wxCommandEvent& event);
220
221 void OnCopyBitmap(wxCommandEvent& event);
222 void OnPasteBitmap(wxCommandEvent& event);
223
224 #if wxUSE_METAFILE
225 void OnPasteMetafile(wxCommandEvent& event);
226 #endif // wxUSE_METAFILE
227
228 void OnCopyFiles(wxCommandEvent& event);
229
230 void OnUsePrimary(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
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)
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
301 virtual size_t GetDataSize() const
302 {
303 return sizeof(ShapeDump);
304 }
305
306 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
320 const wxPoint& GetPosition() const { return m_pos; }
321 const wxColour& GetColour() const { return m_col; }
322 const wxSize& GetSize() const { return m_size; }
323
324 void Move(const wxPoint& pos) { m_pos = pos; }
325
326 // to implement in derived classes
327 virtual Kind GetKind() const = 0;
328
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
336 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:
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
363 virtual ~DnDTriangularShape()
364 {
365 wxLogMessage(wxT("DnDTriangularShape is being deleted"));
366 }
367
368 virtual Kind GetKind() const { return Triangle; }
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:
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
400 virtual ~DnDRectangularShape()
401 {
402 wxLogMessage(wxT("DnDRectangularShape is being deleted"));
403 }
404
405 virtual Kind GetKind() const { return Rectangle; }
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:
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
436 virtual ~DnDEllipticShape()
437 {
438 wxLogMessage(wxT("DnDEllipticShape is being deleted"));
439 }
440
441 virtual Kind GetKind() const { return Ellipse; }
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
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
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
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
519 virtual wxDataFormat GetPreferredFormat(Direction WXUNUSED(dir)) const
520 {
521 return m_formatShape;
522 }
523
524 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
541 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
558 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
585 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
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);
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:
735 DnDShapeDropTarget(DnDShapeFrame *frame)
736 : wxDropTarget(new DnDShapeDataObject)
737 {
738 m_frame = frame;
739 }
740
741 // override base class (pure) virtuals
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 }
749 virtual void OnLeave()
750 {
751 #if wxUSE_STATUSBAR
752 m_frame->SetStatusText(_T("Mouse left the frame"));
753 #endif // wxUSE_STATUSBAR
754 }
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_Help,
799 Menu_Clear,
800 Menu_Copy,
801 Menu_Paste,
802 Menu_CopyBitmap,
803 Menu_PasteBitmap,
804 Menu_PasteMFile,
805 Menu_CopyFiles,
806 Menu_UsePrimary,
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
815 BEGIN_EVENT_TABLE(DnDFrame, wxFrame)
816 EVT_MENU(Menu_Quit, DnDFrame::OnQuit)
817 EVT_MENU(Menu_About, DnDFrame::OnAbout)
818 EVT_MENU(Menu_Drag, DnDFrame::OnDrag)
819 EVT_MENU(Menu_DragMoveDef, DnDFrame::OnDragMoveByDefault)
820 EVT_MENU(Menu_DragMoveAllow,DnDFrame::OnDragMoveAllow)
821 EVT_MENU(Menu_NewFrame, DnDFrame::OnNewFrame)
822 EVT_MENU(Menu_Help, DnDFrame::OnHelp)
823 #if wxUSE_LOG
824 EVT_MENU(Menu_Clear, DnDFrame::OnLogClear)
825 #endif // wxUSE_LOG
826 EVT_MENU(Menu_Copy, DnDFrame::OnCopy)
827 EVT_MENU(Menu_Paste, DnDFrame::OnPaste)
828 EVT_MENU(Menu_CopyBitmap, DnDFrame::OnCopyBitmap)
829 EVT_MENU(Menu_PasteBitmap,DnDFrame::OnPasteBitmap)
830 #if wxUSE_METAFILE
831 EVT_MENU(Menu_PasteMFile, DnDFrame::OnPasteMetafile)
832 #endif // wxUSE_METAFILE
833 EVT_MENU(Menu_CopyFiles, DnDFrame::OnCopyFiles)
834 EVT_MENU(Menu_UsePrimary, DnDFrame::OnUsePrimary)
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 ( !wxApp::OnInit() )
891 return false;
892
893 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
894 // switch on trace messages
895 #if wxUSE_LOG
896 #if defined(__WXGTK__)
897 wxLog::AddTraceMask(_T("clipboard"));
898 #elif defined(__WXMSW__)
899 wxLog::AddTraceMask(wxTRACE_OleCalls);
900 #endif
901 #endif // wxUSE_LOG
902
903 #if wxUSE_LIBPNG
904 wxImage::AddHandler( new wxPNGHandler );
905 #endif
906
907 // create the main frame window
908 DnDFrame *frame = new DnDFrame((wxFrame *) NULL,
909 _T("Drag-and-Drop/Clipboard wxWidgets Sample"),
910 10, 100, 650, 340);
911
912 // activate it
913 frame->Show(true);
914
915 SetTopWindow(frame);
916
917 return true;
918 #else
919 wxMessageBox( _T("This sample has to be compiled with wxUSE_DRAG_AND_DROP"), _T("Building error"), wxOK);
920 return false;
921 #endif // wxUSE_DRAG_AND_DROP
922 }
923
924 #if wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD
925
926 DnDFrame::DnDFrame(wxFrame *frame, const wxChar *title, int x, int y, int w, int h)
927 : wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)),
928 m_strText(_T("wxWidgets drag & drop works :-)"))
929
930 {
931 // frame icon and status bar
932 SetIcon(wxICON(sample));
933
934 #if wxUSE_STATUSBAR
935 CreateStatusBar();
936 #endif // wxUSE_STATUSBAR
937
938 // construct menu
939 wxMenu *file_menu = new wxMenu;
940 file_menu->Append(Menu_Drag, _T("&Test drag..."));
941 file_menu->AppendCheckItem(Menu_DragMoveDef, _T("&Move by default"));
942 file_menu->AppendCheckItem(Menu_DragMoveAllow, _T("&Allow moving"));
943 file_menu->AppendSeparator();
944 file_menu->Append(Menu_NewFrame, _T("&New frame\tCtrl-N"));
945 file_menu->AppendSeparator();
946 file_menu->Append(Menu_Quit, _T("E&xit\tCtrl-Q"));
947
948 #if wxUSE_LOG
949 wxMenu *log_menu = new wxMenu;
950 log_menu->Append(Menu_Clear, _T("Clear\tCtrl-L"));
951 #endif // wxUSE_LOG
952
953 wxMenu *help_menu = new wxMenu;
954 help_menu->Append(Menu_Help, _T("&Help..."));
955 help_menu->AppendSeparator();
956 help_menu->Append(Menu_About, _T("&About"));
957
958 wxMenu *clip_menu = new wxMenu;
959 clip_menu->Append(Menu_Copy, _T("&Copy text\tCtrl-C"));
960 clip_menu->Append(Menu_Paste, _T("&Paste text\tCtrl-V"));
961 clip_menu->AppendSeparator();
962 clip_menu->Append(Menu_CopyBitmap, _T("Copy &bitmap\tCtrl-Shift-C"));
963 clip_menu->Append(Menu_PasteBitmap, _T("Paste b&itmap\tCtrl-Shift-V"));
964 #if wxUSE_METAFILE
965 clip_menu->AppendSeparator();
966 clip_menu->Append(Menu_PasteMFile, _T("Paste &metafile\tCtrl-M"));
967 #endif // wxUSE_METAFILE
968 clip_menu->AppendSeparator();
969 clip_menu->Append(Menu_CopyFiles, _T("Copy &files\tCtrl-F"));
970 clip_menu->AppendSeparator();
971 clip_menu->AppendCheckItem(Menu_UsePrimary, _T("Use &primary selection\tCtrl-P"));
972
973 wxMenuBar *menu_bar = new wxMenuBar;
974 menu_bar->Append(file_menu, _T("&File"));
975 #if wxUSE_LOG
976 menu_bar->Append(log_menu, _T("&Log"));
977 #endif // wxUSE_LOG
978 menu_bar->Append(clip_menu, _T("&Clipboard"));
979 menu_bar->Append(help_menu, _T("&Help"));
980
981 SetMenuBar(menu_bar);
982
983 // make a panel with 3 subwindows
984 wxString strFile(_T("Drop files here!")), strText(_T("Drop text on me"));
985
986 m_ctrlFile = new wxListBox(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 1, &strFile,
987 wxLB_HSCROLL | wxLB_ALWAYS_SB );
988 m_ctrlText = new wxListBox(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 1, &strText,
989 wxLB_HSCROLL | wxLB_ALWAYS_SB );
990
991 #if wxUSE_LOG
992 m_ctrlLog = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
993 wxTE_MULTILINE | wxTE_READONLY |
994 wxSUNKEN_BORDER );
995
996 // redirect log messages to the text window
997 m_pLog = new wxLogTextCtrl(m_ctrlLog);
998 m_pLogPrev = wxLog::SetActiveTarget(m_pLog);
999 #endif // wxUSE_LOG
1000
1001 #if wxUSE_DRAG_AND_DROP
1002 // associate drop targets with the controls
1003 m_ctrlFile->SetDropTarget(new DnDFile(m_ctrlFile));
1004 m_ctrlText->SetDropTarget(new DnDText(m_ctrlText));
1005 #if wxUSE_LOG
1006 m_ctrlLog->SetDropTarget(new URLDropTarget);
1007 #endif // wxUSE_LOG
1008 #endif // wxUSE_DRAG_AND_DROP
1009
1010 wxBoxSizer *sizer_top = new wxBoxSizer( wxHORIZONTAL );
1011 sizer_top->Add(m_ctrlFile, 1, wxEXPAND );
1012 sizer_top->Add(m_ctrlText, 1, wxEXPAND );
1013
1014 wxBoxSizer *sizer = new wxBoxSizer( wxVERTICAL );
1015 sizer->Add(sizer_top, 1, wxEXPAND );
1016 #if wxUSE_LOG
1017 sizer->Add(m_ctrlLog, 2, wxEXPAND);
1018 sizer->SetItemMinSize(m_ctrlLog, 450, 0);
1019 #endif // wxUSE_LOG
1020 sizer->AddSpacer(50);
1021
1022 SetSizer(sizer);
1023 sizer->SetSizeHints( this );
1024
1025 // copy data by default but allow moving it as well
1026 m_moveByDefault = false;
1027 m_moveAllow = true;
1028 menu_bar->Check(Menu_DragMoveAllow, true);
1029 }
1030
1031 void DnDFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
1032 {
1033 Close(true);
1034 }
1035
1036 void DnDFrame::OnSize(wxSizeEvent& event)
1037 {
1038 Refresh();
1039
1040 event.Skip();
1041 }
1042
1043 void DnDFrame::OnPaint(wxPaintEvent& WXUNUSED(event))
1044 {
1045 int w = 0;
1046 int h = 0;
1047 GetClientSize( &w, &h );
1048
1049 wxPaintDC dc(this);
1050 // dc.Clear(); -- this kills wxGTK
1051 dc.SetFont( wxFont( 24, wxDECORATIVE, wxNORMAL, wxNORMAL, false, _T("charter") ) );
1052 dc.DrawText( _T("Drag text from here!"), 100, h-50 );
1053 }
1054
1055 void DnDFrame::OnUpdateUIMoveByDefault(wxUpdateUIEvent& event)
1056 {
1057 // only can move by default if moving is allowed at all
1058 event.Enable(m_moveAllow);
1059 }
1060
1061 void DnDFrame::OnUpdateUIPasteText(wxUpdateUIEvent& event)
1062 {
1063 #ifdef __WXDEBUG__
1064 // too many trace messages if we don't do it - this function is called
1065 // very often
1066 wxLogNull nolog;
1067 #endif
1068
1069 event.Enable( wxTheClipboard->IsSupported(wxDF_TEXT) );
1070 }
1071
1072 void DnDFrame::OnUpdateUIPasteBitmap(wxUpdateUIEvent& event)
1073 {
1074 #ifdef __WXDEBUG__
1075 // too many trace messages if we don't do it - this function is called
1076 // very often
1077 wxLogNull nolog;
1078 #endif
1079
1080 event.Enable( wxTheClipboard->IsSupported(wxDF_BITMAP) );
1081 }
1082
1083 void DnDFrame::OnNewFrame(wxCommandEvent& WXUNUSED(event))
1084 {
1085 #if wxUSE_DRAG_AND_DROP
1086 (new DnDShapeFrame(this))->Show(true);
1087
1088 wxLogStatus(this, wxT("Double click the new frame to select a shape for it"));
1089 #endif // wxUSE_DRAG_AND_DROP
1090 }
1091
1092 void DnDFrame::OnDrag(wxCommandEvent& WXUNUSED(event))
1093 {
1094 #if wxUSE_DRAG_AND_DROP
1095 wxString strText = wxGetTextFromUser
1096 (
1097 _T("After you enter text in this dialog, press any mouse\n")
1098 _T("button in the bottom (empty) part of the frame and \n")
1099 _T("drag it anywhere - you will be in fact dragging the\n")
1100 _T("text object containing this text"),
1101 _T("Please enter some text"), m_strText, this
1102 );
1103
1104 m_strText = strText;
1105 #endif // wxUSE_DRAG_AND_DROP
1106 }
1107
1108 void DnDFrame::OnDragMoveByDefault(wxCommandEvent& event)
1109 {
1110 m_moveByDefault = event.IsChecked();
1111 }
1112
1113 void DnDFrame::OnDragMoveAllow(wxCommandEvent& event)
1114 {
1115 m_moveAllow = event.IsChecked();
1116 }
1117
1118 void DnDFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
1119 {
1120 wxMessageBox(_T("Drag-&-Drop Demo\n")
1121 _T("Please see \"Help|Help...\" for details\n")
1122 _T("Copyright (c) 1998 Vadim Zeitlin"),
1123 _T("About wxDnD"),
1124 wxICON_INFORMATION | wxOK,
1125 this);
1126 }
1127
1128 void DnDFrame::OnHelp(wxCommandEvent& /* event */)
1129 {
1130 wxMessageDialog dialog(this,
1131 _T("This small program demonstrates drag & drop support in wxWidgets. The program window\n")
1132 _T("consists of 3 parts: the bottom pane is for debug messages, so that you can see what's\n")
1133 _T("going on inside. The top part is split into 2 listboxes, the left one accepts files\n")
1134 _T("and the right one accepts text.\n")
1135 _T("\n")
1136 _T("To test wxDropTarget: open wordpad (write.exe), select some text in it and drag it to\n")
1137 _T("the right listbox (you'll notice the usual visual feedback, i.e. the cursor will change).\n")
1138 _T("Also, try dragging some files (you can select several at once) from Windows Explorer (or \n")
1139 _T("File Manager) to the left pane. Hold down Ctrl/Shift keys when you drop text (doesn't \n")
1140 _T("work with files) and see what changes.\n")
1141 _T("\n")
1142 _T("To test wxDropSource: just press any mouse button on the empty zone of the window and drag\n")
1143 _T("it to wordpad or any other droptarget accepting text (and of course you can just drag it\n")
1144 _T("to the right pane). Due to a lot of trace messages, the cursor might take some time to \n")
1145 _T("change, don't release the mouse button until it does. You can change the string being\n")
1146 _T("dragged in in \"File|Test drag...\" dialog.\n")
1147 _T("\n")
1148 _T("\n")
1149 _T("Please send all questions/bug reports/suggestions &c to \n")
1150 _T("Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>"),
1151 _T("wxDnD Help"));
1152
1153 dialog.ShowModal();
1154 }
1155
1156 #if wxUSE_LOG
1157 void DnDFrame::OnLogClear(wxCommandEvent& /* event */ )
1158 {
1159 m_ctrlLog->Clear();
1160 m_ctrlText->Clear();
1161 m_ctrlFile->Clear();
1162 }
1163 #endif // wxUSE_LOG
1164
1165 void DnDFrame::OnLeftDown(wxMouseEvent &WXUNUSED(event) )
1166 {
1167 #if wxUSE_DRAG_AND_DROP
1168 if ( !m_strText.empty() )
1169 {
1170 // start drag operation
1171 wxTextDataObject textData(m_strText);
1172 wxDropSource source(textData, this,
1173 wxDROP_ICON(dnd_copy),
1174 wxDROP_ICON(dnd_move),
1175 wxDROP_ICON(dnd_none));
1176
1177 int flags = 0;
1178 if ( m_moveByDefault )
1179 flags |= wxDrag_DefaultMove;
1180 else if ( m_moveAllow )
1181 flags |= wxDrag_AllowMove;
1182
1183 wxDragResult result = source.DoDragDrop(flags);
1184
1185 #if wxUSE_STATUSBAR
1186 const wxChar *pc;
1187 switch ( result )
1188 {
1189 case wxDragError: pc = _T("Error!"); break;
1190 case wxDragNone: pc = _T("Nothing"); break;
1191 case wxDragCopy: pc = _T("Copied"); break;
1192 case wxDragMove: pc = _T("Moved"); break;
1193 case wxDragCancel: pc = _T("Cancelled"); break;
1194 default: pc = _T("Huh?"); break;
1195 }
1196
1197 SetStatusText(wxString(_T("Drag result: ")) + pc);
1198 #else
1199 wxUnusedVar(result);
1200 #endif // wxUSE_STATUSBAR
1201 }
1202 #endif // wxUSE_DRAG_AND_DROP
1203 }
1204
1205 void DnDFrame::OnRightDown(wxMouseEvent &event )
1206 {
1207 wxMenu menu(_T("Dnd sample menu"));
1208
1209 menu.Append(Menu_Drag, _T("&Test drag..."));
1210 menu.AppendSeparator();
1211 menu.Append(Menu_About, _T("&About"));
1212
1213 PopupMenu( &menu, event.GetX(), event.GetY() );
1214 }
1215
1216 DnDFrame::~DnDFrame()
1217 {
1218 #if wxUSE_LOG
1219 if ( m_pLog != NULL ) {
1220 if ( wxLog::SetActiveTarget(m_pLogPrev) == m_pLog )
1221 delete m_pLog;
1222 }
1223 #endif // wxUSE_LOG
1224 }
1225
1226 void DnDFrame::OnUsePrimary(wxCommandEvent& event)
1227 {
1228 const bool usePrimary = event.IsChecked();
1229 wxTheClipboard->UsePrimarySelection(usePrimary);
1230
1231 wxLogStatus(_T("Now using %s selection"), usePrimary ? _T("primary")
1232 : _T("clipboard"));
1233 }
1234
1235 // ---------------------------------------------------------------------------
1236 // bitmap clipboard
1237 // ---------------------------------------------------------------------------
1238
1239 void DnDFrame::OnCopyBitmap(wxCommandEvent& WXUNUSED(event))
1240 {
1241 // PNG support is not always compiled in under Windows, so use BMP there
1242 #if wxUSE_LIBPNG
1243 wxFileDialog dialog(this, _T("Open a PNG file"), wxEmptyString, wxEmptyString, _T("PNG files (*.png)|*.png"), 0);
1244 #else
1245 wxFileDialog dialog(this, _T("Open a BMP file"), wxEmptyString, wxEmptyString, _T("BMP files (*.bmp)|*.bmp"), 0);
1246 #endif
1247
1248 if (dialog.ShowModal() != wxID_OK)
1249 {
1250 wxLogMessage( _T("Aborted file open") );
1251 return;
1252 }
1253
1254 if (dialog.GetPath().empty())
1255 {
1256 wxLogMessage( _T("Returned empty string.") );
1257 return;
1258 }
1259
1260 if (!wxFileExists(dialog.GetPath()))
1261 {
1262 wxLogMessage( _T("File doesn't exist.") );
1263 return;
1264 }
1265
1266 wxImage image;
1267 image.LoadFile( dialog.GetPath(),
1268 #if wxUSE_LIBPNG
1269 wxBITMAP_TYPE_PNG
1270 #else
1271 wxBITMAP_TYPE_BMP
1272 #endif
1273 );
1274 if (!image.Ok())
1275 {
1276 wxLogError( _T("Invalid image file...") );
1277 return;
1278 }
1279
1280 wxLogStatus( _T("Decoding image file...") );
1281 wxYield();
1282
1283 wxBitmap bitmap( image );
1284
1285 if ( !wxTheClipboard->Open() )
1286 {
1287 wxLogError(_T("Can't open clipboard."));
1288
1289 return;
1290 }
1291
1292 wxLogMessage( _T("Creating wxBitmapDataObject...") );
1293 wxYield();
1294
1295 if ( !wxTheClipboard->AddData(new wxBitmapDataObject(bitmap)) )
1296 {
1297 wxLogError(_T("Can't copy image to the clipboard."));
1298 }
1299 else
1300 {
1301 wxLogMessage(_T("Image has been put on the clipboard.") );
1302 wxLogMessage(_T("You can paste it now and look at it.") );
1303 }
1304
1305 wxTheClipboard->Close();
1306 }
1307
1308 void DnDFrame::OnPasteBitmap(wxCommandEvent& WXUNUSED(event))
1309 {
1310 if ( !wxTheClipboard->Open() )
1311 {
1312 wxLogError(_T("Can't open clipboard."));
1313
1314 return;
1315 }
1316
1317 if ( !wxTheClipboard->IsSupported(wxDF_BITMAP) )
1318 {
1319 wxLogWarning(_T("No bitmap on clipboard"));
1320
1321 wxTheClipboard->Close();
1322 return;
1323 }
1324
1325 wxBitmapDataObject data;
1326 if ( !wxTheClipboard->GetData(data) )
1327 {
1328 wxLogError(_T("Can't paste bitmap from the clipboard"));
1329 }
1330 else
1331 {
1332 const wxBitmap& bmp = data.GetBitmap();
1333
1334 wxLogMessage(_T("Bitmap %dx%d pasted from the clipboard"),
1335 bmp.GetWidth(), bmp.GetHeight());
1336 ShowBitmap(bmp);
1337 }
1338
1339 wxTheClipboard->Close();
1340 }
1341
1342 #if wxUSE_METAFILE
1343
1344 void DnDFrame::OnPasteMetafile(wxCommandEvent& WXUNUSED(event))
1345 {
1346 if ( !wxTheClipboard->Open() )
1347 {
1348 wxLogError(_T("Can't open clipboard."));
1349
1350 return;
1351 }
1352
1353 if ( !wxTheClipboard->IsSupported(wxDF_METAFILE) )
1354 {
1355 wxLogWarning(_T("No metafile on clipboard"));
1356 }
1357 else
1358 {
1359 wxMetaFileDataObject data;
1360 if ( !wxTheClipboard->GetData(data) )
1361 {
1362 wxLogError(_T("Can't paste metafile from the clipboard"));
1363 }
1364 else
1365 {
1366 const wxMetaFile& mf = data.GetMetafile();
1367
1368 wxLogMessage(_T("Metafile %dx%d pasted from the clipboard"),
1369 mf.GetWidth(), mf.GetHeight());
1370
1371 ShowMetaFile(mf);
1372 }
1373 }
1374
1375 wxTheClipboard->Close();
1376 }
1377
1378 #endif // wxUSE_METAFILE
1379
1380 // ----------------------------------------------------------------------------
1381 // file clipboard
1382 // ----------------------------------------------------------------------------
1383
1384 void DnDFrame::OnCopyFiles(wxCommandEvent& WXUNUSED(event))
1385 {
1386 #ifdef __WXMSW__
1387 wxFileDialog dialog(this, _T("Select a file to copy"), wxEmptyString, wxEmptyString,
1388 _T("All files (*.*)|*.*"), 0);
1389
1390 wxArrayString filenames;
1391 while ( dialog.ShowModal() == wxID_OK )
1392 {
1393 filenames.Add(dialog.GetPath());
1394 }
1395
1396 if ( !filenames.IsEmpty() )
1397 {
1398 wxFileDataObject *dobj = new wxFileDataObject;
1399 size_t count = filenames.GetCount();
1400 for ( size_t n = 0; n < count; n++ )
1401 {
1402 dobj->AddFile(filenames[n]);
1403 }
1404
1405 wxClipboardLocker locker;
1406 if ( !locker )
1407 {
1408 wxLogError(wxT("Can't open clipboard"));
1409 }
1410 else
1411 {
1412 if ( !wxTheClipboard->AddData(dobj) )
1413 {
1414 wxLogError(wxT("Can't copy file(s) to the clipboard"));
1415 }
1416 else
1417 {
1418 wxLogStatus(this, wxT("%d file%s copied to the clipboard"),
1419 count, count == 1 ? wxEmptyString : wxEmptyString);
1420 }
1421 }
1422 }
1423 else
1424 {
1425 wxLogStatus(this, wxT("Aborted"));
1426 }
1427 #else // !MSW
1428 wxLogError(wxT("Sorry, not implemented"));
1429 #endif // MSW/!MSW
1430 }
1431
1432 // ---------------------------------------------------------------------------
1433 // text clipboard
1434 // ---------------------------------------------------------------------------
1435
1436 void DnDFrame::OnCopy(wxCommandEvent& WXUNUSED(event))
1437 {
1438 if ( !wxTheClipboard->Open() )
1439 {
1440 wxLogError(_T("Can't open clipboard."));
1441
1442 return;
1443 }
1444
1445 if ( !wxTheClipboard->AddData(new wxTextDataObject(m_strText)) )
1446 {
1447 wxLogError(_T("Can't copy data to the clipboard"));
1448 }
1449 else
1450 {
1451 wxLogMessage(_T("Text '%s' put on the clipboard"), m_strText.c_str());
1452 }
1453
1454 wxTheClipboard->Close();
1455 }
1456
1457 void DnDFrame::OnPaste(wxCommandEvent& WXUNUSED(event))
1458 {
1459 if ( !wxTheClipboard->Open() )
1460 {
1461 wxLogError(_T("Can't open clipboard."));
1462
1463 return;
1464 }
1465
1466 if ( !wxTheClipboard->IsSupported(wxDF_TEXT) )
1467 {
1468 wxLogWarning(_T("No text data on clipboard"));
1469
1470 wxTheClipboard->Close();
1471 return;
1472 }
1473
1474 wxTextDataObject text;
1475 if ( !wxTheClipboard->GetData(text) )
1476 {
1477 wxLogError(_T("Can't paste data from the clipboard"));
1478 }
1479 else
1480 {
1481 wxLogMessage(_T("Text '%s' pasted from the clipboard"),
1482 text.GetText().c_str());
1483 }
1484
1485 wxTheClipboard->Close();
1486 }
1487
1488 #if wxUSE_DRAG_AND_DROP
1489
1490 // ----------------------------------------------------------------------------
1491 // Notifications called by the base class
1492 // ----------------------------------------------------------------------------
1493
1494 bool DnDText::OnDropText(wxCoord, wxCoord, const wxString& text)
1495 {
1496 m_pOwner->Append(text);
1497
1498 return true;
1499 }
1500
1501 bool DnDFile::OnDropFiles(wxCoord, wxCoord, const wxArrayString& filenames)
1502 {
1503 size_t nFiles = filenames.GetCount();
1504 wxString str;
1505 str.Printf( _T("%d files dropped"), (int)nFiles);
1506 m_pOwner->Append(str);
1507 for ( size_t n = 0; n < nFiles; n++ ) {
1508 m_pOwner->Append(filenames[n]);
1509 }
1510
1511 return true;
1512 }
1513
1514 // ----------------------------------------------------------------------------
1515 // DnDShapeDialog
1516 // ----------------------------------------------------------------------------
1517
1518 DnDShapeDialog::DnDShapeDialog(wxFrame *parent, DnDShape *shape)
1519 :wxDialog( parent, 6001, wxT("Choose Shape"), wxPoint( 10, 10 ),
1520 wxSize( 40, 40 ),
1521 wxDEFAULT_DIALOG_STYLE | wxRAISED_BORDER | wxRESIZE_BORDER )
1522 {
1523 m_shape = shape;
1524 wxBoxSizer* topSizer = new wxBoxSizer( wxVERTICAL );
1525
1526 // radio box
1527 wxBoxSizer* shapesSizer = new wxBoxSizer( wxHORIZONTAL );
1528 const wxString choices[] = { wxT("None"), wxT("Triangle"),
1529 wxT("Rectangle"), wxT("Ellipse") };
1530
1531 m_radio = new wxRadioBox( this, wxID_ANY, wxT("&Shape"),
1532 wxDefaultPosition, wxDefaultSize, 4, choices, 4,
1533 wxRA_SPECIFY_COLS );
1534 shapesSizer->Add( m_radio, 0, wxGROW|wxALL, 5 );
1535 topSizer->Add( shapesSizer, 0, wxALL, 2 );
1536
1537 // attributes
1538 wxStaticBox* box = new wxStaticBox( this, wxID_ANY, wxT("&Attributes") );
1539 wxStaticBoxSizer* attrSizer = new wxStaticBoxSizer( box, wxHORIZONTAL );
1540 wxFlexGridSizer* xywhSizer = new wxFlexGridSizer( 4, 2 );
1541
1542 wxStaticText* st;
1543
1544 st = new wxStaticText( this, wxID_ANY, wxT("Position &X:") );
1545 m_textX = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
1546 wxSize( 30, 20 ) );
1547 xywhSizer->Add( st, 1, wxGROW|wxALL, 2 );
1548 xywhSizer->Add( m_textX, 1, wxGROW|wxALL, 2 );
1549
1550 st = new wxStaticText( this, wxID_ANY, wxT("Size &width:") );
1551 m_textW = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
1552 wxSize( 30, 20 ) );
1553 xywhSizer->Add( st, 1, wxGROW|wxALL, 2 );
1554 xywhSizer->Add( m_textW, 1, wxGROW|wxALL, 2 );
1555
1556 st = new wxStaticText( this, wxID_ANY, wxT("&Y:") );
1557 m_textY = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
1558 wxSize( 30, 20 ) );
1559 xywhSizer->Add( st, 1, wxALL|wxALIGN_RIGHT, 2 );
1560 xywhSizer->Add( m_textY, 1, wxGROW|wxALL, 2 );
1561
1562 st = new wxStaticText( this, wxID_ANY, wxT("&height:") );
1563 m_textH = new wxTextCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition,
1564 wxSize( 30, 20 ) );
1565 xywhSizer->Add( st, 1, wxALL|wxALIGN_RIGHT, 2 );
1566 xywhSizer->Add( m_textH, 1, wxGROW|wxALL, 2 );
1567
1568 wxButton* col = new wxButton( this, Button_Colour, wxT("&Colour...") );
1569 attrSizer->Add( xywhSizer, 1, wxGROW );
1570 attrSizer->Add( col, 0, wxALL|wxALIGN_CENTRE_VERTICAL, 2 );
1571 topSizer->Add( attrSizer, 0, wxGROW|wxALL, 5 );
1572
1573 // buttons
1574 wxBoxSizer* buttonSizer = new wxBoxSizer( wxHORIZONTAL );
1575 wxButton* bt;
1576 bt = new wxButton( this, wxID_OK, wxT("Ok") );
1577 buttonSizer->Add( bt, 0, wxALL, 2 );
1578 bt = new wxButton( this, wxID_CANCEL, wxT("Cancel") );
1579 buttonSizer->Add( bt, 0, wxALL, 2 );
1580 topSizer->Add( buttonSizer, 0, wxALL|wxALIGN_RIGHT, 2 );
1581
1582 SetSizer( topSizer );
1583 topSizer->Fit( this );
1584 }
1585
1586 DnDShape *DnDShapeDialog::GetShape() const
1587 {
1588 switch ( m_shapeKind )
1589 {
1590 default:
1591 case DnDShape::None: return NULL;
1592 case DnDShape::Triangle: return new DnDTriangularShape(m_pos, m_size, m_col);
1593 case DnDShape::Rectangle: return new DnDRectangularShape(m_pos, m_size, m_col);
1594 case DnDShape::Ellipse: return new DnDEllipticShape(m_pos, m_size, m_col);
1595 }
1596 }
1597
1598 bool DnDShapeDialog::TransferDataToWindow()
1599 {
1600
1601 if ( m_shape )
1602 {
1603 m_radio->SetSelection(m_shape->GetKind());
1604 m_pos = m_shape->GetPosition();
1605 m_size = m_shape->GetSize();
1606 m_col = m_shape->GetColour();
1607 }
1608 else
1609 {
1610 m_radio->SetSelection(DnDShape::None);
1611 m_pos = wxPoint(1, 1);
1612 m_size = wxSize(100, 100);
1613 }
1614
1615 m_textX->SetValue(wxString() << m_pos.x);
1616 m_textY->SetValue(wxString() << m_pos.y);
1617 m_textW->SetValue(wxString() << m_size.x);
1618 m_textH->SetValue(wxString() << m_size.y);
1619
1620 return true;
1621 }
1622
1623 bool DnDShapeDialog::TransferDataFromWindow()
1624 {
1625 m_shapeKind = (DnDShape::Kind)m_radio->GetSelection();
1626
1627 m_pos.x = wxAtoi(m_textX->GetValue());
1628 m_pos.y = wxAtoi(m_textY->GetValue());
1629 m_size.x = wxAtoi(m_textW->GetValue());
1630 m_size.y = wxAtoi(m_textH->GetValue());
1631
1632 if ( !m_pos.x || !m_pos.y || !m_size.x || !m_size.y )
1633 {
1634 wxMessageBox(_T("All sizes and positions should be non null!"),
1635 _T("Invalid shape"), wxICON_HAND | wxOK, this);
1636
1637 return false;
1638 }
1639
1640 return true;
1641 }
1642
1643 void DnDShapeDialog::OnColour(wxCommandEvent& WXUNUSED(event))
1644 {
1645 wxColourData data;
1646 data.SetChooseFull(true);
1647 for (int i = 0; i < 16; i++)
1648 {
1649 wxColour colour((unsigned char)(i*16), (unsigned char)(i*16), (unsigned char)(i*16));
1650 data.SetCustomColour(i, colour);
1651 }
1652
1653 wxColourDialog dialog(this, &data);
1654 if ( dialog.ShowModal() == wxID_OK )
1655 {
1656 m_col = dialog.GetColourData().GetColour();
1657 }
1658 }
1659
1660 // ----------------------------------------------------------------------------
1661 // DnDShapeFrame
1662 // ----------------------------------------------------------------------------
1663
1664 DnDShapeFrame *DnDShapeFrame::ms_lastDropTarget = NULL;
1665
1666 DnDShapeFrame::DnDShapeFrame(wxFrame *parent)
1667 : wxFrame(parent, wxID_ANY, _T("Shape Frame"))
1668 {
1669 #if wxUSE_STATUSBAR
1670 CreateStatusBar();
1671 #endif // wxUSE_STATUSBAR
1672
1673 wxMenu *menuShape = new wxMenu;
1674 menuShape->Append(Menu_Shape_New, _T("&New default shape\tCtrl-S"));
1675 menuShape->Append(Menu_Shape_Edit, _T("&Edit shape\tCtrl-E"));
1676 menuShape->AppendSeparator();
1677 menuShape->Append(Menu_Shape_Clear, _T("&Clear shape\tCtrl-L"));
1678
1679 wxMenu *menuClipboard = new wxMenu;
1680 menuClipboard->Append(Menu_ShapeClipboard_Copy, _T("&Copy\tCtrl-C"));
1681 menuClipboard->Append(Menu_ShapeClipboard_Paste, _T("&Paste\tCtrl-V"));
1682
1683 wxMenuBar *menubar = new wxMenuBar;
1684 menubar->Append(menuShape, _T("&Shape"));
1685 menubar->Append(menuClipboard, _T("&Clipboard"));
1686
1687 SetMenuBar(menubar);
1688
1689 #if wxUSE_STATUSBAR
1690 SetStatusText(_T("Press Ctrl-S to create a new shape"));
1691 #endif // wxUSE_STATUSBAR
1692
1693 SetDropTarget(new DnDShapeDropTarget(this));
1694
1695 m_shape = NULL;
1696
1697 SetBackgroundColour(*wxWHITE);
1698 }
1699
1700 DnDShapeFrame::~DnDShapeFrame()
1701 {
1702 if (m_shape)
1703 delete m_shape;
1704 }
1705
1706 void DnDShapeFrame::SetShape(DnDShape *shape)
1707 {
1708 if (m_shape)
1709 delete m_shape;
1710 m_shape = shape;
1711 Refresh();
1712 }
1713
1714 // callbacks
1715 void DnDShapeFrame::OnDrag(wxMouseEvent& event)
1716 {
1717 if ( !m_shape )
1718 {
1719 event.Skip();
1720
1721 return;
1722 }
1723
1724 // start drag operation
1725 DnDShapeDataObject shapeData(m_shape);
1726 wxDropSource source(shapeData, this);
1727
1728 const wxChar *pc = NULL;
1729 switch ( source.DoDragDrop(true) )
1730 {
1731 default:
1732 case wxDragError:
1733 wxLogError(wxT("An error occurred during drag and drop operation"));
1734 break;
1735
1736 case wxDragNone:
1737 #if wxUSE_STATUSBAR
1738 SetStatusText(_T("Nothing happened"));
1739 #endif // wxUSE_STATUSBAR
1740 break;
1741
1742 case wxDragCopy:
1743 pc = _T("copied");
1744 break;
1745
1746 case wxDragMove:
1747 pc = _T("moved");
1748 if ( ms_lastDropTarget != this )
1749 {
1750 // don't delete the shape if we dropped it on ourselves!
1751 SetShape(NULL);
1752 }
1753 break;
1754
1755 case wxDragCancel:
1756 #if wxUSE_STATUSBAR
1757 SetStatusText(_T("Drag and drop operation cancelled"));
1758 #endif // wxUSE_STATUSBAR
1759 break;
1760 }
1761
1762 if ( pc )
1763 {
1764 #if wxUSE_STATUSBAR
1765 SetStatusText(wxString(_T("Shape successfully ")) + pc);
1766 #endif // wxUSE_STATUSBAR
1767 }
1768 //else: status text already set
1769 }
1770
1771 void DnDShapeFrame::OnDrop(wxCoord x, wxCoord y, DnDShape *shape)
1772 {
1773 ms_lastDropTarget = this;
1774
1775 wxPoint pt(x, y);
1776
1777 #if wxUSE_STATUSBAR
1778 wxString s;
1779 s.Printf(wxT("Shape dropped at (%d, %d)"), pt.x, pt.y);
1780 SetStatusText(s);
1781 #endif // wxUSE_STATUSBAR
1782
1783 shape->Move(pt);
1784 SetShape(shape);
1785 }
1786
1787 void DnDShapeFrame::OnEditShape(wxCommandEvent& WXUNUSED(event))
1788 {
1789 DnDShapeDialog dlg(this, m_shape);
1790 if ( dlg.ShowModal() == wxID_OK )
1791 {
1792 SetShape(dlg.GetShape());
1793
1794 #if wxUSE_STATUSBAR
1795 if ( m_shape )
1796 {
1797 SetStatusText(_T("You can now drag the shape to another frame"));
1798 }
1799 #endif // wxUSE_STATUSBAR
1800 }
1801 }
1802
1803 void DnDShapeFrame::OnNewShape(wxCommandEvent& WXUNUSED(event))
1804 {
1805 SetShape(new DnDEllipticShape(wxPoint(10, 10), wxSize(80, 60), *wxRED));
1806
1807 #if wxUSE_STATUSBAR
1808 SetStatusText(_T("You can now drag the shape to another frame"));
1809 #endif // wxUSE_STATUSBAR
1810 }
1811
1812 void DnDShapeFrame::OnClearShape(wxCommandEvent& WXUNUSED(event))
1813 {
1814 SetShape(NULL);
1815 }
1816
1817 void DnDShapeFrame::OnCopyShape(wxCommandEvent& WXUNUSED(event))
1818 {
1819 if ( m_shape )
1820 {
1821 wxClipboardLocker clipLocker;
1822 if ( !clipLocker )
1823 {
1824 wxLogError(wxT("Can't open the clipboard"));
1825
1826 return;
1827 }
1828
1829 wxTheClipboard->AddData(new DnDShapeDataObject(m_shape));
1830 }
1831 }
1832
1833 void DnDShapeFrame::OnPasteShape(wxCommandEvent& WXUNUSED(event))
1834 {
1835 wxClipboardLocker clipLocker;
1836 if ( !clipLocker )
1837 {
1838 wxLogError(wxT("Can't open the clipboard"));
1839
1840 return;
1841 }
1842
1843 DnDShapeDataObject shapeDataObject(NULL);
1844 if ( wxTheClipboard->GetData(shapeDataObject) )
1845 {
1846 SetShape(shapeDataObject.GetShape());
1847 }
1848 else
1849 {
1850 wxLogStatus(wxT("No shape on the clipboard"));
1851 }
1852 }
1853
1854 void DnDShapeFrame::OnUpdateUICopy(wxUpdateUIEvent& event)
1855 {
1856 event.Enable( m_shape != NULL );
1857 }
1858
1859 void DnDShapeFrame::OnUpdateUIPaste(wxUpdateUIEvent& event)
1860 {
1861 event.Enable( wxTheClipboard->IsSupported(wxDataFormat(shapeFormatId)) );
1862 }
1863
1864 void DnDShapeFrame::OnPaint(wxPaintEvent& event)
1865 {
1866 if ( m_shape )
1867 {
1868 wxPaintDC dc(this);
1869
1870 m_shape->Draw(dc);
1871 }
1872 else
1873 {
1874 event.Skip();
1875 }
1876 }
1877
1878 // ----------------------------------------------------------------------------
1879 // DnDShape
1880 // ----------------------------------------------------------------------------
1881
1882 DnDShape *DnDShape::New(const void *buf)
1883 {
1884 const ShapeDump& dump = *(const ShapeDump *)buf;
1885 switch ( dump.k )
1886 {
1887 case Triangle:
1888 return new DnDTriangularShape(wxPoint(dump.x, dump.y),
1889 wxSize(dump.w, dump.h),
1890 wxColour(dump.r, dump.g, dump.b));
1891
1892 case Rectangle:
1893 return new DnDRectangularShape(wxPoint(dump.x, dump.y),
1894 wxSize(dump.w, dump.h),
1895 wxColour(dump.r, dump.g, dump.b));
1896
1897 case Ellipse:
1898 return new DnDEllipticShape(wxPoint(dump.x, dump.y),
1899 wxSize(dump.w, dump.h),
1900 wxColour(dump.r, dump.g, dump.b));
1901
1902 default:
1903 wxFAIL_MSG(wxT("invalid shape!"));
1904 return NULL;
1905 }
1906 }
1907
1908 // ----------------------------------------------------------------------------
1909 // DnDShapeDataObject
1910 // ----------------------------------------------------------------------------
1911
1912 #if wxUSE_METAFILE
1913
1914 void DnDShapeDataObject::CreateMetaFile() const
1915 {
1916 wxPoint pos = m_shape->GetPosition();
1917 wxSize size = m_shape->GetSize();
1918
1919 wxMetaFileDC dcMF(wxEmptyString, pos.x + size.x, pos.y + size.y);
1920
1921 m_shape->Draw(dcMF);
1922
1923 wxMetafile *mf = dcMF.Close();
1924
1925 DnDShapeDataObject *self = (DnDShapeDataObject *)this; // const_cast
1926 self->m_dobjMetaFile.SetMetafile(*mf);
1927 self->m_hasMetaFile = true;
1928
1929 delete mf;
1930 }
1931
1932 #endif // wxUSE_METAFILE
1933
1934 void DnDShapeDataObject::CreateBitmap() const
1935 {
1936 wxPoint pos = m_shape->GetPosition();
1937 wxSize size = m_shape->GetSize();
1938 int x = pos.x + size.x,
1939 y = pos.y + size.y;
1940 wxBitmap bitmap(x, y);
1941 wxMemoryDC dc;
1942 dc.SelectObject(bitmap);
1943 dc.SetBrush(wxBrush(wxT("white"), wxSOLID));
1944 dc.Clear();
1945 m_shape->Draw(dc);
1946 dc.SelectObject(wxNullBitmap);
1947
1948 DnDShapeDataObject *self = (DnDShapeDataObject *)this; // const_cast
1949 self->m_dobjBitmap.SetBitmap(bitmap);
1950 self->m_hasBitmap = true;
1951 }
1952
1953 #endif // wxUSE_DRAG_AND_DROP
1954
1955 // ----------------------------------------------------------------------------
1956 // global functions
1957 // ----------------------------------------------------------------------------
1958
1959 static void ShowBitmap(const wxBitmap& bitmap)
1960 {
1961 wxFrame *frame = new wxFrame(NULL, wxID_ANY, _T("Bitmap view"));
1962 #if wxUSE_STATUSBAR
1963 frame->CreateStatusBar();
1964 #endif // wxUSE_STATUSBAR
1965 DnDCanvasBitmap *canvas = new DnDCanvasBitmap(frame);
1966 canvas->SetBitmap(bitmap);
1967
1968 int w = bitmap.GetWidth(),
1969 h = bitmap.GetHeight();
1970 #if wxUSE_STATUSBAR
1971 frame->SetStatusText(wxString::Format(_T("%dx%d"), w, h));
1972 #endif // wxUSE_STATUSBAR
1973
1974 frame->SetClientSize(w > 100 ? 100 : w, h > 100 ? 100 : h);
1975 frame->Show(true);
1976 }
1977
1978 #if wxUSE_METAFILE
1979
1980 static void ShowMetaFile(const wxMetaFile& metafile)
1981 {
1982 wxFrame *frame = new wxFrame(NULL, wxID_ANY, _T("Metafile view"));
1983 frame->CreateStatusBar();
1984 DnDCanvasMetafile *canvas = new DnDCanvasMetafile(frame);
1985 canvas->SetMetafile(metafile);
1986
1987 wxSize size = metafile.GetSize();
1988 frame->SetStatusText(wxString::Format(_T("%dx%d"), size.x, size.y));
1989
1990 frame->SetClientSize(size.x > 100 ? 100 : size.x,
1991 size.y > 100 ? 100 : size.y);
1992 frame->Show();
1993 }
1994
1995 #endif // wxUSE_METAFILE
1996
1997 #endif // wxUSE_DRAG_AND_DROP || wxUSE_CLIPBOARD