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 "guile-util.h"
48 #include "engine-helpers.h"
51 #define DIALOG_PRICE_EDIT_CM_CLASS "dialog-price-edit"
52 #define GNC_PREFS_GROUP "dialogs.price-editor"
53 #define DIALOG_PRICE_EDIT_SOURCE "user:price-editor"
57 G_GNUC_UNUSED
static QofLogModule log_module = GNC_MOD_GUI;
66 GNCPriceEditType type;
68 GtkWidget * namespace_cbwe;
69 GtkWidget * commodity_cbwe;
70 GtkWidget * currency_edit;
71 GtkWidget * date_edit;
72 GtkWidget * source_entry;
73 GtkWidget * type_combobox;
74 GtkWidget * price_edit;
76 GtkWidget * cancel_button;
77 GtkWidget * apply_button;
78 GtkWidget * ok_button;
86 void pedit_dialog_response_cb (GtkDialog *dialog, gint response, gpointer data);
87 void pedit_data_changed_cb (GtkWidget *w, gpointer data);
88 void pedit_commodity_ns_changed_cb (GtkComboBox *cbwe, gpointer data);
89 void pedit_commodity_changed_cb (GtkComboBox *cbwe, gpointer data);
93 gnc_prices_set_changed (
PriceEditDialog *pedit_dialog, gboolean changed)
95 pedit_dialog->changed = changed;
97 gtk_widget_set_sensitive (pedit_dialog->apply_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)
144 const gchar *name_space, *fullname;
150 if (pedit_dialog->price)
152 commodity = gnc_price_get_commodity (pedit_dialog->price);
162 name_space, fullname);
164 currency = gnc_price_get_currency (pedit_dialog->price);
165 date = gnc_price_get_time (pedit_dialog->price);
166 source = gnc_price_get_source (pedit_dialog->price);
167 type = gnc_price_get_typestr (pedit_dialog->price);
168 value = gnc_price_get_value (pedit_dialog->price);
175 source = DIALOG_PRICE_EDIT_SOURCE;
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.tv_sec);
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 gnc_amount_edit_set_amount (GNC_AMOUNT_EDIT (pedit_dialog->price_edit), value);
204 const gchar *fullname;
211 fullname = gtk_entry_get_text( GTK_ENTRY( gtk_bin_get_child( GTK_BIN( GTK_COMBO_BOX(pedit_dialog->commodity_cbwe)))));
213 commodity = gnc_commodity_table_find_full(gnc_get_current_commodities(), name_space, fullname);
215 return _(
"You must select a Security.");
218 (GNC_CURRENCY_EDIT (pedit_dialog->currency_edit));
220 return _(
"You must select a Currency.");
222 date.tv_sec = gnc_date_edit_get_date (GNC_DATE_EDIT (pedit_dialog->date_edit));
225 source = gtk_entry_get_text (GTK_ENTRY (pedit_dialog->source_entry));
227 type = type_index_to_string
228 (gtk_combo_box_get_active (GTK_COMBO_BOX (pedit_dialog->type_combobox)));
230 if (!gnc_amount_edit_evaluate (GNC_AMOUNT_EDIT (pedit_dialog->price_edit)))
231 return _(
"You must enter a valid amount.");
233 value = gnc_amount_edit_get_amount
234 (GNC_AMOUNT_EDIT (pedit_dialog->price_edit));
236 if (!pedit_dialog->price)
238 gnc_price_begin_edit (pedit_dialog->price);
239 gnc_price_set_commodity (pedit_dialog->price, commodity);
240 gnc_price_set_currency (pedit_dialog->price, currency);
241 gnc_price_set_time (pedit_dialog->price, date);
242 gnc_price_set_source (pedit_dialog->price, source);
243 gnc_price_set_typestr (pedit_dialog->price, type);
244 gnc_price_set_value (pedit_dialog->price, value);
245 gnc_price_commit_edit (pedit_dialog->price);
254 pedit_dialog_destroy_cb (GtkWidget *widget, gpointer data)
258 gnc_unregister_gui_component_by_data (DIALOG_PRICE_EDIT_CM_CLASS,
261 if (pedit_dialog->price)
264 pedit_dialog->price = NULL;
265 pedit_dialog->is_new = FALSE;
268 g_free (pedit_dialog);
273 pedit_dialog_response_cb (GtkDialog *dialog, gint response, gpointer data)
277 const char *error_str;
279 if ((response == GTK_RESPONSE_OK) || (response == GTK_RESPONSE_APPLY))
281 error_str = gui_to_price (pedit_dialog);
284 gnc_warning_dialog (pedit_dialog->dialog,
"%s", error_str);
288 gnc_prices_set_changed (pedit_dialog, FALSE);
289 if (TRUE == pedit_dialog->is_new)
294 gnc_gui_refresh_all ();
297 if (response == GTK_RESPONSE_APPLY)
300 pedit_dialog->is_new = TRUE;
303 pedit_dialog->price = new_price;
307 gnc_save_window_size(GNC_PREFS_GROUP, GTK_WINDOW(pedit_dialog->dialog));
308 gtk_widget_destroy (GTK_WIDGET (pedit_dialog->dialog));
309 pedit_dialog_destroy_cb (NULL, pedit_dialog);
315 pedit_commodity_ns_changed_cb (GtkComboBox *cbwe, gpointer data)
320 gnc_prices_set_changed (pedit_dialog, TRUE);
330 pedit_commodity_changed_cb (GtkComboBox *cbwe, gpointer data)
335 const gchar *fullname;
339 gnc_prices_set_changed (pedit_dialog, TRUE);
342 fullname = gtk_entry_get_text( GTK_ENTRY( gtk_bin_get_child( GTK_BIN( GTK_COMBO_BOX(pedit_dialog->commodity_cbwe)))));
344 commodity = gnc_commodity_table_find_full(gnc_get_current_commodities(), name_space, fullname);
349 (pedit_dialog->price_db, commodity);
352 currency = gnc_price_get_currency((
GNCPrice *)price_list->data);
356 (GNC_CURRENCY_EDIT (pedit_dialog->currency_edit), currency);
372 pedit_data_changed_cb (GtkWidget *w, gpointer data)
376 gnc_prices_set_changed (pedit_dialog, TRUE);
381 gnc_price_pedit_dialog_create (GtkWidget *parent,
394 builder = gtk_builder_new();
395 gnc_builder_add_from_file (builder,
"dialog-price.glade",
"liststore1");
396 gnc_builder_add_from_file (builder,
"dialog-price.glade",
"liststore2");
397 gnc_builder_add_from_file (builder,
"dialog-price.glade",
"liststore3");
398 gnc_builder_add_from_file (builder,
"dialog-price.glade",
"Price Dialog");
400 pedit_dialog->session = session;
404 dialog = GTK_WIDGET(gtk_builder_get_object (builder,
"Price Dialog"));
405 pedit_dialog->dialog = dialog;
409 gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (parent));
411 w = GTK_WIDGET(gtk_builder_get_object (builder,
"namespace_cbwe"));
412 pedit_dialog->namespace_cbwe = w;
415 gnc_cbwe_require_list_item(GTK_COMBO_BOX(pedit_dialog->namespace_cbwe));
416 gtk_combo_box_set_active(GTK_COMBO_BOX(pedit_dialog->namespace_cbwe), 1);
418 w = GTK_WIDGET(gtk_builder_get_object (builder,
"commodity_cbwe"));
419 pedit_dialog->commodity_cbwe = w;
421 gnc_cbwe_require_list_item(GTK_COMBO_BOX(pedit_dialog->commodity_cbwe));
426 box = GTK_WIDGET(gtk_builder_get_object (builder,
"currency_box"));
430 pedit_dialog->currency_edit = w;
431 gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
433 g_signal_connect (G_OBJECT (GTK_COMBO_BOX(w)),
"changed",
434 G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
435 label = GTK_WIDGET(gtk_builder_get_object (builder,
"currency_label"));
436 gtk_label_set_mnemonic_widget (GTK_LABEL(label), w);
438 box = GTK_WIDGET(gtk_builder_get_object (builder,
"date_box"));
439 w = gnc_date_edit_new (time (NULL), FALSE, FALSE);
440 pedit_dialog->date_edit = w;
441 gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
443 g_signal_connect (G_OBJECT (w),
"date_changed",
444 G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
445 g_signal_connect (G_OBJECT (GNC_DATE_EDIT (w)->date_entry),
"changed",
446 G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
447 gtk_entry_set_activates_default(GTK_ENTRY(GNC_DATE_EDIT(w)->date_entry), TRUE);
448 label = GTK_WIDGET(gtk_builder_get_object (builder,
"date__label"));
449 gnc_date_make_mnemonic_target (GNC_DATE_EDIT(w), label);
451 w = GTK_WIDGET(gtk_builder_get_object (builder,
"source_entry"));
452 pedit_dialog->source_entry = w;
454 w = GTK_WIDGET(gtk_builder_get_object (builder,
"type_combobox"));
455 pedit_dialog->type_combobox = w;
457 box = GTK_WIDGET(gtk_builder_get_object (builder,
"price_box"));
458 w = gnc_amount_edit_new ();
459 pedit_dialog->price_edit = w;
460 gtk_box_pack_start (GTK_BOX (box), w, TRUE, TRUE, 0);
461 gnc_amount_edit_set_evaluate_on_enter (GNC_AMOUNT_EDIT (w), TRUE);
462 print_info = gnc_default_price_print_info ();
463 gnc_amount_edit_set_print_info (GNC_AMOUNT_EDIT (w), print_info);
464 gtk_entry_set_activates_default(GTK_ENTRY(w), TRUE);
466 label = GTK_WIDGET(gtk_builder_get_object (builder,
"price_label"));
467 gtk_label_set_mnemonic_widget (GTK_LABEL(label), w);
469 entry = gnc_amount_edit_gtk_entry (GNC_AMOUNT_EDIT (w));
470 g_signal_connect (G_OBJECT (entry),
"changed",
471 G_CALLBACK (pedit_data_changed_cb), pedit_dialog);
473 w = GTK_WIDGET(gtk_builder_get_object (builder,
"pd_cancel_button"));
474 pedit_dialog->cancel_button = w;
476 w = GTK_WIDGET(gtk_builder_get_object (builder,
"pd_apply_button"));
477 pedit_dialog->apply_button = w;
478 gnc_prices_set_changed (pedit_dialog, FALSE);
480 w = GTK_WIDGET(gtk_builder_get_object (builder,
"pd_ok_button"));
481 pedit_dialog->ok_button = w;
483 gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, pedit_dialog);
485 g_object_unref(G_OBJECT(builder));
490 close_handler (gpointer user_data)
494 gtk_dialog_response(GTK_DIALOG(pedit_dialog->dialog), GTK_RESPONSE_CANCEL);
499 refresh_handler (GHashTable *changes, gpointer user_data)
508 show_handler (
const char *klass, gint component_id,
509 gpointer user_data, gpointer iter_data)
514 if (!pedit_dialog || (pedit_dialog->price != price))
517 gtk_window_present (GTK_WINDOW(pedit_dialog->dialog));
530 gnc_price_edit_dialog (GtkWidget * parent,
533 GNCPriceEditType type)
538 if ((type == GNC_PRICE_EDIT) &&
539 (gnc_forall_gui_components (DIALOG_PRICE_EDIT_CM_CLASS,
540 show_handler, price)))
544 gnc_price_pedit_dialog_create (parent, pedit_dialog, session);
545 gnc_restore_window_size(GNC_PREFS_GROUP, GTK_WINDOW(pedit_dialog->dialog));
546 pedit_dialog->type = type;
556 gnc_price_set_source (price, DIALOG_PRICE_EDIT_SOURCE);
559 pedit_dialog->is_new = TRUE;
564 pedit_dialog->is_new = FALSE;
568 pedit_dialog->price = price;
569 price_to_gui(pedit_dialog);
570 gnc_prices_set_changed (pedit_dialog, FALSE);
571 component_id = gnc_register_gui_component (DIALOG_PRICE_EDIT_CM_CLASS,
572 refresh_handler, close_handler,
574 gnc_gui_component_set_session (component_id, pedit_dialog->session);
575 gtk_widget_grab_focus (pedit_dialog->commodity_cbwe);
576 gtk_widget_show (pedit_dialog->dialog);
588 gnc_price_edit_by_guid (GtkWidget * parent,
const GncGUID * guid)
593 session = gnc_get_current_session ();
598 gnc_price_edit_dialog(parent, session, price, GNC_PRICE_EDIT);
void gnc_price_list_destroy(PriceList *prices)
GNCPrice * gnc_price_create(QofBook *book)
void gnc_currency_edit_set_currency(GNCCurrencyEdit *gce, const gnc_commodity *currency)
a simple price database for gnucash
utility functions for the GnuCash UI
gchar * gnc_ui_namespace_picker_ns(GtkWidget *cbwe)
void gnc_price_unref(GNCPrice *p)
gboolean gnc_pricedb_add_price(GNCPriceDB *db, GNCPrice *p)
Use a 64-bit unsigned int timespec.
const char * gnc_commodity_get_namespace(const gnc_commodity *cm)
GNCPriceDB * gnc_pricedb_get_db(QofBook *book)
gnc_commodity * gnc_default_currency(void)
Currency selection widget.
QofBook * qof_session_get_book(const QofSession *session)
void gnc_ui_update_commodity_picker(GtkWidget *cbwe, const gchar *name_space, const gchar *init_string)
gnc_commodity * gnc_currency_edit_get_currency(GNCCurrencyEdit *gce)
PriceList * gnc_pricedb_lookup_latest_any_currency(GNCPriceDB *db, const gnc_commodity *commodity)
const char * gnc_commodity_get_printname(const gnc_commodity *cm)
GtkWidget * gnc_currency_edit_new(void)
GNCPrice * gnc_price_clone(GNCPrice *p, QofBook *book)
time64 gnc_time(time64 *tbuf)
get the current local time
void gnc_ui_update_namespace_picker(GtkWidget *cbwe, const gchar *sel, dialog_commodity_mode mode)
"select" and "new" commodity windows
void gnc_price_ref(GNCPrice *p)
const gchar * QofLogModule