summaryrefslogtreecommitdiff
path: root/plugins/Scriver/src/chat/log.cpp
blob: 613669777d3de6572b6c8bea7dd76423b18a19d3 (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
/*
Chat module plugin for Miranda IM

Copyright (C) 2003 Jörgen Persson
Copyright 2003-2009 Miranda ICQ/IM project,

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 "../commonheaders.h"

#ifndef EM_GETSCROLLPOS
#define EM_GETSCROLLPOS	(WM_USER+221)
#endif

static DWORD CALLBACK Log_StreamCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG * pcb)
{
	LOGSTREAMDATA *lstrdat = (LOGSTREAMDATA*)dwCookie;
	if (lstrdat) {
		// create the RTF
		if (lstrdat->buffer == NULL) {
			lstrdat->bufferOffset = 0;
			lstrdat->buffer = pci->Log_CreateRTF(lstrdat);
			lstrdat->bufferLen = lstrlenA(lstrdat->buffer);
		}

		// give the RTF to the RE control
		*pcb = min(cb, lstrdat->bufferLen - lstrdat->bufferOffset);
		CopyMemory(pbBuff, lstrdat->buffer + lstrdat->bufferOffset, *pcb);
		lstrdat->bufferOffset += *pcb;

		// free stuff if the streaming operation is complete
		if (lstrdat->bufferOffset == lstrdat->bufferLen) {
			mir_free(lstrdat->buffer);
			lstrdat->buffer = NULL;
		}
	}

	return 0;
}

void Log_StreamInEvent(HWND hwndDlg, LOGINFO* lin, SESSION_INFO *si, BOOL bRedraw)
{
	if (hwndDlg == 0 || lin == 0 || si == 0)
		return;

	HWND hwndRich = GetDlgItem(hwndDlg, IDC_CHAT_LOG);

	LOGSTREAMDATA streamData;
	ZeroMemory(&streamData, sizeof(streamData));
	streamData.hwnd = hwndRich;
	streamData.si = si;
	streamData.lin = lin;
	streamData.bStripFormat = FALSE;
	streamData.isFirst = bRedraw ? 1 : (GetRichTextLength(hwndRich, CP_ACP, FALSE) == 0);

	if (!bRedraw && si->iType == GCW_CHATROOM && si->bFilterEnabled && !(si->iLogFilterFlags & lin->iType))
		return;

	BOOL bFlag = FALSE;

	EDITSTREAM stream = { 0 };
	stream.pfnCallback = Log_StreamCallback;
	stream.dwCookie = (DWORD_PTR)& streamData;

	SCROLLINFO scroll;
	scroll.cbSize = sizeof(SCROLLINFO);
	scroll.fMask = SIF_RANGE | SIF_POS | SIF_PAGE;
	GetScrollInfo(hwndRich, SB_VERT, &scroll);

	POINT point = { 0 };
	SendMessage(hwndRich, EM_GETSCROLLPOS, 0, (LPARAM)&point);

	// do not scroll to bottom if there is a selection
	CHARRANGE oldsel, sel, newsel;
	SendMessage(hwndRich, EM_EXGETSEL, 0, (LPARAM)&oldsel);
	if (oldsel.cpMax != oldsel.cpMin)
		SendMessage(hwndRich, WM_SETREDRAW, FALSE, 0);

	// set the insertion point at the bottom
	sel.cpMin = sel.cpMax = GetRichTextLength(hwndRich, CP_ACP, FALSE);
	SendMessage(hwndRich, EM_EXSETSEL, 0, (LPARAM)&sel);
	SendMessage(hwndRich, EM_EXGETSEL, 0, (LPARAM)&sel);

	// fix for the indent... must be a M$ bug
	if (sel.cpMax == 0)
		bRedraw = TRUE;

	// should the event(s) be appended to the current log
	WPARAM wp = bRedraw ? SF_RTF : SFF_SELECTION | SF_RTF;

	// get the number of pixels per logical inch
	if (bRedraw) {
		SendMessage(hwndRich, WM_SETREDRAW, FALSE, 0);
		bFlag = TRUE;
	}

	// stream in the event(s)
	streamData.lin = lin;
	streamData.bRedraw = bRedraw;
	SendMessage(hwndRich, EM_STREAMIN, wp, (LPARAM)&stream);

	// do smileys
	if (g_dat.smileyAddInstalled && (bRedraw || (lin->ptszText && lin->iType != GC_EVENT_JOIN 
		&& lin->iType != GC_EVENT_NICK && lin->iType != GC_EVENT_ADDSTATUS && lin->iType != GC_EVENT_REMOVESTATUS)))
	{
		newsel.cpMax = -1;
		newsel.cpMin = sel.cpMin;
		if (newsel.cpMin < 0)
			newsel.cpMin = 0;

		SMADD_RICHEDIT3 sm = { sizeof(sm) };
		sm.hwndRichEditControl = hwndRich;
		sm.Protocolname = si->pszModule;
		sm.rangeToReplace = bRedraw ? NULL : &newsel;
		sm.flags = 0;
		sm.disableRedraw = TRUE;
		sm.hContact = si->windowData.hContact;
		CallService(MS_SMILEYADD_REPLACESMILEYS, 0, (LPARAM)&sm);
	}

	// scroll log to bottom if the log was previously scrolled to bottom, else restore old position
	if (bRedraw || (UINT)scroll.nPos >= (UINT)scroll.nMax - scroll.nPage - 5 || scroll.nMax - scroll.nMin - scroll.nPage < 50)
		SendMessage(GetParent(hwndRich), GC_SCROLLTOBOTTOM, 0, 0);
	else
		SendMessage(hwndRich, EM_SETSCROLLPOS, 0, (LPARAM)&point);

	// do we need to restore the selection
	if (oldsel.cpMax != oldsel.cpMin) {
		SendMessage(hwndRich, EM_EXSETSEL, 0, (LPARAM)& oldsel);
		SendMessage(hwndRich, WM_SETREDRAW, TRUE, 0);
		InvalidateRect(hwndRich, NULL, TRUE);
	}

	// need to invalidate the window
	if (bFlag) {
		sel.cpMin = sel.cpMax = GetRichTextLength(hwndRich, CP_ACP, FALSE);
		SendMessage(hwndRich, EM_EXSETSEL, 0, (LPARAM)& sel);
		SendMessage(hwndRich, WM_SETREDRAW, TRUE, 0);
		InvalidateRect(hwndRich, NULL, TRUE);
	}
}

char* Log_CreateRtfHeader(MODULEINFO *mi, SESSION_INFO *si)
{
	int bufferAlloced, bufferEnd, i = 0;
	int charset = 0;
	BOOL forceCharset = FALSE;

	// guesstimate amount of memory for the RTF header
	bufferEnd = 0;
	bufferAlloced = 4096;
	char *buffer = (char*)mir_realloc(si->pszHeader, bufferAlloced);
	buffer[0] = '\0';

	// ### RTF HEADER

	// font table
	AppendToBuffer(&buffer, &bufferEnd, &bufferAlloced, "{\\rtf1\\ansi\\deff0{\\fonttbl");
	for (i = 0; i < OPTIONS_FONTCOUNT; i++)
		AppendToBuffer(&buffer, &bufferEnd, &bufferAlloced, "{\\f%u\\fnil\\fcharset%u%S;}", i, (!forceCharset) ? pci->aFonts[i].lf.lfCharSet : charset, pci->aFonts[i].lf.lfFaceName);

	// colour table
	AppendToBuffer(&buffer, &bufferEnd, &bufferAlloced, "}{\\colortbl ;");

	for (i = 0; i < OPTIONS_FONTCOUNT; i++)
		AppendToBuffer(&buffer, &bufferEnd, &bufferAlloced, "\\red%u\\green%u\\blue%u;", GetRValue(pci->aFonts[i].color), GetGValue(pci->aFonts[i].color), GetBValue(pci->aFonts[i].color));

	for (i = 0; i < mi->nColorCount; i++)
		AppendToBuffer(&buffer, &bufferEnd, &bufferAlloced, "\\red%u\\green%u\\blue%u;", GetRValue(mi->crColors[i]), GetGValue(mi->crColors[i]), GetBValue(mi->crColors[i]));

	// new paragraph
	AppendToBuffer(&buffer, &bufferEnd, &bufferAlloced, "}\\pard");

	// set tabs and indents
	int iIndent = 0;

	if (g_Settings.dwIconFlags) {
		iIndent += (14 * 1440) / g_dat.logPixelSX;
		AppendToBuffer(&buffer, &bufferEnd, &bufferAlloced, "\\tx%u", iIndent);
	}
	if (g_Settings.ShowTime) {
		int iSize = (g_Settings.LogTextIndent * 1440) / g_dat.logPixelSX;
		AppendToBuffer(&buffer, &bufferEnd, &bufferAlloced, "\\tx%u", iIndent + iSize);
		if (g_Settings.LogIndentEnabled)
			iIndent += iSize;
	}
	AppendToBuffer(&buffer, &bufferEnd, &bufferAlloced, "\\fi-%u\\li%u", iIndent, iIndent);

	return buffer;
}