]>
git.saurik.com Git - wxWidgets.git/blob - docs/doxygen/overviews/customwidgets.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: customwidgets.h
3 // Purpose: topic overview
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
10 @page overview_customwidgets Creating a Custom Widget
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
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.
26 @section overview_customwidgets_how Writing a Custom Widget
28 There are at least two very different ways to implement a new widget.
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.
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.
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.
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
51 @subsection overview_customwidgets_how_generic Writing a Generic Widget
53 Generic widgets are typically derived from wxControl or wxWindow.
54 They are easy to write. The typical "template" is as follows:
57 enum MySpecialWidgetStyles
63 SWS_DEFAULT_STYLE = (SWS_SHOW_BUTTON|SWS_LOOK_SERIOUS)
66 class MySpecialWidget : public wxControl
69 MySpecialWidget() { Init(); }
71 MySpecialWidget(wxWindow *parent,
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")
82 Create(parent, winid, label, pos, size, style, val, name);
85 bool Create(wxWindow *parent,
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);
99 // init widget's internals...
102 virtual wxSize DoGetBestSize() const {
103 // we need to calculate and return the best size of the widget...
106 void OnPaint(wxPaintEvent&) {
107 // draw the widget on a wxDC...
111 DECLARE_DYNAMIC_CLASS(MySpecialWidget)
112 DECLARE_EVENT_TABLE()
118 @subsection overview_customwidgets_how_native Writing a Native Widget
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.
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.
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.
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.