customwidgets.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: customwidgets.h
  3. // Purpose: topic overview
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @page overview_customwidgets Creating a Custom Widget
  9. @tableofcontents
  10. Typically combining the existing @ref group_class_ctrl controls in wxDialogs
  11. and wxFrames is sufficient to fullfill any GUI design. Using the wxWidgets
  12. standard controls makes your GUI looks native on all ports and is obviously
  13. easier and faster.
  14. However there are situations where you need to show some particular kind of
  15. data which is not suited to any existing control. In these cases rather than
  16. hacking an existing control for something it has not been conceived for, it's
  17. better to write a new widget.
  18. @section overview_customwidgets_how Writing a Custom Widget
  19. There are at least two very different ways to implement a new widget.
  20. The first is to build it upon wxWidgets existing classes, thus deriving it from
  21. wxControl or wxWindow. In this way you'll get a @b generic widget. This method
  22. has the advantage that writing a single implementation works on all ports; the
  23. disadvantage is that it the widget will look the same on all platforms, and
  24. thus it may not integrate well with the native look and feel.
  25. The second method is to build it directly upon the native toolkits of the
  26. platforms you want to support (e.g. GTK+, Carbon and GDI). In this way you'll
  27. get a @b native widget. This method in fact has the advantage of a native look
  28. and feel but requires different implementations and thus more work.
  29. In both cases you'll want to better explore some hot topics like:
  30. - @ref overview_windowsizing
  31. - @ref overview_events_custom to implement your custom widget's events.
  32. You will probably need also to gain some familiarity with the wxWidgets
  33. sources, since you'll need to interface with some undocumented wxWidgets
  34. internal mechanisms.
  35. @subsection overview_customwidgets_how_generic Writing a Generic Widget
  36. Generic widgets are typically derived from wxControl or wxWindow.
  37. They are easy to write. The typical "template" is as follows:
  38. @code
  39. enum MySpecialWidgetStyles
  40. {
  41. SWS_LOOK_CRAZY = 1,
  42. SWS_LOOK_SERIOUS = 2,
  43. SWS_SHOW_BUTTON = 4,
  44. SWS_DEFAULT_STYLE = (SWS_SHOW_BUTTON|SWS_LOOK_SERIOUS)
  45. };
  46. class MySpecialWidget : public wxControl
  47. {
  48. public:
  49. MySpecialWidget() { Init(); }
  50. MySpecialWidget(wxWindow *parent,
  51. wxWindowID winid,
  52. const wxString& label,
  53. const wxPoint& pos = wxDefaultPosition,
  54. const wxSize& size = wxDefaultSize,
  55. long style = SWS_DEFAULT_STYLE,
  56. const wxValidator& val = wxDefaultValidator,
  57. const wxString& name = "MySpecialWidget")
  58. {
  59. Init();
  60. Create(parent, winid, label, pos, size, style, val, name);
  61. }
  62. bool Create(wxWindow *parent,
  63. wxWindowID winid,
  64. const wxString& label,
  65. const wxPoint& pos = wxDefaultPosition,
  66. const wxSize& size = wxDefaultSize,
  67. long style = SWS_DEFAULT_STYLE,
  68. const wxValidator& val = wxDefaultValidator,
  69. const wxString& name = wxCollapsiblePaneNameStr);
  70. // accessors...
  71. protected:
  72. void Init() {
  73. // init widget's internals...
  74. }
  75. virtual wxSize DoGetBestSize() const {
  76. // we need to calculate and return the best size of the widget...
  77. }
  78. void OnPaint(wxPaintEvent&) {
  79. // draw the widget on a wxDC...
  80. }
  81. private:
  82. DECLARE_DYNAMIC_CLASS(MySpecialWidget)
  83. DECLARE_EVENT_TABLE()
  84. };
  85. @endcode
  86. @subsection overview_customwidgets_how_native Writing a Native Widget
  87. Writing a native widget is typically more difficult as it requires you to know
  88. the APIs of the platforms you want to support. See @ref page_port_nativedocs
  89. for links to the documentation manuals of the various toolkits.
  90. The organization used by wxWidgets consists in:
  91. - declaring the common interface of the control in a generic header, using
  92. the 'Base' postfix; e.g. MySpecialWidgetBase.
  93. See for example the wxWidgets' @c "wx/button.h" file.
  94. - declaring the real widget class inheriting from the Base version in
  95. platform-specific headers; see for example the wxWidgets' @c "wx/gtk/button.h" file.
  96. - separating the different implementations in different source files, putting
  97. all common stuff in a separate source.
  98. See for example the wxWidgets' @c "src/common/btncmn.cpp", @c "src/gtk/button.cpp"
  99. and @c "src/msw/button.cpp" files.
  100. */