@ -1,6 +1,7 @@
/********************************************************************\
* dialog - transfer . c - - transfer dialog for GnuCash *
* Copyright ( C ) 1999 Linas Vepstas *
* Copyright ( C ) 2000 Dave Peticolas *
* *
* This program is free software ; you can redistribute it and / or *
* modify it under the terms of the GNU General Public License as *
@ -38,13 +39,22 @@
# include "util.h"
typedef enum
{
XFER_DIALOG_FROM ,
XFER_DIALOG_TO
} XferDirection ;
/* This static indicates the debugging module that this .o belongs to. */
static short module = MOD_GUI ;
typedef struct _xferDialog XferDialog ;
static GList * xfer_dialogs = NULL ;
struct _xferDialog
{
GtkWidget * dialog ;
GtkWidget * amount_entry ;
GtkWidget * date_entry ;
GtkWidget * description_entry ;
@ -52,6 +62,9 @@ struct _xferDialog
GNCAccountTree * from ;
GNCAccountTree * to ;
GtkWidget * from_show_button ;
GtkWidget * to_show_button ;
} ;
@ -61,20 +74,19 @@ gnc_xfer_dialog_toggle_cb(GtkToggleButton *button, gpointer data)
GNCAccountTree * tree = GNC_ACCOUNT_TREE ( data ) ;
if ( gtk_toggle_button_get_active ( button ) )
gnc_account_tree_show_ categories ( tree ) ;
gnc_account_tree_show_ income_expense ( tree ) ;
else
gnc_account_tree_hide_ categories ( tree ) ;
gnc_account_tree_hide_ income_expense ( tree ) ;
}
static GtkWidget *
gnc_xfer_dialog_create_tree_frame ( Account * initial , gchar * title ,
GNCAccountTree * * set_tree ,
gnc_xfer_dialog_create_tree_frame ( gchar * title ,
GNCAccountTree * * set_tree ,
GtkWidget * * set_show_button ,
GtkTooltips * tooltips )
{
GtkWidget * frame , * scrollWin , * accountTree , * vbox , * button ;
gboolean is_category ;
int type ;
frame = gtk_frame_new ( title ) ;
@ -82,15 +94,11 @@ gnc_xfer_dialog_create_tree_frame(Account *initial, gchar *title,
gtk_container_add ( GTK_CONTAINER ( frame ) , vbox ) ;
gtk_container_set_border_width ( GTK_CONTAINER ( vbox ) , 10 ) ;
type = xaccAccountGetType ( initial ) ;
is_category = ( type = = EXPENSE ) | | ( type = = INCOME ) ;
accountTree = gnc_account_tree_new ( ) ;
* set_tree = GNC_ACCOUNT_TREE ( accountTree ) ;
gtk_clist_column_titles_hide ( GTK_CLIST ( accountTree ) ) ;
gnc_account_tree_hide_all_but_name ( GNC_ACCOUNT_TREE ( accountTree ) ) ;
if ( ! is_category )
gnc_account_tree_hide_categories ( GNC_ACCOUNT_TREE ( accountTree ) ) ;
gnc_account_tree_hide_income_expense ( GNC_ACCOUNT_TREE ( accountTree ) ) ;
gnc_account_tree_refresh ( GNC_ACCOUNT_TREE ( accountTree ) ) ;
scrollWin = gtk_scrolled_window_new ( NULL , NULL ) ;
@ -117,13 +125,13 @@ gnc_xfer_dialog_create_tree_frame(Account *initial, gchar *title,
}
button = gtk_check_button_new_with_label ( SHOW_INC_EXP_STR ) ;
gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON ( button ) , is_category ) ;
* set_show_button = button ;
gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON ( button ) , TRUE ) ;
gtk_box_pack_start ( GTK_BOX ( vbox ) , button , FALSE , FALSE , 0 ) ;
gtk_tooltips_set_tip ( tooltips , button , SHOW_INC_EXP_MSG , NULL ) ;
gtk_signal_connect ( GTK_OBJECT ( button ) , " toggled " ,
GTK_SIGNAL_FUNC ( gnc_xfer_dialog_toggle_cb ) ,
( gpointer ) accountTree ) ;
GTK_SIGNAL_FUNC ( gnc_xfer_dialog_toggle_cb ) , accountTree ) ;
return frame ;
}
@ -164,9 +172,184 @@ gnc_xfer_update_cb(GtkWidget *widget, GdkEventFocus *event, gpointer data)
}
static GtkWidget *
gnc_xfer_dialog_create ( GtkWidget * parent , Account * initial ,
XferDialog * xferData )
static void
gnc_xfer_dialog_select_account ( XferDialog * xferData , Account * account ,
XferDirection direction )
{
GNCAccountTree * tree ;
GtkWidget * show_button ;
gboolean is_income_expense ;
GNCAccountType type ;
if ( xferData = = NULL )
return ;
if ( account = = NULL )
return ;
switch ( direction )
{
case XFER_DIALOG_FROM :
tree = xferData - > from ;
show_button = xferData - > from_show_button ;
break ;
case XFER_DIALOG_TO :
tree = xferData - > to ;
show_button = xferData - > to_show_button ;
break ;
default :
return ;
}
type = xaccAccountGetType ( account ) ;
is_income_expense = ( type = = EXPENSE ) | | ( type = = INCOME ) ;
gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON ( show_button ) ,
is_income_expense ) ;
gnc_account_tree_select_account ( tree , account , TRUE ) ;
}
/********************************************************************\
* gnc_xfer_dialog_select_from_account *
* select the from account in a xfer dialog *
* *
* Args : xferData - xfer dialog structure *
* account - account to select *
* Return : none *
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void
gnc_xfer_dialog_select_from_account ( XferDialog * xferData , Account * account )
{
gnc_xfer_dialog_select_account ( xferData , account , XFER_DIALOG_FROM ) ;
}
/********************************************************************\
* gnc_xfer_dialog_select_to_account *
* select the to account in a xfer dialog *
* *
* Args : xferData - xfer dialog structure *
* account - account to select *
* Return : none *
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void
gnc_xfer_dialog_select_to_account ( XferDialog * xferData , Account * account )
{
gnc_xfer_dialog_select_account ( xferData , account , XFER_DIALOG_TO ) ;
}
static void
gnc_xfer_dialog_ok_cb ( GtkWidget * widget , gpointer data )
{
XferDialog * xferData = data ;
Account * from , * to ;
char * string ;
double amount ;
time_t time ;
Transaction * trans ;
Split * from_split ;
Split * to_split ;
from = gnc_account_tree_get_current_account ( xferData - > from ) ;
to = gnc_account_tree_get_current_account ( xferData - > to ) ;
if ( ( from = = NULL ) | | ( to = = NULL ) )
{
gnc_error_dialog_parented ( GTK_WINDOW ( xferData - > dialog ) , XFER_NO_ACC_MSG ) ;
return ;
}
if ( from = = to )
{
gnc_error_dialog_parented ( GTK_WINDOW ( xferData - > dialog ) , XFER_SAME_MSG ) ;
return ;
}
if ( ! xaccAccountsHaveCommonCurrency ( from , to ) )
{
gnc_error_dialog_parented ( GTK_WINDOW ( xferData - > dialog ) , XFER_CURR_MSG ) ;
return ;
}
string = gtk_entry_get_text ( GTK_ENTRY ( xferData - > amount_entry ) ) ;
amount = xaccParseAmount ( string , GNC_T ) ;
time = gnc_date_edit_get_date ( GNC_DATE_EDIT ( xferData - > date_entry ) ) ;
/* Create the transaction */
trans = xaccMallocTransaction ( ) ;
xaccTransBeginEdit ( trans , GNC_T ) ;
xaccTransSetDateSecs ( trans , time ) ;
string = gtk_entry_get_text ( GTK_ENTRY ( xferData - > description_entry ) ) ;
xaccTransSetDescription ( trans , string ) ;
/* first split is already there */
to_split = xaccTransGetSplit ( trans , 0 ) ;
xaccSplitSetShareAmount ( to_split , amount ) ;
/* second split must be created */
from_split = xaccMallocSplit ( ) ;
xaccSplitSetShareAmount ( from_split , - amount ) ;
xaccTransAppendSplit ( trans , from_split ) ;
/* TransSetMemo will set the memo for both splits */
string = gtk_entry_get_text ( GTK_ENTRY ( xferData - > memo_entry ) ) ;
xaccTransSetMemo ( trans , string ) ;
/* Now do the 'to' account */
xaccAccountBeginEdit ( to , GNC_F ) ;
xaccAccountInsertSplit ( to , to_split ) ;
xaccAccountCommitEdit ( to ) ;
/* Now do the 'from' account */
xaccAccountBeginEdit ( from , GNC_F ) ;
xaccAccountInsertSplit ( from , from_split ) ;
xaccAccountCommitEdit ( from ) ;
/* finish transaction */
xaccTransCommitEdit ( trans ) ;
/* Refresh everything */
gnc_account_ui_refresh ( to ) ;
gnc_account_ui_refresh ( from ) ;
gnc_refresh_main_window ( ) ;
gnome_dialog_close ( GNOME_DIALOG ( xferData - > dialog ) ) ;
}
static void
gnc_xfer_dialog_cancel_cb ( GtkWidget * widget , gpointer data )
{
XferDialog * xferData = data ;
gnome_dialog_close ( GNOME_DIALOG ( xferData - > dialog ) ) ;
}
static int
gnc_xfer_dialog_close_cb ( GnomeDialog * dialog , gpointer data )
{
XferDialog * xferData = data ;
xfer_dialogs = g_list_remove ( xfer_dialogs , dialog ) ;
g_free ( xferData ) ;
DEBUG ( " xfer dialog destroyed \n " ) ;
return FALSE ;
}
static void
gnc_xfer_dialog_create ( GtkWidget * parent , XferDialog * xferData )
{
GtkWidget * dialog ;
GtkTooltips * tooltips ;
@ -178,17 +361,29 @@ gnc_xfer_dialog_create(GtkWidget * parent, Account * initial,
xferData - > dialog = dialog ;
/* Make this dialog modal */
gtk_window_set_modal ( GTK_WINDOW ( dialog ) , TRUE ) ;
/* parent */
gnome_dialog_set_parent ( GNOME_DIALOG ( dialog ) , GTK_WINDOW ( parent ) ) ;
if ( parent ! = NULL )
gnome_dialog_set_parent ( GNOME_DIALOG ( dialog ) , GTK_WINDOW ( parent ) ) ;
/* default to ok */
gnome_dialog_set_default ( GNOME_DIALOG ( dialog ) , 0 ) ;
/* don't close on buttons */
gnome_dialog_set_close ( GNOME_DIALOG ( dialog ) , FALSE ) ;
/* destroy on close */
gnome_dialog_close_hides ( GNOME_DIALOG ( dialog ) , FALSE ) ;
/* allow grow and shrink, no auto-shrink */
gtk_window_set_policy ( GTK_WINDOW ( dialog ) , TRUE , TRUE , FALSE ) ;
gnome_dialog_button_connect ( GNOME_DIALOG ( dialog ) , 0 ,
GTK_SIGNAL_FUNC ( gnc_xfer_dialog_ok_cb ) ,
xferData ) ;
gnome_dialog_button_connect ( GNOME_DIALOG ( dialog ) , 1 ,
GTK_SIGNAL_FUNC ( gnc_xfer_dialog_cancel_cb ) ,
xferData ) ;
gtk_signal_connect ( GTK_OBJECT ( dialog ) , " close " ,
GTK_SIGNAL_FUNC ( gnc_xfer_dialog_close_cb ) , xferData ) ;
tooltips = gtk_tooltips_new ( ) ;
@ -291,139 +486,69 @@ gnc_xfer_dialog_create(GtkWidget * parent, Account * initial,
gtk_box_pack_start ( GTK_BOX ( GNOME_DIALOG ( dialog ) - > vbox ) ,
hbox , TRUE , TRUE , 0 ) ;
tree = gnc_xfer_dialog_create_tree_frame ( initial , XFRM_STR ,
& xferData - > from , tooltips ) ;
tree = gnc_xfer_dialog_create_tree_frame ( XFRM_STR ,
& xferData - > from ,
& xferData - > from_show_button ,
tooltips ) ;
gtk_box_pack_start ( GTK_BOX ( hbox ) , tree , TRUE , TRUE , 0 ) ;
tree = gnc_xfer_dialog_create_tree_frame ( initial , XFTO_STR ,
& xferData - > to , tooltips ) ;
tree = gnc_xfer_dialog_create_tree_frame ( XFTO_STR ,
& xferData - > to ,
& xferData - > to_show_button ,
tooltips ) ;
gtk_box_pack_start ( GTK_BOX ( hbox ) , tree , TRUE , TRUE , 0 ) ;
}
/* allow grow and shrink, no auto-shrink */
gtk_window_set_policy ( GTK_WINDOW ( dialog ) , TRUE , TRUE , FALSE ) ;
return dialog ;
}
/********************************************************************\
* xferWindow *
* gnc_xfer_dialog *
* opens up a window to do an automatic transfer between accounts *
* *
* Args : parent - the parent of the window to be created *
* initial - the initial account in the from / to fields *
* group - the group from which the accounts are taken *
* Return : none *
* Return : XferDialog structure *
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void
XferDialog *
gnc_xfer_dialog ( GtkWidget * parent , Account * initial )
{
Account * from , * to ;
GtkWidget * dialog ;
XferDialog xferData ;
char * string ;
double amount ;
time_t time ;
gint result ;
if ( xaccGroupGetNumAccounts ( gncGetCurrentGroup ( ) ) < 2 )
{
gnc_error_dialog ( XFER_NSF_MSG ) ;
return ;
}
XferDialog * xferData ;
dialog = gnc_xfer_dialog_create ( parent , initial , & xferData ) ;
xferData = g_new0 ( XferDialog , 1 ) ;
g tk_widget_show_all( dialog ) ;
gnc_xfer_dialog_create ( parent , xferData ) ;
gtk_widget_grab_focus ( xferData . amount_entry ) ;
xfer_dialogs = g_list_prepend ( xfer_dialogs , xferData - > dialog ) ;
gnc_account_tree_select_account ( xferData . from , initial , TRUE ) ;
gnc_account_tree_select_account ( xferData . to , initial , TRUE ) ;
gtk_widget_grab_focus ( xferData - > amount_entry ) ;
while ( 1 )
{
result = gnome_dialog_run ( GNOME_DIALOG ( dialog ) ) ;
if ( result ! = 0 )
break ;
gnc_xfer_dialog_select_from_account ( xferData , initial ) ;
gnc_xfer_dialog_select_to_account ( xferData , initial ) ;
from = gnc_account_tree_get_current_account ( xferData . from ) ;
to = gnc_account_tree_get_current_account ( xferData . to ) ;
gtk_widget_show_all ( xferData - > dialog ) ;
if ( ( from = = NULL ) | | ( to = = NULL ) )
{
gnc_error_dialog_parented ( GTK_WINDOW ( dialog ) , XFER_NO_ACC_MSG ) ;
continue ;
}
gnc_window_adjust_for_screen ( GTK_WINDOW ( xferData - > dialog ) ) ;
if ( from = = to )
{
gnc_error_dialog_parented ( GTK_WINDOW ( dialog ) , XFER_SAME_MSG ) ;
continue ;
}
if ( ! xaccAccountsHaveCommonCurrency ( from , to ) )
{
gnc_error_dialog_parented ( GTK_WINDOW ( dialog ) , XFER_CURR_MSG ) ;
continue ;
}
string = gtk_entry_get_text ( GTK_ENTRY ( xferData . amount_entry ) ) ;
amount = xaccParseAmount ( string , GNC_T ) ;
time = gnc_date_edit_get_date ( GNC_DATE_EDIT ( xferData . date_entry ) ) ;
{
Transaction * trans ;
Split * to_split , * from_split ;
/* Create the transaction */
trans = xaccMallocTransaction ( ) ;
xaccTransBeginEdit ( trans , GNC_T ) ;
xaccTransSetDateSecs ( trans , time ) ;
string = gtk_entry_get_text ( GTK_ENTRY ( xferData . description_entry ) ) ;
xaccTransSetDescription ( trans , string ) ;
/* first split is already there */
to_split = xaccTransGetSplit ( trans , 0 ) ;
xaccSplitSetShareAmount ( to_split , amount ) ;
/* second split must be created */
from_split = xaccMallocSplit ( ) ;
xaccSplitSetShareAmount ( from_split , - amount ) ;
xaccTransAppendSplit ( trans , from_split ) ;
/* TransSetMemo will set the memo for both splits */
string = gtk_entry_get_text ( GTK_ENTRY ( xferData . memo_entry ) ) ;
xaccTransSetMemo ( trans , string ) ;
/* Now do the 'to' account */
xaccAccountBeginEdit ( to , GNC_F ) ;
xaccAccountInsertSplit ( to , to_split ) ;
xaccAccountCommitEdit ( to ) ;
return xferData ;
}
/* Now do the 'from' account */
xaccAccountBeginEdit ( from , GNC_F ) ;
xaccAccountInsertSplit ( from , from_split ) ;
xaccAccountCommitEdit ( from ) ;
/* finish transaction */
xaccTransCommitEdit ( trans ) ;
/********************************************************************\
* gnc_ui_destroy_xfer_windows *
* destroy all open transfer dialogs *
* *
* Args : none *
* Return : none *
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
void
gnc_ui_destroy_xfer_windows ( )
{
GnomeDialog * dialog ;
/* Refresh everything */
gnc_account_ui_refresh ( to ) ;
gnc_account_ui_refresh ( from ) ;
gnc_refresh_main_window ( ) ;
while ( xfer_dialogs ! = NULL )
{
dialog = GNOME_DIALOG ( xfer_dialogs - > data ) ;
break ;
}
gnome_dialog_close ( dialog ) ;
}
DEBUG ( " destroying transfer dialog \n " ) ;
gtk_widget_destroy ( dialog ) ;
}