]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtktextutil.c
Drop the Motif DND protocol
[~andy/gtk] / gtk / gtktextutil.c
index f601e2f9bd9206ab52080734173fe338026b1dd4..a2811f2d3cb8ed0e5ade1a51fa41e103d33eb381 100644 (file)
@@ -12,9 +12,7 @@
  * 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., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
  */
 
-#include <config.h>
-#include "gtktextutil.h"
+#include "config.h"
+
 #include "gtktextview.h"
+#include "gtktextutil.h"
 
 #define GTK_TEXT_USE_INTERNAL_UNSUPPORTED_API
 
@@ -34,7 +33,6 @@
 #include "gtktextbuffer.h"
 #include "gtkmenuitem.h"
 #include "gtkintl.h"
-#include "gtkalias.h"
 
 #define DRAG_ICON_MAX_WIDTH 250
 #define DRAG_ICON_MAX_HEIGHT 250
@@ -69,6 +67,26 @@ static const GtkUnicodeMenuEntry bidi_menu_entries[] = {
   { N_("ZWNJ Zero width _non-joiner"), 0x200C }
 };
 
+static GtkTextUtilCallbackInfo *
+callback_info_new (GtkTextUtilCharChosenFunc  func,
+                   gpointer                   data)
+{
+  GtkTextUtilCallbackInfo *info;
+
+  info = g_slice_new (GtkTextUtilCallbackInfo);
+
+  info->func = func;
+  info->data = data;
+
+  return info;
+}
+
+static void
+callback_info_free (GtkTextUtilCallbackInfo *info)
+{
+  g_slice_free (GtkTextUtilCallbackInfo, info);
+}
+
 static void
 activate_cb (GtkWidget *menu_item,
              gpointer   data)
@@ -84,7 +102,7 @@ activate_cb (GtkWidget *menu_item,
   (* info->func) (buf, info->data);
 }
 
-/**
+/*
  * _gtk_text_util_append_special_char_menuitems
  * @menushell: a #GtkMenuShell
  * @callback:  call this when an item is chosen
@@ -98,32 +116,29 @@ activate_cb (GtkWidget *menu_item,
  * become public sometime, but it probably needs more thought first.
  * e.g. maybe there should be a way to just get the list of items,
  * instead of requiring the menu items to be created.
- **/
+ */
 void
 _gtk_text_util_append_special_char_menuitems (GtkMenuShell              *menushell,
                                               GtkTextUtilCharChosenFunc  func,
                                               gpointer                   data)
 {
   int i;
-  
+
   for (i = 0; i < G_N_ELEMENTS (bidi_menu_entries); i++)
     {
       GtkWidget *menuitem;
       GtkTextUtilCallbackInfo *info;
 
-      /* wasteful to have a bunch of copies, but simplifies mem management */
-      info = g_new (GtkTextUtilCallbackInfo, 1);
-      info->func = func;
-      info->data = data;
-      
+      info = callback_info_new (func, data);
+
       menuitem = gtk_menu_item_new_with_mnemonic (_(bidi_menu_entries[i].label));
       g_object_set_data (G_OBJECT (menuitem), I_("gtk-unicode-menu-entry"),
                          (gpointer)&bidi_menu_entries[i]);
-      
+
       g_signal_connect_data (menuitem, "activate",
                              G_CALLBACK (activate_cb),
-                             info, (GClosureNotify) g_free, 0);
-      
+                             info, (GClosureNotify) callback_info_free, 0);
+
       gtk_widget_show (menuitem);
       gtk_menu_shell_append (menushell, menuitem);
     }
@@ -176,24 +191,30 @@ limit_layout_lines (PangoLayout *layout)
     }
 }
 
-/**
+/*
  * _gtk_text_util_create_drag_icon
  * @widget: #GtkWidget to extract the pango context
  * @text: a #gchar to render the icon
  * @len: length of @text, or -1 for NUL-terminated text
  *
  * Creates a drag and drop icon from @text.
- **/
-GdkPixmap *
+ *
+ * Returns: a #cairo_surface_t to use as DND icon
+ */
+cairo_surface_t *
 _gtk_text_util_create_drag_icon (GtkWidget *widget, 
                                  gchar     *text,
                                  gsize      len)
 {
-  GdkDrawable  *drawable = NULL;
+  GtkStyleContext *style_context;
+  GtkStateFlags state;
+  cairo_surface_t *surface;
   PangoContext *context;
   PangoLayout  *layout;
+  cairo_t      *cr;
   gint          pixmap_height, pixmap_width;
   gint          layout_width, layout_height;
+  GdkRGBA       color;
 
   g_return_val_if_fail (widget != NULL, NULL);
   g_return_val_if_fail (text != NULL, NULL);
@@ -216,70 +237,98 @@ _gtk_text_util_create_drag_icon (GtkWidget *widget,
   pixmap_width  = layout_width  / PANGO_SCALE + DRAG_ICON_LAYOUT_BORDER * 2;
   pixmap_height = layout_height / PANGO_SCALE + DRAG_ICON_LAYOUT_BORDER * 2;
 
-  drawable = gdk_pixmap_new (widget->window,
-                             pixmap_width  + 2,
-                             pixmap_height + 2,
-                             -1);
-
-  gdk_draw_rectangle (drawable,
-                      widget->style->base_gc [GTK_WIDGET_STATE (widget)],
-                      TRUE,
-                      0, 0,
-                      pixmap_width + 1,
-                      pixmap_height + 1);
-
-  gdk_draw_layout (drawable,
-                   widget->style->text_gc [GTK_WIDGET_STATE (widget)],
-                   1 + DRAG_ICON_LAYOUT_BORDER,
-                   1 + DRAG_ICON_LAYOUT_BORDER,
-                   layout);
-
-  gdk_draw_rectangle (drawable,
-                      widget->style->black_gc,
-                      FALSE,
-                      0, 0,
-                      pixmap_width + 1,
-                      pixmap_height + 1);
+  style_context = gtk_widget_get_style_context (widget);
+  state = gtk_widget_get_state_flags (widget);
+
+  surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
+                                               CAIRO_CONTENT_COLOR,
+                                               pixmap_width  + 2,
+                                               pixmap_height + 2);
+  cr = cairo_create (surface);
 
+  gtk_style_context_save (style_context);
+  gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_VIEW);
+
+  gtk_style_context_get_background_color (style_context, state, &color);
+  gdk_cairo_set_source_rgba (cr, &color);
+  cairo_paint (cr);
+
+  gtk_style_context_get_color (style_context, state, &color);
+  gdk_cairo_set_source_rgba (cr, &color);
+  cairo_move_to (cr, 1 + DRAG_ICON_LAYOUT_BORDER, 1 + DRAG_ICON_LAYOUT_BORDER);
+  pango_cairo_show_layout (cr, layout);
+
+  cairo_set_source_rgb (cr, 0, 0, 0);
+  cairo_rectangle (cr, 0.5, 0.5, pixmap_width + 1, pixmap_height + 1);
+  cairo_set_line_width (cr, 1.0);
+  cairo_stroke (cr);
+
+  cairo_destroy (cr);
   g_object_unref (layout);
 
-  return drawable;
+  cairo_surface_set_device_offset (surface, 2, 2);
+
+  gtk_style_context_restore (style_context);
+
+  return surface;
 }
 
 static void
 gtk_text_view_set_attributes_from_style (GtkTextView        *text_view,
-                                         GtkTextAttributes  *values,
-                                         GtkStyle           *style)
+                                         GtkTextAttributes  *values)
 {
-  values->appearance.bg_color = style->base[GTK_STATE_NORMAL];
-  values->appearance.fg_color = style->text[GTK_STATE_NORMAL];
+  GtkStyleContext *context;
+  GdkRGBA bg_color, fg_color;
+  GtkStateFlags state;
+
+  context = gtk_widget_get_style_context (GTK_WIDGET (text_view));
+  state = gtk_widget_get_state_flags (GTK_WIDGET (text_view));
+
+  gtk_style_context_get_background_color (context, state, &bg_color);
+  gtk_style_context_get_color (context, state, &fg_color);
+
+  values->appearance.bg_color.red = CLAMP (bg_color.red * 65535. + 0.5, 0, 65535);
+  values->appearance.bg_color.green = CLAMP (bg_color.green * 65535. + 0.5, 0, 65535);
+  values->appearance.bg_color.blue = CLAMP (bg_color.blue * 65535. + 0.5, 0, 65535);
+
+  values->appearance.fg_color.red = CLAMP (fg_color.red * 65535. + 0.5, 0, 65535);
+  values->appearance.fg_color.green = CLAMP (fg_color.green * 65535. + 0.5, 0, 65535);
+  values->appearance.fg_color.blue = CLAMP (fg_color.blue * 65535. + 0.5, 0, 65535);
 
   if (values->font)
     pango_font_description_free (values->font);
 
-  values->font = pango_font_description_copy (style->font_desc);
+  gtk_style_context_get (context, state, "font", &values->font, NULL);
 }
 
-GdkPixmap *
+cairo_surface_t *
 _gtk_text_util_create_rich_drag_icon (GtkWidget     *widget,
                                       GtkTextBuffer *buffer,
                                       GtkTextIter   *start,
                                       GtkTextIter   *end)
 {
-  GdkDrawable       *drawable = NULL;
+  GtkAllocation      allocation;
+  cairo_surface_t   *surface;
   gint               pixmap_height, pixmap_width;
   gint               layout_width, layout_height;
+  GtkStyleContext   *context;
+  GtkStateFlags      state;
+  GdkRGBA            color;
   GtkTextBuffer     *new_buffer;
   GtkTextLayout     *layout;
   GtkTextAttributes *style;
   PangoContext      *ltr_context, *rtl_context;
   GtkTextIter        iter;
+  cairo_t           *cr;
 
    g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
    g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), NULL);
    g_return_val_if_fail (start != NULL, NULL);
    g_return_val_if_fail (end != NULL, NULL);
 
+   context = gtk_widget_get_style_context (widget);
+   state = gtk_widget_get_state_flags (widget);
+
    new_buffer = gtk_text_buffer_new (gtk_text_buffer_get_tag_table (buffer));
    gtk_text_buffer_get_start_iter (new_buffer, &iter);
 
@@ -301,13 +350,12 @@ _gtk_text_util_create_rich_drag_icon (GtkWidget     *widget,
 
    style = gtk_text_attributes_new ();
 
-   layout_width = widget->allocation.width;
+   gtk_widget_get_allocation (widget, &allocation);
+   layout_width = allocation.width;
 
    if (GTK_IS_TEXT_VIEW (widget))
      {
-       gtk_widget_ensure_style (widget);
-       gtk_text_view_set_attributes_from_style (GTK_TEXT_VIEW (widget),
-                                                style, widget->style);
+       gtk_text_view_set_attributes_from_style (GTK_TEXT_VIEW (widget), style);
 
        layout_width = layout_width
          - gtk_text_view_get_border_window_size (GTK_TEXT_VIEW (widget), GTK_TEXT_WINDOW_LEFT)
@@ -333,34 +381,41 @@ _gtk_text_util_create_rich_drag_icon (GtkWidget     *widget,
    pixmap_width  = layout_width + DRAG_ICON_LAYOUT_BORDER * 2;
    pixmap_height = layout_height + DRAG_ICON_LAYOUT_BORDER * 2;
 
-   drawable = gdk_pixmap_new (widget->window,
-                              pixmap_width  + 2, pixmap_height + 2, -1);
-
-   gdk_draw_rectangle (drawable,
-                       widget->style->base_gc [GTK_WIDGET_STATE (widget)],
-                       TRUE,
-                       0, 0,
-                       pixmap_width + 1,
-                       pixmap_height + 1);
-
-   gtk_text_layout_draw (layout, widget, drawable,
-                         widget->style->text_gc [GTK_WIDGET_STATE (widget)],
-                         - (1 + DRAG_ICON_LAYOUT_BORDER),
-                         - (1 + DRAG_ICON_LAYOUT_BORDER),
-                         0, 0,
-                         pixmap_width, pixmap_height, NULL);
-
-   gdk_draw_rectangle (drawable,
-                       widget->style->black_gc,
-                       FALSE,
-                       0, 0,
-                       pixmap_width + 1,
-                       pixmap_height + 1);
+   surface = gdk_window_create_similar_surface (gtk_widget_get_window (widget),
+                                                CAIRO_CONTENT_COLOR,
+                                                pixmap_width  + 2,
+                                                pixmap_height + 2);
+
+   cr = cairo_create (surface);
+
+   gtk_style_context_save (context);
+   gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
+
+   gtk_style_context_get_background_color (context, state, &color);
+   gdk_cairo_set_source_rgba (cr, &color);
+   cairo_paint (cr);
+
+   cairo_save (cr);
 
+   cairo_translate (cr, 1 + DRAG_ICON_LAYOUT_BORDER, 1 + DRAG_ICON_LAYOUT_BORDER);
+   gtk_text_layout_draw (layout, widget, cr, NULL);
+
+   cairo_restore (cr);
+
+   cairo_set_source_rgb (cr, 0, 0, 0);
+   cairo_rectangle (cr, 0.5, 0.5, pixmap_width + 1, pixmap_height + 1);
+   cairo_set_line_width (cr, 1.0);
+   cairo_stroke (cr);
+
+   cairo_destroy (cr);
    g_object_unref (layout);
    g_object_unref (new_buffer);
 
-   return drawable;
+   cairo_surface_set_device_offset (surface, 2, 2);
+
+   gtk_style_context_restore (context);
+
+   return surface;
 }
 
 
@@ -383,15 +438,17 @@ layout_get_char_width (PangoLayout *layout)
   return width;
 }
 
-/**
+/*
  * _gtk_text_util_get_block_cursor_location
  * @layout: a #PangoLayout
  * @index: index at which cursor is located
- * @rect: cursor location
+ * @pos: cursor location
+ * @at_line_end: whether cursor is drawn at line end, not over some
+ * character
  *
  * Returns: whether cursor should actually be drawn as a rectangle.
- * It may not be the case if character at index is invisible.
- **/
+ *     It may not be the case if character at index is invisible.
+ */
 gboolean
 _gtk_text_util_get_block_cursor_location (PangoLayout    *layout,
                                          gint            index,
@@ -402,6 +459,7 @@ _gtk_text_util_get_block_cursor_location (PangoLayout    *layout,
   PangoLayoutLine *layout_line;
   gboolean rtl;
   gint line_no;
+  const gchar *text;
 
   g_return_val_if_fail (layout != NULL, FALSE);
   g_return_val_if_fail (index >= 0, FALSE);
@@ -423,23 +481,23 @@ _gtk_text_util_get_block_cursor_location (PangoLayout    *layout,
     }
 
   pango_layout_index_to_line_x (layout, index, FALSE, &line_no, NULL);
-  g_return_val_if_fail (line_no >= 0, FALSE);
   layout_line = pango_layout_get_line_readonly (layout, line_no);
-
-  /* end of layout, get last line */
-  if (!layout_line)
-    {
-      line_no -= 1;
-      layout_line = pango_layout_get_line_readonly (layout, line_no);
-    }
-
   g_return_val_if_fail (layout_line != NULL, FALSE);
 
+  text = pango_layout_get_text (layout);
+
   if (index < layout_line->start_index + layout_line->length)
     {
-      /* cursor points to some zero-width character, do not
-       * bother with block cursor */
-      return FALSE;
+      /* this may be a zero-width character in the middle of the line,
+       * or it could be a character where line is wrapped, we do want
+       * block cursor in latter case */
+      if (g_utf8_next_char (text + index) - text !=
+         layout_line->start_index + layout_line->length)
+       {
+         /* zero-width character in the middle of the line, do not
+          * bother with block cursor */
+         return FALSE;
+       }
     }
 
   /* Cursor is at the line end. It may be an empty line, or it could
@@ -459,23 +517,19 @@ _gtk_text_util_get_block_cursor_location (PangoLayout    *layout,
    * pixel of the layout line, so we need to correct it for RTL text. */
   if (layout_line->length)
     {
-      gint left, right;
-      const gchar *text;
-      const gchar *p;
-
-      text = pango_layout_get_text (layout);
-      p = g_utf8_prev_char (text + index);
-
-      pango_layout_line_index_to_x (layout_line, p - text, FALSE, &left);
-      pango_layout_line_index_to_x (layout_line, p - text, TRUE, &right);
-
-      if (MIN (left, right) <= 0)
+      if (layout_line->resolved_dir == PANGO_DIRECTION_RTL)
        {
-          /* last character is on the left, RTL */
-
          PangoLayoutIter *iter;
          PangoRectangle line_rect;
          gint i;
+         gint left, right;
+         const gchar *p;
+
+         p = g_utf8_prev_char (text + index);
+
+         pango_layout_line_index_to_x (layout_line, p - text, FALSE, &left);
+         pango_layout_line_index_to_x (layout_line, p - text, TRUE, &right);
+         pos->x = MIN (left, right);
 
          iter = pango_layout_get_iter (layout);
          for (i = 0; i < line_no; i++)
@@ -484,7 +538,7 @@ _gtk_text_util_get_block_cursor_location (PangoLayout    *layout,
          pango_layout_iter_free (iter);
 
           rtl = TRUE;
-         pos->x = MIN (left, right) + line_rect.x;
+         pos->x += line_rect.x;
        }
       else
        rtl = FALSE;