From: Andrea Zagli Date: Sun, 3 Jul 2011 11:13:16 +0000 (+0200) Subject: Added functions Solipa::string_replace, Solipa::tm_to_gdatetime X-Git-Tag: 0.5.0~56 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=e08276316c7968c8d445059654fcc3145e42290a;p=solipa%2Flibsolipa Added functions Solipa::string_replace, Solipa::tm_to_gdatetime and Solipa::get_gdatetime_from_string. Added test for Solipa::infor_bar. Now the label inside info_bar is resizable and multiline. --- diff --git a/.gitignore b/.gitignore index 1ddf9ff..a4ab5a9 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/configure.ac b/configure.ac index 5d83792..dc357af 100644 --- a/configure.ac +++ b/configure.ac @@ -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 diff --git a/src/utils.c b/src/utils.c index 330a730..5a2dc17 100644 --- a/src/utils.c +++ b/src/utils.c @@ -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; } diff --git a/src/utils.h b/src/utils.h index fa07d6b..3e283e0 100644 --- a/src/utils.h +++ b/src/utils.h @@ -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); diff --git a/tests/Makefile.am b/tests/Makefile.am index f694e60..da8ccfd 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 0000000..bfab59e --- /dev/null +++ b/tests/utils_infobar.c @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2011 Andrea Zagli + * + * 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 + +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; +}