pagesize.cpp

Go to the documentation of this file.
00001 // $Id: pagesize.cpp 1668 2006-08-04 11:45:17Z alex $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 //
00099 // Encapsulates the page sizes available in Camelot.
00100 
00101 /*
00102 */
00103 
00104 #include "camtypes.h"
00105 #include "pagesize.h"
00106 //#include "errors.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00107 //#include "units.h"        // units - in camtypes.h [AUTOMATICALLY REMOVED]
00108 //#include "convert.h"  // conversion utilities - in camtypes.h [AUTOMATICALLY REMOVED]
00109 
00110 //#include "will2.h"
00111 
00112 //#include "ccfile.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00113 #include "oilfiles.h"
00114 //#include "nev.h"
00115 #include <limits.h>     // INT32_MAX 
00116 //#include "pagesres.h" // page related error messages and resources
00117 
00118 // This is not compulsory, but you may as well put it in so that the correct version
00119 // of your file can be registered in the .exe
00120 DECLARE_SOURCE("$Revision: 1668 $");
00121 
00122 // An implement to match the Declare in the .h file.
00123 // If you have many classes, it is recommended to place them all together, here at the start of the file
00124 CC_IMPLEMENT_DYNAMIC(PageSize, ListItem)
00125 CC_IMPLEMENT_DYNAMIC(PageSizesList, List)
00126 
00127 // This will get Camelot to display the filename and linenumber of any memory allocations
00128 // that are not released at program exit
00129 #define new CAM_DEBUG_NEW
00130 
00131 //List PageSize::PageSizeList;          // The global list of installed page sizes.
00132 
00133 
00134 enum PsTokenIndex
00135 {
00136     PsTOKEN_NONE = -1,
00137     TOKEN_PAGESIZE,
00138     TOKEN_PAGESIZEEND,
00139     TOKEN_MILLIMETRES,
00140     TOKEN_INCHES,
00141     // Add new token indexs BEFORE NUM_TOKENS
00142     PsNUM_TOKENS
00143 };
00144 
00145 static struct
00146 {
00147     TCHAR*  Token;
00148 } TokenTable[] = 
00149 {
00150     {_T("PageSize")},
00151     {_T("PageSizeEnd")},
00152     {_T("MILLIMETRES")},
00153     {_T("INCHES")}
00154 };
00155 
00156 
00157 static PsTokenIndex FindToken(const TCHAR* Token)
00158 {
00159     for (INT32 i = 0; i < PsNUM_TOKENS; i++)
00160         if (camStrcmp(TokenTable[i].Token,Token) == 0) return  (PsTokenIndex) i;
00161 
00162     return PsTOKEN_NONE;
00163 }
00164 
00165 /********************************************************************************************
00166 
00167 >   PageSize::PageSize()
00168 
00169     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00170     Created:    12/12/94
00171     Purpose:    Constructor.
00172     Errors:
00173     SeeAlso:    AppPrefsDlg;
00174 
00175 ********************************************************************************************/
00176 
00177 PageSize::PageSize()
00178 {
00179     // Do nothing
00180 }
00181 
00182 /********************************************************************************************
00183 
00184 >   BOOL PageSize::Init(PageId NewId, String_32 *Name, double Width, double Height,
00185                     UnitType Units)
00186 
00187     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00188     Created:    12/12/94
00189     Inputs:     NewId   unique page id for this page size
00190                 Name    textual name for this page (for menus etc.)
00191                 Width   width in the specified units e.g. millimetres
00192                 Height  height in the specified units
00193                 Units   the specified units
00194     Purpose:    Initialiser.
00195     Errors:
00196     SeeAlso:    PageSize; AppPrefsDlg;
00197 
00198 ********************************************************************************************/
00199 
00200 BOOL PageSize::Init(PageId NewId, String_32 *Name, double Width, double Height,
00201                     UnitType Units)
00202 {
00203     // Guard against floating point exceptions by checking the input values
00204     // 0.1" or 0.1mm should be a good enough minimim
00205     ERROR2IF(Width < 0.1 || Width > (double)INT32_MAX, FALSE, "PageSize::Init bad width");
00206     ERROR2IF(Height < 0.1 || Height > (double)INT32_MAX, FALSE, "PageSize::Init bad width");
00207 
00208     Id = NewId;                 // Copy the page id across
00209     PageName = *Name;           // Copy the page name across 
00210     PageWidthInMM = Width;      // Copy the width across
00211     PageHeightInMM = Height;    // Copy the height across
00212     
00213     // Now work out the equivalent millipoint values
00214     // Cannot use the ConvertToNewUnits functions as these require a current doc
00215     // and we have not got one yet! Do it the hard way but use the conversion values
00216     // specified in units.h
00217     switch (Units)
00218     {
00219         case MILLIMETRES:
00220             PageWidth = (INT32)(Width * MM_MP_VAL);
00221             PageHeight = (INT32)(Height * MM_MP_VAL);
00222             break;
00223         case INCHES:
00224             PageWidth = (INT32)(Width * IN_MP_VAL);
00225             PageHeight = (INT32)(Height * IN_MP_VAL);
00226             break;
00227         default:
00228             ERROR2(FALSE,"Bad units used in PageSize::Init");
00229             break;
00230     }
00231     //PageWidth = ConvertToNewUnits(Width, MILLIMETRES, MILLIPOINTS);
00232     //PageHeight = ConvertToNewUnits(Height, MILLIMETRES, MILLIPOINTS);
00233     
00234     return TRUE;
00235 }
00236 
00237 
00238 /********************************************************************************************
00239 
00240 >   PageId PageSize::GetPageId()
00241 
00242     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00243     Created:    12/12/94
00244     Returns:    The id for this page
00245     Purpose:    Returns page id
00246     SeeAlso:    PageSize; AppPrefsDlg;
00247 
00248 ********************************************************************************************/
00249 PageId PageSize::GetPageId()
00250 {
00251     return Id;
00252 }
00253 
00254 /********************************************************************************************
00255 
00256 >   String_32 *PageSize::GetPageName()
00257 
00258     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00259     Created:    12/12/94
00260     Returns:    pointer to the name of this page
00261     Purpose:    Way of finding the name for this page.
00262     SeeAlso:    PageSize; AppPrefsDlg;
00263 
00264 ********************************************************************************************/
00265 String_32 *PageSize::GetPageName()
00266 {
00267     return &PageName;
00268 }
00269 
00270 
00271 /********************************************************************************************
00272 
00273 >   PageSize::GetPageWidth()
00274 
00275     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00276     Created:    12/12/94
00277     Returns:    Width of this page in millipoints.
00278     Purpose:    Way of of finding out the current page width.
00279     SeeAlso:    PageSize; AppPrefsDlg;
00280 
00281 ********************************************************************************************/
00282 INT32 PageSize::GetPageWidth()
00283 {
00284     return PageWidth;
00285 }
00286 
00287 /********************************************************************************************
00288 
00289 >   INT32 PageSize::GetPageHeight()
00290 
00291     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00292     Created:    12/12/94
00293     Returns:    Height of this page in millipoints.
00294     Purpose:    Way of of finding out the current page height.
00295     SeeAlso:    PageSize; AppPrefsDlg;
00296 
00297 ********************************************************************************************/
00298 INT32 PageSize::GetPageHeight()
00299 {
00300     return PageHeight;
00301 }
00302 
00303 
00304 
00305 /******************************************************************************************
00306 ********************************************************************************************/
00307 
00308 
00309 /********************************************************************************************
00310 
00311 >   PageSizesList::PageSizesList()
00312 
00313     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00314     Created:    12/12/94
00315     Purpose:    Constructor.
00316     Errors:
00317     SeeAlso:    AppPrefsDlg;
00318 
00319 ********************************************************************************************/
00320 
00321 PageSizesList::PageSizesList()
00322 {
00323     // Set up useful defaults
00324 
00325     // This is the next page id which will be used when the next page is added
00326     MaxPageId = (INT32)LASTPAGE;
00327 }
00328 
00329 /********************************************************************************************
00330 
00331 >   PageSizesList::~PageSizesList()
00332 
00333     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00334     Created:    12/12/94
00335     Purpose:    Destructor.
00336     Errors:
00337     SeeAlso:    AppPrefsDlg;
00338 
00339 ********************************************************************************************/
00340 
00341 
00342 PageSizesList::~PageSizesList()
00343 {
00344     // Remove all page sizes from the list
00345     ListItem* pItem;
00346 
00347     while ((pItem = this->RemoveHead()) != NULL)
00348         delete pItem;
00349 }
00350 
00351 /********************************************************************************************
00352 
00353 >   BOOL PageSizesList::InitPageSizes()
00354 
00355     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00356     Created:    12/12/94
00357     Returns:    TRUE if initialised ok, FALSE if not.
00358     Purpose:    Scans all PageSize to find out which PageSize are available for use, and
00359                 instantiates one of each, and holds them in a list.
00360     Errors:     Out of memory (if a PageSize fails to initialise for other reasons then an 
00361                 error is reported, but Camelot will still start up ok, i.e. TRUE is 
00362                 returned).
00363     SeeAlso:    PageSize
00364 
00365 ********************************************************************************************/
00366 
00367 BOOL PageSizesList::InitPageSizes()
00368 {
00369     // Reset our store for error message identifiers to the 'no error' state.
00370     ErrorId = 0;
00371     
00372     // First, try and read hot keys from resource file
00373     BOOL Ok = ReadPageSizes();
00374 
00375     // Failed to read in the pages from the resource, tell the user this and what we think
00376     // the problem is, then use the bound in pages.
00377     if (!Ok)
00378     {
00379         // For now if we fail, error 3 to tell the user and then use the bound in version. 
00380         if (ErrorId == 0)
00381             ErrorId = _R(IDW_PAGES_UNKNOWNERROR);
00382     
00383         String_256 ErrorReason;
00384         ErrorReason.MakeMsg(ErrorId);
00385         
00386         // Combine the above error with the main error message
00387         String_256 ErrorMsg;
00388         ErrorMsg.MakeMsg(_R(IDW_PAGES_FAILEDREADING), (TCHAR*) ErrorReason);
00389         Error::SetError(_R(IDW_PAGES_FAILEDREADING), (TCHAR *) ErrorMsg, 0);
00390         InformError();                                                          \
00391         TRACEUSER( "Neville", _T("PageSizesList::InitPageSizes failed to read in list from resource, using bound in one"));
00392 
00393         // Vape any that may have been created
00394         this->DeleteAll();
00395         
00396         // Create, initialise and install the built in PageSize list
00397         PageSize *pPageSize;
00398 
00399         // First size is implied to be custom i.e. if unknown size.
00400         DeclarePageSize(A0, _R(IDS_K_PAGESIZE_A0), 840.0, 1188.0, MILLIMETRES, &pPageSize);
00401         DeclarePageSize(A1, _R(IDS_K_PAGESIZE_A1), 594.0, 840.0, MILLIMETRES, &pPageSize);
00402         DeclarePageSize(A2, _R(IDS_K_PAGESIZE_A2), 420.0, 594.0, MILLIMETRES, &pPageSize);
00403         DeclarePageSize(A3, _R(IDS_K_PAGESIZE_A3), 297.0, 420.0, MILLIMETRES, &pPageSize);
00404         DeclarePageSize(A4, _R(IDS_K_PAGESIZE_A4), 210.0, 297.0, MILLIMETRES, &pPageSize);
00405         DeclarePageSize(A5, _R(IDS_K_PAGESIZE_A5), 148.5, 210.0, MILLIMETRES, &pPageSize);
00406         DeclarePageSize(A6, _R(IDS_K_PAGESIZE_A6), 105.0, 148.5, MILLIMETRES, &pPageSize);
00407         DeclarePageSize(USLEGAL, _R(IDS_K_PAGESIZE_USLEGAL), 8.5, 14, INCHES, &pPageSize);
00408         DeclarePageSize(USLETTER,_R(IDS_K_PAGESIZE_USLETTER), 8.5, 11, INCHES, &pPageSize);
00409         DeclarePageSize(FANFOLD, _R(IDS_K_PAGESIZE_FANFOLD), 8.25, 12, INCHES, &pPageSize);
00410     }
00411     
00412     // All ok
00413     return TRUE;
00414 }
00415 
00416 /********************************************************************************************
00417 
00418 >   void PageSizesList::DeinitPageSizes()
00419 
00420     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00421     Created:    12/12/94
00422     Purpose:    Destroy all the PageSize objects.
00423     SeeAlso:    PageSize
00424 
00425 ********************************************************************************************/
00426 
00427 BOOL PageSizesList::DeinitPageSizes()
00428 {
00429     // Get rid of our PageSize list
00430     this->DeleteAll();
00431 
00432     // All ok
00433     return TRUE;
00434 }
00435 
00436 /********************************************************************************************
00437 
00438 >   BOOL PageSizesList::DeclarePageSize(PageId NewId, String_32 *Name, double Width, double Height,
00439                                     UnitType Units, PageSize **ppPageSize)
00440 
00441     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00442     Created:    12/12/94
00443     Inputs:     NewId       unique pageid.
00444                 Name        textual desciption of page size
00445                 Width       width of page in mm.
00446                 Height      height of page in mm.
00447                 ppPageSize  pointer to a page size pointer to be updated
00448     Returns:    True if page size declared, False if something went wrong. 
00449     Purpose:    A function to declare a new page size to the system.
00450     SeeAlso:    PageSize
00451 
00452 ********************************************************************************************/
00453 
00454 BOOL PageSizesList::DeclarePageSize(PageId NewId, String_32 *Name, double Width, double Height,
00455                                     UnitType Units, PageSize **ppPageSize)
00456 {
00457     // First check to see if somebody has actually declared the pointer to be non null.
00458     PageSize *pPageSize = new PageSize;
00459     if (pPageSize == NULL) return FALSE;
00460 
00461     if (!pPageSize->Init(NewId, Name, Width, Height, Units))
00462     {
00463         /* Error occured - report it and stop trying to initialise the options tab. */
00464         delete pPageSize;       // remove the declared page size
00465         pPageSize = NULL;       // remove the item from the system
00466         InformError();          // tell the user about the problem
00467         return TRUE;
00468     }
00469 
00470     // Add this new item at the end of the list
00471     this->AddTail(pPageSize);
00472 
00473     *ppPageSize = pPageSize;    // update the new end pointer 
00474     
00475     // All ok
00476     return TRUE;
00477 }
00478 
00479 /********************************************************************************************
00480 
00481 >   PageSize *PageSizesList::GetFirstPageSize()
00482 
00483     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00484     Created:    12/12/94
00485     Returns:    Pointer to the first page size NULL if none.
00486     Purpose:    Iterating through the list of PageSize known to Camelot.  This function
00487                 returns the first PageSize.
00488     SeeAlso:    PageSizesList::GetNext()
00489     Scope:      Static
00490 
00491 ********************************************************************************************/
00492 
00493 PageSize *PageSizesList::GetFirstPageSize()
00494 {
00495     return (PageSize *) this->GetHead();
00496 }
00497 
00498 /********************************************************************************************
00499 
00500 >   PageSize *PageSizesList::GetNextPageSize(PageSize *pPageSize)
00501 
00502     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00503     Created:    12/12/94
00504     Inputs:     pPageSize - pointer to the PageSize to be used to get the following
00505                 PageSize.
00506     Returns:    Pointer to the next PageSize, or NULL if none.
00507     Purpose:    Iterating through the list of PageSize known to Camelot.  This function
00508                 returns the next PageSize.
00509     SeeAlso:    PageSizesList::GetFirst
00510 
00511 ********************************************************************************************/
00512 
00513 PageSize *PageSizesList::GetNextPageSize(PageSize *pPageSize)
00514 {
00515     return (PageSize *) this->GetNext(pPageSize);
00516 }
00517 
00518 /********************************************************************************************
00519 
00520 >   String_32* PageSizesList::GetTextualDescription(PageId ThisId)
00521 
00522     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00523     Created:    12/12/94
00524     Inputs:     PageId - id of the page that we are searching for
00525     Returns:    Textual name for the specified page.
00526     Purpose:    Return the name for the page size specified.
00527     SeeAlso:    PageSize::GetFirst
00528 
00529 ********************************************************************************************/
00530 
00531 String_32* PageSizesList::GetTextualDescription(PageId ThisId)
00532 {
00533     PageSize* pPageSize = (PageSize*) this->GetHead();
00534 
00535     while (pPageSize != NULL)
00536     {
00537         if (pPageSize->GetPageId() == ThisId)
00538             return pPageSize->GetPageName();
00539 
00540         pPageSize = (PageSize*) this->GetNext(pPageSize);
00541     }
00542 
00543     ERROR3("PageSizesList::GetTextualDescription Specified page id not found");
00544     return NULL;
00545 }
00546 
00547 
00548 /********************************************************************************************
00549 
00550 >   PageId PageSizesList::GetPageIdFromSize(INT32 Width INT32 Height)
00551 
00552     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00553     Created:    12/12/94
00554     Inputs:     Height  height of page in millipoints.
00555                 Width   width of page in millipoints.
00556     Returns:    PageId  the Id for the specified page.
00557     Purpose:    Returns the name for the page size specified or CUSTOM if not found.
00558     SeeAlso:    PageSize::GetSizeFromPageId()
00559 
00560 ********************************************************************************************/
00561 
00562 PageId PageSizesList::GetPageIdFromSize(INT32 Width, INT32 Height)
00563 {
00564     PageSize* pPageSize = (PageSize*) this->GetHead();
00565 
00566     while (pPageSize != NULL)
00567     {
00568         // Must give a little leeway either side as otherwise rounding errors may
00569         // raise their ugly heads and mean that 841891 != 841892 and hence miss the page size
00570         INT32 bodge = (INT32)(0.5 * MM_MP_VAL); // extra leeway on comparison required
00571         INT32 FoundWidth = pPageSize->GetPageWidth();
00572         INT32 FoundHeight = pPageSize->GetPageHeight(); 
00573         if ( (FoundWidth >= (Width - bodge)) && (FoundWidth <= (Width + bodge)) &&
00574              (FoundHeight  >= (Height - bodge)) && (FoundHeight  <= (Height + bodge)) )
00575             return pPageSize->GetPageId();
00576 
00577         pPageSize = (PageSize*) this->GetNext(pPageSize);
00578     }
00579 
00580     return CUSTOM;
00581 }
00582 
00583 /********************************************************************************************
00584 
00585 >   BOOL PageSizesList::GetSizeFromPageId(PageId ThisId, INT32 *Width, INT32 *Height)
00586 
00587     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00588     Created:    12/12/94
00589     Inputs:     PageId - id of the page that we are searching for
00590     Returns:    Width   the width for the specified page in millipoints.
00591                 Height  the height for the specified page in millipoints.
00592     Purpose:    Returns the width and height the page size specified.
00593     SeeAlso:    PageSize::GetPageIdFromSize()
00594 
00595 ********************************************************************************************/
00596 
00597 BOOL PageSizesList::GetSizeFromPageId(PageId ThisId, INT32 *Width, INT32 *Height)
00598 {
00599     // Set up default values in case of early exit
00600     *Width = 0;
00601     *Height = 0;
00602 
00603     PageSize* pPageSize = (PageSize*) this->GetHead();
00604 
00605     while (pPageSize != NULL)
00606     {
00607         if (pPageSize->GetPageId() == ThisId)
00608         {
00609             *Width = pPageSize->GetPageWidth();         // return found page width
00610             *Height = pPageSize->GetPageHeight();       // return found page height
00611             return TRUE;
00612         }
00613         pPageSize = (PageSize*) this->GetNext(pPageSize);
00614     }
00615 
00616     // Did not find the specified page 
00617     return FALSE;
00618 }
00619 
00620 
00621 
00622 /********************************************************************************************
00623 *** Reading page sizes from the resource or from an ini file                            *****
00624 ********************************************************************************************/
00625 
00626 
00627 
00628 
00629 /********************************************************************************************
00630 
00631 > BOOL PageSizesList::ReadPageSizes()
00632 
00633     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00634     Created:    22/11/95
00635     Inputs:     -
00636     Outputs:    -
00637     Returns:    TRUE if OK, FALSE otherwise
00638     Purpose:    Reads the page size definitions from somewhere (either file or bound-in resource file).
00639     SeeAlso:    HotKey::ReadHotKeys;
00640 
00641 ********************************************************************************************/
00642 
00643 BOOL PageSizesList::ReadPageSizes()
00644 {
00645     BOOL ok = PageSizesList::ReadPageSizesFromDisk();
00646 
00647     if (!ok) ok = PageSizesList::ReadPageSizesFromRes();
00648 
00649     return (ok);
00650 }
00651 
00652 /********************************************************************************************
00653 
00654 > BOOL PageSizesList::ReadPageSizesFromDisk()
00655 
00656     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00657     Created:    22/11/95
00658     Inputs:     -
00659     Outputs:    -
00660     Returns:    TRUE if OK, FALSE otherwise
00661     Purpose:    Reads the page size definitions from disk (i.e. writable media)
00662     SeeAlso:    HotKey::ReadHotKeysFromDisk;
00663 
00664 ********************************************************************************************/
00665 
00666 BOOL PageSizesList::ReadPageSizesFromDisk()
00667 {   
00668     // Not yet implemented
00669 
00670     return FALSE;
00671 }
00672 
00673 /********************************************************************************************
00674 
00675 > BOOL PageSizesList::ReadPageSizesFromRes()
00676 
00677     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00678     Created:    22/11/95
00679     Inputs:     -
00680     Outputs:    -
00681     Returns:    TRUE if OK, FALSE otherwise
00682     Purpose:    Reads the hot key definitions from the bound in resource.
00683     SeeAlso:    HotKey::ReadHotKeysFromRes;
00684 
00685 ********************************************************************************************/
00686 
00687 BOOL PageSizesList::ReadPageSizesFromRes()
00688 {
00689     // Resource File
00690     CCResTextFile file;             
00691 
00692     // Open resource
00693     BOOL ok = file.open(_R(IDH_DEFAULT_PAGESIZES), _R(IDT_CAM_PAGESIZES_RES));
00694 
00695     if (ok)
00696     {
00697         ok = PageSizesList::ReadPageSizesFromFile(file);
00698         file.close();
00699     }
00700 
00701     return (ok);
00702 }
00703 
00704 /********************************************************************************************
00705 
00706 > BOOL PageSizesList::ReadPageSizesFromFile(CCLexFile& file)
00707 
00708     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00709     Created:    22/11/95
00710     Inputs:     file = file that contains the page size definitions
00711     Outputs:    -
00712     Returns:    TRUE if OK, FALSE otherwise
00713     Purpose:    Reads the page size definitions from the given file
00714     SeeAlso:    HotKey::ReadHotKeysFromFile;
00715 
00716 ********************************************************************************************/
00717 
00718 BOOL PageSizesList::ReadPageSizesFromFile(CCLexFile& file)
00719 {
00720     BOOL finished = FALSE;
00721     BOOL ok;;   
00722 
00723     // Initialise lexing routines, and aspects of the lexer
00724     ok = file.InitLexer();
00725     file.SetDelimiters("\r\n");         // Set token delimiting characters
00726     file.SetCommentMarker(';');         // Set comment marker char
00727     file.SetWhitespace(" \t");          // Set whitespace chars
00728     file.SetStringDelimiters("\"\"");   // Set string delimiters
00729 
00730     PsTokenIndex Token;
00731     const TCHAR* TokenBuf = file.GetTokenBuf(); // Token buffer remains constant until lexer deinitialisation
00732     
00733     while (ok && !finished)
00734     {
00735         // Grab a token
00736         ok = file.GetSimpleToken();
00737 
00738         if (ok)
00739         {
00740             // Look the token up in our table
00741             Token = FindToken(TokenBuf);
00742 
00743             switch (Token)
00744             {
00745                 case TOKEN_PAGESIZE:
00746                     // Correst line start found so read in the page size info from that line
00747                     ok = PageSizesList::ReadPageSizesList(file);
00748                     break;
00749 
00750                 case TOKEN_PAGESIZEEND:
00751                     // End of table marker found so our job is over
00752                     finished = TRUE;
00753                     break;
00754 
00755                 default:
00756                     TRACEUSER("Neville", _T("PageSizesList: Unexpected token - %s\n"),TokenBuf);
00757                     ErrorId = _R(IDW_PAGES_UNEXPECTEDMAINTOKEN);
00758                     ok = FALSE;
00759                     break;
00760             }
00761         }
00762         else
00763             ErrorId = _R(IDW_PAGES_FAILEDTOGETTOKEN);
00764     }
00765 
00766     if (!ok)
00767     {
00768         TRACEUSER( "Neville", _T("\nPageSizesList: Offending line - %s\n"),file.GetLineBuf());
00769         //ERROR3("Error reading page sizes.  See TRACE output for details");
00770     }
00771 
00772     // We are now finished with the lexer
00773     file.DeinitLexer();
00774 
00775     return (ok);
00776 }
00777 
00778 /********************************************************************************************
00779 
00780 > BOOL PageSizesList::ReadPageSizesList(CCLexFile& file)
00781 
00782     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00783     Created:    22/11/95
00784     Inputs:     file = file that contains the page size definitions
00785     Outputs:    -
00786     Returns:    TRUE if OK, FALSE otherwise
00787     Purpose:    Read one page size definition from the given file
00788     SeeAlso:    -
00789 
00790 ********************************************************************************************/
00791 
00792 BOOL PageSizesList::ReadPageSizesList(CCLexFile& file)
00793 {
00794     PageSize*   pPageSize   = NULL;
00795     String_32   PageName;
00796     double      Width       = 0.0;
00797     double      Height      = 0.0; 
00798     UnitType    Units       = NOTYPE;
00799 
00800     BOOL ok = PageSizesList::ReadPageSizeDef(file, &PageName, &Width, &Height, &Units);
00801 
00802     if (ok)
00803     {
00804         // First size is implied to be custom i.e. if unknown size.
00805         DeclarePageSize((PageId)MaxPageId, &PageName, Width, Height, Units, &pPageSize);
00806 
00807         // Now increment are next new page id
00808         MaxPageId++;
00809 
00810         if (!ok)
00811         {
00812             ErrorId = _R(IDW_PAGES_FAILEDTOADDPAGE);
00813             TRACEUSER( "Neville", _T("PageSizesList: Unable to add page size to the system\n"));
00814         }
00815     }
00816 
00817     return (ok);
00818 }
00819 
00820 /********************************************************************************************
00821 
00822 > BOOL HotKey::ReadPageSizeDef( CCLexFile& file,
00823                                         String_32* pPageName,
00824                                         double* pWidth, 
00825                                         double* pHeight,
00826                                         UnitType* pUnits)
00827 
00828 
00829     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00830     Created:    22/11/95
00831     Inputs:     file            = file that contains the key definitions
00832                 ppKeyPress      = Place to put the ptr to a key press object
00833                 pOpToken        = ptr to string to place the op token string
00834                 ppTextDesc      = ptr to a ptr to a String_32 object that describes the key def (e.g. "Ctrl+X")
00835                 pCheckUnicode   = Flag to determine how the hot key is compared with key press objects
00836 
00837     Outputs:    *ppKeyPress     = a ptr to a key press object OR NULL
00838                 *pOpToken       = op token of op associated with the key press object OR empty string
00839                 *ppTextDesc     = ptr to a String_32 describing the key combination.
00840                                   If this is NULL, then the key combo has no text description
00841                 *pCheckUnicode  = TRUE if Unicode checking should be done, FALSE otherwise
00842 
00843     Returns:    TRUE if OK
00844                 FALSE otherwise (syntax error, not enough memory, etc)
00845     Purpose:    Generates a key press object and op token from the given file
00846     SeeAlso:    HotKey::ReadKeyDef
00847 
00848 ********************************************************************************************/
00849 
00850 BOOL PageSizesList::ReadPageSizeDef(CCLexFile& file,
00851                                     String_32* pPageName,
00852                                     double* pWidth, 
00853                                     double* pHeight,
00854                                     UnitType* pUnits)
00855 {
00856     // Check the parameters
00857     ERROR2IF(pPageName == NULL, FALSE,"PageSizesList::ReadPageSizeDef pPageName is NULL");
00858     ERROR2IF(pWidth == NULL,    FALSE,"PageSizesList::ReadPageSizeDef pWidth is NULL");
00859     ERROR2IF(pHeight == NULL,   FALSE,"PageSizesList::ReadPageSizeDef pHeight is NULL");
00860     ERROR2IF(pUnits == NULL,    FALSE,"PageSizesList::ReadPageSizeDef pUnits is NULL");
00861 
00862     // Fill in some useful default values
00863     *pPageName              = _T("");
00864     *pWidth                 = 0.0;
00865     *pHeight                = 0.0;
00866     *pUnits                 = MILLIMETRES;  // default to reading in measurements in millimetres
00867     
00868     PsTokenIndex Token;
00869     const TCHAR* TokenBuf   = file.GetTokenBuf();
00870 
00871     // This set of parameters will combine to form a key press object that represents
00872     // the hot key combination
00873 //  String_32* pTextDesc    = NULL;
00874 
00875     // This is FALSE until we have read the text that describes the page size
00876     BOOL TextDescRead       = FALSE;
00877     // This is FALSE until we have read the first number in, which should be the width
00878     BOOL ReadWidth          = FALSE;
00879 
00880     // We haven't finsihed, but we're OK at the mo
00881     BOOL finished           = FALSE;
00882     BOOL ok                 = TRUE;
00883 
00884     while (ok && !finished)
00885     {
00886         // Get the next token
00887         ok = file.GetSimpleToken();
00888 
00889         if (ok)
00890         {
00891             // Find out the type of the token
00892             LexTokenType TokenType = file.GetTokenType();
00893 
00894             switch (TokenType)
00895             {
00896                 case TOKEN_STRING:
00897                     {
00898                         // Read that text into the name of the page as this is the only 
00899                         // string that is expected
00900                         // Make sure the desccription is not too long and there is one
00901                         INT32 length = camStrlen(TokenBuf); 
00902                         ok = ((length <= pPageName->MaxLength()) && (length > 0) );
00903 
00904                         if (ok)
00905                         {
00906                             // Only take as much as we can take
00907                             //TokenBuf.Left(pPageName, pPageName->MaxLength());                 
00908                             *pPageName = TokenBuf;
00909                             
00910                             // We have read the text desc string
00911                             TextDescRead = TRUE;
00912                         }
00913                         else
00914                         {
00915                             TRACEUSER( "Neville", _T("PageSizesList::ReadPageSizeDef blank or too long a page size name!"));
00916                             ErrorId = _R(IDW_PAGES_BADPAGENAME);
00917                         }
00918                         
00919                     }
00920                     break;
00921 
00922                 case TOKEN_NORMAL:
00923                     {
00924                         Token = FindToken(TokenBuf);
00925                         switch (Token)
00926                         {
00927                             // if we find either of the units then this is the last item
00928                             // so this page size is all read in
00929                             case TOKEN_MILLIMETRES:
00930                                 *pUnits = MILLIMETRES;
00931                                 finished = TRUE;
00932                                 break;
00933                             case TOKEN_INCHES:
00934                                 *pUnits = INCHES;
00935                                 finished = TRUE;
00936                                 break;
00937 
00938                             case PsTOKEN_NONE:
00939                                 {
00940                                     double Number;
00941                                     // Could use this but the routine requires a valid doc!
00942                                     //String_256 Value;
00943                                     //Value = TokenBuf;
00944                                     //ok = Convert::StringToDouble( &Value, &Number);
00945                                     // Could use this but is not unicode compliant!
00946                                     //Number = atof((char *)TokenBuf);
00947                                     ok = (camSscanf(TokenBuf,_T("%lg"),&Number) == 1);
00948                                     if (ok)
00949                                     {
00950                                         if (!ReadWidth)
00951                                         {
00952                                             ReadWidth = TRUE;
00953                                             *pWidth = Number;
00954                                         }
00955                                         else
00956                                         {
00957                                             //ok = (camSscanf(TokenBuf,"%lg",pHeight) == 1);
00958                                             *pHeight = Number;
00959                                         }
00960                                     }
00961                                     else
00962                                     {
00963                                         ErrorId = _R(IDW_PAGES_BADPAGESIZE);
00964                                         TRACEUSER( "Neville", _T("PageSize: Expected a size (width/height) but got : '%s'\n"),TokenBuf);
00965                                     }
00966                                 }
00967                                 break;
00968 
00969                             default:
00970                                 // Flag a bad token so that we stop parsing this line
00971                                 ok = FALSE;
00972                                 TRACEUSER( "Neville", _T("PageSize: Didn't expect to get this token ('%s') in the middle of a hot key def\n"),TokenBuf);
00973                                 ErrorId = _R(IDW_PAGES_BADTOKEN);
00974                                 break;
00975                         }
00976                     }
00977                     break;
00978 
00979                 default:
00980                     // bad token so stop parsing this line
00981                     TRACEUSER( "Neville", _T("PageSize: Unexpected token - %s\n"),TokenBuf);
00982                     ok = FALSE;
00983                     ErrorId = _R(IDW_PAGES_UNEXPECTEDTOKEN);
00984                     break;
00985             }
00986         }
00987     }
00988 
00989     return (ok);
00990 }

Generated on Sat Nov 10 03:46:22 2007 for Camelot by  doxygen 1.4.4