Hi,
As an alternative to CEikLabel which has always caused much trouble for me, as explained here, I decided to draw text using the DrawText() function.
The problem I am having with DrawText is: I have two texts which I intend to draw on the SAME line (i.e. the same Y position), one at 5 pixels from the left margin and the other would be 5 pixels from the right. However, I can only draw either of them, not both.
The calling function which calls the Draw() method is:Code:void CPengawas_Logs::Draw( const TRect& aRect ) const { CWindowGc& gc = SystemGc(); gc.Clear(); gc.SetBrushColor(KRgbDarkBlue); gc.SetBrushStyle(CGraphicsContext::ESolidBrush); gc.SetPenColor(KRgbYellow); TRect rect(0, 0, aRect.Width(), 20); gc.DrawRect(rect); _LIT(KFontName, "LatinPlain12"); const TInt KFontSize = 130; TFontSpec fontSpec(KFontName, KFontSize); fontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold); CGraphicsDevice* screenDevice = iEikonEnv->ScreenDevice(); CFont* font; screenDevice->GetNearestFontInTwips(font, fontSpec); gc.UseFont(font); screenDevice->ReleaseFont(font); const TInt KOffset = 5; TInt iItemNo_Width = font->TextWidthInPixels(iItemNoText); TInt iYPos = (rect.Height() + font->AscentInPixels()) / 2; gc.DrawText(iDateTimeText, rect, iYPos, CGraphicsContext::ELeft, KOffset); //"Date and Time" text gc.DrawText(iItemNoText, rect, iYPos, CGraphicsContext::ELeft, rect.Width()-KOffset-iItemNo_Width); //"Item No" text in the format of "1/1000" (lots of items as this lists SMS/Call logs) gc.DiscardFont(); }
Only one of the two strings was printed, either the first or the second, but not both, depending on which one was not commented out.Code:void CPengawas_Logs::DisplayDateTimeAndItemNo(const TInt iItemCount) { TBuf<20> iDateTimeParts = iListBox->Model()->ItemText(iActiveIndex).Right(20); iDateTimeText = iDateTimeParts.Left(19); iItemNoText = _L(""); iItemNoText.AppendNum(iActiveIndex+1); iItemNoText.Append(_L("/")); iItemNoText.AppendNum(iItemCount); DrawNow(); }
Then I "improvised" by using a series of space characters to separate the first and second text.
This one worked.Code:void CPengawas_Logs::Draw( const TRect& aRect ) const { CWindowGc& gc = SystemGc(); gc.Clear(); gc.SetBrushColor(KRgbDarkBlue); gc.SetBrushStyle(CGraphicsContext::ESolidBrush); gc.SetPenColor(KRgbYellow); TRect rect(0, 0, aRect.Width(), 20); gc.DrawRect(rect); _LIT(KFontName, "LatinPlain12"); const TInt KFontSize = 130; TFontSpec fontSpec(KFontName, KFontSize); fontSpec.iFontStyle.SetStrokeWeight(EStrokeWeightBold); CGraphicsDevice* screenDevice = iEikonEnv->ScreenDevice(); CFont* font; screenDevice->GetNearestFontInTwips(font, fontSpec); gc.UseFont(font); screenDevice->ReleaseFont(font); _LIT(KSpace, " "); const TInt KOffset = 5; TInt iDateTime_Width = font->TextWidthInPixels(iDateTimeText); TInt iItemNo_Width = font->TextWidthInPixels(iItemNoText); TInt iSpace_Width = font->TextWidthInPixels(KSpace); TInt iAllTexts_Width = rect.Width()- (2*KOffset); TInt iRemainingWidth = iAllTexts_Width - (iDateTime_Width + iItemNo_Width); TInt iSpaceCount = 0; if (iRemainingWidth > 0) { iSpaceCount = (iRemainingWidth/iSpace_Width); if (iSpaceCount < 0) iSpaceCount = 0; } TInt iDateTimeText_Width = iDateTimeText.Length(); TInt iItemNo_Width = iItemNoText.Length(); RBuf iInformationText; iInformationText.CreateL(iDateTimeText_Width+iSpaceCount+iItemNo_Width); CleanupClosePushL(iInformationText); iInformationText.Copy(iDateTimeText); if (iSpaceCount > 0) for (TInt i=0; i<iSpaceCount; i++) iInformationText.Append(KSpace); iInformationText.Append(iItemNoText); iAppUi->SaveToLog(KLogFile, iInformationText); TInt iYPos = (rect.Height() + font->AscentInPixels()) / 2; //gc.DrawText(iDateTimeText, rect, iYPos, CGraphicsContext::ELeft, KOffset); //gc.DrawText(iItemNoText, rect, iYPos, CGraphicsContext::ELeft, rect.Width()-KOffset-iItemNo_Width); gc.DrawText(iInformationText, rect, iYPos, CGraphicsContext::ELeft, KOffset); CleanupStack::PopAndDestroy(&iInformationText); gc.DiscardFont(); }
My question: Does DrawText print only one "text" on a single line? I haven't tried drawing more than one line, though, but I will for another container soon after I find the method used for this one sufficient.
Thanks in advance.
P.S. The above variable names are converted/translated to English for the ease of understanding. There might be some words incorrectly replaced using Find/Replace in Notepad.
P.P.S. I've googled a lot and browsed the forum/DiBo and all that (as I always do everyday since I began writing this app) but have found no similar problem (i.e. drawing "twice" on the same line). Others mention drawing problems with multiline texts, which I will have to take into account when I am at the next container real soon.
Regards,
Asep

Reply With Quote

