summary refs log tree commit diff stats
path: root/plugins/hextray/sdAlerts.cpp
blob: bf61f1ebc7cdf7a269a45a57044ff2d00f51a40c (plain) (blame)
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
/* X-Tray
 * Copyright (C) 2005 Michael Hotaling <Mike.Hotaling@SinisterDevelopments.com>
 *
 * X-Tray 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.
 * 
 * X-Tray 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 X-Tray; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <windows.h>
#include <winuser.h>
#include <stdio.h>
#include "utility.h"
#include "resource.h"
#include "sdAlerts.h"

int g_iAlerts = 0;

void sdSystemAlert(HINSTANCE hModule, UINT uiDialog, char *szMsg, char *szName, unsigned int iTime)
{
	TCHAR wszMsg[256];
	TCHAR wszName[64];

	HWND hDialog;
	RECT rcWorkArea, rcDlg;
	int ixPos, iyPos;
	int iNumPerCol;
	
	hDialog = CreateDialog(hModule, MAKEINTRESOURCE(uiDialog), NULL, (DLGPROC)sdAlertProc);

	SystemParametersInfo(SPI_GETWORKAREA, 0, &rcWorkArea, 0);
	GetWindowRect(hDialog, &rcDlg);

	iNumPerCol = ((rcWorkArea.bottom - rcWorkArea.top) / (rcDlg.bottom - rcDlg.top));
	ixPos = rcWorkArea.right - (rcDlg.right - rcDlg.left) + 1;
	iyPos = rcWorkArea.bottom - (rcDlg.bottom - rcDlg.top);

	if((g_iAlerts >= iNumPerCol) && (iNumPerCol > 0))
	{
		ixPos -= ((g_iAlerts / iNumPerCol) * (rcDlg.right - rcDlg.left));
		iyPos -= ((g_iAlerts - (iNumPerCol * (g_iAlerts / iNumPerCol))) * (rcDlg.bottom - rcDlg.top));
	}
	else
	{
		iyPos -= (g_iAlerts * (rcDlg.bottom - rcDlg.top));
	}
	SetWindowPos(hDialog, HWND_TOPMOST, ixPos, iyPos, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE);
	
	ConvertString(szName, wszName, 64);
	ConvertString(szMsg, wszMsg, 256);

	SetWindowText(hDialog, wszName);
	SetDlgItemText(hDialog, IDC_ALERT_MSG, wszMsg);
	ShowWindow(hDialog, SW_SHOWNA);

	if(iTime > 0)
	{
		SetTimer(hDialog, 1, iTime, NULL);
	}

	g_iAlerts++;
}

void sdCloseAlerts()
{
	PostMessage(HWND_BROADCAST, RegisterWindowMessage(TEXT("xTray:CloseAllAlertWindows")), 0, 0);
}

LRESULT CALLBACK sdAlertProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
	switch(msg)
	{
	case WM_CLOSE:
		if(g_iAlerts > 0){ g_iAlerts--; }
		DestroyWindow(hwnd);
		return TRUE;
		break;
	case WM_TIMER:
		if(g_iAlerts > 0){ g_iAlerts--; }
		AnimateWindow(hwnd, 600, AW_SLIDE | AW_HIDE | AW_VER_POSITIVE);
		DestroyWindow(hwnd);
		return TRUE;
		break;
	default:
		if(msg == RegisterWindowMessage(TEXT("xTray:CloseAllAlertWindows")))
		{
			if(g_iAlerts > 0){ g_iAlerts--; }
			DestroyWindow(hwnd);
			return TRUE;
		}
		break;
	}

	return FALSE;
}

int sdAlertNum()
{
	return g_iAlerts;
}