Blame gladeui/glade-cursor.c

Packit 1e8aac
/*
Packit 1e8aac
 * Copyright (C) 2001 Ximian, Inc.
Packit 1e8aac
 *
Packit 1e8aac
 * This program is free software; you can redistribute it and/or modify
Packit 1e8aac
 * it under the terms of the GNU General Public License as
Packit 1e8aac
 * published by the Free Software Foundation; either version 2 of the
Packit 1e8aac
 * License, or (at your option) any later version.
Packit 1e8aac
 *
Packit 1e8aac
 * This program is distributed in the hope that it will be useful,
Packit 1e8aac
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 1e8aac
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 1e8aac
 * GNU General Public License for more details.
Packit 1e8aac
 *
Packit 1e8aac
 * You should have received a copy of the GNU General Public License
Packit 1e8aac
 * along with this program; if not, write to the Free Software
Packit 1e8aac
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 1e8aac
 *
Packit 1e8aac
 * Authors:
Packit 1e8aac
 *   Chema Celorio <chema@celorio.com>
Packit 1e8aac
 */
Packit 1e8aac
Packit 1e8aac
#include "config.h"
Packit 1e8aac
Packit 1e8aac
#include "glade-app.h"
Packit 1e8aac
#include "glade-palette.h"
Packit 1e8aac
#include "glade-cursor.h"
Packit 1e8aac
#include "glade-widget-adaptor.h"
Packit 1e8aac
Packit 1e8aac
#include <glib.h>
Packit 1e8aac
#include <glib/gi18n.h>
Packit 1e8aac
Packit 1e8aac
#define ADD_PIXBUF_FILENAME "plus.png"
Packit 1e8aac
Packit 1e8aac
static GladeCursor *cursor = NULL;
Packit 1e8aac
Packit 1e8aac
static void
Packit 1e8aac
set_cursor_recurse (GtkWidget * widget, GdkCursor * gdk_cursor)
Packit 1e8aac
{
Packit 1e8aac
  GList *children, *list;
Packit 1e8aac
Packit 1e8aac
  if (!gtk_widget_get_visible (widget) || !gtk_widget_get_realized (widget))
Packit 1e8aac
    return;
Packit 1e8aac
Packit 1e8aac
  gdk_window_set_cursor (gtk_widget_get_window (widget), gdk_cursor);
Packit 1e8aac
Packit 1e8aac
  if (GTK_IS_CONTAINER (widget) &&
Packit 1e8aac
      (children =
Packit 1e8aac
       glade_util_container_get_all_children (GTK_CONTAINER (widget))) != NULL)
Packit 1e8aac
    {
Packit 1e8aac
      for (list = children; list; list = list->next)
Packit 1e8aac
        {
Packit 1e8aac
          set_cursor_recurse (GTK_WIDGET (list->data), gdk_cursor);
Packit 1e8aac
        }
Packit 1e8aac
      g_list_free (children);
Packit 1e8aac
    }
Packit 1e8aac
}
Packit 1e8aac
Packit 1e8aac
static void
Packit 1e8aac
set_cursor (GladeProject *project,
Packit 1e8aac
	    GdkCursor    *gdk_cursor)
Packit 1e8aac
{
Packit 1e8aac
  GList *list;
Packit 1e8aac
Packit 1e8aac
  for (list = (GList *) glade_project_get_objects (project);
Packit 1e8aac
       list; list = list->next)
Packit 1e8aac
    {
Packit 1e8aac
      GObject *object = list->data;
Packit 1e8aac
Packit 1e8aac
      if (GTK_IS_WIDGET (object) &&
Packit 1e8aac
	  gtk_widget_get_has_window (GTK_WIDGET (object)))
Packit 1e8aac
	{
Packit 1e8aac
	  set_cursor_recurse (GTK_WIDGET (object), gdk_cursor);
Packit 1e8aac
	}
Packit 1e8aac
    }
Packit 1e8aac
}
Packit 1e8aac
Packit 1e8aac
/**
Packit 1e8aac
 * glade_cursor_set:
Packit 1e8aac
 * @window: a #GdkWindow
Packit 1e8aac
 * @type: a #GladeCursorType
Packit 1e8aac
 *
Packit 1e8aac
 * Sets the cursor for @window to something appropriate based on @type.
Packit 1e8aac
 * (also sets the cursor on all visible project widgets)
Packit 1e8aac
 */
Packit 1e8aac
void
Packit 1e8aac
glade_cursor_set (GladeProject    *project,
Packit 1e8aac
		  GdkWindow       *window, 
Packit 1e8aac
		  GladeCursorType  type)
Packit 1e8aac
{
Packit 1e8aac
  GladeWidgetAdaptor *adaptor;
Packit 1e8aac
  GdkCursor *the_cursor = NULL;
Packit 1e8aac
  g_return_if_fail (cursor != NULL);
Packit 1e8aac
Packit 1e8aac
  switch (type)
Packit 1e8aac
    {
Packit 1e8aac
      case GLADE_CURSOR_SELECTOR:
Packit 1e8aac
        the_cursor = cursor->selector;
Packit 1e8aac
        break;
Packit 1e8aac
      case GLADE_CURSOR_ADD_WIDGET:
Packit 1e8aac
        if ((adaptor =
Packit 1e8aac
             glade_project_get_add_item (project)) != NULL)
Packit 1e8aac
          {
Packit 1e8aac
            g_object_get (adaptor, "cursor", &the_cursor, NULL);
Packit 1e8aac
Packit 1e8aac
            if (the_cursor == NULL)
Packit 1e8aac
              the_cursor = cursor->add_widget;
Packit 1e8aac
          }
Packit 1e8aac
        else
Packit 1e8aac
          the_cursor = cursor->add_widget;
Packit 1e8aac
        break;
Packit 1e8aac
      case GLADE_CURSOR_RESIZE_TOP_LEFT:
Packit 1e8aac
        the_cursor = cursor->resize_top_left;
Packit 1e8aac
        break;
Packit 1e8aac
      case GLADE_CURSOR_RESIZE_TOP_RIGHT:
Packit 1e8aac
        the_cursor = cursor->resize_top_right;
Packit 1e8aac
        break;
Packit 1e8aac
      case GLADE_CURSOR_RESIZE_BOTTOM_LEFT:
Packit 1e8aac
        the_cursor = cursor->resize_bottom_left;
Packit 1e8aac
        break;
Packit 1e8aac
      case GLADE_CURSOR_RESIZE_BOTTOM_RIGHT:
Packit 1e8aac
        the_cursor = cursor->resize_bottom_right;
Packit 1e8aac
        break;
Packit 1e8aac
      case GLADE_CURSOR_RESIZE_LEFT:
Packit 1e8aac
        the_cursor = cursor->resize_left;
Packit 1e8aac
        break;
Packit 1e8aac
      case GLADE_CURSOR_RESIZE_RIGHT:
Packit 1e8aac
        the_cursor = cursor->resize_right;
Packit 1e8aac
        break;
Packit 1e8aac
      case GLADE_CURSOR_RESIZE_TOP:
Packit 1e8aac
        the_cursor = cursor->resize_top;
Packit 1e8aac
        break;
Packit 1e8aac
      case GLADE_CURSOR_RESIZE_BOTTOM:
Packit 1e8aac
        the_cursor = cursor->resize_bottom;
Packit 1e8aac
        break;
Packit 1e8aac
      case GLADE_CURSOR_DRAG:
Packit 1e8aac
        the_cursor = cursor->drag;
Packit 1e8aac
        break;
Packit 1e8aac
      default:
Packit 1e8aac
        break;
Packit 1e8aac
    }
Packit 1e8aac
Packit 1e8aac
  if (the_cursor != gdk_window_get_cursor (window))
Packit 1e8aac
    {
Packit 1e8aac
      set_cursor (project, cursor->selector);
Packit 1e8aac
      gdk_window_set_cursor (window, the_cursor);
Packit 1e8aac
    }
Packit 1e8aac
}
Packit 1e8aac
Packit 1e8aac
/**
Packit 1e8aac
 * glade_cursor_init:
Packit 1e8aac
 *
Packit 1e8aac
 * Initializes cursors for use with glade_cursor_set().
Packit 1e8aac
 */
Packit 1e8aac
void
Packit 1e8aac
glade_cursor_init (void)
Packit 1e8aac
{
Packit 1e8aac
  gchar *path;
Packit 1e8aac
  GError *error = NULL;
Packit 1e8aac
  GdkDisplay *display;
Packit 1e8aac
Packit 1e8aac
  cursor = g_new0 (GladeCursor, 1);
Packit 1e8aac
  display = gdk_display_get_default ();
Packit 1e8aac
Packit 1e8aac
  cursor->selector = NULL;
Packit 1e8aac
  cursor->add_widget = gdk_cursor_new_from_name (display, "crosshair");
Packit 1e8aac
  cursor->resize_top_left = gdk_cursor_new_from_name (display, "nw-resize");
Packit 1e8aac
  cursor->resize_top_right = gdk_cursor_new_from_name (display, "ne-resize");
Packit 1e8aac
  cursor->resize_bottom_left = gdk_cursor_new_from_name (display, "sw-resize");
Packit 1e8aac
  cursor->resize_bottom_right = gdk_cursor_new_from_name (display, "se-resize");
Packit 1e8aac
  cursor->resize_left = gdk_cursor_new_from_name (display, "w-resize");
Packit 1e8aac
  cursor->resize_right = gdk_cursor_new_from_name (display, "e-resize");
Packit 1e8aac
  cursor->resize_top = gdk_cursor_new_from_name (display, "n-resize");
Packit 1e8aac
  cursor->resize_bottom = gdk_cursor_new_from_name (display, "s-resize");
Packit 1e8aac
  cursor->drag = gdk_cursor_new_from_name (display, "move");
Packit 1e8aac
  cursor->add_widget_pixbuf = NULL;
Packit 1e8aac
Packit 1e8aac
  /* load "add" cursor pixbuf */
Packit 1e8aac
  path =
Packit 1e8aac
      g_build_filename (glade_app_get_pixmaps_dir (), ADD_PIXBUF_FILENAME,
Packit 1e8aac
                        NULL);
Packit 1e8aac
Packit 1e8aac
  cursor->add_widget_pixbuf = gdk_pixbuf_new_from_file (path, &error);
Packit 1e8aac
Packit 1e8aac
  if (cursor->add_widget_pixbuf == NULL)
Packit 1e8aac
    {
Packit 1e8aac
      g_critical (_("Unable to load image (%s)"), error->message);
Packit 1e8aac
Packit 1e8aac
      g_error_free (error);
Packit 1e8aac
      error = NULL;
Packit 1e8aac
    }
Packit 1e8aac
  g_free (path);
Packit 1e8aac
}
Packit 1e8aac
Packit 1e8aac
const GdkPixbuf *
Packit 1e8aac
glade_cursor_get_add_widget_pixbuf (void)
Packit 1e8aac
{
Packit 1e8aac
  g_return_val_if_fail (cursor != NULL, NULL);
Packit 1e8aac
Packit 1e8aac
  return cursor->add_widget_pixbuf;
Packit 1e8aac
}