📄 serverui.cpp
字号:
//SERVERUI.CPP
#include "serverUi.h"
#include <fbs.h>
#include <gdi.h>
#include <w32std.h>
const TInt KScreenWidth = 160;
const TInt KScreenHeight = 144;
const TInt KMaxUiLines = 8;
const TInt KEntrySize = 18;
const TInt KVertBorderSize = 2;
const TInt KCBASize = 20;
const TInt KCbaHOffset = 2;
const TInt KCbaVOffset = KCBASize - 4;
const TInt KTextVOffset = KEntrySize - 3;
const TInt KTextHOffset = 6;
#define KRGB(r,g,b) ((b) + ((g) << 4) + ((r) << 8))
const TInt KBackgroundColor = KRGB(5,5,10);
const TInt KSelectedColor = KRGB(8,8,10);
const TInt KTitleColor = KRGB(12,12,14);
const TInt KCbaBackgroundColor = KRGB(10,10,10);
CUIEntryList::CUIEntryList()
:CArrayFixFlat<TUIEntry>(16)
{
}
CUIEntryList::~CUIEntryList()
{
}
void CUIEntryList::AddTitleL(const TDesC& aText)
{
TUIEntry entry;
entry.iType = EUITitle;
entry.iText = aText;
AppendL(entry);
}
void CUIEntryList::AddListItemL(const TDesC& aText)
{
TUIEntry entry;
entry.iType = EUILabel;
entry.iText = aText;
AppendL(entry);
}
CServerUi::CServerUi()
{
}
CServerUi::~CServerUi()
{
delete iEntryList;
delete iCbaBitGc;
delete iCbaBitDevice;
delete iBitGc;
delete iBitDevice;
}
void CServerUi::ConstructL(CWsScreenDevice* aDevice, CFbsBitmap* aScreen, CFbsBitmap* aCba, MServerUiObserver* aObserver)
{
iDevice = aDevice;
iObserver = aObserver;
iCba = aCba;
iBitDevice = CFbsBitmapDevice::NewL(aScreen);
iBitGc = CFbsBitGc::NewL();
iCbaBitDevice = CFbsBitmapDevice::NewL(aCba);
iCbaBitGc = CFbsBitGc::NewL();
TFontSpec fontSpec(_L("LatinBold13"), 13);
iDevice->GetNearestFontInTwips(iFont, fontSpec);
iBitGc->Activate(iBitDevice);
iBitGc->UseFont(iFont);
iCbaBitGc->Activate(iCbaBitDevice);
iCbaBitGc->UseFont(iFont);
iEntryList = new(ELeave)CUIEntryList();
}
void CServerUi::Draw(CFbsBitmap* aScreen)
{
TBitmapUtil screenUtil = TBitmapUtil(aScreen);
screenUtil.Begin(TPoint(0,0));
TUint16* ptr = (TUint16*)(aScreen->DataAddress());
DrawEntries(ptr, aScreen);
screenUtil.End();
DrawText();
}
void CServerUi::DrawEntries(TUint16* aPtr, CFbsBitmap* aScreen)
{
TInt count = iEntryList->Count();
if (count > KMaxUiLines)
{
count = KMaxUiLines;
}
TInt size = (count * KEntrySize) + KVertBorderSize;
TInt top = KScreenHeight - size;
TUint16* borderTop = aPtr + (top * KScreenWidth);
TUint16* borderBottom = aPtr + ((KScreenHeight-1) * KScreenWidth);
TUint16* borderLeft = borderTop;
TUint16* borderRight = borderTop + (KScreenWidth-1);
top++;
for (TInt ii=0; ii<count; ii++)
{
TUint16* ptr = aPtr + (top * KScreenWidth);
TInt item = ii;
if (iEntryList->At(item).iType != EUITitle)
{
item += iListOffset;
}
if (iControlType == ENote)
{
iSelected = item;
}
DrawEntry(iEntryList->At(item), ptr, iSelected == item, iEntryList->At(item).iType == EUITitle);
top += KEntrySize;
}
// Draw black border
TInt len = KScreenWidth;
while (len)
{
*borderTop = KRGB(0,0,0);
*borderBottom = KRGB(0,0,0);
borderTop++;
borderBottom++;
len--;
}
len = size;
while (len)
{
*borderLeft = KRGB(0,0,0);
*borderRight = KRGB(0,0,0);
borderLeft += KScreenWidth;
borderRight += KScreenWidth;
len--;
}
}
void CServerUi::DrawText()
{
TInt count = iEntryList->Count();
if (count > KMaxUiLines)
{
count = KMaxUiLines;
}
TInt size = (count * KEntrySize) + KVertBorderSize;
TInt top = KScreenHeight - size;
for (TInt ii=0; ii<count; ii++)
{
TInt item = ii;
if (iEntryList->At(item).iType != EUITitle)
{
item += iListOffset;
}
const TDesC& text = iEntryList->At(item).iText;
iBitGc->DrawText(text, TPoint(KTextHOffset,top+KTextVOffset));
top += KEntrySize;
}
// Draw CBA labels
iCbaBitGc->SetBrushColor(TRgb(0,0,0));
iCbaBitGc->SetPenColor(TRgb(255,255,0));
iCbaBitGc->Clear();
iCbaBitGc->DrawText(iLabelText1, TPoint(iLabelPos1, KCbaVOffset));
iCbaBitGc->DrawText(iLabelText2, TPoint(iLabelPos2, KCbaVOffset));
}
void CServerUi::DrawEntry(TUIEntry& aEntry, TUint16* aPtr, TBool aSelected, TBool aTitle)
{
// Draw the entry background
TInt len = (KScreenWidth* KEntrySize);
TUint16* ptr = aPtr;
if (aTitle)
{
while (len)
{
*ptr = KTitleColor;
ptr++;
len--;
}
}
else if (aSelected)
{
while (len)
{
*ptr = KSelectedColor;
ptr++;
len--;
}
}
else
{
while (len)
{
*ptr = KBackgroundColor;
ptr++;
len--;
}
}
if (aEntry.iType == EUITitle)
{
// Draw additional horizonal line
TInt len = KScreenWidth;
TUint16* ptr = aPtr + (KScreenWidth * (KEntrySize-1));
while (len)
{
*ptr = KRGB(0,0,0);
ptr++;
len--;
}
}
}
void CServerUi::FindInitialSelection()
{
iSelected = -1;
FindNextSelection();
}
void CServerUi::KeyEvent(TInt aScanCode)
{
if (iControlType == EKeySelectQuery)
{
Activate(EFalse);
iObserver->KeySelect(iId, aScanCode);
}
if (aScanCode == EStdKeyDownArrow)
{
FindNextSelection();
}
if (aScanCode == EStdKeyUpArrow)
{
FindPreviousSelection();
}
if (aScanCode == EStdKeyDevice3)
{
if (iControlType == EList) // OK Key
{
Activate(EFalse);
iObserver->ListItemSelection(iId, iSelected-1);
}
else if (iControlType == EQuery)
{
CbaPressed(iLabel1);
}
else if (iControlType == ENote)
{
CbaPressed(iLabel2);
}
}
if (aScanCode == EStdKeyDevice0)
{
CbaPressed(iLabel1);
}
if (aScanCode == EStdKeyDevice1)
{
CbaPressed(iLabel2);
}
}
void CServerUi::FindPreviousSelection()
{
for (TInt ii=iSelected-1; ii>= 0; ii--)
{
if (iEntryList->At(ii).iType == EUILabel)
{
iSelected = ii;
if (iSelected <= iListOffset)
{
iListOffset = iSelected-1;
}
return;
}
}
}
void CServerUi::FindNextSelection()
{
TInt count = iEntryList->Count();
for (TInt ii=iSelected+1; ii< count; ii++)
{
if (iEntryList->At(ii).iType == EUILabel)
{
iSelected = ii;
if (iSelected > iListOffset+KMaxUiLines-1)
{
iListOffset++;
}
return;
}
}
}
void CServerUi::CreateList(TInt aId, const TDesC& aTitle)
{
iId = aId;
iControlType = EList;
iListOffset = 0;
iEntryList->Reset();
iEntryList->AddTitleL(aTitle);
SetCba(ECbaOk, ECbaCancel);
}
void CServerUi::AddListItem(const TDesC& aItem)
{
iEntryList->AddListItemL(aItem);
}
void CServerUi::CreateQueryL(TInt aId, const TDesC& aQuery)
{
CreateQueryL(aId, _L("Question:"), aQuery);
}
void CServerUi::CreateQueryL(TInt aId, const TDesC& aTitle, const TDesC& aQuery)
{
iId = aId;
iControlType = EQuery;
iEntryList->Reset();
iEntryList->AddTitleL(aTitle);
iEntryList->AddListItemL(aQuery);
SetCba(ECbaYes, ECbaNo);
}
void CServerUi::CreateKeySelectQueryL(TInt aId, const TDesC& aTitle, const TDesC& aQuery)
{
iId = aId;
iControlType = EKeySelectQuery;
iEntryList->Reset();
iEntryList->AddTitleL(aTitle);
iEntryList->AddListItemL(aQuery);
SetCba(ECbaEmpty, ECbaEmpty);
}
void CServerUi::CreateNoteL(const TDesC& aTitle, const TDesC& aMessage)
{
iControlType = ENote;
iEntryList->Reset();
if (aTitle.Length())
{
iEntryList->AddTitleL(aTitle);
}
else
{
iEntryList->AddTitleL(_L("Information"));
}
iEntryList->AddListItemL(aMessage);
SetCba(ECbaEmpty, ECbaOk);
}
void CServerUi::Activate(TBool aActivate)
{
if (aActivate)
{
FindInitialSelection();
}
else
{
iObserver->Redraw(EFalse);
}
iIsActive = aActivate;
}
void CServerUi::SetCba(TCbaLabel aLabel1, TCbaLabel aLabel2)
{
iLabel1 = aLabel1;
iLabel2 = aLabel2;
SetTextForLabel(iLabelText1, aLabel1);
SetTextForLabel(iLabelText2, aLabel2);
iLabelPos1 = KCbaHOffset;
iLabelPos2 = (176-KCbaHOffset)-iFont->TextWidthInPixels(iLabelText2);
}
void CServerUi::SetTextForLabel(TDes& aLabel, TCbaLabel aLabelId)
{
switch (aLabelId)
{
case ECbaEmpty:
aLabel.Copy(KNullDesC());
break;
case ECbaOk:
aLabel.Copy(_L("Ok"));
break;
case ECbaCancel:
aLabel.Copy(_L("Cancel"));
break;
case ECbaYes:
aLabel.Copy(_L("Yes"));
break;
case ECbaNo:
aLabel.Copy(_L("No"));
break;
}
}
void CServerUi::CbaPressed(TCbaLabel aLabel)
{
if (iControlType == EList)
{
Activate(EFalse);
if (aLabel == ECbaOk)
{
iObserver->ListItemSelection(iId, iSelected-1);
}
else
{
iObserver->ListItemSelection(iId, -1);
}
}
else if (iControlType == EQuery)
{
Activate(EFalse);
iObserver->CbaPressed(iId, aLabel);
}
else if (iControlType == ENote)
{
Activate(EFalse);
iObserver->CbaPressed(-1, aLabel);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -