|
From: Anthony W. <fo...@us...> - 2005-11-11 03:28:27
|
Update of /cvsroot/notepro/pete/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9643/src Added Files: main.cpp npfile.cpp npwindow.cpp Log Message: --- NEW FILE: main.cpp --- #include "npwindow.h" CNPWindow *window = 0; LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) { switch (nMsg) { case WM_SIZE: { window->onSize(lParam); } break; case WM_COMMAND: { window->onMenuCmd(wParam); } break; case WM_CLOSE: { DestroyWindow(hWnd); } break; case WM_DESTROY: { PostQuitMessage(0); } break; case WM_NOTIFY: { switch (HIWORD(wParam)) { case TCN_SELCHANGE: { window->setActiveTab(TabCtrl_GetCurSel(window->getTabWnd())); MessageBox(0, (char *)TabCtrl_GetCurSel(window->getTabWnd()), 0, 0); } break; } } break; default: return DefWindowProc(hWnd, nMsg, wParam, lParam); } return 0; } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) { window = new CNPWindow(hInstance); if (!window->createWindow(800, 600, WndProc)) return -1; window->enterLoop(); return 0; } --- NEW FILE: npwindow.cpp --- #include "npwindow.h" CNPWindow *_window = 0; CNPWindow::CNPWindow(HINSTANCE hInstance) { _hInstance = hInstance; _file = new CNPFile(); } bool CNPWindow::createWindow(int width, int height, LRESULT (CALLBACK *WndProc)(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)) { _width = width; _height = height; WNDCLASSEX wc; wc.cbClsExtra = 0; wc.cbSize = sizeof(WNDCLASSEX); wc.cbWndExtra = 0; wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); wc.hInstance = _hInstance; wc.lpfnWndProc = WndProc; wc.lpszClassName = "notepro"; wc.lpszMenuName = MAKEINTRESOURCE(IDR_MAIN_MENU); wc.style = NULL; if (!RegisterClassEx(&wc)) return false; _hWnd = CreateWindowEx(WS_EX_APPWINDOW, "notepro", "PETE [Untitled]", WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, CW_USEDEFAULT, CW_USEDEFAULT, _width, _height, NULL, NULL, _hInstance, NULL); if (!_hWnd) return false; RECT rcWnd; TCITEM tci; GetClientRect(_hWnd, &rcWnd); _hTabWnd = CreateWindow(WC_TABCONTROL, "", WS_CHILD | WS_CLIPSIBLINGS | WS_VISIBLE, 0, 0, rcWnd.right, rcWnd.bottom, _hWnd, NULL, _hInstance, NULL); if (!_hTabWnd) return false; tci.mask = TCIF_TEXT; tci.pszText = "Untitled"; _curTab = 0; TabCtrl_InsertItem(_hTabWnd, 0, &tci); _hScinInst = LoadLibrary("SciLexer.dll"); if (!_hScinInst) MessageBox(NULL, "Failed to load Scintilla library", "Initialization Error!", MB_ICONERROR | MB_OK); HWND hScinWndTmp = CreateWindowEx(0, "Scintilla", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_HSCROLL | WS_VSCROLL, 111, 111, _width - 9, _height - 27, _hTabWnd, 0, _hInstance, NULL); if (!hScinWndTmp) MessageBox(NULL, "Failed to create Scintilla window", "Initializatiob Error!", MB_ICONERROR | MB_OK); _hScinWnd.push_back(hScinWndTmp); ShowWindow(_hWnd, SW_SHOW); UpdateWindow(_hWnd); _file->setScinWnd(_hScinWnd[0]); _hAccel = LoadAccelerators(_hInstance, MAKEINTRESOURCE(IDR_NP_ACCEL)); SendMessage(_hScinWnd[0], SCI_STYLESETFONT, 0, (LPARAM)"Lucida Console"); SendMessage(_hScinWnd[0], SCI_STYLESETSIZE, 0, 10); strcpy(_logFont.lfFaceName, "Lucida Console"); _logFont.lfHeight = 0; _logFont.lfWidth = 0; _hMenu = GetMenu(_hWnd); SendMessage(_hScinWnd[0], SCI_SETWRAPMODE, 1, 0); return true; } void CNPWindow::enterLoop() { MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { if (TranslateAccelerator(_hWnd, _hAccel, &msg) == 0) { TranslateMessage(&msg); DispatchMessage(&msg); } } } void CNPWindow::open() { char file[MAX_PATH]; OPENFILENAME ofn; ::ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = _hWnd; ofn.lpstrInitialDir = "C:\\"; ofn.lpstrFilter = NP_FILE_FILTER; ofn.lpstrTitle = "Open File"; ofn.Flags = OFN_CREATEPROMPT; ofn.nFilterIndex = 1; ofn.lpstrFile = file; ofn.lpstrFile[0] = '\0'; ofn.nMaxFile = MAX_PATH; if (GetOpenFileName(&ofn)) { SendMessage(_hScinWnd[0], SCI_CLEARALL, 0, 0); _file->loadFile((char*)ofn.lpstrFile); char msg[MAX_PATH + 100]; sprintf(msg, "PETE [%s]", file); SetWindowText(_hWnd, msg); } } void CNPWindow::save() { if (_file->getFilename() == 0) { saveAs(); } else { _file->writeFile(); } } void CNPWindow::saveAs() { char file[MAX_PATH]; OPENFILENAME ofn; ::ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn); ofn.hwndOwner = _hWnd; ofn.lpstrFilter = NP_FILE_FILTER; ofn.lpstrInitialDir = "C:\\"; ofn.lpstrTitle = "Open File"; ofn.Flags = OFN_CREATEPROMPT; ofn.lpstrFile = file; ofn.lpstrFile[0] = '\0'; ofn.nFilterIndex = 1; ofn.nMaxFile = MAX_PATH; if (GetSaveFileName(&ofn)) { _file->setFilename((char*)ofn.lpstrFile); _file->writeFile(); char msg[MAX_PATH + 100]; sprintf(msg, "PETE [%s]", file); SetWindowText(_hWnd, msg); } } void CNPWindow::chooseFont() { CHOOSEFONT cf; ::ZeroMemory(&cf, sizeof(cf)); cf.hwndOwner = _hWnd; cf.lpLogFont = &_logFont; cf.lStructSize = sizeof(CHOOSEFONT); cf.Flags = CF_SCREENFONTS | CF_INITTOLOGFONTSTRUCT; if (ChooseFont(&cf)) { SendMessage(_hScinWnd[0], SCI_STYLESETFONT, 0, (LPARAM)_logFont.lfFaceName); SendMessage(_hScinWnd[0], SCI_STYLESETSIZE, 0, cf.iPointSize / 10); if (cf.nFontType & BOLD_FONTTYPE) SendMessage(_hScinWnd[0], SCI_STYLESETBOLD, 0, true); else SendMessage(_hScinWnd[0], SCI_STYLESETBOLD, 0, false); if (cf.nFontType & ITALIC_FONTTYPE) SendMessage(_hScinWnd[0], SCI_STYLESETITALIC, 0, true); else SendMessage(_hScinWnd[0], SCI_STYLESETITALIC, 0, false); } } // MESSAGE HANDLERS void CNPWindow::onSize(LPARAM lParam) { _width = (int)LOWORD(lParam); _height = (int)HIWORD(lParam); MoveWindow(_hTabWnd, 0, 0, _width, _height, TRUE); MoveWindow(_hScinWnd[0], 5, 25, _width - 10, _height - 30, TRUE); } void CNPWindow::onMenuCmd(WPARAM wParam) { switch (LOWORD(wParam)) { case IDM_FORMAT_WORDWRAP: { if (GetMenuState(_hMenu, IDM_FORMAT_WORDWRAP, MF_BYCOMMAND) & MF_CHECKED) { SendMessage(_hScinWnd[0], SCI_SETWRAPMODE, 0, 0); CheckMenuItem(_hMenu, IDM_FORMAT_WORDWRAP, MF_UNCHECKED); } else { SendMessage(_hScinWnd[0], SCI_SETWRAPMODE, 1, 0); CheckMenuItem(_hMenu, IDM_FORMAT_WORDWRAP, MF_CHECKED); } } break; case IDM_FORMAT_FONT: { chooseFont(); } break; case IDM_EDIT_CUT: { SendMessage(_hScinWnd[_curTab], SCI_CUT, 0, 0); } break; case IDM_EDIT_COPY: { SendMessage(_hScinWnd[_curTab], SCI_COPY, 0, 0); } break; case IDM_EDIT_PASTE: { SendMessage(_hScinWnd[_curTab], SCI_PASTE, 0, 0); } break; case IDM_EDIT_UNDO: { SendMessage(_hScinWnd[_curTab], SCI_UNDO, 0, 0); } break; case IDM_EDIT_REDO: { SendMessage(_hScinWnd[_curTab], SCI_REDO, 0, 0); } break; case IDM_EDIT_FIND_NP: { _window = this; DialogBox(_hInstance, MAKEINTRESOURCE(IDD_FIND_REPLACE), _hWnd, (DLGPROC)FindReplaceDlgProc); } break; case ID_NEW_CURRENTTAB: { SendMessage(_hScinWnd[_curTab], SCI_CLEARALL, 0, 0); SetWindowText(_hWnd, "PETE [Untitled]"); _file->setFilename(0); } break; case ID_NEW_NEWTAB: { TCITEM tci; tci.mask = TCIF_TEXT; char tmp[80]; sprintf(tmp, "Untitled %d", TabCtrl_GetItemCount(_hTabWnd) + 1); tci.pszText = tmp; TabCtrl_InsertItem(_hTabWnd, TabCtrl_GetItemCount(_hTabWnd) + 1, &tci); HWND hScinWndTmp = CreateWindowEx(0, "Scintilla", "", WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_HSCROLL | WS_VSCROLL, 111, 111, _width - 9, _height - 27, _hTabWnd, 0, _hInstance, NULL); _hScinWnd.push_back(hScinWndTmp); } break; case IDM_FILE_SAVE: { save(); } break; case IDM_FILE_SAVEAS: { saveAs(); } break; case IDM_FILE_OPEN: { open(); } break; case IDM_FILE_EXIT: { DestroyWindow(_hWnd); } break; case IDM_HELP_ABOUT: { DialogBox(_hInstance, MAKEINTRESOURCE(IDD_ABOUT_NP), _hWnd, (DLGPROC)AboutDlgProc); } break; } } // Tab Control void CNPWindow::setActiveTab(int tab) { _curTab = tab; } // DIALOG PROCEDURES BOOL CALLBACK AboutDlgProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) { switch (nMsg) { case WM_INITDIALOG: { char msg[1024]; strcpy(msg, "PETE version 0.6\r\n"); strcat(msg, " Featuring Scintilla text editing\r\n\r\n"); strcat(msg, "Written By:\r\n"); strcat(msg, " Anthony White (foo-bar)\r\n"); strcat(msg, " foo...@co...\r\n"); strcat(msg, " Paul Sullivan (paullyo)"); SetDlgItemText(hWnd, IDC_ABOUT_NP, msg); } case WM_COMMAND: { if (LOWORD(wParam) == IDOK) EndDialog(hWnd, 0); } break; default: return FALSE; } return TRUE; } BOOL CALLBACK FindReplaceDlgProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) { static WPARAM targetStart; WPARAM start; LPARAM end; switch (nMsg) { case WM_INITDIALOG: { targetStart = 0; SetFocus(GetDlgItem(hWnd, IDC_FIND_TEXT)); } break; case WM_COMMAND: { switch (LOWORD(wParam)) { case IDC_FIND_NEXT: { char str[80]; GetDlgItemText(hWnd, IDC_FIND_TEXT, str, 80); HWND hScinWnd = _window->getScinWnd(); SendMessage(hScinWnd, SCI_SETTARGETSTART, targetStart, 0); SendMessage(hScinWnd, SCI_SETTARGETEND, (WPARAM)SendMessage(hScinWnd, SCI_GETLENGTH, 0, 0), 0); if ((int)SendMessage(hScinWnd, SCI_SEARCHINTARGET, (WPARAM)strlen(str), (LPARAM)str) == -1) { targetStart = 0; end = (LPARAM)SendMessage(hScinWnd, SCI_GETLENGTH, 0, 0); SendMessage(hScinWnd, SCI_SETTARGETSTART, targetStart, 0); SendMessage(hScinWnd, SCI_SETTARGETEND, end, 0); } else { start = (WPARAM)SendMessage(hScinWnd, SCI_GETTARGETSTART, 0, 0); end = (LPARAM)SendMessage(hScinWnd, SCI_GETTARGETEND, 0, 0); SendMessage(hScinWnd, SCI_SETSEL, start, end); targetStart = (WPARAM)end; } } break; case IDC_REPLACE: { char str[80]; char rep[80]; GetDlgItemText(hWnd, IDC_FIND_TEXT, str, 80); GetDlgItemText(hWnd, IDC_REPLACE_TEXT, rep, 80); HWND hScinWnd = _window->getScinWnd(); SendMessage(hScinWnd, SCI_SETTARGETSTART, targetStart, 0); SendMessage(hScinWnd, SCI_SETTARGETEND, (WPARAM)SendMessage(hScinWnd, SCI_GETLENGTH, 0, 0), 0); if ((int)SendMessage(hScinWnd, SCI_SEARCHINTARGET, (WPARAM)strlen(str), (LPARAM)str) == -1) { targetStart = 0; end = (LPARAM)SendMessage(hScinWnd, SCI_GETLENGTH, 0, 0); SendMessage(hScinWnd, SCI_SETTARGETSTART, targetStart, 0); SendMessage(hScinWnd, SCI_SETTARGETEND, end, 0); } else { start = SendMessage(hScinWnd, SCI_GETTARGETSTART, 0, 0); end = SendMessage(hScinWnd, SCI_GETTARGETEND, 0, 0); SendMessage(hScinWnd, SCI_SETSEL, start, end); targetStart = (WPARAM)end; SendMessage(hScinWnd, SCI_REPLACETARGET, (WPARAM)strlen(rep), (LPARAM)rep); } } break; case IDC_REPLACE_ALL: { char str[80]; char rep[80]; GetDlgItemText(hWnd, IDC_FIND_TEXT, str, 80); GetDlgItemText(hWnd, IDC_REPLACE_TEXT, rep, 80); HWND hScinWnd = _window->getScinWnd(); SendMessage(hScinWnd, SCI_SETTARGETSTART, 0, 0); SendMessage(hScinWnd, SCI_SETTARGETEND, (WPARAM)SendMessage(hScinWnd, SCI_GETLENGTH, 0, 0), 0); while (true) { if (SendMessage(hScinWnd, SCI_SEARCHINTARGET, (WPARAM)strlen(str), (LPARAM)str) == -1) break; SendMessage(hScinWnd, SCI_REPLACETARGET, (WPARAM)strlen(rep), (LPARAM)rep); SendMessage(hScinWnd, SCI_SETTARGETSTART, 0, 0); SendMessage(hScinWnd, SCI_SETTARGETEND, (WPARAM)SendMessage(hScinWnd, SCI_GETLENGTH, 0, 0), 0); } } break; case IDC_CLOSE: { EndDialog(hWnd, 0); } } } break; case WM_DESTROY: { EndDialog(hWnd, 0); } break; default: return FALSE; } return TRUE; } --- NEW FILE: npfile.cpp --- #include "npfile.h" CNPFile::CNPFile() { _filename = 0; } void CNPFile::loadFile(char *filename) { _filename = filename; FILE *fp = fopen(_filename, "rb"); char data[BLOCKSIZE]; int lenFile = (int)fread(data, 1, sizeof(data), fp); while (lenFile > 0) { SendMessage(_hScinWnd, SCI_ADDTEXT, lenFile, reinterpret_cast<LPARAM>(static_cast<char*>(data))); lenFile = (int)fread(data, 1, sizeof(data), fp); } fclose(fp); SendMessage(_hScinWnd, SCI_SETSAVEPOINT, 0, 0); } void CNPFile::writeFile() { FILE *fp = fopen(_filename, "wb"); if (fp) { char data[BLOCKSIZE + 1]; int lenDoc = (int)SendMessage(_hScinWnd, SCI_GETLENGTH, 0, 0); for (int i = 0; i < lenDoc; i += BLOCKSIZE) { int grabSize = lenDoc - i; if (grabSize > BLOCKSIZE) grabSize = BLOCKSIZE; TEXTRANGE tr; tr.chrg.cpMin = i; tr.chrg.cpMax = i + grabSize; tr.lpstrText = data; SendMessage(_hScinWnd, EM_GETTEXTRANGE, 0, reinterpret_cast<LPARAM>(&tr)); fwrite(data, grabSize, 1, fp); } fclose(fp); } } |