Hello, I've attached a BETA svn diff patch against HEAD which creates a new cell type, RBALN_CELL. I could use some help tweaking this patch if someone is able to provide answers to my questions below.
This cell calculates the balance of all transactions in a register at runtime, allowing a running balance to be displayed when using the "Open Subaccounts" feature. The cell currently calculates the running balance of all splits that are going to/from the lead account and all of its subaccounts, therefore it is not appropriate (currently) to use this cell type in a non-subaccount register view. My Questions: 1. There is an outstanding issue with this patch where it appears the return value of the gnc_split_register_get_rbaln_entry() function is printed in the Balance column of the split rows. The running Balance is correctly printed on the Transaction rows, but the cells in the Balance column on the lines of the split should be blank. Instead, they display 1 (for all transactions where a balance is printed) or 0 (for the blank split at the bottom because the function returns NULL since we don't print the balance for that txn). How can I fix this so that the balance cell of the split lines is blank? 2. The RBALN_CELL was added to the CURSOR_SINGLE_JOURNAL cursor of the INCOME_LEDGER/GENERAL_LEDGER/SEARCH_LEDGER cases in split-register-layout.c. Will this affect other register views besides the subaccount view? I have only tested this with asset accounts (bank, checking) so I'm not sure if it might cause problems with different account types. Please take a look and let me know how I can further improve it! Thanks, -Tim M.
Index: src/register/ledger-core/split-register-model.c =================================================================== --- src/register/ledger-core/split-register-model.c (revision 18194) +++ src/register/ledger-core/split-register-model.c (working copy) @@ -1505,6 +1505,65 @@ } } +static const char * +gnc_split_register_get_rbaln_entry (VirtualLocation virt_loc, + gboolean translate, + gboolean *conditionally_changed, + gpointer user_data) +{ + SplitRegister *reg = user_data; + SRInfo *info = gnc_split_register_get_info (reg); + gnc_numeric value = gnc_numeric_zero(), balance = gnc_numeric_zero(); + Split *split; + Account *account; + Transaction *trans; + GList *node, *children, *child; + int i, row; + + /* This cell calculates the register balance for a particular split at runtime. + * This function should work regardless of the sort order. */ + + /* Return NULL if this is a blank transaction. */ + split = gnc_split_register_get_split (reg, virt_loc.vcell_loc); + if (split == xaccSplitLookup (&info->blank_split_guid, + gnc_get_current_book ())) + return NULL; + + /* Get a list of all subaccounts for matching */ + children = gnc_account_get_descendants(gnc_split_register_get_default_account(reg)); + children = g_list_append(children, gnc_split_register_get_default_account(reg)); + + /* Get the row number we're on, then start with the first row. */ + row = virt_loc.vcell_loc.virt_row; + virt_loc.vcell_loc.virt_row=0; + + while (virt_loc.vcell_loc.virt_row <= row ) { + /* Get new temporary split and its parent transaction */ + split = gnc_split_register_get_split (reg, virt_loc.vcell_loc); + trans = xaccSplitGetParent (split); + + i = 1; + for (node = xaccTransGetSplitList (trans); node; node = node->next) { + Split *secondary = node->data; + i++; + + /* Add up the splits that belong to the transaction if they are + * from the lead account or one of the subaccounts. */ + account = xaccSplitGetAccount (secondary); + + for (child = children; child; child = child->next) { + if (account == child->data) { + balance = gnc_numeric_add_fixed(balance, xaccSplitGetAmount(secondary)); + break; + } + } + } + virt_loc.vcell_loc.virt_row+=i; + } + + return xaccPrintAmount (balance, gnc_split_register_print_info (reg)); +} + static gboolean gnc_split_register_cursor_is_readonly (VirtualLocation virt_loc, gpointer user_data) @@ -2012,7 +2071,11 @@ gnc_split_register_get_debcred_entry, CRED_CELL); + gnc_table_model_set_entry_handler (model, + gnc_split_register_get_rbaln_entry, + RBALN_CELL); + gnc_table_model_set_label_handler (model, gnc_split_register_get_date_label, DATE_CELL); @@ -2101,7 +2164,11 @@ gnc_split_register_get_fcredit_label, FCRED_CELL); + gnc_table_model_set_label_handler (model, + gnc_split_register_get_tbalance_label, + RBALN_CELL); + gnc_table_model_set_default_help_handler( model, gnc_split_register_get_default_help); @@ -2231,7 +2298,10 @@ gnc_table_model_set_fg_color_handler( model, gnc_split_register_get_balance_fg_color, TBALN_CELL); + gnc_table_model_set_fg_color_handler( + model, gnc_split_register_get_balance_fg_color, RBALN_CELL); + gnc_table_model_set_default_bg_color_handler( model, gnc_split_register_get_bg_color); Index: src/register/ledger-core/split-register.h =================================================================== --- src/register/ledger-core/split-register.h (revision 18194) +++ src/register/ledger-core/split-register.h (working copy) @@ -200,6 +200,7 @@ #define NUM_CELL "num" #define PRIC_CELL "price" #define RATE_CELL "exchrate" +#define RBALN_CELL "reg-run-balance" #define RECN_CELL "reconcile" #define SHRS_CELL "shares" #define TBALN_CELL "trans-balance" Index: src/register/ledger-core/split-register-layout.c =================================================================== --- src/register/ledger-core/split-register-layout.c (revision 18194) +++ src/register/ledger-core/split-register-layout.c (working copy) @@ -248,7 +248,7 @@ gnc_table_layout_set_cell (layout, curs, DEBT_CELL, 0, 5); gnc_table_layout_set_cell (layout, curs, CRED_CELL, 0, 6); } - gnc_table_layout_set_cell (layout, curs, RATE_CELL, 0, 7); + gnc_table_layout_set_cell (layout, curs, RATE_CELL, 0, 8); curs_last = curs; curs = gnc_table_layout_get_cursor (layout, @@ -268,7 +268,8 @@ gnc_table_layout_set_cell (layout, curs, DESC_CELL, 0, 2); gnc_table_layout_set_cell (layout, curs, TDEBT_CELL, 0, 5); gnc_table_layout_set_cell (layout, curs, TCRED_CELL, 0, 6); - gnc_table_layout_set_cell (layout, curs, RATE_CELL, 0, 7); + gnc_table_layout_set_cell (layout, curs, RBALN_CELL, 0, 7); + gnc_table_layout_set_cell (layout, curs, RATE_CELL, 0, 8); curs_last = curs; curs = gnc_table_layout_get_cursor (layout, @@ -702,6 +703,14 @@ CELL_ALIGN_LEFT, FALSE, FALSE); + + gnc_register_add_cell (layout, + RBALN_CELL, + PRICE_CELL_TYPE_NAME, + N_("sample:999,999.000") + 7, + CELL_ALIGN_RIGHT, + FALSE, + FALSE); } TableLayout *
_______________________________________________ gnucash-devel mailing list gnucash-devel@gnucash.org https://lists.gnucash.org/mailman/listinfo/gnucash-devel