]> git.saurik.com Git - wxWidgets.git/blame - docs/latex/wx/twindowid.tex
fix a typo
[wxWidgets.git] / docs / latex / wx / twindowid.tex
CommitLineData
12ec9c09
VZ
1\section{Window IDs overview}\label{windowidsoverview}
2
3\wxheading{See Also}
4
5\helpref{wxIdManager}{wxidmanager}
6\helpref{wxWindow::NewControlId}{wxwindownewcontrolid}
7\helpref{wxWindow::UnreserveControlId}{wxwindowunreservecontrolid}
8
9\subsection{Introduction}\label{windowidsoverviewintro}
10
11Various contols and other parts of wxWidgets need an ID. Sometimes the
12ID may be directly provided by the use or have a predefined value, such as
5e6ce06a
VZ
13\texttt{wxID\_OPEN}. Often, however, the value of the ID is unimportant and is
14created automatically by calling \helpref{wxWindow::NewControlId}{wxwindownewcontrolid}
15or by passing \texttt{wxID\_ANY} as the ID of an object.
12ec9c09
VZ
16
17There are two ways to generate an ID. One way, is to start at a negative number,
18and for each new ID, return the next smallest number. This is fine for systems
19that can used the full range of negative numbers for an ID, as this provides
20more than enough IDs and it would take a very very long time to run out and
21wrap around. However, some systems can not use the full range of the ID value.
22Windows, for example, can only use 16 bit IDs, and only has about 32000 possible
23automatic IDs that can be generated by \helpref{wxWindow::NewControlId}{wxwindownewcontrolid}.
24If the program runs long enough, depending on the program itself, using this first
25method would cause the IDs to wrap around into the positive ID range and cause possible
26clashes with any directly specified ID values.
27
28The other way is to keep track of the IDs returned by \helpref{wxWindow::NewControlId}{wxwindownewcontrolid}
29and don't return them again until the ID is completely free and not being used by
30any other objects. This will make sure that the ID values do not clash with one
31another. This is accomplished by keeping a reference count for each of the IDs
32that can possibly be returned by \helpref{wxWindow::NewControlId}{wxwindownewcontrolid}.
33Other IDs are not reference counted.
34
35\subsection{Data types}\label{windowidsoverviewtypes}
36
37A wxWindowID is just the integer type for a window ID. It should be used almost
38everywhere. To help keep track of the count for the automatically generated IDs,
39a new type, wxWindowIDRef exists, that can take the place of wxWindowID where needed.
40When an ID is first created, it is marked as reserved. When assigning it to a
41wxWindowIDRef, the usage count of the ID is increased, or set to 1 if it is currently
42reserved. Assigning the same ID to several wxWindowIDRefs will keep track of the count.
43As the wxWindowIDRef gets destroyed or its value changes, it will decrease the count
44of the used ID. When there are no more wxWindowIDRef types with the created ID, the
45ID is considered free and can then be used again by \helpref{wxWindow::NewControlId}{wxwindownewcontrolid}.
46
47If a created ID is not assigned to a wxWindowIDRef, then it remains reserved until it
48is unreserved manually with \helpref{wxWindow::UnreserveControlId}{wxwindowunreservecontrolid}.
49However, if it is assigned to a wxWindowIDRef, then it will be unreserved automatically
50and will be considered free when the count is 0, and should NOT be manually unreserved.
51
52wxWindowIDRef can store both automatic IDs from \helpref{wxWindow::NewControlId}{wxwindownewcontrolid}
53as well as normal IDs. Reference counting is only done for the automatic IDs. Also,
54wxWindowIDRef has conversion operators that allow it to be treated just like a wxWindowID.
55
56\subsection{Using wxWindowIDRef}\label{windowidsoverviewusing}
57
58A wxWindowIDRef should be used in place of a wxWindowID where you want to make sure the
59ID is not created again by \helpref{wxWindow::NewControlId}{wxwindownewcontrolid}
60at least until the wxWindowIDRef is destroyed, usually when the associated object is destroyed.
61This is done already for windows, menu items, and tool bar items.
62It should only be used in the main thread, as it is not thread safe.
63