From 4283465f23806ae4b8ad7344949f55698c16a203 Mon Sep 17 00:00:00 2001 From: watcherhd Date: Thu, 21 Apr 2011 20:19:47 +0000 Subject: MTextControl - text render API for different plugins git-svn-id: http://miranda-plugins.googlecode.com/svn/trunk@12 e753b5eb-9565-29b2-b5c5-2cc6f99dfbcb --- mTextControl/src/textcontrol.cpp | 200 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 mTextControl/src/textcontrol.cpp (limited to 'mTextControl/src/textcontrol.cpp') diff --git a/mTextControl/src/textcontrol.cpp b/mTextControl/src/textcontrol.cpp new file mode 100644 index 0000000..29caf2e --- /dev/null +++ b/mTextControl/src/textcontrol.cpp @@ -0,0 +1,200 @@ +/* +Miranda Text Control - Plugin for Miranda IM +Copyright (C) 2005 Victor Pavlychko (nullbie@gmail.com) + +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 2 of the License, 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ + +#include "headers.h" + +LRESULT CALLBACK MTextControlWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); +LRESULT MTextControl_OnPaint(HWND hwnd, WPARAM wParam, LPARAM lParam); +//LRESULT MTextControl_Measure(HWND hwnd, int maxw, SIZE *size); + +struct TextControlData +{ + HANDLE htu; + TCHAR *text; + HANDLE mtext; +}; + +void MTextControl_RegisterClass() +{ + WNDCLASSEX wcl; + wcl.cbSize = sizeof(wcl); + wcl.lpfnWndProc = MTextControlWndProc; + wcl.style = CS_GLOBALCLASS; + wcl.cbClsExtra = 0; + wcl.cbWndExtra = 0; + wcl.hInstance = hInst; + wcl.hIcon = NULL; + wcl.hCursor = LoadCursor(NULL, IDC_ARROW); + wcl.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); + wcl.lpszMenuName = NULL; + wcl.lpszClassName = _T(MODULNAME); + wcl.hIconSm = 0; + RegisterClassEx(&wcl); +} + +LRESULT CALLBACK MTextControlWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) +{ + TextControlData *data = (TextControlData *)GetWindowLongPtr(hwnd, GWLP_USERDATA); + switch(msg) + { + case WM_CREATE: + { + data = new TextControlData; + data->text = 0; + data->mtext = 0; + data->htu = htuDefault; + SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)data); + PostMessage(hwnd, MTM_UPDATE, 0, 0); + return 0; + } + + case MTM_SETUSER: + { + data->htu = wParam ? (HANDLE)wParam : htuDefault; + // falldown, DefWindowProc won't process WM_USER ;) + } + + case WM_SETTEXT: + { + DefWindowProc(hwnd, msg, wParam, lParam); + // falldown + } + + case MTM_UPDATE: + { + if (data->text) delete [] data->text; + if (data->mtext) MTI_MTextDestroy(data->mtext); + + int textLength = GetWindowTextLength(hwnd); + data->text = new TCHAR[textLength+1]; + GetWindowText(hwnd, data->text, textLength+1); + #if defined(UNICODE) || defined (_UNICODE) + data->mtext = MTI_MTextCreateW(data->htu, data->text); + #else + data->mtext = MTI_MTextCreate(data->htu, data->text); + #endif + + RECT rc; GetClientRect(hwnd, &rc); + MTI_MTextSetParent(data->mtext, hwnd, rc); + + InvalidateRect(hwnd, 0, TRUE); + + return TRUE; + } + + case WM_PAINT: + { + return MTextControl_OnPaint(hwnd, wParam, lParam); + } + + case WM_ERASEBKGND: + { + HDC hdc = (HDC)wParam; + RECT rc; + GetClientRect(hwnd, &rc); + FillRect(hdc, &rc, GetSysColorBrush(COLOR_BTNFACE)); + return TRUE; + } + +// case WM_NCHITTEST: +// case WM_NCMOUSEMOVE: + case WM_MOUSEMOVE: +// case WM_LBUTTONDOWN: +// case WM_LBUTTONUP: +// case WM_RBUTTONDOWN: +// case WM_RBUTTONUP: + { + if (data && data->mtext) + return MTI_MTextSendMessage(hwnd, data->mtext, msg, wParam, lParam); + break; + } + + } + + return DefWindowProc(hwnd, msg, wParam, lParam); +} + +/// Paint //////////////////////////////////// +LRESULT MTextControl_OnPaint(HWND hwnd, WPARAM wParam, LPARAM lParam) +{ + HDC hdc; + PAINTSTRUCT ps; + hdc = BeginPaint(hwnd, &ps); + + RECT rc; + GetClientRect(hwnd, &rc); + FrameRect(hdc, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH)); + + SetTextColor(hdc, RGB(0,0,0)); + SetBkMode(hdc, TRANSPARENT); + + // Find the text to draw + TextControlData *data = (TextControlData *)GetWindowLongPtr(hwnd, GWLP_USERDATA); + if (data->mtext) + { +/* + // Font-related stuff + LOGFONT lfText; + lfText.lfHeight = -11; //"8" in the font dialog + lfText.lfWidth = lfText.lfEscapement = lfText.lfOrientation = 0; + lfText.lfItalic = lfText.lfUnderline = lfText.lfStrikeOut = FALSE; + lfText.lfCharSet = DEFAULT_CHARSET; + lfText.lfOutPrecision = OUT_DEFAULT_PRECIS; + lfText.lfClipPrecision = CLIP_DEFAULT_PRECIS; + lfText.lfQuality = DEFAULT_QUALITY; + lfText.lfPitchAndFamily = DEFAULT_PITCH | FF_SWISS; + lstrcpy(lfText.lfFaceName,_T("Tahoma")); + lfText.lfWeight = FW_REGULAR; + HFONT hfntSave = (HFONT)SelectObject(hdc, CreateFontIndirect(&lfText)); +*/ + + HFONT hfntSave = 0; + HFONT hfnt = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0); + if (!hfnt) + hfnt = (HFONT)SendMessage(GetParent(hwnd), WM_GETFONT, 0, 0); + if (hfnt) + { + LOGFONT lf; + GetObject(hfnt, sizeof(lf), &lf); + hfntSave = (HFONT)SelectObject(hdc, hfnt); + } + + // Draw the text + RECT rc; + GetClientRect(hwnd, &rc); + POINT pos; + pos.x = 0; + pos.y = 2; + SIZE sz; + sz.cx = rc.right - rc.left; + sz.cy = rc.bottom - rc.top - 4; + MTI_MTextDisplay(hdc, pos, sz, data->mtext); + + if (hfntSave) + SelectObject(hdc, hfntSave); + +// DeleteObject(SelectObject(hdc, hfntSave)); + + } + + // Release the device context + EndPaint(hwnd, &ps); + + return 0; +} -- cgit v1.2.3