dialog-date-close.c

00001 /*
00002  * dialog-date-close.c -- Dialog to ask a question and request a date
00003  * Copyright (C) 2002 Derek Atkins
00004  * Author: Derek Atkins <warlord@MIT.EDU>
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License as
00008  * published by the Free Software Foundation; either version 2 of
00009  * the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, contact:
00018  *
00019  * Free Software Foundation           Voice:  +1-617-542-5942
00020  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
00021  * Boston, MA  02110-1301,  USA       gnu@gnu.org
00022  */
00023 
00024 #include "config.h"
00025 
00026 #include <gnome.h>
00027 #include <glib/gi18n.h>
00028 
00029 #include "dialog-utils.h"
00030 #include "qof.h"
00031 #include "gnc-gui-query.h"
00032 #include "gnc-ui.h"
00033 #include "gnc-ui-util.h"
00034 #include "gnc-date-edit.h"
00035 #include "gnc-account-sel.h"
00036 
00037 #include "business-gnome-utils.h"
00038 #include "dialog-date-close.h"
00039 
00040 typedef struct _dialog_date_close_window {
00041   GtkWidget *dialog;
00042   GtkWidget *date;
00043   GtkWidget *post_date;
00044   GtkWidget *acct_combo;
00045   GtkWidget *memo_entry;
00046   GtkWidget *question_check;
00047   GncBillTerm *terms;
00048   Timespec *ts, *ts2;
00049   GList * acct_types;
00050   GNCBook *book;
00051   Account *acct;
00052   char **memo;
00053   gboolean retval;
00054   gboolean answer;
00055 } DialogDateClose;
00056 
00057 void gnc_dialog_date_close_ok_cb (GtkWidget *widget, gpointer user_data);
00058 
00059 
00060 void
00061 gnc_dialog_date_close_ok_cb (GtkWidget *widget, gpointer user_data)
00062 {
00063   DialogDateClose *ddc = user_data;
00064 
00065   if (ddc->acct_combo) {
00066     Account *acc;
00067 
00068     acc = gnc_account_sel_get_account( GNC_ACCOUNT_SEL(ddc->acct_combo) );
00069 
00070     if (!acc) {
00071       gnc_error_dialog (ddc->dialog,
00072                         _("No Account selected.  Please try again."));
00073       return;
00074     }
00075 
00076     if (xaccAccountGetPlaceholder (acc)) {
00077       gnc_error_dialog (ddc->dialog,
00078                         _("Placeholder account selected.  Please try again."));
00079       return;
00080     }
00081 
00082     ddc->acct = acc;
00083   }
00084 
00085   if (ddc->post_date)
00086     *(ddc->ts2) = gnc_date_edit_get_date_ts (GNC_DATE_EDIT (ddc->post_date));
00087 
00088   if (ddc->date) {
00089     if (ddc->terms)
00090       *(ddc->ts) = gncBillTermComputeDueDate (ddc->terms, *(ddc->ts2));
00091     else
00092       *(ddc->ts) = gnc_date_edit_get_date_ts (GNC_DATE_EDIT (ddc->date));
00093   }
00094 
00095   if (ddc->memo_entry && ddc->memo)
00096     *(ddc->memo) = gtk_editable_get_chars (GTK_EDITABLE (ddc->memo_entry),
00097                                            0, -1);
00098   if (ddc->question_check)
00099           ddc->answer = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(ddc->question_check));
00100   ddc->retval = TRUE;
00101 }
00102 
00103 static void
00104 fill_in_acct_info (DialogDateClose *ddc)
00105 {
00106   GNCAccountSel *gas = GNC_ACCOUNT_SEL (ddc->acct_combo);
00107 
00108   /* How do I set the book? */
00109   gnc_account_sel_set_acct_filters( gas, ddc->acct_types );
00110   gnc_account_sel_set_new_account_ability( gas, TRUE );
00111   gnc_account_sel_set_new_account_modal( gas, TRUE );
00112   gnc_account_sel_set_account( gas, ddc->acct );
00113 }
00114 
00115 static void
00116 build_date_close_window (GtkWidget *hbox, const char *message)
00117 {
00118   GtkWidget *pixmap = NULL;
00119   GtkWidget *label;
00120   GtkWidget *alignment;
00121   char *s;
00122 
00123   s = gnome_program_locate_file (NULL,
00124                                  GNOME_FILE_DOMAIN_PIXMAP,
00125                                  "gnome-question.png", TRUE, NULL);
00126   if (s) {
00127     pixmap = gtk_image_new_from_file(s);
00128     g_free(s);
00129   }
00130 
00131   if (pixmap) {
00132     gtk_box_pack_start (GTK_BOX(hbox), pixmap, FALSE, TRUE, 0);
00133     gtk_widget_show (pixmap);
00134   }
00135 
00136   label = gtk_label_new (message);
00137   gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
00138   gtk_misc_set_padding (GTK_MISC (label), GNOME_PAD, 0);
00139   gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0);
00140   gtk_widget_show (label);
00141 
00142   /* Add some extra space on the right to balance the pixmap */
00143   if (pixmap) {
00144     alignment = gtk_alignment_new (0., 0., 0., 0.);
00145     gtk_widget_set_size_request (alignment, GNOME_PAD, -1);
00146     gtk_widget_show (alignment);
00147     
00148     gtk_box_pack_start (GTK_BOX (hbox), alignment, FALSE, FALSE, 0);
00149   }
00150 }
00151 
00152 gboolean
00153 gnc_dialog_date_close_parented (GtkWidget *parent, const char *message,
00154                                 const char *label_message,
00155                                 gboolean ok_is_default,
00156                                 /* Returned data ... */
00157                                 Timespec *ts)
00158 {
00159   DialogDateClose *ddc;
00160   GtkWidget *date_box;
00161   GtkWidget *hbox;
00162   GtkWidget *label;
00163   GladeXML *xml;
00164   gboolean retval;
00165 
00166   if (!message || !label_message || !ts)
00167     return FALSE;
00168 
00169   ddc = g_new0 (DialogDateClose, 1);
00170   ddc->ts = ts;
00171 
00172   xml = gnc_glade_xml_new ("date-close.glade", "Date Close Dialog");
00173   ddc->dialog = glade_xml_get_widget (xml, "Date Close Dialog");
00174   hbox = glade_xml_get_widget (xml, "the_hbox");
00175   label = glade_xml_get_widget (xml, "label");
00176 
00177   date_box = glade_xml_get_widget (xml, "date_box");
00178   ddc->date = gnc_date_edit_new (time(NULL), FALSE, FALSE);
00179   gtk_box_pack_start (GTK_BOX(date_box), ddc->date, TRUE, TRUE, 0);
00180 
00181   if (parent)
00182     gtk_window_set_transient_for (GTK_WINDOW(ddc->dialog), GTK_WINDOW(parent));
00183 
00184   build_date_close_window (hbox, message);
00185 
00186   gnc_date_edit_set_time_ts (GNC_DATE_EDIT (ddc->date), *ts);
00187   gtk_label_set_text (GTK_LABEL (label), label_message);
00188 
00189   /* Setup signals */
00190   glade_xml_signal_autoconnect_full( xml,
00191                                      gnc_glade_autoconnect_full_func,
00192                                      ddc);
00193 
00194   gtk_widget_show_all (ddc->dialog);
00195 
00196   ddc->retval = FALSE;
00197   while (gtk_dialog_run (GTK_DIALOG (ddc->dialog)) == GTK_RESPONSE_OK) {
00198     /* If reponse is OK but flag is not set, try again */
00199     if (ddc->retval)
00200       break;
00201   }
00202 
00203   gtk_widget_destroy(ddc->dialog);
00204   retval = ddc->retval;
00205   g_list_free (ddc->acct_types);
00206   g_free (ddc);
00207 
00208   return retval;
00209 }
00210 
00211 static void
00212 post_date_changed_cb (GNCDateEdit *gde, gpointer d)
00213 {
00214   DialogDateClose *ddc = d;
00215   Timespec post_date;
00216   Timespec due_date;
00217 
00218   post_date = gnc_date_edit_get_date_ts (gde);
00219   due_date = gncBillTermComputeDueDate (ddc->terms, post_date);
00220   gnc_date_edit_set_time_ts (GNC_DATE_EDIT (ddc->date), due_date);
00221 }
00222 
00223 gboolean
00224 gnc_dialog_dates_acct_question_parented (GtkWidget *parent, const char *message,
00225                                 const char *ddue_label_message,
00226                                 const char *post_label_message,
00227                                 const char *acct_label_message,
00228                                 const char *question_check_message,
00229                                 gboolean ok_is_default,
00230                                 GList * acct_types, GNCBook *book,
00231                                 GncBillTerm *terms,
00232                                 /* Returned Data... */
00233                                 Timespec *ddue, Timespec *post,
00234                                 char **memo, Account **acct, gboolean *answer)
00235 {
00236   DialogDateClose *ddc;
00237   GtkWidget *hbox;
00238   GtkWidget *label;
00239   GtkWidget *date_box;
00240   GtkWidget *acct_box;
00241   GladeXML *xml;
00242   gboolean retval;
00243 
00244   if (!message || !ddue_label_message || !post_label_message ||
00245       !acct_label_message || !acct_types || !book || !ddue || !post || !acct)
00246     return FALSE;
00247   if (question_check_message && !answer)
00248           return FALSE;
00249 
00250   ddc = g_new0 (DialogDateClose, 1);
00251   ddc->ts = ddue;
00252   ddc->ts2 = post;
00253   ddc->book = book;
00254   ddc->acct_types = acct_types;
00255   ddc->acct = *acct;
00256   ddc->memo = memo;
00257   ddc->terms = terms;
00258 
00259   xml = gnc_glade_xml_new ("date-close.glade", "Date Account Dialog");
00260   ddc->dialog = glade_xml_get_widget (xml, "Date Account Dialog");
00261   ddc->memo_entry = glade_xml_get_widget (xml, "memo_entry");
00262   hbox = glade_xml_get_widget (xml, "the_hbox");
00263 
00264   acct_box = glade_xml_get_widget (xml, "acct_hbox");
00265   ddc->acct_combo = gnc_account_sel_new();
00266   gtk_box_pack_start (GTK_BOX(acct_box), ddc->acct_combo, TRUE, TRUE, 0);
00267 
00268   date_box = glade_xml_get_widget (xml, "date_box");
00269   ddc->date = gnc_date_edit_new (time(NULL), FALSE, FALSE);
00270   gtk_box_pack_start (GTK_BOX(date_box), ddc->date, TRUE, TRUE, 0);
00271 
00272   date_box = glade_xml_get_widget (xml, "post_date_box");
00273   ddc->post_date = gnc_date_edit_new (time(NULL), FALSE, FALSE);
00274   gtk_box_pack_start (GTK_BOX(date_box), ddc->post_date, TRUE, TRUE, 0);
00275 
00276   ddc->question_check = glade_xml_get_widget(xml, "question_check");
00277   
00278   if (parent)
00279     gtk_window_set_transient_for (GTK_WINDOW(ddc->dialog), GTK_WINDOW(parent));
00280 
00281   build_date_close_window (hbox, message);
00282 
00283   /* Set the labels */
00284   label = glade_xml_get_widget (xml, "date_label");
00285   gtk_label_set_text (GTK_LABEL (label), ddue_label_message);
00286   label = glade_xml_get_widget (xml, "postdate_label");
00287   gtk_label_set_text (GTK_LABEL (label), post_label_message);
00288   label = glade_xml_get_widget (xml, "acct_label");
00289   gtk_label_set_text (GTK_LABEL (label), acct_label_message);
00290 
00291   if (question_check_message)
00292   {
00293           gtk_label_set_text(GTK_LABEL(GTK_BIN(ddc->question_check)->child), question_check_message);
00294           gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ddc->question_check), *answer);
00295   } else {
00296           gtk_widget_hide(ddc->question_check);
00297           gtk_widget_hide(glade_xml_get_widget(xml, "hide1"));
00298   }
00299 
00300 
00301   /* Set the post date widget */
00302   gnc_date_edit_set_time_ts (GNC_DATE_EDIT (ddc->post_date), *post);
00303 
00304   /* Deal with the terms handling of the due date */
00305   if (terms) {
00306     g_signal_connect (G_OBJECT (ddc->post_date), "date_changed",
00307                       G_CALLBACK (post_date_changed_cb), ddc);
00308     gtk_widget_set_sensitive (ddc->date, FALSE);
00309     post_date_changed_cb (GNC_DATE_EDIT (ddc->post_date), ddc);
00310   } else
00311     gnc_date_edit_set_time_ts (GNC_DATE_EDIT (ddc->date), *ddue);
00312 
00313   /* Setup the account widget */
00314   fill_in_acct_info (ddc);
00315 
00316   /* Setup signals */
00317   glade_xml_signal_autoconnect_full( xml,
00318                                      gnc_glade_autoconnect_full_func,
00319                                      ddc);
00320 
00321   gtk_widget_show_all (ddc->dialog);
00322 
00323   ddc->retval = FALSE;
00324   while (gtk_dialog_run (GTK_DIALOG (ddc->dialog)) == GTK_RESPONSE_OK) {
00325     /* If reponse is OK but flag is not set, try again */
00326     if (ddc->retval)
00327       break;
00328   }
00329 
00330   gtk_widget_destroy(ddc->dialog);
00331   retval = ddc->retval;
00332   *acct = ddc->acct;
00333   if (question_check_message)
00334           *answer = ddc->answer;
00335   g_free (ddc);
00336 
00337   return retval;
00338 }
00339 
00340 gboolean
00341 gnc_dialog_date_acct_parented (GtkWidget *parent, const char *message,
00342                                const char *date_label_message,
00343                                const char *acct_label_message,
00344                                gboolean ok_is_default,
00345                                GList * acct_types, GNCBook *book,
00346                                /* Returned Data... */
00347                                Timespec *date, Account **acct)
00348 {
00349   DialogDateClose *ddc;
00350   GtkWidget *hbox;
00351   GtkWidget *label;
00352   GtkWidget *date_box;
00353   GtkWidget *acct_box;
00354   GladeXML *xml;
00355   gboolean retval;
00356 
00357   if (!message || !date_label_message || !acct_label_message ||
00358       !acct_types || !book || !date || !acct)
00359     return FALSE;
00360 
00361   ddc = g_new0 (DialogDateClose, 1);
00362   ddc->ts = date;
00363   ddc->book = book;
00364   ddc->acct_types = acct_types;
00365   ddc->acct = *acct;
00366 
00367   xml = gnc_glade_xml_new ("date-close.glade", "Date Account Dialog");
00368   ddc->dialog = glade_xml_get_widget (xml, "Date Account Dialog");
00369   hbox = glade_xml_get_widget (xml, "the_hbox");
00370 
00371   acct_box = glade_xml_get_widget (xml, "acct_hbox");
00372   ddc->acct_combo = gnc_account_sel_new();
00373   if (*acct)
00374     gnc_account_sel_set_account (GNC_ACCOUNT_SEL(ddc->acct_combo), *acct);
00375   gtk_box_pack_start (GTK_BOX(acct_box), ddc->acct_combo, TRUE, TRUE, 0);
00376 
00377   date_box = glade_xml_get_widget (xml, "date_box");
00378   ddc->date = gnc_date_edit_new (time(NULL), FALSE, FALSE);
00379   gtk_box_pack_start (GTK_BOX(date_box), ddc->date, TRUE, TRUE, 0);
00380 
00381   if (parent)
00382     gtk_window_set_transient_for (GTK_WINDOW(ddc->dialog), GTK_WINDOW(parent));
00383 
00384   build_date_close_window (hbox, message);
00385 
00386   /* Set the labels */
00387   label = glade_xml_get_widget (xml, "date_label");
00388   gtk_label_set_text (GTK_LABEL (label), date_label_message);
00389   label = glade_xml_get_widget (xml, "acct_label");
00390   gtk_label_set_text (GTK_LABEL (label), acct_label_message);
00391 
00392   /* Set the date widget */
00393   gnc_date_edit_set_time_ts (GNC_DATE_EDIT (ddc->date), *date);
00394 
00395   /* Setup the account widget */
00396   fill_in_acct_info (ddc);
00397 
00398   /* Setup signals */
00399   glade_xml_signal_autoconnect_full( xml,
00400                                      gnc_glade_autoconnect_full_func,
00401                                      ddc);
00402 
00403   gtk_widget_show_all (ddc->dialog);
00404 
00405   gtk_widget_hide_all (glade_xml_get_widget (xml, "postdate_label"));
00406   gtk_widget_hide_all (glade_xml_get_widget (xml, "post_date"));
00407   gtk_widget_hide_all (glade_xml_get_widget (xml, "memo_entry"));
00408   gtk_widget_hide_all (glade_xml_get_widget (xml, "memo_label"));
00409 
00410   ddc->retval = FALSE;
00411   while (gtk_dialog_run (GTK_DIALOG (ddc->dialog)) == GTK_RESPONSE_OK) {
00412     /* If reponse is OK but flag is not set, try again */
00413     if (ddc->retval)
00414       break;
00415   }
00416 
00417   gtk_widget_destroy(ddc->dialog);
00418   retval = ddc->retval;
00419   *acct = ddc->acct;
00420   g_free (ddc);
00421 
00422   return retval;
00423 }

Generated on Thu Jul 3 05:06:30 2008 for GnuCash by  doxygen 1.5.2