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