2005-02-25 [paul] 1.0.1cvs15.12
[claws.git] / src / gtk / gtkutils.c
index 587e66abaea38c9351b24dab025be3ff2e717499..b23177ca4f1f9dee7b0822fc30f72691eb01cd40 100644 (file)
@@ -22,6 +22,7 @@
 #endif
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <gdk/gdkkeysyms.h>
 #include <gdk/gdk.h>
 #include <gtk/gtkwidget.h>
@@ -40,7 +41,7 @@
 #  include <wctype.h>
 #endif
 
-#include "intl.h"
+#include "defs.h"
 #include "gtkutils.h"
 #include "utils.h"
 #include "gtksctree.h"
@@ -48,6 +49,7 @@
 #include "stock_pixmap.h"
 #include "menu.h"
 #include "prefs_account.h"
+#include "prefs_common.h"
 
 #warning FIXME_GTK2
 gboolean gtkut_get_font_size(GtkWidget *widget,
@@ -68,6 +70,21 @@ gboolean gtkut_get_font_size(GtkWidget *widget,
        return TRUE;
 }
 
+void gtkut_widget_set_small_font_size(GtkWidget *widget)
+{
+       PangoFontDescription *font_desc;
+       gint size;
+
+       g_return_if_fail(widget != NULL);
+       g_return_if_fail(widget->style != NULL);
+
+       font_desc = pango_font_description_from_string(NORMAL_FONT);
+       size = pango_font_description_get_size(font_desc);
+       pango_font_description_set_size(font_desc, size * PANGO_SCALE_SMALL);
+       gtk_widget_modify_font(widget, font_desc);
+       pango_font_description_free(font_desc);
+}
+
 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
 {
        g_return_if_fail(color != NULL);
@@ -499,6 +516,141 @@ void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
        gtk_container_remove(container, widget);
 }
 
+gboolean gtkut_text_buffer_match_string(GtkTextBuffer *textbuf,
+                                       const GtkTextIter *iter,
+                                       gunichar *wcs, gint len,
+                                       gboolean case_sens)
+{
+       GtkTextIter start_iter, end_iter;
+       gchar *utf8str, *p;
+       gint match_count;
+
+       start_iter = end_iter = *iter;
+       gtk_text_iter_forward_chars(&end_iter, len);
+
+       utf8str = gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter,
+                                          FALSE);
+       if (!utf8str) return FALSE;
+
+       if ((gint)g_utf8_strlen(utf8str, -1) != len) {
+               g_free(utf8str);
+               return FALSE;
+       }
+
+       for (p = utf8str, match_count = 0;
+            *p != '\0' && match_count < len;
+            p = g_utf8_next_char(p), match_count++) {
+               gunichar wc;
+
+               wc = g_utf8_get_char(p);
+
+               if (case_sens) {
+                       if (wc != wcs[match_count])
+                               break;
+               } else {
+                       if (g_unichar_tolower(wc) !=
+                           g_unichar_tolower(wcs[match_count]))
+                               break;
+               }
+       }
+
+       g_free(utf8str);
+
+       if (match_count == len)
+               return TRUE;
+       else
+               return FALSE;
+}
+
+gboolean gtkut_text_buffer_find(GtkTextBuffer *buffer, const GtkTextIter *iter,
+                               const gchar *str, gboolean case_sens,
+                               GtkTextIter *match_pos)
+{
+       gunichar *wcs;
+       gint len;
+       glong items_read = 0, items_written = 0;
+       GError *error = NULL;
+       GtkTextIter iter_;
+       gboolean found = FALSE;
+
+       wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
+       if (error != NULL) {
+               g_warning("An error occured while converting a string from UTF-8 to UCS-4: %s\n",
+                         error->message);
+               g_error_free(error);
+       }
+       if (!wcs || items_written <= 0) return FALSE;
+       len = (gint)items_written;
+
+       iter_ = *iter;
+       do {
+               found = gtkut_text_buffer_match_string
+                       (buffer, &iter_, wcs, len, case_sens);
+               if (found) {
+                       *match_pos = iter_;
+                       break;
+               }
+       } while (gtk_text_iter_forward_char(&iter_));
+
+       g_free(wcs);
+
+       return found;
+}
+
+gboolean gtkut_text_buffer_find_backward(GtkTextBuffer *buffer,
+                                        const GtkTextIter *iter,
+                                        const gchar *str, gboolean case_sens,
+                                        GtkTextIter *match_pos)
+{
+       gunichar *wcs;
+       gint len;
+       glong items_read = 0, items_written = 0;
+       GError *error = NULL;
+       GtkTextIter iter_;
+       gboolean found = FALSE;
+
+       wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
+       if (error != NULL) {
+               g_warning("An error occured while converting a string from UTF-8 to UCS-4: %s\n", error->message);
+               g_error_free(error);
+       }
+       if (!wcs || items_written <= 0) return FALSE;
+       len = (gint)items_written;
+
+       iter_ = *iter;
+       while (gtk_text_iter_backward_char(&iter_)) {
+               found = gtkut_text_buffer_match_string
+                       (buffer, &iter_, wcs, len, case_sens);
+               if (found) {
+                       *match_pos = iter_;
+                       break;
+               }
+       }
+
+       g_free(wcs);
+
+       return found;
+}
+
+gchar *gtkut_text_view_get_selection(GtkTextView *textview)
+{
+       GtkTextBuffer *buffer;
+       GtkTextIter start_iter, end_iter;
+       gboolean found;
+
+       g_return_val_if_fail(GTK_IS_TEXT_VIEW(textview), NULL);
+
+       buffer = gtk_text_view_get_buffer(textview);
+       found = gtk_text_buffer_get_selection_bounds(buffer,
+                                                    &start_iter,
+                                                    &end_iter);
+       if (found)
+               return gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
+                                               FALSE);
+       else
+               return NULL;
+}
+
 void gtkut_window_popup(GtkWidget *window)
 {
        gint x, y, sx, sy, new_x, new_y;
@@ -515,8 +667,7 @@ void gtkut_window_popup(GtkWidget *window)
        if (new_x != x || new_y != y)
                gdk_window_move(window->window, new_x, new_y);
 
-       gdk_window_raise(window->window);
-       gdk_window_show(window->window);
+       gtk_window_present(GTK_WINDOW(window));
 }
 
 void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
@@ -539,26 +690,6 @@ void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
        *py = y;
 }
 
-void gtkut_widget_disable_theme_engine(GtkWidget *widget)
-{
-#warning FIXME_GTK2
-#if 0
-       GtkStyle *style, *new_style;
-
-       style = gtk_widget_get_style(widget);
-
-       if (style->engine) {
-               GtkThemeEngine *engine;
-
-               engine = style->engine;
-               style->engine = NULL;
-               new_style = gtk_style_copy(style);
-               style->engine = engine;
-               gtk_widget_set_style(widget, new_style);
-       }
-#endif
-}
-
 void gtkut_widget_wait_for_draw(GtkWidget *widget)
 {
        if (!GTK_WIDGET_DRAWABLE(widget)) return;
@@ -682,154 +813,6 @@ void gtkut_set_widget_bgcolor_rgb(GtkWidget *widget, guint rgbvalue)
        gtk_widget_set_style(widget, newstyle);
 }
   
-gboolean gtkut_text_buffer_match_string(GtkTextBuffer *textbuf, gint pos, gunichar *wcs,
-                                       gint len, gboolean case_sens)
-{
-       GtkTextIter start_iter, end_iter;
-       gchar *utf8str;
-       gint match_count = 0;
-
-       gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, pos);
-       gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter, pos + len);
-
-       utf8str = gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
-       if (!utf8str) return FALSE;
-
-       if ((gint) g_utf8_strlen(utf8str, -1) != len) {
-               g_free(utf8str);
-               return FALSE;
-       }
-
-       for (; match_count < len; pos++, match_count++) {
-               gchar *ptr = g_utf8_offset_to_pointer(utf8str, match_count);
-               gunichar ch;
-
-               if (!ptr) break;
-               ch = g_utf8_get_char(ptr);
-
-               if (case_sens) {
-                       if (ch != wcs[match_count])
-                               break;
-               } else {
-                       if (g_unichar_tolower(ch) !=
-                           g_unichar_tolower(wcs[match_count]))
-                               break;
-               }
-       }
-
-       g_free(utf8str);
-
-       if (match_count == len)
-               return TRUE;
-       else
-               return FALSE;
-}
-
-guint gtkut_text_buffer_str_compare_n(GtkTextBuffer *textbuf,
-                                     guint pos1, guint pos2,
-                                     guint len, guint text_len)
-{
-       guint i;
-
-       for (i = 0; i < len && pos1 + i < text_len && pos2 + i < text_len; i++) {
-               GtkTextIter start_iter, end_iter;
-               gchar *utf8str1, *utf8str2;
-
-               gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter,
-                                                  pos1 + i);
-               gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
-                                                  pos1 + i + 1);
-               utf8str1 = gtk_text_buffer_get_text(textbuf,
-                                                   &start_iter,
-                                                   &end_iter,
-                                                   FALSE);
-
-               gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter,
-                                                  pos2 + i);
-               gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter,
-                                                  pos2 + i + 1);
-               utf8str2 = gtk_text_buffer_get_text(textbuf,
-                                                   &start_iter,
-                                                   &end_iter,
-                                                   FALSE);
-
-               if (!utf8str1 || !utf8str2 || strcmp(utf8str1, utf8str2)) {
-                       g_free(utf8str1);
-                       g_free(utf8str2);
-                       break;
-               }
-
-               g_free(utf8str1);
-               g_free(utf8str2);
-       }
-
-       return i;
-}
-
-guint gtkut_text_buffer_str_compare(GtkTextBuffer *textbuf,
-                                   guint start_pos, guint text_len,
-                                   const gchar *str)
-{
-       gunichar *wcs;
-       guint len = 0;
-       glong items_read = 0, items_written = 0;
-       gboolean result;
-       GError *error = NULL;
-
-       if (!str) return 0;
-
-       wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
-       if (error != NULL) {
-               g_warning("An error occured while converting a string from UTF-8 to UCS-4: %s\n",
-                         error->message);
-               g_error_free(error);
-       }
-       if (!wcs || items_written <= 0) return 0;
-       len = (guint) items_written;
-
-       if (len > text_len - start_pos)
-               result = FALSE;
-       else
-               result = gtkut_text_buffer_match_string(textbuf, start_pos,
-                                                       wcs, len,
-                                                       TRUE);
-
-       g_free(wcs);
-
-       return result ? len : 0;
-}
-
-gboolean gtkut_text_buffer_is_uri_string(GtkTextBuffer *textbuf,
-                                        guint start_pos, guint text_len)
-{
-       if (gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "http://")  ||
-           gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "ftp://")   ||
-           gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "https://") ||
-           gtkut_text_buffer_str_compare(textbuf, start_pos, text_len, "www."))
-               return TRUE;
-
-       return FALSE;
-}
-  
-gchar *gtkut_text_view_get_selection(GtkTextView *textview)
-{
-       GtkTextBuffer *buffer;
-       GtkTextIter start_iter, end_iter;
-       gboolean found;
-
-       g_return_val_if_fail(GTK_IS_TEXT_VIEW(textview), NULL);
-
-       buffer = gtk_text_view_get_buffer(textview);
-       found = gtk_text_buffer_get_selection_bounds(buffer,
-                                                    &start_iter,
-                                                    &end_iter);
-       if (found)
-               return gtk_text_buffer_get_text(buffer, &start_iter, &end_iter,
-                                               FALSE);
-       else
-               return NULL;
-}
-
 /*!
  *\brief       Tries to find a focused child using a lame strategy
  */