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