30#include <glib/gi18n.h>
33#include "dialog-utils.h"
35#include "gnc-amount-edit.h"
36#include "gnc-commodity-edit.h"
38#include "gnc-general-select.h"
39#include "gnc-component-manager.h"
41#include "gnc-date-edit.h"
44#include "gnc-session.h"
47#include "gnc-warnings.h"
48#include "engine-helpers.h"
51#define DIALOG_PRICE_EDIT_CM_CLASS "dialog-price-edit"
52#define GNC_PREFS_GROUP "dialogs.price-editor"
55G_GNUC_UNUSED
static QofLogModule log_module = GNC_MOD_GUI;
64 GNCPriceEditType type;
66 GtkWidget * namespace_cbwe;
67 GtkWidget * commodity_cbwe;
68 GtkWidget * currency_edit;
69 GtkWidget * date_edit;
70 GtkWidget * source_entry;
71 GtkWidget * type_combobox;
72 GtkWidget * price_edit;
74 GtkWidget * help_button;
75 GtkWidget * cancel_button;
76 GtkWidget * apply_button;
77 GtkWidget * ok_button;
85void pedit_dialog_response_cb (GtkDialog *dialog, gint response, gpointer data);
86void pedit_data_changed_cb (GtkWidget *w, gpointer data);
87void pedit_commodity_ns_changed_cb (GtkComboBox *cbwe, gpointer data);
88void pedit_commodity_changed_cb (GtkComboBox *cbwe, gpointer data);
94 pedit_dialog->changed = changed;
96 gtk_widget_set_sensitive (pedit_dialog->apply_button, changed);
97 gtk_widget_set_sensitive (pedit_dialog->ok_button, changed);
102type_string_to_index (
const char *type)
104 if (g_strcmp0 (type,
"bid") == 0)
107 if (g_strcmp0 (type,
"ask") == 0)
110 if (g_strcmp0 (type,
"last") == 0)
113 if (g_strcmp0 (type,
"nav") == 0)
121type_index_to_string (
int index)
143 gnc_commodity *commodity = NULL;
144 gnc_commodity *currency = NULL;
145 const gchar *name_space, *fullname;
151 if (pedit_dialog->price)
153 commodity = gnc_price_get_commodity (pedit_dialog->price);
160 gnc_ui_update_namespace_picker(pedit_dialog->namespace_cbwe,
163 name_space, fullname);
165 currency = gnc_price_get_currency (pedit_dialog->price);
166 date = gnc_price_get_time64 (pedit_dialog->price);
167 source = gnc_price_get_source_string (pedit_dialog->price);
168 type = gnc_price_get_typestr (pedit_dialog->price);
169 value = gnc_price_get_value (pedit_dialog->price);
175 source =
"user:price-editor";
177 value = gnc_numeric_zero ();
184 (GNC_CURRENCY_EDIT (pedit_dialog->currency_edit), currency);
187 gnc_date_edit_set_time (GNC_DATE_EDIT (pedit_dialog->date_edit), date);
189 gtk_entry_set_text (GTK_ENTRY (pedit_dialog->source_entry), source);
191 gtk_combo_box_set_active (GTK_COMBO_BOX(pedit_dialog->type_combobox),
192 type_string_to_index (type));
194 print_info = gnc_commodity_print_info (currency, FALSE);
195 gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), print_info);
196 gnc_amount_edit_set_fraction (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), 0);
198 gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), value);
204 const gnc_commodity *commodity,
205 const gnc_commodity *currency,
time64 t)
207 gboolean price_found = FALSE;
209 commodity, currency, t);
213 if (pedit_dialog->is_new)
217 if (!gnc_price_equal (test_price, pedit_dialog->price))
227 gchar *message =
_(
"Are you sure you want to replace the existing price?");
229 dialog = gtk_message_dialog_new (GTK_WINDOW (pedit_dialog->dialog),
230 GTK_DIALOG_DESTROY_WITH_PARENT,
231 GTK_MESSAGE_QUESTION,
233 "%s",
_(
"Replace price?"));
234 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG(dialog),
237 gtk_dialog_add_buttons (GTK_DIALOG(dialog),
238 _(
"_Cancel"), GTK_RESPONSE_CANCEL,
239 _(
"_Replace"), GTK_RESPONSE_YES,
241 gtk_dialog_set_default_response (GTK_DIALOG(dialog), GTK_RESPONSE_YES);
242 response = gnc_dialog_run (GTK_DIALOG(dialog), GNC_PREF_WARN_PRICE_QUOTES_REPLACE);
243 gtk_widget_destroy (dialog);
245 if (response == GTK_RESPONSE_CANCEL)
256 gnc_commodity *commodity;
257 gnc_commodity *currency;
259 const gchar *fullname;
266 fullname = gtk_entry_get_text( GTK_ENTRY( gtk_bin_get_child( GTK_BIN( GTK_COMBO_BOX(pedit_dialog->commodity_cbwe)))));
268 commodity = gnc_commodity_table_find_full(gnc_get_current_commodities(), name_space, fullname);
272 return _(
"You must select a Security.");
276 (GNC_CURRENCY_EDIT (pedit_dialog->currency_edit));
280 return _(
"You must select a Currency.");
283 date = gnc_date_edit_get_date (GNC_DATE_EDIT (pedit_dialog->date_edit));
285 source = gtk_entry_get_text (GTK_ENTRY (pedit_dialog->source_entry));
287 type = type_index_to_string
288 (gtk_combo_box_get_active (GTK_COMBO_BOX (pedit_dialog->type_combobox)));
290 print_info = gnc_commodity_print_info (currency, FALSE);
291 gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), print_info);
292 gnc_amount_edit_set_fraction (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), 0);
294 if (!gnc_amount_edit_evaluate (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), NULL))
297 return _(
"You must enter a valid amount.");
300 value = gnc_amount_edit_get_amount
301 (GNC_AMOUNT_EDIT (pedit_dialog->price_edit));
304 if (pedit_dialog_replace_found_price (pedit_dialog, commodity, currency, date))
306 if (!pedit_dialog->price)
308 gnc_price_begin_edit (pedit_dialog->price);
309 gnc_price_set_commodity (pedit_dialog->price, commodity);
310 gnc_price_set_currency (pedit_dialog->price, currency);
311 gnc_price_set_time64 (pedit_dialog->price, date);
312 gnc_price_set_source_string (pedit_dialog->price, source);
313 gnc_price_set_typestr (pedit_dialog->price, type);
314 gnc_price_set_value (pedit_dialog->price, value);
315 gnc_price_commit_edit (pedit_dialog->price);
328pedit_dialog_destroy_cb (GtkWidget *widget, gpointer data)
332 gnc_unregister_gui_component_by_data (DIALOG_PRICE_EDIT_CM_CLASS,
335 if (pedit_dialog->price)
338 pedit_dialog->price = NULL;
339 pedit_dialog->is_new = FALSE;
342 g_free (pedit_dialog);
347pedit_dialog_response_cb (GtkDialog *dialog, gint response, gpointer data)
350 GNCPrice *new_price = NULL;
351 const char *error_str;
353 if ((response == GTK_RESPONSE_OK) || (response == GTK_RESPONSE_APPLY))
355 error_str = gui_to_price (pedit_dialog);
356 if (g_strcmp0 (error_str,
"CANCEL") == 0)
359 gnc_prices_set_changed (pedit_dialog, FALSE);
364 gnc_warning_dialog (GTK_WINDOW (pedit_dialog->dialog),
"%s", error_str);
368 gnc_prices_set_changed (pedit_dialog, FALSE);
370 if (pedit_dialog->is_new)
373 gnc_gui_refresh_all ();
376 if (response == GTK_RESPONSE_APPLY)
379 pedit_dialog->is_new = TRUE;
382 pedit_dialog->price = new_price;
384 else if (response == GTK_RESPONSE_HELP)
386 gnc_gnome_help (GTK_WINDOW (pedit_dialog->dialog), DF_MANUAL, DL_PRICE_EDIT);
390 gnc_save_window_size(GNC_PREFS_GROUP, GTK_WINDOW(pedit_dialog->dialog));
391 gtk_widget_destroy (GTK_WIDGET (pedit_dialog->dialog));
392 pedit_dialog_destroy_cb (NULL, pedit_dialog);
398pedit_commodity_ns_changed_cb (GtkComboBox *cbwe, gpointer data)
403 gnc_prices_set_changed (pedit_dialog, TRUE);
413pedit_commodity_changed_cb (GtkComboBox *cbwe, gpointer data)
415 gnc_commodity *commodity = NULL;
416 gnc_commodity *currency = NULL;
418 const gchar *fullname;
422 gnc_prices_set_changed (pedit_dialog, TRUE);
425 fullname = gtk_entry_get_text( GTK_ENTRY( gtk_bin_get_child( GTK_BIN( GTK_COMBO_BOX(pedit_dialog->commodity_cbwe)))));
427 commodity = gnc_commodity_table_find_full(gnc_get_current_commodities(), name_space, fullname);
432 (pedit_dialog->price_db, commodity);
435 GNCPrice * price = (GNCPrice*)price_list->data;
437 currency = gnc_price_get_commodity((GNCPrice *)price);
439 currency = gnc_price_get_currency((GNCPrice *)price);
443 (GNC_CURRENCY_EDIT (pedit_dialog->currency_edit), currency);
459pedit_data_changed_cb (GtkWidget *w, gpointer data)
463 gnc_prices_set_changed (pedit_dialog, TRUE);
468gnc_price_pedit_dialog_create (GtkWidget *parent,
481 builder = gtk_builder_new();
482 gnc_builder_add_from_file (builder,
"dialog-price.glade",
"liststore1");
483 gnc_builder_add_from_file (builder,
"dialog-price.glade",
"liststore2");
484 gnc_builder_add_from_file (builder,
"dialog-price.glade",
"liststore3");
485 gnc_builder_add_from_file (builder,
"dialog-price.glade",
"price_dialog");
487 pedit_dialog->session = session;
491 dialog = GTK_WIDGET(gtk_builder_get_object (builder,
"price_dialog"));
492 pedit_dialog->dialog = dialog;
496 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
498 w = GTK_WIDGET(gtk_builder_get_object (builder,
"namespace_cbwe"));
499 pedit_dialog->namespace_cbwe = w;
502 gnc_cbwe_require_list_item(GTK_COMBO_BOX(pedit_dialog->namespace_cbwe));
503 gtk_combo_box_set_active(GTK_COMBO_BOX(pedit_dialog->namespace_cbwe), 1);
505 w = GTK_WIDGET(gtk_builder_get_object (builder,
"commodity_cbwe"));
506 pedit_dialog->commodity_cbwe = w;
508 gnc_cbwe_require_list_item(GTK_COMBO_BOX(pedit_dialog->commodity_cbwe));
513 box = GTK_WIDGET(gtk_builder_get_object (builder,
"currency_box"));
517 pedit_dialog->currency_edit = w;
518 gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
520 g_signal_connect (G_OBJECT (GTK_COMBO_BOX(w)),
"changed",
521 G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
522 label = GTK_WIDGET(gtk_builder_get_object (builder,
"currency_label"));
523 gtk_label_set_mnemonic_widget (GTK_LABEL(label), w);
525 box = GTK_WIDGET(gtk_builder_get_object (builder,
"date_box"));
526 w = gnc_date_edit_new (time (NULL), FALSE, FALSE);
527 pedit_dialog->date_edit = w;
528 gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
530 g_signal_connect (G_OBJECT (w),
"date_changed",
531 G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
532 g_signal_connect (G_OBJECT (GNC_DATE_EDIT (w)->date_entry),
"changed",
533 G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
534 gtk_entry_set_activates_default(GTK_ENTRY(GNC_DATE_EDIT(w)->date_entry), TRUE);
535 label = GTK_WIDGET(gtk_builder_get_object (builder,
"date__label"));
536 gnc_date_make_mnemonic_target (GNC_DATE_EDIT(w), label);
538 w = GTK_WIDGET(gtk_builder_get_object (builder,
"source_entry"));
539 pedit_dialog->source_entry = w;
541 w = GTK_WIDGET(gtk_builder_get_object (builder,
"type_combobox"));
542 pedit_dialog->type_combobox = w;
544 box = GTK_WIDGET(gtk_builder_get_object (builder,
"price_box"));
545 w = gnc_amount_edit_new ();
546 pedit_dialog->price_edit = w;
547 gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
548 entry = gnc_amount_edit_gtk_entry (GNC_AMOUNT_EDIT (w));
549 gnc_amount_edit_set_evaluate_on_enter (GNC_AMOUNT_EDIT (w), TRUE);
551 (GNC_CURRENCY_EDIT (pedit_dialog->currency_edit)));
552 gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (w), print_info);
553 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
555 label = GTK_WIDGET(gtk_builder_get_object (builder,
"price_label"));
556 gnc_amount_edit_make_mnemonic_target (GNC_AMOUNT_EDIT(w), label);
558 g_signal_connect (G_OBJECT (w),
"changed",
559 G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
561 w = GTK_WIDGET(gtk_builder_get_object (builder,
"pd_help_button"));
562 pedit_dialog->help_button = w;
564 w = GTK_WIDGET(gtk_builder_get_object (builder,
"pd_cancel_button"));
565 pedit_dialog->cancel_button = w;
567 w = GTK_WIDGET(gtk_builder_get_object (builder,
"pd_apply_button"));
568 pedit_dialog->apply_button = w;
570 w = GTK_WIDGET(gtk_builder_get_object (builder,
"pd_ok_button"));
571 pedit_dialog->ok_button = w;
573 gnc_prices_set_changed (pedit_dialog, FALSE);
575 gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, pedit_dialog);
577 g_object_unref(G_OBJECT(builder));
582close_handler (gpointer user_data)
586 gtk_dialog_response(GTK_DIALOG(pedit_dialog->dialog), GTK_RESPONSE_CANCEL);
591refresh_handler (GHashTable *changes, gpointer user_data)
600show_handler (
const char *klass, gint component_id,
601 gpointer user_data, gpointer iter_data)
604 GNCPrice * price = iter_data;
606 if (!pedit_dialog || (pedit_dialog->price != price))
609 gtk_window_present (GTK_WINDOW(pedit_dialog->dialog));
622gnc_price_edit_dialog (GtkWidget * parent,
625 GNCPriceEditType type)
630 if ((type == GNC_PRICE_EDIT) &&
631 (gnc_forall_gui_components (DIALOG_PRICE_EDIT_CM_CLASS,
632 show_handler, price)))
636 gnc_price_pedit_dialog_create (parent, pedit_dialog, session);
637 gnc_restore_window_size(GNC_PREFS_GROUP, GTK_WINDOW(pedit_dialog->dialog), GTK_WINDOW(parent));
638 pedit_dialog->type = type;
647 gnc_price_set_source (price, PRICE_SOURCE_EDIT_DLG);
648 gnc_price_set_time64 (price,
gnc_time (NULL));
649 gnc_price_set_value (price, gnc_numeric_zero ());
652 pedit_dialog->is_new = TRUE;
657 pedit_dialog->is_new = FALSE;
661 pedit_dialog->price = price;
662 price_to_gui(pedit_dialog);
663 gnc_prices_set_changed (pedit_dialog, FALSE);
664 component_id = gnc_register_gui_component (DIALOG_PRICE_EDIT_CM_CLASS,
665 refresh_handler, close_handler,
667 gnc_gui_component_set_session (component_id, pedit_dialog->session);
668 gtk_widget_grab_focus (pedit_dialog->commodity_cbwe);
669 gtk_widget_show (pedit_dialog->dialog);
681gnc_price_edit_by_guid (GtkWidget * parent,
const GncGUID * guid)
684 QofSession *session = gnc_get_current_session();
689 price = gnc_price_lookup (guid, book);
693 gnc_price_edit_dialog(parent, session, price, GNC_PRICE_EDIT);
"select" and "new" commodity windows
Currency selection widget.
a simple price database for gnucash
utility functions for the GnuCash UI
QofBook * qof_session_get_book(const QofSession *session)
Returns the QofBook of this session.
gboolean gnc_commodity_equiv(const gnc_commodity *a, const gnc_commodity *b)
This routine returns TRUE if the two commodities are equivalent.
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
Retrieve the namespace for the specified commodity.
const char * gnc_commodity_get_printname(const gnc_commodity *cm)
Retrieve the 'print' name for the specified commodity.
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
time64 gnc_time(time64 *tbuf)
get the current time
void gnc_currency_edit_set_currency(GNCCurrencyEdit *gce, const gnc_commodity *currency)
Set the widget to display a certain currency name.
GtkWidget * gnc_currency_edit_new(void)
Create a new GNCCurrencyEdit widget which can be used to provide an easy way to enter ISO currency co...
gnc_commodity * gnc_currency_edit_get_currency(GNCCurrencyEdit *gce)
Retrieve the displayed currency of the widget.
void gnc_ui_update_commodity_picker(GtkWidget *cbwe, const gchar *name_space, const gchar *init_string)
Given a combo box, fill in all the known commodities for the specified namespace, and then select one...
gchar * gnc_ui_namespace_picker_ns(GtkWidget *cbwe)
Given a combo box, return the currently selected namespaces.
@ DIAG_COMM_ALL
Dialog box should allow selection of anything.
void gnc_gnome_help(GtkWindow *parent, const char *file_name, const char *anchor)
Launch the systems default help browser, gnome's yelp for linux, and open to a given link within a gi...
gnc_commodity * gnc_default_currency(void)
Return the default currency set by the user.
PriceList * gnc_pricedb_lookup_latest_any_currency(GNCPriceDB *db, const gnc_commodity *commodity)
Find the most recent price between a commodity and all other commodities.
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
Return the pricedb associated with the book.
GNCPrice * gnc_pricedb_lookup_day_t64(GNCPriceDB *db, const gnc_commodity *c, const gnc_commodity *currency, time64 t)
Return the price between the two commodities on the indicated day.
gboolean gnc_pricedb_add_price(GNCPriceDB *db, GNCPrice *p)
Add a price to the pricedb.
void gnc_price_list_destroy(PriceList *prices)
gnc_price_list_destroy - destroy the given price list, calling gnc_price_unref on all the prices incl...
void gnc_price_ref(GNCPrice *p)
gnc_price_ref - indicate your need for a given price to stick around (i.e.
GNCPrice * gnc_price_clone(GNCPrice *p, QofBook *book)
gnc_price_clone - returns a newly allocated price that's a content-wise duplicate of the given price,...
GNCPrice * gnc_price_create(QofBook *book)
gnc_price_create - returns a newly allocated and initialized price with a reference count of 1.
void gnc_price_unref(GNCPrice *p)
gnc_price_unref - indicate you're finished with a price (i.e.
The type used to store guids in C.