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