Developing a GUI environment in home-made OS

Coder404 picture Coder404 · Jan 22, 2012 · Viewed 7.9k times · Source

I have made a desktop os with a kernel in c that prints "hello world". I am trying to make a GUI (custom window manager, buttons) for my os in c but I am having trouble. I looked at one tutorial:

http://www.osdever.net/tutorials/view/gui-development

Are there any types of GUI tutorials that are for a desktop operating system in C?

p.s. no Linux and no DOS. only C standard.

Answer

Alexey Frunze picture Alexey Frunze · Jan 23, 2012

I don't think you need a special tutorial here. The core of the most basic GUI consists of managing a list of rectangular objects that represent windows, buttons, pictures, textboxes, etc.

Every rectangular object like that has its x- and y-coordinates and dimensions (width and height). It also has a z (depth) coordinate that tells what objects are below it (their z's are smaller) and what objects are above it (their z's are greater).

Every rectangular object also has a pointer to its parent and to its children. This makes it easy to compose arbitrarily complex windows of smaller and simpler rect. objects. For example, when you grab a window and move it, using these parent/children pointers you can move all objects. Or, if the outer object receives an event, it can forward it to its inner children for handling and vice versa.

When it comes to rendering all these rectangular objects, some of which can be partially or fully obscured by others, the most important thing is to figure out which of all the objects are visible, invisible and partially visible because you don't want to do a lot of unnecessary work. To efficiently draw objects you want to draw every pixel at most once (always or most of the time). In addition to the rectangle-intersecting/subdividing code that'll be needed, this also suggests that every object know how to draw any arbitrary rectangular portion of itself efficiently. This is most trivial for solid-color objects. For pictures it's more or less straightforward (unless you want to have image scaling and color reduction/transformation in-place). For text and vector objects it's hardest.

You can even compose your mouse pointer object out of small rectangular objects and have it drawn and redrawn by the same code as for all other objects. Just make sure the pointer's z (depth) coordinate is such that the pointer is always on top of all other objects.

That's the general idea.