dragimag.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: dragimag.h
  3. // Purpose: wxDragImage sample
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 28/2/2000
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_DRAGIMAGSAMPLE_
  11. #define _WX_DRAGIMAGSAMPLE_
  12. // derived classes
  13. class MyFrame;
  14. class MyApp;
  15. class MyCanvas;
  16. class DragShape;
  17. // MyFrame
  18. class MyFrame: public wxFrame
  19. {
  20. public:
  21. MyFrame();
  22. void OnAbout( wxCommandEvent &event );
  23. void OnQuit( wxCommandEvent &event );
  24. MyCanvas* GetCanvas() const { return m_canvas; }
  25. void SetCanvas(MyCanvas* canvas) { m_canvas = canvas; }
  26. private:
  27. MyCanvas* m_canvas;
  28. wxDECLARE_DYNAMIC_CLASS(MyFrame);
  29. wxDECLARE_EVENT_TABLE();
  30. };
  31. // MyApp
  32. class MyApp: public wxApp
  33. {
  34. public:
  35. MyApp();
  36. virtual bool OnInit();
  37. virtual int OnExit();
  38. //// Operations
  39. // Tile the bitmap
  40. bool TileBitmap(const wxRect& rect, wxDC& dc, wxBitmap& bitmap);
  41. //// Accessors
  42. wxBitmap& GetBackgroundBitmap() const { return (wxBitmap&) m_background; }
  43. bool GetUseScreen() const { return m_useScreen; }
  44. void SetUseScreen(bool useScreen) { m_useScreen = useScreen; }
  45. void OnUseScreen(wxCommandEvent& event);
  46. protected:
  47. wxBitmap m_background;
  48. bool m_useScreen;
  49. DECLARE_EVENT_TABLE()
  50. };
  51. DECLARE_APP(MyApp)
  52. #define TEST_USE_SCREEN 100
  53. // MyCanvas
  54. // Dragging modes
  55. #define TEST_DRAG_NONE 0
  56. #define TEST_DRAG_START 1
  57. #define TEST_DRAG_DRAGGING 2
  58. class MyCanvas: public wxScrolledWindow
  59. {
  60. public:
  61. MyCanvas( wxWindow *parent, wxWindowID, const wxPoint &pos, const wxSize &size );
  62. ~MyCanvas();
  63. void OnPaint( wxPaintEvent &event );
  64. void OnEraseBackground(wxEraseEvent& event);
  65. void OnMouseEvent(wxMouseEvent& event);
  66. void DrawShapes(wxDC& dc);
  67. void EraseShape(DragShape* shape, wxDC& dc);
  68. void ClearShapes();
  69. DragShape* FindShape(const wxPoint& pt) const;
  70. wxList& GetDisplayList() { return m_displayList; }
  71. protected:
  72. private:
  73. wxList m_displayList; // A list of DragShapes
  74. int m_dragMode;
  75. DragShape* m_draggedShape;
  76. DragShape* m_currentlyHighlighted; // The shape that's being highlighted
  77. wxPoint m_dragStartPos;
  78. wxDragImage* m_dragImage;
  79. wxDECLARE_ABSTRACT_CLASS(MyCanvas);
  80. wxDECLARE_EVENT_TABLE();
  81. };
  82. // Ways to drag a shape
  83. #define SHAPE_DRAG_BITMAP 1
  84. #define SHAPE_DRAG_TEXT 2
  85. #define SHAPE_DRAG_ICON 3
  86. // Shape
  87. class DragShape: public wxObject
  88. {
  89. public:
  90. DragShape(const wxBitmap& bitmap);
  91. ~DragShape(){};
  92. //// Operations
  93. bool HitTest(const wxPoint& pt) const;
  94. bool Draw(wxDC& dc, bool highlight = false);
  95. //// Accessors
  96. wxPoint GetPosition() const { return m_pos; }
  97. void SetPosition(const wxPoint& pos) { m_pos = pos; }
  98. wxRect GetRect() const { return wxRect(m_pos.x, m_pos.y, m_bitmap.GetWidth(), m_bitmap.GetHeight()); }
  99. wxBitmap& GetBitmap() const { return (wxBitmap&) m_bitmap; }
  100. void SetBitmap(const wxBitmap& bitmap) { m_bitmap = bitmap; }
  101. int GetDragMethod() const { return m_dragMethod; }
  102. void SetDragMethod(int method) { m_dragMethod = method; }
  103. bool IsShown() const { return m_show; }
  104. void SetShow(bool show) { m_show = show; }
  105. protected:
  106. wxPoint m_pos;
  107. wxBitmap m_bitmap;
  108. int m_dragMethod;
  109. bool m_show;
  110. };
  111. // MyDragImage
  112. // A derived class is required since we're overriding UpdateBackingFromWindow,
  113. // for compatibility with Mac OS X (Core Graphics) which does not support blitting
  114. // from a window.
  115. class MyDragImage: public wxDragImage
  116. {
  117. public:
  118. MyDragImage(MyCanvas* canvas): m_canvas(canvas) {}
  119. MyDragImage(MyCanvas* canvas, const wxBitmap& image, const wxCursor& cursor = wxNullCursor):
  120. wxDragImage(image, cursor), m_canvas(canvas)
  121. {
  122. }
  123. MyDragImage(MyCanvas* canvas, const wxIcon& image, const wxCursor& cursor = wxNullCursor):
  124. wxDragImage(image, cursor), m_canvas(canvas)
  125. {
  126. }
  127. MyDragImage(MyCanvas* canvas, const wxString& str, const wxCursor& cursor = wxNullCursor):
  128. wxDragImage(str, cursor), m_canvas(canvas)
  129. {
  130. }
  131. // On some platforms, notably Mac OS X with Core Graphics, we can't blit from
  132. // a window, so we need to draw the background explicitly.
  133. virtual bool UpdateBackingFromWindow(wxDC& windowDC, wxMemoryDC& destDC, const wxRect& sourceRect,
  134. const wxRect& destRect) const;
  135. protected:
  136. MyCanvas* m_canvas;
  137. };
  138. #endif
  139. // _WX_DRAGIMAGSAMPLE_