C ++ custom UI ToolKit - Options for cross platform abstraction layer

As a fan of the cross-platform text editor Sublime Text 2, I did some research on how it was developed. The developer noted that he is 99% C ++ with some GTK for Linux and that he uses a custom UI toolkit that he calls the "Sublime GUI". This is a quote from dev

Sublime Text 2 uses its own set of user interface tools. There are many applications where this may not make sense, but this is not such an unreasonable choice for Sublime Text, where I always knew that many user interface elements should be customized regardless of the toolkit (for example, text control and tab controls). The UI toolkit is on top of the cross-platform abstraction layer, which is more a combination of the platform’s functionality, rather than the lowest common denominator.

My question is, what are some possibilities for the cross platform abstraction layer? I assume this is at a lower level than GTK, QT, SDL. I'm trying to figure out how to create a custom UI toolkit that will be cross-platform and will only write code once.

I appreciate the benefits of the UI Toolkit, but if I want my hands to be dirty and support my application on Windows, Linux, Mac, I'm not sure where to start.

+7
source share
2 answers

I think the most important question is how to draw and receive keyboard and mouse events.

As I can see, there are two approaches to drawing:

  • Create an OpenGL context and draw your widgets using OpenGL. Like glui .
  • Use your own drawing infrastructure. Like GDI + on windows, XLib on X11.

Of course, you will need to implement certain things for each platform. With OpenGL, you need to write context processing (WGL, GLX, ..) for each platform, while with your own drawing infrastructure you will need much more work. Since all the drawn infrastructures are unique, you probably want to write an abstraction for the drawing, and then implement your widgets using the level of abstraction of the drawing.

As for event handling, I think you will also need to write your own abstraction, because event processing is unique to each platform.

Finally, you must also have an abstraction layer to create the main window in which you draw your widgets and from which you get events.

When working with OpenGL, you can start with glut , which already handles window creation and event handling.

Remember, I have never implemented anything like this. Nevertheless, I would probably try to use the OpenGL approach, because I believe that there is less effort to achieve the goal.

+4
source

There are many GUI tools that work on the platform (Tk, QT and GTK, to name a few)

If you wanted to write your own, then it should not have been tools of a lower level than GTK, QT or the like.

You can open an interface like this

void draw_window(mywindow *mw, char *name){ non platform specific code goes here (maybe arg parsing, etc.) #IFDEF windows windows specific code goes here #ENDIF #IFDEF macosx mac specific code goes here #ENDIF #IFDEF linux linux specific code goes here #ENDIF non platform specific code goes here (tidying up, recording state, etc.) } 

Within each of the sections related to the platform, you can send gui tools for this platform to the toolkit or use any available interface (for example, X11 for Unix).

When compiling the code that you specify the target platform, and this determines which IFDEF sections will be compiled.

Of course, this is too simplified, and great care must be taken so that the interface you are revealing is not too painful to compare with native equivalents.

+1
source

All Articles