×
Namespaces

Variants
Actions

Create Floating Text DialogBox

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Article
Created: savaj (31 Mar 2009)
Last edited: hamishwillee (30 May 2013)

Floating Text DialogBox can be used to show the hint text in a nice way. This is a simple CCoeControl derived class.

Header required:

#include <COECNTRL.H>


Library required:

LIBRARY  cone.lib


Capability required:

Capability None

TextDialog.h

#ifndef TEXTDIALOG_H 
#define TEXTDIALOG_H
 
#include <COECNTRL.H>
 
#define MAX_LINES 20
 
class CTextDialog : public CCoeControl
{
public:
enum TPosition
{
ETopLeft,
ETopRight,
EBottomLeft,
EBottomRight
};
 
public:
TRgb iBgColor;
TRgb iFgColor;
 
void ZeroRect() ;
void ConstructL(TRgb aBackgroundColor, TRgb aForegroundColor,TRgb aBorderColor, TInt aBorder, TInt aMargin) ;
void Draw(const TRect& aRect) const ;
void SetText(TInt aMaxWidth, HBufC* aText, TPosition aPosFrom,TPoint aPos);
 
const CFont* iFont;
TInt iLines[MAX_LINES];
TInt iCurrentWidth, iCurrentLineCount, iLineHeight;
TInt iMargin, iBorder; TRgb iBorderColor;
HBufC* iText;
 
 
};
 
 
#endif /* TEXTDIALOG_H */


TextDialog.cpp

#include "textdialog.h"
 
void CTextDialog::ZeroRect()
{
SetRect( TRect(TPoint(0, 0), TSize(0, 0)) );
}
 
void CTextDialog::ConstructL(TRgb aBackgroundColor, TRgb aForegroundColor,TRgb aBorderColor, TInt aBorder, TInt aMargin)
{
 
CreateWindowL();
 
ZeroRect();
 
iBgColor=aBackgroundColor;
iFgColor=aForegroundColor;
iFont=CCoeEnv::Static()->NormalFont();
iLineHeight=iFont->HeightInPixels()*1.1;
iMargin=aMargin;
iBorder=aBorder;
iBorderColor=aBorderColor;
}
 
void CTextDialog::Draw(const TRect& aRect) const
{
 
CWindowGc& gc = SystemGc();
gc.SetPenStyle(CGraphicsContext::ESolidPen);
gc.SetPenSize(TSize(iBorder, iBorder));
gc.SetPenColor(iBorderColor);
gc.SetBrushColor(iBgColor);
gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
gc.DrawRect(Rect());
 
if (! iText || iText->Length()==0) return;
 
gc.UseFont(iFont);
TInt baseline=iFont->HeightInPixels()-iFont->DescentInPixels();
TPoint p(iBorder+iMargin, baseline+iBorder+iMargin);
TInt pos=0;
gc.SetPenStyle(CGraphicsContext::ESolidPen);
gc.SetPenColor(iFgColor);
for (int i=0; i<iCurrentLineCount; i++) {
 
TPtrC text=(*iText).Mid(pos, iLines[i]-pos);
gc.DrawText(text, p);
pos=iLines[i];
 
 
if ( pos<(*iText).Length() &&
TChar((*iText)[pos]).IsSpace() ) pos++;
p.iY+=iLineHeight;
}
gc.DiscardFont();
}
 
void CTextDialog::SetText(TInt aMaxWidth, HBufC* aText, TPosition aPosFrom, TPoint aPos)
{
iText=aText;
if (! iText || iText->Length()==0)
{
ZeroRect();
return;
}
 
TInt i, w, l, previous_space=0, max_w=0, previous_w=0;
for (i=0, w=0, l=0; i<=aText->Length() && l<MAX_LINES; i++) {
if ( i==aText->Length() || TChar((*aText)[i]).IsSpace()) {
previous_w=w;
previous_space=i;
}
TInt this_w=0;
if (i<aText->Length())
this_w=iFont->CharWidthInPixels( (*aText)[i] );
if (i==aText->Length() || w+this_w > aMaxWidth) {
if (previous_space==0) {
/* if there is no break opportunity,
we just cut the word */

previous_w=w;
previous_space=i-1;
}
if (previous_w > max_w) max_w=previous_w;
iLines[l]=previous_space;
l++;
i=previous_space;
previous_space=0;
w=0;
} else {
w+=this_w;
}
}
iCurrentLineCount=l;
iCurrentWidth=max_w+2*iMargin+2*iBorder;
TPoint tl;
TSize s(iCurrentWidth, iCurrentLineCount*iLineHeight+2*iMargin+2*iBorder);
switch(aPosFrom) {
case ETopLeft:
tl=aPos;
break;
case ETopRight:
tl.iX=aPos.iX-s.iWidth;
tl.iY=aPos.iY;
break;
case EBottomLeft:
tl.iX=aPos.iX;
tl.iY=aPos.iY-s.iHeight;
break;
case EBottomRight:
tl.iX=aPos.iX-s.iWidth;
tl.iY=aPos.iY-s.iHeight;
break;
}
SetRect(TRect(tl, s));
}

How to call it??

if(iTextDialog)
{
delete iTextDialog;
iTextDialog = 0;
}
iTextDialog=new (ELeave) CTextDialog;
iTextDialog->ConstructL(KRgbBlue, KRgbWhite, KRgbBlack, 1, 2);
_LIT(KText, "Testing dialog");
iText = KText().AllocL();
iTextDialog->SetText(70, iText, CTextDialog::EBottomLeft, TPoint(10, 180));
iTextDialog->ActivateL();
This page was last modified on 30 May 2013, at 07:37.
38 page views in the last 30 days.
×