<commit_before>/*
 * Copyright 2009 Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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 program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 * Boston, MA 02111-1307, USA.
 *
 */
#include "nbtk-gtk-frame.h"
#include <math.h>

static GdkColor nbtk_gtk_frame_default_border_color = { 0, 0xdddd, 0xe2e2, 0xe5e5 };

static void nbtk_gtk_frame_buildable_init (GtkBuildableIface *iface);
static void nbtk_gtk_frame_buildable_add_child (GtkBuildable *buildable,
                                                GtkBuilder   *builder,
                                                GObject      *child,
                                                const gchar  *type);

G_DEFINE_TYPE_WITH_CODE (NbtkGtkFrame, nbtk_gtk_frame, GTK_TYPE_FRAME,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, nbtk_gtk_frame_buildable_init))

static void
nbtk_gtk_frame_dispose (GObject *object)
{
  G_OBJECT_CLASS (nbtk_gtk_frame_parent_class)->dispose (object);
}

static void
nbtk_gtk_frame_finalize (GObject *object)
{
  G_OBJECT_CLASS (nbtk_gtk_frame_parent_class)->finalize (object);
}

static void
nbtk_gtk_frame_update_style (NbtkGtkFrame *frame)
{
  GdkColor *border_color;
  char *font = NULL;

  gtk_widget_style_get (GTK_WIDGET (frame),
                        "border-color", &border_color,
                        "title-font", &font,
                        NULL);

  if (border_color)
    {
      frame->border_color = *border_color;
      gdk_color_free (border_color);
    }
  else
    {
      frame->border_color = nbtk_gtk_frame_default_border_color;
    }

  if (font)
    {
      if (GTK_FRAME (frame)->label_widget)
        {
          PangoFontDescription *desc;
          desc = pango_font_description_from_string (font);
          gtk_widget_modify_font (GTK_FRAME (frame)->label_widget, desc);
          pango_font_description_free (desc);
        }
      g_free (font);
    }
}

static void
label_changed_cb (NbtkGtkFrame *frame)
{
  char *font = NULL;
  GtkFrame *gtk_frame = GTK_FRAME (frame);

  if (!gtk_frame->label_widget)
    return;

  /* ensure font is correct */
  gtk_widget_style_get (GTK_WIDGET (frame),
                        "title-font", &font,
                        NULL);
  if (font)
    {
      PangoFontDescription *desc;
      desc = pango_font_description_from_string (font);
      gtk_widget_modify_font (gtk_frame->label_widget, desc);
      pango_font_description_free (desc);
      g_free (font);
    }

  gtk_misc_set_alignment (GTK_MISC (gtk_frame->label_widget), 0.0, 1.0);
}
static void
rounded_rectangle (cairo_t * cr,
                   double x, double y, double w, double h,
                   guint radius)
{
  if (radius > w / 2)
    radius = w / 2;
  if (radius > h / 2)
    radius = h / 2;

  cairo_move_to (cr, x + radius, y);
  cairo_arc (cr, x + w - radius, y + radius, radius, M_PI * 1.5, M_PI * 2);
  cairo_arc (cr, x + w - radius, y + h - radius, radius, 0, M_PI * 0.5);
  cairo_arc (cr, x + radius, y + h - radius, radius, M_PI * 0.5, M_PI);
  cairo_arc (cr, x + radius, y + radius, radius, M_PI, M_PI * 1.5);
}

static void
nbtk_gtk_frame_paint (GtkWidget *widget, GdkRectangle *area)
{
  NbtkGtkFrame *frame = NBTK_GTK_FRAME (widget);
  cairo_t *cairo;
  GtkStyle *style;
  guint width;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (NBTK_GTK_IS_FRAME (widget));
  g_return_if_fail (area != NULL);

  style = gtk_widget_get_style (widget);
  cairo = gdk_cairo_create (widget->window);
  width = gtk_container_get_border_width (GTK_CONTAINER (widget));

  /* draw border */
  if (width != 0)
    {
      gdk_cairo_set_source_color (cairo, &frame->border_color);

      rounded_rectangle (cairo,
                         widget->allocation.x,
                         widget->allocation.y,
                         widget->allocation.width,
                         widget->allocation.height,
                         width);
      cairo_clip (cairo);

      gdk_cairo_rectangle (cairo, area);
      cairo_clip (cairo);

      cairo_paint (cairo);
    }

  /* draw background */
  gdk_cairo_set_source_color (cairo, &style->bg[GTK_WIDGET_STATE (widget)]);
  rounded_rectangle (cairo,
                     widget->allocation.x + width,
                     widget->allocation.y + width,
                     widget->allocation.width - 2 * width,
                     widget->allocation.height - 2 * width,
                     width);
  cairo_clip (cairo);

  gdk_cairo_rectangle (cairo, area);
  cairo_clip (cairo);

  cairo_paint (cairo);

  cairo_destroy (cairo);
}

static gboolean
nbtk_gtk_frame_expose (GtkWidget *widget,
                       GdkEventExpose *event)
{
  GtkWidgetClass *grand_parent;

  if (GTK_WIDGET_DRAWABLE (widget))
    {<commit_msg>[NbtkGtkFrame] re-write frame drawing to correct radii<commit_after>/*
 * Copyright 2009 Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope 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 program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 * Boston, MA 02111-1307, USA.
 *
 */
#include "nbtk-gtk-frame.h"
#include <math.h>

static GdkColor nbtk_gtk_frame_default_border_color = { 0, 0xdddd, 0xe2e2, 0xe5e5 };

static void nbtk_gtk_frame_buildable_init (GtkBuildableIface *iface);
static void nbtk_gtk_frame_buildable_add_child (GtkBuildable *buildable,
                                                GtkBuilder   *builder,
                                                GObject      *child,
                                                const gchar  *type);

G_DEFINE_TYPE_WITH_CODE (NbtkGtkFrame, nbtk_gtk_frame, GTK_TYPE_FRAME,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE, nbtk_gtk_frame_buildable_init))

static void
nbtk_gtk_frame_dispose (GObject *object)
{
  G_OBJECT_CLASS (nbtk_gtk_frame_parent_class)->dispose (object);
}

static void
nbtk_gtk_frame_finalize (GObject *object)
{
  G_OBJECT_CLASS (nbtk_gtk_frame_parent_class)->finalize (object);
}

static void
nbtk_gtk_frame_update_style (NbtkGtkFrame *frame)
{
  GdkColor *border_color;
  char *font = NULL;

  gtk_widget_style_get (GTK_WIDGET (frame),
                        "border-color", &border_color,
                        "title-font", &font,
                        NULL);

  if (border_color)
    {
      frame->border_color = *border_color;
      gdk_color_free (border_color);
    }
  else
    {
      frame->border_color = nbtk_gtk_frame_default_border_color;
    }

  if (font)
    {
      if (GTK_FRAME (frame)->label_widget)
        {
          PangoFontDescription *desc;
          desc = pango_font_description_from_string (font);
          gtk_widget_modify_font (GTK_FRAME (frame)->label_widget, desc);
          pango_font_description_free (desc);
        }
      g_free (font);
    }
}

static void
label_changed_cb (NbtkGtkFrame *frame)
{
  char *font = NULL;
  GtkFrame *gtk_frame = GTK_FRAME (frame);

  if (!gtk_frame->label_widget)
    return;

  /* ensure font is correct */
  gtk_widget_style_get (GTK_WIDGET (frame),
                        "title-font", &font,
                        NULL);
  if (font)
    {
      PangoFontDescription *desc;
      desc = pango_font_description_from_string (font);
      gtk_widget_modify_font (gtk_frame->label_widget, desc);
      pango_font_description_free (desc);
      g_free (font);
    }

  gtk_misc_set_alignment (GTK_MISC (gtk_frame->label_widget), 0.0, 1.0);
}
static void
rounded_rectangle (cairo_t * cr,
                   double x, double y, double w, double h,
                   guint radius)
{
  if (radius > w / 2)
    radius = w / 2;
  if (radius > h / 2)
    radius = h / 2;

  cairo_move_to (cr, x + radius, y);
  cairo_arc (cr, x + w - radius, y + radius, radius, M_PI * 1.5, M_PI * 2);
  cairo_arc (cr, x + w - radius, y + h - radius, radius, 0, M_PI * 0.5);
  cairo_arc (cr, x + radius, y + h - radius, radius, M_PI * 0.5, M_PI);
  cairo_arc (cr, x + radius, y + radius, radius, M_PI, M_PI * 1.5);
}

static void
nbtk_gtk_frame_paint (GtkWidget *widget, GdkRectangle *area)
{
  NbtkGtkFrame *frame = NBTK_GTK_FRAME (widget);
  cairo_t *cairo;
  GtkStyle *style;
  guint width;

  g_return_if_fail (widget != NULL);
  g_return_if_fail (NBTK_GTK_IS_FRAME (widget));
  g_return_if_fail (area != NULL);

  style = gtk_widget_get_style (widget);
  cairo = gdk_cairo_create (widget->window);
  width = gtk_container_get_border_width (GTK_CONTAINER (widget));

  /* clip to area */
  gdk_cairo_rectangle (cairo, area);
  cairo_clip (cairo);

  /* initialise the background */
  gdk_cairo_set_source_color (cairo, &style->bg[GTK_WIDGET_STATE (widget)]);
  cairo_rectangle (cairo, widget->allocation.x, widget->allocation.y,
                   widget->allocation.width, widget->allocation.height);
  cairo_fill (cairo);

  /* draw border */
  if (width != 0)
    {
      gdk_cairo_set_source_color (cairo, &frame->border_color);
      cairo_set_line_width (cairo, width);

      rounded_rectangle (cairo,
                         widget->allocation.x + (width / 2),
                         widget->allocation.y + (width / 2),
                         widget->allocation.width - (width),
                         widget->allocation.height - (width),
                         width);

      cairo_stroke (cairo);
    }

  cairo_destroy (cairo);
}

static gboolean
nbtk_gtk_frame_expose (GtkWidget *widget,
                       GdkEventExpose *event)
{
  GtkWidgetClass *grand_parent;

  if (GTK_WIDGET_DRAWABLE (widget))
    {
{'fraction_non_alphanumeric': 0.09634802302538455, 'fraction_numerical': 0.010380296310276493, 'mean_word_length': 2.5551828245555184, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7620626', 'n_tokens_mistral': 3693, 'n_tokens_neox': 3498, 'n_words': 875}