Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

As a fan of the cross-platform text editor, Sublime Text 2, I've been doing some research into how it was developed. The developer has noted that it's 99% c++ with some GTK for linux and that it uses a custom UI Toolkit he calls "Sublime GUI". This is a quote from the dev

Sublime Text 2 itself uses a custom UI toolkit. There are a lot of apps where this may not make sense, but it's not such an unreasonable choice for Sublime Text, where I always knew that a lot of the UI controls were going to have to be custom no matter the toolkit (e.g., the text control and tab controls). The UI toolkit sits on top of a cross platform abstraction layer, which is more a union of platform functionality rather than lowest common denominator.

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

I appreciate the benefits of a UI Toolkit, but if I wanted to get my hands dirty and have support for my application on Windows, Linux, Mac, I am not sure where to start.

like image 531
Submerged Avatar asked Jan 20 '13 23:01

Submerged


2 Answers

I guess the most important question is how to draw and get keyboard and mouse events.

As I see it there are two approaches for drawing:

  1. Create an OpenGL context and draw your widgets with OpenGL. Like glui.
  2. Use the native drawing infrastructure. Like GDI+ on windows, XLib on X11.

Of course you would need to implement certain things for each platform. With OpenGL you need to write the context (WGL, GLX, ..) handling for each platform, whereas with the native drawing infrastructure you need much more work. Since all drawing infrastructures are unique, you probably want to write an abstraction for the drawing and then implement your widgets with your drawing abstraction layer.

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

Lastly, you should also have an abstraction layer for creating the main window in which you draw your widgets and from which you get the events.

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

Bear in mind, I have never implemented anything like this. However, I would probably try the OpenGL approach because I believe it is less work to reach the goal.

like image 124
Michael Pfeuti Avatar answered Nov 01 '22 15:11

Michael Pfeuti


There are many GUI toolkits that work across platform (Tk, QT and GTK to name a few)

If you wanted to write your own though, it wouldn't have to be a lower level toolkit than GTK, QT or similar.

You could expose an interface similar to 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 platform-specific sections you could dispatch to a gui toolkit for that platform or use whatever interface is available (ie; X11 for Unix).

When you compile the code you specify the target platform, and this determines which IFDEF sections get compiled in.

Of course this is overly simplified, and great care would have to be taken so that the interface you expose isn't too painful to map onto the native equivalents.

like image 2
cjh Avatar answered Nov 01 '22 13:11

cjh