summaryrefslogtreecommitdiff
path: root/messagebox.c
blob: b3999244cae7c7e7097ce748946812e202bebd17 (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
#include "messagebox.h"

cMessageBoxThread::cMessageBoxThread(cPixmap *content, int displayTime) {
	this->content = content;
	FrameTime = 30; // ms
	FadeTime = 200; // ms
	this->displayTime = displayTime;
}

cMessageBoxThread::~cMessageBoxThread(void) {
    Cancel(-1);
	while (Active())
        cCondWait::SleepMs(10);
}

void cMessageBoxThread::Action(void) {
	uint64_t Start = cTimeMs::Now();
	while (Running()) {
		uint64_t Now = cTimeMs::Now();
		cPixmap::Lock();
		double t = min(double(Now - Start) / FadeTime, 1.0);
	    int Alpha = t * ALPHA_OPAQUE;
		if (Running() && content) {
			content->SetAlpha(Alpha);
			osdManager.flush();
		}
		cPixmap::Unlock();
        int Delta = cTimeMs::Now() - Now;
        if (Delta < FrameTime)
           cCondWait::SleepMs(FrameTime - Delta);
		if ((Now - Start) > FadeTime)
			break;
    }
	cCondWait::SleepMs(displayTime - 2*FadeTime);
	Start = cTimeMs::Now();
	while (Running()) {
		uint64_t Now = cTimeMs::Now();
		cPixmap::Lock();
		double t = min(double(Now - Start) / FadeTime, 1.0);
	    int Alpha = (1-t) * ALPHA_OPAQUE;
		if (Running() && content) {
			content->SetAlpha(Alpha);
			osdManager.flush();
		}
		cPixmap::Unlock();
        int Delta = cTimeMs::Now() - Now;
        if (Delta < FrameTime)
           cCondWait::SleepMs(FrameTime - Delta);
		if ((Now - Start) > FadeTime)
			break;
    }
	osdManager.flush();
}

//--cMessageBox-------------------------------------------------------------
cMessageBoxThread *cMessageBox::msgboxThread = NULL;
cPixmap *cMessageBox::content = NULL;

bool cMessageBox::Start(int displayTime, cString msg) {
	int width = (tvguideConfig.osdWidth - 600)/2;
	if (!content) {
		int height = 400;
		content = osdManager.requestPixmap(5, cRect((tvguideConfig.osdWidth - width)/2, 
													(tvguideConfig.osdHeight- height)/2, 
													width, height));
	}
	if (msgboxThread) {
		delete msgboxThread;
		msgboxThread = NULL;
	}
	if (!msgboxThread) {
		msgboxThread = new cMessageBoxThread(content, displayTime);
		cTextWrapper message;
		message.Set(msg, tvguideConfig.FontMessageBox, width - 40);
		int textHeight = tvguideConfig.FontMessageBox->Height();
		int textLines = message.Lines();
		int height = textLines * (textHeight+20);
		cPixmap::Lock();
		content->SetViewPort(cRect((tvguideConfig.osdWidth - width)/2,(tvguideConfig.osdHeight- height)/2, width, height));
		content->SetAlpha(0);
		content->Fill(theme.Color(clrBorder));
		content->DrawRectangle(cRect(2,2,width-4, height-4), theme.Color(clrBackground));
		int textWidth = 0;
		for (int i=0; i<textLines; i++) {
			textWidth = tvguideConfig.FontMessageBox->Width(message.GetLine(i));
			content->DrawText(cPoint((width - textWidth)/2, 20 + i*textHeight), message.GetLine(i), theme.Color(clrFont), clrTransparent, tvguideConfig.FontMessageBox);
		}
		cPixmap::Unlock();
		msgboxThread->Start();
        return true;
	}
	return false;
}

void cMessageBox::Stop(void) {
	if (msgboxThread) {
		delete msgboxThread;
		msgboxThread = NULL;
	}
}

void cMessageBox::Destroy(void) {
	if (msgboxThread) {
		delete msgboxThread;
		msgboxThread = NULL;
	}
	if (content) {
		osdManager.releasePixmap(content);
		content = NULL;
	}
}