Author: akhaldi Date: Mon Jan 19 12:40:34 2015 New Revision: 66061
URL: http://svn.reactos.org/svn/reactos?rev=66061&view=rev Log: [NOTEPAD] Allow the user to print only the current selection. Brought to you by Ricardo Hanke. CORE-9052
Modified: trunk/reactos/base/applications/notepad/dialog.c
Modified: trunk/reactos/base/applications/notepad/dialog.c URL: http://svn.reactos.org/svn/reactos/trunk/reactos/base/applications/notepad/d... ============================================================================== --- trunk/reactos/base/applications/notepad/dialog.c [iso-8859-1] (original) +++ trunk/reactos/base/applications/notepad/dialog.c [iso-8859-1] Mon Jan 19 12:40:34 2015 @@ -206,6 +206,73 @@ return _tcsrchr(szFilename, _T('.')) != NULL; }
+int GetSelectionTextLength(HWND hWnd) +{ + DWORD dwStart = 0; + DWORD dwEnd = 0; + + SendMessage(hWnd, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd); + + return dwEnd - dwStart; +} + +int GetSelectionText(HWND hWnd, LPTSTR lpString, int nMaxCount) +{ + DWORD dwStart = 0; + DWORD dwEnd = 0; + DWORD dwSize; + HRESULT hResult; + LPTSTR lpTemp; + + if (!lpString) + { + return 0; + } + + SendMessage(hWnd, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd); + + if (dwStart == dwEnd) + { + return 0; + } + + dwSize = GetWindowTextLength(hWnd) + 1; + lpTemp = HeapAlloc(GetProcessHeap(), 0, dwSize * sizeof(TCHAR)); + if (!lpTemp) + { + return 0; + } + + dwSize = GetWindowText(hWnd, lpTemp, dwSize); + + if (!dwSize) + { + HeapFree(GetProcessHeap(), 0, lpTemp); + return 0; + } + + hResult = StringCchCopyN(lpString, nMaxCount, lpTemp + dwStart, dwEnd - dwStart); + HeapFree(GetProcessHeap(), 0, lpTemp); + + switch (hResult) + { + case S_OK: + { + return dwEnd - dwStart; + } + + case STRSAFE_E_INSUFFICIENT_BUFFER: + { + return nMaxCount - 1; + } + + default: + { + return 0; + } + } +} + static BOOL DoSaveFile(VOID) { BOOL bRet = TRUE; @@ -551,7 +618,14 @@ printer.hInstance = Globals.hInstance;
/* Set some default flags */ - printer.Flags = PD_RETURNDC; + printer.Flags = PD_RETURNDC | PD_SELECTION; + + /* Disable the selection radio button if there is no text selected */ + if (!GetSelectionTextLength(Globals.hEdit)) + { + printer.Flags = printer.Flags | PD_NOSELECTION; + } + printer.nFromPage = 0; printer.nMinPage = 1; /* we really need to calculate number of pages to set nMaxPage and nToPage */ @@ -587,7 +661,15 @@ cHeightPels = GetDeviceCaps(printer.hDC, VERTRES);
/* Get the file text */ - size = GetWindowTextLength(Globals.hEdit) + 1; + if (printer.Flags & PD_SELECTION) + { + size = GetSelectionTextLength(Globals.hEdit) + 1; + } + else + { + size = GetWindowTextLength(Globals.hEdit) + 1; + } + pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(TCHAR)); if (!pTemp) { @@ -596,7 +678,15 @@ ShowLastError(); return; } - size = GetWindowText(Globals.hEdit, pTemp, size); + + if (printer.Flags & PD_SELECTION) + { + size = GetSelectionText(Globals.hEdit, pTemp, size); + } + else + { + size = GetWindowText(Globals.hEdit, pTemp, size); + }
border = 150; for (copycount=1; copycount <= printer.nCopies; copycount++) {