summaryrefslogtreecommitdiff
path: root/plugins/Scriver/src/infobar.cpp
blob: a7ce5bfa30e9aa23fcec2abce6758d565bda8c89 (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
/*
Scriver

Copyright (c) 2000-12 Miranda ICQ/IM project,

all portions of this codebase are copyrighted to the people
listed in contributors.txt.

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"

void SetupInfobar(InfobarWindowData* idat) {
	HWND hwnd = idat->hWnd;
    CHARFORMAT2 cf2;
	memset(&cf2, 0, sizeof(cf2));
    LOGFONT lf;
    DWORD colour = db_get_dw(NULL, SRMMMOD, SRMSGSET_INFOBARBKGCOLOUR, SRMSGDEFSET_INFOBARBKGCOLOUR);
    SendDlgItemMessage(hwnd, IDC_INFOBAR_NAME, EM_SETBKGNDCOLOR, 0, colour);
    SendDlgItemMessage(hwnd, IDC_INFOBAR_STATUS, EM_SETBKGNDCOLOR, 0, colour);
    LoadMsgDlgFont(MSGFONTID_INFOBAR_NAME, &lf, &colour, FALSE);
    cf2.dwMask = CFM_COLOR | CFM_FACE | CFM_CHARSET | CFM_SIZE | CFM_WEIGHT | CFM_BOLD | CFM_ITALIC;
    cf2.cbSize = sizeof(cf2);
    cf2.crTextColor = colour;
    cf2.bCharSet = lf.lfCharSet;
    _tcsncpy(cf2.szFaceName, lf.lfFaceName, LF_FACESIZE);
    cf2.dwEffects = ((lf.lfWeight >= FW_BOLD) ? CFE_BOLD : 0) | (lf.lfItalic ? CFE_ITALIC : 0);
    cf2.wWeight = (WORD)lf.lfWeight;
    cf2.bPitchAndFamily = lf.lfPitchAndFamily;
	cf2.yHeight = abs(lf.lfHeight) * 1440 / g_dat.logPixelSY;
    SendDlgItemMessageA(hwnd, IDC_INFOBAR_NAME, EM_SETCHARFORMAT, 0, (LPARAM)&cf2);

    LoadMsgDlgFont(MSGFONTID_INFOBAR_STATUS, &lf, &colour, FALSE);
    cf2.dwMask = CFM_COLOR | CFM_FACE | CFM_CHARSET | CFM_SIZE | CFM_WEIGHT | CFM_BOLD | CFM_ITALIC;
    cf2.cbSize = sizeof(cf2);
    cf2.crTextColor = colour;
    cf2.bCharSet = lf.lfCharSet;
    _tcsncpy(cf2.szFaceName, lf.lfFaceName, LF_FACESIZE);
    cf2.dwEffects = ((lf.lfWeight >= FW_BOLD) ? CFE_BOLD : 0) | (lf.lfItalic ? CFE_ITALIC : 0);
    cf2.wWeight = (WORD)lf.lfWeight;
    cf2.bPitchAndFamily = lf.lfPitchAndFamily;
    cf2.yHeight = abs(lf.lfHeight) * 1440 / g_dat.logPixelSY;
    SendDlgItemMessageA(hwnd, IDC_INFOBAR_STATUS, EM_SETCHARFORMAT, 0, (LPARAM)&cf2);

    RefreshInfobar(idat);
}

static HICON GetExtraStatusIcon(InfobarWindowData* idat)
{
	BYTE bXStatus = db_get_b(idat->mwd->windowData.hContact, idat->mwd->szProto, "XStatusId", 0);
	if (bXStatus > 0)
		return (HICON) CallProtoService(idat->mwd->szProto, PS_GETCUSTOMSTATUSICON, bXStatus, 0);

	return NULL;
}

void RefreshInfobar(InfobarWindowData* idat)
{
	HWND hwnd = idat->hWnd;
	struct SrmmWindowData *dat = idat->mwd;
	TCHAR *szContactName = GetNickname(dat->windowData.hContact, dat->szProto);
	TCHAR *szContactStatusMsg = db_get_tsa(dat->windowData.hContact, "CList", "StatusMsg");
	TCHAR *szXStatusName = db_get_tsa(idat->mwd->windowData.hContact, idat->mwd->szProto, "XStatusName");
	TCHAR *szXStatusMsg = db_get_tsa(idat->mwd->windowData.hContact, idat->mwd->szProto, "XStatusMsg");
	HICON hIcon = GetExtraStatusIcon(idat);
	TCHAR szText[2048];
	SETTEXTEX st;
	if ( szXStatusMsg && *szXStatusMsg )
		mir_sntprintf(szText, 2047, _T("%s (%s)"), TranslateTS(szXStatusName), szXStatusMsg);
	else
		mir_sntprintf(szText, 2047, _T("%s"), TranslateTS(szXStatusName));
	st.flags = ST_DEFAULT;
	st.codepage = 1200;
	SendDlgItemMessage(hwnd, IDC_INFOBAR_NAME, EM_SETTEXTEX, (WPARAM) &st, (LPARAM)szContactName);
	SendDlgItemMessage(hwnd, IDC_INFOBAR_STATUS, EM_SETTEXTEX, (WPARAM) &st, (LPARAM)szContactStatusMsg);
	hIcon = (HICON)SendDlgItemMessage(hwnd, IDC_XSTATUSICON, STM_SETICON, (WPARAM)hIcon, 0);
	if (hIcon) {
		DestroyIcon(hIcon);
	}
	SetToolTipText(hwnd, idat->hXStatusTip, szText, NULL);
	SendMessage(hwnd, WM_SIZE, 0, 0);
	InvalidateRect(hwnd, NULL, TRUE);
	//RedrawWindow(hwnd, NULL, NULL, RDW_INVALIDATE);
	RedrawWindow(GetDlgItem(hwnd, IDC_AVATAR), NULL, NULL, RDW_INVALIDATE);
	mir_free(szContactStatusMsg);
	mir_free(szContactName);
	mir_free(szXStatusName);
	mir_free(szXStatusMsg);
}

static INT_PTR CALLBACK InfobarWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static BOOL bWasCopy;
	InfobarWindowData* idat = (InfobarWindowData *) GetWindowLongPtr(hwnd, GWLP_USERDATA);
	if (!idat && msg!=WM_INITDIALOG) return FALSE;
	switch (msg) {
	case WM_INITDIALOG:
		{
			RECT rect = {0};
			bWasCopy = FALSE;
			idat = (InfobarWindowData *) lParam;
			idat->hWnd = hwnd;
			idat->hXStatusTip = CreateToolTip(hwnd, NULL, NULL, &rect);
			SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)idat);
			SendDlgItemMessage(hwnd, IDC_INFOBAR_NAME, EM_AUTOURLDETECT, (WPARAM) TRUE, 0);
			SendDlgItemMessage(hwnd, IDC_INFOBAR_NAME, EM_SETEVENTMASK, 0, ENM_MOUSEEVENTS | ENM_LINK | ENM_KEYEVENTS);
			SendDlgItemMessage(hwnd, IDC_INFOBAR_STATUS, EM_AUTOURLDETECT, (WPARAM) TRUE, 0);
			SendDlgItemMessage(hwnd, IDC_INFOBAR_STATUS, EM_SETEVENTMASK, 0, ENM_MOUSEEVENTS | ENM_LINK | ENM_KEYEVENTS);
			SetupInfobar(idat);
		}
		return TRUE;

	case WM_SIZE:
		{
			if (wParam==SIZE_RESTORED || wParam==SIZE_MAXIMIZED) {
				HDWP hdwp;
				RECT rc;
				int dlgWidth, dlgHeight;
				int avatarWidth = 0;
				int avatarHeight = 0;
				GetClientRect(hwnd, &rc);
				dlgWidth = rc.right - rc.left;
				dlgHeight = rc.bottom - rc.top;
				if (idat->mwd->avatarPic && (g_dat.flags&SMF_AVATAR)) {
					BITMAP bminfo;
					GetObject(idat->mwd->avatarPic, sizeof(bminfo), &bminfo);
					if ( bminfo.bmWidth != 0 && bminfo.bmHeight != 0 ) {
						avatarHeight = dlgHeight - 2;
						avatarWidth = bminfo.bmWidth * avatarHeight / bminfo.bmHeight;
						if (avatarWidth > dlgHeight) {
							avatarWidth = dlgHeight - 2;
							avatarHeight = bminfo.bmHeight * avatarWidth / bminfo.bmWidth;
						}
					}
				}
				hdwp = BeginDeferWindowPos(4);
				hdwp = DeferWindowPos(hdwp, GetDlgItem(hwnd, IDC_INFOBAR_NAME), 0, 16, 0, dlgWidth - avatarWidth - 2 - 32, dlgHeight/2, SWP_NOZORDER);
				hdwp = DeferWindowPos(hdwp, GetDlgItem(hwnd, IDC_INFOBAR_STATUS), 0, 16, dlgHeight/2, dlgWidth - avatarWidth - 2 - 32, dlgHeight/2, SWP_NOZORDER);
				hdwp = DeferWindowPos(hdwp, GetDlgItem(hwnd, IDC_AVATAR), 0, dlgWidth - avatarWidth - 2, (dlgHeight - avatarHeight) / 2, avatarWidth, (dlgHeight + avatarHeight - 2) / 2, SWP_NOZORDER);
				hdwp = DeferWindowPos(hdwp, GetDlgItem(hwnd, IDC_XSTATUSICON), 0, dlgWidth - avatarWidth - 2 - 16, dlgHeight/4 - 8, 16, 16, SWP_NOZORDER);
				rc.left = dlgWidth - avatarWidth - 2 - 16; 
				rc.top = dlgHeight/4 - 8;
				rc.bottom = rc.top + 20;
				rc.right = rc.left + 16;
				SetToolTipRect(hwnd, idat->hXStatusTip, &rc);
				EndDeferWindowPos(hdwp);
			}
			return TRUE;
		}
	case WM_CTLCOLORDLG:
	case WM_CTLCOLORSTATIC:
		return (INT_PTR)g_dat.hInfobarBrush;

	case WM_DROPFILES:
		SendMessage(GetParent(hwnd), WM_DROPFILES, wParam, lParam);
		return FALSE;	

	case WM_NOTIFY:
		{
			LPNMHDR pNmhdr = (LPNMHDR)lParam;
			switch (pNmhdr->idFrom) {
			case IDC_INFOBAR_NAME:
			case IDC_INFOBAR_STATUS:
				switch (pNmhdr->code) {
				case EN_MSGFILTER:
					switch (((MSGFILTER *) lParam)->msg) {
					case WM_CHAR:
						SendMessage(GetParent(hwnd), ((MSGFILTER *) lParam)->msg, ((MSGFILTER *) lParam)->wParam, ((MSGFILTER *) lParam)->lParam);
						SetWindowLongPtr(hwnd, DWLP_MSGRESULT, TRUE);
						return TRUE;
					case WM_LBUTTONUP:
						{
							CHARRANGE sel;
							SendDlgItemMessage(hwnd, pNmhdr->idFrom, EM_EXGETSEL, 0, (LPARAM)&sel);
							bWasCopy = FALSE;
							if (sel.cpMin != sel.cpMax) {
								SendDlgItemMessage(hwnd, pNmhdr->idFrom, WM_COPY, 0, 0);
								sel.cpMin = sel.cpMax ;
								SendDlgItemMessage(hwnd, pNmhdr->idFrom, EM_EXSETSEL, 0, (LPARAM)& sel);
								bWasCopy = TRUE;
							}
							SetFocus(GetParent(hwnd));
						}
					}
					break;
				case EN_LINK:
					switch (((ENLINK *) lParam)->msg) {
					case WM_RBUTTONDOWN:
					case WM_LBUTTONUP:
						if (!bWasCopy) {
							if (HandleLinkClick(g_hInst, hwnd, GetDlgItem(GetParent(hwnd), IDC_MESSAGE),(ENLINK*)lParam)) {
								SetWindowLongPtr(hwnd, DWLP_MSGRESULT, TRUE);
								return TRUE;
							}
						}
						bWasCopy = FALSE;
						break;
					}
				}
				break;
			}
			break;
		}

	case WM_DRAWITEM:
		{
			LPDRAWITEMSTRUCT dis = (LPDRAWITEMSTRUCT) lParam;
			if (dis->hwndItem == GetDlgItem(hwnd, IDC_AVATAR)) {
				RECT rect;
				HDC hdcMem = CreateCompatibleDC(dis->hDC);
				int itemWidth = dis->rcItem.right - dis->rcItem.left + 1;
				int itemHeight = dis->rcItem.bottom - dis->rcItem.top + 1;
				HBITMAP hbmMem = CreateCompatibleBitmap(dis->hDC, itemWidth, itemHeight);
				hbmMem = (HBITMAP) SelectObject(hdcMem, hbmMem);
				rect.top = 0;
				rect.left = 0;
				rect.right = itemWidth - 1;
				rect.bottom = itemHeight - 1;
				FillRect(hdcMem, &rect, g_dat.hInfobarBrush);
				if (idat->mwd->avatarPic && (g_dat.flags&SMF_AVATAR)) {
					BITMAP bminfo;
					GetObject(idat->mwd->avatarPic, sizeof(bminfo), &bminfo);
					if ( bminfo.bmWidth != 0 && bminfo.bmHeight != 0 ) {
						int avatarHeight = itemHeight;
						int avatarWidth = bminfo.bmWidth * avatarHeight / bminfo.bmHeight;
						if (avatarWidth > itemWidth) {
							avatarWidth = itemWidth;
							avatarHeight = bminfo.bmHeight * avatarWidth / bminfo.bmWidth;
						}

						AVATARDRAWREQUEST adr = { sizeof(adr) };
						adr.hContact = idat->mwd->windowData.hContact;
						adr.hTargetDC = hdcMem;
						adr.rcDraw.right = avatarWidth - 1;
						adr.rcDraw.bottom = avatarHeight - 1;
						adr.dwFlags = AVDRQ_DRAWBORDER | AVDRQ_HIDEBORDERONTRANSPARENCY;
						CallService(MS_AV_DRAWAVATAR, 0, (LPARAM)&adr);
					}
				}
				BitBlt(dis->hDC, 0, 0, itemWidth, itemHeight, hdcMem, 0, 0, SRCCOPY);
				hbmMem = (HBITMAP) SelectObject(hdcMem, hbmMem);
				DeleteObject(hbmMem);
				DeleteDC(hdcMem);
				return TRUE;
			}
			return CallService(MS_CLIST_MENUDRAWITEM, wParam, lParam);
		}
	case WM_LBUTTONDOWN:
		SendMessage(idat->mwd->hwnd, WM_LBUTTONDOWN, wParam, lParam);
		return TRUE;
	case WM_RBUTTONUP:
		{
			POINT pt;
			HMENU hMenu = (HMENU) CallService(MS_CLIST_MENUBUILDCONTACT, (WPARAM) idat->mwd->windowData.hContact, 0);
			GetCursorPos(&pt);
			TrackPopupMenu(hMenu, 0, pt.x, pt.y, 0, GetParent(hwnd), NULL);
			DestroyMenu(hMenu);
		}
		break;
	case WM_DESTROY:
		if (idat->hXStatusTip != NULL) {
			DestroyWindow(idat->hXStatusTip);
			idat->hXStatusTip = NULL;
		}
		mir_free(idat);
	}
	return FALSE;
}

InfobarWindowData *CreateInfobar(HWND hParent, struct SrmmWindowData *dat)
{
	InfobarWindowData *idat = (InfobarWindowData *) mir_alloc(sizeof(InfobarWindowData));
	idat->mwd = dat;
	idat->hWnd = CreateDialogParam(g_hInst, MAKEINTRESOURCE(IDD_INFOBAR), hParent, InfobarWndProc, (LPARAM)idat);
	RichUtil_SubClass(idat->hWnd);
	SetWindowPos(idat->hWnd, HWND_TOP, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOREPOSITION);
	return idat;
}