aboutsummaryrefslogtreecommitdiff
path: root/Src/omBrowser/toolbarProgress.cpp
blob: 3e544711bc6841f47e9bfa63ff6e6f321b3a0100 (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
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
#include "main.h"
#include "./toolbarProgress.h"
#include "./toolbar.h"
#include "./graphics.h"
#include "./resource.h"
#include "../Plugins/General/gen_ml/ml_ipc_0313.h"
#include "./ifc_imageloader.h"
#include <strsafe.h>

#define PROGRESS_FRAMECOUNT		9

static ATOM TOOLBARPROGRESS_PROP = 0;
#define TIMER_PROGRESSSTEP_ID			111
#define TIMER_PROGRESSSTEP_INTERVAL		120


ToolbarProgress::ToolbarProgress(LPCSTR pszName, UINT nStyle, LPCWSTR pszText, LPCWSTR pszDescription) 
	: ToolbarItem(pszName, nStyle, ICON_NONE, pszText, pszDescription), 
		bitmap(NULL), frame(0), hTimer(NULL)

{
}

ToolbarProgress::~ToolbarProgress()
{	
	if (NULL != hTimer)
		Animate(hTimer, FALSE);
	
	if (NULL != bitmap)
		DeleteObject(bitmap);
}

static void CALLBACK ToolbarProgress_TimerElapsed(HWND hwnd, UINT uMsg, UINT_PTR eventId, DWORD dwTime)
{
	ToolbarProgress *item = (ToolbarProgress*)GetProp(hwnd, MAKEINTATOM(TOOLBARPROGRESS_PROP));
	if (NULL == item || 0 != ((ToolbarItem::stateDisabled | ToolbarItem::stateHidden) & item->GetStyle()))
	{
		if (NULL != item)
			item->Animate(hwnd, FALSE);
		else
			KillTimer(hwnd, TIMER_PROGRESSSTEP_ID);
		return;
	}
	item->Animate(hwnd, TRUE);
}


ToolbarItem* CALLBACK ToolbarProgress::CreateInstance(ToolbarItem::Template *item)
{
	if (NULL == item) 
		return NULL;

	return new ToolbarProgress( (NULL != item->name) ? item->name : TOOLCLS_PROGRESS,
							item->style | styleNoChevron,
							item->text, 
							item->description);
}

void ToolbarProgress::SetStyle(HWND hToolbar, UINT newStyle, UINT styleMask)
{
	__super::SetStyle(hToolbar, newStyle, styleMask);

	if (0 == ((stateDisabled | stateHidden) & style))
	{
		Animate(hToolbar, TRUE);
	}
}

BOOL ToolbarProgress::Animate(HWND hToolbar, BOOL fAnimate)
{
	if (FALSE == fAnimate)
	{
		if (NULL != hTimer)
		{
			RemoveProp(hTimer, MAKEINTATOM(TOOLBARPROGRESS_PROP));
			KillTimer(hTimer, TIMER_PROGRESSSTEP_ID);
			hTimer = NULL;
		}

		if (0 != frame)
		{
			frame = 0;
			InvalidateRect(hToolbar, &rect, FALSE);
		}

		
	}
	else
	{
		if (NULL == hTimer)
		{
			if (0 == TOOLBARPROGRESS_PROP)
			{
				TOOLBARPROGRESS_PROP = GlobalAddAtom(L"omToolbarProgressProp");
				if (0 == TOOLBARPROGRESS_PROP) return FALSE;
			}

			if (!SetProp(hToolbar, MAKEINTATOM(TOOLBARPROGRESS_PROP), (HANDLE)this))
				return FALSE;
	
			if (!SetTimer(hToolbar, TIMER_PROGRESSSTEP_ID, TIMER_PROGRESSSTEP_INTERVAL,
					ToolbarProgress_TimerElapsed))
			{
				RemoveProp(hToolbar, MAKEINTATOM(TOOLBARPROGRESS_PROP));
				return FALSE;

			}
			hTimer = hToolbar;
		}

		if (++frame >= PROGRESS_FRAMECOUNT)
		{
			frame = 1;
		}
		InvalidateRect(hToolbar, &rect, FALSE);
		
	}

	return TRUE;
}

void ToolbarProgress::UpdateSkin(HWND hToolbar)
{
	
	if (NULL != bitmap)
	{
		DeleteObject(bitmap);
		bitmap = NULL;
	}
				
	ifc_omimageloader *loader;
	
	if (SUCCEEDED(Plugin_QueryImageLoader(Plugin_GetInstance(), MAKEINTRESOURCE(IDR_TOOLBARPROGRESS_IMAGE), TRUE, &loader)))
	{
		BITMAPINFOHEADER headerInfo;
		BYTE *pixelData;
		if (SUCCEEDED(loader->LoadBitmapEx(&bitmap, &headerInfo, (void**)&pixelData)))
		{
			if (headerInfo.biHeight < 0) headerInfo.biHeight = -headerInfo.biHeight;
			
			Image_Colorize(pixelData, headerInfo.biWidth, headerInfo.biHeight, headerInfo.biBitCount, 
				Toolbar_GetBkColor(hToolbar), Toolbar_GetFgColor(hToolbar), TRUE);
		}
		loader->Release();
	}

}

BOOL ToolbarProgress::AdjustRect(HWND hToolbar, RECT *proposedRect)
{
	BITMAP bm;
	if (NULL == bitmap || sizeof(BITMAP) != GetObject(bitmap, sizeof(BITMAP), &bm))
		return FALSE;

	if (bm.bmHeight < 0) bm.bmHeight = -bm.bmHeight;
	bm.bmHeight /= PROGRESS_FRAMECOUNT;
	proposedRect->right = proposedRect->left + bm.bmWidth;
	proposedRect->top += (((proposedRect->bottom - proposedRect->top) - bm.bmHeight)/2);
	proposedRect->bottom = proposedRect->top + bm.bmHeight;

	return TRUE;
}

BOOL ToolbarProgress::Paint(HWND hToolbar, HDC hdc, const RECT *paintRect, UINT state)
{	
	BITMAP bm;
	if (NULL == bitmap || sizeof(BITMAP) != GetObject(bitmap, sizeof(BITMAP), &bm))
		return FALSE;
	
	if (bm.bmHeight < 0) bm.bmHeight = -bm.bmHeight;
	bm.bmHeight /= PROGRESS_FRAMECOUNT;
	
	RECT blitRect;
	if (!IntersectRect(&blitRect, paintRect))
		return TRUE;
	
	BOOL success = FALSE;
	HDC hdcSrc = CreateCompatibleDC(hdc);

	if (NULL != hdcSrc)
	{
		HBITMAP hbmpOld = (HBITMAP)SelectObject(hdcSrc, bitmap);

		success = BitBlt(hdc, blitRect.left, blitRect.top, 
					blitRect.right - blitRect.left, blitRect.bottom - blitRect.top, 
					hdcSrc, 
					blitRect.left - rect.left, 
					bm.bmHeight * frame + (blitRect.top - rect.top), 
					SRCCOPY);

		SelectObject(hdcSrc, hbmpOld);
		DeleteDC(hdcSrc);
	}
	
	return success;

}