📄 editor.c
字号:
/************************************************************************ * IRC - Internet Relay Chat, win32/editor.c * Copyright (C) 2004 Dominick Meglio (codemastr) * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 1, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */#include <windows.h>#include <windowsx.h>#include <commctrl.h>#include <richedit.h>#include <commdlg.h>#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <io.h>#include <fcntl.h>#include "resource.h"#include "setup.h"#include "win32.h"#include "sys.h"LRESULT CALLBACK GotoDLG(HWND, UINT, WPARAM, LPARAM);LRESULT CALLBACK ColorDLG(HWND, UINT, WPARAM, LPARAM);HWND hFind;/* Draws the statusbar for the editor * Parameters: * hInstance - The instance to create the statusbar in * hwndParent - The parent of the statusbar * iId - The message value used to send messages to the parent * Returns: * The handle to the statusbar */HWND DrawStatusbar(HINSTANCE hInstance, HWND hwndParent, UINT iId){ HWND hStatus, hTip; TOOLINFO ti; RECT clrect; hStatus = CreateStatusWindow(WS_CHILD|WS_VISIBLE|SBT_TOOLTIPS, NULL, hwndParent, iId); hTip = CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL, WS_POPUP|TTS_NOPREFIX|TTS_ALWAYSTIP, 0, 0, 0, 0, hwndParent, NULL, hInstance, NULL); GetClientRect(hStatus, &clrect); ti.cbSize = sizeof(TOOLINFO); ti.uFlags = TTF_SUBCLASS; ti.hwnd = hStatus; ti.uId = 1; ti.hinst = hInstance; ti.rect = clrect; ti.lpszText = "Go To"; SendMessage(hTip, TTM_ADDTOOL, 0, (LPARAM)&ti); return hStatus;}/* Draws the toolbar for the editor * Parameters: * hInstance - The instance to create the toolbar in * hwndParent - The parent of the toolbar * Returns: * The handle to the toolbar */HWND DrawToolbar(HINSTANCE hInstance, HWND hwndParent) { HWND hTool; TBADDBITMAP tbBit; int newidx; TBBUTTON tbButtons[10] = { { STD_FILENEW, IDM_NEW, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0}, { STD_FILESAVE, IDM_SAVE, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0}, { 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, {0}, 0L, 0}, { STD_CUT, IDM_CUT, 0, TBSTYLE_BUTTON, {0}, 0L, 0}, { STD_COPY, IDM_COPY, 0, TBSTYLE_BUTTON, {0}, 0L, 0}, { STD_PASTE, IDM_PASTE, 0, TBSTYLE_BUTTON, {0}, 0L, 0}, { 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, {0}, 0L, 0}, { STD_UNDO, IDM_UNDO, 0, TBSTYLE_BUTTON, {0}, 0L, 0}, { STD_REDOW, IDM_REDO, 0, TBSTYLE_BUTTON, {0}, 0L, 0}, { 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, {0}, 0L, 0} }; TBBUTTON tbAddButtons[7] = { { 0, IDC_BOLD, TBSTATE_ENABLED, TBSTYLE_CHECK, {0}, 0L, 0}, { 1, IDC_UNDERLINE, TBSTATE_ENABLED, TBSTYLE_CHECK, {0}, 0L, 0}, { 2, IDC_COLOR, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0}, { 3, IDC_BGCOLOR, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0}, { 0, 0, TBSTATE_ENABLED, TBSTYLE_SEP, {0}, 0L, 0}, { 4, IDC_GOTO, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0}, { STD_FIND, IDC_FIND, TBSTATE_ENABLED, TBSTYLE_BUTTON, {0}, 0L, 0} }; hTool = CreateToolbarEx(hwndParent, WS_VISIBLE|WS_CHILD|TBSTYLE_FLAT|TBSTYLE_TOOLTIPS, IDC_TOOLBAR, 0, HINST_COMMCTRL, IDB_STD_SMALL_COLOR, tbButtons, 10, 0, 0, 100, 30, sizeof(TBBUTTON)); tbBit.hInst = hInstance; tbBit.nID = IDB_BITMAP1; newidx = SendMessage(hTool, TB_ADDBITMAP, (WPARAM)5, (LPARAM)&tbBit); tbAddButtons[0].iBitmap += newidx; tbAddButtons[1].iBitmap += newidx; tbAddButtons[2].iBitmap += newidx; tbAddButtons[3].iBitmap += newidx; tbAddButtons[5].iBitmap += newidx; SendMessage(hTool, TB_ADDBUTTONS, (WPARAM)7, (LPARAM)&tbAddButtons); return hTool;}/* Dialog procedure for the color selection dialog * Parameters: * hDlg - The dialog handle * message - The message received * wParam - The first message parameter * lParam - The second message parameter * Returns: * TRUE if the message was processed, FALSE otherwise */LRESULT CALLBACK ColorDLG(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { static HBRUSH hBrushWhite, hBrushBlack, hBrushDarkBlue, hBrushDarkGreen, hBrushRed, hBrushDarkRed, hBrushPurple, hBrushOrange, hBrushYellow, hBrushGreen, hBrushVDarkGreen, hBrushLightBlue, hBrushBlue, hBrushPink, hBrushDarkGray, hBrushGray; static UINT ResultMsg = 0; switch (message) { case WM_INITDIALOG: hBrushWhite = CreateSolidBrush(RGB(255,255,255)); hBrushBlack = CreateSolidBrush(RGB(0,0,0)); hBrushDarkBlue = CreateSolidBrush(RGB(0,0,127)); hBrushDarkGreen = CreateSolidBrush(RGB(0,147,0)); hBrushRed = CreateSolidBrush(RGB(255,0,0)); hBrushDarkRed = CreateSolidBrush(RGB(127,0,0)); hBrushPurple = CreateSolidBrush(RGB(156,0,156)); hBrushOrange = CreateSolidBrush(RGB(252,127,0)); hBrushYellow = CreateSolidBrush(RGB(255,255,0)); hBrushGreen = CreateSolidBrush(RGB(0,252,0)); hBrushVDarkGreen = CreateSolidBrush(RGB(0,147,147)); hBrushLightBlue = CreateSolidBrush(RGB(0,255,255)); hBrushBlue = CreateSolidBrush(RGB(0,0,252)); hBrushPink = CreateSolidBrush(RGB(255,0,255)); hBrushDarkGray = CreateSolidBrush(RGB(127,127,127)); hBrushGray = CreateSolidBrush(RGB(210,210,210)); ResultMsg = (UINT)lParam; SetFocus(NULL); return TRUE; case WM_DRAWITEM: { LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)lParam; if (wParam == IDC_WHITE) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushWhite); if (wParam == IDC_BLACK) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushBlack); if (wParam == IDC_DARKBLUE) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushDarkBlue); if (wParam == IDC_DARKGREEN) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushDarkGreen); if (wParam == IDC_RED) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushRed); if (wParam == IDC_DARKRED) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushDarkRed); if (wParam == IDC_PURPLE) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushPurple); if (wParam == IDC_ORANGE) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushOrange); if (wParam == IDC_YELLOW) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushYellow); if (wParam == IDC_GREEN) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushGreen); if (wParam == IDC_VDARKGREEN) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushVDarkGreen); if (wParam == IDC_LIGHTBLUE) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushLightBlue); if (wParam == IDC_BLUE) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushBlue); if (wParam == IDC_PINK) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushPink); if (wParam == IDC_DARKGRAY) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushDarkGray); if (wParam == IDC_GRAY) FillRect(lpdis->hDC, &lpdis->rcItem, hBrushGray); DrawEdge(lpdis->hDC, &lpdis->rcItem, EDGE_SUNKEN, BF_RECT); return TRUE; } case WM_COMMAND: { COLORREF clrref; if (LOWORD(wParam) == IDC_WHITE) clrref = RGB(255,255,255); else if (LOWORD(wParam) == IDC_BLACK) clrref = RGB(0,0,0); else if (LOWORD(wParam) == IDC_DARKBLUE) clrref = RGB(0,0,127); else if (LOWORD(wParam) == IDC_DARKGREEN) clrref = RGB(0,147,0); else if (LOWORD(wParam) == IDC_RED) clrref = RGB(255,0,0); else if (LOWORD(wParam) == IDC_DARKRED) clrref = RGB(127,0,0); else if (LOWORD(wParam) == IDC_PURPLE) clrref = RGB(156,0,156); else if (LOWORD(wParam) == IDC_ORANGE) clrref = RGB(252,127,0); else if (LOWORD(wParam) == IDC_YELLOW) clrref = RGB(255,255,0); else if (LOWORD(wParam) == IDC_GREEN) clrref = RGB(0,252,0); else if (LOWORD(wParam) == IDC_VDARKGREEN) clrref = RGB(0,147,147); else if (LOWORD(wParam) == IDC_LIGHTBLUE) clrref = RGB(0,255,255); else if (LOWORD(wParam) == IDC_BLUE) clrref = RGB(0,0,252); else if (LOWORD(wParam) == IDC_PINK) clrref = RGB(255,0,255); else if (LOWORD(wParam) == IDC_DARKGRAY) clrref = RGB(127,127,127); else if (LOWORD(wParam) == IDC_GRAY) clrref = RGB(210,210,210); SendMessage(GetParent(hDlg), ResultMsg, (WPARAM)clrref, (LPARAM)hDlg); break; } case WM_CLOSE: EndDialog(hDlg, TRUE); case WM_DESTROY: DeleteObject(hBrushWhite); DeleteObject(hBrushBlack); DeleteObject(hBrushDarkBlue); DeleteObject(hBrushDarkGreen); DeleteObject(hBrushRed); DeleteObject(hBrushDarkRed); DeleteObject(hBrushPurple); DeleteObject(hBrushOrange); DeleteObject(hBrushYellow); DeleteObject(hBrushGreen); DeleteObject(hBrushVDarkGreen); DeleteObject(hBrushLightBlue); DeleteObject(hBrushBlue); DeleteObject(hBrushPink); DeleteObject(hBrushDarkGray); DeleteObject(hBrushGray); break; } return FALSE;}/* Dialog procedure for the goto dialog * Parameters: * hDlg - The dialog handle * message - The message received * wParam - The first message parameter * lParam - The second message parameter * Returns: * TRUE if the message was processed, FALSE otherwise */LRESULT CALLBACK GotoDLG(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { if (message == WM_COMMAND) { if (LOWORD(wParam) == IDCANCEL) EndDialog(hDlg, TRUE); else if (LOWORD(wParam) == IDOK) { HWND hWnd = GetDlgItem(GetParent(hDlg),IDC_TEXT); int line = GetDlgItemInt(hDlg, IDC_GOTO, NULL, FALSE); int pos = SendMessage(hWnd, EM_LINEINDEX, (WPARAM)--line, 0); SendMessage(hWnd, EM_SETSEL, (WPARAM)pos, (LPARAM)pos); SendMessage(hWnd, EM_SCROLLCARET, 0, 0); EndDialog(hDlg, TRUE); } } return FALSE;}LRESULT CALLBACK FromFileDLG(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { HWND hWnd; static FINDREPLACE find; static char findbuf[256]; static unsigned char *file; static HWND hTool, hClip, hStatus; static RECT rOld; CHARFORMAT2 chars; if (message == WM_FINDMSGSTRING) { FINDREPLACE *fr = (FINDREPLACE *)lParam; if (fr->Flags & FR_FINDNEXT) { HWND hRich = GetDlgItem(hDlg, IDC_TEXT); DWORD flags=0; FINDTEXTEX ft; CHARRANGE chrg; if (fr->Flags & FR_DOWN) flags |= FR_DOWN; if (fr->Flags & FR_MATCHCASE) flags |= FR_MATCHCASE; if (fr->Flags & FR_WHOLEWORD) flags |= FR_WHOLEWORD; ft.lpstrText = fr->lpstrFindWhat; SendMessage(hRich, EM_EXGETSEL, 0, (LPARAM)&chrg); if (flags & FR_DOWN) { ft.chrg.cpMin = chrg.cpMax; ft.chrg.cpMax = -1; } else { ft.chrg.cpMin = chrg.cpMin; ft.chrg.cpMax = -1; } if (SendMessage(hRich, EM_FINDTEXTEX, flags, (LPARAM)&ft) == -1) MessageBox(NULL, "Unreal has finished searching the document", "Find", MB_ICONINFORMATION|MB_OK); else { SendMessage(hRich, EM_EXSETSEL, 0, (LPARAM)&(ft.chrgText)); SendMessage(hRich, EM_SCROLLCARET, 0, 0); SetFocus(hRich); } } return TRUE; } switch (message) { case WM_INITDIALOG: { int fd,len; unsigned char *buffer = '\0', *string = '\0'; EDITSTREAM edit; StreamIO *stream = malloc(sizeof(StreamIO)); unsigned char szText[256]; struct stat sb; HWND hWnd = GetDlgItem(hDlg, IDC_TEXT), hTip; file = (unsigned char *)lParam; if (file) wsprintf(szText, "UnrealIRCd Editor - %s", file); else strcpy(szText, "UnrealIRCd Editor - New File"); SetWindowText(hDlg, szText); lpfnOldWndProc = (FARPROC)SetWindowLong(hWnd, GWL_WNDPROC, (DWORD)RESubClassFunc); hTool = DrawToolbar(hInst, hDlg); hStatus = DrawStatusbar(hInst, hDlg, IDC_STATUS); SendMessage(hWnd, EM_SETEVENTMASK, 0, (LPARAM)ENM_SELCHANGE); chars.cbSize = sizeof(CHARFORMAT2); chars.dwMask = CFM_FACE; strcpy(chars.szFaceName,"Fixedsys"); SendMessage(hWnd, EM_SETCHARFORMAT, (WPARAM)SCF_ALL, (LPARAM)&chars); if ((fd = open(file, _O_RDONLY|_O_BINARY)) != -1) { fstat(fd,&sb); /* Only allocate the amount we need */ buffer = malloc(sb.st_size+1); buffer[0] = 0; len = read(fd, buffer, sb.st_size); buffer[len] = 0; len = CountRTFSize(buffer)+1; string = malloc(len); bzero(string,len); IRCToRTF(buffer,string); RTFBuf = string; len--; stream->size = &len; stream->buffer = &RTFBuf; edit.dwCookie = (UINT)stream; edit.pfnCallback = SplitIt; SendMessage(hWnd, EM_EXLIMITTEXT, 0, (LPARAM)0x7FFFFFFF); SendMessage(hWnd, EM_STREAMIN, (WPARAM)SF_RTF|SFF_PLAINRTF, (LPARAM)&edit); SendMessage(hWnd, EM_SETMODIFY, (WPARAM)FALSE, 0); SendMessage(hWnd, EM_EMPTYUNDOBUFFER, 0, 0); close(fd); RTFBuf = NULL; free(buffer); free(string); free(stream); hClip = SetClipboardViewer(hDlg); if (SendMessage(hWnd, EM_CANPASTE, 0, 0)) SendMessage(hTool, TB_ENABLEBUTTON, (WPARAM)IDM_PASTE, (LPARAM)MAKELONG(TRUE,0)); else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -