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" 55 G_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;
85 void pedit_dialog_response_cb (GtkDialog *dialog, gint response, gpointer data);
86 void pedit_data_changed_cb (GtkWidget *w, gpointer data);
87 void pedit_commodity_ns_changed_cb (GtkComboBox *cbwe, gpointer data);
88 void pedit_commodity_changed_cb (GtkComboBox *cbwe, gpointer data);
92 gnc_prices_set_changed (
PriceEditDialog *pedit_dialog, gboolean changed)
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);
102 type_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)
121 type_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);
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);
270 return _(
"You must select a Security.");
273 (GNC_CURRENCY_EDIT (pedit_dialog->currency_edit));
275 return _(
"You must select a Currency.");
277 date = gnc_date_edit_get_date (GNC_DATE_EDIT (pedit_dialog->date_edit));
279 source = gtk_entry_get_text (GTK_ENTRY (pedit_dialog->source_entry));
281 type = type_index_to_string
282 (gtk_combo_box_get_active (GTK_COMBO_BOX (pedit_dialog->type_combobox)));
284 print_info = gnc_commodity_print_info (currency, FALSE);
285 gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), print_info);
286 gnc_amount_edit_set_fraction (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), 0);
288 if (!gnc_amount_edit_evaluate (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), NULL))
289 return _(
"You must enter a valid amount.");
291 value = gnc_amount_edit_get_amount
292 (GNC_AMOUNT_EDIT (pedit_dialog->price_edit));
295 if (pedit_dialog_replace_found_price (pedit_dialog, commodity, currency, date))
297 if (!pedit_dialog->price)
299 gnc_price_begin_edit (pedit_dialog->price);
300 gnc_price_set_commodity (pedit_dialog->price, commodity);
301 gnc_price_set_currency (pedit_dialog->price, currency);
302 gnc_price_set_time64 (pedit_dialog->price, date);
303 gnc_price_set_source_string (pedit_dialog->price, source);
304 gnc_price_set_typestr (pedit_dialog->price, type);
305 gnc_price_set_value (pedit_dialog->price, value);
306 gnc_price_commit_edit (pedit_dialog->price);
319 pedit_dialog_destroy_cb (GtkWidget *widget, gpointer data)
323 gnc_unregister_gui_component_by_data (DIALOG_PRICE_EDIT_CM_CLASS,
326 if (pedit_dialog->price)
329 pedit_dialog->price = NULL;
330 pedit_dialog->is_new = FALSE;
333 g_free (pedit_dialog);
338 pedit_dialog_response_cb (GtkDialog *dialog, gint response, gpointer data)
341 GNCPrice *new_price = NULL;
342 const char *error_str;
344 if ((response == GTK_RESPONSE_OK) || (response == GTK_RESPONSE_APPLY))
346 error_str = gui_to_price (pedit_dialog);
347 if (g_strcmp0 (error_str,
"CANCEL") == 0)
350 gnc_prices_set_changed (pedit_dialog, FALSE);
355 gnc_warning_dialog (GTK_WINDOW (pedit_dialog->dialog),
"%s", error_str);
359 gnc_prices_set_changed (pedit_dialog, FALSE);
361 if (pedit_dialog->is_new)
364 gnc_gui_refresh_all ();
367 if (response == GTK_RESPONSE_APPLY)
370 pedit_dialog->is_new = TRUE;
373 pedit_dialog->price = new_price;
375 else if (response == GTK_RESPONSE_HELP)
377 gnc_gnome_help (GTK_WINDOW (pedit_dialog->dialog), DF_MANUAL, DL_PRICE_EDIT);
381 gnc_save_window_size(GNC_PREFS_GROUP, GTK_WINDOW(pedit_dialog->dialog));
382 gtk_widget_destroy (GTK_WIDGET (pedit_dialog->dialog));
383 pedit_dialog_destroy_cb (NULL, pedit_dialog);
389 pedit_commodity_ns_changed_cb (GtkComboBox *cbwe, gpointer data)
394 gnc_prices_set_changed (pedit_dialog, TRUE);
404 pedit_commodity_changed_cb (GtkComboBox *cbwe, gpointer data)
406 gnc_commodity *commodity = NULL;
407 gnc_commodity *currency = NULL;
409 const gchar *fullname;
413 gnc_prices_set_changed (pedit_dialog, TRUE);
416 fullname = gtk_entry_get_text( GTK_ENTRY( gtk_bin_get_child( GTK_BIN( GTK_COMBO_BOX(pedit_dialog->commodity_cbwe)))));
418 commodity = gnc_commodity_table_find_full(gnc_get_current_commodities(), name_space, fullname);
423 (pedit_dialog->price_db, commodity);
426 GNCPrice * price = (GNCPrice*)price_list->data;
428 currency = gnc_price_get_commodity((GNCPrice *)price);
430 currency = gnc_price_get_currency((GNCPrice *)price);
434 (GNC_CURRENCY_EDIT (pedit_dialog->currency_edit), currency);
450 pedit_data_changed_cb (GtkWidget *w, gpointer data)
454 gnc_prices_set_changed (pedit_dialog, TRUE);
459 gnc_price_pedit_dialog_create (GtkWidget *parent,
472 builder = gtk_builder_new();
473 gnc_builder_add_from_file (builder,
"dialog-price.glade",
"liststore1");
474 gnc_builder_add_from_file (builder,
"dialog-price.glade",
"liststore2");
475 gnc_builder_add_from_file (builder,
"dialog-price.glade",
"liststore3");
476 gnc_builder_add_from_file (builder,
"dialog-price.glade",
"price_dialog");
478 pedit_dialog->session = session;
482 dialog = GTK_WIDGET(gtk_builder_get_object (builder,
"price_dialog"));
483 pedit_dialog->dialog = dialog;
487 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
489 w = GTK_WIDGET(gtk_builder_get_object (builder,
"namespace_cbwe"));
490 pedit_dialog->namespace_cbwe = w;
493 gnc_cbwe_require_list_item(GTK_COMBO_BOX(pedit_dialog->namespace_cbwe));
494 gtk_combo_box_set_active(GTK_COMBO_BOX(pedit_dialog->namespace_cbwe), 1);
496 w = GTK_WIDGET(gtk_builder_get_object (builder,
"commodity_cbwe"));
497 pedit_dialog->commodity_cbwe = w;
499 gnc_cbwe_require_list_item(GTK_COMBO_BOX(pedit_dialog->commodity_cbwe));
504 box = GTK_WIDGET(gtk_builder_get_object (builder,
"currency_box"));
508 pedit_dialog->currency_edit = w;
509 gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
511 g_signal_connect (G_OBJECT (GTK_COMBO_BOX(w)),
"changed",
512 G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
513 label = GTK_WIDGET(gtk_builder_get_object (builder,
"currency_label"));
514 gtk_label_set_mnemonic_widget (GTK_LABEL(label), w);
516 box = GTK_WIDGET(gtk_builder_get_object (builder,
"date_box"));
517 w = gnc_date_edit_new (time (NULL), FALSE, FALSE);
518 pedit_dialog->date_edit = w;
519 gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
521 g_signal_connect (G_OBJECT (w),
"date_changed",
522 G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
523 g_signal_connect (G_OBJECT (GNC_DATE_EDIT (w)->date_entry),
"changed",
524 G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
525 gtk_entry_set_activates_default(GTK_ENTRY(GNC_DATE_EDIT(w)->date_entry), TRUE);
526 label = GTK_WIDGET(gtk_builder_get_object (builder,
"date__label"));
527 gnc_date_make_mnemonic_target (GNC_DATE_EDIT(w), label);
529 w = GTK_WIDGET(gtk_builder_get_object (builder,
"source_entry"));
530 pedit_dialog->source_entry = w;
532 w = GTK_WIDGET(gtk_builder_get_object (builder,
"type_combobox"));
533 pedit_dialog->type_combobox = w;
535 box = GTK_WIDGET(gtk_builder_get_object (builder,
"price_box"));
536 w = gnc_amount_edit_new ();
537 pedit_dialog->price_edit = w;
538 gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
539 entry = gnc_amount_edit_gtk_entry (GNC_AMOUNT_EDIT (w));
540 gnc_amount_edit_set_evaluate_on_enter (GNC_AMOUNT_EDIT (w), TRUE);
542 (GNC_CURRENCY_EDIT (pedit_dialog->currency_edit)));
543 gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (w), print_info);
544 gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
546 label = GTK_WIDGET(gtk_builder_get_object (builder,
"price_label"));
547 gnc_amount_edit_make_mnemonic_target (GNC_AMOUNT_EDIT(w), label);
549 g_signal_connect (G_OBJECT (w),
"changed",
550 G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
552 w = GTK_WIDGET(gtk_builder_get_object (builder,
"pd_help_button"));
553 pedit_dialog->help_button = w;
555 w = GTK_WIDGET(gtk_builder_get_object (builder,
"pd_cancel_button"));
556 pedit_dialog->cancel_button = w;
558 w = GTK_WIDGET(gtk_builder_get_object (builder,
"pd_apply_button"));
559 pedit_dialog->apply_button = w;
561 w = GTK_WIDGET(gtk_builder_get_object (builder,
"pd_ok_button"));
562 pedit_dialog->ok_button = w;
564 gnc_prices_set_changed (pedit_dialog, FALSE);
566 gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, pedit_dialog);
568 g_object_unref(G_OBJECT(builder));
573 close_handler (gpointer user_data)
577 gtk_dialog_response(GTK_DIALOG(pedit_dialog->dialog), GTK_RESPONSE_CANCEL);
582 refresh_handler (GHashTable *changes, gpointer user_data)
591 show_handler (
const char *klass, gint component_id,
592 gpointer user_data, gpointer iter_data)
595 GNCPrice * price = iter_data;
597 if (!pedit_dialog || (pedit_dialog->price != price))
600 gtk_window_present (GTK_WINDOW(pedit_dialog->dialog));
613 gnc_price_edit_dialog (GtkWidget * parent,
616 GNCPriceEditType type)
621 if ((type == GNC_PRICE_EDIT) &&
622 (gnc_forall_gui_components (DIALOG_PRICE_EDIT_CM_CLASS,
623 show_handler, price)))
627 gnc_price_pedit_dialog_create (parent, pedit_dialog, session);
628 gnc_restore_window_size(GNC_PREFS_GROUP, GTK_WINDOW(pedit_dialog->dialog), GTK_WINDOW(parent));
629 pedit_dialog->type = type;
638 gnc_price_set_source (price, PRICE_SOURCE_EDIT_DLG);
639 gnc_price_set_time64 (price,
gnc_time (NULL));
640 gnc_price_set_value (price, gnc_numeric_zero ());
643 pedit_dialog->is_new = TRUE;
648 pedit_dialog->is_new = FALSE;
652 pedit_dialog->price = price;
653 price_to_gui(pedit_dialog);
654 gnc_prices_set_changed (pedit_dialog, FALSE);
655 component_id = gnc_register_gui_component (DIALOG_PRICE_EDIT_CM_CLASS,
656 refresh_handler, close_handler,
658 gnc_gui_component_set_session (component_id, pedit_dialog->session);
659 gtk_widget_grab_focus (pedit_dialog->commodity_cbwe);
660 gtk_widget_show (pedit_dialog->dialog);
672 gnc_price_edit_by_guid (GtkWidget * parent,
const GncGUID * guid)
675 QofSession *session = gnc_get_current_session();
680 price = gnc_price_lookup (guid, book);
684 gnc_price_edit_dialog(parent, session, price, GNC_PRICE_EDIT);
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...
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.
GNCPrice * gnc_price_create(QofBook *book)
gnc_price_create - returns a newly allocated and initialized price with a reference count of 1...
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...
void gnc_currency_edit_set_currency(GNCCurrencyEdit *gce, const gnc_commodity *currency)
Set the widget to display a certain currency name.
a simple price database for gnucash
utility functions for the GnuCash UI
gchar * gnc_ui_namespace_picker_ns(GtkWidget *cbwe)
Given a combo box, return the currently selected namespaces.
void gnc_price_unref(GNCPrice *p)
gnc_price_unref - indicate you're finished with a price (i.e.
gboolean gnc_pricedb_add_price(GNCPriceDB *db, GNCPrice *p)
Add a price to the pricedb.
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
Retrieve the namespace for the specified commodity.
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
Return the pricedb associated with the book.
gnc_commodity * gnc_default_currency(void)
Return the default currency set by the user.
Dialog box should allow selection of anything.
Currency selection widget.
QofBook * qof_session_get_book(const QofSession *session)
Returns the QofBook of this session.
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_currency_edit_get_currency(GNCCurrencyEdit *gce)
Retrieve the displayed currency of the widget.
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.
const char * gnc_commodity_get_printname(const gnc_commodity *cm)
Retrieve the 'print' name for the specified commodity.
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...
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...
time64 gnc_time(time64 *tbuf)
get the current time
gint64 time64
Most systems that are currently maintained, including Microsoft Windows, BSD-derived Unixes and Linux...
The type used to store guids in C.
void gnc_ui_update_namespace_picker(GtkWidget *cbwe, const gchar *sel, dialog_commodity_mode mode)
Given a combo box, fill in the known commodity namespaces and then select one.
"select" and "new" commodity windows
void gnc_price_ref(GNCPrice *p)
gnc_price_ref - indicate your need for a given price to stick around (i.e.
gboolean gnc_commodity_equiv(const gnc_commodity *a, const gnc_commodity *b)
This routine returns TRUE if the two commodities are equivalent.