How to make a window using C++ on mac - c++

I am looking for a way to use C++ to make a window using Xcode. the reason being I have looked for ways to do this, but they all seem to be using visual basic, and seeing as I have a mac, i can not run this. If it just to difficult to make a window on Xcode, is there another program I can use, because I am hoping to later turn this into a user interface for a program...
~Regards
Mark Calhoun

Well, you may want to look into a framework that Herb Sutter actually mentioned at Going Native, back in 2012: http://www.openframeworks.cc
I've actually experimented with this framework in Xcode 5 and it works as advertised

You should probably start with Apple's "Start Developing Mac Apps Today" tutorial. In particular the Your First Mac App page shows how to create a simple app with a window and some controls, and how to set that up in Xcode.

Related

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.

How can I 'break away' from Cocoa and develop Mac OpenGL applications in C/C++?

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.

How do I program a select folder dialogue in C++?

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 ...

Mac C++ GUI coding - comparison to Win32

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.

How to take screenshot in Mac OS X using Cocoa or C++

How to take screenshot programmically of desktop area in Mac OS X ?
Two interesting options I have seen, but yet to use professionally, are the screencapture utility and a MacFuse demo.
The screencapture utility has been around since 10.2, according to the man page, and could be linked to a Cocoa application by use of NSTask.
The MacFuse demo worked by creating a new screenshot each time a folder was opened, or something like that. The idea being you could write a quick script to access the image when you needed it, without having to have the script actually run on that machine.
But seriously, Apple has some other sample code called "Son of Grab" which uses the new CGWindow API which is pretty awesome.
http://developer.apple.com/samplecode/SonOfGrab/
One way of going about doing this would be to use NSTask in conjuction with the 'screencapture' command line command.
For example:
NSTask *theProcess;
theProcess = [[NSTask alloc] init];
[theProcess setLaunchPath:#"/usr/sbin/screencapture"];
// use arguments to set save location
[theProcess setArguments:#"blahblah"];
[theProcess launch];
The you could open up the file wherever you told it to be saved, process it, and then delete it as needed. Obviously stopgap, but it would work.
If you're fine with Leopard compatibility, there's a very powerful new CGWindow API that will let you grab screen shots, window shots, or composites of any range of window layers.
http://developer.apple.com/samplecode/SonOfGrab/
Qt includes an example screenshot app in examples\desktop\screenshot. Qt works on a range of platforms, including MacOSX.
http://trolltech.com/products/qt/
The following might be helpful if you are attempting to accomplish this with C++ or python. Also, this would be even more helpful in the case that you want your programmatic method to be cross-platform portable. (Windows, Linux, Mac osx, and even beyond)
An earlier response mentions QT.
In the same way that QT will allow you to capture and save a screenshot, so does another "competing" framework, namely wxWidgets. wxWidgets is a C++ framework, but it also provides python bindings via wxPython.
To read more, use the following link, search the book for wxScreenDC and choose "Page 139" from the list of pages that match the search:
http://books.google.com/books?id=CyMsvtgnq0QC&vq="accessing+the+screen+with+wxScreendc"
If you consider REALbasic, this is extremely easy to do with RB and the MBS Plugins. I've just written an application that does timed screenshots using RB and the MBS Plugins. You can read about it here: http://tektalkin.blogspot.com/2008/08/screenaudit-for-mac-osx.html