19

I have been all over the web looking for an answer to this, and my question is this: How does a GUI framework work? for instance how does Qt work, is there any books or wibsites on the topic of writing a GUI framework from scratch? and also does the framework have to call methods from the operating systems GUI framework?

-- Thank you to any one who takes the time to try to answer this question, and forgive me if i misspelled anything.

3
  • 5
    This is a good question, does anyone know of a textbook that covers the 3 or more design approaches to building and actual GUI framework, not just as an api user? Commented Jan 24, 2021 at 8:25
  • 2
    @GrantRostig did you ever find such a textbook? Commented Jan 17, 2022 at 10:40
  • 2
    @SujalSingh, not not even course notes or slides. Commented Jan 20, 2022 at 1:47

2 Answers 2

26

In the old days we did a lot of GUI programming from scratch. It is not as hard as it seems, but it requires a few weeks to come with results.

First you need a good drawing library. Minimal functionality for this library is drawing clipped rectangles (using patterns), lines, bitmaps, and fonts. You can cheat by creating fonts as bitmaps, and clipped rectangle is just a bunch of horizontal lines.

Now you need at least drivers for mouse, keyboard, and timer (if not already provided by the operating system). In general, you will need to detect keys, symbol keys (such as shift, etc.), mouse moves and mouse clicks. Basic timer functions will allow you to detect double clicks.

Then you need to create a window data structure. This data structure needs to have coordinates i.e. a rectangle, link to parent window (if not top window), and window function i.e. the function that will be called when this window should handle some events.

Once you can draw on screen you need some rectangle algebra functions. You need at least good function to calculate intersection of rectangles, and a quick resolution of relative to absolute coordinates. For example - if your child window has parent then its' x and y should recursively be added to parent x and y until you reach top window.

At this point you have your: - primitive graphical functions, - window structure, - mouse driver, keyboard driver, and timer, - rectangle arithmetic.

Now you can write your main event harvesting function. This function will run all the time. It's purpose will be to detect events and send messages to correct windows. What is an event? Well, when you start your program, store mouse x and y coordinates. Then in a loop check if they have changed. If they have changed, find the window at that position ... and send WM_MOUSEMOVE event to it. Your harvesting function should handle: - mouse moves - mouse clicks - mouse double clicks (remember last click and position, measure time and decide if it is a double click or not) - timer events - keyboard buffer changes ...

Now you should be able to send events to windows. But you really need a mechanism for it. It is a combination of message queue, and window procedure. It usually works like this: each window has a window procedure which commonly accepts four arguments: message id (i.e. is it mouse move, is it paint message), window handle, parameter 1 and parameter 2. You can call this window procedure directly using something like a send_message functions. Or you can send this window a message via post_message function. This will put message to the queue and window will process messages one by one, eventually receiving this one. So why should you call one messages directly and put others to the queue? Because of priority. You see, a keyboard click can wait some time before being processed. But a window redraw must complete immediately to prevent flicker and wrong data on screen.

So your harvest_events function sends messages to windows using post_message, and send_message. And your window message pump gets them using typical message pump like this:

while (pmsg = get_message() != NULL) send_message(pmsg->id, pmsg->hwnd, pmsg->p1, pmsg->p2);

get_message simply obtains message from the queue, and calls send message. Simple, huh? Well, not quite so. This way you would only receive driver messages to windows, but you also need some functions to redraw windows, move them, etc.. When you create move_window function, resize_window, show_window, and hide_window function, your window coordinates will change. Parts of other windows will be uncovered (if top window is moved or closed).You need to calculate which windows are affected by coordinate changes and send paint message to those windows (to repaint only the parts that were uncovered - remember, you have clipping drawing functions so this will work).

These functions introduces messages msg_paint, msg_move, msg_resize, msg_hide...

Last, you need to maintain hierarchy of windows. Your top window should be the desktop. It should have child windows (application top windows). These windows may have further child windows (buttons, edit boxes, etc.) The obvious structure for holding these is the window tree. When you detect mouse click you have to traverse window tree and do it in a smart way (finding out who has focus, who is modal, etc.) to send message to the right window. And when you draw you also must traverse all children to see who is uncovered and who is not. Last but not least, you need to handle mouse rectangle as top window to prevent flickering the mouse as windows are re-drawn or (using timers and msg_paint events) animated.

That's roughly it.

3
  • 1
    To map such gui to modern object oriented languages you commonly create window class, remember window handle, maintain window list in static member, and rewire events from static window function to correct class by using modern c++ signals and slots or similar technology. The initial implementation of GUI is typically procedural. But because it is event driver, it is easy to wrap by OO language. I don't think there are books for that. It's the ancient wisdom that can be obtained by old open source libraries such as text DFLAT library or graphical GEM gui library.
    – Tomaz Stih
    Commented Sep 18, 2018 at 13:46
  • 4
    IMO, this answer along with the comment should be the accepted answer. Commented Nov 27, 2018 at 14:51
  • 2
    Does anyone know of a textbook that covers the 3 or more design approaches to building and actual GUI framework, not just as an api user? Commented Jan 24, 2021 at 8:24
8

A GUI framework like Qt generally works by taking the existing OS's primitive objects (windows, fonts, bitmaps, etc), wrapping them in more platform-neutral and less clunky classes/structures/handles, and giving you the functionality you'll need to manipulate them. Yes, that almost always involves using the OS's own functions, but it doesn't HAVE to -- if you're designing an API to draw an OpenGL UI, for example, most of the underlying OS's GUI stuff won't even work, and you'll be doing just about everything on your own.

Either way, it's not for the faint of heart. If you have to ask how a GUI framework works, you're not even close to ready to design one. You're better off sticking with an existing framework and extending it to do the spiffy stuff it doesn't do already.

1
  • Hay thank you for your response, things are starting to make more sense. And I would have to agree with you on that im probably not yet ready to develop one yet, but hopefully someday I will be at that level of understanding. But thanks for your answer it helps.
    – AlexW.H.B.
    Commented May 25, 2010 at 21:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.