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