/** * Returns: * TRUE - User agreed to close (both save/don't save) * FALSE - User cancelled close by selecting "Cancel" */ BOOL DoCloseFile(void) { int nResult; static const WCHAR empty_strW[] = { 0 }; nResult=GetWindowTextLengthW(Globals.hEdit); if (SendMessageW(Globals.hEdit, EM_GETMODIFY, 0, 0) && (nResult || Globals.szFileName[0])) { /* prompt user to save changes */ nResult = AlertFileNotSaved(Globals.szFileName); switch (nResult) { case IDYES: return DIALOG_FileSave(); case IDNO: break; case IDCANCEL: return(FALSE); default: return(FALSE); } /* switch */ } /* if */ SetFileNameAndEncoding(empty_strW, ENCODING_ANSI); UpdateWindowCaption(); return(TRUE); }
BOOL DIALOG_FileSaveAs(VOID) { OPENFILENAMEW saveas; WCHAR szPath[MAX_PATH]; WCHAR szDir[MAX_PATH]; static const WCHAR szDefaultExt[] = { 't','x','t',0 }; static const WCHAR txt_files[] = { '*','.','t','x','t',0 }; ZeroMemory(&saveas, sizeof(saveas)); GetCurrentDirectoryW(ARRAY_SIZE(szDir), szDir); lstrcpyW(szPath, txt_files); saveas.lStructSize = sizeof(OPENFILENAMEW); saveas.hwndOwner = Globals.hMainWnd; saveas.hInstance = Globals.hInstance; saveas.lpstrFilter = Globals.szFilter; saveas.lpstrFile = szPath; saveas.nMaxFile = ARRAY_SIZE(szPath); saveas.lpstrInitialDir = szDir; saveas.Flags = OFN_ENABLETEMPLATE | OFN_ENABLEHOOK | OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_ENABLESIZING; saveas.lpfnHook = OfnHookProc; saveas.lpTemplateName = MAKEINTRESOURCEW(IDD_OFN_TEMPLATE); saveas.lpstrDefExt = szDefaultExt; /* Preset encoding to what file was opened/saved last with. */ Globals.encOfnCombo = Globals.encFile; Globals.bOfnIsOpenDialog = FALSE; retry: if (!GetSaveFileNameW(&saveas)) return FALSE; switch (DoSaveFile(szPath, Globals.encOfnCombo)) { case SAVED_OK: SetFileNameAndEncoding(szPath, Globals.encOfnCombo); UpdateWindowCaption(); return TRUE; case SHOW_SAVEAS_DIALOG: goto retry; default: return FALSE; } }
void DoOpenFile(LPCWSTR szFileName, ENCODING enc) { static const WCHAR dotlog[] = { '.','L','O','G',0 }; HANDLE hFile; LPSTR pTemp; DWORD size; DWORD dwNumRead; int lenW; WCHAR* textW; int i; WCHAR log[5]; /* Close any files and prompt to save changes */ if (!DoCloseFile()) return; hFile = CreateFileW(szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(hFile == INVALID_HANDLE_VALUE) { AlertFileNotFound(szFileName); return; } size = GetFileSize(hFile, NULL); if (size == INVALID_FILE_SIZE) { CloseHandle(hFile); ShowLastError(); return; } /* Extra memory for (WCHAR)'\0'-termination. */ pTemp = HeapAlloc(GetProcessHeap(), 0, size+2); if (!pTemp) { CloseHandle(hFile); ShowLastError(); return; } if (!ReadFile(hFile, pTemp, size, &dwNumRead, NULL)) { CloseHandle(hFile); HeapFree(GetProcessHeap(), 0, pTemp); ShowLastError(); return; } CloseHandle(hFile); size = dwNumRead; if (enc == ENCODING_AUTO) enc = detect_encoding_of_buffer(pTemp, size); else if (size >= 2 && (enc==ENCODING_UTF16LE || enc==ENCODING_UTF16BE)) { /* If UTF-16 (BE or LE) is selected, and there is a UTF-16 BOM, * override the selection (like native Notepad). */ if ((BYTE)pTemp[0] == 0xff && (BYTE)pTemp[1] == 0xfe) enc = ENCODING_UTF16LE; else if ((BYTE)pTemp[0] == 0xfe && (BYTE)pTemp[1] == 0xff) enc = ENCODING_UTF16BE; } switch (enc) { case ENCODING_UTF16BE: byteswap_wide_string((WCHAR*) pTemp, size/sizeof(WCHAR)); /* Forget whether the file is BE or LE, like native Notepad. */ enc = ENCODING_UTF16LE; /* fall through */ case ENCODING_UTF16LE: textW = (LPWSTR)pTemp; lenW = size/sizeof(WCHAR); break; default: { int cp = (enc==ENCODING_UTF8) ? CP_UTF8 : CP_ACP; lenW = MultiByteToWideChar(cp, 0, pTemp, size, NULL, 0); textW = HeapAlloc(GetProcessHeap(), 0, (lenW+1) * sizeof(WCHAR)); if (!textW) { ShowLastError(); HeapFree(GetProcessHeap(), 0, pTemp); return; } MultiByteToWideChar(cp, 0, pTemp, size, textW, lenW); HeapFree(GetProcessHeap(), 0, pTemp); break; } } /* Replace '\0's with spaces. Other than creating a custom control that * can deal with '\0' characters, it's the best that can be done. */ for (i = 0; i < lenW; i++) if (textW[i] == '\0') textW[i] = ' '; textW[lenW] = '\0'; if (lenW >= 1 && textW[0] == 0xfeff) SetWindowTextW(Globals.hEdit, textW+1); else SetWindowTextW(Globals.hEdit, textW); HeapFree(GetProcessHeap(), 0, textW); SendMessageW(Globals.hEdit, EM_SETMODIFY, FALSE, 0); SendMessageW(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0); SetFocus(Globals.hEdit); /* If the file starts with .LOG, add a time/date at the end and set cursor after */ if (GetWindowTextW(Globals.hEdit, log, ARRAY_SIZE(log)) && !lstrcmpW(log, dotlog)) { static const WCHAR lfW[] = { '\r','\n',0 }; SendMessageW(Globals.hEdit, EM_SETSEL, GetWindowTextLengthW(Globals.hEdit), -1); SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW); DIALOG_EditTimeDate(); SendMessageW(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lfW); } SetFileNameAndEncoding(szFileName, enc); UpdateWindowCaption(); }