diff --git a/src/SplitLedger.c b/src/SplitLedger.c index ffacdb3e21..405fb29199 100644 --- a/src/SplitLedger.c +++ b/src/SplitLedger.c @@ -208,10 +208,11 @@ printf ("leave LedgerTraverse with %d \n", reg->cursor_phys_row); static void LedgerDestroy (SplitRegister *reg) { + Transaction *trans; + /* be sure to destroy the "blank split" */ if (reg->user_hook) { Split *split; - Transaction *trans; split = (Split *) (reg->user_hook); @@ -223,6 +224,19 @@ LedgerDestroy (SplitRegister *reg) xaccTransCommitEdit (trans); reg->user_hook = NULL; } + + /* be sure to take care of any open transactions */ + if (reg->user_huck) { + trans = (Transaction *) (reg->user_huck); + + /* I suppose we could also rollback here ... its not clear what + * the desirable behaviour should be from the user's point of view + * when they close a window with an uncommitted edit in it ... + * Maybe we should prompt them ?? + */ + xaccTransCommitEdit (trans); + reg->user_huck = NULL; + } } /* ======================================================== */ @@ -284,7 +298,7 @@ void xaccSRSaveRegEntry (SplitRegister *reg) { Split *split; - Transaction *trans; + Transaction *trans, *oldtrans; Account * acc; unsigned int changed; int style; @@ -327,8 +341,15 @@ printf ("save split is %p \n", split); } trans = xaccSplitGetParent (s); + /* determine whether we should commit the previous edit */ + oldtrans = (Transaction *) (reg->user_huck); + if (oldtrans != trans) { + xaccTransCommitEdit (oldtrans); + xaccTransBeginEdit (trans, 0); + reg->user_huck = (void *) trans; + } + split = xaccMallocSplit (); - xaccTransBeginEdit (trans, 0); xaccTransAppendSplit (trans, split); if (force_double_entry_awareness) { @@ -341,7 +362,14 @@ printf ("save split is %p \n", split); } else { trans = xaccSplitGetParent (split); - xaccTransBeginEdit (trans, 0); + + /* determine whether we should commit the previous edit */ + oldtrans = (Transaction *) (reg->user_huck); + if (oldtrans != trans) { + xaccTransCommitEdit (oldtrans); + xaccTransBeginEdit (trans, 0); + reg->user_huck = (void *) trans; + } } /* copy the contents from the cursor to the split */ @@ -459,8 +487,6 @@ printf ("save split is %p \n", split); xaccSplitSetValue (split, (reg->valueCell->amount)); } - xaccTransCommitEdit (trans); - printf ("finished saving split %s of trans %s \n", xaccSplitGetMemo(split), xaccTransGetDescription(trans)); diff --git a/src/register/splitreg.c b/src/register/splitreg.c index 3acea2ecf3..995b6caae8 100644 --- a/src/register/splitreg.c +++ b/src/register/splitreg.c @@ -571,6 +571,7 @@ xaccInitSplitRegister (SplitRegister *reg, int type) reg->user_hook = NULL; reg->user_hack = NULL; + reg->user_huck = NULL; reg->destroy = NULL; reg->type = type; @@ -771,6 +772,7 @@ xaccDestroySplitRegister (SplitRegister *reg) reg->destroy = NULL; reg->user_hook = NULL; reg->user_hack = NULL; + reg->user_huck = NULL; xaccDestroyTable (reg->table); reg->table = NULL; diff --git a/src/register/splitreg.h b/src/register/splitreg.h index 67120f0c8f..9de6ba009a 100644 --- a/src/register/splitreg.h +++ b/src/register/splitreg.h @@ -158,13 +158,14 @@ struct _SplitRegister { short num_virt_rows; short cursor_phys_row; short cursor_virt_row; - void * user_hack; BasicCell *header_label_cells[NUM_CELLS]; /* user_hook allows users of this object to hang * private data onto it */ void *user_hook; + void *user_hack; + void *user_huck; /* The destroy callback gives user's a chance * to free up any associated user_hook data */