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