GPS4Palm

Source Code Documentation


RouteEditForm.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002  *
00003  * $RCSfile: RouteEditForm_8c-source.html,v $
00004  *
00005  * GPS4Palm Route Edit Form
00006  *
00007  * This program is Copyright (C) 03/2003 Matthias Prinke
00008  * <matthias.prinke@surfeu.de> and covered by GNU's GPL.
00009  * In particular, this program is free software and comes WITHOUT
00010  * ANY WARRANTY.
00011  *
00012  * $Author: mp $
00013  *
00014  * $Date: 2007-10-08 20:40:34 $
00015  *
00016  * $Revision: 1.7.2.1 $
00017  *
00018  * $Log: RouteEditForm_8c-source.html,v $
00018  * Revision 1.7.2.1  2007-10-08 20:40:34  mp
00018  * updated for gps4palm V0.9.5 beta
00018  *
00019  * Revision 1.9  2004-12-10 19:58:25  mp
00020  * implemented clipboard, SysKeyboardDialog, and SysGraffitiReferenceDialog
00021  *
00022  * Revision 1.8  2004/11/27 10:16:57  mp
00023  * replaced ErrFatalDisplay() by Die() for memory allocation failure
00024  *
00025  * Revision 1.7  2004/11/24 21:21:42  mp
00026  * moved static function declarations from header to implementation file
00027  *
00028  * Revision 1.6  2004/11/23 17:48:08  mp
00029  * removed unused variables
00030  *
00031  * Revision 1.5  2004/11/16 20:47:30  mp
00032  * added check after creating new record, modified PackWaypoint() call
00033  *
00034  * Revision 1.4  2004/11/16 18:46:28  mp
00035  * fixed route editing, changed waypoint add function from append to insert
00036  *
00037  * Revision 1.3  2004/11/15 19:56:51  mp
00038  * create_route()/store_route() rewritten,
00039  * buttons "ok" and "cancel" replaced by "done" (better PalmOS UI compliancy),
00040  * implemented route waypoint editing: add, delete, move up, move down
00041  *
00042  * Revision 1.2  2004/11/14 22:45:45  mp
00043  * added RouteEditAllTable and RouteEditSelTable functions
00044  *
00045  * Revision 1.1  2004/11/13 15:55:17  mp
00046  * initial version
00047  *
00048  *
00049  ****************************************************************************/
00050 #include <PalmOS.h>
00051 #include "ResourceDefines.h"
00052 #include <Unix/sys_types.h>
00053 #include "stringil.h"
00054 #include "Data.h"
00055 #include "RouteEditForm.h"
00056 #include "GPS.h"                /*  ReadFromGPS() */
00057 #include "Serial.h"
00058 #include "Utils.h"
00059 #include "common.h"
00060 
00061 
00062 /* Global Variables */
00063 
00064 extern PrefsType        gPrefs;         /* Preferences data structure */
00065 extern DmOpenRef        gWaypointDB;    /* Waypoint Data Base reference */
00066 extern DmOpenRef        gRouteDB;       /* Route Data Base reference */
00067 extern GPSType          gGPSData;       /* GPS Data */
00068 extern ActRouteType     gSelRoute;      /* Selected route */
00069 
00070 
00071 /* Static Functions */
00072 static void RouteEditFormInit(void)                     ROUTEEDIT_SECTION;
00073 static Err create_route(route_t *route)                 ROUTEEDIT_SECTION;
00074 static void store_route(route_t *route)                 ROUTEEDIT_SECTION;
00075 
00076 
00077 /*****************************************************************************
00078  * FUNCTION:    RouteEditFormInit
00079  *
00080  * DESCRIPTION: Route Edit Form Init Function (not used)
00081  *
00082  ****************************************************************************/
00083 static 
00084 void RouteEditFormInit(void)
00085 {  
00086 
00087 }
00088 
00089 
00090 /*****************************************************************************
00091  * FUNCTION:    TableDrawWptCell
00092  *
00093  * DESCRIPTION: Custom table cell drawing function for Waypoint ID
00094  *              (max. 6 characters). The DB record number is
00095  *              retrieved via TblGetRowData().
00096  *
00097  * GLOBALS:     gWayptDB        -- waypoint DB reference
00098  *              gRouteDB        -- route DB reference
00099  *
00100  * PARAMETERS:  Note: The function's prototype is defined by PalmOS
00101  *                    (TableDrawItemFuncType).
00102  *
00103  * RETURNED:    -
00104  ****************************************************************************/
00105 static void TableDrawWaypointIdCell(void *tblP, Int16 row, Int16 column,
00106                    RectangleType *bounds)
00107 {
00108   FormPtr               frmP;                   /* form ptr */
00109   UInt16                tbl_id;                 /* table object ID */
00110   Char                  str[7];                 /* string buffer */
00111   route_t               *route;                 /* route */
00112   waypoint_t            waypoint;               /* unpacked waypoint */
00113   packed_waypoint_t     *packed_waypoint;       /* packed waypoint (ptr) */
00114   MemHandle             routeDBEntry;           /* record handle */
00115   MemHandle             waypointDBEntry;        /* record handle */
00116   UInt16                rec;                    /* record index */
00117   UInt16                index;                  /* waypoint index */
00118   Err                   err;                    /* error code */
00119   
00120   frmP = FrmGetActiveForm();
00121   FntSetFont(stdFont);
00122   MemSet((char *)str, 7, 0);
00123   
00124   /* get object ID of current table */
00125   tbl_id = FrmGetObjectId(frmP, MyFrmGetObjectIndexFromPtr(frmP, tblP));
00126     
00127   /* erase table cell */
00128   WinEraseRectangle(bounds, 0);
00129   
00130   /* check if row is usable */
00131   if (TblRowUsable(tblP, row)) {
00132   
00133     if (tbl_id == RouteEditAllTable) {
00134       /* All available waypoints */
00135 
00136       /* get record index from row data */
00137       rec = (UInt16)TblGetRowData(tblP, row);
00138       
00139       waypointDBEntry = DmQueryRecord(gWaypointDB, rec);
00140       packed_waypoint = (packed_waypoint_t *)MemHandleLock(waypointDBEntry);
00141       UnpackWaypoint(&waypoint, packed_waypoint);
00142       StrNCopy(str, (Char *)&waypoint.ident, 6);
00143       MemHandleUnlock(waypointDBEntry);
00144  
00145     } else {
00146       /* Waypoints of selected route */
00147    
00148       /* get waypoint index from row data */
00149       index = (UInt16)TblGetRowData(tblP, row);
00150 
00151       err = DmFindRecordByID(gRouteDB,
00152         gSelRoute.routeID, &rec);
00153       
00154       routeDBEntry = DmQueryRecord(gRouteDB, rec);
00155             
00156       /* lock route record */
00157       route = (route_t *)MemHandleLock(routeDBEntry);
00158 
00159       /* get waypoint record ID from route record */
00160       err = DmFindRecordByID(gWaypointDB,
00161         route->wpt_rec_id[index], &rec);
00162 
00163       /* unlock route record */
00164       MemHandleUnlock(routeDBEntry);
00165 
00166       if (!err) {
00167 
00168         /* get waypoint record */
00169         waypointDBEntry = DmQueryNextInCategory(gWaypointDB, &rec, 
00170           dmAllCategories);
00171 
00172         /* lock waypoint record */
00173         packed_waypoint = (packed_waypoint_t *)MemHandleLock(waypointDBEntry);
00174 
00175         /* unpack waypoint record */
00176         UnpackWaypoint(&waypoint, packed_waypoint);
00177 
00178         /* Copy Identifier */
00179         StrNCopy(str, waypoint.ident, 6);
00180 
00181         /* unlock waypoint record */
00182         MemHandleUnlock(waypointDBEntry);
00183       } else {
00184         /* debugging only, we should never go here */
00185         StrCopy(str, "???");
00186       }
00187     }
00188     
00189     /* Write to table cell */
00190     WinDrawChars(str, StrLen(str), bounds->topLeft.x + 1, bounds->topLeft.y);
00191   } /* if (TblRowUsable(tblP, row)) */
00192 }
00193 
00194 
00195 /****************************************************************************/
00196 /**
00197  * \brief       All waypoints/selected waypoints table drawing function
00198  *
00199  * \param       tbl_id          table id
00200  * \param       top             top table row
00201  * \param       sel             ptr to selected table row, may be NULL if
00202  *                                selection/scrollbar is handled elsewhere
00203  *
00204  *****************************************************************************/
00205 static
00206 void routeEditDrawTable(UInt16 tbl_id, Int32 top, UInt16 *sel)
00207 {
00208   TablePtr              tblP;                   /* table ptr */
00209   FormPtr               frmP;                   /* form ptr */
00210   ScrollBarPtr          sclP;                   /* scrollbar ptr */
00211   UInt16                row;                    /* table row counter */
00212   UInt16                rec;                    /* record nr */
00213   UInt16                max_rows = 0;           /* total nr. of routes */
00214   UInt16                numRows;                /* number of visible rows */
00215   MemHandle             routeDBEntry;           /* record handle */
00216   route_t               *route;                 /* route */
00217   MemHandle             waypointDBEntry;        /* record handle */
00218   Err                   err;                    /* error code */
00219   
00220   frmP = FrmGetActiveForm();
00221   tblP = GetObjectFromForm(frmP, tbl_id);
00222   
00223   if (tbl_id == RouteEditAllTable) {
00224     sclP = GetObjectFromForm(frmP, RouteEditAllScrl);
00225   
00226     /* total number of waypoints */
00227     max_rows = DmNumRecordsInCategory(gWaypointDB, dmAllCategories);
00228   
00229   } else {
00230     sclP = GetObjectFromForm(frmP, RouteEditSelScrl);
00231   
00232     /* get number of waypoints on this route */
00233     err = DmFindRecordByID(gRouteDB, gSelRoute.routeID,
00234       &rec);
00235 
00236     if (!err) {
00237       /* selected route found */
00238       routeDBEntry = DmQueryRecord(gRouteDB, rec);    
00239       
00240       /* lock record */
00241       route = (route_t *)MemHandleLock(routeDBEntry);
00242 
00243       max_rows = route->items;
00244       
00245       /* unlock record */
00246       MemHandleUnlock(routeDBEntry);
00247       
00248     } /* if (!err) */
00249   } /* if (tbl_id == RouteEditAllTable) */
00250   
00251   /* number of visible rows */
00252   numRows = TblGetNumberOfRows(tblP);
00253 
00254   /* set columns usable */
00255   TblSetColumnUsable(tblP, 0, true);
00256       
00257   
00258   if (tbl_id == RouteEditAllTable) {
00259     /* set row data to Waypoint DB entries (record index) */
00260     for (row = 0; row < numRows; row++) {
00261 
00262       /* default: mark row as not usable (empty or no valid record) */
00263       TblSetRowUsable(tblP, row, false);
00264 
00265       if (row + top < max_rows) {
00266         /* seek from beginning of database */
00267         rec = 0;
00268 
00269         /* get next record */
00270         err = DmSeekRecordInCategory(gWaypointDB, &rec /* index */,
00271           top + row /* offset */, dmSeekForward, dmAllCategories);
00272 
00273         if (err == errNone) {
00274           /* query record */
00275           waypointDBEntry = DmQueryRecord(gWaypointDB, rec);
00276 
00277           if (waypointDBEntry) {
00278             /*
00279              * Record is valid, set row data to index and
00280              * mark table row as usable.
00281              */
00282             TblSetRowData(tblP, row, rec);
00283             TblSetRowUsable(tblP, row, true);
00284           }
00285         }
00286       } /*  if (row + top < max_rows) */
00287     } /* for (row = 0; row < numRows; row++) */  
00288   
00289   } else {
00290     /* (tbl_id == RouteEditSelTable) */
00291     
00292     /* set row data to Waypoint DB entries (record index) */
00293     for (row = 0; row < numRows; row++) {
00294       /* default: mark row as not usable (empty or no valid record) */
00295       TblSetRowUsable(tblP, row, false);
00296 
00297       if (row + top < max_rows) {
00298         TblSetRowData(tblP, row, row + top);
00299         TblSetRowUsable(tblP, row, true);
00300       }
00301     }
00302   }
00303   
00304   /* initialize table */
00305   for (row = 0; row < numRows; row++) {
00306     
00307     if (row + top < max_rows) {
00308       /* set all rows selectable */
00309       TblSetRowSelectable(tblP, row, true);
00310       
00311       /* set columns to custom style, set drawing functions */
00312       TblSetItemStyle(tblP, row, 0, customTableItem);    
00313       TblSetCustomDrawProcedure(tblP, 0, TableDrawWaypointIdCell);
00314        
00315       /* mark all rows as invalid to trigger table update by FrmDrawForm() */
00316       TblMarkRowInvalid(tblP, row);
00317     }
00318   }
00319 
00320   /* set scrollbar: value, min, max, pagesize */
00321   if (max_rows <= numRows) {
00322     SclSetScrollBar(sclP, 0, 0, 0, numRows);
00323   } else {
00324     SclSetScrollBar(sclP, top, 0, max_rows - numRows, numRows);
00325   }
00326   
00327   /*
00328    * Selected route scolled below visible table rows,
00329    *   selection is switched to last visible entry.
00330    */
00331   if ( *sel > (top + numRows - 1) ) {
00332     *sel = top + numRows - 1;
00333   }
00334 
00335   /*
00336    * Selected route scolled above visible table rows,
00337    *   selection is switched to first visible entry.
00338    */  
00339   if (*sel < top) {
00340     *sel = top;
00341   }
00342 
00343   for (row = 0; row < numRows; row++, top++) {
00344     if ((top < max_rows) && (top == *sel)) {
00345       TblSelectItem(tblP, row, 0);
00346       break;
00347     }
00348   }
00349   
00350   FrmDrawForm(frmP);
00351 }
00352 
00353 
00354 /*****************************************************************************
00355  * FUNCTION:    create_route
00356  *
00357  * DESCRIPTION: Add new route to database
00358  *
00359  * GLOBALS:     gRouteDB        -- Route Database Reference
00360  *              gSelRoute       -- return new routeID
00361  *
00362  * PARAMETERS:  route           -- ptr to route record
00363  *              
00364  *
00365  * RETURNED:    false if no unused route number exists
00366  ****************************************************************************/
00367 static
00368 Err create_route(route_t *route)
00369 {
00370   UInt16        nmbr;                   /* route number */
00371   UInt16        index;                  /* record index */
00372   MemHandle     rteH;                   /* route record handle */
00373   MemHandle     routeDBEntry;           /* route record handle */
00374   route_t       *rteP;                  /* route record ptr */
00375   Boolean       rte_exists = false;     /* flag: route exists */
00376 
00377   /* init route comment */
00378   MemSet((Char *)route->cmnt, 21, 0);
00379     
00380   /* clear number of items (waypoints) */
00381   route->items = 0;
00382   
00383   for (nmbr=0; nmbr <= 255; nmbr++) {
00384     route->nmbr = (G_byte)nmbr;
00385     
00386     /* Find sort position of new record in route DB */
00387     index = DmFindSortPosition(gRouteDB, route, 0 /* newRecordInfo */,
00388       (DmComparF *)CompareRteNmbr /* DmComparF *compar */, 0 /* other */);
00389 
00390     if (index > 0) {
00391       /* check if route with same number already exists */
00392       
00393       rteH = DmQueryRecord(gRouteDB, index - 1);
00394       rteP = MemHandleLock(rteH);
00395       if (route->nmbr == rteP->nmbr) {
00396         MemHandleUnlock(rteH);
00397         rte_exists = true;
00398       } else {
00399         MemHandleUnlock(rteH);
00400         rte_exists = false;
00401         break;
00402       }
00403     } else {
00404       rte_exists = false;
00405       break;
00406     } /* if (index > 0) */
00407   } /* for (nmbr=0; nmbr <= 255; nmbr++) */
00408            
00409   if (rte_exists) {
00410     /* failure */
00411     gSelRoute.valid = false;
00412     return 1;
00413   } else {    
00414     /* success */
00415 
00416     /* create comment */
00417     StrPrintF(route->cmnt, "<Route #%d>", route->nmbr);  
00418     
00419     /* create new route record */
00420     routeDBEntry = DmNewRecord(gRouteDB, &index, 
00421       1 /* size, will be set by PackRoute() */);
00422 
00423     if (routeDBEntry) {
00424       DmReleaseRecord(gRouteDB, index, false /* dirty */);
00425     } else {
00426       Die("Cannot create route record!");
00427     }
00428 
00429     /* Store Route Header in Database */
00430     PackRouteHdr(route, index);    
00431     
00432     /* get unique route ID */
00433     DmRecordInfo(gRouteDB, index, NULL /* *attrP */,
00434       &gSelRoute.routeID /* *uniqueIDP */, NULL /* *chunkIDP */);
00435     
00436     gSelRoute.valid = true;
00437     return 0;
00438   }    
00439 }
00440 
00441 
00442 /*****************************************************************************
00443  * FUNCTION:    store_route
00444  *
00445  * DESCRIPTION: Add new route to database
00446  *
00447  * GLOBALS:     gRouteDB        -- Route Database Reference
00448  *              gSelRoute       -- Selected Route (unique ID)
00449  *
00450  * PARAMETERS:  route           -- Route Ptr
00451  *              
00452  *
00453  * RETURNED:    %
00454  ****************************************************************************/
00455 static
00456 void store_route(route_t *route)
00457 {
00458   FormPtr               frmP;
00459   Char                  *strP;
00460   UInt16                len;
00461   UInt16                rec;
00462   Err                   err;
00463 
00464   frmP = FrmGetActiveForm();
00465   
00466   /* Copy Route Comment from Field */
00467   strP = FldGetTextPtr(FrmGetObjectPtr(frmP,
00468     FrmGetObjectIndex(frmP, RouteCmntField)));
00469   len = StrLen(strP);
00470   MemSet((Char *)route->cmnt, 21, 0);
00471   StrNCopy(route->cmnt, strP, (len > 20) ? 20 : len);
00472   
00473   err = DmFindRecordByID(gRouteDB, gSelRoute.routeID,
00474     &rec);
00475 
00476   if (!err) {
00477     /* selected route found */
00478 
00479     /* Store Route Header in Database */
00480     PackRouteHdr(route, rec);
00481   }  
00482 }
00483 
00484 
00485 /*****************************************************************************
00486  * FUNCTION:    RouteEditFormHandleEvent
00487  *
00488  * DESCRIPTION: Route Edit Form Event Handler
00489  *
00490  * GLOBALS:     gPrefs          -- Application Preferences
00491  *              gRouteDB        -- Route Data Base reference
00492  *              gSelRoute       -- Selected or new route
00493  *
00494  * PARAMETERS:  eventP          -- pointer to event structure
00495  *
00496  * RETURNED:    Status flag: event handled
00497  ****************************************************************************/
00498 Boolean RouteEditFormHandleEvent(EventPtr eventP)
00499 {
00500   Boolean               handled;                /* flag: event handled */
00501   FormPtr               frmP;                   /* form ptr */
00502   TablePtr              tblP;                   /* table ptr */
00503   ScrollBarPtr          sclP;                   /* scrollbar ptr */
00504   FieldPtr              fldP;                   /* field ptr */
00505   FieldAttrType         fld_attr;               /* field attributes */
00506   route_t               *routeP;                /* route ptr */
00507   MemHandle             routeDBEntry;           /* route record handle */
00508   UInt16                rec;                    /* record index */
00509   Char                  tmp[7];                 /* temp. string buffer */
00510   UInt16                nr_waypoints;           /* total number of waypoints */
00511   UInt16                index;                  /* waypoint index */
00512   UInt32                wpt_rec_id;             /* waypoint record ID */
00513   Err                   err;                    /* error code */
00514   static route_t        route;                  /* route */
00515   static Int32          waypt_top = 0;          /* all waypoints table top */
00516   static Int32          route_top = 0;          /* route waypoints table top */
00517   static UInt16         waypt_sel = 0;          /* all waypoints selected row */
00518   static UInt16         route_sel = 0;          /* route waypoints sel. row */
00519 
00520   frmP = FrmGetActiveForm();
00521   handled = false;
00522 
00523   /* get no. of all waypoints */
00524   nr_waypoints = DmNumRecordsInCategory(gWaypointDB, dmAllCategories);
00525 
00526   
00527   switch (eventP->eType) {
00528     case frmOpenEvent:
00529       RouteEditFormInit();
00530       FrmDrawForm(frmP);
00531 
00532       /* initialize table view and selection */
00533       waypt_top = 0;
00534       route_top = 0;
00535       waypt_sel = 0;
00536       route_sel = 0;
00537 
00538       if (!gSelRoute.valid) {
00539         /* RouteEditForm opened with button "new" */
00540         
00541         /* create new route */
00542         err = create_route(&route);
00543         
00544         if (err) {
00545           /* no route number available */
00546           FrmAlert(RouteCreateAlert);
00547           FrmGotoForm(RouteForm);
00548           handled = true;
00549           break;
00550         }
00551         
00552       }
00553 
00554       /* write route data to fields */
00555       
00556       /* find route record */
00557       err = DmFindRecordByID(gRouteDB, gSelRoute.routeID,
00558         &rec);
00559         
00560       if (!err) {       
00561         /* selected or new route found */
00562         routeDBEntry = DmQueryRecord(gRouteDB, rec);
00563         
00564         /* lock record */
00565         routeP = (route_t *)MemHandleLock(routeDBEntry);
00566           
00567         /* copy route number from record */
00568         MemSet((Char *)tmp, 7, 0);
00569         StrPrintF(tmp, "%d", routeP->nmbr);
00570         SetFieldText(RouteNmbrField, tmp, false, true);
00571         route.nmbr = routeP->nmbr;
00572           
00573         /* copy comment from record */
00574         SetFieldText(RouteCmntField, routeP->cmnt, false, true);
00575         StrNCopy(route.cmnt, routeP->cmnt, 20);
00576           
00577         /* copy number of items from record */
00578         route.items = routeP->items;
00579           
00580         /* unlock record */
00581         MemHandleUnlock(routeDBEntry);
00582       } /* if (!err) */
00583       
00584       /* draw tables */
00585       routeEditDrawTable(RouteEditAllTable, waypt_top, &waypt_sel);
00586       routeEditDrawTable(RouteEditSelTable, route_top, &route_sel);
00587       
00588       handled = true;
00589       break;
00590 
00591     case menuEvent:    
00592       /* check if RouteCmntField has focus */
00593       fldP = (FieldPtr)GetObjectFromActiveForm(RouteCmntField);
00594       FldGetAttributes(fldP, &fld_attr);
00595       
00596       switch (eventP->data.menu.itemID) {
00597 
00598         case UndoMenu:
00599           if (fld_attr.hasFocus == 1)
00600             FldUndo(fldP);
00601           handled = true;
00602           break;
00603           
00604         case CutMenu:
00605           if (fld_attr.hasFocus == 1)
00606             FldCut(fldP);
00607           handled = true;
00608           break;
00609           
00610         case CopyMenu:
00611           if (fld_attr.hasFocus == 1)
00612             FldCopy(fldP);
00613           handled = true;
00614           break;
00615           
00616         case PasteMenu:
00617           if (fld_attr.hasFocus == 1)
00618             FldPaste(fldP);
00619           handled = true;
00620           break;
00621           
00622         case KeyboardMenu:
00623           if (fld_attr.hasFocus == 1)
00624             SysKeyboardDialog(kbdAlpha);
00625           handled = true;
00626           break;
00627           
00628         case GraffitiMenu:
00629           SysGraffitiReferenceDialog(referenceDefault);
00630           handled = true;
00631           break;
00632           
00633       } /* switch (eventP->data.menu.itemID) */
00634       break;
00635   
00636   return (fld_attr.hasFocus == 1);  
00637     case tblSelectEvent:
00638       /* update table row selection */
00639       {
00640         UInt16 id = eventP->data.tblSelect.tableID;
00641         
00642         if (id == RouteEditAllTable) {
00643           waypt_sel = eventP->data.tblSelect.row + waypt_top;
00644         } else {
00645           route_sel = eventP->data.tblSelect.row + route_top;
00646         }
00647       }
00648       handled = true;
00649       break;
00650 
00651       
00652     case sclRepeatEvent:
00653       /* handle scroll enevt */
00654       {
00655         UInt16 id = eventP->data.sclRepeat.scrollBarID;
00656         
00657         if (id == RouteEditAllScrl) {
00658           Doscroll(RouteEditAllTable, id, nr_waypoints, &waypt_top);
00659           routeEditDrawTable(RouteEditAllTable, waypt_top, &waypt_sel);
00660         } else {
00661           DmFindRecordByID(gRouteDB,
00662             gSelRoute.routeID, &rec);
00663       
00664           routeDBEntry = DmQueryRecord(gRouteDB, rec);
00665             
00666           /* lock route record */
00667           routeP = (route_t *)MemHandleLock(routeDBEntry);
00668 
00669           Doscroll(RouteEditSelTable, id, routeP->items, &route_top);
00670           
00671           /* unlock route record */
00672           MemHandleUnlock(routeDBEntry);
00673         
00674           routeEditDrawTable(RouteEditSelTable, route_top, &route_sel);
00675         }
00676       }
00677       
00678       /* Note: scrollbar needs to handle the event, too */
00679       handled = false;
00680       break;
00681       
00682 
00683     case keyDownEvent:
00684       /* "virtual" up/down keys trigger paging of "All Waypoints" table */
00685       switch (eventP->data.keyDown.chr) {
00686         case pageUpChr:
00687         case pageDownChr:
00688           Dopage(RouteEditAllTable,
00689             (eventP->data.keyDown.chr == pageDownChr),
00690             nr_waypoints, &waypt_top);
00691           routeEditDrawTable(RouteEditAllTable, waypt_top, &waypt_sel);
00692       
00693           /* Note: scrollbar needs to handle the event, too */
00694           handled = false;
00695           break;
00696       }
00697       break;
00698 
00699     
00700     case ctlSelectEvent:
00701       switch (eventP->data.ctlSelect.controlID) {
00702         case RouteEditAddButton:
00703           /* add waypoint to route (after selection) */
00704           
00705           /* get ptr to waypoint table */
00706           tblP = (TablePtr)GetObjectFromActiveForm(RouteEditAllTable);
00707           
00708           /* get waypoint record index from row data */
00709           rec = (UInt16)TblGetRowData(tblP, waypt_sel-waypt_top);
00710           
00711           /* get unique waypoint ID */
00712           DmRecordInfo(gWaypointDB, rec, NULL /* *attrP */,
00713             &wpt_rec_id /* *uniqueIDP */, NULL /* *chunkIDP */);
00714           
00715           /* get route record index from unique ID */ 
00716           DmFindRecordByID(gRouteDB, gSelRoute.routeID, &rec);
00717           
00718           /* get ptr to route table */
00719           tblP = (TablePtr)GetObjectFromActiveForm(RouteEditSelTable);
00720           
00721           /* get waypoint ID index from row data */
00722           index = (UInt16)TblGetRowData(tblP, route_sel-route_top);
00723                           
00724           /* add selected waypoint to current route */
00725           InsertRouteWpt(rec, wpt_rec_id, index);
00726           
00727           /* increment waypoint count */
00728           route.items++;
00729 
00730           /* 
00731            * Redraw route table, select new Waypoint 
00732            * and make sure it is visible.
00733            */
00734           /* select new waypoint */
00735           if (route.items > 1)
00736             route_sel++;
00737           
00738           /* set top table row to ensure selected item is visible */
00739           if (route_sel-route_top > TblGetNumberOfRows(tblP)-1) {
00740             route_top++;            
00741           }
00742           
00743           /* update scroll bar */
00744           sclP = (ScrollBarPtr)GetObjectFromActiveForm(RouteEditSelScrl);
00745           SclSetScrollBar(sclP, route_sel /* value */,
00746             0 /* min */, route.items-1 /* max */,
00747             TblGetNumberOfRows(tblP) /* page size */);  
00748           
00749           /* redraw table */
00750           routeEditDrawTable(RouteEditSelTable, route_top, &route_sel);
00751           
00752           handled = true;
00753           break;
00754 
00755           
00756         case RouteEditUpButton:
00757         case RouteEditDnButton:
00758           /* move waypoint up/down in route */
00759           if (route.items == 0)
00760             break;
00761                   
00762           /* get ptr to route table */
00763           tblP = (TablePtr)GetObjectFromActiveForm(RouteEditSelTable);
00764           
00765           /* get waypoint ID index from row data */
00766           index = (UInt16)TblGetRowData(tblP, route_sel-route_top);
00767           
00768           /* get route record index from unique ID */ 
00769           DmFindRecordByID(gRouteDB, gSelRoute.routeID, &rec);
00770           
00771           if (eventP->data.ctlSelect.controlID == RouteEditUpButton) {
00772             /* move up */
00773             if (route_sel == 0)
00774               break;
00775             MoveRouteWpt(rec, index, true /* up */);
00776             route_sel--;
00777             
00778           } else {
00779             /* move down */
00780             if (route_sel == route.items-1)
00781               break;
00782             MoveRouteWpt(rec, index, false /* up */);
00783             route_sel++;
00784           }
00785 
00786           /* set top table row to ensure selected item is visible */
00787           if (route_sel-route_top > TblGetNumberOfRows(tblP)-1) {
00788             route_top++;            
00789           }
00790           
00791           if (route_sel-route_top < 0) {
00792             route_top--;
00793           }
00794           
00795           /* update scroll bar */
00796           sclP = (ScrollBarPtr)GetObjectFromActiveForm(RouteEditSelScrl);
00797           SclSetScrollBar(sclP, route_sel /* value */,
00798             0 /* min */, route.items-1 /* max */,
00799             TblGetNumberOfRows(tblP) /* page size */);  
00800           
00801           /* redraw table */
00802           routeEditDrawTable(RouteEditSelTable, route_top, &route_sel);
00803           
00804           handled = true;
00805           break;
00806         
00807         
00808         case RouteEditDelButton:
00809           /* delete waypoint from route */
00810           if (route.items == 0)
00811             break;
00812           
00813           /* get ptr to route table */
00814           tblP = (TablePtr)GetObjectFromActiveForm(RouteEditSelTable);
00815           
00816           /* get waypoint ID index from row data */
00817           index = (UInt16)TblGetRowData(tblP, route_sel-route_top);
00818 
00819           /* get route record index from unique ID */ 
00820           DmFindRecordByID(gRouteDB, gSelRoute.routeID, &rec);
00821           
00822           /* delete selected waypoint */
00823           DelRouteWpt(rec, index);
00824           
00825           route.items--;
00826           
00827           /* 
00828            * Redraw route table, select previous Waypoint
00829            * of the route and make sure it is visible.
00830            */
00831           if (route_sel > 0) {
00832             route_sel--;
00833           }
00834           
00835           Doscroll(RouteEditSelTable, RouteEditSelScrl, route.items, &route_top);
00836           
00837           /* redraw table */
00838           routeEditDrawTable(RouteEditSelTable, route_top, &route_sel);
00839 
00840           /* update selection */
00841           TblSelectItem(tblP, route_sel-route_top, 0);
00842           handled = true;
00843           break;          
00844         
00845         
00846         case RouteEditDoneButton:
00847           /* close form */
00848           FrmGotoForm(RouteForm);
00849           handled = true;
00850           break;
00851           
00852       }
00853       break;
00854 
00855 
00856     case frmCloseEvent:
00857       /* store route header (Comment Field!) before closing form */
00858       if (gSelRoute.valid)
00859         store_route(&route);
00860     
00861       /* Deallocate Fields' Text Memory */
00862       SetFieldText(RouteNmbrField, NULL, false, true);
00863       SetFieldText(RouteCmntField, NULL, false, true);
00864       break;
00865   }
00866   
00867   return(handled);
00868 } /* RouteEditFormHandleEvent() */

Created: Mon, 08 Oct 2007 22:33:16 +0200
Copyright ©2004 M. Prinke