]>
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
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
11 @page overview_customwidgets Creating a custom widget
13 @li @ref overview_customwidgets_whenwhy
14 @li @ref overview_customwidgets_how
19 @section overview_customwidgets_whenwhy When and why you should write your custom widget
21 Typically combining the existing @ref group_class_ctrl controls in wxDialogs and
22 wxFrames is sufficient to fullfill any GUI design.
23 Using the wxWidgets standard controls makes your GUI looks native on all ports
24 and is obviously easier and faster.
26 However there are situations where you need to show some particular kind of data
27 which is not suited to any existing control.
28 In these cases rather than hacking an existing control for something it has not
29 been conceived for, it's better to write a new widget.
32 @section overview_customwidgets_how How to write the custom widget
34 There are at least two very different ways to implement a new widget.
36 The first is to build it upon wxWidgets existing classes, thus deriving it from
37 wxControl or wxWindow. In this way you'll get a @b generic widget.
38 This method has the advantage that writing a single
39 implementation works on all ports; the disadvantage is that it the widget will
40 look the same on all platforms, and thus it may not integrate well with the
43 The second method is to build it directly upon the native toolkits of the platforms you
44 want to support (e.g. GTK+, Carbon and GDI). In this way you'll get a @b native widget.
45 This method in fact has the advantage of a native look and feel but requires different
46 implementations and thus more work.
48 In both cases you'll want to better explore some hot topics like:
49 - @ref overview_windowsizing
50 - @ref overview_events_custom to implement your custom widget's events.
51 You will probably need also to gain some familiarity with the wxWidgets sources,
52 since you'll need to interface with some undocumented wxWidgets internal mechanisms.
55 @subsection overview_customwidgets_how_generic Writing a generic widget
57 Generic widgets are typically derived from wxControl or wxWindow.
58 They are easy to write. The typical "template" is as follows:
62 enum MySpecialWidgetStyles
68 SWS_DEFAULT_STYLE = (SWS_SHOW_BUTTON|SWS_LOOK_SERIOUS)
71 class MySpecialWidget : public wxControl
74 MySpecialWidget() { Init(); }
76 MySpecialWidget(wxWindow *parent,
78 const wxString& label,
79 const wxPoint& pos = wxDefaultPosition,
80 const wxSize& size = wxDefaultSize,
81 long style = SWS_DEFAULT_STYLE,
82 const wxValidator& val = wxDefaultValidator,
83 const wxString& name = "MySpecialWidget")
87 Create(parent, winid, label, pos, size, style, val, name);
90 bool Create(wxWindow *parent,
92 const wxString& label,
93 const wxPoint& pos = wxDefaultPosition,
94 const wxSize& size = wxDefaultSize,
95 long style = SWS_DEFAULT_STYLE,
96 const wxValidator& val = wxDefaultValidator,
97 const wxString& name = wxCollapsiblePaneNameStr);
104 // init widget's internals...
107 virtual wxSize DoGetBestSize() const {
108 // we need to calculate and return the best size of the widget...
111 void OnPaint(wxPaintEvent&) {
112 // draw the widget on a wxDC...
116 DECLARE_DYNAMIC_CLASS(MySpecialWidget)
117 DECLARE_EVENT_TABLE()
123 @subsection overview_customwidgets_how_native Writing a native widget
125 Writing a native widget is typically more difficult as it requires you to
126 know the APIs of the platforms you want to support.
127 See @ref page_port_nativedocs for links to the documentation manuals of the
130 The organization used by wxWidgets consists in:
131 - declaring the common interface of the control in a generic header, using
132 the 'Base' postfix; e.g. MySpecialWidgetBase.
133 See for example the wxWidgets' @c "wx/button.h" file.
135 - declaring the real widget class inheriting from the Base version in
136 platform-specific headers; see for example the wxWidgets' @c "wx/gtk/button.h" file.
138 - separing the different implementations in different source files, putting
139 all common stuff in a separate source.
140 See for example the wxWidgets' @c "src/common/btncmn.cpp", @c "src/gtk/button.cpp"
141 and @c "src/msw/button.cpp" files.