| 1 |
#include "stdafx.h" |
| 2 |
#include "CPixelbit.h" |
| 3 |
|
| 4 |
// 内部定数 |
| 5 |
const char *const DIALOG_TITLE = "RailSim II"; |
| 6 |
|
| 7 |
const int FLASHBUF_SIZE = 1024; // 一時バッファサイズ |
| 8 |
const int FLASHBUF_NUM = 8; // 一時バッファ数 |
| 9 |
|
| 10 |
// 内部グローバル |
| 11 |
char g_FlashBuf[FLASHBUF_NUM][FLASHBUF_SIZE]; // 一時バッファ |
| 12 |
|
| 13 |
int g_FlashBufSelect = 0; // 一時バッファ番号 |
| 14 |
|
| 15 |
/* |
| 16 |
* 一時バッファに書式付出力 |
| 17 |
*/ |
| 18 |
char *FlashIn( |
| 19 |
char *format, // 書式 |
| 20 |
... // 任意パラメタ |
| 21 |
){ |
| 22 |
g_FlashBufSelect = (g_FlashBufSelect+1)%FLASHBUF_NUM; |
| 23 |
va_list vl; |
| 24 |
va_start(vl, format); |
| 25 |
vsprintf(g_FlashBuf[g_FlashBufSelect], format, vl); |
| 26 |
va_end(vl); |
| 27 |
return g_FlashBuf[g_FlashBufSelect]; |
| 28 |
} |
| 29 |
|
| 30 |
/* |
| 31 |
* 一時バッファのアドレスを取得 |
| 32 |
*/ |
| 33 |
char *FlashOut( |
| 34 |
int n // 番号 (-1 で前回 FlashIn したバッファ) |
| 35 |
){ |
| 36 |
return g_FlashBuf[n<0 ? g_FlashBufSelect : n]; |
| 37 |
} |
| 38 |
|
| 39 |
/* |
| 40 |
* 書式付ダイアログ |
| 41 |
*/ |
| 42 |
void Dialog( |
| 43 |
char *format, // 書式 |
| 44 |
... // 任意パラメタ |
| 45 |
){ |
| 46 |
g_FlashBufSelect = (g_FlashBufSelect+1)%FLASHBUF_NUM; |
| 47 |
va_list vl; |
| 48 |
va_start(vl, format); |
| 49 |
vsprintf(g_FlashBuf[g_FlashBufSelect], format, vl); |
| 50 |
va_end(vl); |
| 51 |
MessageBox(GetActiveWindow(), |
| 52 |
g_FlashBuf[g_FlashBufSelect], DIALOG_TITLE, MB_APPLMODAL); |
| 53 |
} |
| 54 |
|
| 55 |
/* |
| 56 |
* エラーダイアログ |
| 57 |
*/ |
| 58 |
void ErrorDialog( |
| 59 |
char *format, // 書式 |
| 60 |
... // 任意パラメタ |
| 61 |
){ |
| 62 |
g_FlashBufSelect = (g_FlashBufSelect+1)%FLASHBUF_NUM; |
| 63 |
va_list vl; |
| 64 |
va_start(vl, format); |
| 65 |
vsprintf(g_FlashBuf[g_FlashBufSelect], format, vl); |
| 66 |
va_end(vl); |
| 67 |
ShowCursor(TRUE); |
| 68 |
DestroyWindow(svw.hWnd); |
| 69 |
MessageBox(GetActiveWindow(), |
| 70 |
g_FlashBuf[g_FlashBufSelect], DIALOG_TITLE, MB_APPLMODAL); |
| 71 |
//PostQuitMessage(0); |
| 72 |
ExitProcess(0); |
| 73 |
} |
| 74 |
|
| 75 |
/* |
| 76 |
* はい・いいえダイアログ |
| 77 |
*/ |
| 78 |
int YesNo( |
| 79 |
char *format, // 書式 |
| 80 |
... // 任意パラメタ |
| 81 |
){ |
| 82 |
g_FlashBufSelect = (g_FlashBufSelect+1)%FLASHBUF_NUM; |
| 83 |
va_list vl; |
| 84 |
va_start(vl, format); |
| 85 |
vsprintf(g_FlashBuf[g_FlashBufSelect], format, vl); |
| 86 |
va_end(vl); |
| 87 |
return MessageBox(GetActiveWindow(), |
| 88 |
g_FlashBuf[g_FlashBufSelect], DIALOG_TITLE, MB_YESNO|MB_APPLMODAL); |
| 89 |
} |
| 90 |
|
| 91 |
/* |
| 92 |
* はい・いいえ・キャンセルダイアログ |
| 93 |
*/ |
| 94 |
int YesNoCancel( |
| 95 |
char *format, // 書式 |
| 96 |
... // 任意パラメタ |
| 97 |
){ |
| 98 |
g_FlashBufSelect = (g_FlashBufSelect+1)%FLASHBUF_NUM; |
| 99 |
va_list vl; |
| 100 |
va_start(vl, format); |
| 101 |
vsprintf(g_FlashBuf[g_FlashBufSelect], format, vl); |
| 102 |
va_end(vl); |
| 103 |
return MessageBox(GetActiveWindow(), |
| 104 |
g_FlashBuf[g_FlashBufSelect], DIALOG_TITLE, MB_YESNOCANCEL|MB_APPLMODAL); |
| 105 |
} |
| 106 |
|
| 107 |
/* |
| 108 |
* ファイル選択ダイアログ |
| 109 |
* |
| 110 |
* 戻り値: TRUE = 選択成功、FALSE = キャンセル、失敗 |
| 111 |
*/ |
| 112 |
BOOL SelectFile( |
| 113 |
HWND hWnd, // 呼び出し元ハンドル |
| 114 |
char *file, // ファイル名格納先 |
| 115 |
int len, // ファイル名の最大文字数 |
| 116 |
const char *filt, // フィルタ |
| 117 |
const char *def, // デフォルト拡張子 |
| 118 |
int flag // TRUE = 書込、FALSE = 読込 |
| 119 |
){ |
| 120 |
OPENFILENAME ofn; |
| 121 |
memset(&ofn, 0, sizeof(OPENFILENAME)); |
| 122 |
memset(file, 0, sizeof(file)); |
| 123 |
ofn.lStructSize = sizeof(OPENFILENAME); |
| 124 |
ofn.hwndOwner = hWnd; |
| 125 |
ofn.lpstrFilter = filt; |
| 126 |
ofn.nFilterIndex = 0; |
| 127 |
ofn.lpstrDefExt = def; |
| 128 |
ofn.nMaxFile = len; |
| 129 |
ofn.lpstrFile = file; |
| 130 |
BOOL ret; |
| 131 |
if(flag){ |
| 132 |
ofn.Flags = OFN_OVERWRITEPROMPT|OFN_HIDEREADONLY; |
| 133 |
ret = GetSaveFileName(&ofn); |
| 134 |
}else{ |
| 135 |
ofn.Flags = OFN_FILEMUSTEXIST|OFN_HIDEREADONLY; |
| 136 |
ret = GetOpenFileName(&ofn); |
| 137 |
} |
| 138 |
return ret; |
| 139 |
} |
| 140 |
|
| 141 |
/* |
| 142 |
* 色作成ダイアログ |
| 143 |
* |
| 144 |
* 戻り値: TRUE = 選択成功、FALSE = キャンセル、失敗 |
| 145 |
*/ |
| 146 |
BOOL ColorDialog( |
| 147 |
HWND hWnd, // 呼び出し元ハンドル |
| 148 |
PDWORD color, // 色格納先 |
| 149 |
DWORD init // 初期化色 |
| 150 |
){ |
| 151 |
static COLORREF colorsave[16]; |
| 152 |
CHOOSECOLOR ccs; |
| 153 |
int r, g, b; |
| 154 |
SplitColor(init, &r, &g, &b); |
| 155 |
ccs.lStructSize = sizeof(CHOOSECOLOR); |
| 156 |
ccs.hwndOwner = hWnd; |
| 157 |
ccs.rgbResult = RGB(r, g, b); |
| 158 |
ccs.lpCustColors = colorsave; |
| 159 |
ccs.Flags = CC_FULLOPEN|CC_RGBINIT; |
| 160 |
BOOL ret = ChooseColor(&ccs); |
| 161 |
if(!ret) return FALSE; |
| 162 |
*color = MakePixel(GetRValue(ccs.rgbResult), |
| 163 |
GetGValue(ccs.rgbResult), GetBValue(ccs.rgbResult)); |
| 164 |
return TRUE; |
| 165 |
} |
| 166 |
|
| 167 |
/* |
| 168 |
* GetLastError() の詳細を表示 |
| 169 |
*/ |
| 170 |
void ShowLastError(){ |
| 171 |
LPVOID lpMsgBuf; |
| 172 |
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM |
| 173 |
|FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), |
| 174 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); |
| 175 |
MessageBox(NULL, (LPSTR)lpMsgBuf, "Error", MB_APPLMODAL); |
| 176 |
LocalFree(lpMsgBuf); |
| 177 |
} |
| 178 |
|
| 179 |
/* |
| 180 |
* 拡張子チェック |
| 181 |
*/ |
| 182 |
bool CheckFileExt( |
| 183 |
char *fname, // ファイル名 |
| 184 |
char *ext // 拡張子 (ピリオド含む) |
| 185 |
){ |
| 186 |
int len1 = strlen(fname), len2 = strlen(ext); |
| 187 |
return len1>len2 && !strcmpi(&fname[len1-len2], ext); |
| 188 |
} |
| 189 |
|
| 190 |
/* |
| 191 |
* 拡張子自動付加 |
| 192 |
*/ |
| 193 |
string FixFileExt( |
| 194 |
char *fname, // ファイル名 |
| 195 |
char *ext // 拡張子 (ピリオドまず) |
| 196 |
){ |
| 197 |
int len = strlen(fname), next = strlen(ext); |
| 198 |
if(*CharPrev(fname, fname+len)=='.') return string(fname)+ext; |
| 199 |
if(len<next+1) return string(fname)+string(".")+ext; |
| 200 |
if(_strcmpi(fname+len-next, ext)) return string(fname)+string(".")+ext; |
| 201 |
if(*CharPrev(fname, fname+len-next)=='.') return fname; |
| 202 |
return string(fname)+string(".")+ext; |
| 203 |
} |
| 204 |
|
| 205 |
/* |
| 206 |
* ファイル名フルパスからディレクトリ部分のみ取り出す |
| 207 |
*/ |
| 208 |
void CutPath( |
| 209 |
char *path // パス格納先 |
| 210 |
){ |
| 211 |
char *ptr = path+strlen(path); |
| 212 |
for(; ptr>path && *ptr!='\\'; ptr = CharPrev(path, ptr)) *ptr = 0; |
| 213 |
} |
| 214 |
|
| 215 |
/* |
| 216 |
* ファイル名フルパスからファイル名部分のみ取り出す |
| 217 |
*/ |
| 218 |
void CutFileName( |
| 219 |
char *name, // ファイル名格納先 |
| 220 |
char *path // フルパス |
| 221 |
){ |
| 222 |
char *ptr = path+strlen(path); |
| 223 |
while(ptr>path && *ptr!='\\') ptr = CharPrev(path, ptr); |
| 224 |
strcpy(name, *ptr=='\\' ? ptr+1 : ptr); |
| 225 |
} |
| 226 |
|
| 227 |
/* |
| 228 |
* 実行ファイルのディレクトリを取得 |
| 229 |
*/ |
| 230 |
void GetAppPath( |
| 231 |
char *path // パス格納先 |
| 232 |
){ |
| 233 |
GetModuleFileName(GetModuleHandle(NULL), path, 1024); |
| 234 |
CutPath(path); |
| 235 |
} |
| 236 |
|
| 237 |
/* |
| 238 |
* 指定されたファイルパスと同じ場所をカレントディレクトリに指定 |
| 239 |
*/ |
| 240 |
void MoveToFile( |
| 241 |
char *path // パス格納先 |
| 242 |
){ |
| 243 |
char *tmp = FlashIn("%s", path); |
| 244 |
CutPath(tmp); |
| 245 |
chdir(tmp); |
| 246 |
} |
| 247 |
|
| 248 |
/* |
| 249 |
* ファイル名に '/' '\' が含まれているか調べる |
| 250 |
*/ |
| 251 |
bool CheckSlash( |
| 252 |
const char *chk // パス格納先 |
| 253 |
){ |
| 254 |
for(; *chk; chk = CharNext(chk)) if(*chk=='\\' || *chk=='/') return true; |
| 255 |
return false; |
| 256 |
} |
| 257 |
|
| 258 |
/* |
| 259 |
* convert from " to '' |
| 260 |
*/ |
| 261 |
string ExpandDoubleQuote( |
| 262 |
const string &str // 対象文字列 |
| 263 |
){ |
| 264 |
string ret = str; |
| 265 |
int i = 0; |
| 266 |
while(ret[i] && i<ret.size()){ |
| 267 |
if(ret[i]=='\"'){ |
| 268 |
ret[i] = '\''; |
| 269 |
ret.insert(i, 1, '\''); |
| 270 |
} |
| 271 |
const char *cs = ret.c_str(); |
| 272 |
i = CharNext(cs+i)-cs; |
| 273 |
} |
| 274 |
return ret; |
| 275 |
} |
| 276 |
|
| 277 |
/* |
| 278 |
* convert from '' to " |
| 279 |
*/ |
| 280 |
string RestoreDoubleQuote( |
| 281 |
const string &str // 対象文字列 |
| 282 |
){ |
| 283 |
string ret = str; |
| 284 |
int i = 0; |
| 285 |
while(i<(int)ret.size()-1 && ret[i]){ |
| 286 |
if(ret[i]=='\'' && ret[i+1]=='\''){ |
| 287 |
ret[i] = '\"'; |
| 288 |
ret.erase(i+1, 1); |
| 289 |
} |
| 290 |
const char *cs = ret.c_str(); |
| 291 |
i = CharNext(cs+i)-cs; |
| 292 |
} |
| 293 |
return ret; |
| 294 |
} |