<commit_before>/* GAIL - The GNOME Accessibility Implementation Library
 * Copyright 2001 Sun Microsystems Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that 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 library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <string.h>
#include <gtk/gtk.h>
#include "gailimage.h"

static void      gail_image_class_init         (GailImageClass *klass);
static void      gail_image_init               (GailImage      *image);
static void      gail_image_initialize         (AtkObject       *accessible,
                                                gpointer        data);
static G_CONST_RETURN gchar* gail_image_get_name  (AtkObject     *accessible);


static void      atk_image_interface_init      (AtkImageIface  *iface);

static G_CONST_RETURN gchar *
                 gail_image_get_image_description (AtkImage     *image);
static void	 gail_image_get_image_position    (AtkImage     *image,
                                                   gint         *x,
                                                   gint         *y,
                                                   AtkCoordType coord_type);
static void      gail_image_get_image_size     (AtkImage        *image,
                                                gint            *width,
                                                gint            *height);
static gboolean  gail_image_set_image_description (AtkImage     *image,
                                                const gchar     *description);
static void      gail_image_finalize           (GObject         *object);

G_DEFINE_TYPE_WITH_CODE (GailImage, gail_image, GAIL_TYPE_WIDGET,
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_IMAGE, atk_image_interface_init))

static void
gail_image_class_init (GailImageClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  AtkObjectClass  *class = ATK_OBJECT_CLASS (klass);

  gobject_class->finalize = gail_image_finalize;
  class->initialize = gail_image_initialize;
  class->get_name = gail_image_get_name;
}

static void
gail_image_init (GailImage *image)
{
  image->image_description = NULL;
}

static void
gail_image_initialize (AtkObject *accessible,
                       gpointer data)
{
  ATK_OBJECT_CLASS (gail_image_parent_class)->initialize (accessible, data);

  accessible->role = ATK_ROLE_ICON;
}

/* Copied from gtktoolbar.c, keep in sync */
static gchar *
elide_underscores (const gchar *original)
{
  gchar *q, *result;
  const gchar *p, *end;
  gsize len;
  gboolean last_underscore;
  
  if (!original)
    return NULL;

  len = strlen (original);
  q = result = g_malloc (len + 1);
  last_underscore = FALSE;
  
  end = original + len;
  for (p = original; p < end; p++)
    {
      if (!last_underscore && *p == '_')
	last_underscore = TRUE;
      else
	{
	  last_underscore = FALSE;
	  if (original + 2 <= p && p + 1 <= end && 
              p[-2] == '(' && p[-1] == '_' && p[0] != '_' && p[1] == ')')
	    {
	      q--;
	      *q = '\0';
	      p++;
	    }
	  else
	    *q++ = *p;
	}
    }

  if (last_underscore)
    *q++ = '_';
  
  *q = '\0';
  
  return result;
}

static G_CONST_RETURN gchar*
gail_image_get_name (AtkObject *accessible)
{
  GtkWidget* widget;
  GtkImage *image;
  GailImage *image_accessible;
  GtkStockItem stock_item;
  gchar *stock_id;
  const gchar *name;

  name = ATK_OBJECT_CLASS (gail_image_parent_class)->get_name (accessible);
  if (name)
    return name;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
  /*
   * State is defunct
   */
  if (widget == NULL)
    return NULL;

  g_return_val_if_fail (GTK_IS_IMAGE (widget), NULL);
  image = GTK_IMAGE (widget);
  image_accessible = GAIL_IMAGE (accessible);

  g_free (image_accessible->stock_name);
  image_accessible->stock_name = NULL;

  gtk_image_get_stock (image, &stock_id, NULL);
  if (gtk_image_get_storage_type (image) != GTK_IMAGE_STOCK ||
      stock_id == NULL)
    return NULL;

  if (!gtk_stock_lookup (stock_id, &stock_item))
    return NULL;

  image_accessible->stock_name = elide_underscores (stock_item.label);
  return image_accessible->stock_name;
}

static void
atk_image_interface_init (AtkImageIface *iface)
{
  iface->get_image_description = gail_image_get_image_description;
  iface->get_image_position = gail_image_get_image_position;
  iface->get_image_size = gail_image_get_image_size;
  iface->set_image_description = gail_image_set_image_description;
}

static G_CONST_RETURN gchar * 
gail_image_get_image_description (AtkImage     *image)
{
  GailImage* aimage = GAIL_IMAGE (image);

  return aimage->image_description;
}

static void<commit_msg>gail: don't try to get stock id from images that aren't stock<commit_after>/* GAIL - The GNOME Accessibility Implementation Library
 * Copyright 2001 Sun Microsystems Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that 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 library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <string.h>
#include <gtk/gtk.h>
#include "gailimage.h"

static void      gail_image_class_init         (GailImageClass *klass);
static void      gail_image_init               (GailImage      *image);
static void      gail_image_initialize         (AtkObject       *accessible,
                                                gpointer        data);
static G_CONST_RETURN gchar* gail_image_get_name  (AtkObject     *accessible);


static void      atk_image_interface_init      (AtkImageIface  *iface);

static G_CONST_RETURN gchar *
                 gail_image_get_image_description (AtkImage     *image);
static void	 gail_image_get_image_position    (AtkImage     *image,
                                                   gint         *x,
                                                   gint         *y,
                                                   AtkCoordType coord_type);
static void      gail_image_get_image_size     (AtkImage        *image,
                                                gint            *width,
                                                gint            *height);
static gboolean  gail_image_set_image_description (AtkImage     *image,
                                                const gchar     *description);
static void      gail_image_finalize           (GObject         *object);

G_DEFINE_TYPE_WITH_CODE (GailImage, gail_image, GAIL_TYPE_WIDGET,
                         G_IMPLEMENT_INTERFACE (ATK_TYPE_IMAGE, atk_image_interface_init))

static void
gail_image_class_init (GailImageClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  AtkObjectClass  *class = ATK_OBJECT_CLASS (klass);

  gobject_class->finalize = gail_image_finalize;
  class->initialize = gail_image_initialize;
  class->get_name = gail_image_get_name;
}

static void
gail_image_init (GailImage *image)
{
  image->image_description = NULL;
}

static void
gail_image_initialize (AtkObject *accessible,
                       gpointer data)
{
  ATK_OBJECT_CLASS (gail_image_parent_class)->initialize (accessible, data);

  accessible->role = ATK_ROLE_ICON;
}

/* Copied from gtktoolbar.c, keep in sync */
static gchar *
elide_underscores (const gchar *original)
{
  gchar *q, *result;
  const gchar *p, *end;
  gsize len;
  gboolean last_underscore;
  
  if (!original)
    return NULL;

  len = strlen (original);
  q = result = g_malloc (len + 1);
  last_underscore = FALSE;
  
  end = original + len;
  for (p = original; p < end; p++)
    {
      if (!last_underscore && *p == '_')
	last_underscore = TRUE;
      else
	{
	  last_underscore = FALSE;
	  if (original + 2 <= p && p + 1 <= end && 
              p[-2] == '(' && p[-1] == '_' && p[0] != '_' && p[1] == ')')
	    {
	      q--;
	      *q = '\0';
	      p++;
	    }
	  else
	    *q++ = *p;
	}
    }

  if (last_underscore)
    *q++ = '_';
  
  *q = '\0';
  
  return result;
}

static G_CONST_RETURN gchar*
gail_image_get_name (AtkObject *accessible)
{
  GtkWidget* widget;
  GtkImage *image;
  GailImage *image_accessible;
  GtkStockItem stock_item;
  gchar *stock_id;
  const gchar *name;

  name = ATK_OBJECT_CLASS (gail_image_parent_class)->get_name (accessible);
  if (name)
    return name;

  widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
  /*
   * State is defunct
   */
  if (widget == NULL)
    return NULL;

  g_return_val_if_fail (GTK_IS_IMAGE (widget), NULL);
  image = GTK_IMAGE (widget);
  image_accessible = GAIL_IMAGE (accessible);

  g_free (image_accessible->stock_name);
  image_accessible->stock_name = NULL;

  if (gtk_image_get_storage_type (image) != GTK_IMAGE_STOCK)
    return NULL;

  gtk_image_get_stock (image, &stock_id, NULL);
  if (stock_id == NULL)
    return NULL;

  if (!gtk_stock_lookup (stock_id, &stock_item))
    return NULL;

  image_accessible->stock_name = elide_underscores (stock_item.label);
  return image_accessible->stock_name;
}

static void
atk_image_interface_init (AtkImageIface *iface)
{
  iface->get_image_description = gail_image_get_image_description;
  iface->get_image_position = gail_image_get_image_position;
  iface->get_image_size = gail_image_get_image_size;
  iface->set_image_description = gail_image_set_image_description;
}

static G_CONST_RETURN gchar * 
gail_image_get_image_description (AtkImage     *image)
{
  GailImage* aimage = GAIL_IMAGE (image);

  return aimage->image_description;
}

static void
{'fraction_non_alphanumeric': 0.08813559322033898, 'fraction_numerical': 0.005273069679849341, 'mean_word_length': 2.6561101549053356, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1338694', 'n_tokens_mistral': 3440, 'n_tokens_neox': 3290, 'n_words': 875}