29 #include <glib/gi18n.h>
32 #include "engine-helpers.h"
33 #include "dialog-utils.h"
34 #include "assistant-stock-split.h"
35 #include "assistant-utils.h"
36 #include "gnc-amount-edit.h"
37 #include "gnc-component-manager.h"
39 #include "gnc-date-edit.h"
41 #include "gnc-gui-query.h"
47 #define ASSISTANT_STOCK_SPLIT_CM_CLASS "assistant-stock-split"
51 SPLIT_COL_ACCOUNT = 0,
62 GtkWidget * assistant;
65 GtkWidget * account_view;
69 GtkWidget * date_edit;
70 GtkWidget * distribution_edit;
71 GtkWidget * description_entry;
72 GtkWidget * price_edit;
73 GtkWidget * price_currency_edit;
76 GtkWidget * cash_edit;
77 GtkWidget * memo_entry;
78 GtkWidget * income_tree;
79 GtkWidget * asset_tree;
84 void gnc_stock_split_assistant_window_destroy_cb (GtkObject *
object, gpointer user_data);
85 void gnc_stock_split_assistant_prepare (GtkAssistant *assistant,
88 void gnc_stock_split_assistant_details_prepare (GtkAssistant *assistant,
90 gboolean gnc_stock_split_assistant_details_complete (GtkAssistant *assistant,
92 gboolean gnc_stock_split_assistant_cash_complete (GtkAssistant *assistant,
94 void gnc_stock_split_assistant_finish (GtkAssistant *assistant,
96 void gnc_stock_split_assistant_cancel (GtkAssistant *gtkassistant,
101 gnc_stock_split_assistant_window_destroy_cb (GtkObject *
object, gpointer user_data)
105 gnc_unregister_gui_component_by_data (ASSISTANT_STOCK_SPLIT_CM_CLASS, info);
114 GtkTreeRowReference *reference = NULL;
124 view = GTK_TREE_VIEW(info->account_view);
125 list = GTK_LIST_STORE(gtk_tree_view_get_model(view));
127 gtk_list_store_clear (list);
130 for (node = accounts; node; node = node->next)
150 print_info = gnc_account_print_info (account, FALSE);
152 gtk_list_store_append(list, &iter);
153 gtk_list_store_set(list, &iter,
154 SPLIT_COL_ACCOUNT, account,
155 SPLIT_COL_FULLNAME, full_name,
157 SPLIT_COL_SHARES, xaccPrintAmount(balance, print_info),
160 if (account == selected_account)
162 path = gtk_tree_model_get_path(GTK_TREE_MODEL(list), &iter);
163 reference = gtk_tree_row_reference_new(GTK_TREE_MODEL(list), path);
164 gtk_tree_path_free(path);
171 g_list_free(accounts);
175 GtkTreeSelection* selection = gtk_tree_view_get_selection(view);
176 path = gtk_tree_row_reference_get_path(reference);
177 gtk_tree_row_reference_free(reference);
180 gtk_tree_selection_select_path(selection, path);
181 gtk_tree_view_scroll_to_cell(view, path, NULL, TRUE, 0.5, 0.0);
182 gtk_tree_path_free(path);
191 selection_changed_cb (GtkTreeSelection *selection,
198 if (!gtk_tree_selection_get_selected(selection, &list, &iter))
200 gtk_tree_model_get(list, &iter,
201 SPLIT_COL_ACCOUNT, &info->acct,
216 account = info->acct;
218 g_return_if_fail (account != NULL);
220 print_info = gnc_account_print_info (account, FALSE);
222 gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (info->distribution_edit),
224 gnc_amount_edit_set_fraction (GNC_AMOUNT_EDIT (info->distribution_edit),
228 book = gnc_account_get_book (account);
235 currency = gnc_price_get_currency(prices->data);
245 (GNC_CURRENCY_EDIT (info->price_currency_edit),
250 void gnc_stock_split_assistant_prepare (GtkAssistant *assistant, GtkWidget *page,
253 gint currentpage = gtk_assistant_get_current_page(assistant);
255 if (currentpage == 2)
256 gnc_stock_split_assistant_details_prepare(assistant, user_data);
261 gnc_stock_split_assistant_details_prepare (GtkAssistant *assistant,
266 refresh_details_page(info);
271 gnc_stock_split_assistant_details_complete (GtkAssistant *assistant,
278 result = gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT (info->distribution_edit), &amount, TRUE);
285 result = gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT (info->price_edit), &amount, TRUE);
288 else if ( result > 0)
298 gnc_stock_split_assistant_cash_complete (GtkAssistant *assistant,
306 result = gnc_amount_edit_expr_is_valid (GNC_AMOUNT_EDIT (info->cash_edit), &amount, TRUE);
309 else if ( result > 0)
328 gnc_stock_split_assistant_finish (GtkAssistant *assistant,
332 GList *account_commits;
341 account = info->acct;
342 g_return_if_fail (account != NULL);
344 amount = gnc_amount_edit_get_amount
345 (GNC_AMOUNT_EDIT (info->distribution_edit));
348 gnc_suspend_gui_refresh ();
356 date = gnc_date_edit_get_date (GNC_DATE_EDIT (info->date_edit));
360 const char *description;
362 description = gtk_entry_get_text (GTK_ENTRY (info->description_entry));
369 account_commits = g_list_prepend (NULL, account);
380 gnc_set_num_action (NULL, split, NULL, Q_(
"Action Column|Split"));
382 amount = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT (info->price_edit));
391 ce = GNC_CURRENCY_EDIT (info->price_currency_edit);
398 gnc_price_begin_edit (price);
401 gnc_price_set_time (price, ts);
402 gnc_price_set_source (price,
"user:stock-split");
403 gnc_price_set_typestr (price,
"unknown");
404 gnc_price_set_value (price, amount);
405 gnc_price_commit_edit (price);
407 book = gnc_get_current_book ();
411 gnc_error_dialog (info->window,
"%s", _(
"Error adding price."));
416 amount = gnc_amount_edit_get_amount (GNC_AMOUNT_EDIT (info->cash_edit));
421 memo = gtk_entry_get_text (GTK_ENTRY (info->memo_entry));
429 account_commits = g_list_prepend (account_commits, account);
447 account_commits = g_list_prepend (account_commits, account);
461 for (node = account_commits; node; node = node->next)
463 g_list_free (account_commits);
465 gnc_resume_gui_refresh ();
467 gnc_close_gui_component_by_data (ASSISTANT_STOCK_SPLIT_CM_CLASS, info);
472 gnc_stock_split_assistant_cancel (GtkAssistant *assistant, gpointer user_data)
475 gnc_close_gui_component_by_data (ASSISTANT_STOCK_SPLIT_CM_CLASS, info);
480 gnc_stock_split_assistant_view_filter_income (
Account *account,
491 gnc_stock_split_assistant_view_filter_asset (
Account *account,
503 gnc_stock_split_details_valid_cb (GtkWidget *widget, gpointer user_data)
506 GtkAssistant *assistant = GTK_ASSISTANT(info->window);
507 gint num = gtk_assistant_get_current_page (assistant);
508 GtkWidget *page = gtk_assistant_get_nth_page (assistant, num);
510 gtk_assistant_set_page_complete (assistant, page,
511 gnc_stock_split_assistant_details_complete (assistant, user_data));
516 gnc_stock_split_cash_valid_cb (GtkWidget *widget, gpointer user_data)
519 GtkAssistant *assistant = GTK_ASSISTANT(info->window);
520 gint num = gtk_assistant_get_current_page (assistant);
521 GtkWidget *page = gtk_assistant_get_nth_page (assistant, num);
523 gtk_assistant_set_page_complete (assistant, page,
524 gnc_stock_split_assistant_cash_complete (assistant, user_data));
534 builder = gtk_builder_new();
535 gnc_builder_add_from_file (builder ,
"assistant-stock-split.glade",
"Stock Split Assistant");
536 window = GTK_WIDGET(gtk_builder_get_object (builder,
"Stock Split Assistant"));
537 info->window = window;
540 gnc_assistant_set_colors (GTK_ASSISTANT (info->window));
543 gtk_assistant_set_page_complete (GTK_ASSISTANT (window),
544 GTK_WIDGET(gtk_builder_get_object(builder,
"intro_page_label")),
546 gtk_assistant_set_page_complete (GTK_ASSISTANT (window),
547 GTK_WIDGET(gtk_builder_get_object(builder,
"stock_account_page")),
549 gtk_assistant_set_page_complete (GTK_ASSISTANT (window),
550 GTK_WIDGET(gtk_builder_get_object(builder,
"stock_cash_page")),
552 gtk_assistant_set_page_complete (GTK_ASSISTANT (window),
553 GTK_WIDGET(gtk_builder_get_object(builder,
"finish_page_label")),
560 GtkTreeSelection *selection;
561 GtkCellRenderer *renderer;
562 GtkTreeViewColumn *column;
564 info->account_view = GTK_WIDGET(gtk_builder_get_object(builder,
"account_view"));
566 view = GTK_TREE_VIEW(info->account_view);
568 store = gtk_list_store_new(NUM_SPLIT_COLS, G_TYPE_POINTER, G_TYPE_STRING,
569 G_TYPE_STRING, G_TYPE_STRING);
570 gtk_tree_view_set_model(view, GTK_TREE_MODEL(store));
571 g_object_unref(store);
573 renderer = gtk_cell_renderer_text_new();
574 column = gtk_tree_view_column_new_with_attributes(_(
"Account"), renderer,
575 "text", SPLIT_COL_FULLNAME,
577 gtk_tree_view_append_column(view, column);
579 renderer = gtk_cell_renderer_text_new();
580 column = gtk_tree_view_column_new_with_attributes(_(
"Symbol"), renderer,
581 "text", SPLIT_COL_MNEMONIC,
583 gtk_tree_view_append_column(view, column);
585 renderer = gtk_cell_renderer_text_new();
586 column = gtk_tree_view_column_new_with_attributes(_(
"Shares"), renderer,
587 "text", SPLIT_COL_SHARES,
589 gtk_tree_view_append_column(view, column);
591 selection = gtk_tree_view_get_selection(view);
592 gtk_tree_selection_set_mode(selection, GTK_SELECTION_BROWSE);
593 g_signal_connect (selection,
"changed",
594 G_CALLBACK (selection_changed_cb), info);
605 table = GTK_WIDGET(gtk_builder_get_object(builder,
"stock_details_table"));
606 info->description_entry = GTK_WIDGET(gtk_builder_get_object(builder,
"description_entry"));
608 date = gnc_date_edit_new (
gnc_time (NULL), FALSE, FALSE);
609 gtk_table_attach_defaults (GTK_TABLE (table), date, 1, 2, 0, 1);
610 gtk_widget_show (date);
611 info->date_edit = date;
613 label = GTK_WIDGET(gtk_builder_get_object(builder,
"date_label"));
614 gnc_date_make_mnemonic_target (GNC_DATE_EDIT(date), label);
616 amount = gnc_amount_edit_new ();
617 g_signal_connect (amount,
"changed",
618 G_CALLBACK (gnc_stock_split_details_valid_cb), info);
619 gnc_amount_edit_set_evaluate_on_enter (GNC_AMOUNT_EDIT (amount), TRUE);
620 gtk_table_attach_defaults (GTK_TABLE (table), amount, 1, 2, 1, 2);
621 gtk_widget_show (amount);
622 info->distribution_edit = amount;
624 label = GTK_WIDGET(gtk_builder_get_object(builder,
"distribution_label"));
625 gtk_label_set_mnemonic_widget(GTK_LABEL(label), amount);
627 amount = gnc_amount_edit_new ();
628 gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (amount),
629 gnc_default_price_print_info ());
630 g_signal_connect (amount,
"changed",
631 G_CALLBACK (gnc_stock_split_details_valid_cb), info);
632 gnc_amount_edit_set_evaluate_on_enter (GNC_AMOUNT_EDIT (amount), TRUE);
633 gtk_table_attach_defaults (GTK_TABLE (table), amount, 1, 2, 5, 6);
634 gtk_widget_show (amount);
635 info->price_edit = amount;
637 label = GTK_WIDGET(gtk_builder_get_object(builder,
"price_label"));
638 gtk_label_set_mnemonic_widget(GTK_LABEL(label), amount);
642 gtk_widget_show (info->price_currency_edit);
643 gtk_table_attach_defaults (GTK_TABLE (table), info->price_currency_edit, 1, 2, 6, 7);
653 GtkTreeSelection *selection;
655 box = GTK_WIDGET(gtk_builder_get_object(builder,
"cash_box"));
656 amount = gnc_amount_edit_new ();
657 g_signal_connect (amount,
"changed",
658 G_CALLBACK (gnc_stock_split_cash_valid_cb), info);
659 gnc_amount_edit_set_evaluate_on_enter (GNC_AMOUNT_EDIT (amount), TRUE);
660 gtk_box_pack_start (GTK_BOX (box), amount, TRUE, TRUE, 0);
661 info->cash_edit = amount;
663 label = GTK_WIDGET(gtk_builder_get_object(builder,
"cash_label"));
664 gtk_label_set_mnemonic_widget(GTK_LABEL(label), amount);
666 info->memo_entry = GTK_WIDGET(gtk_builder_get_object(builder,
"memo_entry"));
670 info->income_tree = tree;
672 gnc_stock_split_assistant_view_filter_income,
676 gtk_widget_show (tree);
678 gtk_tree_view_expand_all (GTK_TREE_VIEW(tree));
679 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(tree));
680 gtk_tree_selection_unselect_all (selection);
681 g_signal_connect (selection,
"changed",
682 G_CALLBACK (gnc_stock_split_cash_valid_cb), info);
684 label = GTK_WIDGET(gtk_builder_get_object(builder,
"income_label"));
685 gtk_label_set_mnemonic_widget (GTK_LABEL(label), tree);
687 scroll = GTK_WIDGET(gtk_builder_get_object(builder,
"income_scroll"));
688 gtk_container_add (GTK_CONTAINER (scroll), tree);
692 info->asset_tree = tree;
694 gnc_stock_split_assistant_view_filter_asset,
698 gtk_widget_show (tree);
700 label = GTK_WIDGET(gtk_builder_get_object(builder,
"asset_label"));
701 gtk_label_set_mnemonic_widget (GTK_LABEL(label), tree);
703 scroll = GTK_WIDGET(gtk_builder_get_object(builder,
"asset_scroll"));
704 gtk_container_add (GTK_CONTAINER (scroll), tree);
706 gtk_tree_view_expand_all (GTK_TREE_VIEW(tree));
707 selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(tree));
708 gtk_tree_selection_unselect_all (selection);
709 g_signal_connect (selection,
"changed",
710 G_CALLBACK (gnc_stock_split_cash_valid_cb), info);
713 g_signal_connect (G_OBJECT(window),
"destroy",
714 G_CALLBACK (gnc_stock_split_assistant_window_destroy_cb), info);
716 gtk_builder_connect_signals(builder, info);
717 g_object_unref(G_OBJECT(builder));
723 refresh_handler (GHashTable *changes, gpointer user_data)
728 old_account = info->acct;
730 if (fill_account_list (info, info->acct) == 0)
732 gnc_close_gui_component_by_data (ASSISTANT_STOCK_SPLIT_CM_CLASS, info);
736 if (NULL == info->acct || old_account == info->acct)
return;
740 close_handler (gpointer user_data)
744 gtk_widget_destroy (info->window);
756 gnc_stock_split_dialog (GtkWidget *parent,
Account * initial)
765 gnc_stock_split_assistant_create (info);
767 component_id = gnc_register_gui_component (ASSISTANT_STOCK_SPLIT_CM_CLASS,
768 refresh_handler, close_handler,
771 gnc_gui_component_watch_entity_type (component_id,
773 QOF_EVENT_MODIFY | QOF_EVENT_DESTROY);
775 if (fill_account_list (info, initial) == 0)
777 gnc_warning_dialog (parent,
"%s", _(
"You don't have any stock accounts with balances!"));
778 gnc_close_gui_component_by_data (ASSISTANT_STOCK_SPLIT_CM_CLASS, info);
782 gtk_widget_show_all (info->window);
784 gnc_window_adjust_for_screen (GTK_WINDOW(info->window));
void xaccSplitSetValue(Split *s, gnc_numeric amt)
void gnc_price_list_destroy(PriceList *prices)
GNCPrice * gnc_price_create(QofBook *book)
#define xaccTransAppendSplit(t, s)
Transaction * xaccMallocTransaction(QofBook *book)
void xaccTransSetDatePostedSecsNormalized(Transaction *trans, time64 time)
void xaccSplitMakeStockSplit(Split *s)
GList * gnc_account_get_descendants_sorted(const Account *account)
void gnc_currency_edit_set_currency(GNCCurrencyEdit *gce, const gnc_commodity *currency)
const char * gnc_commodity_get_mnemonic(const gnc_commodity *cm)
gboolean xaccAccountIsPriced(const Account *acc)
utility functions for the GnuCash UI
GNCAccountType xaccAccountGetType(const Account *acc)
int xaccAccountGetCommoditySCU(const Account *acc)
gnc_numeric gnc_numeric_neg(gnc_numeric a)
void gnc_price_unref(GNCPrice *p)
gboolean gnc_pricedb_add_price(GNCPriceDB *db, GNCPrice *p)
void xaccTransSetDescription(Transaction *trans, const char *desc)
Use a 64-bit unsigned int timespec.
gboolean gnc_numeric_zero_p(gnc_numeric a)
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
gboolean gnc_numeric_negative_p(gnc_numeric a)
void xaccTransSetCurrency(Transaction *trans, gnc_commodity *curr)
gnc_commodity * gnc_default_currency(void)
Currency selection widget.
void xaccSplitSetAmount(Split *s, gnc_numeric amt)
void gnc_tree_view_account_set_filter(GncTreeViewAccount *view, gnc_tree_view_account_filter_func func, gpointer data, GSourceFunc destroy)
gchar * gnc_account_get_full_name(const Account *account)
GtkTreeView implementation for gnucash account tree.
void xaccSplitSetMemo(Split *split, const char *memo)
GtkTreeView * gnc_tree_view_account_new(gboolean show_root)
gnc_commodity * gnc_currency_edit_get_currency(GNCCurrencyEdit *gce)
PriceList * gnc_pricedb_lookup_latest_any_currency(GNCPriceDB *db, const gnc_commodity *commodity)
void xaccTransCommitEdit(Transaction *trans)
void xaccTransBeginEdit(Transaction *trans)
gnc_numeric xaccAccountGetBalance(const Account *acc)
gboolean gnc_numeric_positive_p(gnc_numeric a)
Split * xaccMallocSplit(QofBook *book)
GtkWidget * gnc_currency_edit_new(void)
void xaccAccountBeginEdit(Account *acc)
gnc_commodity * xaccAccountGetCommodity(const Account *acc)
#define xaccAccountInsertSplit(acc, s)
gboolean xaccAccountGetPlaceholder(const Account *acc)
Account * gnc_tree_view_account_get_selected_account(GncTreeViewAccount *view)
time64 gnc_time(time64 *tbuf)
get the current local time
API for Transactions and Splits (journal entries)
void xaccAccountCommitEdit(Account *acc)