/* * pHatt - PalmOS(TM) Hattrick (http://www.hattrick.org) Manager Assistant * * win32.c - windows-wrapper for convert * * 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 * */ #define STRICT #define WIN32_LEAN_AND_MEAN #define _WIN32_WINNT 0x0400 #include #include #include #include #include #include #include #include #include "win32.h" #include "win32.rh" #include "hrf2pdb.h" #undef WIN32_LEAN_AND_MEAN LPCSTR appName = ApplicationName; char *oldfile; char *hrffile; char *newfile; void modalfatalbox(char *msg) { MessageBox(NULL, msg, "Fatal Error", MB_SYSTEMMODAL | MB_ICONERROR | MB_OK); exit(1); } void *safemalloc(size_t size) { void *p; p = malloc(size); if ( ! p ) { char str[200]; strcpy(str, "Out of memory!"); modalfatalbox(str); } if ( p ) { memset(p, 0, size); } return p; } void *saferealloc(void *ptr, size_t size) { void *p; if ( !ptr ) p = malloc(size); else p = realloc(ptr, size); if ( !p ) { char str[200]; strcpy(str, "Out of memory!"); modalfatalbox(str); } return p; } void safefree(void *ptr) { if ( ptr ) free(ptr); } void usage(int e) { switch (e) { case ERR_OLD: modalfatalbox("Could not open old database"); break; case ERR_HRF: modalfatalbox("Could not open hrf file"); break; case ERR_NEW: modalfatalbox("Could not open new database"); break; case ERR_EMPTY: modalfatalbox("No hrf or new database given"); break; case ERR_VERSION: modalfatalbox("Old database has wrong version"); break; case ERR_NOPLAYER: modalfatalbox("No players found in hrf file"); break; case ERR_DB: modalfatalbox("Old database is not a valid pHatt-DB"); break; case ERR_ALLOC: modalfatalbox("Problem allocation memory"); break; case ERR_UTF: modalfatalbox("Illegal characters in UTF-8 Player name"); break; default: modalfatalbox("An unknown fatal error occured"); break; } exit(e); } static void fatalError() { LPVOID errorstring; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &errorstring, 0, NULL); MessageBox(NULL, errorstring, "Fatal Error", MB_OK | MB_ICONEXCLAMATION); exit(1); } char *chooseFile(BOOL rw, BOOL pdb, char *filename) { OPENFILENAME fn; BOOL oResult; char *chosenfile; chosenfile = snewn(MAX_PATH, char); if ( filename ) sprintf(chosenfile, "%s", filename); fn.lStructSize = sizeof(OPENFILENAME); fn.hwndOwner = NULL; fn.hInstance = NULL; if ( pdb ) fn.lpstrFilter = "Palm Databases (*.pdb)\0*.pdb\0All Files (*.*)\0*.*\0"; else fn.lpstrFilter = "Hattrick Files (*.hrf)\0*.hrf\0All Files (*.*)\0*.*\0"; fn.lpstrCustomFilter = NULL; fn.nMaxCustFilter = 0; fn.nFilterIndex = 0; fn.lpstrFile = chosenfile; fn.nMaxFile = MAX_PATH; fn.lpstrFileTitle = NULL; fn.nMaxFileTitle = 0; fn.lpstrInitialDir = NULL; fn.lpstrTitle = NULL; if ( rw ) fn.Flags = OFN_EXPLORER | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST; else fn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; fn.nFileOffset = 0; fn.nFileExtension = 0; fn.lpstrDefExt = ( rw ? pdb ? "pdb" : "hrf" : NULL); fn.lCustData = 0; fn.lpfnHook = NULL; fn.lpTemplateName = NULL; oResult = ( rw ? GetSaveFileName(&fn) : GetOpenFileName(&fn)); if ( oResult == FALSE ) return NULL; return chosenfile; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { int e; switch (msg) { case WM_COMMAND: switch (GET_WM_COMMAND_ID(wParam,lParam)) { case IDC_OLDBUTTON: if ( ! (oldfile = chooseFile(FALSE, TRUE, oldfile))) break; SetDlgItemText(hwnd, IDC_OLDDB, (LPCTSTR)oldfile); break; case IDC_HRFBUTTON: if ( ! (hrffile = chooseFile(FALSE, FALSE, hrffile))) break; SetDlgItemText(hwnd, IDC_HRFFILE, (LPCTSTR)hrffile); break; case IDC_NEWBUTTON: if ( ! (newfile = chooseFile(TRUE, TRUE, newfile))) break; SetDlgItemText(hwnd, IDC_NEWDB, (LPCTSTR)newfile); break; case IDCANCEL: SendMessage(hwnd, WM_CLOSE, 0, 0); break; case IDOK: if ( ! ( hrffile && newfile )) { MessageBox(NULL, "Need filenames for .hrf and new database", "Error", MB_OK | MB_ICONEXCLAMATION); break; } if ( oldfile && (! strcmp(oldfile, newfile))) { MessageBox(NULL, "Old and new Databases need to be different", "Error", MB_OK | MB_ICONEXCLAMATION); break; } e = convert(oldfile, hrffile, newfile); if ( e ) MessageBox(NULL, "An unknown error occured during converting", "Error", MB_OK | MB_ICONEXCLAMATION); else { MessageBox(NULL, "The database has been converted", "Convert", MB_OK); exit(0); } break; default: return DefDlgProc(hwnd, msg, wParam, lParam); break; } break; case WM_CLOSE: DestroyWindow(hwnd); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefDlgProc(hwnd, msg, wParam, lParam); break; } return 0; } int APIENTRY WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmdline, int show) { WNDCLASSEX wc; HWND hwnd; MSG msg; HMENU hmenu; hwnd = FindWindow(ApplicationName, 0); if ( hwnd ) { ShowWindow(hwnd,SW_SHOW); SetForegroundWindow(hwnd); return FALSE; } InitCommonControls(); if ( ! prev ) { wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_VREDRAW | CS_HREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = DLGWINDOWEXTRA; wc.hInstance = inst; wc.hIcon = LoadIcon(inst, MAKEINTRESOURCE(BigIcon)); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = GetStockBrush(LTGRAY_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = ApplicationName; wc.hIconSm=(HICON)LoadImage(inst, MAKEINTRESOURCE(SmallIcon),IMAGE_ICON,16,16,0); if (! RegisterClassEx(&wc) ) fatalError(); } oldfile = hrffile = newfile = NULL; hwnd = CreateDialog(inst, MAKEINTRESOURCE(MainDialog), NULL, NULL); if ( ! hwnd ) fatalError(); ShowWindow(hwnd, show); UpdateWindow(hwnd); hmenu = GetSystemMenu(hwnd, FALSE); DeleteMenu(hmenu,SC_SIZE,MF_BYCOMMAND); DeleteMenu(hmenu,SC_MAXIMIZE,MF_BYCOMMAND); while (GetMessage(&msg, NULL, 0, 0)) { if (!IsDialogMessage(hwnd, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return 0; }