4 * Copyright 2000 Mike McCormack <Mike_McCormack@looksmart.com.au>
5 * Copyright 1997,98 Marcel Baur <mbaur@g26.ethz.ch>
6 * To be distributed under the Wine License
9 * - Use wine Heap instead of malloc/free (done)
10 * - use scroll bars (vertical done)
11 * - cut 'n paste (clipboard)
15 * - encapsulate data structures (?) - half done
16 * - free unused memory
17 * - solve Open problems
18 * - smoother scrolling
19 * - seperate view code and document code
21 * This program is intended as a testbed for winelib as much as
22 * a useful application.
37 extern BOOL DoCloseFile(void);
38 extern void DoOpenFile(LPCSTR szFileName);
40 NOTEPAD_GLOBALS Globals;
43 /* Using a pointer to pointer data structure to
44 achieve a little more efficiency. Hopefully
45 it will be worth it, because it complicates the
46 code - mjm 26 Jun 2000 */
48 #define BUFFERCHUNKSIZE 0xe0
49 typedef struct TAGLine {
55 /* FIXME: make this info into a structure */
56 /* typedef struct tagBUFFER { */
61 DWORD dwXpos=0,dwYpos=0; /* position of caret in char coords */
62 DWORD dwCaretXpos=0,dwCaretYpos=0; /* position of caret in pixel coords */
63 TEXTMETRIC tm; /* textmetric for current font */
64 RECT rectClient; /* client rectangle of the window we're drawing in */
65 /* } BUFFER, *LPBUFFER */
67 VOID InitFontInfo(HWND hWnd)
69 HDC hDC = GetDC(hWnd);
73 GetTextMetrics(hDC, &tm);
87 /* convert x,y character co-ords into x pixel co-ord */
88 DWORD CalcStringWidth(HDC hDC, DWORD x, DWORD y)
100 if(lpBuffer[y].lpLine == NULL)
102 len = (x<lpBuffer[y].dwWidth) ?
103 x : lpBuffer[y].dwWidth;
104 GetTextExtentPoint(hDC, lpBuffer[y].lpLine, len, &size);
109 void CalcCaretPos(HDC hDC, DWORD dwXpos, DWORD dwYpos)
111 dwCaretXpos = CalcStringWidth(hDC, dwXpos, dwYpos);
112 dwCaretYpos = tm.tmHeight*(dwYpos-dwVOffset);
113 SetCaretPos(dwCaretXpos,dwCaretYpos);
116 DWORD GetLinesPerPage(HWND hWnd)
118 return (rectClient.bottom/tm.tmHeight); /* round down */
121 /* render one line of text and blank space */
122 void RenderLine(HDC hDC, DWORD lineno)
130 /* erase the space at the end of a line using a white rectangle */
131 rect.top = tm.tmHeight*(lineno-dwVOffset);
132 rect.bottom = tm.tmHeight*(lineno-dwVOffset+1);
134 if(lpBuffer && (lineno<dwLines))
135 rect.left = CalcStringWidth(hDC, lpBuffer[lineno].dwWidth,lineno);
138 rect.right = rectClient.right;
140 /* use the white pen so there's not outline */
141 hPrev = SelectObject(hDC, GetStockObject(WHITE_PEN));
142 Rectangle(hDC, rect.left, rect.top, rect.right, rect.bottom);
143 SelectObject(hDC, hPrev);
145 if(lpBuffer && lpBuffer[lineno].lpLine)
147 TextOut(hDC, 0, rect.top, lpBuffer[lineno].lpLine,
148 lpBuffer[lineno].dwWidth);
153 * Paint the buffer onto the window.
155 void RenderWindow(HDC hDC) {
161 /* FIXME: render only necessary lines */
162 for(i = dwVOffset; i < (dwVOffset+GetLinesPerPage(0)); i++)
169 * Check that correct buffers exist to access line y pos x
170 * Only manages memory.
172 * Returns TRUE if the line is accessable
173 * FALSE if there is a problem
175 BOOL ValidateLine(DWORD y,DWORD x)
179 /* check to see that the BUFFER has enough lines */
181 if( (max<=y) || (lpBuffer == NULL))
184 max += BUFFERCHUNKSIZE;
185 /* use GlobalAlloc first time round */
187 lpBuffer = (LPLINE) GlobalReAlloc((HGLOBAL)lpBuffer,GMEM_FIXED,
190 lpBuffer = (LPLINE) GlobalAlloc(GMEM_FIXED, max*sizeof(LINE));
193 ZeroMemory(&lpBuffer[dwLines], sizeof(LINE)*(max-dwLines) );
197 /* check to see that the LINE is wide enough */
198 max = lpBuffer[y].dwMaxWidth;
199 if( (max <= x) || (lpBuffer[y].lpLine == NULL) )
202 max += BUFFERCHUNKSIZE;
203 /* use GlobalAlloc first */
204 if(lpBuffer[y].lpLine)
205 lpBuffer[y].lpLine = (LPSTR)GlobalReAlloc((HGLOBAL)lpBuffer[y].lpLine,
208 lpBuffer[y].lpLine = (LPSTR)GlobalAlloc( GMEM_FIXED, max);
209 if(lpBuffer[y].lpLine == NULL)
211 lpBuffer[y].dwWidth = 0;
212 lpBuffer[y].dwMaxWidth = max;
217 /* inserts a new line into the buffer */
218 BOOL DoNewLine(HDC hDC)
223 /* check to see if we need more memory for the buffer pointers */
224 if(!ValidateLine(dwLines,0))
227 /* shuffle up all the lines */
228 for(i=dwLines; i>(dwYpos+1); i--)
230 lpBuffer[i] = lpBuffer[i-1];
233 ZeroMemory(&lpBuffer[dwYpos+1],sizeof(LINE));
235 /* copy the characters after the carat (if any) to the next line */
236 src = &lpBuffer[dwYpos].lpLine[dwXpos];
237 cnt = lpBuffer[dwYpos].dwWidth-dwXpos;
238 if(!ValidateLine(dwYpos+1,cnt)) /* allocates the buffer */
239 return FALSE; /* FIXME */
240 dst = &lpBuffer[dwYpos+1].lpLine[0];
241 memcpy(dst, src, cnt);
242 lpBuffer[dwYpos+1].dwWidth = cnt;
243 lpBuffer[dwYpos].dwWidth -= cnt;
245 /* move the cursor */
250 /* update the window */
251 RenderLine(hDC, dwYpos-1);
252 RenderLine(hDC, dwYpos);
253 CalcCaretPos(hDC, dwXpos, dwYpos);
254 /* FIXME: don't use globals */
255 SetScrollRange(Globals.hMainWnd, SB_VERT, 0, dwLines, TRUE);
261 * Attempt a basic edit buffer
263 BOOL AddCharToBuffer(HDC hDC, char ch)
265 /* we can use lpBuffer[dwYpos] */
266 if(!ValidateLine(dwYpos,0))
269 /* shuffle the rest of the line*/
270 if(!ValidateLine(dwYpos, lpBuffer[dwYpos].dwWidth))
272 lpBuffer[dwYpos].dwWidth++;
273 memmove(&lpBuffer[dwYpos].lpLine[dwXpos+1],
274 &lpBuffer[dwYpos].lpLine[dwXpos],
275 lpBuffer[dwYpos].dwWidth-dwXpos);
277 /* add the character */
278 lpBuffer[dwYpos].lpLine[dwXpos] = ch;
283 /* update the window and cursor position */
284 RenderLine(hDC,dwYpos);
285 CalcCaretPos(hDC,dwXpos,dwYpos);
291 /* erase a character */
292 BOOL DoBackSpace(HDC hDC)
298 if(lpBuffer[dwYpos].lpLine && (dwXpos>0))
301 /* FIXME: use memmove */
302 for(i=dwXpos; i<(lpBuffer[dwYpos].dwWidth-1); i++)
303 lpBuffer[dwYpos].lpLine[i]=lpBuffer[dwYpos].lpLine[i+1];
305 lpBuffer[dwYpos].dwWidth--;
306 RenderLine(hDC, dwYpos);
307 CalcCaretPos(hDC,dwXpos,dwYpos);
311 /* Erase a newline. To do this we join two lines */
318 oldlen = lpBuffer[dwYpos-1].dwWidth;
319 if(lpBuffer[dwYpos-1].lpLine&&lpBuffer[dwYpos].lpLine)
321 /* concatonate to the end of the line above line */
322 src = &lpBuffer[dwYpos].lpLine[0];
323 dest = &lpBuffer[dwYpos-1].lpLine[lpBuffer[dwYpos-1].dwWidth];
324 len = lpBuffer[dwYpos].dwWidth;
326 /* check the length of the new line */
327 if(!ValidateLine(dwYpos-1,lpBuffer[dwYpos-1].dwWidth + len))
330 memcpy(dest,src,len);
331 lpBuffer[dwYpos-1].dwWidth+=len;
332 GlobalFree( (HGLOBAL)lpBuffer[dwYpos].lpLine);
334 else if (!lpBuffer[dwYpos-1].lpLine)
336 lpBuffer[dwYpos-1]=lpBuffer[dwYpos];
337 } /* else both are NULL */
338 RenderLine(hDC,dwYpos-1);
340 /* don't zero - it's going to get trashed anyhow */
342 /* shuffle up all the lines below this one */
343 for(i=dwYpos; i<(dwLines-1); i++)
345 lpBuffer[i] = lpBuffer[i+1];
349 /* clear the last line */
350 ZeroMemory(&lpBuffer[dwLines-1],sizeof (LINE));
351 RenderLine(hDC,dwLines-1);
354 /* adjust the cursor position to joining point */
358 CalcCaretPos(hDC,dwXpos,dwYpos);
359 SetScrollRange(Globals.hMainWnd, SB_VERT, 0, dwLines, TRUE);
364 /* as used by File->New */
365 void TrashBuffer(void)
369 /* variables belonging to the buffer */
372 for(i=0; i<dwLines; i++)
374 if(lpBuffer[i].lpLine)
375 GlobalFree ((HGLOBAL)lpBuffer[i].lpLine);
376 ZeroMemory(&lpBuffer[i],sizeof (LINE));
378 GlobalFree((HGLOBAL)lpBuffer);
384 /* variables belonging to the view */
388 /* FIXME: don't use globals */
389 SetScrollPos(Globals.hMainWnd, SB_VERT, dwVOffset, FALSE);
390 SetScrollRange(Globals.hMainWnd, SB_VERT, 0, dwLines, TRUE);
395 * Add a line to the buffer
397 /* FIXME: this breaks lines longer than BUFFERCHUNKSIZE */
399 LPSTR buffer, /* pointer to buffer with file data */
400 DWORD size, /* number of bytes available in buffer */
408 for(i=0; i<size; i++)
412 if(ValidateLine(dwLines,i))
414 memcpy(&lpBuffer[dwLines].lpLine[0],&buffer[0],i);
415 lpBuffer[dwLines].dwWidth = i;
422 /* make a line of the rest */
423 if( (i == BUFFERCHUNKSIZE) || nomore )
425 if(ValidateLine(dwLines,i))
427 memcpy(&lpBuffer[dwLines].lpLine[0],&buffer[0],i);
428 lpBuffer[dwLines].dwWidth = i;
439 * This is probably overcomplicated by lpBuffer data structure...
440 * Read blocks from the file, then add each line from the
441 * block to the buffer until there is none left. If all
442 * a slab isn't used, try load some more data from the file.
444 void LoadBufferFromFile(LPCSTR szFileName)
449 DWORD size,i,len,bytes_left,bytes_read;
451 hFile = OpenFile(szFileName, &ofs, OF_READ);
452 if(hFile == INVALID_HANDLE_VALUE)
454 size = BUFFERCHUNKSIZE;
455 pTemp = (LPSTR) GlobalAlloc(GMEM_FIXED, size);
458 bytes_read = 1; /* anything non-zero */
467 bytes_left+=bytes_read;
469 /* add strings to buffer */
472 (len = CreateLine(&pTemp[i], bytes_left, !bytes_read));
473 i+= len,bytes_left-=len );
475 /* move leftover to front of buffer */
477 memmove(&pTemp[0],&pTemp[i], bytes_left);
480 MessageBox(Globals.hMainWnd, "Finished", "Info", MB_OK);
483 BOOL DoInput(HDC hDC, WPARAM wParam, LPARAM lParam)
488 return DoBackSpace(hDC);
490 return DoNewLine(hDC);
492 return AddCharToBuffer(hDC,wParam);
496 BOOL GotoHome(HWND hWnd)
504 BOOL GotoEndOfLine(HWND hWnd)
506 dwXpos = lpBuffer[dwYpos].dwWidth;
510 BOOL GotoDown(HWND hWnd)
512 if((dwYpos+1) >= dwLines)
517 if (dwXpos>lpBuffer[dwYpos].dwWidth)
522 BOOL GotoUp(HWND hWnd)
527 if (dwXpos>lpBuffer[dwYpos].dwWidth)
532 BOOL GotoLeft(HWND hWnd)
540 return GotoEndOfLine(hWnd);
544 BOOL GotoRight(HWND hWnd)
546 if(dwXpos<lpBuffer[dwYpos].dwWidth)
557 /* check the caret is still on the screen */
558 BOOL ScrollABit(HWND hWnd)
565 if(dwYpos>(dwVOffset+GetLinesPerPage(hWnd)))
567 dwVOffset = dwYpos - GetLinesPerPage(hWnd) + 1;
573 /* FIXME: move the window around so we can still see the caret */
574 VOID DoEdit(HWND hWnd, WPARAM wParam, LPARAM lParam)
582 case VK_HOME: GotoHome(hWnd);
585 case VK_END: GotoEndOfLine(hWnd);
588 case VK_LEFT: GotoLeft(hWnd);
591 case VK_RIGHT: GotoRight(hWnd);
594 case VK_DOWN: GotoDown(hWnd);
597 case VK_UP: GotoUp(hWnd);
607 CalcCaretPos(hDC, dwXpos, dwYpos);
611 InvalidateRect(hWnd, NULL, FALSE);
614 void ButtonDownToCaretPos(HWND hWnd, WPARAM wParam, LPARAM lParam)
616 DWORD x, y, caretx, carety;
617 BOOL refine_guess = TRUE;
623 caretx = x/tm.tmAveCharWidth; /* guess */
624 carety = dwVOffset + y/tm.tmHeight;
632 refine_guess = FALSE;
635 /* if the cursor is past the bottom, put it after the last char */
636 if(refine_guess && (carety>=dwLines) )
639 caretx=lpBuffer[carety].dwWidth;
640 refine_guess = FALSE;
643 /* cursor past end of line? */
644 if(refine_guess && (x>CalcStringWidth(hDC,lpBuffer[carety].dwWidth,carety)))
646 caretx = lpBuffer[carety].dwWidth;
647 refine_guess = FALSE;
650 /* FIXME: doesn't round properly */
653 if(CalcStringWidth(hDC,caretx,carety)<x)
655 while( (caretx<lpBuffer[carety].dwWidth) &&
656 (CalcStringWidth(hDC,caretx+1,carety)<x))
661 while((caretx>0)&&(CalcStringWidth(hDC,caretx-1,carety)>x))
666 /* set the caret's position */
669 CalcCaretPos(hDC, caretx, carety);
673 void DoScroll(HWND hWnd, WPARAM wParam, LPARAM lParam)
675 DWORD dy = GetLinesPerPage(hWnd);
677 switch(wParam) /* vscroll code */
684 if(dwVOffset<dwLines)
688 if( (dy+dwVOffset) > dwLines)
689 dwVOffset = dwLines - 1;
696 /* position scroll */
697 SetScrollPos(hWnd, SB_VERT, dwVOffset, TRUE);
700 /***********************************************************************
702 * NOTEPAD_MenuCommand
704 * All handling of main menu events
707 int NOTEPAD_MenuCommand (WPARAM wParam)
710 case NP_FILE_NEW: DIALOG_FileNew(); break;
711 case NP_FILE_OPEN: DIALOG_FileOpen(); break;
712 case NP_FILE_SAVE: DIALOG_FileSave(); break;
713 case NP_FILE_SAVEAS: DIALOG_FileSaveAs(); break;
714 case NP_FILE_PRINT: DIALOG_FilePrint(); break;
715 case NP_FILE_PAGESETUP: DIALOG_FilePageSetup(); break;
716 case NP_FILE_PRINTSETUP: DIALOG_FilePrinterSetup();break;
717 case NP_FILE_EXIT: DIALOG_FileExit(); break;
719 case NP_EDIT_UNDO: DIALOG_EditUndo(); break;
720 case NP_EDIT_CUT: DIALOG_EditCut(); break;
721 case NP_EDIT_COPY: DIALOG_EditCopy(); break;
722 case NP_EDIT_PASTE: DIALOG_EditPaste(); break;
723 case NP_EDIT_DELETE: DIALOG_EditDelete(); break;
724 case NP_EDIT_SELECTALL: DIALOG_EditSelectAll(); break;
725 case NP_EDIT_TIMEDATE: DIALOG_EditTimeDate();break;
726 case NP_EDIT_WRAP: DIALOG_EditWrap(); break;
728 case NP_SEARCH_SEARCH: DIALOG_Search(); break;
729 case NP_SEARCH_NEXT: DIALOG_SearchNext(); break;
731 case NP_HELP_CONTENTS: DIALOG_HelpContents(); break;
732 case NP_HELP_SEARCH: DIALOG_HelpSearch(); break;
733 case NP_HELP_ON_HELP: DIALOG_HelpHelp(); break;
734 case NP_HELP_LICENSE: DIALOG_HelpLicense(); break;
735 case NP_HELP_NO_WARRANTY: DIALOG_HelpNoWarranty(); break;
736 case NP_HELP_ABOUT_WINE: DIALOG_HelpAboutWine(); break;
738 /* Handle languages */
740 LANGUAGE_DefaultHandle(wParam);
747 /***********************************************************************
752 LRESULT NOTEPAD_WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
756 HANDLE hDrop; /* drag & drop */
757 CHAR szFileName[MAX_STRING_LEN];
760 lstrcpy(szFileName, "");
765 GetClientRect(hWnd, &rectClient);
770 CreateCaret(Globals.hMainWnd, 0, 1, tm.tmHeight);
771 SetCaretPos(dwCaretXpos, dwCaretYpos);
772 ShowCaret(Globals.hMainWnd);
780 GetClientRect(hWnd, &rectClient);
781 hContext = BeginPaint(hWnd, &ps);
782 RenderWindow(hContext);
787 DoEdit(hWnd, wParam, lParam);
791 GetClientRect(hWnd, &rectClient);
793 hContext = GetDC(hWnd);
794 DoInput(hContext,wParam,lParam);
795 ReleaseDC(hWnd,hContext);
800 /* figure out where the mouse was clicked */
801 ButtonDownToCaretPos(hWnd, wParam, lParam);
805 DoScroll(hWnd, wParam, lParam);
806 InvalidateRect(hWnd, NULL, FALSE); /* force a redraw */
810 /* FIXME: this is a bit messy */
811 NOTEPAD_MenuCommand(wParam);
812 InvalidateRect(hWnd, NULL, FALSE); /* force a redraw */
813 hContext = GetDC(hWnd);
814 CalcCaretPos(hContext,dwXpos,dwYpos);
815 ReleaseDC(hWnd,hContext);
818 case WM_DESTROYCLIPBOARD:
819 MessageBox(Globals.hMainWnd, "Empty clipboard", "Debug", MB_ICONEXCLAMATION);
833 GetClientRect(Globals.hMainWnd, &Windowsize);
837 /* User has dropped a file into main window */
838 hDrop = (HANDLE) wParam;
839 DragQueryFile(hDrop, 0, (CHAR *) &szFileName, sizeof(szFileName));
841 DoOpenFile(szFileName);
845 return DefWindowProc (hWnd, msg, wParam, lParam);
852 /***********************************************************************
857 int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE prev, LPSTR cmdline, int show)
861 char className[] = "NPClass"; /* To make sure className >= 0x10000 */
862 char winName[] = "Notepad";
864 /* Select Language */
872 Globals.lpszIniFile = "notepad.ini";
873 Globals.lpszIcoFile = "notepad.ico";
875 Globals.hInstance = hInstance;
878 Globals.hMainIcon = ExtractIcon(Globals.hInstance,
879 Globals.lpszIcoFile, 0);
881 if (!Globals.hMainIcon) {
882 Globals.hMainIcon = LoadIcon(0, MAKEINTRESOURCE(DEFAULTICON));
885 lstrcpy(Globals.szFindText, "");
886 lstrcpy(Globals.szFileName, "");
887 lstrcpy(Globals.szMarginTop, "25 mm");
888 lstrcpy(Globals.szMarginBottom, "25 mm");
889 lstrcpy(Globals.szMarginLeft, "20 mm");
890 lstrcpy(Globals.szMarginRight, "20 mm");
891 lstrcpy(Globals.szHeader, "&n");
892 lstrcpy(Globals.szFooter, "Page &s");
893 lstrcpy(Globals.Buffer, "Hello World");
896 class.style = CS_HREDRAW | CS_VREDRAW;
897 class.lpfnWndProc = NOTEPAD_WndProc;
898 class.cbClsExtra = 0;
899 class.cbWndExtra = 0;
900 class.hInstance = Globals.hInstance;
901 class.hIcon = LoadIcon (0, IDI_APPLICATION);
902 class.hCursor = LoadCursor (0, IDC_ARROW);
903 class.hbrBackground = GetStockObject (WHITE_BRUSH);
904 class.lpszMenuName = 0;
905 class.lpszClassName = className;
908 if (!RegisterClass (&class)) return FALSE;
913 Globals.hMainWnd = CreateWindow (className, winName,
914 WS_OVERLAPPEDWINDOW + WS_HSCROLL + WS_VSCROLL,
915 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0,
916 LoadMenu(Globals.hInstance, STRING_MENU_Xx),
917 Globals.hInstance, 0);
919 Globals.hFindReplaceDlg = 0;
921 LANGUAGE_SelectByName(Globals.lpszLanguage);
923 SetMenu(Globals.hMainWnd, Globals.hMainMenu);
925 ShowWindow (Globals.hMainWnd, show);
926 UpdateWindow (Globals.hMainWnd);
930 /* Identify Messages originating from FindReplace */
932 Globals.nCommdlgFindReplaceMsg = RegisterWindowMessage("commdlg_FindReplace");
933 if (Globals.nCommdlgFindReplaceMsg==0) {
934 MessageBox(Globals.hMainWnd, "Could not register commdlg_FindReplace window message",
935 "Error", MB_ICONEXCLAMATION);
938 /* now handle command line */
940 while (*cmdline && (*cmdline == ' ' || *cmdline == '-'))
944 /* LPCSTR topic_id; */
946 if (*cmdline++ == ' ') continue;
949 if (option) cmdline++;
950 while (*cmdline && *cmdline == ' ') cmdline++;
955 case 'P': printf("Print file: ");
956 /* Not yet able to print a file */
961 /* Set up Drag&Drop */
963 DragAcceptFiles(Globals.hMainWnd, TRUE);
965 /* now enter mesage loop */
967 while (GetMessage (&msg, 0, 0, 0)) {
968 if (IsDialogMessage(Globals.hFindReplaceDlg, &msg)!=0) {
969 /* Message belongs to FindReplace dialog */
970 /* We just let IsDialogMessage handle it */
974 /* Message belongs to the Notepad Main Window */
975 TranslateMessage (&msg);
976 DispatchMessage (&msg);