Palmos Development Tutorial -13

zhaozj2021-02-08  246

Chapter 13 The Overview User Interface In this chapter and next chapter, we will create a complete calculator application. From China we will learn how to use public function libraries, especially how to use Mathllib, we will also learn many programming details and how to improve user interfaces. Calculator's design begins with a calculator form. It will include: 1 Display Digital Zone 2 Represents 10 Digital Buttons 3 Data Buttons 4 Symbol Button 5 Index Tuts 6 Plus, Declining, Multiply, Due Buttons 7 Equal Number Button 8 Clear Button 9 Complete ("DONE") button Copy the calculation result to the shear plate and turn off the calculator can be added, subtracted, multiplied, divided by the numbers, up to 99, 999, 999 * 1099. In this chapter, we discuss the visual design of the calculator form and the content of the user interface element, in the next chapter, will discuss the functional design of the calculator. Visual design In this section, we will carefully tell how to design the user interface. At this point, I think a lot. I hope my idea can give you how to build a general concept of the user interface. These ideas are mainly based on Chapter 7 and Palm OS SDK literature. When designing the user interface, I consider that users may get used to something on the calculator interface. For example, the historical form of numbers is specific. Also, some actual needs are also conventional, such as displaying areas at the top, so as to cover the display area when the user is pressed. We will also follow the requirements of Palm Computing, which makes it big enough to make the button to operate with your fingers and don't always ask for help and input pen. Based on these considerations, our calculator should be this model: arrange a few rows of buttons in the rules below the display area. I am very happy to comply with this kind of traditional request, because this is not heard that there are too many complaints about this, at least in my surroundings. It looks a successful design. Of course, don't trust the traditional view, if this kind of traditional requirement does not be people who are satisfied. For example, when we design a virtual recorder, I suspect whether it is like a real recorder, because many people are dissatisfied with this design. The next step should consider emissions between the buttons should be discharged in. One of these important factors is to take into account their use of their usage. I want the authoritative research results already come out, but I still use my own ideas. For a good user interface, I may propose the worst design patterns in real life, because you and my developers may be more mediocre in the model. So in the real design, we should do some investigations and research to determine what our users think. Here is some guess for the number of buttons: 1 Digital button averages less than once during each calculation process; 2 decimal point buttons may be less than 1/2 times; 3 Symbol buttons use approximately once; 4 index button The number of uses is very small; the 5th equity button is once each time; 6 Clear button is almost performed once; 7 The completion button is less than once each time; the other is worth considering the consequence of the misunderstanding button. If the error is pressed, it will cause very serious consequences, so they must be placed far away from the clicked frequency. There are 20 buttons in our design, based on the above considerations, I designed it to four columns, as shown in Figure 13-1. 7 8 9/4 5 6 * 1 2 3 - /-. DONE C Exp = Figure 13-1 My Button Design The next step is how to place each button on the form. At the time of placement, it should be noted that there is one around each button (there are two buttons if bold) the border of the pixel. Therefore, there should be a pixel for the left and top of the top. We need an edit box to display the calculation results. The edit box is much better than using the label.

We set the edit box attribute to no-underline, cannot edit, right alignment. We can also add a button to the edit box to generate a border so that it does not hang on the screen. I found that the height of the edit box is 20 pixels is a good choice. If the maximum size of Palm is 160 pixels, there are many ways to place each button. Here is my idea. I used the editor of the height of 20 pixels, the button height is 23, and the width of the two is 35 pixels. I am using the height and width of all buttons. It is just the height and width of the screen, thereby giving a button height and width. Below is the calculation method of the button width: 1 35 1 4 1 35 1 4 1 35 1 4 1 35 1 1 36 37 41 42 77 78 82 83 118 119 123 124 159 160 below is a highly calculated method: 20 3 1 23 1 3 1 23 1 3 1 1 3 1 23 1 20 23 24 47 48 51 52 75 76 79 80 103 104 107 108 131 132 135 136 159 160 This means how we will place buttons: lay down on 1, 42, 83 and 124, at 24, 52, 80, 108 Pulled with 136. Creating a calculator Now we add resources and code to the program. First, you should make a backup of our previous chapter for a backup. There are four calculator projects on the CD. Ch.12 is the first version, only the OK button on the form. Calculator 2 is the version of Chapter VII, which has three forms and a menu bar. The name of this version you saw after this chapter called Calculator Ch.13. Add the edit box and buttons to our project to make a program's user interface for the addition of Calculator.RSRC content. Let's add an edit box, then add a button from top to the top to the top, steps below: 1. Click the Start button, select Programs | CodeWarreor Lite for Palm OS | Constructor Run Configuration; 2. Edit the resource file. Select File | Open Project File In Constructor will generate a selection dialog that is selected from the SRC folder in the SRC folder. Open the CALC form in the ResOSouse Type and Name list; 4. Select the OK button Press the Delete button to delete it. When the button is selected, there are four small blacks around the border, and its properties are displayed in the Layout Properties panel; 5. Create an edit box.

Select Windows | Catalog, dragging a edit box from the Catalog template to the form of the Layout APPEARANCE panel; 6. Set the properties of the edit box as follows: Object Identifier = Number Left Origin = 5 TOP Origin = 4 Width = 150 Height = 18 Editable = No Check underline = No Check Left justified = No Check Other properties take the default. After you hit the Layout APPEARANCE panel, the edit box property appears in the Layout Properties panel, select each property editing and modification; 7. Drag buttons from the Catalog template to the form; 8. The properties of the settings button are as follows: Object Identifier = FieldBorder Left Origin = 1 Top Origin = 1 Width = 158 Height = 18 Remove the OK button in the Label property. Other properties take the default value; 9. Drag the "7" button from the Catalog template to the form; 10. The properties of the setting button are as follows: Object Identifier = seven left origin = 1 TOP Origin = 24 width = 35 height = 23 font = label = 7 Other properties take default; 11. Create a "8" button. Select the "7" button and press CTRL-C copy. Click the form to select yourself, press CTRL-V pastry; 12. The properties of the setting button are as follows: Object Identifier = Eight Left Origin = 42 TOP Origin = 24 Label = 8 Other properties and "7" buttons are exactly the same; 13. Create a "9" button. Click the Form and press the Ctrl-V and then paste the "7" button, modify the properties as follows: Object Identifier = Nine Left Origin = 83 TOP Origin = 24 label = 9; 14. Create other buttons as creating a "9" button. The changes in each button attribute are shown in the following table: Object Identifier Left Origin Top Origin Label Device 124 24 / Four 1 52 4 Five 42 52 5 Six 83 52 6 Multiply 124 52 * One 80 1 Two 42 80 2 Three 83 80 3 Substract 124 80 - Changsign 1 108 /- Point 42 108. Zero 83 108 0 Add 124 108 DONE 1 136 DONE CLEAR 42 136 C Exponent 83 136 Exp Equals 124 136 = 15. The form built is shown in Figure 13-2. Some people like to put the 0 button below the 2 button. This is your own calculator, so how to place it with you, as long as you define their names in the table, then it will work properly. I don't like the name of the Done button very much. Ideally, your user knows whether to replace the clipboard's things, maybe "Clip 'N' DIE" can accurately describe this. When you add all the buttons, you must remember to save.

Create a warning Select Alerts from the Resource Types List and press Ctrl-K to create a new warning message. Name it as Mathliberror. Double-click Open, modify the properties: Alert type is ERROR, Title property is Error. Writing "This Application Requires The Free Sharing Library Mathlib to BE Installed On Your Palm Device, please install it" is written in the Message property. Creating an application icon Creates a size icon for the calculator according to the steps of Chapter 4. When it is complete, save the Calculator.RSRC exit constructor. Adding fcalc.c content Now we start to add code for all new buttons. Click Start and select Program | CodeWarrior Lite to run the CodeWarrior IDE. Then select File | Open Recent | Calculator. Select Fcal.c from the project list to open. First modify the version number to the new version number. Below I will explain the new code in detail, pointing out the new content and modifications. // // fcalc.c // code for the "CALC" form. // Copyright (c) 1999, Robert Mykland. All rights reserved. // // // inclus // // #include "app.h" // The definitions for this application #include "cagc.h" // Calculator Guts Here we saw a new header file - Calc.h. It defines another module - Calc.c, Calc.c handles calculator operations outside the platform. Since this header is applied only to Fcalc.c, it is here instead of app.h. This shows that only fcalc.c uses a function of Calc.c. /// // Global prototypes // /// Void Calcinit (void); void CalcStop (void); void cagcdisplay; void cagcsignalerror (void); we added four A new global function. Functions Calcinit () and Functions CalcStop () New and Remove Resources for Fcalc.c. We can see how it is defined in Appinit () and AppStop () macro from app.h. The function of the function calcdisplay () is to update the contents of the calculator display. Functions CalcSignalerror () allows errors that appear through the interface display program. Note that although the user interface is fully encapsulated but can be accessed by both functions. / // local variables // / static char ccomma; // The Comma Symbol We're Using static char cpoint; // the decimal point we're using static handle hnumber; // handle to the number in Our Edit Field is above The module defines several variables used by the function. Note that they are defined as static because they cannot access them outside of the module.

This greatly reduces the number of times of modifying these variables because you accidentally modify these variables. Character-like variables CCOMMA and CPOINT represent thousands of characters and decimal points, respectively. Users can adjust these settings in the FORMATS form in the system reference of the PALM device. Below we will see how to read these parameters and how to modify them in the user interface. Another variable is used to handle the calculator to display digital memory. We will see it in the future, such as handling HTEXT in FieldToy. Function Calcinit () // // Global Functions // // // ------------------------------------------------------------------------------------------------------------------------------------------ ------------------------------------------ Void Calcinit (// --- -------------------------------------------------- ----------------------- // Initializes this module. // ------------------- -------------------------------------------------- ------- void) // --------------------------------------- ------------------------------------- {DWORD DCHOICE; char * cpnumber; this is the module The starting section of a function Calcinit (). It doesn't have any parameters and there is no return value. Variable Dchoice is used to store the number of times of selecting thousands and decimal points, and the variable cpnumber is used to initialize the memory into the field. Find and load a dynamic library // Find MathLib if ((SysLibFind (MathLibName, & MathLibRef) == 0) || SysLibLoad (LibType, MathLibCreator, & MathLibRef) || MathLibOpen (MathLibRef, MathLibVersion)) {FrmAlert (MathLibErrorAlert); hardStop ( 0);} The core of the calculator is to use the public library Mathlib to perform floating point calculations. Below we will discuss Mathlib in detail. First use syslibfind () to find any of the installed public libraries, in general, Mathlib libraries are one of the commonly used libraries. The next step is created to the library connection using the function syslibload (). The function MathLibopen () is initialized by the Mathlib call to make itself. Syslibfind () and syslibload () are the self-band function of Palm OS, and we can check them from the system manager of the Palm OS reference (Reference.pdf). If Mathlib is not found, loaded or opened for some reason, then we will call the function frmalert () to pop up MATHLIBERROR warning information. The application is then exited by calling the hardstop () to pilotmain (). Function frMalert () is defined in Form Manager (Ref1.pdf).

Initialization memory block // Allocate our field chunk hNumber = MemHandleNew (MAX_NUMBER_SIZE); // Lock the memory, get the pointer cpNumber = MemHandleLock (hNumber); // Initialize it StrCopy (cpNumber, "0"); // Unlock the field's Memory MemHandleunlock (HNUMBER); these four functions are very similar to the functions in the FieldToy. We used three Memory Manager functions (REF3.PDF) to assign and initialize the memory of the calculator display field. Function MemHandleNew () Assign Memory, Function MemHandLock () prevents the memory from reassigning the memory when we use, and the function strPy () (defined in string manager) is used to initialize zero the display memory block. Finally, we use the function memhandleunlock () to unlock the memory block. Get and use system priority // Handle the number format switch (Dchoice) {default: ccomma = ','; cpoint = '.'; Break; Case nfperiodcomma: ccomma = '; cpoint =', '; Break; CASE NFSPACMMA: CCOMMA = '; CPOINT =', '; Break; Case Nfapostropheperiod: ccomma =' / '; cpoint ='. '; Break; Case NfapostropheComma: ccomma =' / ''; cpoint = ','; Break;} // We're done return;} At the end of the initializer, it is used to describe how the numbers are displayed. We first call the function preientPreference () to get the system's priority, then use the Switch statement to assign the static variable CCOMMA and CPOINT. PregetPReference () is one of the system priority function.

Function CalcStop (): Endatabase Mathlib // --------------------------------------- ------------------------------------- void CalcStop (// -------- -------------------------------------------------- ------------------ // Cleans Up stuff from this module. // --------------------- -------------------------------------------------- ----- void) // ----------------------------------------- ----------------------------------- {uint uiusecount; // close mathlib mathlibclose (Mathlibref, & UiuseCount); IF (UiuseCount == 0) syslibremove (Mathlibref); // free the number field chunk memhandlefree (hnumber); // We're Done Return;} The top is a function CalcStop () corresponding to function CALCINIT (). First, we close the connection with Mathlib. Mathlib can track a few programs that use it, we use it to returns the value to determine if this public library should be removed. Since Palm OS is a single task operating system, in general, calling this function is not wrong. Functions syslibremove () is a reserved function, but Mathlib's creator suggests to use this function in this case. Finally, we use the function memhandlefree () (defined in Memory Manager) to release the memory used. CalcFormEventHandler's modification // ------------------------------------------- ------------------------------ Boolean CalcFormEventhandler (//------------ -------------------------------------------------- ------------ // Handles Events for this form. // Returns True if it full full handled the Event. // ----------------- -------------------------------------------------- --------- Eventptr speak) / / ------------------------------------ ---------------------------------------- {// Handle The Event Switch (Spevent- > eType) {This is a "old" CalcFormEventHandler () function, which does not have any difference with our function in the previous chapter. We need to have rewritten this function.

Initializing fields // The form is opened case frmOpenEvent: {FormPtr spForm; Word wIndex; FieldPtr spField; ControlPtr spButton; char caPoint [2]; // Get the field spForm = FrmGetActiveForm (); wIndex = FrmGetObjectIndex (spForm, calcNumberField); SPFIELD = frMgetObjectptr (spform, windex); // Draw the field fldsettextHandle (spfield, hnumber); FlddrawField (SPFIELD); After the form is loaded, the event frmopnevent immediately occurs. When we know that the form is loaded, use this event to initialize the form. First, we use three functions frMgetActiveForm (), frMgetObjectIndex (), frMgetObjectPtr () to get pointers for fields. Then we use the function fldsetTextHandle () and FldDrawField () in the Field Manager and display it in the edit box. Note that even if we use the same memory block, each time you display a field must also call fldsetTextHandle () because Palm OS is re-computational size and set field text in this function. For example, if we enter 123, but there is no call fldsetTextHandle () in the middle, and only the number 1 will only appear at the initial value of 0. This is because if fldsetTextHandle () or similar functions are not called to change its field length, it will follow the length of the initial field with only one. Change button text // Get the decimal point button wIndex = (spForm, calcPointButton) FrmGetObjectIndex; spButton = FrmGetObjectPtr (spForm, wIndex); // Draw the decimal point button caPoint [0] = cPoint; caPoint [1] = '/ 0 CTLSetLabel (spButton, Capoint);} Break; Due to the Palm OS allows the user to change the shape of the number, in the ideal case, we must force the interface to reflect this change. We use a other extended character to represent the symbol on the decimal point button. First we call the function frMgetObjectIndex () and functions frmgetObjectPtr () (defined in form manager, ref1.pdf) to get a pointer to the button structure. Create a character for a decimal point, then call the function CTLSetLabel () to change the tag content and redraw the button (Control Manager, Ref1.pdf).

Form button event processing // A control was selected case ctlSelectEvent: {// Handle the different buttons switch (spEvent-> data.ctlSelect.controlID) {case calcAddButton: calcAdd (); break; case calcChangesignButton: calcChangeSign (); break ; case calcClearButton: calcClear (); break; case calcDivideButton: calcDivide (); break; case calcDoneButton: {FormPtr spForm; Word wIndex; FieldPtr spField; // Get the field spForm = FrmGetActiveForm (); wIndex = FrmGetObjectIndex (spForm, calcNumberField ); spField = FrmGetObjectPtr (spForm, wIndex); // Select all FldSetSelection (spField, 0, FldGetTextLength (spField)); // Copy to clipboard FldCopy (spField); // Exit the application ErrThrow (0);} break; case calcEightButton: calcAppend (8); break; case calcEqualsButton: calcEquals (); break; case calcExponentButton: calcExponent (); break; case calcFieldborderButton: // Ignore this button return (true); case calcFiveButton: calcAppend (5); break Case CalcFourButton: Calcappend (4); Break; Case Calcmultip lyButton: calcMultiply (); break; case calcNineButton: calcAppend (9); break; case calcOneButton: calcAppend (1); break; case calcPointButton: calcPoint (); break; case calcSevenButton: calcAppend (7); break; case calcSixButton: calcAppend (6); break; case calcSubtractButton: calcSubtract (); break; case calcThreeButton: calcAppend (3); break; case calcTwoButton: calcAppend (2); break; case calcZeroButton: calcAppend (0); break; we can event Event structure variable control ControlID knows which button is pressed. The event structure is defined in the header file Event.h. In Calculator_RES.H, the ID number is defined in the constructor. In response to each button event, we call a series of functions. These functions are defined in the module Calc.c. There is no function in the CalcFieldBorderButton event because we intend to ignore it. In this way, the system will only issue a BEEP sound but not flash.

If you want to eliminate the Beep sound, you can add code to main.c, and screen it before calling frMDispatChevent (). The results are retained in the clipboard case calcDoneButton: {FormPtr spForm; Word wIndex; FieldPtr spField; // Get the field spForm = FrmGetActiveForm (); wIndex = FrmGetObjectIndex (spForm, calcNumberField); spField = FrmGetObjectPtr (spForm, wIndex); / / Select all fldsetSelection (SPFIELD, 0, FLDGETTEXTLENGTLENGTLENGTLENGTLENGTLENGT); // Copy to Clipboard FldStop (SPFIELD); // EXIT THE Application SoftStop (0);} Break; When Press the Done button, the program exits and will result Save on the clipboard. First get the pointer to the field before, call the function fldsetSerection () and the function fldgetTextLength () get the full content of the field, and then call another Field Manager Function FldCopy () copy the content to the clipboard. Finally, we use functions SoftStop () to end the event queue and exit the program. // a menu itvent: // handle the menu Event CalcFormMenueventHandler (SpEvent);} // We're Done Return (false);} MENU event processing and previous. This completes the new version of CalFormEventHandle (). Function caverdisplay () // -------------------------------------------- ------------------------------------------- -------------------------------------------------- ------------- // Displays the number in the field. // Important: a Leading or - Sign Is Assumed for all numbers passed to // this function! // ---- -------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------ -------------------------------------------------- ---- {char * cpsrc; int tentsize; char * cpdest; char canumber [40]; char * cpText; FormPtr spform; Word WINDEX; FieldPtr SPFIELD; this is the start section of CalcDisplay (). Functions can display floating point numbers such as 123456.78e-4 like traditional C, and can place thousands of characters and decimal points in the corresponding position.

In the Palm OS String Manager function, there is a function strlocalizenumber () to arrange the position of thousands of characters and decimal points, but it requires a thousand characters in the number. Since I need to enter thousands of characters, it is better to re-do let the display function more powerful. There are two parts. The first part is to rearrange the number we want to display; the second part is the real display on the screen. Adding a thousand stroke function to floating point numbers Calcdisplay () is to re-adjust the data we need, add thousands of characters and some other details. // Find the end of the number and determine exponent and decimal wintsize = -1; // to account for leading sign for (cpsrc = cpnumber; * cpsrc! = '/ 0'; cpsrc ) {IF ((* cpsrc == '. ') || (* cpsrc ==' e ')) Break; WintSize ;} How many numbers have the first loop computing to determine how many thousand characters to add, add to where it is added. Note that in this function, assume that each data has and - symbols. // start source and destination cpsrc = cpnumber; cpdest = canumber; // handle the Leading Signiff * CPDest = '-'; here we define source pointers and target pointers, copy old String to the new one. The next step is determined if the symbol is negative, we will copy. // copy the integer part while (iontesize) {* cpdest = * cpsrc ; iontesize ---;} while ((iontesize% 3)! = 0); if (intsize == 0) Break; * cpdest = ccomma; } In this section, we minimize the number of numbers, plus a thousand characters in front of each group. If the number of numbers is reduced to zero, then we jump out of the loop before adding a thousand consequence. // do the decimal point if (* cpsrc == '.') {Cpsrc ; * cpdest = cpoint;} // Copy the decimal part while (* cpsrc& (* cpsrc! = 'E') * cpdest = * CPSRC ; this part of the role is to copy the decimal point to ".". Then, copy the remaining numbers directly to the new string.

// build up the exponent if any if (* cpsrc == 'e') {* cpdest = ''; while (* cpsrc) * cpdest = * cpsrc ;} // zero delimit the string * cpdest = '/ 0' Next, copy the index to the indeter. Finally, the string is adjusted. This completes the writing of the first part of the function CALCDISPLAY (). Showing results // Change the string in the handle // Lock the memory, get the pointer cpText = MemHandleLock (hNumber); // Initialize it StrCopy (cpText, caNumber); // Unlock the field's memory MemHandleUnlock (hNumber); // Get the field pointer spForm = FrmGetActiveForm (); wIndex = FrmGetObjectIndex (spForm, calcNumberField); spField = FrmGetObjectPtr (spForm, wIndex); // Refresh the field FldSetTextHandle (spField, hNumber); FldDrawField (spField); // We're Done Return;} Let the new field to copy into the memory block being used in the field. Then re-get the field pointer and refreshed, which is very similar to what we do in frmopnevent. Function Calcsignalerror () // -------------------------------------------- -------------------------------------------- -------------------------------------------------- ------------- // Signals an error. // ----------------------------- ----------------------------------------------- void) / / --------------------------------------------------- --------------------------- {SndPlaySystemsound (SNDERROR); Return;} To display error message, we call the function SndPlaySystemsound () to issue Warning sound. This function is defined in Ref2.pdf. The addition of the content of the Fcalc.h file is given to the Fcalc.c file to make them universally, must be declared in the header file fcalc.h.

The following is the content of the modified fcalc.h file: #ifndef fcalc_h #define fcalc_h // // fcalc.h // definitions for the "Calc" form. // Copyright (c) 1999, Robert Mykland. All Rights Reserved. // /// // Global Prototypes // /// void calcInit (void); void calcStop (void); Boolean calcFormEventHandler (EventPtr spEvent); void calcDisplay (char * cpNumber); void calcSignalError (void); #endif / / Fcalc_h and usually, this file contains the prototype of the full bureau in FCALC.C. Calc.h module In the next chapter we will add a general code that the calculator is running. Here, create a header file to define these functions. In this header file, even if the program is incomplete, compiling connection and debugging can be compiled. It uses macro to replace a function in the general sense. Another way to define these functions is to create a new module and function, but only add some test code (like the call to CalcDisplay (). #ifndef coalc_h #define Calc_H /// Calc.h // Definitions for the gener Calculation Routines. // Copyright (c) 1999, Robert Mykland. All Rights Reserved. // /// // Global Prototypes //// / void Calcadd (void); // Queue An Add Operation Void Calcappend (int); // Append a Digit Void Calchangesign (Void); // Change the Sign of The entry void Calcclear (Void); // Clear / Reset THE calculator void calcDivide (void); // Queue a divide operation void calcEquals (void); // Finish the current operation void calcExponent (void); // Start gathering the exponent void calcMultiply (void); // Queue a multiply operation void Calcpoint (void); // Start Gathering The Fraction Void Calcsubtract (Void); // Queue A Subtraction Operation // // Global constants // // #define max_number_size 40 #ENDIF / / CALC_H CALC.C's function is here The definition is given, which includes a set of function definitions that test CalcDisPlay ().

Add ///////////////rluted // all the palm incrudes #include "mathlib.h" // definitions for mathlib #include "calculator_res.h" // Resource Definitions #include "found.h" // definitions for the "About" form #include "fork #include" fforc.h "// definitions for the" CALC "form #include" fprefs.h "// definitions for the" prefs "form # INCLUDE "main.h" // definitions for the main entry point #include "moptions.h" // definitions for the "options" MENU Here we added library function Mathlib.h, which contains the definition of the MathLib function. // this defines the macro what initializes the app #define Appinit () / {/ Calcinit (); /} // this defines the macro what cleans up the app #define appstop () / {/ CALCSTOP (); /} Here We add two macros to appinit () and AppStop () in an empty definition of APPSTOP (). If the module needs to initialize or sweep, you should add a function call to these macros. Installing Mathlib Mathlib is a self-extractable executable (Mathlib.exe) on the CD. Double-click the icon to extract it into a directory and follow the steps: In the unzipped folder, we will find anything we need to use Mathlib, including the HTML document. Copy Mathlib.c and Mathlib.h to the SRC folder of the Calculatot project. Add MATHLIB.C to the project to make it together and determine it is a basic input library. We also have to install the mathlib.prc file for the Palm device. With Palm Install Tool and HotSync, you can complete, open the App | INFO observation entry list to determine if the transfer is successful. When the first test program is prescribed, it is possible to install Mathlib to Palm before the MATHLIBERROR warning message. Debug download and run the program. Press Different Button Test Functions CALDISPLAY (), check the Done button to exit the program smoothly and save the current number on the clipboard. Change the value of the thousand characters and decimal points via System Preference to see if they can be reflected from the interface. Finally, see if you can produce a About form and a prefs form. What is going on next? Next We will continue the other part of the calculator program, learn how to add a general C code to Palm.

The list below is FCALC.C's full code: // // fcalc.c // code for the "CALC" form. // Copyright (c) 1999, Robert Mykland. All Rights reserved. // // includes // // #include "app.h" // the definitions for this application #include "cagc.h" // Calculator guts ///// Void Calcinit (void); Void CalcStop void); Boolean calcFormEventHandler (EventPtr spEvent); void calcDisplay (char * cpNumber); void calcSignalError (void); / // Local Variables // / static char cComma; // The comma symbol we're using static char cPoint; / / The decimal point we're using static handle hnumber; // handle to the number in outade field // // // ---------------- -------------------------------------------------- ---------- Void Calcinit (//---------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------- // Initializes this module. // -------------------------------------------------- ------------------------- void) // --------------------- ---------------------------- --------------------------- {DWORD DCHOICE; Char * cpnumber; // Find Mathlib IF ((Syslibfind (Mathlibname, & Mathlibref) == 0) || SysLibLoad (libType, MathLibCreator, & MathLibRef) || MathLibOpen (MathLibRef, MathLibVersion)) {FrmAlert (MathLibErrorAlert); sendStopEvent ();} // Allocate our field chunk hNumber = MemHandleNew (MAX_NUMBER_SIZE); // Lock the memory , get the pointer cpNumber = MemHandleLock (hNumber); // Initialize it StrCopy (cpNumber, "0"); // Unlock the field's memory MemHandleUnlock (hNumber); // Get the number format dChoice = PrefGetPreference (prefNumberFormat); // Handle The Number Format Switch (Dchoice) {Default: ccomma =

','; cpoint = ';'; ccomma = ';'; cpoint = ','; Break; Case NFSpaceComma: ccomma = '; cpoint =', '; Break; Case NFAPOSTROPHEPERID: CCOMMA = '/' '; cpoint ='. '; Break; Case nfapostropheComma: ccomma =' / ''; cpoint = ','; Break;} // We're done return;} // ------- -------------------------------------------------- ------------------- Void CalcStop (// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------- // Cleans Up stuff from this module. // --------------------------------------- ------------------------------------- void) // --------- -------------------------------------------------- ----------------- {uint uiusecount; // Close Mathlib MathlibClose (Mathlibref, & UiuseCount); if (UiuseCount == 0) syslibremove (Mathlibref); // Free the Number Field Chunk memhandlefree (hunumber); // We're done return;} // --------------------------------- ------------------------------------------ Boolean CalcFormEventHandler (// - ---------------------- -------------------------------------------------- - // Handles Events for this form. // Returns True if it full handled the Event. // --------------------------- ------------------------------------------------ EventPtr Spect) / / ---------------------------------------------- ------------------------------ {// Handle The Event Switch (Spevent-> ETYPE) {// The Form is Opened case frmOpenEvent: {FormPtr spForm; Word wIndex; FieldPtr spField; ControlPtr spButton; char caPoint [2]; // Get the field spForm = FrmGetActiveForm (); wIndex = FrmGetObjectIndex (spForm, calcNumberField);

spField = FrmGetObjectPtr (spForm, wIndex); // Draw the field FldSetTextHandle (spField, hNumber); FldDrawField (spField); // Get the decimal point button wIndex = FrmGetObjectIndex (spForm, calcPointButton); spButton = FrmGetObjectPtr (spForm, wIndex) ; // Draw The Decimal Point Button Capoint [0] = CPOINT; Capoint [1] = '/ 0'; CTLSetLabel (spbutton, capoint);} Break; // a control WAS SELECTED CASE CTLSELECTEVENT: {// Handle THE DIFFERENT buttons switch (spEvent-> data.ctlSelect.controlID) {case calcAddButton: calcAdd (); break; case calcChangesignButton: calcChangeSign (); break; case calcClearButton: calcClear (); break; case calcDivideButton: calcDivide (); break; case calcDoneButton: {char * cpNumber; // Lock the memory, get the pointer cpNumber = MemHandleLock (hNumber); // Copy to the clipboard ClipboardAddItem (clipboardText, cpNumber, StrLen (cpNumber)); // Unlock the field's memory MemHandleUnlock (hNumber ); // EXIT the application senedstopevent ();} Break; Case CalceiGH tButton: calcAppend (8); break; case calcEqualsButton: calcEquals (); break; case calcExponentButton: calcExponent (); break; case calcFieldborderButton: // Ignore this button return (true); case calcFiveButton: calcAppend (5); break; case calcFourButton: calcAppend (4); break; case calcMultiplyButton: calcMultiply (); break; case calcNineButton: calcAppend (9); break; case calcOneButton: calcAppend (1); break; case calcPointButton: calcPoint (); break; case calcSevenButton : calcAppend (7); break; case calcSixButton: calcAppend (6); break; case calcSubtractButton: calcSubtract (); break; case calcThreeButton: calcAppend (3); break; case calcTwoButton: calcAppend (2); break;

case calcZeroButton: calcAppend (0); break;}} break; // A menu item was selected case menuEvent: // Handle the menu event calcFormMenuEventHandler (spEvent); return (true);} // We're done return (false } // --------------------------------------------- ------------------------------- void cagcdisplay (// -------------- -------------------------------------------------- ------------ // Displays the number in the field. // Important: a Leading or - Sign Is Assumed for all numbers passed to // this function! // ----- -------------------------------------------------- ------------------------------------------------------- -------------------------------------------------- --- {char * cpSrc; int iIntSize; char * cpDest; char caNumber [40]; char * cpText; FormPtr spForm; Word wIndex; FieldPtr spField; // Find the end of the number and determine exponent and decimal iIntSize = - 1; // TO Account for Leading Sign for (CPSRC = CPNumber; * CPSRC! = '/ 0'; CPSRC ) {IF ((* CPSRC == '.') || (* cpsrc == 'e')) Break; ilk;} // start source and destination cpsrc = cpnumber; cpdest = canumber; // Handle the Leading Signix (* CPSRC == '-') * cpdest = '- '; // copy the integer part while (iontesize) {* cpdest = * cpsrc ; iontesize ---;} while ((iontesize% 3)! = 0); if (intsize == 0) Break; * cpdest = CCOMMA;} // do the decimal point if (* cpsrc == '.') {cpsrc ; * cpdest = cpoint;} // copy the decimal part while (* CPSRC && (* CPSRC! = 'E')) * * CPDest = * CPSRC ;

// build up the exponent if any if (* cpsrc == 'e') {* cpdest = ''; while (* cpsrc) * cpdest = * cpsrc ;} // zero delimit the string * cpdest = '/ 0' ; // Change the string in the handle // Lock the memory, get the pointer cpText = MemHandleLock (hNumber); // Initialize it StrCopy (cpText, caNumber); // Unlock the field's memory MemHandleUnlock (hNumber); // Get the field pointer spForm = FrmGetActiveForm (); wIndex = FrmGetObjectIndex (spForm, calcNumberField); spField = FrmGetObjectPtr (spForm, wIndex); // Refresh the field FldSetTextHandle (spField, hNumber); FldDrawField (spField); // We're done Return;} // --------------------------------------------- ------------------------------- Void Calcsignalerror (// -------------- -------------------------------------------------- ------------ // Signals An error. // ------------------------------ ---------------------------------------------- Void) // -------------------------------------------------- -------------------------- {SNDPLAY SystemSound; Return;} The following is the full code of app.h: #ifndef app_h #define app_h /// App.h // Definitions for the application. // CopyRight (c) 1999, Robert Mykland. All Rights Reserved. // // includes // // #include // all the palm incdude #include "mathlib.h" // definitions for mathlib #include "calculator_res.h" // resource definitions # INCLUDE "fabout.h" // definitions for the "About" form #include "fcalc.h" // definitions for the "CALC" form #include "fprefs.h" // definitions for the "prefs" form #include " Main.h "

转载请注明原文地址:https://www.9cbs.com/read-2725.html

New Post(0)