summaryrefslogtreecommitdiff
path: root/plugins/mTextControl/src/services.cpp
blob: 0afa1df715e291a60e870c5e410463149348221a (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
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"

static HANDLE hService[11] = {0,0,0,0,0,0,0,0,0,0,0};

INT_PTR MText_Register(WPARAM, LPARAM);
INT_PTR MText_Create(WPARAM, LPARAM);
INT_PTR MText_CreateW(WPARAM, LPARAM);
INT_PTR MText_CreateEx(WPARAM, LPARAM);
INT_PTR MText_Measure(WPARAM, LPARAM);
INT_PTR MText_Display(WPARAM, LPARAM);
INT_PTR MText_SetParent(WPARAM, LPARAM);
INT_PTR MText_SendMessage(WPARAM, LPARAM);
INT_PTR MText_CreateProxy(WPARAM, LPARAM);
INT_PTR MText_Destroy(WPARAM, LPARAM);
INT_PTR MText_GetInterface(WPARAM, LPARAM);

struct TextObject
{
	DWORD options;
	IFormattedTextDraw *ftd;
	TextObject(): options(0), ftd(0) {}
	~TextObject() { if (ftd) delete ftd; }
};

//---------------------------------------------------------------------------
// elper functions
void MText_InitFormatting0(IFormattedTextDraw *ftd, DWORD options) {
	LRESULT lResult;

	// urls
	ftd->getTextService()->TxSendMessage(EM_AUTOURLDETECT, TRUE, 0, &lResult);
}

void MText_InitFormatting1(TextObject *text) {
	// bbcodes
	bbCodeParse(text->ftd);

	// smilies
//	HWND hwnd = (HWND)CallServiceSync(MS_TEXT_CREATEPROXY, (WPARAM)text, 0);
	HWND hwnd = CreateProxyWindow(text->ftd->getTextService());
	SMADD_RICHEDIT3 sm = {0};
	sm.cbSize = sizeof(sm);
	sm.hwndRichEditControl = hwnd;
	sm.rangeToReplace = 0;
	sm.Protocolname = 0;
	sm.flags = SAFLRE_INSERTEMF;
	CallService(MS_SMILEYADD_REPLACESMILEYS, RGB(0xff, 0xff, 0xff), (LPARAM)&sm);
	DestroyWindow(hwnd);

//	text->ftd->getTextService()->TxSendMessage(EM_SETSEL, 0, -1, &lResult);
/*
	// rtl stuff
	PARAFORMAT2 pf2;
	pf2.cbSize = sizeof(pf2);
	pf2.dwMask = PFM_ALIGNMENT|PFM_RTLPARA;
	pf2.wEffects = PFE_RTLPARA;
	pf2.wAlignment = PFA_RIGHT;
	text->ftd->getTextService()->TxSendMessage(EM_SETSEL, 0, -1, &lResult);
	text->ftd->getTextService()->TxSendMessage(EM_SETPARAFORMAT, 0, (LPARAM)&pf2, &lResult);
	text->ftd->getTextService()->TxSendMessage(EM_SETSEL, 0, 0, &lResult);
*/
}

//---------------------------------------------------------------------------
// NEW mtextcontrol interface:
//
// obtain the full mtextcontrol interface from the library. it is much faster as use of 
// miranda core CallService to access to mtextcontrol (no core traffic).
// This interface provides full access to mtextcontrol internal functions,
// thus enabling devs to fully utilize the mtextcontrol API.
// All functions will be exported as miranda services for compatibility.
//
// the interface is populated during the Load(void) handler, so you can assume it is ready when Miranda
// throw the ME_SYSTEM_MODULESLOADED event and you can generate a warning in your ModulesLoaded() when
// it depends on the mtextcontrol interface and the mtextcontrol plugin is missing.
// 
// example:
// 
// MTEXT_INTERFACE MText = {0};
// 
// 	mir_getMTI(&MText);
// 
// all interface function designed as old mtextcontrol helper functions.
// therefore it is easy to convert your old plugin code to new interface.
//
// example:
//
// old code: MTextCreate (...
// new code: MText.Create(...

//---------------------------------------------------------------------------
// subscribe to MText services
INT_PTR MText_Register(WPARAM wParam, LPARAM lParam)
{
	DWORD userOptions = (DWORD)wParam;
	char *userTitle = (char *)lParam;
	return (INT_PTR)MTI_TextUserAdd(userTitle, userOptions);
}

//---------------------------------------------------------------------------
// allocate text object
HANDLE DLL_CALLCONV 
MTI_MTextCreate (HANDLE userHandle, char *text) {
	TextObject *result = new TextObject;
	result->options = TextUserGetOptions(userHandle);
	result->ftd = new CFormattedTextDraw;
	result->ftd->Create();
	InitRichEdit(result->ftd->getTextService());

	MText_InitFormatting0(result->ftd, result->options);
	result->ftd->putTextA(text);
	MText_InitFormatting1(result);

	return (HANDLE)result;
}

INT_PTR MText_Create(WPARAM wParam, LPARAM lParam) {
	//HANDLE userHandle = (HANDLE)wParam;
	//char *text = (char *)lParam;
	return (INT_PTR)MTI_MTextCreate ((HANDLE)wParam, (char *)lParam);
}

//---------------------------------------------------------------------------
// allocate text object (unicode)
HANDLE DLL_CALLCONV 
MTI_MTextCreateW (HANDLE userHandle, WCHAR *text) {

	TextObject *result = new TextObject;
	result->options = TextUserGetOptions(userHandle);
	result->ftd = new CFormattedTextDraw;
	result->ftd->Create();
	InitRichEdit(result->ftd->getTextService());

	MText_InitFormatting0(result->ftd, result->options);
	result->ftd->putTextW(text);
	MText_InitFormatting1(result);

	return (HANDLE)result;

}

INT_PTR MText_CreateW(WPARAM wParam, LPARAM lParam) {

	//HANDLE userHandle = (HANDLE)wParam;
	//WCHAR *wtext = (WCHAR *)lParam;
	return (INT_PTR)(HANDLE)MTI_MTextCreateW ((HANDLE)wParam, (WCHAR *)lParam);

}

//---------------------------------------------------------------------------
// allocate text object (advanced)
HANDLE DLL_CALLCONV 
MTI_MTextCreateEx (HANDLE userHandle, HCONTACT hContact, void *text, DWORD flags) {
	TextObject *result = new TextObject;
	result->options = TextUserGetOptions(userHandle);
	result->ftd = new CFormattedTextDraw;
	result->ftd->Create();
	InitRichEdit(result->ftd->getTextService());

	MText_InitFormatting0(result->ftd, result->options);
	if (flags & MTEXT_FLG_WCHAR) result->ftd->putTextW((WCHAR *)text);
	else result->ftd->putTextA((char *)text);
	MText_InitFormatting1(result);

	return 0;
}

INT_PTR MText_CreateEx(WPARAM wParam, LPARAM lParam) {
	HANDLE userHandle = (HANDLE)wParam;
	MTEXTCREATE *textCreate = (MTEXTCREATE *)lParam;
	MTI_MTextCreateEx (userHandle, textCreate->hContact, textCreate->text, textCreate->flags);
	return 0;
}

//---------------------------------------------------------------------------
// measure text object
int DLL_CALLCONV 
MTI_MTextMeasure (HDC dc, SIZE *sz, HANDLE text) {
	if (!text) return 0;

	long lWidth=sz->cx, lHeight=sz->cy;
	((TextObject *)text)->ftd->get_NaturalSize(dc, &lWidth, &lHeight);
	sz->cx = lWidth;
	sz->cy = lHeight;
//	FancyMeasure(((TextObject *)text)->fancy, displayInfo);

	return 0;
}

INT_PTR MText_Measure(WPARAM wParam, LPARAM lParam) {
	LPMTEXTDISPLAY displayInfo = (LPMTEXTDISPLAY)wParam;
	if (!displayInfo) return 0;
	if (!(TextObject *)displayInfo->text) return 0;
	MTI_MTextMeasure (displayInfo->dc, &displayInfo->sz, displayInfo->text);
	return 0;
}

int DLL_CALLCONV 
//---------------------------------------------------------------------------
// display text object
MTI_MTextDisplay (HDC dc, POINT pos, SIZE sz, HANDLE text) {
	if (!text) return 0;

	COLORREF cl = GetTextColor(dc);
//	if (GetTextColor(dc)&0xffffff != 0)
	{
		LRESULT lResult;
		CHARFORMAT cf = {0};
		cf.cbSize = sizeof(cf);
		cf.dwMask = CFM_COLOR;
		cf.crTextColor = cl;
		((TextObject *)text)->ftd->getTextService()->TxSendMessage(EM_SETCHARFORMAT, SCF_ALL, (LPARAM)&cf, &lResult);
	}

	SetBkMode(dc, TRANSPARENT);

	long lWidth=sz.cx, lHeight;
	((TextObject *)text)->ftd->get_NaturalSize(dc, &lWidth, &lHeight);
	RECT rt;
	rt.left = pos.x;
	rt.top = pos.y;
	rt.right = pos.x + lWidth;
	rt.bottom = pos.y + lHeight;
	((TextObject *)text)->ftd->Draw(dc, &rt);

	return 0;
}

INT_PTR MText_Display(WPARAM wParam, LPARAM lParam) {
	LPMTEXTDISPLAY displayInfo = (LPMTEXTDISPLAY)wParam;
	if (!displayInfo) return 0;
	if (!displayInfo->text) return 0;
	MTI_MTextDisplay (displayInfo->dc, displayInfo->pos, displayInfo->sz, displayInfo->text);
	return 0;
}

int DLL_CALLCONV 
//---------------------------------------------------------------------------
// set parent window for text object (this is required for mouse handling, etc)
MTI_MTextSetParent (HANDLE text, HWND hwnd, RECT rect) {
	if (!text) return 0;
	((TextObject *)text)->ftd->setParentWnd(hwnd, rect);
	return 0;
}

INT_PTR MText_SetParent(WPARAM wParam, LPARAM lParam) {
	LPMTEXTSETPARENT info = (LPMTEXTSETPARENT)wParam;
	//TextObject *text = (TextObject *)info->text;
	if (!info) return 0;
	if (!info->text) return 0;
	MTI_MTextSetParent (info->text, info->hwnd, info->rc);
	return 0;
}

//---------------------------------------------------------------------------
// send message to an object
int DLL_CALLCONV 
MTI_MTextSendMessage (HWND hwnd, HANDLE text, UINT msg, WPARAM wParam, LPARAM lParam) {
	LRESULT lResult;
	if (!text) return 0;
	((TextObject *)text)->ftd->getTextService()->TxSendMessage(msg, wParam, lParam, &lResult);

	if (hwnd && (msg == WM_MOUSEMOVE)) {
		HDC hdc = GetDC(hwnd);
		((TextObject *)text)->ftd->getTextService()->OnTxSetCursor(DVASPECT_CONTENT, 0, NULL, NULL, hdc, NULL, NULL, LOWORD(0), HIWORD(0));
		ReleaseDC(hwnd, hdc);
	}

	return lResult;
}

INT_PTR MText_SendMessage(WPARAM wParam, LPARAM lParam) {
	LPMTEXTMESSAGE message = (LPMTEXTMESSAGE)wParam;
	TextObject *text = (TextObject *)message->text;
	if (!message->text) return 0;
	return (INT_PTR)MTI_MTextSendMessage (message->hwnd, message->text, message->msg, message->wParam, message->lParam);
}

//---------------------------------------------------------------------------
// create a proxy window
HWND DLL_CALLCONV 
MTI_MTextCreateProxy (HANDLE text) {
	if (!text) return 0;
	return CreateProxyWindow(((TextObject *)text)->ftd->getTextService());
}

INT_PTR MText_CreateProxy(WPARAM wParam, LPARAM lParam) {
	if (!wParam) return 0;
	return (INT_PTR)MTI_MTextCreateProxy((HANDLE)wParam);
}

int DLL_CALLCONV 
//---------------------------------------------------------------------------
// destroy text object
MTI_MTextDestroy (HANDLE text) {
	//HANDLE textHandle = (HANDLE)wParam;
	//TextObject *text = (TextObject *)textHandle;
	if (text) delete (TextObject *)text;
	return 0;
}

INT_PTR MText_Destroy(WPARAM wParam, LPARAM lParam) {
	HANDLE textHandle = (HANDLE)wParam;
	TextObject *text = (TextObject *)textHandle;
	if (text) delete text;
	return 0;
}

//---------------------------------------------------------------------------
// populate the interface
INT_PTR MText_GetInterface(WPARAM wParam, LPARAM lParam) {
	MTEXT_INTERFACE *MText = (MTEXT_INTERFACE *)lParam;
	if ( MText == NULL )
		return CALLSERVICE_NOTFOUND;

	MText->version		= pluginInfoEx.version;
	MText->Register		= MTI_TextUserAdd;
	MText->Create		= MTI_MTextCreate;
	MText->CreateW		= MTI_MTextCreateW;
//	MText->CreateT		= defined inside api
	MText->CreateEx	= MTI_MTextCreateEx;
	MText->Measure		= MTI_MTextMeasure;
	MText->Display		= MTI_MTextDisplay;
	MText->SetParent	= MTI_MTextSetParent;
	MText->SendMsg		= MTI_MTextSendMessage;
	MText->CreateProxy	= MTI_MTextCreateProxy;
	MText->Destroy		= MTI_MTextDestroy;

	return S_OK;
}

//---------------------------------------------------------------------------
// Load / Unload services
void LoadServices()
{
	int nService = 0;
	hService[nService++] = CreateServiceFunction(MS_TEXT_REGISTER,		MText_Register);
	hService[nService++] = CreateServiceFunction(MS_TEXT_CREATE,		MText_Create);
#ifdef UNICODE
	hService[nService++] = CreateServiceFunction(MS_TEXT_CREATEW,		MText_CreateW);
#endif
	hService[nService++] = CreateServiceFunction(MS_TEXT_CREATEEX,		MText_CreateEx);
	hService[nService++] = CreateServiceFunction(MS_TEXT_MEASURE,		MText_Measure);
	hService[nService++] = CreateServiceFunction(MS_TEXT_DISPLAY,		MText_Display);
	hService[nService++] = CreateServiceFunction(MS_TEXT_SETPARENT,		MText_SetParent);
	hService[nService++] = CreateServiceFunction(MS_TEXT_SENDMESSAGE,	MText_SendMessage);
	hService[nService++] = CreateServiceFunction(MS_TEXT_CREATEPROXY,	MText_CreateProxy);
	hService[nService++] = CreateServiceFunction(MS_TEXT_DESTROY,		MText_Destroy);
	hService[nService++] = CreateServiceFunction(MS_TEXT_GETINTERFACE,	MText_GetInterface);
}

void UnloadServices()
{
	int nService = sizeof(hService)/sizeof(*hService);
	while (nService--)
		if (hService[nService])
			DestroyServiceFunction(hService[nService]);
}