summaryrefslogtreecommitdiff
path: root/plugins/mTextControl/src/textcontrol.cpp
blob: 4dad57ec55855f00fdfbcb75309ee8a1b182e5f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
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 "stdafx.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;
	wchar_t *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 = MODULNAMEW;
	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 wchar_t[textLength + 1];
			GetWindowText(hwnd, data->text, textLength + 1);
			data->mtext = MTI_MTextCreateW(data->htu, data->text);

			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:
		RECT rc;
		GetClientRect(hwnd, &rc);
		FillRect((HDC)wParam, &rc, GetSysColorBrush(COLOR_BTNFACE));
		return TRUE;

	case WM_MOUSEMOVE:
		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, LPARAM)
{
	PAINTSTRUCT ps;
	HDC 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) {
		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);
	}

	// Release the device context
	EndPaint(hwnd, &ps);

	return 0;
}