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