×

Discussion Board

Results 1 to 9 of 9
  1. #1
    Registered User
    Join Date
    Sep 2012
    Location
    Bandung, Indonesia
    Posts
    27

    DrawText Problem - Texts on Same Line

    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.
    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();	
    	}
    The calling function which calls the Draw() method is:
    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();
    	}
    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.
    Then I "improvised" by using a series of space characters to separate the first and second text.
    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();	
    	}
    This one worked.

    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
    Last edited by asepm; 2013-02-07 at 10:00.

  2. #2
    Registered User
    Join Date
    Sep 2012
    Location
    Bandung, Indonesia
    Posts
    27

    Re: DrawText Problem - Texts on Same Line

    Unfortunately, using DrawText() caused my app to freeze and my device to reboot as well, just like using CEikLabel. Anything wrong with the above code? Please do let me know, anyone. I'm almost at my wit's end now.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Location
    Bandung, Indonesia
    Posts
    27

    Re: DrawText Problem - Texts on Same Line

    Oh my, ReleaseFont was incorrectly pasted in a wrong place, due to a hurry Will edit the code, try installing the new sis file and see what happens next.
    EDITED: ReleaseFont was placed in the right section, I think, but still no luck.

    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);
    
    	_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);
            screenDevice->ReleaseFont(font);
    	gc.DiscardFont();	
    	}
    I do appreciate your kind thoughts on this error.
    Last edited by asepm; 2013-02-07 at 10:26.

  4. #4
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,672

    Re: DrawText Problem - Texts on Same Line

    Note that
    Quote Originally Posted by documentation
    The surrounding box is filled with the current brush colour (not a pattern) and is drawn without any outline.
    Your default brush is opaque white, consider setting KNullBrush or whatever its name.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Location
    Bandung, Indonesia
    Posts
    27

    Re: DrawText Problem - Texts on Same Line

    Wizard, thanks.
    I will replace the SetBrushStyle part now.

    On Emulator, no such error, though.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Location
    Bandung, Indonesia
    Posts
    27

    Re: DrawText Problem - Texts on Same Line

    Quote Originally Posted by wizard_hu_ View Post
    Note thatYour default brush is opaque white, consider setting KNullBrush or whatever its name.
    Still no success. The phone rebooted over and over again. Maybe because this is a Nokia E71 (more than 4 years old) or what? But other apps I use on my device on a daily basis (including Y-Tasks and Y-Browser) seem to be working just fine.

  7. #7
    Nokia Developer Champion
    Join Date
    Jun 2008
    Location
    Noida,India
    Posts
    3,987

    Re: DrawText Problem - Texts on Same Line

    I think the rebooting reason is somewhere else, putting/removing label or drawing a simple text has nothing to do with it.You should consider re-checking the architecture of your app. I am sure putting these labels or drawing text in a fresh helloworld project would not reboot the device.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Location
    Bandung, Indonesia
    Posts
    27

    Re: DrawText Problem - Texts on Same Line

    Yeah, I think so, Vineet. It's most likely that since I have a total of 10 AOs, the app has been suffering from this particular error. I actually believed earlier that this could've been related to the "SizeChanged()" or "Draw()" functions, which I will have to cross check.

  9. #9
    Registered User
    Join Date
    Sep 2012
    Location
    Bandung, Indonesia
    Posts
    27

    Re: DrawText Problem - Texts on Same Line

    At last I think I have to admit that the cause of incessant reboots of my device is due to nothing but the long process in the Draw() method that I used. As Symbian's Creating Custom Controls workbook explains, the Draw() function should be made as fast as possible. That also means that the use of "custom fonts" as I used earlier is a bad idea and strongly discouraged. This is for anybody who might experience an error similar to what I've encountered for the last couple of days.

Similar Threads

  1. How to change line spacing in QPainter::drawText
    By ziggx in forum [Archived] Beginning Qt
    Replies: 2
    Last Post: 2011-01-30, 17:50
  2. copying texts 5800 problem.
    By vegeta_sayf in forum [Archived] Flash Lite on Nokia Devices
    Replies: 3
    Last Post: 2010-04-15, 13:42
  3. Problem with DrawText..
    By gb0071 in forum Symbian
    Replies: 9
    Last Post: 2009-03-04, 09:46
  4. Problem with DrawText()
    By gowthamgowtham in forum Symbian
    Replies: 3
    Last Post: 2008-02-24, 12:19
  5. New line in DrawText
    By kaisub in forum Symbian
    Replies: 1
    Last Post: 2003-05-30, 11:15

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×