/* 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 "gdk.h"
#include "gdkprivate.h"
#include <string.h>
#include <unistd.h>

void
gdk_draw_point (GdkDrawable *drawable,
                GdkGC       *gc,
                gint         x,
                gint         y)
{
}

void
gdk_draw_line (GdkDrawable *drawable,
	       GdkGC       *gc,
	       gint         x1,
	       gint         y1,
	       gint         x2,
	       gint         y2)
{
  /* FIXME: Maybe draw horizontal --- and vertical | lines? */
}

void
gdk_draw_rectangle (GdkDrawable *drawable,
		    GdkGC       *gc,
		    gint         filled,
		    gint         x,
		    gint         y,
		    gint         width,
		    gint         height)
{
}

void
gdk_draw_arc (GdkDrawable *drawable,
	      GdkGC       *gc,
	      gint         filled,
	      gint         x,
	      gint         y,
	      gint         width,
	      gint         height,
	      gint         angle1,
	      gint         angle2)
{
}

void
gdk_draw_polygon (GdkDrawable *drawable,
		  GdkGC       *gc,
		  gint         filled,
		  GdkPoint    *points,
		  gint         npoints)
{
}

/* gdk_draw_string
 *
 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
 *
 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
 */
void
gdk_draw_string (GdkDrawable *drawable,
		 GdkFont     *font,
		 GdkGC       *gc,
		 gint         x,
		 gint         y,
		 const gchar *string)
{
  GdkGCPrivate *gc_private;
  GdkWindowPrivate *win = (GdkWindowPrivate *) drawable;
  WINDOW *cw = win->cwin;
  char buf[256], *s;

  strcpy(buf, string);	/* FIXME: buffer overrun */
  s = buf;

  fprintf( stderr, "At %d, %d print '%s', window %d, %d [%d, %d] (%p): ", y/FY, x/FX, s, win->yabs/FY, win->xabs/FX, win->cvisible.y/FY, win->cvisible.x/FX, drawable );
  gc_private = (GdkGCPrivate*) gc;

  g_return_if_fail(gc);

  if (!cw) {
    fprintf( stderr, "window not visible\n" );
    return;
  }

  if ((x > 32000) || (y>32000) || (x < -32000) || (y < -32000)) {
    g_warning( "Fixme: out of range params passed to gdk_draw_string\n" );
    return;
  }

  y -= FY;
  {
    GdkRectangle text, res;
    text.x = x;
    text.y = y;
    text.height = FY;
    text.width = FX*strlen(s);
    if (!gdk_rectangle_intersect (&text, &gc_private->clip, &res)) {
      fprintf( stderr, "outside clip region\n" );
      return;
    } 
      
    res.x += win->xabs;
    res.y += win->yabs;
    if (!gdk_rectangle_intersect (&res, &win->cvisible, &text)) {
      fprintf( stderr, "outside window\n" );
      return;
    }
    text.x -= win->xabs;
    text.y -= win->yabs;

    if ((text.x - x) < 0)
      g_error( "strange, - shift\n" );
          
    s += (text.x - x)/FX;
    if (text.width/FX > strlen(s))
      g_error( "strange, too short\n" );
    s[text.width/FX] = 0;
    x = text.x;
  }
  fprintf( stderr, "resulting draw at %d, %d ", y/FY, x/FX );
  if (gdk_use_colors) {
    int pair = (gc_private->background*8 + gc_private->foreground);
    if ((pair < 0) || (pair > 8*8))
      pair = 0;
    fprintf( stderr, " color %d = (%d,%d) requested\n", pair, gc_private->background, gc_private->foreground );
    wattron(cw, COLOR_PAIR(pair));
  } else fprintf( stderr, "\n" );
  wmove(cw, y/FY, x/FX );
  waddstr(cw, s );
}

#define C(f, b) if (gdk_use_colors) attron(COLOR_PAIR(8*b + f));
void
gdk_initial (void)
{
  move(0, 0);
  C(4,1);
  addstr(" g");
  C(2,1);
  addstr("d");
  C(6,1)
  addstr("k");
  C(7,1);
  attron(A_BOLD);
  addstr(".cursed");
  attroff(A_BOLD);
  addstr(" (c) 1999 Pavel Machek <pavel@ucw.cz>, LGPL, press M-SPC for menu "); 
  refresh();
}

void
gdk_statusline (char *msg)
{
  int i,j,k = 0;
  move(0, 0);
  C(7,1);
  addch(' ');
  for (i=0; i<strlen(msg); i++) {
    if (msg[i] != '_') {
      addch(msg[i]);
      attroff(A_BOLD);
      k++;
    } else
      attron(A_BOLD);
  }
  attroff(A_BOLD);
  for (j=k; j<(gdk_screen_width()/FX); j++)
    addch(' ');
  refresh();
}

void
gdk_statusline_err (char *msg)
{
  gdk_statusline (msg);
  sleep(1);
}

/* gdk_draw_text
 *
 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
 *
 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
 */
void
gdk_draw_text (GdkDrawable *drawable,
	       GdkFont     *font,
	       GdkGC       *gc,
	       gint         x,
	       gint         y,
	       const gchar *text,
	       gint         text_length)
{
  char tmp[258];

  if (text_length>255)
    g_error("string too long");
  tmp[257]=0;
  strncpy(tmp, text, text_length);
  gdk_draw_string(drawable, font, gc, x, y, tmp);
}

void gdk_draw_cursor	 (GdkDrawable  *drawable,
			  GdkFont      *font,
			  GdkGC	       *gc,
			  gint		x,
			  gint		y,
			  int		style)
{
  GdkWindowPrivate *win = (GdkWindowPrivate *) drawable;

  win->cursorx = x;
  win->cursory = y;
  g_warning( "Cursor moved to %d,%d\n", x, y );
}

void
gdk_draw_text_wc (GdkDrawable	 *drawable,
		  GdkFont	 *font,
		  GdkGC		 *gc,
		  gint		  x,
		  gint		  y,
		  const GdkWChar *text,
		  gint		  text_length)
{
  gchar *text_8bit;
  gint i;

  text_8bit = g_new (gchar, text_length+1);
  for (i=0; i<text_length; i++) text_8bit[i] = text[i];
  text_8bit[i]=0;
  gdk_draw_string(drawable, font, gc, x, y, text_8bit);
  g_free (text_8bit);
}

void
gdk_draw_pixmap (GdkDrawable *drawable,
		 GdkGC       *gc,
		 GdkPixmap   *src,
		 gint         xsrc,
		 gint         ysrc,
		 gint         xdest,
		 gint         ydest,
		 gint         width,
		 gint         height)
{
#if 0
  gdk_draw_text (drawable, NULL, gc, xdest, ydest+FY, "[PIXMAP]", MIN(width/FX,8));
#endif
}

void
gdk_draw_image (GdkDrawable *drawable,
		GdkGC       *gc,
		GdkImage    *image,
		gint         xsrc,
		gint         ysrc,
		gint         xdest,
		gint         ydest,
		gint         width,
		gint         height)
{
#if 0
  gdk_draw_text (drawable, NULL, gc, xdest, ydest+FY, "[IMAGE]", MIN(width/FX,7));
#endif
}

void
gdk_draw_points (GdkDrawable *drawable,
		 GdkGC       *gc,
		 GdkPoint    *points,
		 gint         npoints)
{
}

void
gdk_draw_segments (GdkDrawable *drawable,
		   GdkGC       *gc,
		   GdkSegment  *segs,
		   gint         nsegs)
{
}

void
gdk_draw_lines (GdkDrawable *drawable,
              GdkGC       *gc,
              GdkPoint    *points,
              gint         npoints)
{
}

