]> saetta.ns0.it Git - solipa/libsolipa/commitdiff
Added functions Solipa::string_replace, Solipa::tm_to_gdatetime
authorAndrea Zagli <azagli@libero.it>
Sun, 3 Jul 2011 11:13:16 +0000 (13:13 +0200)
committerAndrea Zagli <azagli@libero.it>
Sun, 3 Jul 2011 11:13:16 +0000 (13:13 +0200)
and Solipa::get_gdatetime_from_string.
Added test for Solipa::infor_bar.
Now the label inside info_bar is resizable and multiline.

.gitignore
configure.ac
src/utils.c
src/utils.h
tests/Makefile.am
tests/utils_infobar.c [new file with mode: 0644]

index 1ddf9ffe904db50681c117c33b1324b1a5938169..a4ab5a98214fa3ca4d4503ee1cec7f51a5cff99c 100644 (file)
@@ -49,9 +49,12 @@ Rules-quot
 *.exe
 tests/allegato
 tests/mail
+tests/mail_get_addresses_from_string
+tests/mailui
 tests/progresswindow
 tests/utils
 tests/utils_codfisc_piva
 tests/utils_format_money
+tests/utils_infobar
 tests/utils_round
 *.csv
index 5d837927c989490940f534718feb9b62cca4d098..dc357afa62d9c764153fd507f9eaa64ae11e13b0 100644 (file)
@@ -31,7 +31,7 @@ AC_PROG_RANLIB
 
 # Checks for libraries.
 PKG_CHECK_MODULES(SOLIPA, [gobject-2.0 >= 2.24.0
-                           glib-2.0 >= 2.24.0
+                           glib-2.0 >= 2.26.0
                            libgdaex >= 0.3.0
                            camel-provider-1.2 >= 2.32
                            libgtkform >= 0.3.0
index 330a730dea6e7fc7e8253e8eaf4d3bda8ba728a9..5a2dc173e05b42f9c7758891dd147a43c6086d30 100644 (file)
@@ -486,6 +486,31 @@ solipa_gtktreemodel_to_csv_gui (Solipa *solipa,
        return ret;
 }
 
+gchar
+*solipa_string_replace (const gchar *string,
+                        const gchar *origin,
+                        const gchar *replace)
+{
+       gchar *ret;
+       gchar *p;
+
+       if (string == NULL) return NULL;
+       if (origin == NULL || replace == NULL) return g_strdup (string);
+
+       p = g_strstr_len (string, -1, origin);
+
+       if (p == NULL)
+               {
+                       return g_strdup (string);
+               }
+
+       ret = g_strndup (string, p - string);
+
+       ret = g_strdup_printf ("%s%s%s", ret, replace, p + strlen (origin));
+
+       return ret;
+}
+
 gchar
 *solipa_format_money (gdouble number,
                       gint decimals,
@@ -617,6 +642,176 @@ GDate
                               (GDateYear)tt_tm->tm_year + 1900);
 }
 
+GDateTime
+*solipa_tm_to_gdatetime (struct tm *tm_data)
+{
+       GDateTime *ret;
+
+       ret = NULL;
+       if (tm_data != NULL)
+               {
+                       ret = g_date_time_new_local (tm_data->tm_year,
+                                                    tm_data->tm_mon,
+                                                    tm_data->tm_mday,
+                                                    tm_data->tm_hour,
+                                                    tm_data->tm_min,
+                                                    tm_data->tm_sec);
+               }
+
+       return ret;
+}
+
+const gchar
+*solipa_tm_to_sql (struct tm *tm_data, const gchar *format)
+{
+       const gchar *ret;
+       gchar *_format;
+
+       ret = g_strdup ("NULL");
+
+       if (tm_data != NULL)
+               {
+                       if (format == NULL)
+                               {
+                                       _format = g_strdup ("%Y-%m-%d %H:%M:%S");
+                               }
+                       else
+                               {
+                                       _format = g_strstrip (g_strdup (format));
+                                       if (g_strcmp0 (_format, "") == 0)
+                                               {
+                                                       _format = g_strdup ("%Y-%m-%d %H:%M:%S");
+                                               }
+                               }
+                       ret = g_strdup_printf ("'%s'", g_date_time_format (solipa_tm_to_gdatetime (tm_data), _format));
+               }
+
+       return ret;
+}
+
+GDateTime
+*solipa_get_gdatetime_from_string (const gchar *string, const gchar *format)
+{
+       GDateTime *ret;
+       gchar *new_str;
+       gchar *new_format;
+       gchar **str_tokens;
+       gchar **format_tokens;
+       gchar *delimiters;
+       guint len_strv;
+       guint len_formatv;
+       guint i;
+       guint i_to;
+
+       gint year;
+       gint month;
+       gint day;
+       gint hour;
+       gint minute;
+       gdouble seconds;
+
+       g_return_val_if_fail (string != NULL, NULL);
+
+       new_str = g_strstrip (g_strdup (string));
+       if (g_strcmp0 (new_str, "") == 0)
+               {
+                       g_free (new_str);
+                       return NULL;
+               }
+
+       if (format == NULL)
+               {
+                       new_format = g_strdup ("%Y-%m-%d %H:%M:%S");
+               }
+       else
+               {
+                       new_format = g_strstrip (g_strdup (format));
+                       if (g_strcmp0 (new_format, "") == 0)
+                               {
+                                       new_format = g_strdup ("%Y-%m-%d %H:%M:%S");
+                               }
+               }
+
+       /* removes format identifiers to find delimiters */
+       delimiters = solipa_string_replace (new_format, "%Y", "");
+       delimiters = solipa_string_replace (delimiters, "%m", "");
+       delimiters = solipa_string_replace (delimiters, "%d", "");
+       delimiters = solipa_string_replace (delimiters, "%H", "");
+       delimiters = solipa_string_replace (delimiters, "%M", "");
+       delimiters = solipa_string_replace (delimiters, "%S", "");
+
+       if (delimiters == NULL || g_strcmp0 (delimiters, "") == 0)
+               {
+                       g_free (delimiters);
+                       g_free (new_format);
+                       g_free (new_str);
+                       return NULL;
+               }
+
+       str_tokens = g_strsplit_set (string, delimiters, -1);
+       if (str_tokens == NULL)
+               {
+                       g_free (delimiters);
+                       g_free (new_format);
+                       g_free (new_str);
+                       return NULL;
+               }
+
+       format_tokens = g_strsplit_set (format, delimiters, -1);
+       if (format_tokens == NULL)
+               {
+                       g_free (delimiters);
+                       g_free (new_format);
+                       g_free (new_str);
+                       return NULL;
+               }
+
+       len_strv = g_strv_length (str_tokens);
+       len_formatv = g_strv_length (format_tokens);
+
+       i_to = MIN (len_strv, len_formatv);
+       for (i = 0; i < i_to; i++)
+               {
+                       if (g_strcmp0 (format_tokens[i], "%Y") == 0)
+                               {
+                                       year = strtol (str_tokens[i], NULL, 10) - 1900;
+                               }
+                       else if (g_strcmp0 (format_tokens[i], "%m") == 0)
+                               {
+                                       month = strtol (str_tokens[i], NULL, 10) - 1;
+                               }
+                       else if (g_strcmp0 (format_tokens[i], "%d") == 0)
+                               {
+                                       day = strtol (str_tokens[i], NULL, 10);
+                               }
+                       else if (g_strcmp0 (format_tokens[i], "%H") == 0)
+                               {
+                                       hour = strtol (str_tokens[i], NULL, 10);
+                               }
+                       else if (g_strcmp0 (format_tokens[i], "%M") == 0)
+                               {
+                                       minute = strtol (str_tokens[i], NULL, 10);
+                               }
+                       else if (g_strcmp0 (format_tokens[i], "%S") == 0)
+                               {
+                                       seconds = g_strtod (str_tokens[i], NULL);
+                               }
+               }
+
+       g_free (delimiters);
+       g_free (new_format);
+       g_free (new_str);
+
+       ret = g_date_time_new_local (year,
+                                    month,
+                                    day,
+                                    hour,
+                                    minute,
+                                    seconds);
+
+       return ret;
+}
+
 gdouble
 solipa_round (gdouble value, guint n_decimals)
 {
@@ -741,35 +936,6 @@ struct tm
        return ret;
 }
 
-const gchar
-*solipa_tm_to_sql (struct tm *tm_data, const gchar *format)
-{
-       const gchar *ret;
-       gchar *_format;
-
-       ret = g_strdup ("NULL");
-
-       if (tm_data != NULL)
-               {
-                       if (format == NULL)
-                               {
-                                       _format = g_strdup ("%Y-%m-%d %H:%M");
-                               }
-                       else
-                               {
-                                       _format = g_strdup (format);
-                                       g_strstrip (_format);
-                                       if (g_strcmp0 (_format, "") == 0)
-                                               {
-                                                       _format = g_strdup ("%Y-%m-%d %H:%M");
-                                               }
-                               }
-                       ret = g_strdup_printf ("'%s'", gtk_form_field_datetime_get_str_from_tm (tm_data, _format));
-               }
-
-       return ret;
-}
-
 gboolean
 solipa_chk_codice_fiscale (const gchar *codice_fiscale, gboolean empty_good)
 {
@@ -1085,17 +1251,44 @@ solipa_message_dialog (GtkWidget *transient,
        return risp;
 }
 
+#define LABEL_SIZE 90
+
+static void
+label_size_request (GtkWidget *widget,
+                    GtkRequisition *requisition,
+                    gpointer user_data)
+{
+       requisition->width = LABEL_SIZE;
+}
+
+static void
+label_size_allocate_after (GtkWidget *widget,
+                           GtkAllocation *allocation,
+                           gpointer user_data)
+{
+       GtkWidget *lbl = (GtkWidget *)user_data;
+       gint width = LABEL_SIZE;
+
+       if (allocation->width > width)
+               width = allocation->width;
+
+       gtk_widget_set_size_request (lbl, width, -1);
+       /* Sometimes labels aren't drawn correctly after resize without this */
+       gtk_widget_queue_draw (lbl);
+}
+
 GtkWidget
 *solipa_info_bar (GtkMessageType type, const gchar *message_text)
 {
        GtkWidget *infobar;
        GtkWidget *hbox;
        GtkWidget *img;
+       GtkWidget *event_box;
        GtkWidget *lbl;
 
        gchar *stock;
 
-       hbox = gtk_hbox_new (FALSE, 5);
+       hbox = gtk_hbox_new (FALSE, 10);
        switch (type)
                {
                        case GTK_MESSAGE_INFO:
@@ -1121,15 +1314,29 @@ GtkWidget
 
        img = gtk_image_new_from_stock (stock, GTK_ICON_SIZE_BUTTON);
        gtk_box_pack_start (GTK_BOX (hbox), img, FALSE, FALSE, 0);
+
+       event_box = gtk_event_box_new ();
+       gtk_event_box_set_visible_window (GTK_EVENT_BOX (event_box), FALSE);
+       gtk_box_pack_start (GTK_BOX (hbox), event_box, TRUE, TRUE, 0);
+
        lbl = gtk_label_new (message_text);
-       gtk_box_pack_start (GTK_BOX (hbox), lbl, TRUE, TRUE, 0);
+       gtk_label_set_line_wrap (GTK_LABEL (lbl), TRUE);
+       gtk_label_set_line_wrap_mode (GTK_LABEL (lbl), PANGO_WRAP_WORD_CHAR);
+       gtk_label_set_justify (GTK_LABEL (lbl), GTK_JUSTIFY_CENTER);
+       gtk_misc_set_alignment (GTK_MISC (lbl), 0.5, 0.5);
+       gtk_container_add (GTK_CONTAINER (event_box), lbl);
+
+       /* A Hack so that PANGO_WRAP_WORD_CHAR works nicely */
+       g_signal_connect (G_OBJECT (event_box), "size-request",
+                         G_CALLBACK (label_size_request), NULL);
+       g_signal_connect_after (G_OBJECT (event_box), "size-allocate",
+                               G_CALLBACK (label_size_allocate_after), (gpointer)lbl);
+
        infobar = gtk_info_bar_new ();
-       gtk_container_add (GTK_CONTAINER (gtk_info_bar_get_content_area (GTK_INFO_BAR (infobar))), hbox);
        gtk_info_bar_set_message_type (GTK_INFO_BAR (infobar), type);
+       gtk_container_add (GTK_CONTAINER (gtk_info_bar_get_content_area (GTK_INFO_BAR (infobar))), hbox);
 
-       gtk_widget_show (hbox);
-       gtk_widget_show (img);
-       gtk_widget_show (lbl);
+       gtk_widget_show_all (infobar);
 
        return infobar;
 }
index fa07d6b29f194ef2227d9300fbae6926ebb90336..3e283e06a9d2af3526cc9aabbe04af24c395a975 100644 (file)
@@ -48,12 +48,21 @@ guint solipa_gtktreemodel_to_csv_gui (Solipa *solipa,
                                  gchar **columns_title,
                                  guint n_columns);
 
+gchar *solipa_string_replace (const gchar *string,
+                              const gchar *origin,
+                              const gchar *replace);
+
 gchar *solipa_format_money (gdouble number, gint decimals, gboolean with_currency_symbol);
 gdouble solipa_unformat_money (const gchar *value);
 
 struct tm *solipa_get_now_tm (void);
 GDate *solipa_get_today_gdate (void);
 
+GDateTime *solipa_tm_to_gdatetime (struct tm *tm_data);
+const gchar *solipa_tm_to_sql (struct tm *tm_data, const gchar *format);
+
+GDateTime *solipa_get_gdatetime_from_string (const gchar *string, const gchar *format);
+
 gdouble solipa_round (gdouble value, guint n_decimals);
 
 const gchar *solipa_gtk_tree_model_get_value_string (GtkTreeModel *model, GtkTreeIter *iter, gint column);
@@ -62,8 +71,6 @@ gdouble solipa_gtk_tree_model_get_value_double (GtkTreeModel *model, GtkTreeIter
 const gchar *solipa_gtk_tree_model_get_value_double_sql (GtkTreeModel *model, GtkTreeIter *iter, gint column);
 struct tm *solipa_gtk_tree_model_get_value_tm (GtkTreeModel *model, GtkTreeIter *iter, gint column, const gchar *format);
 
-const gchar *solipa_tm_to_sql (struct tm *tm_data, const gchar *format);
-
 gboolean solipa_chk_codice_fiscale (const gchar *codice_fiscale, gboolean empty_good);
 gboolean solipa_chk_partita_iva (const gchar *partita_iva, gboolean empty_good);
 
index f694e60c67829e69994d2f7044e2dec29567abc3..da8ccfd2dc0a39f4cc33b6c18da231282943a065 100644 (file)
@@ -17,4 +17,5 @@ noinst_PROGRAMS = allegato \
                   utils \
                   utils_codfisc_piva \
                   utils_format_money \
+                  utils_infobar \
                   utils_round
diff --git a/tests/utils_infobar.c b/tests/utils_infobar.c
new file mode 100644 (file)
index 0000000..bfab59e
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2011 Andrea Zagli <azagli@libero.it>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include <utils.h>
+
+GtkWidget *w;
+
+gboolean
+on_w_delete_event (GtkWidget *widget,
+                   GdkEvent *event,
+                   gpointer user_data)
+{
+       return FALSE;
+}
+
+int
+main (int argc, char *argv[])
+{
+       GtkWidget *infobar;
+
+       gtk_init (&argc, &argv);
+
+       w = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+       g_signal_connect (w, "delete-event", G_CALLBACK (on_w_delete_event), NULL);
+       g_signal_connect (w, "destroy", gtk_main_quit, NULL);
+
+       infobar = solipa_info_bar (GTK_MESSAGE_INFO, "Message very very very very very very very very very very very very very very very very very very very very very very very long");
+       gtk_container_add (GTK_CONTAINER (w), infobar);
+
+       gtk_widget_show (w);
+
+       gtk_main ();
+
+       return 0;
+}