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