diff --git a/sc2/ChangeLog b/sc2/ChangeLog index b8d33bdb7..73e106c53 100644 --- a/sc2/ChangeLog +++ b/sc2/ChangeLog @@ -1,4 +1,5 @@ Changes towards version 0.7: +- Added a native error box for MacOSX (like we have for Windows) - Alex - Fix for weird colors problem on MacOSX w/ SDL 1.2.14; also improves overall compatibility on all platforms - Alex - Unix build system cleanups, fix detection of SDL, libmikmod, pthread - SvdB diff --git a/sc2/src/libs/log/Makeinfo b/sc2/src/libs/log/Makeinfo index 83a190013..f1195db7d 100644 --- a/sc2/src/libs/log/Makeinfo +++ b/sc2/src/libs/log/Makeinfo @@ -1 +1,13 @@ uqm_CFILES="uqmlog.c" + +case "$HOST_SYSTEM" in + Darwin) + uqm_MFILES="msgbox_macosx.m" + ;; + MINGW32*|CYGWIN*) + uqm_CFILES="$uqm_CFILES msgbox_win.c" + ;; + *) + uqm_CFILES="$uqm_CFILES msgbox_stub.c" + ;; +esac diff --git a/sc2/src/libs/log/loginternal.h b/sc2/src/libs/log/loginternal.h new file mode 100644 index 000000000..445715524 --- /dev/null +++ b/sc2/src/libs/log/loginternal.h @@ -0,0 +1,24 @@ +/* + * This program 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; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef UQM_LOGINTERNAL_H_INCL__ +#define UQM_LOGINTERNAL_H_INCL__ + +#include + +extern FILE *streamOut; + +#endif /* UQM_LOGINTERNAL_H_INCL__ */ diff --git a/sc2/src/libs/log/msgbox.h b/sc2/src/libs/log/msgbox.h new file mode 100644 index 000000000..72934a59d --- /dev/null +++ b/sc2/src/libs/log/msgbox.h @@ -0,0 +1,23 @@ +/* + * This program 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; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef UQM_MSGBOX_H_INCL__ +#define UQM_MSGBOX_H_INCL__ + +extern void log_displayBox (const /*UTF-8*/char *title, int isError, + const /*UTF-8*/char *msg); + +#endif /* UQM_MSGBOX_H_INCL__ */ diff --git a/sc2/src/libs/log/msgbox_macosx.m b/sc2/src/libs/log/msgbox_macosx.m new file mode 100644 index 000000000..8d1d30646 --- /dev/null +++ b/sc2/src/libs/log/msgbox_macosx.m @@ -0,0 +1,37 @@ +/* + * This program 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; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#import +#import "msgbox.h" + +void +log_displayBox (const /*UTF-8*/char *title, int isError, + const /*UTF-8*/char *msg) +{ + NSString *titleStr = [NSString stringWithUTF8String:title]; + NSString *msgStr = [NSString stringWithUTF8String:msg]; + + if (!titleStr || !msgStr) + return; // Oops, out of memory? + + if (isError) + NSRunCriticalAlertPanel (titleStr, msgStr, nil, nil, nil); + else + NSRunInformationalAlertPanel (titleStr, msgStr, nil, nil, nil); + + // titleStr and msgStr are autoreleased +} + diff --git a/sc2/src/libs/log/msgbox_stub.c b/sc2/src/libs/log/msgbox_stub.c new file mode 100644 index 000000000..8e0b6b6c6 --- /dev/null +++ b/sc2/src/libs/log/msgbox_stub.c @@ -0,0 +1,34 @@ +/* + * This program 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; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "msgbox.h" +#include "loginternal.h" + +void +log_displayBox (const /*UTF-8*/char *title, int isError, + const /*UTF-8*/char *msg) +{ + // We do not know how to display a box. Perhaps it's done with a + // hefty dose of pixie dust, or perhaps with a hammer and nails. + // So just inform the user of our predicament + fprintf (streamOut, "Do not know how to display %s box\n", + isError ? "an error" : "a"); + + // Suppress the compiler warnings in any case. + (void)title; + (void)msg; +} + diff --git a/sc2/src/libs/log/msgbox_win.c b/sc2/src/libs/log/msgbox_win.c new file mode 100644 index 000000000..c4e0021ad --- /dev/null +++ b/sc2/src/libs/log/msgbox_win.c @@ -0,0 +1,67 @@ +/* + * This program 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; either version 2 of the License, or + * (at your option) any later version. + * + * This program 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. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "msgbox.h" +#define WIN32_LEAN_AND_MEAN +#include +#include +#include + + +// Converts a UTF-8 string to Windows WideChar. +// Caller is responsible for free()ing the returned string. +static LPWSTR +toWideChar (const /*UTF-8*/char *str) +{ + int cch; + LPWSTR wstr; + + cch = MultiByteToWideChar (CP_UTF8, 0, str, -1, NULL, 0); + if (cch == 0) + return NULL; // failed, probably no UTF8 converter + + wstr = malloc (cch * sizeof (WCHAR)); + if (!wstr) + return NULL; // out of memory + + cch = MultiByteToWideChar (CP_UTF8, 0, str, -1, wstr, cch); + if (cch == 0) + { // Failed. It should not fail here if it succeeded just above, + // but it did. Not much can be done about it. + free (wstr); + return NULL; + } + + return wstr; +} + +void +log_displayBox (const /*UTF-8*/char *title, int isError, + const /*UTF-8*/char *msg) +{ + LPWSTR swTitle = toWideChar (title); + LPWSTR swMsg = toWideChar (msg); + UINT uType = isError ? MB_ICONWARNING : MB_ICONINFORMATION; + + if (swTitle && swMsg) + MessageBoxW (NULL, swMsg, swTitle, uType); + else // Could not convert; let's try ASCII, though it may look ugly + MessageBoxA (NULL, msg, title, uType); + + free (swTitle); + free (swMsg); +} + diff --git a/sc2/src/libs/log/uqmlog.c b/sc2/src/libs/log/uqmlog.c index 98e71e82a..5e9cc3dd5 100644 --- a/sc2/src/libs/log/uqmlog.c +++ b/sc2/src/libs/log/uqmlog.c @@ -15,6 +15,8 @@ */ #include "uqmlog.h" +#include "loginternal.h" +#include "msgbox.h" #include #include #include @@ -48,14 +50,13 @@ static volatile bool noThreadReady = false; static bool showBox = true; static bool errorBox = true; -static FILE *streamOut; +FILE *streamOut; static volatile int qlock = 0; static Mutex qmutex; static void exitCallback (void); static void displayLog (bool isError); -static void displayBox (const char *title, bool isError, const char *msg); static void lockQueue (void) @@ -324,45 +325,6 @@ displayLog (bool isError) *p = '\0'; - displayBox ("The Ur-Quan Masters", isError, msgBuf); + log_displayBox ("The Ur-Quan Masters", isError, msgBuf); } - -#if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64) - -extern int __stdcall MessageBoxA (void *hWnd, const char* lpText, - const char* lpCaption, uint32 uType); - -#define MB_ICONEXCLAMATION 0x00000030L -#define MB_ICONASTERISK 0x00000040L - -static void -displayBox (const char *title, bool isError, const char *msg) -{ - MessageBoxA (0, msg, title, - isError ? MB_ICONEXCLAMATION : MB_ICONASTERISK); -} - -#elif defined(__APPLE__) - -static void -displayBox (const char *title, bool isError, const char *msg) -{ - (void) title; - (void) isError; - (void) msg; - // do something here -} - -#else - -static void -displayBox (const char *title, bool isError, const char *msg) -{ - (void) title; - (void) isError; - (void) msg; - // do nothing here -} - -#endif /* OS disjunction */ diff --git a/sc2/src/libs/log/uqmlog.h b/sc2/src/libs/log/uqmlog.h index f8f173fb5..38db089f4 100644 --- a/sc2/src/libs/log/uqmlog.h +++ b/sc2/src/libs/log/uqmlog.h @@ -14,8 +14,8 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#ifndef _UQMLOG_H -#define _UQMLOG_H +#ifndef UQMLOG_H_INCL__ +#define UQMLOG_H_INCL__ #include "port.h" #include "types.h" @@ -56,5 +56,4 @@ extern void log_add_nothreadV (log_Level, const char *fmt, va_list) VPRINTF_FUNCTION(2); -#endif /* _UQMLOG_H */ - +#endif /* UQMLOG_H_INCL__ */