Handle drawing in Symbian

This article will show how to draw an image and shapes in your Symbian application.
Will start from the Hello World project described in the previous article.

First thing to do is chose an image, open it in Paint and save it as you desire(256 color, 24bit bmp) just remember
this information, to use it later on.Save the images to the group directory where the mmp file is.

You need 3 file to be able to load an image: *.mmp , *.mbm and *.mbg file

1.MMP file

The *.mmp file that is file that is located in the group directory of your project,
besides listing the desired mbm files you also can add lib files,
more information about mmp files and other symbian project components you can find on nokia forum:

http://www.forum.nokia.com

Right click on mmp file and select Open with->Text editor to view it’s content.

You should add the following lines to the mmp file:

START BITMAP test7.mbm
TARGETPATH \resource\apps
HEADER
SOURCE c24 smiley.bmp smilmask.bmp
END

The mbm file should be named after the application(my application is test7 so I named it test7.mbm).
c24 is the type of bmp – 24bit bmp file.
eg. if your bmp is 24bit, than use c24
if your bmp if 256color, 256color=2^8 -> 8bit, this means you’ll have to use c8
…and so on
smiley.bmp, smilmask.bmp the name of the bmp files, you can add as many as you like.

After you’ve added this to the mmp file, recompile,
and the mbm and mbg files will be automatically created.

2.MBM file

MBM – multi bitmap file used by Carbide to load the images, this file can contain multiple bitmaps.

You can either create this file yourself from some known photo design applications like PhotoShop or
let Carbide create it with some simple lines inserted in *.mmp file at compilation process(this is the method I’ve chosen).
If you change the images you’ll have first to delete this file from where is created so that it takes the new bmp, at
least this is what it happened to me, otherwise it didn’t create the new mbm. You will also need to comment the parts
from the code that refer to the image ids for the compilation to take place.

Where is the MBM file located?
You don’t really need this information, at least not for this purpose, as at compilation process carbide takes it
from where it has saved it, mine was located in:

c:\Symbian\9.1\S60_3rd_MR\Epoc32\Data\z\resource\apps\

, anyway the location is different from a platform to another, you’ll have to serch for it somewhere in
c:\Symbian\9.1\S60_3rd_MR\Epoc32 if you want to know it’s location for sure. I guess it depends on
EPOCROOT, platform, TARGETPATH..I didn’t bother too much about this.

3.MBG file

This file contains the ids for the bmp files, that you’ll use later in the application for loading them.
As I mentioned earlier this file is created on compilation procces. Mine was created in:

c:\Symbian\9.1\S60_3rd_MR\Epoc32\BUILD\work\Symbian\test7\group\test7\GCCE\
and in
c:\Symbian\9.1\S60_3rd_MR\Epoc32\include\

Don’t modify this file! if you do, it won’t work anymore, let Carbide create it.
Once it is created you can include the file in your project:

#include <*.mbg>

then you can use the ids, my ids are:

EMbmTest7Smiley and EMbmTest7Smilmask

If you delete this file you’ll need to comment the parts of the code were the ids are used and where it is included,
and also delete the mbm file(otherwise it will not be created), in order to recompile the code and the mbm and mbg
files to be created once again.

4.The code:

Include first the mbg file:

#include *.mbg;

I specially made a class for loading and drawing the bmp files.
Here are some snippets from the class I’ve created:

void CImageHandle::LoadBitmap(const TDesC& aPathAndFile,TInt aId)
{
TParse mbfn;
mbfn.Set(aPathAndFile,&KTxtCDrive,NULL);

if(aId == EMbmTest7Smiley)
iBitmap->Load(mbfn.FullName(),aId);
else if(aId == EMbmTest7Smilmask)
iMaskBitmap->Load(mbfn.FullName(),aId);

return;
}

void CImageHandle::Draw(CWindowGc& aGc,const TDesC& aPathAndFile,TInt aId,TInt aIdMask)
{
iBitmap = new (ELeave) CFbsBitmap();
iMaskBitmap = new (ELeave) CFbsBitmap();

LoadBitmap(aPathAndFile,aId);
LoadBitmap(aPathAndFile,aIdMask);

// set a rectangle for the top-left quadrant of the source bitmap
TSize bmpSizeInPixels=iBitmap->SizeInPixels();
TSize bmpPieceSize(bmpSizeInPixels.iWidth,bmpSizeInPixels.iHeight);
TRect bmpPieceRect(TPoint(0,0),bmpPieceSize);

TPoint pos(20,20);

aGc.BitBlt(pos, iBitmap);
//aGc.DrawBitmap(bmpPieceRect,iBitmap);
//aGc.BitBltMasked(pos,iBitmap,bmpPieceRect,iMaskBitmap,EFalse);
}

And here is the code for the drawing event:

void Ctest7AppView::Draw(const TRect& /*aRect*/) const
{
CImageHandle* Ctest7DrawObj;

// Get the standard graphics context
CWindowGc& gc = SystemGc();

// Gets the control's extent
TRect drawRect(Rect());

// Clears the screen
gc.Clear(drawRect);

Ctest7DrawObj = new ( ELeave ) CImageHandle;
Ctest7DrawObj->Draw(gc,KTxtMBMname,EMbmTest7Smiley,EMbmTest7Smilmask);

TPoint startPoint(50,50);
TPoint endPoint(590,190);

gc.DrawLine(startPoint,endPoint);

}

Besides displaying the image it also draws a line, it’s very simple, other shapes as well.

In order for this code to work you’ll have to include some libs:hlplch.lib fbscli.lib bafl.lib

And here is the result:

Source code: drawimagearticle

5. How to add a lib file?

You can open the mmp file in carbide and there you have the option to add a lib file, or simply edit it in
notepad.

It is a good idea to start learning about Exception Handling as these are often used in Symbian programming,
I didn’t use them as I don’t know how they work at this moment, it looked simple but when I’ve used them something
went wrong so I didn’t investigate it anymore, I worked without them. I would appreciate if someone who has the knowledge
would write a few lines about this. Here are some links that might help:

http://www.newlc.com/Exception-Handling-and-Cleanup.html
http://www.newlc.com/LEAVE-and-TRAP.html

By Meda Chiorean

Leave a Reply