Mac OS X Platform Notes

From WxPerl Wiki
Jump to: navigation, search

Getting Stable 2.8.12 wxWidgets on 64 bit Mac

To work with Stable 2.8.12 wxWidgets on a modern 64 bit Mac you need to install a 32 bit Perl. You can install a binary distribution:

Citrus Perl Provides a 32 bit version including wxWidgets 2.8.12

ActivePerl The currently supported community editions are only suitable for 64 bit wxWidgets 2.9.x

You may also build your own Perl. The simplest way to build a compatible Perl for Wx and wxWidgets 2.8.12 is to use perlbrew with the correct recipe.

To install perlbrew, use the following 3 commands in your home directory:

curl -L http://xrl.us/perlbrewinstall | bash
~/perl5/perlbrew/bin/perlbrew init
echo "source ~/perl5/perlbrew/etc/bashrc" >> .bash_profile

Note that the last command is slightly different to that which you may be used to on other systems.

Close the current terminal and open a new one so that the perlbrew environment is present.

Build your Perl with the following which should work on Snow Leopard and Lion with Xcode 4.1 & 4.2. For Leopard (10.5) substitute the 10.5 SDK and min version where appropriate.

perlbrew install perl-5.16.1 \
-Dcc=gcc \
-Dld=g++ \
-Dusethreads \
-Duseithreads \
-Duseshrplib \
-Accflags="-isysroot /Developer/SDKs/MacOSX10.6.sdk" \
-Accflags="-mmacosx-version-min=10.6" \
-Accflags="-arch i386 -DUSE_SITECUSTOMIZE -Duselargefiles -fno-merge-constants" \
-Aldflags="-Wl,-search_paths_first -arch i386" \
-Aldflags="-syslibroot,/Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.6" \
-Alddlflags="-Wl,-search_paths_first -arch i386"
-Alddlflags="-syslibroot,/Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.6" \

For Lion and Mountain Lion with Xcode 4.3 or above the location of the development SDK's has changed

perlbrew install perl-5.16.1 \
-Dcc=gcc \
-Dld=g++ \
-Dusethreads \
-Duseithreads \
-Duseshrplib \
-Accflags="-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk" \
-Accflags="-mmacosx-version-min=10.6" \
-Accflags="-arch i386 -DUSE_SITECUSTOMIZE -Duselargefiles -fno-merge-constants" \
-Aldflags="-Wl,-search_paths_first -arch i386" \
-Aldflags="-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk -mmacosx-version-min=10.6" \
-Alddlflags="-Wl,-search_paths_first -arch i386"
-Alddlflags="-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk -mmacosx-version-min=10.6" \


You can use this Perl in your current terminal session by

perlbrew use perl-5.14.2

At this point you could install Wx from CPAN and all will work OK. However, you will link against the system install wxWidgets libraries which are not the latest. Preferably you should download the source for Alien::wxWidgets from CPAN and build that:

perl Build.PL

When asked if you want to download and install wxWidgets, answer 'yes'. Note that this is not the default. Accept 2.8.12 as the version to install and accept .tar.gz' as the download format. You can accept the defaults for all other questions. You can now:

perl Build
perl Build test
perl Build install

With that completed you are now ready to

cpan -i Wx

Getting Development 2.9.4 wxWidgets on Mac

To work with Development 2.9.4 wxWidgets with a default 64 bit Perl on Snow Leopard, Lion and Mountain Lion Alien::wxWidgets will set the flags correctly so you can just do :

perl Build.PL \
 --wxWidgets-version=2.9.4 \
 --wxWidgets-source=tar.bz2 \
 --wxWidgets-build=1 \
 --wxWidgets-build-opengl=1


You can also build wxWidgets 2.9.4 for a 32 bit Perl on a 64 bit Mac but a few more params need to be passed to force the architecture:

For Snow Leopard or Lion with Xcode 4.2 and below.

perl Build.PL \
 --wxWidgets-version=2.9.4 \
 --wxWidgets-source=tar.bz2 \
 --wxWidgets-build=1 \
 --wxWidgets-build-opengl=1 \
 --wxWidgets-extraflags=" CFLAGS='-arch i386' CXXFLAGS='-arch i386' 
                          CPPFLAGS='-arch i386' LDFLAGS='-arch i386' 
                          OBJCFLAGS='-arch i386' OBJCXXFLAGS='-arch i386'
                          --with-macosx-sdk=/Developer/SDKs/MacOSX10.6.sdk 
                          --with-macosx-version-min=10.6 
                          --with-osx_cocoa "

For Lion and Mountain Lion with Xcode 4.3 or above the location of the SDKs has changed

perl Build.PL \
 --wxWidgets-version=2.9.4 \
 --wxWidgets-source=tar.bz2 \
 --wxWidgets-build=1 \
 --wxWidgets-build-opengl=1 \
 --wxWidgets-extraflags=" CFLAGS='-arch i386' CXXFLAGS='-arch i386' 
                          CPPFLAGS='-arch i386' LDFLAGS='-arch i386' 
                          OBJCFLAGS='-arch i386' OBJCXXFLAGS='-arch i386'
                          --with-macosx-sdk=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk 
                          --with-macosx-version-min=10.6 
                          --with-osx_cocoa "

As always on Mac OS X it is strongly recommended that you do not use the system Perl. A simple perlbrew recipe for a native 64 bit Perl on Snow Leopard, Lion or Mountain Lion is:

perlbrew install perl-5.16.1 \
-Dcc=gcc \
-Dld=g++ \
-Dusethreads \
-Duseithreads \
-Duseshrplib \
-Accflags="-DUSE_SITECUSTOMIZE -Duselargefiles -fno-merge-constants" \
-Aldflags="-Wl,-search_paths_first" \
-Alddlflags="-Wl,-search_paths_first"

Common Menubar

On Mac OS X, it is common for a program to be open but to have no windows open at all. The only clue that the program is active is that the menu bar changes.

If your program is document-based, a Mac user may be confused if closing a Wx::Frame causes the program to exit. The user may have been intending to close one document and then open another.

If you want to support the standard Mac document behaviour, there is a standard Wx::App call, SetExitOnFrameDelete(), which tells the program not to exit when its last top-level frame is closed. To setup the menubar to be displayed when no frames are open there is a Mac only static function in Wx::MenuBar, MacSetCommonMenuBar().


use Wx 0.99;

......

my $app = MyAppClass->new();

......

$app->SetExitOnFrameDelete(0);

......

Wx::MenuBar::MacSetCommonMenuBar($mymenubar);

The layout for some standard menu items on Mac OS X is slightly different than on other platforms. If you assign the following id's to a Wx::MenuItem then that menuitem will be displayed in the appropriate Mac specific location.


wxID_ABOUT             displayed in the application menu
wxID_EXIT              displayed in the application menu as 'Quit'
wxID_PREFERENCES       displayed in the application menu as 'Preferences'

The items will be removed from any menu you have placed them in and displayed in the application menu.

To correctly place your own Help menu you must name it '&Help' or if you have internationalised your application, the provided translation of help.

my $helptitle = '&Help';

   or

my $helptitle = gettext( '&Help' );

my $helpmenu  = Wx::Menu->new( $helptitle );

$menubar->Append( $helpmenu, $helptitle );

Accessing the Current Menu

You can access the current menubar on Mac OS X with the static function MacGetInstalledMenuBar.

use Wx 0.99;

my $menubar = Wx::MenuBar::MacGetInstalledMenuBar();

Each time a Wx::Frame gets the focus, wxWidgets takes its menubar and uses that to set the current menubar for your application. Most of the time this is exactly what you want. However, there may be occasions where it is not. For example, when using the Wx::HtmlHelpController you may not want the default menubar of that frame to be set as your application menu. Wx::MenuBar instances have a method, MacInstallMenuBar, that installs the menubar as the current application menubar.

use Wx 0.99;

wxTheApp->{storedmenubar} = Wx::MenuBar->new();

.....

wxTheApp->{storedmenubar}->MacInstallMenuBar();

Pitfalls with wxWidgets 3.0.0 and homebrew

You need to uninstall xz from homebrew to install wxWidgets 3.0.0 with Alien::wxWidgets.

brew uninstall xz

You can reinstall xz after you have installed Alien::wxWidgets with

cpanm Alien::wxWidgets