/* Gdk - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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 <netinet/in.h>
#include "gdk.h"
#include "config.h"
#include "gdkinput.h"
#include "gdkprivate.h"

#if HAVE_CONFIG_H
#  include <config.h>
#  if STDC_HEADERS
#    include <stdlib.h>
#    include <stdio.h>
#    include <string.h>
#  endif
#else
#  include <stdlib.h>
#  include <stdio.h>
#endif

static void window_update (GdkWindowPrivate *win);
static void gdk_window_clear_area_with (GdkWindow *window, gint x, gint y, gint width, gint height, char c);
static gchar *gdk_window_type (GdkWindowPrivate *win);


extern int gdk_screen_dirty;

void
gdk_window_init (void)
{
  static char zeros[4096] = { 0, 0, };
  gdk_root_parent.x = gdk_root_parent.y = 0;
  gdk_root_parent.width = gdk_screen_width() -FX-FX;
  gdk_root_parent.height = gdk_screen_height() -FY-FY;
  gdk_root_parent.visible = 1;
  gdk_root_parent.fill = '.';
  ((GdkVisualPrivate *) system_visual)->xvisual = &zeros;
  window_update(&gdk_root_parent);
}

static void
window_position (GdkWindowPrivate *win, int *x, int *y)
{
  static GdkWindowPrivate *parent;

  *x = win->x; *y = win->y;

  parent = (GdkWindowPrivate *) win->parent;
  if (parent)
    *x+=parent->xabs,
    *y+=parent->yabs;
}

void
gdk_window_paint(GdkWindowPrivate *win)
{
  GdkWindowPrivate *child;
  GList *tmp_list;

  if (win->destroyed)
    return;

  if (!win->visible)	/* Children of invisible window must be invisible */
    return;

  if (win->cwin && win->visible) {
    touchwin(win->cwin);
    wmove(win->cwin, win->cursory/FY, win->cursorx/FX);
    wnoutrefresh(win->cwin);
    fprintf( stderr, "window_paint: go to %d, %d\n", win->cursorx, win->cursory );
  }

  tmp_list = win->children;
  while (tmp_list)
    {
      child = (GdkWindowPrivate *)tmp_list->data;
      gdk_window_paint( child );
      tmp_list = tmp_list->next;
    }
}

void
gdk_flush(void)
{
  fprintf( stderr, "Painting everything..." );
  while (gdk_screen_dirty) {
    gdk_screen_dirty = 0;
    gdk_window_expose(&gdk_root_parent);
  }
  gdk_window_paint(&gdk_root_parent);
  fprintf( stderr, "done\n" );
  doupdate();
}

static void
clip_update (GdkWindowPrivate *win)
{
  GdkWindowPrivate *parent;
  GdkRectangle temp;
  int x, y;
  int isvis;

  window_position( win, &x, &y );
  win->xabs = x;
  win->yabs = y;
  temp.x = x;
  temp.y = y;
  temp.width = win->width;
  temp.height = win->height;
  parent=(GdkWindowPrivate *) win->parent;
  isvis = 1;
  if (!parent)
    win->cvisible = temp;
  else
    isvis = gdk_rectangle_intersect( &temp, &parent->cvisible, &win->cvisible );
  if (!isvis) {
        win->cvisible.x = win->cvisible.y = win->cvisible.height = win->cvisible.width = 0;
  }
  if (win->cvisible.height > 32000) {
    g_warning( "win->height > 32000\n" );
    win->cvisible.height = 0;
  }
}

static int
window_visible (GdkWindowPrivate *win, int *x1, int *y1, int *w1, int *h1)
{
  int x, y, w, h;
  if (!win->visible)
    return 0;
  x = win->cvisible.x;
  y = win->cvisible.y;
  w = win->cvisible.width;
  h = win->cvisible.height;
  if ((x<0) || (y<0) || ((x+w) > gdk_screen_width()) || ((y+h) > gdk_screen_height())) {
    fprintf( stderr, "%d, %d, %d, %d (%d,%d)\n", x, y, w, h, gdk_screen_width(), gdk_screen_height() );
    g_error ("Impossible condition\n" );
  }
  *x1 = x; *y1 = y; *w1 = w; *h1 = h;
  return (win->cvisible.width && win->cvisible.height);
}

static void
window_update (GdkWindowPrivate *win)
{
  GdkWindowPrivate *child;
  GList *tmp_list;

  clip_update(win);
  if (!win->destroyed)
    {
      int x, y, w, h;
      if (win->cwin) { /* HACK */
	  fprintf( stderr, "Update: Window is now invisible\n" );
	  delwin(win->cwin);
	  win->cwin = NULL;
      }
      if (!win->cwin) {
	if (window_visible(win, &x, &y, &w, &h)) {
	  fprintf( stderr, "Window %p now visible, at %d, %d, %d, %d\n", win, h/FY, w/FX, y/FY, x/FX );
	  win->cwin = newwin( h/FY, w/FX, y/FY+1, x/FX );
	  if (!win->cwin)
	    g_error( "FAILED allocating curses window\n" );
	  gdk_window_clear((GdkWindow *) win);
	  gdk_screen_dirty = 1;
#if 0
	  gdk_window_expose(win);
#endif
	} else fprintf( stderr, "Update: Window stays invisible\n" );
      }
#if 0
      else {
	if (!window_visible(win, &x, &y, &w, &h)) {
	  fprintf( stderr, "Update: Window is now invisible\n" );
	  wnoutrefresh(win->cwin);
	  delwin(win->cwin);
	  doupdate();
	  win->cwin = NULL;
	} else {
	  fprintf( stderr, "Move window %s (%x) to %d, %d, %d, %d\n", win->title, win, h/FY, w/FX, y/FY, x/FX );
	  doupdate();
#if 0
	  wresize( win->cwin, h/FY, w/FX );
#endif
	  doupdate();
	}
      }
#endif

      tmp_list = win->children;
      while (tmp_list)
	{
	  child = (GdkWindowPrivate *)tmp_list->data;
	  window_update( child );
	  tmp_list = tmp_list->next;
	}
    }
}

void
gdk_set_fg_window (void)
{
  GdkWindow *win = NULL;
  if (1) {
    fprintf( stderr, "fg_window destroyed, guessing\n" );
    if (gdk_root_parent.children) {
      int i = g_list_length(gdk_root_parent.children);

      for (i--; i>=0; i--) {
	win = g_list_nth_data(gdk_root_parent.children, i);
	if (((GdkWindowPrivate *) win)->visible)
	  break;
      }
    }
  } else win = fg_window;

  fg_window = win;
  if (fg_window) {
    GdkWindowPrivate *priv = ((GdkWindowPrivate *)win);
    char buf[1024];
    sprintf( buf, "%s (%d,%d,%d,%d; %p:%s)", priv->title, priv->x/FX, priv->y/FY, priv->width/FX, priv->height/FY, priv, gdk_window_type(priv) );
    gdk_statusline( buf );
  } else gdk_initial();
  fprintf( stderr, "Setting foreground window to %p\n", win );
}

GdkWindow*
gdk_window_new (GdkWindow     *parent,
		GdkWindowAttr *attributes,
		gint           attributes_mask)
{
  GdkWindow *window;
  GdkWindowPrivate *private;
  GdkWindowPrivate *parent_private;
  gchar *title;

  g_return_val_if_fail (attributes != NULL, NULL);

  if (!parent)
    parent = (GdkWindow*) &gdk_root_parent;

  parent_private = (GdkWindowPrivate*) parent;
  if (parent_private->destroyed)
    return NULL;

  private = g_new (GdkWindowPrivate, 1);
  window = (GdkWindow*) private;

  private->parent = parent;

  parent_private->children = g_list_append (parent_private->children, window);
  private->destroyed = FALSE;
  private->resize_count = 0;
  private->ref_count = 1;

  private->x = (attributes_mask & GDK_WA_X) ? attributes->x : 0;
  private->y = (attributes_mask & GDK_WA_Y) ? attributes->y : 0;
#if 0
  if (private->x < 0)
    private->x = 0;
  if (private->y < 0)
    private->y = 0;
#endif
  private->width = attributes->width?attributes->width: MinFX; /* gdk_screen_width() */;
  private->height = attributes->height?attributes->height: MinFY; /* gdk_screen_height(); */
  private->cwin = NULL; 

  private->window_type = attributes->window_type;
  private->extension_events = FALSE;

  private->filters = NULL;
  private->children = NULL;
  private->visible = 0;

  private->fill = ' ';

  window->user_data = NULL;

  gdk_window_ref (window);

  if (attributes_mask & GDK_WA_TITLE)
    title = attributes->title;
  else
    title = g_get_prgname ();
  private->title = title;

  fprintf(stderr, "New window: %s (%p,par=%p, %d,%d,%d,%d)\n", title, private, private->parent, private->x, private->y, private->width, private->height );
  window_update(private);

  private->visible = 0;
  gdk_set_fg_window();
  gdk_window_clear(window);

#if 0
  {
  }
#endif

  return window;
}

GdkWindow *
gdk_window_foreign_new (guint32 anid)
{
  g_warning( "What is gdk_window_foreign_new good for?" );
  return NULL;
}

static gchar *
gdk_window_type (GdkWindowPrivate *win)
{
  if (!win->visible)
    return "invisible";
  switch (win->window_type) {
  case GDK_WINDOW_TOPLEVEL:	return "toplevel";
  case GDK_WINDOW_CHILD:	return "child";
  case GDK_WINDOW_DIALOG:	return "dialog";
  case GDK_WINDOW_TEMP:		return "temp";
  case GDK_WINDOW_FOREIGN:	return "foreign";
  case GDK_WINDOW_ROOT:		return "root";
  case GDK_WINDOW_PIXMAP:	return "pixmap";
  default:			return "unknown?!";
  }
}

/* Call this function when you want a window and all its children to
   disappear.  When xdestroy is true, a request to destroy the XWindow
   is sent out.  When it is false, it is assumed that the XWindow has
   been or will be destroyed by destroying some ancestor of this
   window.  */

static void
gdk_window_internal_destroy (GdkWindow *window, gboolean xdestroy,
			     gboolean our_destroy)
{
  GdkWindowPrivate *private;
  GdkWindowPrivate *temp_private;
  GdkWindow *temp_window;
  GList *children;
  GList *tmp;

  g_return_if_fail (window != NULL);

  private = (GdkWindowPrivate*) window;

  switch (private->window_type)
    {
    case GDK_WINDOW_TOPLEVEL:
    case GDK_WINDOW_CHILD:
    case GDK_WINDOW_DIALOG:
    case GDK_WINDOW_TEMP:
    case GDK_WINDOW_FOREIGN:
      if (!private->destroyed)
	{
	  private->x = -500;
	  window_update(private);
	  if (private->parent)
	    {
	      GdkWindowPrivate *parent_private = (GdkWindowPrivate *)private->parent;
	      if (parent_private->children)
		parent_private->children = g_list_remove (parent_private->children, window);
	    }

	  if (private->window_type != GDK_WINDOW_FOREIGN)
	    {
	      children = tmp = private->children;
	      private->children = NULL;

	      while (tmp)
		{
		  temp_window = tmp->data;
		  tmp = tmp->next;
		  
		  temp_private = (GdkWindowPrivate*) temp_window;
		  if (temp_private)
		    gdk_window_internal_destroy (temp_window, FALSE,
						 our_destroy);
		}

	      g_list_free (children);
	    }

	  if (private->extension_events != 0)
	    gdk_input_window_destroy (window);

	  if (private->filters)
	    {
	      tmp = private->filters;

	      while (tmp)
		{
		  g_free (tmp->data);
		  tmp = tmp->next;
		}

	      g_list_free (private->filters);
	      private->filters = NULL;
	    }

	  
	  gdk_window_unref (window);

	  private->destroyed = TRUE;
	}
      break;

    case GDK_WINDOW_ROOT:
      g_warning ("attempted to destroy root window");
      break;

    case GDK_WINDOW_PIXMAP:
      g_warning ("called gdk_window_destroy on a pixmap (use gdk_pixmap_unref)");
      break;
    }
}

/* Like internal_destroy, but also destroys the reference created by
   gdk_window_new. */

void
window_top(GdkWindowPrivate *win)
{
  gdk_window_raise( (GdkWindow *) win );
}

void
gdk_window_destroy (GdkWindow *window)
{
  fprintf( stderr, "window destroy %p\n", window );
  gdk_window_internal_destroy (window, TRUE, TRUE);
  if (window == fg_window)
    fg_window = NULL;

  gdk_set_fg_window();
  gdk_window_unref (window);
}

/* This function is called when the XWindow is really gone.  */

void
gdk_window_destroy_notify (GdkWindow *window)
{
  g_warning( "gdk_window_destroy_notify: impossible happened" );
}

GdkWindow*
gdk_window_ref (GdkWindow *window)
{
  GdkWindowPrivate *private = (GdkWindowPrivate *)window;
  g_return_val_if_fail (window != NULL, NULL);

  private->ref_count += 1;
  return window;
}

void
gdk_window_unref (GdkWindow *window)
{
  GdkWindowPrivate *private = (GdkWindowPrivate *)window;
  g_return_if_fail (window != NULL);

  private->ref_count -= 1;
  if (private->ref_count == 0)
    {
      if (!private->destroyed)
	{
	  g_warning ("losing last reference to undestroyed window\n");
	}
      g_dataset_destroy (window);
      g_free (window);
    }
}

void
gdk_window_show (GdkWindow *window)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  private = (GdkWindowPrivate*) window;
  if (!private->destroyed)
    {
      private->visible = 1;
      window_update(private);

      gdk_screen_dirty = 1;
      gdk_set_fg_window();

      fprintf(stderr, "gdk_window_show %p, at %d,%d,%d,%d\n",
	      private, private->x, private->y, private->width, private->height);
      {
	GdkEvent *event;
#if 0
	event = gdk_event_new();
	event->any.type = GDK_MAP;
	event->any.window = window;
	gdk_event_queue(event, private);

	event = gdk_event_new();
	event->visibility.type = GDK_VISIBILITY_NOTIFY;
	event->visibility.window = window;
	event->visibility.state = GDK_VISIBILITY_PARTIAL;
	gdk_event_queue(event, private);
#endif
	event = gdk_event_new();
	event->configure.type = GDK_CONFIGURE;
	event->configure.window = window;
	event->configure.width = private->width;
	event->configure.height = private->height;
	event->configure.x = private->x;
	event->configure.y = private->y;
	gdk_event_queue(event, private);


	event = gdk_event_new();
	event->configure.type = GDK_CONFIGURE;
	event->configure.window = window;
	event->configure.width = private->width;
	event->configure.height = private->height;
	event->configure.x = private->x;
	event->configure.y = private->y;
	gdk_event_queue(event, private);
      }  
    }
}

void
gdk_window_hide (GdkWindow *window)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  fprintf( stderr, "Hide window %p\n", window );

  private = (GdkWindowPrivate*) window;
  if (!private->destroyed) {
    private->visible = 0;

    gdk_screen_dirty = 1;
    gdk_set_fg_window();
  }
}

void
gdk_window_withdraw (GdkWindow *window)
{
  gdk_window_hide (window);
}

void
gdk_window_move (GdkWindow *window,
		 gint       x,
		 gint       y)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  private = (GdkWindowPrivate*) window;
  private->x = x;
  private->y = y;
  window_update (private);
}

void
gdk_window_resize (GdkWindow *window,
		   gint       width,
		   gint       height)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  if (width < MinFX)
    width = MinFX;
  if (height < MinFY)
    height = MinFY;

  private = (GdkWindowPrivate*) window;

  if (!private->destroyed &&
      ((private->resize_count > 0) ||
       (private->width != (guint16) width) ||
       (private->height != (guint16) height)))
    {
      private->resize_count += 1;

      private->width = width;
      private->height = height;
      window_update(private);
    }
}

void
gdk_window_move_resize (GdkWindow *window,
			gint       x,
			gint       y,
			gint       width,
			gint       height)
{
  gdk_window_resize(window, width, height);
  gdk_window_move(window, x, y);
}

void
gdk_window_reparent (GdkWindow *window,
		     GdkWindow *new_parent,
		     gint       x,
		     gint       y)
{
  GdkWindowPrivate *window_private;
  GdkWindowPrivate *parent_private;
  GdkWindowPrivate *old_parent_private;

  g_return_if_fail (window != NULL);

  if (!new_parent)
    new_parent = (GdkWindow*) &gdk_root_parent;

  window_private = (GdkWindowPrivate*) window;
  old_parent_private = (GdkWindowPrivate*)window_private->parent;
  parent_private = (GdkWindowPrivate*) new_parent;

  window_private->parent = new_parent;

  if (old_parent_private)
    old_parent_private->children = g_list_remove (old_parent_private->children, window);
  parent_private->children = g_list_append (parent_private->children, window);
  
}

void
gdk_window_clear (GdkWindow *window)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  private = (GdkWindowPrivate*) window;

  if (!private->destroyed)
    gdk_window_clear_area_with( window, 0, 0, private->width, private->height, private->fill );
}

static void
gdk_window_clear_area_with (GdkWindow *window,
		       gint       x,
		       gint       y,
		       gint       width,
		       gint       height,
		       gchar c)
{
  GdkWindowPrivate *gw = (GdkWindowPrivate *) window;
  WINDOW *cw = gw->cwin;
  char s[2];
  s[1] = 0;
  s[0] = c;

#if 1
  fprintf( stderr, "At %d, %d clear, window %p\n", y/FY, x/FX, gw );

  if (!cw) {
    g_warning( "No curses window asociated - pixmap?!" );
    return;
  }
  
  {
    int yl;
    y -= 7;
    y /= FY;
    x /= FX;
    yl = y+height/FY;
    while (y < yl) {
      int i;
      wmove(cw, y, x);
      for (i = 0; i < width/FX; i++)
	waddstr(cw, s);
      y++;
    }
  }
#endif
}

void
gdk_window_clear_area (GdkWindow *window,
		       gint       x,
		       gint       y,
		       gint       width,
		       gint       height)
{

  gdk_window_clear_area_with (window, x, y, width, height, ' ' );
}


void
gdk_window_clear_area_e (GdkWindow *window,
		         gint       x,
		         gint       y,
		         gint       width,
		         gint       height)
{
  gdk_window_clear_area (window, x, y, width, height);
}

void
gdk_window_copy_area (GdkWindow    *window,
		      GdkGC        *gc,
		      gint          x,
		      gint          y,
		      GdkWindow    *source_window,
		      gint          source_x,
		      gint          source_y,
		      gint          width,
		      gint          height)
{
  g_warning( "I really should implement gdk_window_copy_area" );
}

void
gdk_window_raise (GdkWindow *window)
{
  GdkWindowPrivate *private;
  
  g_return_if_fail (window != NULL);
  
  private = (GdkWindowPrivate*) window;
  
  if (!private->destroyed) {
    GdkWindowPrivate *parent = (GdkWindowPrivate *) private->parent;
    if (parent->children) {
      parent->children = g_list_remove(parent->children, private);
      parent->children = g_list_prepend(parent->children, private);
    }
  }
  gdk_set_fg_window();
}

void
gdk_window_lower (GdkWindow *window)
{
  GdkWindowPrivate *private;
  
  g_return_if_fail (window != NULL);
  
  private = (GdkWindowPrivate*) window;
  
  if (!private->destroyed)
    g_warning( "lower: Not implemented" );
}

void
gdk_window_set_user_data (GdkWindow *window,
			  gpointer   user_data)
{
  g_return_if_fail (window != NULL);
  
  window->user_data = user_data;
}

void
gdk_window_set_hints (GdkWindow *window,
		      gint       x,
		      gint       y,
		      gint       min_width,
		      gint       min_height,
		      gint       max_width,
		      gint       max_height,
		      gint       flags)
{
}

void 
gdk_window_set_geometry_hints (GdkWindow      *window,
			       GdkGeometry    *geometry,
			       GdkWindowHints  geom_mask)
{
}

void
gdk_window_set_title (GdkWindow   *window,
		      const gchar *title)
{
  GdkWindowPrivate *private;
  
  g_return_if_fail (window != NULL);
  
  private = (GdkWindowPrivate*) window;
  if (!private->destroyed)
    private->title = g_strdup(title);
}

void          
gdk_window_set_role (GdkWindow   *window,
		     const gchar *role)
{
}

void          
gdk_window_set_transient_for (GdkWindow *window, 
			      GdkWindow *parent)
{
}

void
gdk_window_set_background (GdkWindow *window,
			   GdkColor  *color)
{
  g_warning( "I should implement gdk_window_set_background" );
}

void
gdk_window_set_back_pixmap (GdkWindow *window,
			    GdkPixmap *pixmap,
			    gint       parent_relative)
{
}

void
gdk_window_set_cursor (GdkWindow *window,
		       GdkCursor *cursor)
{
}

void
gdk_window_set_colormap (GdkWindow   *window,
			 GdkColormap *colormap)
{
}

void
gdk_window_get_user_data (GdkWindow *window,
			  gpointer  *data)
{
  g_return_if_fail (window != NULL);
  
  *data = window->user_data;
}

void
gdk_window_get_geometry (GdkWindow *window,
			 gint      *x,
			 gint      *y,
			 gint      *width,
			 gint      *height,
			 gint      *depth)
{
  GdkWindowPrivate *window_private;
  
  if (!window)
    window = (GdkWindow*) &gdk_root_parent;
  
  window_private = (GdkWindowPrivate*) window;
  
  if (!window_private->destroyed)
    {
      if (x)
	*x = window_private->x;
      if (y)
	*y = window_private->y;
      if (width)
	*width = window_private->width;
      if (height)
	*height = window_private->width;
      if (depth)
	*depth = 1;
    }
}

void
gdk_window_get_position (GdkWindow *window,
			 gint      *xp,
			 gint      *yp)
{
  GdkWindowPrivate *window_private;
  GdkWindowPrivate *parent_private;
  int x, y;
  
  g_return_if_fail (window != NULL);
  
  window_private = (GdkWindowPrivate*) window;
  parent_private = (GdkWindowPrivate*) window_private->parent;
    
  x = window_private->x;
  y = window_private->y;
  if (parent_private) {
    x -= parent_private->x;
    y -= parent_private->y;
  }
  if (xp)
    *xp = x;
  if (yp)
    *yp = y;
}

void
gdk_window_get_size (GdkWindow *window,
		     gint       *width,
		     gint       *height)
{
  GdkWindowPrivate *window_private;
  
  window_private = (GdkWindowPrivate*) window;
  
  if (width)
    *width = window_private?window_private->width:0;
  if (height)
    *height = window_private?window_private->height:0;
}

GdkVisual*
gdk_window_get_visual (GdkWindow *window)
{
  return system_visual;
}

GdkColormap*
gdk_window_get_colormap (GdkWindow *window)
{
  return FULL;
}

GdkWindowType
gdk_window_get_type (GdkWindow *window)
{
  GdkWindowPrivate *window_private;

  g_return_val_if_fail (window != NULL, (GdkWindowType) -1);

  window_private = (GdkWindowPrivate*) window;
  return window_private->window_type;
}

gint
gdk_window_get_origin (GdkWindow *window,
		       gint      *x,
		       gint      *y)
{
  GdkWindowPrivate *private;
  gint return_val;
  gint tx = 0;
  gint ty = 0;

  g_return_val_if_fail (window != NULL, 0);

  private = (GdkWindowPrivate*) window;

  if (!private->destroyed)
    {
      return_val = 1;
      window_position(private, &tx, &ty);
    }
  else
    return_val = 0;
  
  if (x)
    *x = tx;
  if (y)
    *y = ty;
  
  return return_val;
}

gboolean
gdk_window_get_deskrelative_origin (GdkWindow *window,
				    gint      *x,
				    gint      *y)
{
  gdk_window_get_origin (window, x, y);
  return 1;
}

void
gdk_window_get_root_origin (GdkWindow *window,
			    gint      *x,
			    gint      *y)
{
  GdkWindowPrivate *private;

  g_return_if_fail (window != NULL);

  private = (GdkWindowPrivate*) window;
  if (x)
    *x = 0;
  if (y)
    *y = 0;
  return;
}

GdkWindow*
gdk_window_get_pointer (GdkWindow       *window,
			gint            *x,
			gint            *y,
			GdkModifierType *mask)
{
  if (x)
    *x = 0;
  if (y)
    *y = 0;
  if (mask)
    *mask = 0;
  
  return NULL;
}

GdkWindow*
gdk_window_at_pointer (gint *win_x,
		       gint *win_y)
{
  g_warning( "gdk_winow_at_pointer not implemented" );
  return NULL;
}

GdkWindow*
gdk_window_get_parent (GdkWindow *window)
{
  g_return_val_if_fail (window != NULL, NULL);

  return ((GdkWindowPrivate*) window)->parent;
}

GdkWindow*
gdk_window_get_toplevel (GdkWindow *window)
{
  GdkWindowPrivate *private;

  g_return_val_if_fail (window != NULL, NULL);

  private = (GdkWindowPrivate*) window;

  while (private->window_type == GDK_WINDOW_CHILD)
    {
      window = ((GdkWindowPrivate*) window)->parent;
      private = (GdkWindowPrivate*) window;
    }

  return window;
}

GList*
gdk_window_get_children (GdkWindow *window)
{
  return NULL;
}

GdkEventMask  
gdk_window_get_events      (GdkWindow       *window)
{
  return 0;
}

void          
gdk_window_set_events      (GdkWindow       *window,
			    GdkEventMask     event_mask)
{
}

void
gdk_window_add_colormap_windows (GdkWindow *window)
{
}

/*
 * This needs the X11 shape extension.
 * If not available, shaped windows will look
 * ugly, but programs still work.    Stefan Wille
 */
void
gdk_window_shape_combine_mask (GdkWindow *window,
			       GdkBitmap *mask,
			       gint x, gint y)
{
}

void          
gdk_window_add_filter     (GdkWindow     *window,
			   GdkFilterFunc  function,
			   gpointer       data)
{
  GdkWindowPrivate *private;
  GList *tmp_list;
  GdkEventFilter *filter;

  private = (GdkWindowPrivate*) window;
  if (private && private->destroyed)
    return;

  if(private)
    tmp_list = private->filters;
  else
    tmp_list = gdk_default_filters;

  while (tmp_list)
    {
      filter = (GdkEventFilter *)tmp_list->data;
      if ((filter->function == function) && (filter->data == data))
	return;
      tmp_list = tmp_list->next;
    }

  filter = g_new (GdkEventFilter, 1);
  filter->function = function;
  filter->data = data;

  if(private)
    private->filters = g_list_append (private->filters, filter);
  else
    gdk_default_filters = g_list_append (gdk_default_filters, filter);
}

void
gdk_window_remove_filter  (GdkWindow     *window,
			   GdkFilterFunc  function,
			   gpointer       data)
{
  GdkWindowPrivate *private;
  GList *tmp_list, *node;
  GdkEventFilter *filter;

  private = (GdkWindowPrivate*) window;

  if(private)
    tmp_list = private->filters;
  else
    tmp_list = gdk_default_filters;

  while (tmp_list)
    {
      filter = (GdkEventFilter *)tmp_list->data;
      node = tmp_list;
      tmp_list = tmp_list->next;

      if ((filter->function == function) && (filter->data == data))
	{
	  if(private)
	    private->filters = g_list_remove_link (private->filters, node);
	  else
	    gdk_default_filters = g_list_remove_link (gdk_default_filters, tmp_list);
	  g_list_free_1 (node);
	  g_free (filter);
	  
	  return;
	}
    }
}

void
gdk_window_set_override_redirect(GdkWindow *window,
				 gboolean override_redirect)
{
}

void          
gdk_window_set_icon        (GdkWindow *window, 
			    GdkWindow *icon_window,
			    GdkPixmap *pixmap,
			    GdkBitmap *mask)
{
}

void          
gdk_window_set_icon_name   (GdkWindow *window, 
			    gchar *    name)
{
}

void          
gdk_window_set_group   (GdkWindow *window, 
			GdkWindow *leader)
{
}

void
gdk_window_set_decorations (GdkWindow      *window,
			    GdkWMDecoration decorations)
{
}

void
gdk_window_set_functions (GdkWindow    *window,
			  GdkWMFunction functions)
{
}

GList *
gdk_window_get_toplevels (void)
{
  GList *new_list = NULL;
  GList *tmp_list;

  tmp_list = gdk_root_parent.children;
  while (tmp_list)
    {
      new_list = g_list_prepend (new_list, tmp_list->data);
      tmp_list = tmp_list->next;
    }

  return new_list;
}

void
gdk_window_set_child_shapes (GdkWindow *window)
{
  g_return_if_fail (window != NULL);
}

void
gdk_window_merge_child_shapes (GdkWindow *window)
{
  g_return_if_fail (window != NULL);
}

/*************************************************************
 * gdk_window_is_visible:
 *     Check if the given window is mapped.
 *   arguments:
 *     window: 
 *   results:
 *     is the window mapped
 *************************************************************/

gboolean 
gdk_window_is_visible (GdkWindow *window)
{
  g_return_val_if_fail (window != NULL, FALSE);

  return TRUE; /* private->mapped; */
}

/*************************************************************
 * gdk_window_is_viewable:
 *     Check if the window and all ancestors of the window
 *     are mapped. (This is not necessarily "viewable" in
 *     the X sense, since we only check as far as we have
 *     GDK window parents, not to the root window)
 *   arguments:
 *     window:
 *   results:
 *     is the window viewable
 *************************************************************/

gboolean 
gdk_window_is_viewable (GdkWindow *window)
{
  GdkWindowPrivate *private = (GdkWindowPrivate *)window;

  g_return_val_if_fail (window != NULL, FALSE);

  while (private && 
	 (private != &gdk_root_parent) &&
	 (private->window_type != GDK_WINDOW_FOREIGN))
    {
      /*      if (!private->mapped)
	      return FALSE; */

      private = (GdkWindowPrivate *)private->parent;
    }

  return TRUE;
}

void          
gdk_drawable_set_data (GdkDrawable   *drawable,
		       const gchar   *key,
		       gpointer	      data,
		       GDestroyNotify destroy_func)
{
  g_dataset_set_data_full (drawable, key, data, destroy_func);
}


/*************************************************************
 * gdk_window_set_static_gravities:
 *     Set the bit gravity of the given window to static,
 *     and flag it so all children get static subwindow
 *     gravity.
 *   arguments:
 *     window: window for which to set static gravity
 *     use_static: Whether to turn static gravity on or off.
 *   results:
 *     Does the XServer support static gravity?
 *************************************************************/

gboolean 
gdk_window_set_static_gravities (GdkWindow *window,
				 gboolean   use_static)
{
  return FALSE;
}

