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