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