/* 
   PXKButtonCell.m

   NSButtonCell for GNUstep GUI X/DPS Backend

   Copyright (C) 1996 Free Software Foundation, Inc.

   Author:  Scott Christley <scottc@net-community.com>
   Date: March 1996
   
   This file is part of the GNUstep GUI X/DPS Backend.

   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.

   If you are interested in a warranty or support for this source code,
   contact Scott Christley <scottc@net-community.com> for more information.
   
   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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/ 

#include <gnustep/xdps/PXKButtonCell.h>
#include <gnustep/xdps/PXKView.h>
#include <gnustep/xdps/PXKWindow.h>
#include <gnustep/xdps/PXKDPSContext.h>

@implementation PXKButtonCell

/* Assumes frame has already been translated to X coordinates */
- (void)drawImage:(NSRect)cellFrame;
{
#if 0    
    const PXKRect *r = newFrame;
    frame = r[0];
#endif

    NSPoint location;
    NSSize size;
    NSImage *image;

    /*
     * Place image centered for now
     */
#if 0
    location.x = 3;
    location.y = 3;
#endif

    image = [self image];
    if (image) {

	size = [image size];

	location.x = cellFrame.size.width /2.0 - size.width/2.0;
	location.y = cellFrame.size.height/2.0 - size.height/2.0;
	
	//[image drawInView:self at:&location];
    }
}

/* Assumes frame has already been translated to X coordinates */
- (void)drawBorder:(NSRect)cellFrame;
{
  // No border then don't draw it
  if ([self isBordered] == NO) {
    return;
  }

  if (cell_type == NSMomentaryChangeButton) {
    [self drawBorderUp: cellFrame];
    return;
  }
    
  if (cell_state == 0) {
    [self drawBorderUp: cellFrame];
  } else {
    [self drawBorderDown: cellFrame];
  }	
}

/* Assumes frame has already been translated to X coordiantes */
- (void)drawBorderDown:(NSRect)cellFrame;
{
  Window xWindow = [(PXKWindow *)[control_view window] xWindow];
  Display *xdisplay = [(PXKDPSContext *)[NSDPSContext currentContext]
					xDisplay];

  // draw the bottom and right lit edges
  XDrawLine(xdisplay, xWindow, pxkWhiteGC,
	    cellFrame.origin.x,
	    cellFrame.origin.y + cellFrame.size.height-1,
	    cellFrame.origin.x + cellFrame.size.width-1,
	    cellFrame.origin.y + cellFrame.size.height-1);
  XDrawLine(xdisplay, xWindow, pxkWhiteGC, 
	    cellFrame.origin.x + cellFrame.size.width-1,
	    cellFrame.origin.y,
	    cellFrame.origin.x + cellFrame.size.width-1,
	    cellFrame.origin.y + cellFrame.size.height-1);
    
  /*  XDrawLine(xdisplay, xWindow, pxkWhiteGC,
	    cellFrame.origin.x + 2,
	    cellFrame.origin.y + cellFrame.size.height-2,
	    cellFrame.origin.x + cellFrame.size.width-2,
	    cellFrame.origin.y + cellFrame.size.height-2);
  XDrawLine(xdisplay, xWindow, pxkWhiteGC,
	    cellFrame.origin.x + cellFrame.size.width-2,
	    cellFrame.origin.y + 1,
	    cellFrame.origin.x + cellFrame.size.width-2,
	    cellFrame.origin.y + cellFrame.size.height-2);
	    */

  // draw the top and left dark edges
  XDrawLine(xdisplay, xWindow, pxkBlackGC,
	    cellFrame.origin.x,
	    cellFrame.origin.y,
	    cellFrame.origin.x + cellFrame.size.width-2,
	    cellFrame.origin.y);
  XDrawLine(xdisplay, xWindow, pxkBlackGC,
	    cellFrame.origin.x,
	    cellFrame.origin.y,
	    cellFrame.origin.x,
	    cellFrame.origin.y + cellFrame.size.height-2);
}

/* Assumes frame has already been translated to X coordiantes */
- (void)drawBorderUp:(NSRect)cellFrame;
{
  Window xWindow = [(PXKWindow *)[control_view window]xWindow];
  Display *xdisplay = [(PXKDPSContext *)[NSDPSContext currentContext]
					xDisplay];

  // draw the bottom and right dark edges
  XDrawLine(xdisplay, xWindow, pxkBlackGC, 
	    cellFrame.origin.x,
	    cellFrame.origin.y + cellFrame.size.height-1,
	    cellFrame.origin.x + cellFrame.size.width-1,
	    cellFrame.origin.y + cellFrame.size.height-1);
  XDrawLine(xdisplay, xWindow, pxkBlackGC,
	    cellFrame.origin.x + cellFrame.size.width-1,
	    cellFrame.origin.y,
	    cellFrame.origin.x + cellFrame.size.width-1,
	    cellFrame.origin.y + cellFrame.size.height-1);

  /*  XDrawLine(xdisplay, xWindow, pxkDarkgrayGC, 
	    cellFrame.origin.x + 2,
	    cellFrame.origin.y + cellFrame.size.height-2,
	    cellFrame.origin.x + cellFrame.size.width-2,
	    cellFrame.origin.y + cellFrame.size.height-2);
  XDrawLine(xdisplay, xWindow, pxkDarkgrayGC,
	    cellFrame.origin.x + cellFrame.size.width-2,
	    cellFrame.origin.y + 1,
	    cellFrame.origin.x + cellFrame.size.width-2,
	    cellFrame.origin.y + cellFrame.size.height-2);
	    */

  // draw the top and left lit edges
  XDrawLine(xdisplay, xWindow, pxkWhiteGC,
	    cellFrame.origin.x,
	    cellFrame.origin.y,
	    cellFrame.origin.x + cellFrame.size.width-2,
	    cellFrame.origin.y);
  XDrawLine(xdisplay, xWindow, pxkWhiteGC,
	    cellFrame.origin.x,
	    cellFrame.origin.y,
	    cellFrame.origin.x,
	    cellFrame.origin.y + cellFrame.size.height-2);
}

/* Assumes frame has already been translated to X coordiantes */
- (void)drawText:(NSRect)cellFrame;
{
  Window xWindow = [(PXKWindow *)[control_view window]xWindow];
  NSDPSContext *cur = [NSDPSContext currentContext];
  Display *xdisplay = [(PXKDPSContext *)cur xDisplay];
  float len1;
  float width1;
  int x, y;
  float font_height;
  NSString *title;
  XFontStruct *win_font;
  NSColor *c = [NSColor blackColor];
  GC xgc;

  (void)load_font(&win_font);

  title = [self title];
  if (title) {
    /* need length for both XTextWidth and XDrawString */
    len1 = [title length];

    if (!win_font) {
      fprintf(stderr, "Error loading font. Exiting.\n");
      exit(-1);
    }
    
    /* get string widths for centering */
    width1 = (float)XTextWidth(win_font, [title cString], len1);
    font_height = win_font->ascent + win_font->descent;

    x = (int)(cellFrame.origin.x + ((cellFrame.size.width - width1)/2.0));
    y = (int)(cellFrame.origin.y + (float)win_font->ascent +
      ((cellFrame.size.height - font_height)/2.0));
    NSLog(@"PXKButtonCell text %f %f %d %d %f\n", width1, font_height, x, y,
	  cellFrame.size.width);
  }

#if 0
    /* output text, centered on each line */
    XDrawString(xDisplay, xWindow, win_gc, 5, font_height, stringValue, len1);

#else

    // Set the color in the current GC
    [c set];
    xgc = [(PXKDPSContext *)cur xGC];

    if (title) {
      if (cell_state == 0) {
	/* output text, centered on each line */
	XDrawString(xdisplay, xWindow, xgc,
		    x, y, [title cString], len1);
      } else {
	XDrawString(xdisplay, xWindow, xgc,
		    x + 1, y + 1, [title cString], len1);
      }
    }
	
#endif
}

// OpenStep drawing methods
- (void)drawInteriorWithFrame:(NSRect)cellFrame
		       inView:(NSView *)controlView
{
  Window xWindow = [(PXKWindow *)[control_view window]xWindow];
  Display *xdisplay = [(PXKDPSContext *)[NSDPSContext currentContext]
					xDisplay];
  NSRect xframe = [[[controlView window] contentView] bounds];
  NSRect cf = [controlView convertRect: cellFrame toView: nil];

  // Flip the coordinate system
  cf.origin.y = xframe.size.height - cf.size.height - cf.origin.y;

  // Fill the interior
  if (cell_state == 0)
    XFillRectangle(xdisplay, xWindow, pxkLightgrayGC,
		   cf.origin.x + 1, cf.origin.y + 1,
		   cf.size.width - 2, cf.size.height - 2);
  else
    XFillRectangle(xdisplay, xWindow, pxkWhiteGC,
		   cf.origin.x + 1, cf.origin.y + 1,
		   cf.size.width - 2, cf.size.height - 2);

  // Draw the image or the text
  if (cell_type == NSImageCellType)
    [self drawImage: cf];
  else
    [self drawText: cf];
}

- (void)drawWithFrame:(NSRect)cellFrame
	       inView:(NSView *)controlView
{
  NSRect xframe = [[[controlView window] contentView] bounds];
  NSRect cf = [controlView convertRect: cellFrame toView: nil];

  // Flip the coordinate system
  cf.origin.y = xframe.size.height - cf.size.height - cf.origin.y;

  // Save last view drawn to
  control_view = controlView;

  // now draw it
  [self drawBorder:cf];
  [self drawInteriorWithFrame: cellFrame inView: controlView];
}

- (void)highlight:(BOOL)lit
	withFrame:(NSRect)cellFrame
	   inView:(NSView *)controlView
{
  cell_highlighted = lit;
  cell_state = lit;
  [self drawWithFrame: cellFrame inView: controlView];
}

@end
