John.Wiley.and.Sons.Rapid.Mobile.Enterprise.Development.for.Symbian.OS.An.Introduction.to.OPL.Application.Design.and.Programming.May.2005.eBook-LinG (779881), страница 21
Текст из файла (страница 21)
If so, the entire entry isinversed to represent the cursor.On moving the cursor (back in the KeyboardDriver: procedure)we simply ask the whole screen to be redrawn. It would be a simpleexercise for the reader to optimize this code by only redrawing the wholescreen if TopCursor% is altered – and only reprinting two entries for anormal cursor move.6.2.6Editing EntriesEditing an entry is a simple matter of pulling the two pieces of information out of the database using B.Heading$, placing these LOCALstring variables into Dialog, and then writing the changes back out tothe database:PROC EditNote:LOCAL Foo%,Heading$(255),Text$(255),USE BPOSITION TrueCursor%Heading$=B.Heading$Text$=B.Text$dINIT "Edit Note"dEDIT Heading$,"Heading",100dEDIT Text$,"Text",230dBUTTONS "Close",KdBUTTONEnter%LOCK ONFoo%=DIALOGLOCK OFFIF Foo%<>0MODIFYB.Heading$=Heading$B.Text$=Text$PUTShowEntries:ENDIFENDPThe first half of this procedure should be recognizable from both theINI procedures from the Event Core, and constructing a Dialog from theConvert chapter.Again, we’re doing some defensive coding, and making sure thatwe assume nothing.
That’s why we make sure we are using the "B"database, and that the POSITION inside the database corresponds withthe TrueCursor% position.Once we exit the Dialog (and we’ve not left it with a cancel or anescape key press, which would see us RETURNed to the main programloop), we need to save the changes held in the LOCAL (and temporary)variables back into the database. Firstly, we let the database know thatwe are about to change something in the current record by using theOUR FIRST OPL DATABASE109MODIFY command.
We then list all the fields, including those wherethere are changes we want to make. Note that even if there is a field withno changes, we still need to include it here.Once all our changes are in place, we then commit the changes to thedatabase entry with the PUT command. Finally, in case the heading waschanged, we re-display the screen by callingShowEntries:Note that when you add or edit a record, this record goes to the end ofthe database, so your cursor may be in the same position, but it will beon a different record.6.2.7 Adding EntriesNot surprisingly, adding a new record to a database is very similar toediting a record:PROC AddNote:LOCAL Foo%,Heading$(255),Text$(255)USE BdINIT "Create New Note"dEDIT Heading$,"Heading",100dEDIT Text$,"Text",230dBUTTONS "Close",KdBUTTONEnter%LOCK ONFoo%=DIALOGLOCK OFFIF Foo%<>0INSERTMODIFYB.Heading$=Heading$B.Text$=Text$PUTLASTShowEntries:ENDIFENDPThe first of the two changes is that we don’t need to load in any existingvalues, because there aren’t any.
So once we make sure we’re using thecorrect database, we can go straight into the Dialog box.The second change is the use of the INSERT command, which, ratherobviously, inserts a new blank record into the current database, whichwe can then MODIFY and PUT information into in the same way as whenwe were editing a record. Note that INSERT will add the blank recordat the end of the database (like adding a new index card to the backof a pile), so your cursor will be in the same position at the end of thisprocedure, but on a different record.1106.2.8DATABASES AND A NOTEPAD PROGRAMDeleting EntriesThe final operation we need to be able to do is to delete an entry fromthe database when we no longer want it:PROC DeleteNote:USE BPOSITION TrueCursor%rem *** Check if it can be deletedIF COUNT=1GIPRINT "Cannot Delete Last Note."RETURNELSEERASEENDIFShowEntries:ENDPAs with our other ‘editing’ routines, we start by making sure we are atthe correct position in the correct database.
We then check to see ifthere is only one record in the database. While it is possible to writeyour program so it can handle the display and the cursor controls whenthere are no variables, that is left as an exercise for the reader. Here wehave a simple one-line check that stops the user from deleting the lastentry, using the COUNT command (which returns the number of entries inthe database). If there is one record, then we RETURN with no changes(after displaying a status message using GIPRINT), otherwise we use theERASE command. This acts on the entry at the current POSITION (whichof course we made sure was matched up to the value of TrueCursor%at the start of the procedure).When you delete a record, the position in the record is moved tothe next entry in the database, which is also where TrueCursor% nowpoints to.
Note that if you make a lot of changes to the database (e.g.deleting many entries or editing a lot of data), the file size can sometimesgrow as ‘dead’ space is left in the file on disk. This can be reclaimedimmediately using the COMPRESS command. See the command listingfor more details.6.2.9 Putting Everything in PlaceSo now we’ve looked at all the code sections we’ll need to writeour Notepad program. Way back when we started the Event Core, weshowed you that the key to making a program is breaking it down intosections – exactly what we’ve done here in looking at the things we needfor the Notepad program.What’s left is to tie everything together in the main loop inside EventCore, and this means adding in the menu system and the hot keys (we’vealready shown the cursor controls you’ll need).OUR FIRST OPL DATABASE111The InitApp: ProcedureThis is where you will make the main changes from the Event Core.
Askyourself what you need to do before you go into the main loop:• open the database• set the cursor position• display the entries.PROC InitApp:rem Load DatabaseOpenFile:("C:\System\Apps\OPLPad\OPLPad.db")rem Set Cursor Positions and valuesTrueCursor%=1TopCursor%=1MaxCursor%=INT(ScreenHeight%/FontHeight%)rem Display EntriesShowEntries:ENDPBecause InitApp: is called after Init:, the variables for the ScreenHeight% and FontHeight% will have been calculated when wechecked the screen sizes (we’ll add in the font information as we needit). What differentiates the Init: and InitApp: procedures? InitApp: may be called again during the running of the program, Init:cannot.6.2.10 Adding Menus and Hot KeysWe’ll be using the standard menu layout used in the Event Core, andadding in the options we’ll need for the Notepad.
We’ll do this by addingin either a new menu card (for UIQ or Series 80 UI machines) or acascading menu on a Series 60 machine.We need to add in "New Note", "Edit Note" and "Delete Note". If you’readding this as a menu card, then something like:mCARD "New Note",%n,"Edit Note",%a,"Delete Note",%dand for Series 60 devices:mCASC "Notes","New Note",%n,"Edit Note",%a,"Delete Note",%drem Call this Cascading menu from inside your menu systemYou’d then add the three hot keys into the ActionHotKey: procedure,which call the procedures we’ve worked on above.112DATABASES AND A NOTEPAD PROGRAM6.3 SummaryFrom the INI database in the Event Core (Chapter 3) we took theseprinciples and introduced the OPL database commands:• POSITION, NEXT, BACK, FIRST, LAST – used to move the databasecursor around a database• USE – switch between open databases• INSERT – insert a new blank record at the end of a database• MODIFY – change the contents of a record in a database• ERASE – remove a record from a database.Using these, and the idea of a cursor from Chapter 5, we looked atcreating a Notepad style application that would be saved to disk so it isavailable when you open it again.7Publishing your OPL ApplicationOPL applications tend to be written for three main purposes.
The firstwill be as a handy tool for yourself to use, in which case once you havethe code working, you can quite happily carry on using it as is. Thesecond will be if you are programming an application for use inside yourown business environment – once it is coded, tested, and deployed toyour users, you can move on to the next project. But a large number ofOPL applications will be released on the Internet, some of them as freelyavailable tools for other Symbian OS phone users, some as sharewareapplications, and some as commercial applications. In this chapter, we’regoing to look at the difference between these terms and how you can goabout marketing your OPL applications to other Symbian OS users.7.1 Types of ApplicationThere are basically four ways you can make your OPL applicationavailable.1.
Open Source: You make the source code to your application availableto other people who can then modify it or use it as an example fortheir own programs. There are various ways to do this and these comewith too many intricacies to cover here – a quick Google search for‘open source’ should give you lots of advice.2. Freeware: These are full applications that do not require any paymentto the author for usage. Freeware exists for a variety of reasons – forexample, there is no longer any support from the author, the producthas limited appeal, or the author is simply being philanthropic! Asthe author, you retain the copyright to the product and ownership ofit, as well as the source code, but you make the binaries available,for free, for other people to use.3.
Shareware: This is the traditional ‘try before you buy’ concept, whereyou (as the author) provide a limited version of your application114PUBLISHING YOUR OPL APPLICATIONto anyone who wants it. This is the working demonstration versionwhich you should aim to make available to as many people aspossible. What this version should do is make the user want tocontinue using it (or get access to more functionality).