Пример #1
0
static void
test_parse_text (void)
{
  GPtrArray *items;

  static const gchar *text = "line-background=\"rgba(235,202,210,.4)\"\n"
                             "foreground=\"rgba(100%, 50%, 25%,.4)\"\n"
                             "color: #8d9091;\n"
                             "color: #123;\n"
                             "background-color: hsl(65, 70%, 72%);\n"
                             "text-shadow: 0 1px black;";

  printf ("\n");
  items = gstyle_color_parse (text);
  for (gint i = 0; i < items->len; ++i)
    {
      gchar *str;
      const GstyleColor *color;
      GstyleColorItem *item;
      guint start, len;

      item = g_ptr_array_index (items, i);
      color = gstyle_color_item_get_color (item);
      start = gstyle_color_item_get_start (item);
      len = gstyle_color_item_get_len (item);
      str = gstyle_color_to_string ((GstyleColor *)color, GSTYLE_COLOR_KIND_ORIGINAL);

      printf ("item(%i,%i): '%s'\n", start, len, str);
      g_free (str);
    }

  g_ptr_array_free (items, TRUE);
}
static void
gb_color_picker_document_monitor_colorize (GbColorPickerDocumentMonitor *self,
                                           GtkTextBuffer                *buffer,
                                           GtkTextIter                  *begin,
                                           GtkTextIter                  *end)
{
  g_autofree gchar *text = NULL;
  g_autoptr(GPtrArray) items = NULL;
  GstyleColorItem *item;
  GtkTextTag *tag;
  GtkTextIter real_begin;
  GtkTextIter real_end;
  GtkTextIter tag_begin;
  GtkTextIter tag_end;
  gint offset;
  gint len;
  gint pos;

  g_return_if_fail (GB_IS_COLOR_PICKER_DOCUMENT_MONITOR (self));
  g_return_if_fail (GTK_IS_TEXT_BUFFER (buffer));

  if (begin == NULL)
    gtk_text_buffer_get_start_iter (GTK_TEXT_BUFFER (buffer), &real_begin);
  else
    real_begin = *begin;

  if (end == NULL)
    gtk_text_buffer_get_end_iter (GTK_TEXT_BUFFER (buffer), &real_end);
  else
    real_end = *end;

  if (gtk_text_iter_equal (&real_begin, &real_end))
    return;

  offset = gtk_text_iter_get_offset (&real_begin);
  text = gtk_text_buffer_get_slice (GTK_TEXT_BUFFER (buffer), &real_begin, &real_end, TRUE);

  items = gstyle_color_parse (text);
  for (guint n = 0; n < items->len; ++n)
    {
      GstyleColor *color;

      item = g_ptr_array_index (items, n);
      pos = offset + gstyle_color_item_get_start (item);
      gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (buffer), &tag_begin, pos);
      len = gstyle_color_item_get_len (item);
      gtk_text_buffer_get_iter_at_offset (GTK_TEXT_BUFFER (buffer), &tag_end, pos + len);
      color = (GstyleColor *)gstyle_color_item_get_color (item);

      tag = gb_color_picker_helper_create_color_tag (GTK_TEXT_BUFFER (buffer), color);
      gtk_text_buffer_apply_tag (GTK_TEXT_BUFFER (buffer), tag, &tag_begin, &tag_end);
      /* FIXME: is the tag added to the tag table or should we handle a hash table/tag table ourself ? */
    }
}