×
Namespaces

Variants
Actions

RBufConsole class example

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Code ExampleArticle
Created: Carbider (29 Nov 2007)
Last edited: hamishwillee (30 May 2013)

This code example show how to add an observer to RBuf descriptors.


Contents

Introduction

Whilst your application does some actions it needs to generate messages for user or for debug purposes.

If you have GUI application you will need some text storage which can be filled up and be shown as GUI message. I’ve developed such a class called RBufConsole which is comfortable for the aims above and is a good example of RBuf Symbian descriptor.


RBuf is a Symbian descriptor designed to manipulate strings and binary data.


In this example RBuf is used for appending strings, reallocation memory buffer, passing function parameters.


In addition, some Active Object can save messages to the same storage while performing it’s tasks. This is possible with RBufConsoleObserver interface class which relates to Observer paradigm.


So when Active object produces some action it calls Observer's callback function to add message to the RBufConsole storage.

After Active Object completes it can call the show message event or main program can do it.


RBufConsole Constructor

Assigns the resource (from rss file) for GUI Message (will be shown below). Initializes MessageStorage variable – RBuf class instance.

CRBufConsole::CRBufConsole(TInt aResourceID)
{
 
//Set resource ID from rss file
MessageDialogResourceID = aResourceID;
 
//initializaing storage
TInt maxSizeOfMsgData = 150;
_LIT(KGenesis , "App starts\n");
MessageStorage.Create(KGenesis(), maxSizeOfMsgData);
}

RBufConsole Append() method

The Append() method will check if the new message can exceed the descriptor’s size and will reallocate memory in this case

void CRBufConsole::Append(TPtrC16 appendString)
{
const TInt newLength = MessageStorage.Length() + appendString.Length();
 
if (MessageStorage.MaxLength() < newLength )
{
//Reallocate storage's memory if it is not enough
MessageStorage.ReAlloc(newLength);
}
MessageStorage.Append(appendString);
}


RBufConsole Show() method

When programmer decides, he can show all messages stored in RBufConsole storage, in GUI Popup.

void CRBufConsole::Show()
{
_LIT(KDialogHeader, "Output");
 
RBuf title;
CAknMessageQueryDialog* dlg = new (ELeave)CAknMessageQueryDialog();
dlg->PrepareLC(MessageDialogResourceID);
 
title.Create(KDialogHeader());
dlg->QueryHeading()->SetTextL(title);
 
//Assign storage to be outputed
dlg->SetMessageTextL(MessageStorage);
 
//show scrollable message
dlg->RunLD();
 
title.Close();
 
return;
}


Resource file for Message Query Dialog

In your .rss file you should define resource for CAknMessageQueryDialog. It would be passed as a parameter to constructor: CRBufConsole (R_RBUFCONSOLE_QUERY_DIALOG);

RESOURCE DIALOG r_rbufconsole_query_dialog
{
flags = EGeneralQueryFlags | EEikDialogFlagNoBorder | EEikDialogFlagNoShadow;
buttons = R_AVKON_SOFTKEYS_OK_EMPTY;
items=
{
DLG_LINE
{
type = EAknCtPopupHeadingPane;
id = EAknMessageQueryHeaderId;
itemflags = EEikDlgItemNonFocusing;
control = AVKON_HEADING
{
};
},
DLG_LINE
{
type = EAknCtMessageQuery;
id = EAknMessageQueryContentId;
control = AVKON_MESSAGE_QUERY
{
};
}
};
}


Usage examples

Append new message:

iConsole.Append(_L("step 1\n"));

Show storage contents:

iConsole.Show(); //Shows RBufConsole contents as a GUI scrollable message


Execute Append() callback function inside Active Object:

iRBufConsoleObserver->ObserverAppend(_L("Active object completes\n"));


Download this example

Download Zip archive with full example: File:WikiConsoleExample.zip . It is based on Carbide C++ basic GUI application. I will grow functionality in the course of time.

To know how to import project from Zip see How to get project from CVS to Carbide C++.


See also

This page was last modified on 30 May 2013, at 07:40.
45 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×