static void
update_background_color (GtkSourceMarkAttributes *attrs, GtkSourceBuffer *buffer)
{
	GtkSourceStyleScheme *scheme;
	GtkSourceStyle *style;

	scheme = gtk_source_buffer_get_style_scheme (buffer);
	style = gtk_source_style_scheme_get_style (scheme, "search-match");

	if (style)
	{
		gboolean bgset;
		gchar *bg;

		g_object_get (style, "background-set", &bgset, "background", &bg, NULL);

		if (bgset)
		{
			GdkRGBA color;
			gdk_rgba_parse (&color, bg);
			gtk_source_mark_attributes_set_background (attrs, &color);
			g_free (bg);

			return;
		}
	}

	gtk_source_mark_attributes_set_background (attrs, NULL);
}
static void
update_background_color (GeditView *view)
{
	GtkSourceView *source_view = GTK_SOURCE_VIEW (view);
	GtkSourceStyle *style;
	GtkSourceStyleScheme *scheme;
	GtkTextBuffer *buffer;
	
	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));
	
	scheme = gtk_source_buffer_get_style_scheme (GTK_SOURCE_BUFFER (buffer));
	style = gtk_source_style_scheme_get_style (scheme, "search-match");
	
	if (style)
	{
		gboolean bgset;
		gchar *bg;
		
		g_object_get (style, "background-set", &bgset, "background", &bg, NULL);
		
		if (bgset)
		{
			GdkColor color;
			gdk_color_parse (bg, &color);
			gtk_source_view_set_mark_category_background (source_view,
								      BOOKMARK_CATEGORY,
								      &color);
			g_free (bg);
			
			return;
		}
	}
	
	gtk_source_view_set_mark_category_background (source_view,
						      BOOKMARK_CATEGORY,
						      NULL);
}
static gboolean
get_style_rgba (GtkSourceStyleScheme *scheme,
                const gchar          *style_name,
                int                   type,
                GdkRGBA              *rgba)
{
  GtkSourceStyle *style;

  g_assert (!scheme || GTK_SOURCE_IS_STYLE_SCHEME (scheme));
  g_assert (style_name != NULL);
  g_assert (type == FOREGROUND || type == BACKGROUND);
  g_assert (rgba != NULL);

  memset (rgba, 0, sizeof *rgba);

  if (scheme == NULL)
    return FALSE;

  if (NULL != (style = gtk_source_style_scheme_get_style (scheme, style_name)))
    {
      g_autofree gchar *str = NULL;
      gboolean set = FALSE;

      g_object_get (style,
                    type ? "background" : "foreground", &str,
                    type ? "background-set" : "foreground-set", &set,
                    NULL);

      if (str != NULL)
        gdk_rgba_parse (rgba, str);

      return set;
    }

  return FALSE;
}
Ejemplo n.º 4
0
gboolean
ide_source_style_scheme_apply_style (GtkSourceStyleScheme *style_scheme,
                                     const gchar          *style_name,
                                     GtkTextTag           *tag)
{
  g_autofree gchar *foreground = NULL;
  g_autofree gchar *background = NULL;
  g_autofree gchar *underline_color = NULL;
  GdkRGBA underline_rgba;
  GtkSourceStyle *style;
  const gchar *colon;
  PangoUnderline pango_underline;
  gboolean foreground_set = FALSE;
  gboolean background_set = FALSE;
  gboolean bold = FALSE;
  gboolean bold_set = FALSE;
  gboolean underline_set = FALSE;
  gboolean underline_color_set = FALSE;
  gboolean italic = FALSE;
  gboolean italic_set = FALSE;

  g_return_val_if_fail (GTK_SOURCE_IS_STYLE_SCHEME (style_scheme), FALSE);
  g_return_val_if_fail (style_name != NULL, FALSE);

  g_object_set (tag,
                "foreground-set", FALSE,
                "background-set", FALSE,
                "weight-set", FALSE,
                "underline-set", FALSE,
                "underline-rgba-set", FALSE,
                "style-set", FALSE,
                NULL);

  style = gtk_source_style_scheme_get_style (style_scheme, style_name);

  if (style == NULL && (colon = strchr (style_name, ':')))
    {
      gchar defname[64];

      g_snprintf (defname, sizeof defname, "def%s", colon);

      style = gtk_source_style_scheme_get_style (style_scheme, defname);

      if (style == NULL)
        return FALSE;
    }

  g_object_get (style,
                "background", &background,
                "background-set", &background_set,
                "foreground", &foreground,
                "foreground-set", &foreground_set,
                "bold", &bold,
                "bold-set", &bold_set,
                "pango-underline", &pango_underline,
                "underline-set", &underline_set,
                "underline-color", &underline_color,
                "underline-color-set", &underline_color_set,
                "italic", &italic,
                "italic-set", &italic_set,
                NULL);

  if (background_set)
    g_object_set (tag, "background", background, NULL);

  if (foreground_set)
    g_object_set (tag, "foreground", foreground, NULL);

  if (bold_set && bold)
    g_object_set (tag, "weight", PANGO_WEIGHT_BOLD, NULL);

  if (italic_set && italic)
    g_object_set (tag, "style", PANGO_STYLE_ITALIC, NULL);

  if (underline_set)
    g_object_set (tag, "underline", pango_underline, NULL);

  if (underline_color_set && underline_color != NULL)
    {
      gdk_rgba_parse (&underline_rgba, underline_color);
      g_object_set (tag,
                    "underline-rgba", &underline_rgba,
                    NULL);
    }
  return TRUE;
}
Ejemplo n.º 5
0
static void
gtk_source_map_rebuild_css (GtkSourceMap *map)
{
	GtkSourceMapPrivate *priv;
	GtkSourceStyleScheme *style_scheme;
	GtkSourceStyle *style = NULL;
	GtkTextBuffer *buffer;
	GString *gstr;
	gboolean alter_alpha = TRUE;
	gchar *background = NULL;

	priv = gtk_source_map_get_instance_private (map);

	if (priv->view == NULL)
	{
		return;
	}

	/*
	 * This is where we calculate the CSS that maps the font for the
	 * minimap as well as the styling for the scrubber.
	 *
	 * The font is calculated from #GtkSourceMap:font-desc. We convert this
	 * to CSS using _gtk_source_pango_font_description_to_css(). It gets
	 * applied to the minimap widget via the CSS style provider which we
	 * attach to the child widget in gtk_source_map_init().
	 *
	 * The rules for calculating the style for the scrubber are as follows.
	 *
	 * If the current style scheme provides a background color for the
	 * scrubber using the "map-overlay" style name, we use that without
	 * any transformations.
	 *
	 * If the style scheme contains a "selection" style scheme, used for
	 * selected text, we use that with a 0.75 alpha value.
	 *
	 * If none of these are met, we take the background from the
	 * #GtkStyleContext using the deprecated
	 * gtk_style_context_get_background_color(). This is non-ideal, but
	 * currently required since we cannot indicate that we want to
	 * alter the alpha for gtk_render_background().
	 */

	gstr = g_string_new (NULL);

	/* Calculate the font if one has been set */
	if (priv->font_desc != NULL)
	{
		gchar *css;

		css = _gtk_source_pango_font_description_to_css (priv->font_desc);
		g_string_append_printf (gstr, "GtkSourceView { %s }\n", css != NULL ? css : "");
		g_free (css);
	}

	buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->view));
	style_scheme = gtk_source_buffer_get_style_scheme (GTK_SOURCE_BUFFER (buffer));

	if (style_scheme != NULL)
	{

		style = gtk_source_style_scheme_get_style (style_scheme, "map-overlay");

		if (style != NULL)
		{
			/* styling is taking as is only if we found a "map-overlay". */
			alter_alpha = FALSE;
		}
		else
		{
			style = gtk_source_style_scheme_get_style (style_scheme, "selection");
		}
	}

	if (style != NULL)
	{
		g_object_get (style,
		              "background", &background,
		              NULL);
	}

	if (background == NULL)
	{
		GtkStyleContext *context;
		GdkRGBA color;

		/*
		 * We failed to locate a style for both "map-overlay" and for
		 * "selection". That means we need to fallback to using the
		 * selected color for the gtk+ theme. This uses deprecated
		 * API because we have no way to tell gtk_render_background()
		 * to render with an alpha.
		 */

		context = gtk_widget_get_style_context (GTK_WIDGET (priv->view));
		gtk_style_context_save (context);
		gtk_style_context_add_class (context, "view");
		G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
		gtk_style_context_get_background_color (context, GTK_STATE_FLAG_SELECTED, &color);
		G_GNUC_END_IGNORE_DEPRECATIONS;
		gtk_style_context_restore (context);
		background = gdk_rgba_to_string (&color);

		/*
		 * Make sure we alter the alpha. It is possible this could be
		 * FALSE here if we found a style for map-overlay but it did
		 * not contain a background color.
		 */
		alter_alpha = TRUE;
	}

	if (alter_alpha)
	{
		GdkRGBA color;

		gdk_rgba_parse (&color, background);
		color.alpha = 0.75;
		g_free (background);
		background = gdk_rgba_to_string (&color);
	}


	if (background != NULL)
	{
		g_string_append_printf (gstr,
		                        "GtkSourceMap.scrubber {\n"
		                        "\tbackground-color: %s;\n"
		                        "\tborder-top: 1px solid shade(%s,0.9);\n"
		                        "\tborder-bottom: 1px solid shade(%s,0.9);\n"
		                        "}\n",
		                        background,
		                        background,
		                        background);
	}

	g_free (background);

	if (gstr->len > 0)
	{
		gtk_css_provider_load_from_data (priv->css_provider, gstr->str, gstr->len, NULL);
	}

	g_string_free (gstr, TRUE);
}
static void
sync_tag_style (GtkSourceStyleScheme *style_scheme,
                GtkTextTag           *tag)
{
    g_autofree gchar *foreground = NULL;
    g_autofree gchar *background = NULL;
    g_autofree gchar *tag_name = NULL;
    gchar *style_name = NULL;
    const gchar *colon;
    GtkSourceStyle *style;
    gboolean foreground_set = FALSE;
    gboolean background_set = FALSE;
    gboolean bold = FALSE;
    gboolean bold_set = FALSE;
    gboolean underline = FALSE;
    gboolean underline_set = FALSE;
    gboolean italic = FALSE;
    gboolean italic_set = FALSE;
    gsize tag_name_len;
    gsize prefix_len;

    g_object_set (tag,
                  "foreground-set", FALSE,
                  "background-set", FALSE,
                  "weight-set", FALSE,
                  "underline-set", FALSE,
                  "style-set", FALSE,
                  NULL);

    g_object_get (tag, "name", &tag_name, NULL);

    if (tag_name == NULL || style_scheme == NULL)
        return;

    prefix_len = strlen (PRIVATE_TAG_PREFIX);
    tag_name_len = strlen (tag_name);
    style_name = tag_name;

    /*
     * Check if this is a private tag.A tag is private if it starts with
     * PRIVATE_TAG_PREFIX "gb-private-tag".
     * ex: gb-private-tag:c:boolean
     * If the tag is private extract the original style name by moving the string
     * strlen (PRIVATE_TAG_PREFIX) + 1 (the colon) characters.
     */
    if (tag_name_len > prefix_len && memcmp (tag_name, PRIVATE_TAG_PREFIX, prefix_len) == 0)
        style_name = tag_name + prefix_len + 1;

    style = gtk_source_style_scheme_get_style (style_scheme, style_name);
    if (style == NULL && (colon = strchr (style_name, ':')))
    {
        gchar defname[64];
        g_snprintf (defname, sizeof defname, "def%s", colon);
        style = gtk_source_style_scheme_get_style (style_scheme, defname);
        if (style == NULL)
            return;
    }

    g_object_get (style,
                  "background", &background,
                  "background-set", &background_set,
                  "foreground", &foreground,
                  "foreground-set", &foreground_set,
                  "bold", &bold,
                  "bold-set", &bold_set,
                  "underline", &underline,
                  "underline-set", &underline_set,
                  "italic", &italic,
                  "italic-set", &italic_set,
                  NULL);

    if (background_set)
        g_object_set (tag, "background", background, NULL);

    if (foreground_set)
        g_object_set (tag, "foreground", foreground, NULL);

    if (bold_set && bold)
        g_object_set (tag, "weight", PANGO_WEIGHT_BOLD, NULL);

    if (italic_set && italic)
        g_object_set (tag, "style", PANGO_STYLE_ITALIC, NULL);

    if (underline_set && underline)
        g_object_set (tag, "underline", PANGO_UNDERLINE_SINGLE, NULL);
}