From a5c17b411112c4b3c8a5c62181bf5e7058635c82 Mon Sep 17 00:00:00 2001 From: Linas Vepstas Date: Sat, 8 Aug 1998 20:24:36 +0000 Subject: [PATCH] introduce more flexible, robust format handling git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@952 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/register/pricecell.c | 35 ++++++++++++----------------------- src/register/pricecell.h | 8 ++++---- src/register/splitreg.c | 5 +++++ 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/src/register/pricecell.c b/src/register/pricecell.c index 9e6d6a1264..42bad5e95c 100644 --- a/src/register/pricecell.c +++ b/src/register/pricecell.c @@ -108,6 +108,7 @@ xaccInitPriceCell (PriceCell *cell) xaccInitBasicCell( &(cell->cell)); cell->amount = 0.0; cell->blank_zero = 1; + cell->prt_format = strdup ("%.2f"); SET ( &(cell->cell), ""); @@ -121,6 +122,7 @@ void xaccDestroyPriceCell (PriceCell *cell) { cell->amount = 0.0; + free (cell->prt_format); cell->prt_format = 0x0; xaccDestroyBasicCell ( &(cell->cell)); } @@ -135,7 +137,7 @@ void xaccSetPriceCellValue (PriceCell * cell, double amt) if (cell->blank_zero && (VERY_SMALL > amt) && ((-VERY_SMALL) < amt)) { buff[0] = 0x0; } else { - sprintf (buff, "%.3f", amt); + sprintf (buff, cell->prt_format, amt); } SET ( &(cell->cell), buff); @@ -145,21 +147,13 @@ void xaccSetPriceCellValue (PriceCell * cell, double amt) /* ================================================ */ -void xaccSetAmountCellValue (PriceCell * cell, double amt) +void xaccSetPriceCellFormat (PriceCell * cell, char * fmt) { - char buff[40]; - cell->amount = amt; + if (cell->prt_format) free (cell->prt_format); + cell->prt_format = strdup (fmt); - /* if amount is zero, and blanking is set, then print blank */ - if (cell->blank_zero && (VERY_SMALL > amt) && ((-VERY_SMALL) < amt)) { - buff[0] = 0x0; - } else { - sprintf (buff, "%.2f", amt); - } - SET ( &(cell->cell), buff); - - /* set the cell color to red if the value is negative */ - COLORIZE (cell, amt); + /* make sure that the cell is updated with the new format */ + xaccSetPriceCellValue (cell, cell->amount); } /* ================================================ */ @@ -179,11 +173,11 @@ void xaccSetDebCredCellValue (PriceCell * deb, SET ( &(deb->cell), ""); } else if (0.0 < amt) { - sprintf (buff, "%.2f", amt); + sprintf (buff, cred->prt_format, amt); SET ( &(cred->cell), buff); SET ( &(deb->cell), ""); } else { - sprintf (buff, "%.2f", -amt); + sprintf (buff, deb->prt_format, -amt); SET ( &(cred->cell), ""); SET ( &(deb->cell), buff); } @@ -194,15 +188,10 @@ void xaccSetDebCredCellValue (PriceCell * deb, static void PriceSetValue (BasicCell *_cell, const char *str) { - char buff[40]; PriceCell *cell = (PriceCell *) _cell; + double amt = xaccParseUSAmount (str); - SET (_cell, str); - - cell->amount = xaccParseUSAmount (str); - - sprintf (buff, "%.2f", cell->amount); - SET ( &(cell->cell), buff); + xaccSetPriceCellValue (cell, amt); } /* --------------- end of file ---------------------- */ diff --git a/src/register/pricecell.h b/src/register/pricecell.h index 9a6164de28..83eca14ec6 100644 --- a/src/register/pricecell.h +++ b/src/register/pricecell.h @@ -52,9 +52,9 @@ typedef struct _PriceCell { BasicCell cell; - double amount; /* the amount associated with this cell */ + double amount; /* the amount associated with this cell */ short blank_zero; /* controls printing of zero values */ - + char *prt_format; /* controls display of value; printf format */ } PriceCell; /* installs a callback to handle price recording */ @@ -65,8 +65,8 @@ void xaccDestroyPriceCell (PriceCell *); /* updates amount, string format is three decimal places */ void xaccSetPriceCellValue (PriceCell *, double amount); -/* updates amount, string format is two decimal places */ -void xaccSetAmountCellValue (PriceCell *, double amount); +/* use printf-style format to control display */ +void xaccSetPriceCellFormat (PriceCell *, char * fmt); /* updates two cells; the deb cell if amt is negative, * the credit cell if amount is positive, and makes the other cell diff --git a/src/register/splitreg.c b/src/register/splitreg.c index c599360cdc..158dca2f6a 100644 --- a/src/register/splitreg.c +++ b/src/register/splitreg.c @@ -466,6 +466,11 @@ void xaccInitSplitRegister (SplitRegister *reg, int type) xaccSetPriceCellValue (reg->creditCell, 0.0); xaccSetPriceCellValue (reg->valueCell, 0.0); + /* use three decimal places to print share-related info. + * The format is a printf-style format for a double. */ + xaccSetPriceCellFormat (reg->shrsCell, "%.3f"); + xaccSetPriceCellFormat (reg->priceCell, "%.3f"); + /* -------------------------------- */ /* define how traversal works */ configTraverse (reg);