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