ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

dialog.c
Go to the documentation of this file.
00001 /*
00002  *  Notepad (dialog.c)
00003  *
00004  *  Copyright 1998,99 Marcel Baur <mbaur@g26.ethz.ch>
00005  *  Copyright 2002 Sylvain Petreolle <spetreolle@yahoo.fr>
00006  *  Copyright 2002 Andriy Palamarchuk
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021  */
00022 
00023 #include <notepad.h>
00024 
00025 LRESULT CALLBACK EDIT_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
00026 
00027 static const TCHAR helpfile[]     = _T("notepad.hlp");
00028 static const TCHAR empty_str[]    = _T("");
00029 static const TCHAR szDefaultExt[] = _T("txt");
00030 static const TCHAR txt_files[]    = _T("*.txt");
00031 
00032 static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam);
00033 
00034 #ifndef UNICODE
00035 static LPSTR ConvertToASCII(LPSTR pszText)
00036 {
00037     int    sz;
00038     LPWSTR pszTextW = (LPWSTR)pszText;
00039 
00040     /* default return value */
00041     pszText = NULL;
00042     do {
00043         /* query about requested size for conversion */
00044         sz = WideCharToMultiByte(CP_ACP, 0, pszTextW, -1, NULL, 0, NULL, NULL);
00045         if (!sz)
00046             break;
00047 
00048         /* get space for ASCII buffer */
00049         pszText = (LPSTR)HeapAlloc(GetProcessHeap(), 0, sz);
00050         if (pszText == NULL)
00051             break;
00052 
00053         /* if previous diagnostic call worked fine,
00054          * then this one will work too,
00055          * so no need to test return value here
00056          */
00057         WideCharToMultiByte(CP_ACP, 0, pszTextW, -1, pszText, sz, NULL, NULL);
00058     } while (0);
00059 
00060     HeapFree(GetProcessHeap(), 0, pszTextW);
00061     return pszText;
00062 }
00063 
00064 static LPWSTR ConvertToUNICODE(LPSTR pszText, DWORD *pdwSize)
00065 {
00066     int    sz;
00067     LPWSTR pszTextW = NULL;
00068 
00069     do {
00070         /* query about requested size for conversion */
00071         sz = MultiByteToWideChar(CP_ACP, 0, pszText, -1, NULL, 0);
00072         if (!sz)
00073             break;
00074 
00075         /* get space for UNICODE buffer */
00076         pszTextW = HeapAlloc(GetProcessHeap(), 0, sz*sizeof(WCHAR));
00077         if (pszText == NULL)
00078             break;
00079 
00080         /* if previous diagnostic call worked fine,
00081          * then this one will work too,
00082          * so no need to test return value here
00083          */
00084         MultiByteToWideChar(CP_ACP, 0, pszText, -1, pszTextW, sz);
00085 
00086         /* report the new size of the text to the caller */
00087         *pdwSize = sz;
00088     } while (0);
00089 
00090     HeapFree(GetProcessHeap(), 0, pszText);
00091     return pszTextW;
00092 }
00093 #endif
00094 
00095 VOID ShowLastError(void)
00096 {
00097     DWORD error = GetLastError();
00098     if (error != NO_ERROR)
00099     {
00100         LPTSTR lpMsgBuf = NULL;
00101         TCHAR szTitle[MAX_STRING_LEN];
00102 
00103         LoadString(Globals.hInstance, STRING_ERROR, szTitle, SIZEOF(szTitle));
00104         FormatMessage(
00105             FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
00106             NULL, error, 0,
00107             (LPTSTR) &lpMsgBuf, 0, NULL);
00108         MessageBox(NULL, lpMsgBuf, szTitle, MB_OK | MB_ICONERROR);
00109         LocalFree(lpMsgBuf);
00110     }
00111 }
00112 
00118 static void UpdateWindowCaption(void)
00119 {
00120   TCHAR szCaption[MAX_STRING_LEN] = _T("");
00121   TCHAR szNotepad[MAX_STRING_LEN];
00122 
00123   LoadString(Globals.hInstance, STRING_NOTEPAD, szNotepad, SIZEOF(szNotepad));
00124 
00125   if (Globals.szFileTitle[0] != '\0')
00126   {
00127       StringCchCat(szCaption, MAX_STRING_LEN, Globals.szFileTitle);
00128   }
00129   else
00130   {
00131       LoadString(Globals.hInstance, STRING_UNTITLED, szCaption, SIZEOF(szCaption));
00132   }
00133   
00134   StringCchCat(szCaption, MAX_STRING_LEN, _T(" - "));
00135   StringCchCat(szCaption, MAX_STRING_LEN, szNotepad);
00136   SetWindowText(Globals.hMainWnd, szCaption);
00137 }
00138 
00139 static void AlertFileNotFound(LPCTSTR szFileName)
00140 {
00141    TCHAR szMessage[MAX_STRING_LEN];
00142    TCHAR szResource[MAX_STRING_LEN];
00143 
00144    /* Load and format szMessage */
00145    LoadString(Globals.hInstance, STRING_NOTFOUND, szResource, SIZEOF(szResource));
00146    wsprintf(szMessage, szResource, szFileName);
00147 
00148    /* Load szCaption */
00149    LoadString(Globals.hInstance, STRING_NOTEPAD,  szResource, SIZEOF(szResource));
00150 
00151    /* Display Modal Dialog */
00152    MessageBox(Globals.hMainWnd, szMessage, szResource, MB_ICONEXCLAMATION);
00153 }
00154 
00155 static int AlertFileNotSaved(LPCTSTR szFileName)
00156 {
00157    TCHAR szMessage[MAX_STRING_LEN];
00158    TCHAR szResource[MAX_STRING_LEN];
00159    TCHAR szUntitled[MAX_STRING_LEN];
00160 
00161    LoadString(Globals.hInstance, STRING_UNTITLED, szUntitled, SIZEOF(szUntitled));
00162 
00163    /* Load and format Message */
00164    LoadString(Globals.hInstance, STRING_NOTSAVED, szResource, SIZEOF(szResource));
00165    wsprintf(szMessage, szResource, szFileName[0] ? szFileName : szUntitled);
00166 
00167    /* Load Caption */
00168    LoadString(Globals.hInstance, STRING_NOTEPAD, szResource, SIZEOF(szResource));
00169 
00170    /* Display modal */
00171    return MessageBox(Globals.hMainWnd, szMessage, szResource, MB_ICONEXCLAMATION|MB_YESNOCANCEL);
00172 }
00173 
00179 BOOL FileExists(LPCTSTR szFilename)
00180 {
00181    WIN32_FIND_DATA entry;
00182    HANDLE hFile;
00183 
00184    hFile = FindFirstFile(szFilename, &entry);
00185    FindClose(hFile);
00186 
00187    return (hFile != INVALID_HANDLE_VALUE);
00188 }
00189 
00190 
00191 BOOL HasFileExtension(LPCTSTR szFilename)
00192 {
00193     LPCTSTR s;
00194 
00195     s = _tcsrchr(szFilename, _T('\\'));
00196     if (s)
00197         szFilename = s;
00198     return _tcsrchr(szFilename, _T('.')) != NULL;
00199 }
00200 
00201 
00202 static VOID DoSaveFile(VOID)
00203 {
00204     HANDLE hFile;
00205     LPTSTR pTemp;
00206     DWORD size;
00207 
00208     hFile = CreateFile(Globals.szFileName, GENERIC_WRITE, FILE_SHARE_WRITE,
00209                        NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
00210     if(hFile == INVALID_HANDLE_VALUE)
00211     {
00212         ShowLastError();
00213         return;
00214     }
00215 
00216     size = GetWindowTextLength(Globals.hEdit) + 1;
00217     pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*pTemp));
00218     if (!pTemp)
00219     {
00220         CloseHandle(hFile);
00221         ShowLastError();
00222         return;
00223     }
00224     size = GetWindowText(Globals.hEdit, pTemp, size);
00225 
00226 #ifndef UNICODE
00227     pTemp = (LPTSTR)ConvertToUNICODE(pTemp, &size);
00228     if (!pTemp) {
00229         /* original "pTemp" already freed */
00230         CloseHandle(hFile);
00231         ShowLastError();
00232         return;
00233     }
00234 #endif
00235 
00236     if (size)
00237     {
00238         if (!WriteText(hFile, (LPWSTR)pTemp, size, Globals.iEncoding, Globals.iEoln))
00239             ShowLastError();
00240         else
00241             SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
00242     }
00243 
00244     CloseHandle(hFile);
00245     HeapFree(GetProcessHeap(), 0, pTemp);
00246 }
00247 
00253 BOOL DoCloseFile(void)
00254 {
00255     int nResult;
00256 
00257     if (SendMessage(Globals.hEdit, EM_GETMODIFY, 0, 0))
00258     {
00259         /* prompt user to save changes */
00260         nResult = AlertFileNotSaved(Globals.szFileName);
00261         switch (nResult) {
00262             case IDYES:     DIALOG_FileSave();
00263                             break;
00264 
00265             case IDNO:      break;
00266 
00267             case IDCANCEL:  return(FALSE);
00268                             break;
00269 
00270             default:        return(FALSE);
00271                             break;
00272         } /* switch */
00273     } /* if */
00274 
00275     SetFileName(empty_str);
00276 
00277     UpdateWindowCaption();
00278     return(TRUE);
00279 }
00280 
00281 void DoOpenFile(LPCTSTR szFileName)
00282 {
00283     static const TCHAR dotlog[] = _T(".LOG");
00284     HANDLE hFile;
00285     LPTSTR pszText;
00286     DWORD dwTextLen;
00287     TCHAR log[5];
00288 
00289     /* Close any files and prompt to save changes */
00290     if (!DoCloseFile())
00291         return;
00292 
00293     hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
00294                        OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
00295     if (hFile == INVALID_HANDLE_VALUE)
00296     {
00297         ShowLastError();
00298         goto done;
00299     }
00300 
00301     if (!ReadText(hFile, (LPWSTR *)&pszText, &dwTextLen, &Globals.iEncoding, &Globals.iEoln))
00302     {
00303         ShowLastError();
00304         goto done;
00305     }
00306 #ifndef UNICODE
00307     pszText = ConvertToASCII(pszText);
00308     if (pszText == NULL) {
00309         ShowLastError();
00310         goto done;
00311     }
00312 #endif
00313     SetWindowText(Globals.hEdit, pszText);
00314 
00315     SendMessage(Globals.hEdit, EM_SETMODIFY, FALSE, 0);
00316     SendMessage(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
00317     SetFocus(Globals.hEdit);
00318 
00319     /*  If the file starts with .LOG, add a time/date at the end and set cursor after
00320      *  See http://support.microsoft.com/?kbid=260563
00321      */
00322     if (GetWindowText(Globals.hEdit, log, SIZEOF(log)) && !_tcscmp(log, dotlog))
00323     {
00324         static const TCHAR lf[] = _T("\r\n");
00325         SendMessage(Globals.hEdit, EM_SETSEL, GetWindowTextLength(Globals.hEdit), -1);
00326         SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lf);
00327         DIALOG_EditTimeDate();
00328         SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)lf);
00329     }
00330 
00331     SetFileName(szFileName);
00332     UpdateWindowCaption();
00333     NOTEPAD_EnableSearchMenu();
00334 done:
00335     if (hFile != INVALID_HANDLE_VALUE)
00336         CloseHandle(hFile);
00337     if (pszText)
00338         HeapFree(GetProcessHeap(), 0, pszText);
00339 }
00340 
00341 VOID DIALOG_FileNew(VOID)
00342 {
00343     /* Close any files and prompt to save changes */
00344     if (DoCloseFile()) {
00345         SetWindowText(Globals.hEdit, empty_str);
00346         SendMessage(Globals.hEdit, EM_EMPTYUNDOBUFFER, 0, 0);
00347         SetFocus(Globals.hEdit);
00348         NOTEPAD_EnableSearchMenu();
00349     }
00350 }
00351 
00352 VOID DIALOG_FileOpen(VOID)
00353 {
00354     OPENFILENAME openfilename;
00355     TCHAR szDir[MAX_PATH];
00356     TCHAR szPath[MAX_PATH];
00357 
00358     ZeroMemory(&openfilename, sizeof(openfilename));
00359 
00360     GetCurrentDirectory(SIZEOF(szDir), szDir);
00361     if (Globals.szFileName[0] == 0)
00362         _tcscpy(szPath, txt_files);
00363     else
00364         _tcscpy(szPath, Globals.szFileName);
00365 
00366     openfilename.lStructSize       = sizeof(openfilename);
00367     openfilename.hwndOwner         = Globals.hMainWnd;
00368     openfilename.hInstance         = Globals.hInstance;
00369     openfilename.lpstrFilter       = Globals.szFilter;
00370     openfilename.lpstrFile         = szPath;
00371     openfilename.nMaxFile          = SIZEOF(szPath);
00372     openfilename.lpstrInitialDir   = szDir;
00373     openfilename.Flags             = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
00374         OFN_HIDEREADONLY;
00375     openfilename.lpstrDefExt       = szDefaultExt;
00376 
00377 
00378     if (GetOpenFileName(&openfilename)) {
00379         if (FileExists(openfilename.lpstrFile))
00380             DoOpenFile(openfilename.lpstrFile);
00381         else
00382             AlertFileNotFound(openfilename.lpstrFile);
00383     }
00384 }
00385 
00386 
00387 VOID DIALOG_FileSave(VOID)
00388 {
00389     if (Globals.szFileName[0] == '\0')
00390         DIALOG_FileSaveAs();
00391     else
00392         DoSaveFile();
00393 }
00394 
00395 static UINT_PTR CALLBACK DIALOG_FileSaveAs_Hook(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
00396 {
00397     TCHAR szText[128];
00398     HWND hCombo;
00399 
00400     UNREFERENCED_PARAMETER(wParam);
00401 
00402     switch(msg)
00403     {
00404         case WM_INITDIALOG:
00405             hCombo = GetDlgItem(hDlg, ID_ENCODING);
00406 
00407             LoadString(Globals.hInstance, STRING_ANSI, szText, SIZEOF(szText));
00408             SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
00409 
00410             LoadString(Globals.hInstance, STRING_UNICODE, szText, SIZEOF(szText));
00411             SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
00412 
00413             LoadString(Globals.hInstance, STRING_UNICODE_BE, szText, SIZEOF(szText));
00414             SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
00415 
00416             LoadString(Globals.hInstance, STRING_UTF8, szText, SIZEOF(szText));
00417             SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
00418 
00419             SendMessage(hCombo, CB_SETCURSEL, Globals.iEncoding, 0);
00420 
00421             hCombo = GetDlgItem(hDlg, ID_EOLN);
00422 
00423             LoadString(Globals.hInstance, STRING_CRLF, szText, SIZEOF(szText));
00424             SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
00425 
00426             LoadString(Globals.hInstance, STRING_LF, szText, SIZEOF(szText));
00427             SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
00428 
00429             LoadString(Globals.hInstance, STRING_CR, szText, SIZEOF(szText));
00430             SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM) szText);
00431 
00432             SendMessage(hCombo, CB_SETCURSEL, Globals.iEoln, 0);
00433             break;
00434 
00435         case WM_NOTIFY:
00436             if (((NMHDR *) lParam)->code == CDN_FILEOK)
00437             {
00438                 hCombo = GetDlgItem(hDlg, ID_ENCODING);
00439                 if (hCombo)
00440                     Globals.iEncoding = (int) SendMessage(hCombo, CB_GETCURSEL, 0, 0);
00441 
00442                 hCombo = GetDlgItem(hDlg, ID_EOLN);
00443                 if (hCombo)
00444                     Globals.iEoln = (int) SendMessage(hCombo, CB_GETCURSEL, 0, 0);
00445             }
00446             break;
00447     }
00448     return 0;
00449 }
00450 
00451 VOID DIALOG_FileSaveAs(VOID)
00452 {
00453     OPENFILENAME saveas;
00454     TCHAR szDir[MAX_PATH];
00455     TCHAR szPath[MAX_PATH];
00456 
00457     ZeroMemory(&saveas, sizeof(saveas));
00458 
00459     GetCurrentDirectory(SIZEOF(szDir), szDir);
00460     if (Globals.szFileName[0] == 0)
00461         _tcscpy(szPath, txt_files);
00462     else
00463         _tcscpy(szPath, Globals.szFileName);
00464 
00465     saveas.lStructSize       = sizeof(OPENFILENAME);
00466     saveas.hwndOwner         = Globals.hMainWnd;
00467     saveas.hInstance         = Globals.hInstance;
00468     saveas.lpstrFilter       = Globals.szFilter;
00469     saveas.lpstrFile         = szPath;
00470     saveas.nMaxFile          = SIZEOF(szPath);
00471     saveas.lpstrInitialDir   = szDir;
00472     saveas.Flags             = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
00473         OFN_HIDEREADONLY | OFN_EXPLORER | OFN_ENABLETEMPLATE | OFN_ENABLEHOOK;
00474     saveas.lpstrDefExt       = szDefaultExt;
00475     saveas.lpTemplateName    = MAKEINTRESOURCE(DIALOG_ENCODING);
00476     saveas.lpfnHook          = DIALOG_FileSaveAs_Hook;
00477 
00478     if (GetSaveFileName(&saveas)) {
00479         SetFileName(szPath);
00480         UpdateWindowCaption();
00481         DoSaveFile();
00482     }
00483 }
00484 
00485 VOID DIALOG_FilePrint(VOID)
00486 {
00487     DOCINFO di;
00488     PRINTDLG printer;
00489     SIZE szMetric;
00490     int cWidthPels, cHeightPels, border;
00491     int xLeft, yTop, pagecount, dopage, copycount;
00492     unsigned int i;
00493     LOGFONT hdrFont;
00494     HFONT font, old_font=0;
00495     DWORD size;
00496     LPTSTR pTemp;
00497     static const TCHAR times_new_roman[] = _T("Times New Roman");
00498 
00499     /* Get a small font and print some header info on each page */
00500     ZeroMemory(&hdrFont, sizeof(hdrFont));
00501     hdrFont.lfHeight = 100;
00502     hdrFont.lfWeight = FW_BOLD;
00503     hdrFont.lfCharSet = ANSI_CHARSET;
00504     hdrFont.lfOutPrecision = OUT_DEFAULT_PRECIS;
00505     hdrFont.lfClipPrecision = CLIP_DEFAULT_PRECIS;
00506     hdrFont.lfQuality = PROOF_QUALITY;
00507     hdrFont.lfPitchAndFamily = VARIABLE_PITCH | FF_ROMAN;
00508     _tcscpy(hdrFont.lfFaceName, times_new_roman);
00509 
00510     font = CreateFontIndirect(&hdrFont);
00511 
00512     /* Get Current Settings */
00513     ZeroMemory(&printer, sizeof(printer));
00514     printer.lStructSize           = sizeof(printer);
00515     printer.hwndOwner             = Globals.hMainWnd;
00516     printer.hInstance             = Globals.hInstance;
00517 
00518     /* Set some default flags */
00519     printer.Flags                 = PD_RETURNDC;
00520     printer.nFromPage             = 0;
00521     printer.nMinPage              = 1;
00522     /* we really need to calculate number of pages to set nMaxPage and nToPage */
00523     printer.nToPage               = 0;
00524     printer.nMaxPage              = (WORD) -1;
00525 
00526     /* Let commdlg manage copy settings */
00527     printer.nCopies               = (WORD)PD_USEDEVMODECOPIES;
00528 
00529     if (!PrintDlg(&printer)) return;
00530 
00531     assert(printer.hDC != 0);
00532 
00533     /* initialize DOCINFO */
00534     di.cbSize = sizeof(DOCINFO);
00535     di.lpszDocName = Globals.szFileTitle;
00536     di.lpszOutput = NULL;
00537     di.lpszDatatype = NULL;
00538     di.fwType = 0;
00539 
00540     if (StartDoc(printer.hDC, &di) <= 0) return;
00541 
00542     /* Get the page dimensions in pixels. */
00543     cWidthPels = GetDeviceCaps(printer.hDC, HORZRES);
00544     cHeightPels = GetDeviceCaps(printer.hDC, VERTRES);
00545 
00546     /* Get the file text */
00547     size = GetWindowTextLength(Globals.hEdit) + 1;
00548     pTemp = HeapAlloc(GetProcessHeap(), 0, size * sizeof(TCHAR));
00549     if (!pTemp)
00550     {
00551         ShowLastError();
00552         return;
00553     }
00554     size = GetWindowText(Globals.hEdit, pTemp, size);
00555 
00556     border = 150;
00557     for (copycount=1; copycount <= printer.nCopies; copycount++) {
00558         i = 0;
00559         pagecount = 1;
00560         do {
00561             static const TCHAR letterM[] = _T("M");
00562 
00563             if (pagecount >= printer.nFromPage &&
00564     /*          ((printer.Flags & PD_PAGENUMS) == 0 ||  pagecount <= printer.nToPage))*/
00565             pagecount <= printer.nToPage)
00566                 dopage = 1;
00567             else
00568                 dopage = 0;
00569 
00570             old_font = SelectObject(printer.hDC, font);
00571             GetTextExtentPoint32(printer.hDC, letterM, 1, &szMetric);
00572 
00573             if (dopage) {
00574                 if (StartPage(printer.hDC) <= 0) {
00575                     static const TCHAR failed[] = _T("StartPage failed");
00576                     static const TCHAR error[] = _T("Print Error");
00577                     MessageBox(Globals.hMainWnd, failed, error, MB_ICONEXCLAMATION);
00578                     return;
00579                 }
00580                 /* Write a rectangle and header at the top of each page */
00581                 Rectangle(printer.hDC, border, border, cWidthPels-border, border+szMetric.cy*2);
00582                 /* I don't know what's up with this TextOut command. This comes out
00583                 kind of mangled.
00584                 */
00585                 TextOut(printer.hDC, border*2, border+szMetric.cy/2, Globals.szFileTitle, lstrlen(Globals.szFileTitle));
00586             }
00587 
00588             /* The starting point for the main text */
00589             xLeft = border*2;
00590             yTop = border+szMetric.cy*4;
00591 
00592             SelectObject(printer.hDC, old_font);
00593             GetTextExtentPoint32(printer.hDC, letterM, 1, &szMetric);
00594 
00595             /* Since outputting strings is giving me problems, output the main
00596             text one character at a time.
00597             */
00598             do {
00599                 if (pTemp[i] == '\n') {
00600                     xLeft = border*2;
00601                     yTop += szMetric.cy;
00602                 }
00603                 else if (pTemp[i] != '\r') {
00604                     if (dopage)
00605                         TextOut(printer.hDC, xLeft, yTop, &pTemp[i], 1);
00606                     xLeft += szMetric.cx;
00607                 }
00608             } while (i++<size && yTop<(cHeightPels-border*2));
00609 
00610             if (dopage)
00611                 EndPage(printer.hDC);
00612             pagecount++;
00613         } while (i<size);
00614     }
00615 
00616     EndDoc(printer.hDC);
00617     DeleteDC(printer.hDC);
00618     HeapFree(GetProcessHeap(), 0, pTemp);
00619 }
00620 
00621 VOID DIALOG_FilePrinterSetup(VOID)
00622 {
00623     PRINTDLG printer;
00624 
00625     ZeroMemory(&printer, sizeof(printer));
00626     printer.lStructSize         = sizeof(printer);
00627     printer.hwndOwner           = Globals.hMainWnd;
00628     printer.hInstance           = Globals.hInstance;
00629     printer.Flags               = PD_PRINTSETUP;
00630     printer.nCopies             = 1;
00631 
00632     PrintDlg(&printer);
00633 }
00634 
00635 VOID DIALOG_FileExit(VOID)
00636 {
00637     PostMessage(Globals.hMainWnd, WM_CLOSE, 0, 0l);
00638 }
00639 
00640 VOID DIALOG_EditUndo(VOID)
00641 {
00642     SendMessage(Globals.hEdit, EM_UNDO, 0, 0);
00643 }
00644 
00645 VOID DIALOG_EditCut(VOID)
00646 {
00647     SendMessage(Globals.hEdit, WM_CUT, 0, 0);
00648 }
00649 
00650 VOID DIALOG_EditCopy(VOID)
00651 {
00652     SendMessage(Globals.hEdit, WM_COPY, 0, 0);
00653 }
00654 
00655 VOID DIALOG_EditPaste(VOID)
00656 {
00657     SendMessage(Globals.hEdit, WM_PASTE, 0, 0);
00658 }
00659 
00660 VOID DIALOG_EditDelete(VOID)
00661 {
00662     SendMessage(Globals.hEdit, WM_CLEAR, 0, 0);
00663 }
00664 
00665 VOID DIALOG_EditSelectAll(VOID)
00666 {
00667     SendMessage(Globals.hEdit, EM_SETSEL, 0, (LPARAM)-1);
00668 }
00669 
00670 VOID DIALOG_EditTimeDate(VOID)
00671 {
00672     SYSTEMTIME   st;
00673     TCHAR        szDate[MAX_STRING_LEN];
00674     TCHAR        szText[MAX_STRING_LEN * 2 + 2];
00675 
00676     GetLocalTime(&st);
00677 
00678     GetTimeFormat(LOCALE_USER_DEFAULT, 0, &st, NULL, szDate, MAX_STRING_LEN);
00679     _tcscpy(szText, szDate);
00680     _tcscat(szText, _T(" "));
00681     GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL, szDate, MAX_STRING_LEN);
00682     _tcscat(szText, szDate);
00683     SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szText);
00684 }
00685 
00686 VOID DoCreateStatusBar(VOID)
00687 {
00688     RECT rc;
00689     RECT rcstatus;
00690     BOOL bStatusBarVisible;
00691 
00692     // Check if status bar object already exists.
00693     if (Globals.hStatusBar == NULL)
00694     {
00695         // Try to create the status bar
00696         Globals.hStatusBar = CreateStatusWindow(
00697                                 WS_CHILD | WS_VISIBLE | WS_EX_STATICEDGE,
00698                                 NULL,
00699                                 Globals.hMainWnd,
00700                                 CMD_STATUSBAR_WND_ID);
00701 
00702         if (Globals.hStatusBar == NULL)
00703         {
00704             ShowLastError();
00705             return;
00706         }
00707 
00708         // Load the string for formatting column/row text output
00709         LoadString(Globals.hInstance, STRING_LINE_COLUMN, Globals.szStatusBarLineCol, MAX_PATH-1);
00710 
00711         // Set the status bar for single-text output
00712         SendMessage(Globals.hStatusBar, SB_SIMPLE, (WPARAM)TRUE, (LPARAM)0);
00713     }
00714 
00715     // Set status bar visible or not accordind the the settings.
00716     if (Globals.bWrapLongLines == TRUE ||
00717         Globals.bShowStatusBar == FALSE)
00718     {
00719         bStatusBarVisible = FALSE;
00720         ShowWindow(Globals.hStatusBar, SW_HIDE);
00721     }
00722     else
00723     {
00724         bStatusBarVisible = TRUE;
00725         ShowWindow(Globals.hStatusBar, SW_SHOW);
00726         SendMessage(Globals.hStatusBar, WM_SIZE, 0, 0);
00727     }
00728 
00729     // Set check state in show status bar item.
00730     if (Globals.bShowStatusBar == TRUE)
00731     {
00732         CheckMenuItem(Globals.hMenu, CMD_STATUSBAR, MF_BYCOMMAND | MF_CHECKED);
00733     }
00734     else
00735     {
00736         CheckMenuItem(Globals.hMenu, CMD_STATUSBAR, MF_BYCOMMAND | MF_UNCHECKED);
00737     }
00738 
00739     // Update menu mar with the previous changes
00740     DrawMenuBar(Globals.hMainWnd);
00741 
00742     // Sefety test is edit control exists
00743     if (Globals.hEdit != NULL)
00744     {
00745         // Retrieve the sizes of the controls
00746         GetClientRect(Globals.hMainWnd, &rc);
00747         GetClientRect(Globals.hStatusBar, &rcstatus);
00748 
00749         // If status bar is currently visible, update dimensions of edir control
00750         if (bStatusBarVisible)
00751             rc.bottom -= (rcstatus.bottom - rcstatus.top);
00752 
00753         // Resize edit control to right size.
00754         MoveWindow(Globals.hEdit, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, TRUE);
00755     }
00756 
00757     // Update content with current row/column text
00758     DIALOG_StatusBarUpdateCaretPos();
00759 }
00760 
00761 VOID DoCreateEditWindow(VOID)
00762 {
00763     DWORD  dwStyle;
00764     int    iSize;
00765     LPTSTR pTemp = NULL;
00766 
00767     iSize = 0;
00768 
00769     // If the edit control already exists, try to save its content
00770     if (Globals.hEdit != NULL)
00771     {
00772         // number of chars currently written into the editor.
00773         iSize = GetWindowTextLength(Globals.hEdit);
00774 
00775         if (iSize)
00776         {
00777             // Allocates temporary buffer.
00778             pTemp = HeapAlloc(GetProcessHeap(), 0, (iSize + 1) * sizeof(TCHAR));
00779 
00780             if (!pTemp)
00781             {
00782                 ShowLastError();
00783                 return;
00784             }
00785 
00786             // Recover the text into the control.
00787             GetWindowText(Globals.hEdit, pTemp, iSize + 1);
00788         }
00789 
00790         // Restore original window procedure
00791         SetWindowLongPtr(Globals.hEdit, GWLP_WNDPROC, (LONG_PTR)Globals.EditProc);
00792 
00793         // Destroy the edit control
00794         DestroyWindow(Globals.hEdit);
00795     }
00796 
00797     // Update wrap status into the main menu and recover style flags
00798     if (Globals.bWrapLongLines)
00799     {
00800         dwStyle = EDIT_STYLE_WRAP;
00801         EnableMenuItem(Globals.hMenu, CMD_STATUSBAR, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
00802     } else {
00803         dwStyle = EDIT_STYLE;
00804         EnableMenuItem(Globals.hMenu, CMD_STATUSBAR, MF_BYCOMMAND | MF_ENABLED);
00805     }
00806 
00807     // Update previous changes
00808     DrawMenuBar(Globals.hMainWnd);
00809 
00810     // Create the new edit control
00811     Globals.hEdit = CreateWindowEx(
00812                         WS_EX_CLIENTEDGE,
00813                         EDIT_CLASS,
00814                         NULL,
00815                         dwStyle,
00816                         CW_USEDEFAULT,
00817                         CW_USEDEFAULT,
00818                         CW_USEDEFAULT,
00819                         CW_USEDEFAULT,
00820                         Globals.hMainWnd,
00821                         NULL,
00822                         Globals.hInstance,
00823                         NULL);
00824 
00825     if (Globals.hEdit == NULL)
00826     {
00827         ShowLastError();
00828         return;
00829     }
00830 
00831     SendMessage(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE);
00832     SendMessage(Globals.hEdit, EM_LIMITTEXT, 0, 0);
00833 
00834     // If some text was previously saved, restore it.
00835     if (iSize != 0)
00836     {
00837         SetWindowText(Globals.hEdit, pTemp);
00838         HeapFree(GetProcessHeap(), 0, pTemp);
00839     }
00840 
00841     // Sub-class a new window callback for row/column detection.
00842     Globals.EditProc = (WNDPROC) SetWindowLongPtr(Globals.hEdit, GWLP_WNDPROC, (LONG_PTR)EDIT_WndProc);
00843 
00844     // Create/update status bar
00845     DoCreateStatusBar();
00846 
00847     // Finally shows new edit control and set focus into it.
00848     ShowWindow(Globals.hEdit, SW_SHOW);
00849     SetFocus(Globals.hEdit);
00850 }
00851 
00852 VOID DIALOG_EditWrap(VOID)
00853 {
00854     Globals.bWrapLongLines = !Globals.bWrapLongLines;
00855 
00856     DoCreateEditWindow();
00857 }
00858 
00859 VOID DIALOG_SelectFont(VOID)
00860 {
00861     CHOOSEFONT cf;
00862     LOGFONT lf=Globals.lfFont;
00863 
00864     ZeroMemory( &cf, sizeof(cf) );
00865     cf.lStructSize=sizeof(cf);
00866     cf.hwndOwner=Globals.hMainWnd;
00867     cf.lpLogFont=&lf;
00868     cf.Flags=CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT;
00869 
00870     if( ChooseFont(&cf) )
00871     {
00872         HFONT currfont=Globals.hFont;
00873 
00874         Globals.hFont=CreateFontIndirect( &lf );
00875         Globals.lfFont=lf;
00876         SendMessage( Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, (LPARAM)TRUE );
00877         if( currfont!=NULL )
00878             DeleteObject( currfont );
00879     }
00880 }
00881 
00882 typedef HWND (WINAPI *FINDPROC)(LPFINDREPLACE lpfr);
00883 
00884 static VOID DIALOG_SearchDialog(FINDPROC pfnProc)
00885 {
00886     ZeroMemory(&Globals.find, sizeof(Globals.find));
00887     Globals.find.lStructSize      = sizeof(Globals.find);
00888     Globals.find.hwndOwner        = Globals.hMainWnd;
00889     Globals.find.hInstance        = Globals.hInstance;
00890     Globals.find.lpstrFindWhat    = Globals.szFindText;
00891     Globals.find.wFindWhatLen     = SIZEOF(Globals.szFindText);
00892     Globals.find.lpstrReplaceWith = Globals.szReplaceText;
00893     Globals.find.wReplaceWithLen  = SIZEOF(Globals.szReplaceText);
00894     Globals.find.Flags            = FR_DOWN;
00895 
00896     /* We only need to create the modal FindReplace dialog which will */
00897     /* notify us of incoming events using hMainWnd Window Messages    */
00898 
00899     Globals.hFindReplaceDlg = pfnProc(&Globals.find);
00900     assert(Globals.hFindReplaceDlg !=0);
00901 }
00902 
00903 VOID DIALOG_Search(VOID)
00904 {
00905     DIALOG_SearchDialog(FindText);
00906 }
00907 
00908 VOID DIALOG_SearchNext(VOID)
00909 {
00910     if (Globals.find.lpstrFindWhat != NULL)
00911       NOTEPAD_FindNext(&Globals.find, FALSE, TRUE);
00912     else
00913       DIALOG_Search();
00914 }
00915 
00916 VOID DIALOG_Replace(VOID)
00917 {
00918     DIALOG_SearchDialog(ReplaceText);
00919 }
00920 
00921 static INT_PTR CALLBACK DIALOG_GoTo_DialogProc(HWND hwndDialog, UINT uMsg, WPARAM wParam, LPARAM lParam)
00922 {
00923     BOOL bResult = FALSE;
00924     HWND hTextBox;
00925     TCHAR szText[32];
00926 
00927     switch(uMsg) {
00928     case WM_INITDIALOG:
00929         hTextBox = GetDlgItem(hwndDialog, ID_LINENUMBER);
00930         _sntprintf(szText, SIZEOF(szText), _T("%d"), lParam);
00931         SetWindowText(hTextBox, szText);
00932         break;
00933     case WM_COMMAND:
00934         if (HIWORD(wParam) == BN_CLICKED)
00935         {
00936             if (LOWORD(wParam) == IDOK)
00937             {
00938                 hTextBox = GetDlgItem(hwndDialog, ID_LINENUMBER);
00939                 GetWindowText(hTextBox, szText, SIZEOF(szText));
00940                 EndDialog(hwndDialog, _ttoi(szText));
00941                 bResult = TRUE;
00942             }
00943             else if (LOWORD(wParam) == IDCANCEL)
00944             {
00945                 EndDialog(hwndDialog, 0);
00946                 bResult = TRUE;
00947             }
00948         }
00949         break;
00950     }
00951 
00952     return bResult;
00953 }
00954 
00955 VOID DIALOG_GoTo(VOID)
00956 {
00957     INT_PTR nLine;
00958     LPTSTR pszText;
00959     int nLength, i;
00960     DWORD dwStart, dwEnd;
00961 
00962     nLength = GetWindowTextLength(Globals.hEdit);
00963     pszText = (LPTSTR) HeapAlloc(GetProcessHeap(), 0, (nLength + 1) * sizeof(*pszText));
00964     if (!pszText)
00965         return;
00966 
00967     /* Retrieve current text */
00968     GetWindowText(Globals.hEdit, pszText, nLength + 1);
00969     SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM) &dwStart, (LPARAM) &dwEnd);
00970 
00971     nLine = 1;
00972     for (i = 0; pszText[i] && (i < (int) dwStart); i++)
00973     {
00974         if (pszText[i] == '\n')
00975             nLine++;
00976     }
00977 
00978     nLine = DialogBoxParam(Globals.hInstance, MAKEINTRESOURCE(DIALOG_GOTO),
00979         Globals.hMainWnd, DIALOG_GoTo_DialogProc, nLine);
00980 
00981     if (nLine >= 1)
00982     {
00983         for (i = 0; pszText[i] && (nLine > 1) && (i < nLength - 1); i++)
00984         {
00985             if (pszText[i] == '\n')
00986                 nLine--;
00987         }
00988         SendMessage(Globals.hEdit, EM_SETSEL, i, i);
00989         SendMessage(Globals.hEdit, EM_SCROLLCARET, 0, 0);
00990     }
00991     HeapFree(GetProcessHeap(), 0, pszText);
00992 }
00993 
00994 VOID DIALOG_StatusBarUpdateCaretPos(VOID)
00995 {
00996     int line, col;
00997     TCHAR buff[MAX_PATH];
00998     DWORD dwStart, dwSize;
00999 
01000     SendMessage(Globals.hEdit, EM_GETSEL, (WPARAM)&dwStart, (LPARAM)&dwSize);
01001     line = SendMessage(Globals.hEdit, EM_LINEFROMCHAR, (WPARAM)dwStart, 0);
01002     col  = dwStart - SendMessage(Globals.hEdit, EM_LINEINDEX, (WPARAM)line, 0);
01003 
01004     _stprintf(buff, Globals.szStatusBarLineCol, line+1, col+1);
01005     SendMessage(Globals.hStatusBar, SB_SETTEXT, SB_SIMPLEID, (LPARAM)buff);
01006 }
01007 
01008 VOID DIALOG_ViewStatusBar(VOID)
01009 {
01010     Globals.bShowStatusBar = !Globals.bShowStatusBar;
01011 
01012     DoCreateStatusBar();
01013 }
01014 
01015 VOID DIALOG_HelpContents(VOID)
01016 {
01017     WinHelp(Globals.hMainWnd, helpfile, HELP_INDEX, 0);
01018 }
01019 
01020 VOID DIALOG_HelpSearch(VOID)
01021 {
01022         /* Search Help */
01023 }
01024 
01025 VOID DIALOG_HelpHelp(VOID)
01026 {
01027     WinHelp(Globals.hMainWnd, helpfile, HELP_HELPONHELP, 0);
01028 }
01029 
01030 #ifdef _MSC_VER
01031 #pragma warning(disable : 4100)
01032 #endif
01033 INT_PTR CALLBACK
01034 AboutDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
01035 {
01036     HWND    hLicenseEditWnd;
01037     TCHAR  *strLicense;
01038 
01039     switch (message)
01040     {
01041     case WM_INITDIALOG:
01042 
01043         hLicenseEditWnd = GetDlgItem(hDlg, IDC_LICENSE);
01044 
01045         /* 0x1000 should be enought */
01046         strLicense = (TCHAR *)_alloca(0x1000);
01047         LoadString(GetModuleHandle(NULL), STRING_LICENSE, strLicense, 0x1000);
01048 
01049         SetWindowText(hLicenseEditWnd, strLicense);
01050 
01051         return TRUE;
01052 
01053     case WM_COMMAND:
01054 
01055         if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
01056         {
01057             EndDialog(hDlg, LOWORD(wParam));
01058             return TRUE;
01059         }
01060 
01061         break;
01062     }
01063 
01064     return 0;
01065 }
01066 
01067 
01068 
01069 VOID DIALOG_HelpAboutWine(VOID)
01070 {
01071     TCHAR szNotepad[MAX_STRING_LEN];
01072     HICON notepadIcon = LoadIcon(Globals.hInstance, MAKEINTRESOURCE(IDI_NPICON));
01073 
01074     LoadString(Globals.hInstance, STRING_NOTEPAD, szNotepad, SIZEOF(szNotepad));
01075     ShellAbout(Globals.hMainWnd, szNotepad, 0, notepadIcon);
01076     DeleteObject(notepadIcon);
01077 }
01078 
01079 
01080 /***********************************************************************
01081  *
01082  *           DIALOG_FilePageSetup
01083  */
01084 VOID DIALOG_FilePageSetup(void)
01085 {
01086   DialogBox(Globals.hInstance, MAKEINTRESOURCE(DIALOG_PAGESETUP),
01087             Globals.hMainWnd, DIALOG_PAGESETUP_DlgProc);
01088 }
01089 
01090 
01091 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
01092  *
01093  *           DIALOG_PAGESETUP_DlgProc
01094  */
01095 
01096 static INT_PTR WINAPI DIALOG_PAGESETUP_DlgProc(HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam)
01097 {
01098 
01099     switch (msg)
01100     {
01101     case WM_COMMAND:
01102         if (HIWORD(wParam) == BN_CLICKED)
01103         {
01104             switch (LOWORD(wParam))
01105             {
01106             case IDOK:
01107                 /* save user input and close dialog */
01108                 GetDlgItemText(hDlg, 0x141, Globals.szHeader, SIZEOF(Globals.szHeader));
01109                 GetDlgItemText(hDlg, 0x143, Globals.szFooter, SIZEOF(Globals.szFooter));
01110                 GetDlgItemText(hDlg, 0x14A, Globals.szMarginTop, SIZEOF(Globals.szMarginTop));
01111                 GetDlgItemText(hDlg, 0x150, Globals.szMarginBottom, SIZEOF(Globals.szMarginBottom));
01112                 GetDlgItemText(hDlg, 0x147, Globals.szMarginLeft, SIZEOF(Globals.szMarginLeft));
01113                 GetDlgItemText(hDlg, 0x14D, Globals.szMarginRight, SIZEOF(Globals.szMarginRight));
01114                 EndDialog(hDlg, IDOK);
01115                 return TRUE;
01116 
01117             case IDCANCEL:
01118                 /* discard user input and close dialog */
01119                 EndDialog(hDlg, IDCANCEL);
01120                 return TRUE;
01121 
01122             case IDHELP:
01123                 {
01124                     /* FIXME: Bring this to work */
01125                     static const TCHAR sorry[] = _T("Sorry, no help available");
01126                     static const TCHAR help[] = _T("Help");
01127                     MessageBox(Globals.hMainWnd, sorry, help, MB_ICONEXCLAMATION);
01128                     return TRUE;
01129                 }
01130 
01131             default:
01132                 break;
01133             }
01134         }
01135         break;
01136 
01137     case WM_INITDIALOG:
01138         /* fetch last user input prior to display dialog */
01139         SetDlgItemText(hDlg, 0x141, Globals.szHeader);
01140         SetDlgItemText(hDlg, 0x143, Globals.szFooter);
01141         SetDlgItemText(hDlg, 0x14A, Globals.szMarginTop);
01142         SetDlgItemText(hDlg, 0x150, Globals.szMarginBottom);
01143         SetDlgItemText(hDlg, 0x147, Globals.szMarginLeft);
01144         SetDlgItemText(hDlg, 0x14D, Globals.szMarginRight);
01145         break;
01146     }
01147 
01148     return FALSE;
01149 }

Generated on Sun May 27 2012 04:16:36 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.