turrier.fr

Source : ct|23.04.08

< Tutorials Computer, Multimedia, Chinese

Displaying text number and drawing on a Palm
with OnboardC

Onboardc is a free development tool allowing to create Palm programs in C compiled language, without runtime. If you do not know how to download, install and use Onboardc, go on the tutorial "How to program for Palm OS with the tool OnBoardC " who explains in detail how to proceed.

We will suppose that you know those elements, and we come now to regard how to display, with the help of a short and simple program, texts, numbers, lines and filled rectangles (then points) on the screen of a Palm.

We will begin to create a skeleton of programme as short as possible, in order to isolate the essential of the code. Then we will study four importants functions, permitting to display text, numbers, lines and rectangles on the screen of the Palm. Lastly we will complete the skeleton of the programme, using those functions in an example. In this tutorial, we will follow the following plan :

1) Realization of a skeleton for a short programme;
2) Study the functions permitting to display texts, numbers and drawings;
3) Use of the functions in an example program using the skeleton.

Realization of a skeleton for a short program

Our skeleton of short program, of which contents is indicated hereafter, comprises 4 parts:
A-A part containing the declaration of the file <Palmos.h> which will be used by the compiler;
B-A part containing your specific code;
C-A part containing the code of the window procedure of the program. This procedure watchs if the user clicks with the mouse and, in the affirmative, she terminates the program if the click corresponds to a "end program message";
D-A part containing the principal procedure of the programme, which executes once the beginning procedure debut() and the window procedure boucle() in loop.

If you have already done a Windows program, the principal procedure, called Pilotmain(), tallies to the procedure Winmain().

//--------------------
//A-Définitions
//--------------------
#ifdef __GNUC__
#include <PalmOS.h>
#endif
//---------------------------
//B-Votre code
//---------------------------
static int debut()
{ //Ajouter votre code ici
return 0;
}
//----------------------
//C-Procédure de fenêtre
//----------------------
static void boucle()
{
EventType evt;
do
{
EvtGetEvent(&evt, evtWaitForever);
if (!SysHandleEvent(&evt)) FrmDispatchEvent(&evt);
}
while (evt.eType!=appStopEvent);
}
//--------------------
//D-Procédure principale
//---------------------
UInt32 PilotMain(UInt16 launchCode, void *cmdPBP, UInt16 launchFlags)
{ if (launchCode==sysAppLaunchCmdNormalLaunch)
{if (debut() ==0) boucle();}
return 1;
}

If you compile then execute this source program it will simply display an empty titleless window. This is quite normal. To give up this program and to return to the "Applications" screen of the Palm, click the little house, placed down-left under the window of the programme.

image0

image1

Study the functions permitting to display
texts, numbers and drawings

We will use four functions, which are declared in the file "Onboardheaderv40.pdb", supplyed with the Onboardc tool (you can easily verify this, opening this file with the tool TL-PDB).

1) The function StrprintF()

This function allows to display the content (who can be formatted...) of a string char *formatstr in a string char* s. His prototype is : Int16 StrPrintF(char *s, const char *formatStr, ...).

//Example:
int n=3; followed with StrprintF(texte,"n = %i",2*n);// put the value 2*3= 6 in the string "texte". StrPrintF(texte,"bonjour !") //put the text "bonjour !" in the string "texte".

2) The function WinDrawChars()

This function allows to display, to a location (x,y) of the screen, the content of a string *chars having a lenght len. His prototype is : void WinDrawChars(const char *chars, Int16 len, Coord x, Coord y).

//Example:
Windrawchars(text,Strlen(text),10,30) //display the content of the string "texte" to the location (10,30) of the screen.

3) The function WinDrawLine()

This function allows to draw a line to the screen, between the points of coordinates (x1,y1) and (x2,y2). His prototype is void WinDrawLine(Coord x1, Coord y1, Coord x2, Coord y2).

//Example:
WinDrawLine(0, 50, 200, 50);//draw a line to the screen between points (0,50) and (200,50)

4) The function WinDrawRectangle()

This function allows to draw a full rectangle. Up-left and bottom-right points of this rectangle must be defined in a variable rp of type Rectangletype *. The prototype of this function is void WinDrawRectangle(const RectangleType *rP, UInt16 cornerDiam). The type Rectangletype is defined by a structure containing 2 points (up-left and bottom-right).The parameter cornerDiam takes the value 0.
typedef struct {
PointType topLeft;
PointType extent;
} RectangleType;
typedef RectangleType *RectanglePtr;

//Example :
RectangleType r;
r.topLeft.x=100;
r.topLeft.y=100;
r.extent.x=5;
r.extent.y=5;
WinDrawRectangle (&r,0);

Use of these functions in an example program
using the skeleton

We will use those functions, inserting following code lines in the procedure debut() (part B) of our program skeleton. The three others parts (A, C and D) remaining unchanged.

//--------------------
//A: Définitions
//--------------------
#ifdef __GNUC__
#include <PalmOS.h>
#endif
//---------------------------
//B: Your code
//---------------------------
static int debut()
{
RectangleType r;
r.topLeft.x=80;
r.topLeft.y=20;
r.extent.x=5;
r.extent.y=5;
char* mot="Bonjour !";
char texte[80];
int n;
n = 1234;
StrPrintF(texte,"n = %i",2*n);
WinDrawChars (mot, StrLen(mot),10,10);
WinDrawChars(texte,StrLen(texte),10,30);
WinDrawRectangle (&r,0);
WinDrawLine(0, 50, 200, 50);
return 0;
}
//----------------------
//C: Programm loop
//----------------------
static void boucle()
{
EventType evt;
do
{
EvtGetEvent(&evt, evtWaitForever);
if (!SysHandleEvent(&evt)) FrmDispatchEvent(&evt);
}
while (evt.eType!=appStopEvent);
}
//--------------------
//D: Main procedure
//---------------------
UInt32 PilotMain(UInt16 launchCode, void *cmdPBP, UInt16 launchFlags)
{
if (launchCode==sysAppLaunchCmdNormalLaunch)
{if (debut() ==0) boucle();}
return 1;
}

Create an Onboardc project named "essai" for instance. Insert the preceding source code in the source file "essai.c", then compile it.

image2

Once the compilation is terminated, click the icon "essai".

image3

During the execution, the programme displays to the screen:
- "Bonjour !", at the location (10,10) ;
- 2*1234 soit 2468, at the location (10,30) ;
- a filled rectangle with a size 5x5 pixels, at the location (80,20) ;
- a line between the points (0,50) and (200,50).

image4

To permit you to economize time and to reduce the risks of manipulation errors, you will find here a copy of the source program in the format txt . This copy is saved in a file named "copie_essai.txt". Open it in TL-PDB, then create the corresponding file "copie_essai.pdb". Transfer this file "copie_essai.pdb" in the Palm emulator (or in the Palm himself ), then open it with SrcEdit and copy the corresponding text in the clipboard of the Palm emulator (or in that of the Palm).

After this, create a new project with Onboardc, be careful to not give it the same name (otherwise you will have an error of the style "Out of memory ! error 14"" during the compilation) ( this is always painful...)

image5

In the present case , to avoid this error, call your new projet "essai" for instance , or give it the name that you want but do not call it especially "copie_essai"... Copy, in the source file "essai.c" of your new project, the source text that you have put in the Palm clipboard. Compile and execute the programme.

Now you can create new programs containing original drawings. For instance , you can create a programme displaying a drawing or a reusable pattern. Execute this programme with the Palm emulator, then copy the content of the screen in the Windows clipboard ( with the aid of the key "Impr écran" ) and after paste it in Paint. You can so save your drawings in the format "bmp" or "png" to reuse them later with another software.


Valid XHTML 1.0 Transitional

© http://turrier.fr (2007)