summaryrefslogtreecommitdiff
path: root/plugins/MirandaG15/src/CChatScreen.cpp
blob: 9fb9acb88bbce387b71efef479a71408d34e9d21 (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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
#include "stdafx.h"
#include "CChatScreen.h"
#include "CConfig.h"
#include "CAppletManager.h"

//************************************************************************
// Constructor
//************************************************************************
CChatScreen::CChatScreen()
{
	m_bTyping = false;
	m_hContact = NULL;
	m_bHideTitle = false;
	m_bHideLabels = false;

	m_dwMaximizedTimer = 0;
	m_bMaximizedTimer = false;
	m_bCloseTimer = false;
	m_dwCloseTimer = 0;

	m_eReplyState = REPLY_STATE_NONE;
	m_iStatus = ID_STATUS_OFFLINE;
}

//************************************************************************
// Destructor
//************************************************************************
CChatScreen::~CChatScreen()
{
}

//************************************************************************
// Initializes the screen 
//************************************************************************
bool CChatScreen::Initialize()
{
	if(!CScreen::Initialize())
		return false;

	m_InfoText.Initialize();
	m_UserName.Initialize();
	m_UserStatus.Initialize();
	m_UserProto.Initialize();
	m_Input.Initialize();
	m_TextLog.Initialize();
	m_Scrollbar.Initialize();
	
	UpdateObjects();
	// other attributes
		m_InfoText.SetAlignment(DT_CENTER);
		m_InfoText.SetWordWrap(TRUE);
		m_InfoText.SetText(_T(""));
		m_InfoText.Show(0);

		m_UserName.SetAlignment(DT_CENTER);
		m_UserName.SetWordWrap(TRUE);
		m_UserName.SetText(_T("Proto"));


		m_UserStatus.SetAlignment(DT_LEFT);
		m_UserStatus.SetWordWrap(TRUE);
		m_UserStatus.SetText(_T("Status"));
	
		
		m_UserProto.SetAlignment(DT_RIGHT);
		m_UserProto.SetWordWrap(TRUE);
		m_UserProto.SetText(_T("User"));

		m_Input.Show(0);
	
		
		
		m_TextLog.Show(1);
		
	
	
		m_TextLog.SetScrollbar(&m_Scrollbar);

		AddObject(&m_Scrollbar);
		AddObject(&m_TextLog);
		AddObject(&m_Input);
		AddObject(&m_InfoText);
		AddObject(&m_UserName);
		AddObject(&m_UserStatus);
		AddObject(&m_UserProto);


	SetButtonBitmap(0,IDB_UP);
	SetButtonBitmap(1,IDB_DOWN);
	SetButtonBitmap(2,IDB_HISTORY);
	SetButtonBitmap(3,IDB_REPLY);

	return true;
}

//************************************************************************
// Shutdown the screen
//************************************************************************
bool CChatScreen::Shutdown()
{
	if(!CScreen::Shutdown())
		return false;

	return true;
}

//************************************************************************
// update's the screens objects ( sizes, fonts etc)
//************************************************************************
void CChatScreen::UpdateObjects()
{
	m_bHideTitle = false;
	m_bHideLabels = !CConfig::GetBoolSetting(SHOW_LABELS);
	
	if(IsMaximized())
	{
		if(!m_bHideTitle && !CConfig::GetBoolSetting(MAXIMIZED_TITLE))
			m_bHideTitle = true;

		if(!m_bHideLabels && !CConfig::GetBoolSetting(MAXIMIZED_LABELS))
			m_bHideLabels = true;
	}

	// Fonts
	m_TextLog.SetFont(CConfig::GetFont(FONT_SESSION));
	m_Input.SetFont(CConfig::GetFont(FONT_SESSION));
	m_InfoText.SetFont(CConfig::GetFont(FONT_SESSION));

	m_UserName.SetFont(CConfig::GetFont(FONT_TITLE));
	m_UserStatus.SetFont(CConfig::GetFont(FONT_TITLE));
	m_UserProto.SetFont(CConfig::GetFont(FONT_TITLE));
		
	int w = GetWidth() - 8;
	// Sizes
	m_UserName.SetSize(w*0.4, CConfig::GetFontHeight(FONT_TITLE));
	m_UserStatus.SetSize(w*0.25, CConfig::GetFontHeight(FONT_TITLE));
	m_UserProto.SetSize(w*0.3, CConfig::GetFontHeight(FONT_TITLE));
	
	int iHeight =GetHeight();
	iHeight -= m_bHideTitle?0:CConfig::GetFontHeight(FONT_TITLE)+2;
	iHeight -= m_bHideLabels?0:5;

	m_Input.SetSize(GetWidth()-5, iHeight);
	m_TextLog.SetSize(GetWidth()-5, iHeight);
	
	m_InfoText.SetSize(160, 28);
	m_InfoText.SetOrigin(0,(iHeight-CConfig::GetFontHeight(FONT_SESSION))/2);

	// Origins
	
	m_UserName.SetOrigin(8+w*0.25, 0);
	m_UserStatus.SetOrigin(8, 0);
	m_UserProto.SetOrigin(GetWidth()-w*0.3, 0);
	
	m_TextLog.SetOrigin(0, m_bHideTitle?0:CConfig::GetFontHeight(FONT_TITLE)+2);
	m_Input.SetOrigin(0,m_bHideTitle?0:CConfig::GetFontHeight(FONT_TITLE)+2);
	
	m_InfoText.SetOrigin(0, 10);
	
	m_UserName.Show(!m_bHideTitle);
	m_UserStatus.Show(!m_bHideTitle);
	m_UserProto.Show(!m_bHideTitle);

	m_Scrollbar.SetOrigin(GetWidth()-4,(m_bHideTitle?0:CConfig::GetFontHeight(FONT_TITLE)+2));
	m_Scrollbar.SetSize(4,iHeight);

	// other options
	m_TextLog.SetLogSize(CConfig::GetIntSetting(SESSION_LOGSIZE));
		
	m_Input.SetBreakKeys(CConfig::GetBoolSetting(SESSION_SENDRETURN)?KEYS_RETURN:KEYS_CTRL_RETURN);
	m_Input.ShowSymbols(CConfig::GetBoolSetting(SESSION_SYMBOLS));

	ShowButtons(!m_bHideLabels);
}

//************************************************************************
// update's the screens title labels
//************************************************************************
void CChatScreen::UpdateLabels()
{
	tstring strNickname = CAppletManager::GetContactDisplayname(m_hContact);
	char *szProto = (char*)CallService(MS_PROTO_GETCONTACTBASEPROTO,(UINT)m_hContact,0);
	char *szStatus = NULL;
	m_iStatus = ID_STATUS_OFFLINE;

	tstring strProto = _T("");
	tstring strStatus = _T("");
	if(szProto != NULL)
	{
		strProto = _T("(") + toTstring(szProto) + _T(")");
		m_iStatus = db_get_w(m_hContact,szProto,"Status",ID_STATUS_OFFLINE);
	}
	
	szStatus = (char *) CallService(MS_CLIST_GETSTATUSMODEDESCRIPTION, m_iStatus, 0);
	if(szStatus != NULL)
		strStatus = toTstring(szStatus);

	m_UserName.SetText(strNickname.c_str());
	m_UserStatus.SetText(strStatus.c_str());

	if(m_bContactTyping && CConfig::GetBoolSetting(SESSION_SHOWTYPING))
		m_UserProto.SetText(CAppletManager::TranslateString(_T("typing..")));
	else
		m_UserProto.SetText(strProto.c_str());
}

// returns wether the input mode is active
bool CChatScreen::IsInputActive()
{
	if(m_eReplyState != REPLY_STATE_NONE)
		return true;
	else
		return false;
}

//************************************************************************
// returns the chat contact
//************************************************************************
HANDLE CChatScreen::GetContact()
{
	return m_hContact;
}

//************************************************************************
// sets the screen's chat contact
//************************************************************************
bool CChatScreen::SetContact(HCONTACT hContact)
{
	if(hContact == NULL)
	{
		m_hContact = NULL;
		return true;
	}
	
	// Check if the contact is valid
	char *szProto = (char *) CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
	m_strProto = toTstring(szProto);

	CIRCConnection *pIRCCon = CAppletManager::GetInstance()->GetIRCConnection(m_strProto);
	if(pIRCCon)
		m_bIRCProtocol = true;
	else
		m_bIRCProtocol = false;
			
			
	if(!szProto)
		return false;

	if(m_eReplyState != REPLY_STATE_NONE)
		DeactivateMessageMode();
	else if(IsMaximized())
		Minimize();

	m_bContactTyping = false;
	m_hContact = hContact;
	m_TextLog.ClearLog();
	UpdateLabels();

	LoadHistory();

	return true;
}

//************************************************************************
// loads the contacts history
//************************************************************************
void CChatScreen::LoadHistory()
{
	if(!m_hContact)
		return;

	char *szProto = (char *) CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)m_hContact, 0);
	
	if(m_bIRCProtocol && db_get_b(m_hContact, szProto, "ChatRoom", 0) != 0)
	{
		if(!CAppletManager::GetInstance()->IsIRCHookEnabled())
		{
			time_t now;
			tm tm_now;
			time(&now);
			localtime_s(&tm_now,&now);

			AddIncomingMessage(CAppletManager::TranslateString(_T("IRC-Chatroom support is disabled!\nYou need to install the patched IRC.dll (see the readme) to use IRC-Chatrooms on the LCD")),&tm_now,true);
		}
		else
		{
			CIRCHistory *pHistory = CAppletManager::GetInstance()->GetIRCHistory(m_hContact);
			if(pHistory)
			{
				list<SIRCMessage>::iterator iter = pHistory->LMessages.begin();
				while(iter != pHistory->LMessages.end())
				{
					if((*iter).bIsMe)
						AddOutgoingMessage((*iter).strMessage,&((*iter).Time),true);
					else
						AddIncomingMessage((*iter).strMessage,&((*iter).Time),true);
					iter++;
				}
			}
		}
	}
	else
	{
		// Get last events from database
		CEvent Event;
		list<HANDLE> LHandles;
		HANDLE hEvent = db_event_last(m_hContact);
		HANDLE hUnread = db_event_firstUnread(m_hContact);
		
		if(CConfig::GetBoolSetting(SESSION_LOADDB))
		{
			while(hEvent != NULL && hUnread != NULL)
			{
				LHandles.push_front(hEvent);
				if(CConfig::GetBoolSetting(SESSION_LOADDB) && *(LHandles.begin()) == hUnread)
					break;
				hEvent = db_event_prev(hEvent);
			}
		}
		else
		{
			for (int i = CConfig::GetIntSetting(SESSION_LOGSIZE); i > 0 && hEvent!=NULL; i--)
			{
					LHandles.push_front(hEvent);
					hEvent = db_event_prev(hEvent);
			}
		}
		
		bool bRead = true;
		while(!(LHandles.empty()))
		{
			if(CAppletManager::TranslateDBEvent(&Event,(LPARAM)m_hContact,(WPARAM)*(LHandles.begin())))
			{
				if(Event.hValue == hUnread)
					bRead = false;
				if(Event.eType == EVENT_MSG_RECEIVED)
				{
					AddIncomingMessage(Event.strValue,&Event.Time);
					if(!bRead && CConfig::GetBoolSetting(SESSION_MARKREAD) && !CAppletManager::IsMessageWindowOpen(m_hContact))
						CAppletManager::MarkMessageAsRead(m_hContact,Event.hValue);
				}
				else
					AddOutgoingMessage(Event.strValue,&Event.Time);

			}		
			LHandles.pop_front();
		}
	}
}

//************************************************************************
// Updates the screen
//************************************************************************
bool CChatScreen::Update()
{
	if(!CScreen::Update())
		return false;
	
	if(CConfig::GetBoolSetting(SESSION_CLOSE)) {
		if(!CAppletManager::GetInstance()->GetLCDConnection()->IsForeground()) {
			if(!m_bCloseTimer && CConfig::GetIntSetting(SESSION_CLOSETIMER) != 0) {
				m_bCloseTimer = true;
				m_dwCloseTimer = GetTickCount();
			} else if(CConfig::GetIntSetting(SESSION_CLOSETIMER) == 0 || GetTickCount() - m_dwCloseTimer >= CConfig::GetIntSetting(SESSION_CLOSETIMER)) {
				m_bCloseTimer = false;
				CAppletManager::GetInstance()->ActivateEventScreen();
				return true;
			}
		}
	}

	if(m_bMaximizedTimer && m_dwMaximizedDuration != INFINITE)
	{
		if(m_dwMaximizedTimer + m_dwMaximizedDuration <= GetTickCount())
			Minimize();
	}

	if(m_eReplyState == REPLY_STATE_INPUT && !m_Input.IsInputActive())
		SendCurrentMessage();
	
	// Handle Typing notifications
	if(IsInputActive())
	{
		if(m_Input.GetLastInputTime() + 10000 <= GetTickCount())
		{
			if(m_bTyping)
			{
				m_bTyping = false;
				CAppletManager::GetInstance()->SendTypingNotification(m_hContact,0);
			}
		}
		else if(CConfig::GetBoolSetting(SESSION_SENDTYPING) && !m_bTyping)
		{
			m_bTyping = true;
			CAppletManager::GetInstance()->SendTypingNotification(m_hContact,1);
		}
	}
	else if(m_bTyping)
	{
		m_bTyping = false;
		CAppletManager::GetInstance()->SendTypingNotification(m_hContact,0);
	}

	return true;
}

//************************************************************************
// Draws the screen
//************************************************************************
bool CChatScreen::Draw(CLCDGfx *pGfx)
{
	if(!CScreen::Draw(pGfx))
		return false;

	if(!m_bHideTitle)
	{
		int iTitleHeight = CConfig::GetFontHeight(FONT_TITLE)+1;
		pGfx->DrawLine(0,iTitleHeight<6?6:iTitleHeight,GetWidth(),iTitleHeight<6?6:iTitleHeight);
		int iOffset = (iTitleHeight-5)/2;
		pGfx->DrawBitmap(1,iOffset,5,5,CAppletManager::GetInstance()->GetStatusBitmap(m_iStatus));
	}

	return true;
}

//************************************************************************
// Adds an outgoing message to the log
//************************************************************************
void CChatScreen::AddOutgoingMessage(tstring strMessage,tm *time,bool bIRC)
{
	tstring strPrefix = bIRC?_T(""):_T(">> ");
	if(CConfig::GetBoolSetting(SESSION_TIMESTAMPS))
		strPrefix += CAppletManager::GetFormattedTimestamp(time) + _T(" ");

	// adjust the scroll mode
	m_TextLog.SetAutoscrollMode(SCROLL_LINE);

	// add the message
	m_TextLog.AddText(strPrefix + strMessage,true);

}

//************************************************************************
// Adds an incoming message to the log
//************************************************************************
void CChatScreen::AddIncomingMessage(tstring strMessage,tm *time,bool bIRC)
{
	tstring strPrefix = bIRC?_T(""):_T("<< ");
	if(CConfig::GetBoolSetting(SESSION_TIMESTAMPS))
		strPrefix += CAppletManager::GetFormattedTimestamp(time) + _T(" ");

	
	// adjust the scroll mode
	EScrollMode eMode;
	switch(CConfig::GetIntSetting(SESSION_AUTOSCROLL))
	{
	case SESSION_AUTOSCROLL_NONE: eMode = SCROLL_NONE; break;
	case SESSION_AUTOSCROLL_FIRST: eMode = SCROLL_MESSAGE; break;
	case SESSION_AUTOSCROLL_LAST: eMode = SCROLL_LINE; break;
	}
	m_TextLog.SetAutoscrollMode(eMode);

	// add the message
	m_TextLog.AddText(strPrefix + strMessage);
}

//************************************************************************
// activates the input mode
//************************************************************************
void CChatScreen::ActivateMessageMode()
{
	m_InfoText.Show(0);
	m_TextLog.Show(0);
	m_TextLog.SetScrollbar(NULL);
	m_Input.SetScrollbar(&m_Scrollbar);

	if(m_eReplyState != REPLY_STATE_FAILED)
		m_Input.Reset();

	m_Input.Show(1);
	m_Input.ActivateInput();

	m_eReplyState = REPLY_STATE_INPUT;

	SetButtonBitmap(2,IDB_BACK);
	SetButtonBitmap(3,IDB_SEND);	

	if(CConfig::GetBoolSetting(SESSION_REPLY_MAXIMIZED))
		Maximize();
	else
		Minimize();
}

//************************************************************************
// sends the message
//************************************************************************
void CChatScreen::SendCurrentMessage()
{
	if(m_Input.GetText().empty())
	{
		DeactivateMessageMode();
		return;
	}
	ASSERT(m_eReplyState == REPLY_STATE_INPUT);

	m_eReplyState = REPLY_STATE_SENDING;

	m_Input.DeactivateInput();

	m_InfoText.SetText(CAppletManager::TranslateString(_T("Sending message...")));
	m_InfoText.Show(1);
	m_Input.Show(0);
	
	m_hMessage = CAppletManager::SendMessageToContact(m_hContact,m_Input.GetText());
	if(m_hMessage == NULL)
	{
		DeactivateMessageMode();
		return;
	}
	SetButtonBitmap(2,NULL);
	SetButtonBitmap(3,NULL);
}

//************************************************************************
// invalidates the message mode
//************************************************************************
void CChatScreen::InvalidateMessageMode(tstring strError)
{
	m_eReplyState = REPLY_STATE_FAILED;

	m_InfoText.SetText(strError);

	SetButtonBitmap(2,IDB_BACK);
	SetButtonBitmap(3,IDB_SEND);	

	if(IsMaximized())
		Minimize();
}

//************************************************************************
// deactivates the input mode
//************************************************************************
void CChatScreen::DeactivateMessageMode()
{
	m_Input.Reset();

	m_Input.SetScrollbar(NULL);
	m_TextLog.SetScrollbar(&m_Scrollbar);

	m_TextLog.Show(1);
	m_InfoText.Show(0);
	m_Input.Show(0);
	
	m_Input.DeactivateInput();

	m_eReplyState = REPLY_STATE_NONE;
	
	SetButtonBitmap(2,IDB_HISTORY);
	SetButtonBitmap(3,IDB_REPLY);

	if(IsMaximized())
		Minimize();
}

//************************************************************************
// maximizes the content object
//************************************************************************
void CChatScreen::Maximize(DWORD dwTimer)
{
	m_bMaximizedTimer = true;
	m_dwMaximizedTimer = GetTickCount();
	m_dwMaximizedDuration = dwTimer;
	UpdateObjects();
}

//************************************************************************
// minimizes the content object
//************************************************************************
void CChatScreen::Minimize()
{
	m_bMaximizedTimer = false;
	m_dwMaximizedTimer = 0;
	UpdateObjects();
}

//************************************************************************
// returns wether the content is maximized
//************************************************************************
bool CChatScreen::IsMaximized()
{
	return m_bMaximizedTimer;
}

//************************************************************************
// Called when the screen size has changed
//************************************************************************
void CChatScreen::OnSizeChanged()
{
	CScreen::OnSizeChanged();
	UpdateObjects();
}

//************************************************************************
// Called when the applet's configuration has changed
//************************************************************************
void CChatScreen::OnConfigChanged()
{
	CScreen::OnConfigChanged();

	UpdateObjects();

	m_TextLog.ClearLog();
	LoadHistory();
}

//************************************************************************
// Called when an event is received
//************************************************************************
void CChatScreen::OnEventReceived(CEvent *pEvent)
{
	// only let events for this contact pass
	if(pEvent->hContact != m_hContact &&
		// expect for IRC events without a contact -> global notifications
		!((pEvent->eType == EVENT_IRC_SENT || pEvent->eType == EVENT_IRC_RECEIVED) && pEvent->hContact == NULL))
		return;
	
	switch(pEvent->eType)
	{
	case EVENT_MESSAGE_ACK:
		if(pEvent->hValue != m_hMessage)
			return;

		if(pEvent->iValue == ACKRESULT_SUCCESS)
			DeactivateMessageMode();
		else
			InvalidateMessageMode(pEvent->strValue.empty()?CAppletManager::TranslateString(_T("Could not send the message!")):pEvent->strValue);
		break;
	case EVENT_IRC_SENT:
		// Add the message to the log
		AddOutgoingMessage(pEvent->strValue,&pEvent->Time,true);
		break;
	case EVENT_IRC_RECEIVED:
		// Add the message to the log
		AddIncomingMessage(pEvent->strValue,&pEvent->Time,true);
		break;
	case EVENT_MSG_RECEIVED:
		// mark it as read if required
		if(CConfig::GetBoolSetting(SESSION_MARKREAD) && !CAppletManager::IsMessageWindowOpen(m_hContact))
			CAppletManager::MarkMessageAsRead(m_hContact,pEvent->hValue);
		// Add the message to the log
		AddIncomingMessage(pEvent->strValue,&pEvent->Time);
		break;
	case EVENT_MSG_SENT:
		// Add the message to the log
		AddOutgoingMessage(pEvent->strValue,&pEvent->Time);
		break;
	case EVENT_CONTACT_HIDDEN:
		// contact is set to hidden
		if(pEvent->iValue == 1)
		{
			// Close the chat screen if the contact is an irc chatroom
			if(!(m_bIRCProtocol && db_get_b(pEvent->hContact, toNarrowString(m_strProto).c_str(), "ChatRoom", 0) != 0))
				break;
		}
		else
			break;
	case EVENT_CONTACT_DELETED:
		CAppletManager::GetInstance()->ActivateEventScreen();
		break;
	case EVENT_TYPING_NOTIFICATION:
		m_bContactTyping = pEvent->iValue != 0;
		UpdateLabels();
		break;
	case EVENT_STATUS:
	case EVENT_CONTACT_NICK:
	case EVENT_SIGNED_OFF:
	case EVENT_SIGNED_ON:
		UpdateLabels();
		break;
	}
}

//************************************************************************
// Called when an LCD-button is pressed
//************************************************************************
void CChatScreen::OnLCDButtonDown(int iButton)
{
	switch(m_eReplyState)
	{
	case REPLY_STATE_NONE:
		if(iButton == LGLCDBUTTON_CANCEL) {
			CAppletManager::GetInstance()->ActivatePreviousScreen();
		} else if(iButton == LGLCDBUTTON_BUTTON2 || iButton == LGLCDBUTTON_MENU)
			CAppletManager::GetInstance()->ActivateEventScreen();
		// enter reply mode
		else if(iButton == LGLCDBUTTON_BUTTON3 || iButton == LGLCDBUTTON_OK)
			ActivateMessageMode();
		else {
			bool bRes = false;
			if(iButton == LGLCDBUTTON_BUTTON0 || iButton == LGLCDBUTTON_UP) {
				bRes = m_TextLog.ScrollUp();
			} else if(iButton == LGLCDBUTTON_BUTTON1 || iButton == LGLCDBUTTON_DOWN) {
				bRes = m_TextLog.ScrollDown();
			}

			if(bRes && CConfig::GetBoolSetting(SESSION_SCROLL_MAXIMIZED)) {
				Maximize(5000);
			}
		}
		break;
	case REPLY_STATE_FAILED:
		if(iButton == LGLCDBUTTON_BUTTON2 || iButton == LGLCDBUTTON_CANCEL) {
			DeactivateMessageMode();
		} else if(iButton == LGLCDBUTTON_BUTTON3 || iButton == LGLCDBUTTON_OK) {
			ActivateMessageMode();
		} else if(iButton == LGLCDBUTTON_CANCEL) {
			DeactivateMessageMode();
			CAppletManager::GetInstance()->ActivatePreviousScreen();
		} else if(iButton == LGLCDBUTTON_MENU) {
			DeactivateMessageMode();
			CAppletManager::GetInstance()->ActivateEventScreen();
		}
		break;
	case REPLY_STATE_SENDING:
		break;
	case REPLY_STATE_INPUT:
		if(iButton == LGLCDBUTTON_BUTTON0 || iButton == LGLCDBUTTON_UP) {
			m_Input.ScrollLine(0);
		} else if(iButton == LGLCDBUTTON_BUTTON1 || iButton == LGLCDBUTTON_DOWN) {
			m_Input.ScrollLine(1);
		// send the message
		} else if(iButton == LGLCDBUTTON_BUTTON3 || iButton == LGLCDBUTTON_OK) {
			SendCurrentMessage();
		// cancel message mode
		} else if(iButton == LGLCDBUTTON_BUTTON2 || iButton == LGLCDBUTTON_CANCEL) {
			DeactivateMessageMode();
		}
		break;
	}
}

//************************************************************************
// Called when an LCD-button event is repeated
//************************************************************************
void CChatScreen::OnLCDButtonRepeated(int iButton)
{
	switch(m_eReplyState)
	{
	case REPLY_STATE_NONE:
		if(iButton < 2)
		{
			bool bRes = false;
			if(iButton == LGLCDBUTTON_BUTTON0 || iButton == LGLCDBUTTON_UP) {
				bRes = m_TextLog.ScrollUp();
			} else if(iButton == LGLCDBUTTON_BUTTON1 || iButton == LGLCDBUTTON_DOWN) {
				bRes = m_TextLog.ScrollDown();
			}

			if(bRes && CConfig::GetBoolSetting(SESSION_SCROLL_MAXIMIZED))
				Maximize(5000);
		}
		break;
	case REPLY_STATE_INPUT:
		if(iButton == LGLCDBUTTON_BUTTON0 || iButton == LGLCDBUTTON_UP) {
			m_Input.ScrollLine(0);
		} else if(iButton == LGLCDBUTTON_BUTTON1 || iButton == LGLCDBUTTON_DOWN) {
			m_Input.ScrollLine(1);
		}
	}
}

//************************************************************************
// Called when an LCD-button is released
//************************************************************************
void CChatScreen::OnLCDButtonUp(int iButton)
{
	
}

//************************************************************************
// Called when the screen is activated
//************************************************************************
void CChatScreen::OnActivation()
{
	m_bCloseTimer = false;
}

//************************************************************************
// Called when the screen is deactivated
//************************************************************************
void CChatScreen::OnDeactivation()
{
}	

//************************************************************************
// Called when the screen has expired
//************************************************************************
void CChatScreen::OnExpiration()
{
}