wxWidgets core working

61 views
Skip to first unread message

Maciej Wnęk

unread,
Mar 11, 2021, 8:36:16 AM3/11/21
to wx-users
Hello,
I know that wxWidgets framework is based on native Windows API on Windows OS, and
I would like to know how you programmed the core, i.e. how handling events works. I don't know how you do it. I am trying to work with Windows API and every window has to have a window procedure, when I register a WNDCLASS I have to give the window's procedure's address to one of the wndclass fields. And this pointer is a pointer to normal function, which cannot be a member. So how wxWidgets works like this when I create a new window, a new window's procedure is created and also this procedure has to have access to this window's controls, for example a button. Where does the window procedure have these objects from, while buttons and HWND is a member of class, but procedure isn't? I was trying to undestand your code from open source, but it is really hard and I couldn't surround it. Could you explain this to me, please?

Tim Roberts

unread,
Mar 11, 2021, 2:35:28 PM3/11/21
to wx-users
When you register a window class with RegisterClassEx, the structure you pass in has a cbWndExtra member that lets you add a custom region to the window structure.  You access those with GetWindowLongPtr and SetWindowLongPtr.  So, when you create the window, you create your own C++ object, and stuff the pointer into one of the extra regions with SetWindowLongPtr.  Then, you have your window procedure as a static member function.  You have it pull the "this" pointer with GetWindowLongPtr, and call a regular member function to handle the request.  Very, very common approach.

Tim Roberts

unread,
Mar 11, 2021, 2:49:55 PM3/11/21
to wx-users
The other approach is to keep a global table that maps window handles to object pointers.  That happens to be the approach used by wxWidgets.  The function wxFindWinFromHandle does that mapping.

Maciej Wnęk

unread,
Mar 12, 2021, 8:41:13 AM3/12/21
to wx-u...@googlegroups.com
So I have to have a special object which has two members: handle to window and my C++ object, and then I must create an array of these objects?


--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
 
To unsubscribe, send email to wx-users+u...@googlegroups.com
or visit http://groups.google.com/group/wx-users
---
You received this message because you are subscribed to a topic in the Google Groups "wx-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wx-users/RgPiwyMlKXU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to wx-users+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wx-users/561d18cd-d105-4a5d-b7f4-595d7bff2600n%40googlegroups.com.

Maciej Wnęk

unread,
Mar 12, 2021, 8:41:28 AM3/12/21
to wx-u...@googlegroups.com
Thank you very much!!!

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
 
To unsubscribe, send email to wx-users+u...@googlegroups.com
or visit http://groups.google.com/group/wx-users
---
You received this message because you are subscribed to a topic in the Google Groups "wx-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wx-users/RgPiwyMlKXU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to wx-users+u...@googlegroups.com.

Tim Roberts

unread,
Mar 12, 2021, 12:34:45 PM3/12/21
to wx-u...@googlegroups.com
Maciej Wnęk wrote:
>
> So I have to have a special object which has two members: handle to
> window and my C++ object, and then I must create an array of these
> objects?

Essentially.  You need a std::map<HWND, wxWindow*> (or whatever your
base object is).   In wxWidgets, they use a private implementation of
exactly that.

--
Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.


Maciej Wnęk

unread,
Mar 13, 2021, 8:54:43 AM3/13/21
to wx-u...@googlegroups.com
And I must define one wndproc for every window and in the body I must check which my c++ object HWND belongs to?

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.

To unsubscribe, send email to wx-users+u...@googlegroups.com
or visit http://groups.google.com/group/wx-users
---
You received this message because you are subscribed to a topic in the Google Groups "wx-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wx-users/RgPiwyMlKXU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to wx-users+u...@googlegroups.com.

Tim Roberts

unread,
Mar 13, 2021, 10:52:44 PM3/13/21
to wx-users
You define one wndproc for every TYPE of window.  That's why it's done in RegisterClassEx.  All of your buttons have the same behavior, for example.  All of the wndprocs can use the same lookup scheme.

Maciej Wnęk

unread,
Mar 14, 2021, 9:20:30 AM3/14/21
to wx-u...@googlegroups.com
So I every type of the window has to have ITS OWN wndproc or ONE wndproc for all of the windows?

Maciej Wnęk

unread,
Mar 14, 2021, 9:21:54 AM3/14/21
to wx-u...@googlegroups.com
So every type of the window has to have ITS OWN wndproc or ONE wndproc for all of the windows?

niedz., 14 mar 2021 o 04:52 Tim Roberts <ti...@probo.com> napisał(a):

Tim Roberts

unread,
Mar 15, 2021, 12:18:24 PM3/15/21
to wx-users
That's up to you -- whatever makes sense for your application.  In MFC and ATL, for example, there's a basic wndproc defined in the base CWindow class.  Window classes that derive from that typically don't override the wndproc.  Instead, they define a table (the "message map") that maps window messages to member functions in the class.  They could have done that by defining virtual functions, but there are more than a thousand window messages, and they didn't want to force every window object to have a vtable that was a thousand entries long.  In a small project that doesn't have dozens of different kinds of windows, you might have a custom wndproc for each type of window.

PB

unread,
Mar 15, 2021, 1:16:06 PM3/15/21
to wx-users
One can see a simple example of doing something like this here: https://devblogs.microsoft.com/oldnewthing/20050422-08/?p=35813

That example does not use HWND to C++ class window map but the message handling principle is still the same. There is member static s_WndProc() in the base class registered as the window procedure for all the windows.   s_WndProc() translates a HWND to an actual C++ window class and then calls its virtual HandleMessage() so each class can do its own message processing.

You can find many such examples when looking for something like HWND C++ wrapper. They will not be new but this is a technique dating back to 1990s.

Maciej Wnęk

unread,
Mar 15, 2021, 1:47:05 PM3/15/21
to wx-u...@googlegroups.com
But in wxWidgets core there is used technique with one wndproc for every class based on base class wxWindow and there c++ object is found and then there is called his virtual function which handle messages?

--
Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
 
To unsubscribe, send email to wx-users+u...@googlegroups.com
or visit http://groups.google.com/group/wx-users
---
You received this message because you are subscribed to a topic in the Google Groups "wx-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wx-users/RgPiwyMlKXU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to wx-users+u...@googlegroups.com.

Tim Roberts

unread,
Mar 15, 2021, 1:59:28 PM3/15/21
to wx-users
I mentioned all of this before.  Yes, wxWidgets uses a single wndproc.  It's called wxWndProc, in window.cpp.  It does the handle lookup and eventually calls wxWindowMSW::MSWHandleMessage, which has a huge case statement to handle the interesting window messages.  From there, they get translated into wx EVT_XXXX events and dispatched.

Maciej Wnęk

unread,
Mar 15, 2021, 2:05:53 PM3/15/21
to wx-u...@googlegroups.com
In wndproc can I also first check c++ object and then in its HandleMessage check type of message?

Tim Roberts

unread,
Mar 15, 2021, 2:15:21 PM3/15/21
to wx-users
There are an infinite number of ways to organize a window procedure.  We are in no position to direct you to one method or another.  There are very, very, very few people who are creating raw Win32 GUI applications today, because it doesn't make sense.  Virtually everyone uses a framework, like MFC or WTL or wxWidgets or Qt or Windows Forms or WPF, and all of those have dispatchers built in.  There is really no benefit to writing this all from scratch.

Maciej Wnęk

unread,
Mar 15, 2021, 2:36:34 PM3/15/21
to wx-u...@googlegroups.com
Ok thank now I know everything about, I wanted to do it because I programmed in Win32 API a few simple apps, and I didn't know what managing messages works but now I do.

Reply all
Reply to author
Forward
0 new messages