Simpler cross-platform programming with wxWidgets and wyoGuide

Few developers can afford to ignore cross-platform issues when developing applications these days. I've written a guide to help developers with the task, along with a demo application that uses the wxWidgets framework.

Why is cross-platform programming is so important these days? For one reason, because there are many more platforms than there used to be, and the number is increasing. User expectations in the desktop area have increased, so the look and feel has to carry across platforms. There is an increasing chance you'll have to port your code to another platform someday, though you never know which platform you or your users might prefer.

Unfortunately, cross-platform programming isn't as easy as just exchanging some menus. You have to know the little differences between the platforms, and you have to know how to overcome these differences.

As an open source developer you might be inclined to ignore cross-platform altogether, since all you care about is supporting your own platform, and others can support other platforms if they want to. But it's not easy to port a project if the lead developer doesn't care about portability. Instead, others may try to fork instead of participate in your project. Besides, other projects get more attention and a broader audience when they are designed for cross-platform portability from the start.

Whenever anyone talks about cross-platform programming, the question "why not Java" pops up. Java is nice, but for many tasks it's not good enough. While Java applications may run anywhere, that doesn't necessarily mean they look as good on different platforms. Even Java applications have to be carefully made cross-platform, with all the little differences taken care of. And since today there are compilers and interpreters for almost all popular languages available on most platforms, it doesn't matter much which language you choose.

So what can a developer do? Nobody can expect a programmer, particularly an open source programmer working mostly in his spare time, to develop his project for all platforms. But cross-platform programming doesn't impose much additional work if it's done the right way. Here are some simple steps to follow. These steps aren't mandatory -- cross-platform programming can be done in many ways -- but if you stick to this advice, cross-platform programming won't impose any additional work.

Separate GUI code from functionality code

Choose a framework that makes cross-platform programming easy

Follow the cross-platform programming guidelines of wyoGuide

Separating GUI code from the rest is something that most developers do anyway, since large projects have to be modularized. For cross-platform programming, the reason to separate GUI code is that non-GUI code is easier to move to other platforms. While platform considerations for non-GUI code, such as file and directory access, have to be taken into consideration, the impact of the particular platform is smaller and there are many more solutions for these issues than for GUI code.

You need to choose a development framework, but which one? For non-GUI code that question isn't as important, since any decent framework has enough cross-platform functionality. For GUI code there is only one real contender: wxWidgets. No other framework is as complete, as thoroughly native, as easily usable, and as freely licensed as wxWidgets. Only for some special cases might other frameworks do better.

If you had unlimited resources you could build your own framework to fit your cross-platform needs, but supporting and evolving a decent framework today involves many developers, which not even the largest project can afford.

Using a decent framework is only one side of cross-platform programming. The other is following cross-platform guidelines as they are outlined in wyoGuide, a guide and tutorial I created for developing cross-platform applications. These guidelines summarize all kinds of platform-specific guidelines, either by giving advice on how to achieve the best platform fit, or by showing how to circumvent problems. Cross-platform guidelines are always compromises among conflicting platform-specific guidelines, but either you spend the resources for exact platform-specific development (if you are able) or live with a few compromises. wyoGuide is a base which you can build upon and extend.

wyoGuide

What is wyoGuide? Have a look at the first paragraph of its front page:

wyoGuide is a guide and a tutorial for developing well-designed cross-platform applications. Where possible any guideline is accompanied with sample code written in C++ using the wxWidgets framework. The included demo application applies all these guidelines and is well suited as a starting code base for your own project to speed up your software development cycle.

Let's see how wyoGuide applies to a sample application. Assume you want to have an application with a menu bar, with the first choice named "File." Did you know that Mac OS X has an application menu before the "File" menu? Besides, isn't the menu on the Mac shown on the top of the screen? That is taken care of either by the system or by the framework, but what neither can solve for you is which menu entries belongs into which place. wyoGuide offers a nice solution for this problem. It shows how easy it is to code around the problem of the application menu on the Mac, albeit it needs some conditional compilation.

Yet another menu problem is the key shortcut for "Redo," which can be easily circumvented with the code here. This problem could be solved easily without wyoGuide; the only help wyoGuide gives you is to show that there is a conflict you should take care of. There are many such small conflicts that many developers would overlook if they were not listed in a guide.

wyoGuide doesn't give advice just on how to do cross-platform programming, but also on creating well-designed code in general. For instance, it has a guideline about designing the initial size of your windows. How important is that? If a user's first impression doesn't fit his expectations, he may simply discard your application without delving deeper for all the terrific features inside.

There are many more guidelines, some more important, some less. All of the guidelines should be viewed as a help and not as a command. Not all of the guidelines are appropriate in each case. But applying the appropriate guidelines will help your application work well with other applications and on any platform.

wyoGuide-Demo

wyoGuide includes a sample application that might come in handy if you're just started your project, want to add a GUI to your console application, or need some working GUI code for other reasons. The wyoGuide-Demo is a fully working sample application that applies all the wyoGuide guidelines. You can examine the code to get more insight or use it as the starting code base for your application.

If you use wyoGuide-Demo as the base of your application you can be sure your application has all the common features a user expects these days and looks the way it should on any platform. You can then concentrate more on the functionality you want to implement.

If you're starting a new project or converting a console project to a GUI project, take wyoGuide-Demo, replace all the names with your own, remove any excess code, and start adding your functional code at the right places. Your application will most probably run right from the beginning, requiring much less debugging work. Also, you're able to develop on the platform where you have the best development tools, even if your target platform is completely different.

Ordinary middle-sized projects are a more difficult case, since you must decide whether to start anew or tweak the existing code. Try tweaking the code first and start anew later if tweaking isn't enough. At least try separating non-GUI code within the current code so starting anew involves a smaller part of the full project.

Large projects usually have large numbers of developers doing framework work. For these, the easiest approach is to build a wxWidgets port inside their own framework (sort of a wxWidgets platform) to begin a smooth transition. If difficulties arise when something is missing within wxWidgets, developers can add and extend wxWidgets; that's the benefit of open source. If these additions are needed faster than the release cycle of wxWidgets allows, the wxCode project provides an intermediate solution.

If you are interested in what real applications that use wyoGuide look like, check out wyoEditor and wyoFiler.

Conclusion

Cross-platform programming is a necessity few developers can avoid nowadays. There are many ways to do cross-platform development, but only through the use of the guidelines of wyoGuide can developers produce good results without a lot of work. Using wxWidgets as the framework isn't mandatory but will help greatly. Following the advice of wyoGuide not only allows for easy cross-platform programming, but also produces full- featured applications. It helps make the distinction between a professional-looking full-featured application and the others.

Otto Wyss is a contributor to the wxWidgets project and the author of wyoGuide.