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