Window message procedures in Linux vs Windows - c++

In Windows when you create a window, you must define a (c++)
LRESULT CALLBACK message_proc(HWND Handle, UINT Message, WPARAM WParam, LPARAM LParam);
to handle all the messages sent from the OS to the window, like keypresses and such.
Im looking to do some reading on how the same system works in Linux. Maybe it is because I fall a bit short on the terminology but I fail to find anything on this through google (although Im sure there must be plenty!).
Is it still just one single C function that handles all the communication?
Does the function definition differ on different WMs (Gnome, KDE) or is it handled on a lower level in the OS?
Edit: Ive looked into tools like QT and WxWidgets, but those frameworks seems to be geared more towards developing GUI extensive applications. Im rather looking for a way to create a basic window (restrict resize, borders/decorations) for my OGL graphics and retrieve input on more than one platform. And according to my initial research, this kind of function is the only way to retrieve that input.
What would be the best route? Reading up, learning and then use QT or WxWidgets? Or learning how the systems work and implement those few basic features I want myself?

Well at the very basic level you have the X Window protocol http://en.wikipedia.org/wiki/X_Window_System_core_protocol, which we can be pretty complex to handle if you want to do any application. Next on the stack there's Xlib http://en.wikipedia.org/wiki/Xlib which is a "convenient" wrapper around the X protocol, but still is complex for "real life" applications. It's on top of Xlib that most other frameworks are built, trying to simplify application development. The most know are: Xt, Gtk, Qt, etc.
Like in window you have a "event loop", and if you want you can implement on top of it a GetMessage/DispachMessage metaphor to mimic the windows behavior. That way you may have a WNDPROC, but natively X doesn't provide such thing.
Before reinventing the wheel is preferable to take a look at similar applications, what they are using.
If you need something simple you can try SDL http://www.libsdl.org/, which is a cross platform library to aimed to develop games/simple applications. Another alternative is Allegro game library http://www.talula.demon.co.uk/allegro/.

In principle it is absolutely the same. However, it has nothing to do with communication with the OS (nor does it on win32, using user32.dll is entirely optional)
A GUI application has an event loop somewhere, which processes messages from a queue at some level.
There are a lot of libraries typically used to "hide" this behaviour - you can use them (and indeed, you should). If anything, the Xlib event system is even more perverse than Win32's user32.dll one, and is less widely understood, therefore fewer people use it directly.
In Linux or in Windows, applications can use the low-level GUI, or can use a library. Most use a library. Applications can also choose to do neither and operate without a GUI (server applications typically do this). Applications can create multiple threads, one of which sits in an event loop, and others work differently. This is a popular approach too.
Most GUI applications use a higher level library for their GUI
Non-interactive applications, e.g. server applications, don't use the GUI at all and don't use the libraries (e.g. XLib, user32.dll)
Applications which don't lend themselves to an "Event loop" (e.g. Games) typically use a separate thread to process their event loop.
These things are largely true on Win32 and Linux.

It's totally and utterly different. That window procedure is 100% specific to the Windows OS. For linux, it will depend on the window manager (gnome, kde - as you've already mentioned). If you wish to do cross-platform development, you might want to look at things like QT.
You may wish to take a look at the following URLs:
http://www.qtsoftware.com/products/appdev
http://en.wikipedia.org/wiki/Qt_toolkit

As stated by xhantt, what transport the equivalent messages you are looking for is the X Window System. Which, indeed, can be a bit complex.
With XLib you will need to handle the events registering and dequeuing in your main loop. See the XLib manual for a complete description on how to proceed. But don't forget that you will only catch window and inputs events this way. Not every OS messages.
You can also look for XCB which is a newer, and probably easier, library.
If you build your application on top of those two library, it will run smoothly under (almost, we can never be too sure) every WM. And you won't require any dependency that most linux user don't already have on their installation. If you go with Qt, GTK, etc... It will be easier and work under any WM, but they may not have library installed.

Related

developing GUI application without using GUI toolkit

Is it possible to write GUI application without using GUI toolkit ? As the GUI toolkit like GTK+ itself is written in c language, when there were no such toolkits at starting so how could programmers developed GUI apps only using c or c++ without using such toolkits? How can one write Gui application in c or c++ without using any GUI toolkit?
You can program Windows GUI applications using the Win32 API directly, without using any separate toolkit like GTK+. One reference on how to do that is here: http://www.winprog.org/tutorial/start.html
It's not so common these days, and not for the faint of heart.
Doing GUI stuff at the API level in Windows is not difficult, but involves a lot of work.
As a starting point you can check out my old Windows API programming tutorial “Lessons in Windows API Programming (C++)”.
Going that route you would do well to obtain a copy of the 5th edition or earlier (not 6th or later) of Charles Petzold’s “Programming Windows”, which is considered the Bible on the subject.
You start with a frame buffer for the graphics, upon that you write a set of primitive functions to do basic geometry (lines, circles, polygons, bit copies). Then you create an event queue, and a way to populate it with input events (keyboard, mouse, etc.).
You'll also need to create font and text routines.
Those are the basics upon which any GUI are built, as basic guis are little more than boxes that take click events, and eventually keyboard events.
It's a lot of work.
If you want to look at GUI programming at a lower level, consider looking up are it was originally done in the primitve OSes (such as early Windows, early Mac OS, early X Windows).
Mac OS made much of the work explicit. It offered a Window Manager, and other high level controls, but with a bit of study you can see how these were built on top of Quickdraw (MacOS graphics primitive library).
None of this addresses the modern issue of GPU acceleration and the like, that's a completely different layer of complexity to the problem.

Creating many native GUI frontends for a cross-platform application

I've been away from GUI programming for quite some time so please pardon my ignorance.
I would like to attempt the following:
Write a Mac OSX app but still be able to port to Win/Linux (i.e. C++ core with Obj-C GUI)
Avoid Qt/other toolkits on OSX (i.e. talk to Cocoa directly - I feel that many Qt apps I use stick out like sore thumbs compared to the rest of my system)
Not as important, but it would be nice to avoid Visual Studio if it means I can have the freedom to use newer C++ features even on Windows if they help create better code.
I believe this configuration might get me what I'm looking for:
Core C++ Static Library
OSX GUI (Cocoa)
Windows GUI (Qt+MinGW?) OR (no new C++ features, Visual Studio + ManagedC++/C#/????)
Linux GUI (Qt)
Once again, sorry for my ignorance but is this possible? Is this sane? Are there any real-world open source examples accomplish something like this?
There is quite a few OS X applications that have completely custom-designed looks that don't use very many stock controls. iStat Menus comes to mind, but there are many other examples. They still look good, but it's done by manually designing them to look good and to "mesh" with the overall look of OS X applications. Even their preferences pane doesn't use stock buttons.
Thus, you can go quite far using Qt, you just have to pay close attention to what you're doing - similarly to the way other developers are paying close attention even when using Cocoa. You'll find that Qt's controls offer functionality often above and beyond what's offered in Cocoa.
That said, on OS X sometimes you may need to run some native code that expects a CFRunLoop to be present. It's good to know that Qt's event loop already spins a runloop for you, so as long as you have an event loop spinning in a given thread, you can use runloop-based code - the default runloop is provided by Qt's implementation of QEventDispatcher (somewhere in its guts). For non-gui threads, the unmodified QThread does it for you. This is useful for using asynchronous IOKit functionality, for example. Another answer of mine presents some Cocoa mouse event grabbing code. A previous version that used Carbon can be found in the edit history of that answer.
Same goes for Windows: Qt runs a message sink for all top-level windows it owns, and you can integrate native controls/windows using qtwinmigrate. You can also integrate ActiveX controls using the Active Qt framework.
Well I think you should try Qt even on OSX. Qt allows native/custom look of applications (those cases you mentioned are probably bad examples - you probably haven't noticed that lots of other applications also use Qt).
Tools I usually use for multi-platform development:
C++ (now C++11 since all major compilers more or less support it)
Boost
Qt
CMake as build system generator
If you use this tool-set you can choose whichever platform you like for development and still be multi-platform without extensive work on the other platforms.

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.
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:
Create an OpenGL context and draw your widgets with OpenGL. Like glui.
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.
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.

C++ UI framework from scratch?

I want to create a C++ UI framework (something like QT or like ubuntu unity Desktop)
How is programmed , is it using OpenGL or lets take plasma ui of QT (how is this programmed )?
Direct answers , reference links anything will be helpful.
Some interesting opengl based UI I founf on the web
LiquidEngine
http://www.youtube.com/watch?v=k0saaAIjIEY
Libnui
en.wikipedia.org/wiki/Libnui
Some UI frameworks render everything themselves, and work based on some kind of clipping-window-within-the-host-systems-screen. Non-display aspects (such as input event handling) have to be translated to/from the host systems underlying APIs.
Some UI frameworks translate as much as possible to some underlying framework.
wxWidgets can do both. You can choose a native version (e.g. wxMSW if you're on Windows) and most wxWidgets controls will be implemented using native Windows controls. Equally, you can choose the wxUniversal version, where all controls are implemented by the wxWidgets library itself.
The trouble is that typical GUI frameworks are huge. If you want a more manageable example to imitate, you might look at FLTK. I haven't got around to studying it myself, but it has a reputation for being consise.
There are also some GUI toolkits that are specifically aimed at games programming, such as Crazy Eddies GUI. My guess - these are probably as idependent of the underlying API as possible, so that particular applications can implement the mapping to whichever underlying API they happen to target (OpenGL, DirectX, SDL, whatever) and can be the boss of the GUI rather than visa versa.
http://www.wxwidgets.org/
http://www.fltk.org/
http://www.cegui.org.uk/wiki/index.php/Main_Page
"no really, don't write your own wm or toolkit"
The #Xorg-devel guys on irc.freenode.org
doing one anyway means that you have to test against a wide range of more or less buggy WMs and X implementations, and that you have to frequently update to be compatible with the latest Xorg server and X protocol features (like Xinput 2.1)
understandably, the Xorg people are tired to support old, unmaintained toolkits and applications. They already have enough bugs.
The GUI frameworks are very dependant on a windows system, which dictates what is allowed and how windows are created and rendered. For example, pass a specific option to create a borderless or full-screen window.
Since you mentioned opengl and ubuntu, I guess you want to start on a linux platform. You should study xlib, for which you can find reference here.
Since the qt library is open source, you can download it and peek into it's sources.
A UI library isn't developed from scratch. It relies on the OS' windowing system, which relies on the driver from your graphics adapter, which relies on the OS kernel, which relies on... and so on.
To develop any software "from scratch", you can start by writing your own BIOS. Once you're done with that, move on to writing an OS, and then you should be just about ready to write the software you wanted. Good luck.
And this is assuming you're willing to cheat, of course, and use a compiler you didn't write from scratch.
Before you do that, it's worth that you spend one week on thinking:
1, Do you really know how to do it? I doubt that.
2, Do you really need to do it? I doubt that too.

Touch friendly GUI in Windows Mobile

I'm porting an audio processing application written in C++ from Windows to Windows Mobile (version 5+). Basically what I need to port is the GUI. The application is quite complicated and the GUI will need to be able to offer a lot of functionality. I would like to create a touch friendly user interface that also looks good. Which basically means that standard WinMo controls are out the window.
I've looked at libraries such as Fluid and they look like something I would like to use. However, as I said I'm developing i C++. Even though it would be possible to only write the GUI part i some .NET language I rather not. My experience with .NET on Windows Mobile is that it doesn't work very well...
Can anyone either suggest a C/C++ touch friendly GUI library for Windows Mobile or some kind of "best practices" document/how-to on how to use the standard Windows Mobile controls in order to make the touch friendly and also work and look well in later versions of Windows Mobile (in particular version 6.5)?
There are two aspects to your question:
Libraries. For this I would take a look at Qt for CE/WinMo. The C++ alternative is MFC.
GUI Design. About Face and Designing Interfaces (J. Tidwell) are a couple of good books.
Also:
make sure that your UI is finger-friendly, I hate it when I have to use a stylus.
keep in mind that on touch screens you can't have tooltips (no mouse over) and you don't have a mouse pointer. WinMo uses click and long click, but the latter is not easily discoverable.
add joystick UI navigation
don't try to cram too many controls on the tiny screen, use tabs or drill-down menus
I don't know any good C++ libs but you could try SlideUI mobile controls (it is in .NET), but you wouldn’t need any specific knowledge to use it and it's available via design time and easy to use.
http://www.devslide.com/products/slideui
Disclosure: I am affiliated with devslide.