/* 
   PXKActionCell.m

   NSActionCell for GNUstep GUI X/DPS Backend

   Copyright (C) 1996 Free Software Foundation, Inc.

   Author:  Pascal Forget <pascal@wsc.com>
   Date: October 1995
   
   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/PXKActionCell.h>
#include <gnustep/xdps/PXKApplication.h>
#include <gnustep/xdps/PXKView.h>
#include <gnustep/xdps/PXKCell.h>

@implementation PXKActionCell

- (void)drawTextInRect:(NSRect)frame;
{
    /* Goofy implementation for testing only */
    
    int len1;
    int width;
    int font_height;
    int alignment = 0;
    XFontStruct *win_font;
#if 0
    fprintf(stdout, "Cell text frame: %d %d %d %d\n",
	    frame.origin.x, frame.origin.y,
	    frame.size.width, frame.size.height);
#endif    
    (void)load_font(&win_font);
    
    /* need length for both XTextWidth and XDrawString */
    len1 = [[self stringValue] length];

    /* get string widths for centering */
    width = XTextWidth(win_font, [[self stringValue] cString], len1);

    font_height = win_font->ascent + win_font->descent;

    /*
     * Wipe out the contents of the window by filling its
     * area with the backGroundColor (defined in NSView.h)
     */

    if ([self isOpaque]) {
	XFillRectangle(pxkDisplay, [(PXKView *)control_view xWindow], 
		       pxkLightgrayGC,
		       frame.origin.x+2, frame.origin.y+2,
		       frame.size.width-4, frame.size.height-4);
    }
    
    if((alignment == NSLeftTextAlignment) && (width < frame.size.width)) {
	XDrawString(pxkDisplay, [(PXKView *)control_view xWindow],
		    pxkBlackGC,
		    frame.origin.x + 5,
		    frame.origin.y + font_height,
		    [[self stringValue] cString], len1);
    } else if((alignment == NSRightTextAlignment) 
	      && ((width+5)<frame.size.width)) {
      XDrawString(pxkDisplay, [(PXKView *)control_view xWindow],
		    pxkBlackGC,
		    frame.origin.x + frame.size.width - 5 - width,
		    frame.origin.y + font_height,
		    [[self stringValue] cString], len1);
    } else {

	if (width > (frame.size.width-4)) {
	    XDrawString(pxkDisplay, [(PXKView *)control_view xWindow],
			pxkBlackGC,
			frame.origin.x + frame.size.width - width-5, 
			frame.origin.y + font_height,
			[[self stringValue] cString], len1);	    
	} else {
	    /* output text, centered on each line */
	    XDrawString(pxkDisplay, [(PXKView *)control_view xWindow],
			pxkBlackGC,
			frame.origin.x + (frame.size.width - width-6)/2, 
			frame.origin.y + font_height,
			[[self stringValue] cString], len1);
	}
    }
}

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView;
{
    control_view = controlView;

    if ([self image]) {
	[(PXKCell *)self drawImage];
    }

    if ([self stringValue]) {
	[self drawTextInRect:[self titleRectForBounds:cellFrame]];
    }
}

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView;
{
    control_view = controlView;

    [(PXKCell *)self drawBorder];
    [self drawInteriorWithFrame:cellFrame inView:controlView];
}

- (NSRect)titleRectForBounds:(NSRect)theRect;
{
#if 0
    fprintf(stdout, "Cell titleRectForBounds: %d %d %d %d\n",
	    theRect.origin.x, theRect.origin.y,
	    theRect.size.width, theRect.size.height);
#endif        
    /* Goofy implementation for testing only */
    return theRect;
}

@end
