File: utils.c

package info (click to toggle)
grisbi 1.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 28,808 kB
  • sloc: ansic: 161,477; sh: 4,559; makefile: 918; xml: 580; perl: 370
file content (1060 lines) | stat: -rw-r--r-- 28,539 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
/* ************************************************************************** */
/*     Copyright (C)    2000-2003 Cédric Auger (cedric@grisbi.org)            */
/*          2003-2008 Benjamin Drieu (bdrieu@april.org)                       */
/*          2003-2004 Alain Portal (aportal@univ-montp2.fr)                   */
/*          2003-2004 Francois Terrot (francois.terrot@grisbi.org)            */
/*          2008-2017 Pierre Biava (grisbi@pierre.biava.name)                 */
/*          http://www.grisbi.org                                             */
/*                                                                            */
/*  This program is free software; you can redistribute it and/or modify      */
/*  it under the terms of the GNU General Public License as published by      */
/*  the Free Software Foundation; either version 2 of the License, or         */
/*  (at your option) any later version.                                       */
/*                                                                            */
/*  This program is distributed in the hope that it will be useful,           */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of            */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
/*  GNU General Public License for more details.                              */
/*                                                                            */
/*  You should have received a copy of the GNU General Public License         */
/*  along with this program; if not, write to the Free Software               */
/*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
/*                                                                            */
/* ************************************************************************** */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <glib/gi18n.h>

/*START_INCLUDE*/
#include "utils.h"
#include "dialog.h"
#include "grisbi_app.h"
#include "gsb_automem.h"
#include "gsb_data_account.h"
#include "gsb_dirs.h"
#include "gsb_rgba.h"
#include "parametres.h"
#include "structures.h"
#include "erreur.h"
/*END_INCLUDE*/

#ifdef GTKOSXAPPLICATION
#include "grisbi_osx.h"
#endif  /* GTKOSXAPPLICATION */

#ifdef G_OS_WIN32
#include <windows.h>
#endif  /* G_OS_WIN32 */

/*START_STATIC*/
/*END_STATIC*/

/*START_EXTERN*/
extern GtkWidget *fenetre_preferences;
/*END_EXTERN*/

/*START_GLOBAL*/
struct CsvSeparators csv_separators[] =			/* Contains all pre-defined CSV separators. */
{
    { N_("Comma"),		"," },
    { N_("Semi-colon"),	";" },
    { N_("Colon"),		":" },
    { N_("Tabulation"),	"\t" },
    { N_("Other"),		NULL },
};
/*END_GLOBAL*/

/******************************************************************************/
/* Private functions                                                          */
/******************************************************************************/
/**
 * Callback triggered when user changed the pre-defined csv separators
 * combobox.  Update the text entry and thus the preview.
 *
 * \param combo		GtkComboBox that triggered event.
 * \param entry		Associated entry to change.
 *
 * \return			FALSE
 **/
static gboolean utils_widget_csv_separators_combo_changed (GtkComboBox *combo,
														   GtkWidget *entry)
{
    gint active = gtk_combo_box_get_active (combo);

    if (csv_separators [active].value)
    {
		gtk_entry_set_text (GTK_ENTRY (entry), csv_separators [active].value);
    }
    else
    {
		gtk_entry_set_text (GTK_ENTRY (entry), "");
    }

    return FALSE;
}

/******************************************************************************/
/* Public functions                                                           */
/******************************************************************************/
/**
 * Bascule de l'état NORMAL à PRELIGHT et inversemment
 *
 * \param GtkWidget         event_box
 * \param GdkEventMotion    event
 * \param GtkStyleContext   context
 *
 * \return FALSE
 * */
gboolean utils_event_box_change_state (GtkWidget *event_box,
									   GdkEventMotion *event,
									   GtkStyleContext *context)
{
    GtkStateFlags state;

    state = gtk_style_context_get_state (context);

    if (state == GTK_STATE_FLAG_ACTIVE)
        gtk_style_context_set_state (context, GTK_STATE_FLAG_PRELIGHT);
    else if (state == GTK_STATE_FLAG_PRELIGHT)
        gtk_style_context_set_state (context, GTK_STATE_FLAG_ACTIVE);

    return FALSE;
}

/**
 * called by a "clicked" callback on a check button,
 * according to its state, sensitive or not the widget given in param
 *
 * \param button a GtkCheckButton
 * \param widget a widget to sensitive or unsensitive
 *
 * \return FALSE
 * */
gboolean sens_desensitive_pointeur (GtkWidget *bouton,
									GtkWidget *widget)
{
    gtk_widget_set_sensitive (widget,
                        gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (bouton)));

    return FALSE;
}


/**
 * sensitive a widget
 * this is usually a callback, so 2 parameters, the first one is not used
 *
 * \param object the object which receive the signal, not used so can be NULL
 * \param widget the widget to sensitive
 *
 * \return FALSE
 * */
gboolean sensitive_widget (gpointer object,
						   GtkWidget *widget)
{
    gtk_widget_set_sensitive (widget, TRUE);
    return FALSE;
}

/**
 * unsensitive a widget
 * this is usually a callback, so 2 parameters, the first one is not used
 *
 * \param object the object which receive the signal, not used so can be NULL
 * \param widget the widget to unsensitive
 *
 * \return FALSE
 * */
gboolean desensitive_widget (gpointer object,
							 GtkWidget *widget)
{
    gtk_widget_set_sensitive (widget, FALSE);
    return FALSE;
}

/**
 * si la commande du navigateur contient %s, on le remplace par url,
 * sinon on ajoute l'url à la fin et &
 *
 * sous Windows si la commande est vide ou egale a la valeur par defaut
 * on lance le butineur par defaut (open)
 */
gboolean lance_navigateur_web (const gchar *url)
{
    gchar **split;
    gchar *chaine = NULL;
    gchar* tmp_str;

#ifdef G_OS_WIN32
    gboolean use_default_browser = TRUE;

    if (conf.browser_command && strlen (conf.browser_command))
    {
        use_default_browser = !strcmp (conf.browser_command,ETAT_WWW_BROWSER);
    }

#else /* G_OS_WIN32 */
    if (!(conf.browser_command && strlen (conf.browser_command)))
    {
        tmp_str = g_strdup_printf (_("Grisbi was unable to execute a web browser to "
                        "browse url:\n<span foreground=\"blue\">%s</span>.\n\n"
                        "Please adjust your settings to a valid executable."), url);
        dialogue_error_hint (tmp_str, _("Cannot execute web browser"));
        g_free (tmp_str);

        return FALSE;
    }
#endif /* G_OS_WIN32 */

#ifdef G_OS_WIN32
    if (!use_default_browser)
    {
#endif /* G_OS_WIN32 */
        /* search if the sequence `%s' is in the string
         * and split the string before and after this delimiter */
        split = g_strsplit (conf.browser_command, "%s", 0);

        if (split[1])
        {
            /* he has a %s in the command */
            /* concat the string before %s, the url and the string after %s */
            tmp_str = g_strconcat (" \"", url, "\" ", NULL);
            chaine = g_strjoinv (tmp_str, split);
            g_free(tmp_str);
            g_strfreev (split);

            /* add the & character at the end */
            tmp_str = g_strconcat (chaine, "&", NULL);
            g_free (chaine);
            chaine = tmp_str;
        }
        else
            chaine = g_strconcat (conf.browser_command, " \"", url, "\"&", NULL);

        if (system (chaine) == -1)
        {
            tmp_str = g_strdup_printf (_("Grisbi was unable to execute a web browser to "
                        "browse url <tt>%s</tt>.\nThe command was: %s.\n"
                        "Please adjust your settings to a valid executable."),
                        url, chaine);
            dialogue_error_hint (tmp_str, _("Cannot execute web browser"));
            g_free(tmp_str);
        }

#ifdef G_OS_WIN32
    }
    else
    {
                ShellExecute(0, "open", url, 0, 0, SW_SHOWNORMAL);
    }
#endif /* G_OS_WIN32 */

	g_free(chaine);

    return FALSE;
}

/**
 * Create a box with a nice bold title and content slightly indented.
 * All content is packed vertically in a GtkVBox.  The paddingbox is
 * also packed in its parent.
 *
 * \param parent Parent widget to pack paddingbox in
 * \param fill Give all available space to padding box or not
 * \param title Title to display on top of the paddingbox
 */
GtkWidget *new_paddingbox_with_title (GtkWidget *parent,
									  gboolean fill,
									  const gchar *title)
{
    GtkWidget *vbox, *hbox, *paddingbox, *label;
	GtkWidget *label2;
	gchar* tmp_str;

    vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, MARGIN_BOX);
    if (GTK_IS_BOX(parent))
    {
	gtk_box_pack_start (GTK_BOX (parent), vbox,
			     fill, fill, 0);
    }

    /* Creating label */
    label = gtk_label_new (NULL);
    utils_labels_set_alignment (GTK_LABEL (label), 0, 1);
    tmp_str = g_markup_printf_escaped ("<span weight=\"bold\">%s</span>", title);
    gtk_label_set_markup (GTK_LABEL (label), tmp_str);
    g_free(tmp_str);
    gtk_box_pack_start (GTK_BOX (vbox), label,
			 FALSE, FALSE, 0);
    gtk_widget_show (label);

    /* Creating horizontal box */
    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
    gtk_box_pack_start (GTK_BOX (vbox), hbox,
			 fill, fill, 0);

    /* Some padding.  ugly but the HiG advises it this way ;-) */
    label2 = gtk_label_new ("    ");
    gtk_box_pack_start (GTK_BOX (hbox), label2,
			 FALSE, FALSE, 0);

    /* Then make the vbox itself */
    paddingbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, MARGIN_BOX);
    gtk_box_pack_start (GTK_BOX (hbox), paddingbox,
			 TRUE, TRUE, 0);

	/* set label as object for futur usage */
	g_object_set_data (G_OBJECT (paddingbox), "paddingbox_label", label);

    if (GTK_IS_BOX(parent))
    {
	gtk_box_set_spacing (GTK_BOX(parent), 18);
    }

    return paddingbox;
}

/**
 * Function that makes a nice title with an optional icon.  It is
 * mainly used to automate preference tabs with titles.
 *
 * \param title Title that will be displayed in window
 * \param filename (relative or absolute) to an image in a file format
 * recognized by gtk_image_new_from_file().  Use NULL if you don't
 * want an image to be displayed
 *
 * \returns A pointer to a vbox widget that will contain all created
 * widgets and user defined widgets
 */
GtkWidget *new_vbox_with_title_and_icon (const gchar *title,
										 const gchar *image_filename)
{
    GtkWidget *vbox_pref, *hbox, *label, *image, *eb;
	gchar* tmpstr1;
	gchar* tmpstr2;

    vbox_pref = gtk_box_new (GTK_ORIENTATION_VERTICAL, MARGIN_BOX);
    gtk_widget_show (vbox_pref);

    eb = gtk_event_box_new ();
    gtk_widget_set_name (eb, "grey_box");
    gtk_box_pack_start (GTK_BOX (vbox_pref), eb, FALSE, FALSE, 0);


    /* Title hbox */
    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, MARGIN_BOX);
    gtk_widget_show (hbox);
    gtk_container_add (GTK_CONTAINER (eb), hbox);
    gtk_container_set_border_width (GTK_CONTAINER (hbox), 3);

    /* Icon */
    if (image_filename)
    {
	gchar* tmpstr = g_build_filename (gsb_dirs_get_pixmaps_dir (),
					  image_filename, NULL);
	image = gtk_image_new_from_file (tmpstr);
	g_free(tmpstr);
	gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
	gtk_widget_show (image);
    }

    /* Nice huge title */
    label = gtk_label_new (title);
    tmpstr1 = g_markup_escape_text (title, strlen(title));
    tmpstr2 = g_strconcat ("<span size=\"x-large\" weight=\"bold\">",
					tmpstr1,
					"</span>",
					NULL);
    gtk_label_set_markup (GTK_LABEL(label), tmpstr2);
    g_free(tmpstr1);
    g_free(tmpstr2);
    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
    gtk_widget_show (label);

    return vbox_pref;
}

/**
 * Returns TRUE if an account is loaded in memory.  Usefull to be sure
 * there is data to process.
 *
 * \return TRUE if an account is loaded in memory.
 */
gboolean assert_account_loaded (void)
{
  return gsb_data_account_get_accounts_amount () != 0;
}




/**
 * Function to explicitly update window "outside gtk_main ()"
 * For example during computations
 *
 * \return
 */
void update_gui (void)
{
    while (g_main_context_iteration (NULL, FALSE));
}


void register_button_as_linked (GtkWidget *widget,
								GtkWidget *linked)
{
    GSList * links;

    g_return_if_fail (widget != NULL);

    links = g_object_get_data (G_OBJECT(widget), "linked");
    g_object_set_data (G_OBJECT(widget), "linked", g_slist_append (links, linked));
}

/**
 *
 *
 *
 */
gboolean radio_set_active_linked_widgets (GtkWidget *widget)
{
    GSList * links;

    links = g_object_get_data (G_OBJECT(widget), "linked");

    while (links)
    {
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(links -> data),
				       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)));
	links = links -> next;
    }

    return FALSE;
}


/**
 * Get the string of the running GTK version.
 * Must be freed when no longer used
 *
 * \return string
 */
gchar *get_gtk_run_version (void)
{
    gchar *version = NULL;

    version = g_strdup_printf ("%d.%d.%d",
                                gtk_major_version,
                                gtk_minor_version,
                                gtk_micro_version);

    return version;
}

/**
 *
 *
 *
 *
 * */
void lance_mailer (const gchar *uri)
{
    GError *error = NULL;

#if GTK_CHECK_VERSION (3,22,0)
	GtkWindow *window;

	window = GTK_WINDOW (grisbi_app_get_active_window (NULL));
    if (gtk_show_uri_on_window (window, uri, GDK_CURRENT_TIME, &error) == FALSE)
#else
	if (gtk_show_uri (NULL, uri, GDK_CURRENT_TIME, &error) == FALSE)
#endif
    {
        gchar *tmp_str;

        tmp_str = g_strdup_printf (_("Grisbi was unable to execute a mailer to write at <tt>%s</tt>.\n"
                    "The error was: %s."),
                    uri, error -> message);
        g_error_free (error);
        dialogue_error_hint (tmp_str, _("Cannot execute mailer"));
        g_free(tmp_str);
    }
}

/**
 * set the background colors of a list store
 *
 * \param tree_view
 * \param n° de colonne
 *
 * \return FALSE
 * */
gboolean utils_set_list_store_background_color (GtkWidget *tree_view,
											   gint color_column)
{
    GtkTreeModel *model;
    GtkTreeIter iter;

    if (!tree_view)
        return FALSE;

    model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_view));

    if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter))
    {
        gint current_color = 0;

        do
        {
            gtk_list_store_set (GTK_LIST_STORE (model),
								&iter,
								color_column, gsb_rgba_get_couleur_with_indice ("couleur_fond", current_color),
								-1);

            current_color = !current_color;
        }
        while (gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter));
    }

    return FALSE;
}

/**
 * set the background colors of a tree store
 *
 * \param tree_view
 * \param n° de colonne
 *
 * \return FALSE
 * */
gboolean utils_set_tree_store_background_color (GtkWidget *tree_view,
											   gint color_column)
{
    GtkTreeModel *model;
    GtkTreeIter iter;

    if (!tree_view)
        return FALSE;

    model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_view));

    if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter))
    {
        gint current_color = 0;
        GtkTreeIter fils_iter;
        GtkTreePath *path;

        do
        {
            gtk_tree_store_set (GTK_TREE_STORE (model),
                        &iter,
                        color_column, gsb_rgba_get_couleur_with_indice ("couleur_fond", current_color),
                        -1);

            current_color = !current_color;
            path = gtk_tree_model_get_path (model, &iter);

            if (gtk_tree_model_iter_children (GTK_TREE_MODEL (model), &fils_iter, &iter)
             &&
             gtk_tree_view_row_expanded (GTK_TREE_VIEW (tree_view), path))
            {
                GtkTreeIter third_iter;

                do
                {
                    gtk_tree_store_set (GTK_TREE_STORE (model),
                                &fils_iter,
                                color_column, gsb_rgba_get_couleur_with_indice ("couleur_fond", current_color),
                                -1);

                    current_color = !current_color;
                    gtk_tree_path_free (path);
                    path = gtk_tree_model_get_path (model, &fils_iter);

                    if (gtk_tree_model_iter_children (GTK_TREE_MODEL (model), &third_iter, &fils_iter)
                     &&
                     gtk_tree_view_row_expanded (GTK_TREE_VIEW (tree_view), path))
                    {
                        do
                        {
                            gtk_tree_store_set (GTK_TREE_STORE (model),
                                        &third_iter,
                                        color_column, gsb_rgba_get_couleur_with_indice ("couleur_fond", current_color),
                                        -1);

                            current_color = !current_color;
                        }
                        while (gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &third_iter));
                    }
                }
                while (gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &fils_iter));
            }

            gtk_tree_path_free (path);
        }
        while (gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter));
    }

    return FALSE;
}

/**
 * crée un une image avec 2 états
 *
 * \param etat  0 = Warning 1 = OK
 *
 * \return      widget initialisé
 * */
GtkWidget *utils_get_image_with_etat (GtkMessageType msg,
									  gint initial,
									  const gchar *tooltip_0,
									  const gchar *tooltip_1)
{
    GtkWidget *hbox;
    GtkWidget *icon_0;
    GtkWidget *icon_1;
    GValue value = G_VALUE_INIT;

    g_value_init (&value, G_TYPE_BOOLEAN);
    g_value_set_boolean (&value, TRUE);

    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, MARGIN_BOX);
    g_object_set_data (G_OBJECT (hbox), "initial", GINT_TO_POINTER (initial));

    if (msg == GTK_MESSAGE_WARNING)
        icon_0 = gtk_image_new_from_icon_name ("gtk-dialog-warning", GTK_ICON_SIZE_MENU);
    else
        icon_0 = gtk_image_new_from_icon_name ("gtk-dialog-error", GTK_ICON_SIZE_MENU);

    g_object_set_property (G_OBJECT (icon_0), "no-show-all", &value);
    if (tooltip_0)
        gtk_widget_set_tooltip_text (icon_0, tooltip_0);
    gtk_box_pack_start (GTK_BOX (hbox), icon_0, FALSE, FALSE, 0);
    g_object_set_data (G_OBJECT (hbox), "icon_0", icon_0);

    icon_1 = gtk_image_new_from_icon_name ("gtk-apply", GTK_ICON_SIZE_MENU);
    g_object_set_property (G_OBJECT (icon_1), "no-show-all", &value);

    if (tooltip_1)
        gtk_widget_set_tooltip_text (icon_1, tooltip_1);
    gtk_box_pack_start (GTK_BOX (hbox), icon_1, FALSE, FALSE, 0);
    g_object_set_data (G_OBJECT (hbox), "icon_1", icon_1);

    if (initial)
        gtk_widget_show (icon_1);
    else
        gtk_widget_show (icon_0);

    gtk_widget_show (hbox);

    /* return */
    return hbox;
}

/**
 * change l'icone du widget en fonction de etat
 *
 * \param etat  0 = Warning 1 = OK
 *
 * \return      TRUE if OK FALSE si rien à faire
 * */
gboolean utils_set_image_with_etat (GtkWidget *widget,
									gint etat)
{
    GtkWidget *icon_0;
    GtkWidget *icon_1;
    GtkWidget *hbox;
    gint initial;

    hbox = g_object_get_data (G_OBJECT (widget), "icon");

    initial = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (hbox), "initial"));
    if (initial == etat)
        return FALSE;

    /* on met la nouvelle valeur pour initial */
    g_object_set_data (G_OBJECT (hbox), "initial", GINT_TO_POINTER (etat));

    icon_0 = g_object_get_data (G_OBJECT (hbox), "icon_0");
    icon_1 = g_object_get_data (G_OBJECT (hbox), "icon_1");

    if (etat)
    {
        gtk_widget_hide (icon_0);
        gtk_widget_show (icon_1);
    }
    else
    {
        gtk_widget_show (icon_0);
        gtk_widget_hide (icon_1);
    }

    /* return */
    return TRUE;
}

/**
 * Deleting children of container
 *
 * \param widget    container with children
 *
 * \return          none
 * */
void utils_container_remove_children (GtkWidget *widget)
{
    GList *children;

    children = gtk_container_get_children (GTK_CONTAINER (widget));

    if (children && children -> data)
    {
        gtk_container_remove (GTK_CONTAINER (widget), GTK_WIDGET (children -> data));
        g_list_free (children);
    }
}

/**
 *  expand all the tree_view and select le path when the widget is realized
 *
 *
 *
 * */
void utils_tree_view_set_expand_all_and_select_path_realize (GtkWidget *tree_view,
															 const gchar *str_path)
{
    GtkTreePath *path;

    gtk_tree_view_expand_all (GTK_TREE_VIEW (tree_view));

    /* selection du premier item sélectionnable */
    path = gtk_tree_path_new_from_string (str_path);

    gtk_tree_selection_select_path (GTK_TREE_SELECTION (
                        gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view))),
                        path);

    gtk_tree_path_free (path);

}

/**
 * Cette fonction retourne TRUE si tous les items sont sélectionnés
 *
 * \param le tree_view considéré
 *
 * \return TRUE si tous sélectionnés FALSE autrement.
 */
gboolean utils_tree_view_all_rows_are_selected (GtkTreeView *tree_view)
{
    GtkTreeModel *model;
    GtkTreeIter iter;
    GtkTreeSelection *selection;
    GList *rows_list;
    gint index;

    model = gtk_tree_view_get_model (tree_view);
    selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
    rows_list = gtk_tree_selection_get_selected_rows (selection, &model);
    index = g_list_length (rows_list);

    if (gtk_tree_model_get_iter_first (model, &iter))
    {
        do
        {
            index--;
            if (index < 0)
                break;
        }
        while (gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter));
    }

    g_list_free_full (rows_list, (GDestroyNotify) gtk_tree_path_free);

    if (index == 0)
        return TRUE;
    else
        return FALSE;
}

/**
 * Cette fonction retourne un GtkListStore à partir d'un tableau de chaine
 *
 * \param le tableau de chaines à mettre dans le modèle
 *
 * \return un GtkListStore.
 */
GtkListStore *utils_list_store_create_from_string_array (const gchar **array)
{
    GtkListStore *store = NULL;
    gint i = 0;

    store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);

    while (array[i])
    {
        GtkTreeIter iter;
        gchar *string = gettext (array[i]);

        gtk_list_store_append (store, &iter);
        gtk_list_store_set (store, &iter, 0, string, 1, i, -1);

        i++;
    }

    /* return */
    return store;
}

/**
 * Cette fonction crée la colonne visible d'un GtkComboBox
 *
 * \param le combo à initialiser
 * \param le numéro de la colonne texte
 *
 * \return
 */
void utils_gtk_combo_box_set_text_renderer (GtkComboBox *combo,
											int num_col)
{
    GtkCellRenderer *renderer;

    renderer = gtk_cell_renderer_text_new ();
    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
    gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer,
                                    "text", num_col,
                                    NULL);
}

/**
 * revoie un combo_box avec une GtkListStore et la colonne 0 en texte
 * \param le tableau de chaines à mettre dans le modèle
 *
 * \return un GtkComboBox.
 */
GtkWidget *utils_combo_box_make_from_string_array (const gchar **array)
{
    GtkWidget *combo;
    GtkTreeModel *model;

    combo = gtk_combo_box_new ();

    model = GTK_TREE_MODEL (utils_list_store_create_from_string_array (array));
    gtk_combo_box_set_model (GTK_COMBO_BOX (combo), model);
    utils_gtk_combo_box_set_text_renderer (GTK_COMBO_BOX (combo), 0);
    gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);

    return combo;
}

/**
 * set xalign and yalign to label
 *
 * \param
 * \param
 * \param
 *
 * \return
 * */
void utils_labels_set_alignment (GtkLabel *label,
								 gfloat xalign,
								 gfloat yalign)
{
    gtk_label_set_xalign (label, xalign);
    gtk_label_set_yalign (label, yalign);
}

/**
 * set xalign and yalign to label
 *
 * \param
 * \param
 * \param
 *
 * \return
 * */
void utils_widget_set_padding (GtkWidget *widget,
							   gint xpad,
							   gint ypad)
{
    if (xpad)
    {
        gtk_widget_set_margin_start (widget, xpad);
        gtk_widget_set_margin_end (widget, xpad);
    }

    if (ypad)
    {
        gtk_widget_set_margin_top (widget, ypad);
        gtk_widget_set_margin_bottom (widget, ypad);
    }
}

/**
 * Création d'un GtkToolButton à partir d'une image et d'un label
 *
 * \param image_name    filename
 * \param label_name    label for button
 *
 * \return a GtkToolItem or NULL
 * */
GtkWidget *utils_menu_item_new_from_image_label (const gchar *image_name,
												 const gchar *label_name)
{
    GtkWidget *menu_item = NULL;
    gchar *filename;

    filename = g_build_filename (gsb_dirs_get_pixmaps_dir (), image_name, NULL);
    if (filename)
    {
		GtkWidget *box;
        GtkWidget *image;
		GtkWidget *label;

        image = gtk_image_new_from_file (filename);
        g_free (filename);
		label = gtk_label_new (label_name);
		box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, MARGIN_BOX);
		gtk_container_add (GTK_CONTAINER (box), image);
		gtk_container_add (GTK_CONTAINER (box), label);

        menu_item = gtk_menu_item_new ();
		gtk_container_add (GTK_CONTAINER (menu_item), box);

    }
	else
		gtk_menu_item_new_with_label (label_name);

    return menu_item;
}

/**
 *
 *
 * \param
 * \param
 * \param
 *
 * \return
 **/
guint utils_widget_csv_separators_combo_block_unblock (gpointer instance,
													   gpointer entry,
													   gboolean block)
{
	guint hid;

	if (block)
		hid = g_signal_handlers_block_by_func (instance,
											   utils_widget_csv_separators_combo_changed,
											   entry);
	else
		hid = g_signal_handlers_unblock_by_func (instance,
												 utils_widget_csv_separators_combo_changed,
												 entry);

	return hid;
}

/**
 *
 *
 * \param
 * \param
 * \param
 *
 * \return
 **/
GtkWidget *utils_widget_csv_separators_new (GtkSizeGroup *size_group,
											GCallback hook_entry,
											gpointer assistant)
{
	GtkWidget *hbox;
	GtkWidget *combobox;
	GtkWidget *entry;
    gint i = 0;

	hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, MARGIN_BOX);

    combobox = gtk_combo_box_text_new ();
    do
    {
		gchar *complete_name;

		complete_name = g_strdup_printf ("%s : \"%s\"",
										 _(csv_separators [i].name),
										 (csv_separators [i].value ?
										 csv_separators [i].value : ""));
		gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combobox), complete_name);
		g_free (complete_name);

    }
    while (csv_separators [i ++].value);

	entry = gsb_automem_entry_new (NULL,
								   G_CALLBACK (hook_entry),
								   assistant);
    g_object_set_data (G_OBJECT(entry), "combobox", combobox);
    g_object_set_data (G_OBJECT(entry), "assistant", assistant);
	g_object_set_data (G_OBJECT(assistant), "entry", entry);

	/* set size of widgets */
	if (size_group)
	{
		gtk_size_group_add_widget (size_group, combobox);
		gtk_size_group_add_widget (size_group, entry);
		gtk_box_pack_start (GTK_BOX (hbox), combobox, FALSE, FALSE, MARGIN_BOX);
		gtk_box_pack_start (GTK_BOX (hbox), entry, FALSE, FALSE, MARGIN_BOX);
	}
	else
	{
		gtk_box_pack_start (GTK_BOX (hbox), combobox, TRUE, TRUE, MARGIN_BOX);
		gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, MARGIN_BOX);
	}

	/* set signals */
    g_signal_connect (G_OBJECT (combobox),
					  "changed",
					  G_CALLBACK (utils_widget_csv_separators_combo_changed),
					  entry);

	gtk_widget_show_all (hbox);

	return hbox;
}

/**
 *
 *
 * \param
 *
 * \return
 **/
gint utils_widget_csv_separators_combo_update (const gchar *separator)
{
	gint i = 0;

    while (csv_separators [i].value)
    {
        if (strcmp (csv_separators [i].value, separator) == 0)
        {
            break;
        }
        i ++ ;
    }

	return i;
}

/**
 *
 *
 * \param
 *
 * \return
 **/
/* Local Variables: */
/* c-basic-offset: 4 */
/* End: */