1 /* $Id: macos.mm 25663 2013-08-05 20:36:06Z michi_cc $ */
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
10 /** @file macos.mm Code related to MacOSX. */
12 #include "../../stdafx.h"
13 #include "../../core/bitmath_func.hpp"
14 #include "../../rev.h"
16 #include "../../string_func.h"
19 #define Point OTTDPoint
20 #include <AppKit/AppKit.h>
25 * This file contains objective C
26 * Apple uses objective C instead of plain C to interact with OS specific/native functions
31 * Get the version of the MacOS we are running under. Code adopted
32 * from http://www.cocoadev.com/index.pl?DeterminingOSVersion
33 * @param return_major major version of the os. This would be 10 in the case of 10.4.11
34 * @param return_minor minor version of the os. This would be 4 in the case of 10.4.11
35 * @param return_bugfix bugfix version of the os. This would be 11 in the case of 10.4.11
36 * A return value of -1 indicates that something went wrong and we don't know.
38 void GetMacOSVersion(int *return_major, int *return_minor, int *return_bugfix)
43 SInt32 systemVersion, version_major, version_minor, version_bugfix;
44 if (Gestalt(gestaltSystemVersion, &systemVersion) == noErr) {
45 if (systemVersion >= 0x1040) {
46 if (Gestalt(gestaltSystemVersionMajor, &version_major) == noErr) *return_major = (int)version_major;
47 if (Gestalt(gestaltSystemVersionMinor, &version_minor) == noErr) *return_minor = (int)version_minor;
48 if (Gestalt(gestaltSystemVersionBugFix, &version_bugfix) == noErr) *return_bugfix = (int)version_bugfix;
50 *return_major = (int)(GB(systemVersion, 12, 4) * 10 + GB(systemVersion, 8, 4));
51 *return_minor = (int)GB(systemVersion, 4, 4);
52 *return_bugfix = (int)GB(systemVersion, 0, 4);
60 * Show the system dialogue message (SDL on MacOSX).
62 * @param title Window title.
63 * @param message Message text.
64 * @param buttonLabel Button text.
66 void ShowMacDialog(const char *title, const char *message, const char *buttonLabel)
68 NSRunAlertPanel([ NSString stringWithUTF8String:title ], [ NSString stringWithUTF8String:message ], [ NSString stringWithUTF8String:buttonLabel ], nil, nil);
71 #elif defined WITH_COCOA
73 extern void CocoaDialog(const char *title, const char *message, const char *buttonLabel);
76 * Show the system dialogue message (Cocoa on MacOSX).
78 * @param title Window title.
79 * @param message Message text.
80 * @param buttonLabel Button text.
82 void ShowMacDialog(const char *title, const char *message, const char *buttonLabel)
84 CocoaDialog(title, message, buttonLabel);
91 * Show the system dialogue message (console on MacOSX).
93 * @param title Window title.
94 * @param message Message text.
95 * @param buttonLabel Button text.
97 void ShowMacDialog(const char *title, const char *message, const char *buttonLabel)
99 fprintf(stderr, "%s: %s\n", title, message);
106 * Show an error message.
108 * @param buf error message text.
109 * @param system message text originates from OS.
111 void ShowOSErrorBox(const char *buf, bool system)
113 /* Display the error in the best way possible. */
115 ShowMacDialog("OpenTTD has encountered an error", buf, "Quit");
117 ShowMacDialog(buf, "See the readme for more info.", "Quit");
121 void OSOpenBrowser(const char *url)
123 [ [ NSWorkspace sharedWorkspace ] openURL:[ NSURL URLWithString:[ NSString stringWithUTF8String:url ] ] ];
127 * Determine and return the current user's locale.
129 const char *GetCurrentLocale(const char *)
131 static char retbuf[32] = { '\0' };
132 NSUserDefaults *defs = [ NSUserDefaults standardUserDefaults ];
133 NSArray *languages = [ defs objectForKey:@"AppleLanguages" ];
134 NSString *preferredLang = [ languages objectAtIndex:0 ];
135 /* preferredLang is either 2 or 5 characters long ("xx" or "xx_YY"). */
137 /* Since Apple introduced encoding to CString in OSX 10.4 we have to make a few conditions
138 * to get the right code for the used version of OSX. */
139 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
140 if (MacOSVersionIsAtLeast(10, 4, 0)) {
141 [ preferredLang getCString:retbuf maxLength:32 encoding:NSASCIIStringEncoding ];
145 #if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4)
146 /* maxLength does not include the \0 char in contrast to the call above. */
147 [ preferredLang getCString:retbuf maxLength:31 ];
156 * Return the contents of the clipboard (COCOA).
158 * @param buffer Clipboard content.
159 * @param last The pointer to the last element of the destination buffer
160 * @return Whether clipboard is empty or not.
162 bool GetClipboardContents(char *buffer, const char *last)
164 NSPasteboard *pb = [ NSPasteboard generalPasteboard ];
165 NSArray *types = [ NSArray arrayWithObject:NSStringPboardType ];
166 NSString *bestType = [ pb availableTypeFromArray:types ];
168 /* Clipboard has no text data available. */
169 if (bestType == nil) return false;
171 NSString *string = [ pb stringForType:NSStringPboardType ];
172 if (string == nil || [ string length ] == 0) return false;
174 strecpy(buffer, [ string UTF8String ], last);
180 uint GetCPUCoreCount()
183 #if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
184 if (MacOSVersionIsAtLeast(10, 5, 0)) {
185 count = [ [ NSProcessInfo processInfo ] activeProcessorCount ];
189 #if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
190 count = MPProcessorsScheduled();
198 * Check if a font is a monospace font.
199 * @param name Name of the font.
200 * @return True if the font is a monospace font.
202 bool IsMonospaceFont(CFStringRef name)
204 NSFont *font = [ NSFont fontWithName:(NSString *)name size:0.0f ];
206 return font != NULL ? [ font isFixedPitch ] : false;