Merge branch 'id-docs'

Improve documentation of window and menu item IDs.

Closes https://github.com/wxWidgets/wxWidgets/pull/890
This commit is contained in:
Vadim Zeitlin
2018-08-27 02:15:53 +02:00
4 changed files with 55 additions and 13 deletions

View File

@@ -11,18 +11,36 @@
@tableofcontents @tableofcontents
Various controls and other parts of wxWidgets need an ID. Sometimes the ID may When creating a new wxWindow-derived class or adding a menu item, its ID must
be directly provided by the user or have a predefined value, such as be specified. An ID is just a unique (at least locally, i.e. inside the same
@c wxID_OPEN. Often, however, the value of the ID is unimportant and in this top level window) integer allowing to find the window or menu item later and to
case it is enough to use @c wxID_ANY as the ID of an object which tells distinguish between events from different objects.
wxWidgets to assign an ID automatically. All such automatically-assigned IDs
are negative, so the IDs predefined in the user code should always be positive If applicable, stock IDs such as ::wxID_EXIT (for menu item) or ::wxID_OK (for
to avoid clashes with them. a button) should be used, see @ref page_stockitems for the full list of such
IDs.
If the value of an ID is not important, ::wxID_ANY should be used, telling
wxWidgets to allocate a unique ID automatically. All such
automatically-assigned IDs are negative and so won't conflict with any
user-defined IDs as long as they are positive.
If you do care about the ID value but don't want to specify it as a literal in If you do care about the ID value but don't want to specify it as a literal in
your code, you can use wxWindow::NewControlId() to create an ID that had never your code, you can use wxWindow::NewControlId() to create an ID that had never
been returned by this function before and, being also negative, never conflicts been returned by this function before. Such IDs are also negative.
with any IDs explicitly defined in the program if the advice above is followed.
Finally, you can just define your own IDs. Typically this is done by using a
C++ enum to automatically ensure their uniqueness. If you do this, please note
that your custom IDs must be positive to avoid clashes with the automatically
assigned IDs discussed above and should @e not have values 0 or 1, that can
result in surprising behaviour under some platforms. Finally, you also need to
avoid defining IDs in the range from ::wxID_LOWEST to ::wxID_HIGHEST which is
reserved for wxWidgets-defined IDs, see ::wxStandardID for more details.
To avoid all these restrictions, it is best to avoid using hard-coded IDs at
all, they are not needed when using wxEvtHandler::Bind() for event handling
(unlike with the previously used event tables).
@see wxIdManager, wxWindow::NewControlId(), wxWindow::UnreserveControlId() @see wxIdManager, wxWindow::NewControlId(), wxWindow::UnreserveControlId()

View File

@@ -557,20 +557,27 @@ enum wxBackgroundStyle
Notice that some, but @em not all, of these IDs are also stock IDs, i.e. Notice that some, but @em not all, of these IDs are also stock IDs, i.e.
you can use them for the button or menu items without specifying the label you can use them for the button or menu items without specifying the label
which will be provided by the underlying platform itself. See @ref page_stockitems "the which will be provided by the underlying platform itself. See
list of stock items" for the subset of standard IDs which are stock IDs as well. @ref page_stockitems "the list of stock items" for the subset of standard
IDs which are stock IDs as well.
*/ */
enum wxStandardID enum wxStandardID
{ {
/** /**
This id delimits the lower bound of the range used by automatically-generated ids This id delimits the lower bound of the range used by automatically-generated ids
(i.e. those used when wxID_ANY is specified during construction). (i.e.\ those used when wxID_ANY is specified during construction).
It is defined as a relatively large negative number and its exact value
is platform-dependent.
*/ */
wxID_AUTO_LOWEST, wxID_AUTO_LOWEST,
/** /**
This id delimits the upper bound of the range used by automatically-generated ids This id delimits the upper bound of the range used by automatically-generated ids
(i.e. those used when wxID_ANY is specified during construction). (i.e.\ those used when wxID_ANY is specified during construction).
It is defined as a relatively small negative number and its exact value
is platform-dependent.
*/ */
wxID_AUTO_HIGHEST, wxID_AUTO_HIGHEST,
@@ -590,6 +597,11 @@ enum wxStandardID
*/ */
wxID_ANY = -1, wxID_ANY = -1,
/**
Start of the range reserved for wxWidgets-defined IDs.
Don't define custom IDs in the range from wxID_LOWEST to wxID_HIGHEST.
*/
wxID_LOWEST = 4999, wxID_LOWEST = 4999,
wxID_OPEN, wxID_OPEN,
@@ -737,6 +749,15 @@ enum wxStandardID
/** IDs used by generic file ctrl (4 consecutive starting from this value) */ /** IDs used by generic file ctrl (4 consecutive starting from this value) */
wxID_FILECTRL = 5950, wxID_FILECTRL = 5950,
/**
End of the range reserved for wxWidgets-defined IDs.
Don't define custom IDs in the range from wxID_LOWEST to wxID_HIGHEST.
When using an enum to define a number of custom IDs, assigning the
value of @c wxID_HIGHEST+1 to the first element ensures that none of
the enum elements will conflict with any standard IDs.
*/
wxID_HIGHEST = 5999 wxID_HIGHEST = 5999
}; };

View File

@@ -548,6 +548,8 @@ public:
@param id @param id
The menu command identifier. The menu command identifier.
See @ref overview_windowids for more information about IDs (same
considerations apply to both window and menu item IDs).
@param item @param item
The string to appear on the menu item. The string to appear on the menu item.
See wxMenuItem::SetItemLabel() for more details. See wxMenuItem::SetItemLabel() for more details.

View File

@@ -358,6 +358,7 @@ public:
Pointer to a parent window. Pointer to a parent window.
@param id @param id
Window identifier. If wxID_ANY, will automatically create an identifier. Window identifier. If wxID_ANY, will automatically create an identifier.
See @ref overview_windowids for more information about IDs.
@param pos @param pos
Window position. wxDefaultPosition indicates that wxWidgets Window position. wxDefaultPosition indicates that wxWidgets
should generate a default position for the window. should generate a default position for the window.