summaryrefslogtreecommitdiff
path: root/plugins/Clist_modern/src/modern_toolbar.cpp
blob: 05ee5239eab92f17c29d250ddddabd421480fb7c (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
/*

Miranda NG: the free IM client for Microsoft* Windows*

Copyright (c) 2012-17 Miranda NG project (https://miranda-ng.org),
Copyright (c) 2000-08 Miranda ICQ/IM project,
Copyright 2007 Artem Shpynov

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 "stdafx.h"
#include "m_skinbutton.h"
#include <m_toptoolbar.h>
#include "modern_sync.h"

struct
{
	char *pszButtonID, *pszButtonName, *pszServiceName;
	char *pszTooltipUp, *pszTooltipDn;
	int icoDefIdx, defResource, defResource2;
	BOOL bVisByDefault;
	HANDLE hButton;
}
static BTNS[] =
{
	{ "MainMenu", LPGEN("Main menu"), "CList/ShowMainMenu", LPGEN("Main menu"), nullptr, 100, IDI_RESETVIEW, IDI_RESETVIEW, TRUE },
	{ "StatusMenu", LPGEN("Status menu"), "CList/ShowStatusMenu", LPGEN("Status menu"), nullptr, 105, IDI_RESETVIEW, IDI_RESETVIEW, TRUE },
	{ "AccoMgr", LPGEN("Accounts"), MS_PROTO_SHOWACCMGR, LPGEN("Accounts..."), nullptr, 282, IDI_ACCMGR, IDI_ACCMGR, TRUE },
	{ "ShowHideOffline", LPGEN("Show/Hide offline contacts"), MS_CLIST_TOGGLEHIDEOFFLINE, LPGEN("Hide offline contacts"), LPGEN("Show offline contacts"), 110, IDI_RESETVIEW, IDI_RESETVIEW, TRUE },
	{ "FindUser", LPGEN("Find user"), "FindAdd/FindAddCommand", LPGEN("Find user"), nullptr, 140, IDI_RESETVIEW, IDI_RESETVIEW, TRUE },
	{ "Options", "Options", "Options/OptionsCommand", "Options", nullptr, 150, IDI_RESETVIEW, IDI_RESETVIEW, TRUE },
	{ "UseGroups", LPGEN("Use/Disable groups"), MS_CLIST_TOGGLEGROUPS, LPGEN("Use groups"), LPGEN("Disable groups"), 160, IDI_RESETVIEW, IDI_RESETVIEW, FALSE },
	{ "EnableSounds", LPGEN("Enable/Disable sounds"), MS_CLIST_TOGGLESOUNDS, LPGEN("Enable sounds"), LPGEN("Disable sounds"), 170, IDI_RESETVIEW, IDI_RESETVIEW, FALSE },
	{ "Minimize", LPGEN("Minimize"), "CList/ShowHide", LPGEN("Minimize"), nullptr, 180, IDI_RESETVIEW, IDI_RESETVIEW, FALSE }
};

static void SetButtonPressed(int i, int state)
{
	CallService(MS_TTB_SETBUTTONSTATE, (WPARAM)BTNS[i].hButton, state ? TTBST_PUSHED : 0);
}

void Modern_InitButtons()
{
	for (int i = 0; i < _countof(BTNS); i++) {
		TTBButton tbb = {};

		if (BTNS[i].pszButtonID) {
			tbb.name = LPGEN(BTNS[i].pszButtonName);
			tbb.pszService = BTNS[i].pszServiceName;
			tbb.pszTooltipUp = LPGEN(BTNS[i].pszTooltipUp);
			tbb.pszTooltipDn = LPGEN(BTNS[i].pszTooltipDn);

			char buf[255];
			if (i != 0) {
				mir_snprintf(buf, "%s%s%s", TTB_OPTDIR, BTNS[i].pszButtonID, "_dn");
				tbb.hIconHandleUp = RegisterIcolibIconHandle(buf, "Toolbar", BTNS[i].pszTooltipUp, L"icons\\toolbar_icons.dll", BTNS[i].icoDefIdx, g_hInst, BTNS[i].defResource);
			}
			else tbb.hIconHandleUp = RegisterIcolibIconHandle(buf, "Toolbar", BTNS[i].pszTooltipUp, nullptr, 0, nullptr, SKINICON_OTHER_MAINMENU);

			if (BTNS[i].pszTooltipDn) {
				mir_snprintf(buf, "%s%s%s", TTB_OPTDIR, BTNS[i].pszButtonID, "_up");
				tbb.hIconHandleDn = RegisterIcolibIconHandle(buf, "Toolbar", BTNS[i].pszTooltipDn, L"icons\\toolbar_icons.dll", BTNS[i].icoDefIdx + 1, g_hInst, BTNS[i].defResource2);
			}
			else tbb.hIconHandleDn = nullptr;
		}
		else tbb.dwFlags |= TTBBF_ISSEPARATOR;

		tbb.dwFlags |= (BTNS[i].bVisByDefault ? TTBBF_VISIBLE : 0);
		BTNS[i].hButton = TopToolbar_AddButton(&tbb);
	}

	SetButtonPressed(3, db_get_b(0, "CList", "HideOffline", SETTING_HIDEOFFLINE_DEFAULT));
	SetButtonPressed(6, db_get_b(0, "CList", "UseGroups", SETTING_USEGROUPS_DEFAULT));
	SetButtonPressed(7, db_get_b(0, "Skin", "UseSound", SETTING_ENABLESOUNDS_DEFAULT));
}

///////////////////////////////////////////////////////////////////////////////

#define MTBM_LAYEREDPAINT (WM_USER+20)

struct ModernToolbarCtrl : public TTBCtrl
{
	XPTHANDLE mtbXPTheme;
};

struct
{
	HBITMAP  mtb_hBmpBackground;
	COLORREF mtb_bkColour;
	WORD     mtb_backgroundBmpUse;
	BOOL     mtb_useWinColors;
}
static tbdat = { nullptr, CLCDEFAULT_BKCOLOUR, CLCDEFAULT_BKBMPUSE, CLCDEFAULT_USEWINDOWSCOLOURS };

static int ehhToolBarSettingsChanged(WPARAM hContact, LPARAM lParam)
{
	DBCONTACTWRITESETTING *cws = (DBCONTACTWRITESETTING*)lParam;
	if (hContact != 0)
		return 0;

	if (!strcmp(cws->szModule, "CList")) {
		if (!strcmp(cws->szSetting, "HideOffline"))
			SetButtonPressed(3, cws->value.bVal);
		else if (!strcmp(cws->szSetting, "UseGroups"))
			SetButtonPressed(6, cws->value.bVal);
	}
	else if (!strcmp(cws->szModule, "Skin")) {
		if (!strcmp(cws->szSetting, "UseSound"))
			SetButtonPressed(7, cws->value.bVal);
	}

	return 0;
}

static int ehhToolBarBackgroundSettingsChanged(WPARAM, LPARAM)
{
	if (tbdat.mtb_hBmpBackground) {
		DeleteObject(tbdat.mtb_hBmpBackground);
		tbdat.mtb_hBmpBackground = nullptr;
	}

	if (g_CluiData.fDisableSkinEngine) {
		tbdat.mtb_bkColour = cliGetColor("ToolBar", "BkColour", CLCDEFAULT_BKCOLOUR);
		if (db_get_b(0, "ToolBar", "UseBitmap", CLCDEFAULT_USEBITMAP)) {
			ptrW tszBitmapName(db_get_wsa(0, "ToolBar", "BkBitmap"));
			if (tszBitmapName)
				tbdat.mtb_hBmpBackground = Bitmap_Load(tszBitmapName);
		}
		tbdat.mtb_useWinColors = db_get_b(0, "ToolBar", "UseWinColours", CLCDEFAULT_USEWINDOWSCOLOURS);
		tbdat.mtb_backgroundBmpUse = db_get_b(0, "ToolBar", "BkBmpUse", CLCDEFAULT_BKBMPUSE);
	}
	PostMessage(pcli->hwndContactList, WM_SIZE, 0, 0);
	return 0;
}

static BOOL sttDrawToolBarBackground(HWND hwnd, HDC hdc, RECT *rect, ModernToolbarCtrl* pMTBInfo)
{
	BOOL bFloat = (GetParent(hwnd) != pcli->hwndContactList);
	if (g_CluiData.fDisableSkinEngine || !g_CluiData.fLayered || bFloat) {
		HBRUSH hbr;

		RECT rc;
		if (rect)
			rc = *rect;
		else
			GetClientRect(hwnd, &rc);

		if (!(tbdat.mtb_backgroundBmpUse && tbdat.mtb_hBmpBackground) && tbdat.mtb_useWinColors) {
			HRESULT hr = S_FALSE;
			if (xpt_IsThemed(pMTBInfo->mtbXPTheme))
				hr = xpt_DrawTheme(pMTBInfo->mtbXPTheme, pMTBInfo->hWnd, hdc, 0, 0, &rc, &rc);

			if (hr == S_FALSE) {
				hbr = GetSysColorBrush(COLOR_3DFACE);
				FillRect(hdc, &rc, hbr);
			}
		}
		else if (!tbdat.mtb_hBmpBackground && !tbdat.mtb_useWinColors) {
			hbr = CreateSolidBrush(tbdat.mtb_bkColour);
			FillRect(hdc, &rc, hbr);
			DeleteObject(hbr);
		}
		else DrawBackGround(hwnd, hdc, tbdat.mtb_hBmpBackground, tbdat.mtb_bkColour, tbdat.mtb_backgroundBmpUse);
	}
	return TRUE;
}

static void sttDrawNonLayeredSkinedBar(HWND hwnd, HDC hdc)
{
	RECT rc = { 0 };
	GetClientRect(hwnd, &rc);
	rc.right++;
	rc.bottom++;
	HDC hdc2 = CreateCompatibleDC(hdc);
	HBITMAP hbmp = ske_CreateDIB32(rc.right, rc.bottom);
	HBITMAP hbmpo = (HBITMAP)SelectObject(hdc2, hbmp);
	if (GetParent(hwnd) != pcli->hwndContactList) {
		HBRUSH br = GetSysColorBrush(COLOR_3DFACE);
		FillRect(hdc2, &rc, br);
	}
	else ske_BltBackImage(hwnd, hdc2, &rc);

	SendMessage(hwnd, MTBM_LAYEREDPAINT, (WPARAM)hdc2, 0);
	BitBlt(hdc, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, hdc2, rc.left, rc.top, SRCCOPY);
	SelectObject(hdc2, hbmpo);
	DeleteObject(hbmp);
	DeleteDC(hdc2);

	SelectObject(hdc, (HFONT)GetStockObject(DEFAULT_GUI_FONT));
	ValidateRect(hwnd, nullptr);
}

static LRESULT CALLBACK toolbarWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	ModernToolbarCtrl* pMTBInfo = (ModernToolbarCtrl*)GetWindowLongPtr(hwnd, 0);

	switch (msg) {
	case WM_ERASEBKGND:
		return (g_CluiData.fDisableSkinEngine) ? sttDrawToolBarBackground(hwnd, (HDC)wParam, nullptr, pMTBInfo) : 0;

	case WM_NCPAINT:
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			BOOL bFloat = (GetParent(hwnd) != pcli->hwndContactList);
			if (g_CluiData.fDisableSkinEngine || !g_CluiData.fLayered || bFloat) {
				BeginPaint(hwnd, &ps);
				if ((!g_CluiData.fLayered || bFloat) && !g_CluiData.fDisableSkinEngine)
					sttDrawNonLayeredSkinedBar(hwnd, ps.hdc);
				else
					sttDrawToolBarBackground(hwnd, ps.hdc, &ps.rcPaint, pMTBInfo);
				EndPaint(hwnd, &ps);
			}
		}
		return DefWindowProc(hwnd, msg, wParam, lParam);

	case WM_NOTIFY:
		if (((LPNMHDR)lParam)->code == BUTTONNEEDREDRAW)
			pcli->pfnInvalidateRect(hwnd, nullptr, FALSE);
		return 0;

	case MTBM_LAYEREDPAINT:
		{
			RECT MyRect = { 0 };
			HDC hDC = (HDC)wParam;
			GetWindowRect(hwnd, &MyRect);

			RECT rcClient;
			GetClientRect(hwnd, &rcClient);
			SkinDrawGlyph(hDC, &rcClient, &rcClient, "Bar,ID=ToolBar,Part=Background");

			for (int i = 0; i < pMTBInfo->pButtonList->realCount; i++) {
				RECT childRect;
				POINT Offset;
				TTBCtrlButton* mtbi = (TTBCtrlButton*)pMTBInfo->pButtonList->items[i];
				GetWindowRect(mtbi->hWindow, &childRect);
				Offset.x = childRect.left - MyRect.left;
				Offset.y = childRect.top - MyRect.top;
				SendMessage(mtbi->hWindow, BUTTONDRAWINPARENT, (WPARAM)hDC, (LPARAM)&Offset);
			}
		}
		return 0;

	case WM_DESTROY:
		xpt_FreeThemeForWindow(hwnd);
		CallService(MS_SKINENG_REGISTERPAINTSUB, (WPARAM)hwnd, 0);
		break;
	}

	return mir_callNextSubclass(hwnd, toolbarWndProc, msg, wParam, lParam);
}

static int ToolBar_LayeredPaintProc(HWND hWnd, HDC hDC, RECT *, HRGN, DWORD, void *)
{
	return SendMessage(hWnd, MTBM_LAYEREDPAINT, (WPARAM)hDC, 0);
}

void CustomizeToolbar(HWND hwnd)
{
	mir_subclassWindow(hwnd, toolbarWndProc);
	SendMessage(hwnd, TTB_SETCUSTOMDATASIZE, 0, sizeof(ModernToolbarCtrl));

	ModernToolbarCtrl* pMTBInfo = (ModernToolbarCtrl*)GetWindowLongPtr(hwnd, 0);

	CLISTFrame Frame = { sizeof(Frame) };
	Frame.tname = L"Toolbar";
	Frame.hWnd = hwnd;
	Frame.align = alTop;
	Frame.Flags = F_VISIBLE | F_NOBORDER | F_LOCKED | F_UNICODE | F_NO_SUBCONTAINER;
	Frame.height = 18;
	Frame.hIcon = Skin_LoadIcon(SKINICON_OTHER_FRAME);
	pMTBInfo->hFrame = (HANDLE)CallService(MS_CLIST_FRAMES_ADDFRAME, (WPARAM)&Frame, 0);

	CallService(MS_SKINENG_REGISTERPAINTSUB, (WPARAM)hwnd, (LPARAM)ToolBar_LayeredPaintProc);

	pMTBInfo->mtbXPTheme = xpt_AddThemeHandle(hwnd, L"TOOLBAR");
	pMTBInfo->bHardUpdate = TRUE;

	Modern_InitButtons();
}

#define TTB_OPTDIR "TopToolBar"

static char szUrl[] = "https://miranda-ng.org/p/TopToolBar";

static wchar_t szWarning[] = LPGENW("To view a toolbar in Clist_modern you need the TopToolBar plugin. Click Yes to download it or No to continue");

static void CopySettings(const char* to, const char* from, int defValue)
{
	db_set_b(0, TTB_OPTDIR, to, db_get_b(0, "ModernToolBar", from, defValue));
}

/////////////////////////////////////////////////////////////////////////////////////////

void CustomizeButton(HANDLE ttbid, HWND hWnd, LPARAM lParam);

static int Toolbar_ModuleReloaded(WPARAM wParam, LPARAM)
{
	PLUGININFOEX *pInfo = (PLUGININFOEX*)wParam;
	if (!mir_strcmpi(pInfo->shortName, "TopToolBar"))
		TopToolbar_SetCustomProc(CustomizeButton, 0);

	return 0;
}

static int Toolbar_ModulesLoaded(WPARAM, LPARAM)
{
	CallService(MS_BACKGROUNDCONFIG_REGISTER, (WPARAM)(LPGEN("Toolbar background")"/ToolBar"), 0);

	HookEvent(ME_DB_CONTACT_SETTINGCHANGED, ehhToolBarSettingsChanged);

	TopToolbar_SetCustomProc(CustomizeButton, 0);

	BYTE bOldSetting = 0;
	if (!db_get_b(0, "Compatibility", "TTB_Upgrade", 0)) {
		if (bOldSetting = db_get_b(0, "CLUI", "ShowButtonBar", 1)) {
			CopySettings("BUTTWIDTH", "option_Bar0_BtnWidth", 20);
			CopySettings("BUTTHEIGHT", "option_Bar0_BtnHeight", 20);
			CopySettings("BUTTGAP", "option_Bar0_BtnSpace", 1);
			CopySettings("BUTTAUTOSIZE", "option_Bar0_Autosize", 1);
			CopySettings("BUTTMULTI", "option_Bar0_Multiline", 1);

			db_unset(0, "CLUI", "ShowButtonBar");

			db_delete_module(0, "ModernToolBar");
		}
		db_set_b(0, "Compatibility", "TTB_Upgrade", 1);
	}

	if (!ServiceExists(MS_TTB_REMOVEBUTTON) && bOldSetting == 1)
		if (IDYES == MessageBox(nullptr, TranslateW(szWarning), TranslateT("Toolbar upgrade"), MB_ICONQUESTION | MB_YESNO))
			Utils_OpenUrl(szUrl);

	return 0;
}

///////////////////////////////////////////////////////////////////////////////

HRESULT ToolbarLoadModule()
{
	HookEvent(ME_BACKGROUNDCONFIG_CHANGED, ehhToolBarBackgroundSettingsChanged);
	HookEvent(ME_SYSTEM_MODULELOAD, Toolbar_ModuleReloaded);
	HookEvent(ME_SYSTEM_MODULESLOADED, Toolbar_ModulesLoaded);
	return S_OK;
}