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