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