I am struggling to find a good tutorial on how a Mac C++ GUI application is structured. Coming from Windows programming I'm used to message loops and window handles... is it comparable on Macs or totally different?
Any links or examples are welcome, particularly those aimed at transitioning from Win32 rather than assuming I'm a noob to programming in general.
update: I should point out this is for a game-like application so I don't need to access common controls; I essentially just need a window to render in and a message loop. I don't know if that's below the Cocoa/Carbon API level or if one or the other still has to be used.
I once was in the same situation as you. I would suggest checking out the Mac Dev Center and reading their "Getting Started" guide.
I had done a development on Mac OS X with C++. I was forced to use Carbon with it.
Then we used Qt as solution for C++ development with Mac. But I always had to compromise with bugs already present in Qt Framework. But its worth looking into to get few ideas.
Other than that Objective C++ is also nice. Though you will have to follow Cocoa Design Patterns to come with good application.
Related
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.
I am looking to get started with some 3D programming in C or C++. The problem I have is that it seems like the only tutorials I can find for Mac OS use objective C and Cocoa frameworks. I want to obtain the same environment as Windows users, more or less.
If I try to use a text editor and g++ compiler, I am missing headers, but, if I try to use Xcode, I am forced to grapple with Cocoa, which is frustrating to me. I don't really see any reason why the OpenGL/GLUT that comes pre-installed on Mac should force me to use Xcode, but it seems I can't get the header files without it.
How can I get through all of the Apple 'developer friendly' interfaces to write some old-fashioned code with full cross-platform portability?
Some portion of Objective-C is inevitable if you want to use the latest benefits of the OSX/Cocoa. The easiest way to port an existing application to MacOS would be the following:
Write the "bare bones" nibless application in Objective-C. It would only be a single AppDelegate class and a little setup in the main() function
Add the custom NSGLView descendant in your window which you create in the AppDelegate's didFinishLaunching event handler
Setup the CVDisplayLink and rendering callback in the NSGLView initialization
Use your existing OpenGL rendering code in the CVDisplayLink's callback
Now for the interesting part: where to get all of this ?
Surprisingly, a good nibless application sample is the UI for OSX's port of QEMU (yes, the emulator). Also the Apple's official GLEssenstialPractices demo shows all the information you need to set up OpenGL rendering pipeline. All the rest is up to you.
The detailed and modern introduction to system-level OSX programming can be found in the "Advanced Mac OS X Programming" book by Mark Dalrymple. It explains many things and after reading all of this I've understood most of the design decisions in the OS (it really makes you accept all the "non-standard" things if you think from the performance viewpoint).
To get through the "nibless" programming I would recommend you to read the blog posts like this one http://blog.kleymeyer.com/2008/05/creating-cocoa-applications-programatically-ie-nib-less/ The google search helps a lot.
The same tricks apply to the CocoaTouch/iOS and there are a lot of questions answered on SO, like this one Cocoa touch/Xcode - generating NIB-less graphics context
If you want to create cross-platform applications you could create a project with the Command Line Tool template.
Next, import the OpenGL and GLUT framework. This will get you a "blank" C++ project with the required OpenGL and GLUT headers.
Lighthouse 3D gives you some tips about portability and how to initiate your first project.
http://www.lighthouse3d.com/tutorials/glut-tutorial/initialization/
I have created a software layer (named cocoglut) that allows the translatation of basic or essential GLUT calls to COCOA. This library allows creating/destroying windows and register callbacks from a C/C++ application, just by using GLUT calls, without the need for nib files or for XCode project files (and can be compiled from the command line). This option uses full retina display resolution. The source is on GitHub.
I've been searching extensively but all of the resources are somewhat confusing as I'm very novice in programming and trying to learn. When I copy their source code over, it often has errors.
I looked on the microsoft website and found http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog.aspx
But it was even more confusing and I"m terribly lost now. I'm open to pretty much any method, thanks!
You probably want SHBrowseForFolder:
http://msdn.microsoft.com/en-us/library/windows/desktop/bb762115(v=vs.85).aspx
First of all: walk before running.
Try to learn language appropriately step by step with some online tutorial like this:
http://www.learncpp.com
Graphical user interface is harder than basic programming. Once you know a language then select a GUI toolkit. Win32 and MFC are the classic c++ windows toolkits, but are not my favorites. As a novice perhaps you should try Java o C# before C++.
wxWidgets, GTKmm and Qt are better C++ GUI frameworks in my opinion. Each has its own hurdles. Try to learn the language and your IDE (Visual Studio, Codelite,Code::Blocks,e tc) configuration and usage to program effectively.
I suggest you look at Qt at http://www.qt.io/ and Qt Creator at http://www.qt.io/ide/. This toolkit is pretty powerful, you have a good IDE (Qt Creator), you can do zillions of things, there is a default file/folder selection dialog you can customize, and it's rather easy to port Qt applications to other operating systems like Linux or MacOS. If you want to design a GUI application, this is worth a close look. Besides, you can even integrate OpenGL graphics into your windows ...
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.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
In your opinion, what is the best way to create gui in Windows ? with gtk or win32 api ?
Do you recommend GTK for windows ? Yes ? NO ? Why ?
If you are making the gui only for windows, I would strongly recommend WIN 32 Api. I have Made many applications in GTK+ , pyGtk , FLTK and have learned Qt, MFC and SmartWin++. Believe me , But I like Win32 Api the best. It may have a steep learning curve, but for creating native windows applications , it is the fastest and the BEST. And the more complex youre program gets, the easier Win32 Api is compared to other toolkits. And there are things you can do in Win32 Api, which you can not do in any other toolkit.
TESTED:
starting time (simple gui with menu and buttons):
GTK+ = 7 secs;
Qt = 4 secs;
WxWidgets = 3.32 seconds;
FLTK = 1 second;
Win32 Api = 0.34 seconds;
space taken:
Gtk+ = 132 kb;
Qt = 4.5 mb;
WxWidgets = 4.5 mb;
FLTK = 54 kb;
Win32 Api = 6.5 kb;
Let's see.
Win32 is very low-level, C based, and awkward to use.
MFC is considered obsolete.
C# (or C++) with .NET is probably your primary choice for Windows-specific development.
There are even semi-limited ways to port that code to other platforms (Mono).
Java is great for very platform-independent code that "just runs". Sorry, you said C++.
QT is relatively platform-independent.
GTK+, of course, although I personally don't have much experience with it.
Personally, if I do something Windows-specific, I use .NET - the tools in Visual Studio are very powerful, and it's a great all-encompassing suite.
For platform-independent stuff, I use Java, but that may not be your tool of choice. I've seen QT used a lot for that purpose.
You really have a lot of GUI toolkits/frameworks to choose from: Qt, wxWidgets, GTK+/gtkmm, WinAPI, MFC, .NET WinForms/WPF... and those are only the popular ones.
Since you limit yourself to C++, I'd strike out .NET because C++ on .NET is intended to serve as a connection between the unmanaged and managed world. That doesn't mean you can't use it for other types of development, but given the awkward syntax and countless pitfalls I'd not go with it. Moreover, the WinForms code generator of VS puts the forms' code into the header file.. brrrr
As others have stated, WinAPI is written in C, very fast and powerful, but very low level and not easy to program/learn. MFC would be an option since it's written in C++, easier to use than WinAPI and also very powerful. However, it's pretty much obsolete (due to the presence of .NET, mostly).
I wouldn't recommend GTK+/gtkmm (a C++ wrapper for GTK+) for Windows since you don't get the native windows look, it's rather annoying to set up on your developer machine and it also drags around tons of dependencies that you have to install on the user machine. That's actually a pity because especially gtkmm has a very beautiful class hierarchy and design. Probably one of the best designed GUI libraries :)
That said, what would I recommend? Either Qt or wxWidgets. Both are written in (fairly modern) C++, actively developed, have a good library design, run multi-platform and offer lots of functionality. In any case, play around with a few of the libraries listed in the answers here and see which one lets you do the things you want to do most easily :)
both are for c, but there is a good wrapper for gtk (gtkmm).
gtk has its own look, so theres no skinning of ui elements on the user side(with windows styles). but i like to programm with it more.
win32, mfc, .net are mostly limited to ms visual studio, while gtk is very hard to use with vs.
you should have a look on win32, .net, gtkmm and qt. just try to write and compile a simple hello world program with them
upsides of win32:
native windows code
fast
downsides:
no classes, only c with handles (very crappy)
in my opinion very bad documented
upsides of gtk(mm):
easy to learn/programm
good documented
downsides:
somehow difficult to install the development files
no native windows look
win32 api is too complicated, MFC is too annoying.
I have used MFC, win32api, and Qt in windows. In my opnion, Qt is the best one.
I havent tried GTK, so sorry knowing nothing about it.
Edit 2019: It looks all these options are outdated, how about the cross-platform solutions, react-native windows, electron
If Linux (or Mac) compatibility is your concern, then Qt. Else Win32.
I have used GTK+ in the past for a multi-platform application. I found it relatively simple to learn and use. To my mind the main advantage of GTK+ is that you will be able to port your application to other windowing systems. And the main disadvantage is that it will not look exactly like other windows applications. If you are doing cross platform work or are already very familiar with GTK+ (and don't have time to spend learning a new toolkit), I would recommend it.
Personally I prefer Qt, but it really depends on what kind of user interfaces you want to make.
Against Win32:
low-level, high complexity to accomplish trivial things. You have to do EVERYTHING
if you go this route I would recomment a book like the one from Petzold.
Pro Qt:
Good looking GUIs
Can change the look and feel very easily by creating stylesheets
Signal and slots mechanism notifies you of UI events such as "button clicked" etc.
Nice layout system
Integrated with Visual Studio IDE
Modern object oriented c++ code, easy to understand and use
Qt Assistant (Very good documentation)
Relatively liberal licensing (LGPL)
Qt Designer - WYSIWYG design tool you can use for form design
Comes with a wealth of other c++ functionality including XML, networking, eventloops,
threading, database access, etc
Against Qt:
Intermediary step of using MOC compiler
Pro WPF:
if you want the new WPF capabilities of the new Windows platforms, WPF is the way to go.
You may want to try Winforms or WPF. If you're limited to using C/C++, you can embed .NET code using the /clr option for the compiler to embed .NET code for Winforms or WPF.
Sources:
http://msdn.microsoft.com/en-us/library/k8d11d4s(VS.71).aspx
http://msdn.microsoft.com/en-us/library/ms754130.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx
Personal experience
For platform independent development, I would recommend Qt instead of the current GTK. GTK2 drawing was very slow compared to Qt as well as Win32. If you love native look feel, wxWidgets is made for you.