summaryrefslogtreecommitdiff
path: root/last_contact/LastContact.cpp
blob: 6499ed3a535669d9c62d047b9a9c514a8495e9f1 (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
/*
Miranda plugin template, originally by Richard Hughes
http://miranda-icq.sourceforge.net/

This file is placed in the public domain. Anybody is free to use or
modify it as they wish with no restriction.
There is no warranty.
*/

#include "common.h"
#include "options.h"

HINSTANCE hInst;
PLUGINLINK *pluginLink;
MM_INTERFACE mmi;

HANDLE hEventWindow;

//typedef LRESULT (CALLBACK *WNDPROC)(HWND, UINT, WPARAM, LPARAM);

HANDLE hLastContact = 0;
HWND hWndLastContact = 0;

PLUGININFO pluginInfo={
	sizeof(PLUGININFO),
	MODULE,
	PLUGIN_MAKE_VERSION(0,0,0,4),
	"Re-open the last open message window using a configurable hot key.",
	"Scott Ellis",
	"mail@scottellis.com.au",
	"© 2006 Scott Ellis",
	"http://scottellis.com.au",
	0,		//not transient
	0		//doesn't replace anything built-in
};

extern "C" BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
	hInst=hinstDLL;
	return TRUE;
}

extern "C" __declspec(dllexport) PLUGININFO* MirandaPluginInfo(DWORD mirandaVersion)
{
	return &pluginInfo;
}

void PlaceOnTop(HWND hwnd) {
	// set window style
	//LONG ex_style = GetWindowLong(hwnd, GWL_EXSTYLE);
	//ex_style |= WS_EX_TOPMOST;
	//SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
	
	SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);

	PostMessage(hwnd, WM_SYSCOMMAND, SC_HOTKEY, (LPARAM)hwnd);

	if(DBGetContactSettingByte(0, MODULE, "MakeChildFullScreen", 0)) {
		// make window child of any full-screen topmost windows
		int w = GetSystemMetrics(SM_CXSCREEN);
		int h = GetSystemMetrics(SM_CYSCREEN);

		HWND hWnd = 0;
		while (hWnd = FindWindowEx(NULL, hWnd, NULL, NULL)) {
			if(!IsWindowVisible(hWnd) || IsIconic(hWnd))
				continue;
				
			if (!(GetWindowLong(hWnd, GWL_EXSTYLE) & WS_EX_TOPMOST))
				continue;

			// not sure if this could be done more simply using 'IsZoomed'?
			RECT WindowRect;
			GetWindowRect(hWnd, &WindowRect);
			if ((w != (WindowRect.right - WindowRect.left)) || (h != (WindowRect.bottom - WindowRect.top)))
				continue;

			SetParent(hwnd, hWnd);
			break;
		}
	}
}

int WindowEvent(WPARAM wParam, LPARAM lParam) {
	MessageWindowEventData *ed = (MessageWindowEventData *)lParam;

	/*
	// ignore chat room contacts?
	char *proto = (char *)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)ed->hContact, 0);
	if(proto && DBGetContactSettingByte(ed->hContact, proto, "ChatRoom", 0)) return 0; 
	*/

	switch(ed->uType) {
	case MSG_WINDOW_EVT_OPENING:
		break;
	case MSG_WINDOW_EVT_OPEN:
		break;
	case MSG_WINDOW_EVT_CLOSING:
		break;
	case MSG_WINDOW_EVT_CLOSE:
		hLastContact = ed->hContact;
		hWndLastContact = ed->hwndWindow;
		if(options.make_topmost) {
			HWND hwnd = hWndLastContact, parent = 0;
			while((parent = GetParent(hwnd)) != 0) hwnd = parent;
			PlaceOnTop(hwnd);
		}
		break;
	}

	return 0;
}

int ModulesLoaded(WPARAM wParam,LPARAM lParam) {
	if(ServiceExists(MS_UPDATE_REGISTER)) {
		// register with updater
		Update update = {0};
		char szVersion[16];

		update.cbSize = sizeof(Update);

		update.szComponentName = pluginInfo.shortName;
		update.pbVersion = (BYTE *)CreateVersionString(pluginInfo.version, szVersion);
		update.cpbVersion = strlen((char *)update.pbVersion);

		update.szUpdateURL = UPDATER_AUTOREGISTER;
		
		// these are the three lines that matter - the archive, the page containing the version string, and the text (or data) 
		// before the version that we use to locate it on the page
		// (note that if the update URL and the version URL point to standard file listing entries, the backend xml
		// data will be used to check for updates rather than the actual web page - this is not true for beta urls)
		update.szBetaUpdateURL = "http://www.scottellis.com.au/miranda_plugins/last_contact.zip";
		update.szBetaVersionURL = "http://www.scottellis.com.au/miranda_plugins/ver_last_contact.html";
		update.pbBetaVersionPrefix = (BYTE *)MODULE " version ";
		
		update.cpbBetaVersionPrefix = strlen((char *)update.pbBetaVersionPrefix);

		CallService(MS_UPDATE_REGISTER, 0, (WPARAM)&update);
	}

	hEventWindow = HookEvent(ME_MSG_WINDOWEVENT, WindowEvent);

	return 0;
}

int Shutdown(WPARAM wParam, LPARAM lParam) {
	if(hEventWindow) UnhookEvent(hEventWindow);
	return 0;
}

void CALLBACK TimerProc(HWND, UINT, UINT_PTR, DWORD) {
	if(GetAsyncKeyState(options.vk) 
		&& (options.mod_alt == false || GetAsyncKeyState(VK_MENU)) 
		&& (options.mod_shift == false || GetAsyncKeyState(VK_SHIFT)) 
		&& (options.mod_ctrl == false || GetAsyncKeyState(VK_CONTROL)) 
		&& hLastContact) 
	{
		if(options.hide_if_visible && IsWindow(hWndLastContact) && IsWindowVisible(hWndLastContact)) {
			PostMessage(hWndLastContact, WM_COMMAND, 1023, 0);			// chat close tab
			PostMessage(hWndLastContact, WM_COMMAND, IDCANCEL, 0);		// srmm/scriver close session button
			PostMessage(hWndLastContact, WM_COMMAND, 1025, 0);			// tabsrmm close session button
		} else {
			CallService(MS_MSG_SENDMESSAGE, (WPARAM)hLastContact, 0);
		}
	}
}

UINT_PTR timer_id;

extern "C" int __declspec(dllexport) Load(PLUGINLINK *link)
{
	pluginLink = link;

	mir_getMMI(&mmi);

	HookEvent(ME_SYSTEM_MODULESLOADED, ModulesLoaded);
	HookEvent(ME_SYSTEM_PRESHUTDOWN, Shutdown);

	InitOptions();

	// clear the key pressed flags
	GetAsyncKeyState(options.vk);
	if(options.mod_alt) GetAsyncKeyState(VK_MENU);
	if(options.mod_shift) GetAsyncKeyState(VK_SHIFT);
	if(options.mod_ctrl) GetAsyncKeyState(VK_CONTROL);

	timer_id = SetTimer(0, 0, 500, TimerProc);

	return 0;
}

extern "C" int __declspec(dllexport) Unload(void)
{
	KillTimer(0, timer_id);
	return 0;
}