/*$Copyright:
 * Copyright (C) 1992.5.22. Recruit Co.,Ltd. 
 * Institute for Supercomputing Research
 * All rights reserved.
 * NewsBase  by ISR, Kazuto MIYAI, Gary ARAKAKI, Katsunori SUZUKI, Kok-meng Lue
 *
 * You may freely copy, distribute and reuse the code in this program under 
 * following conditions.
 * - to include this notice in the source code, if it is to be distributed 
 *   with source code.
 * - to add the file named "COPYING" within the code, which shall include 
 *   GNU GENERAL PUBLIC LICENSE(*).
 * - to display an acknowledgement in binary code as follows: "This product
 *   includes software developed by Recruit Co.,Ltd., ISR."
 * - to display a notice which shall state that the users may freely copy,
 *   distribute and reuse the code in this program under GNU GENERAL PUBLIC
 *   LICENSE(*)
 * - to indicate the way to access the copy of GNU GENERAL PUBLIC LICENSE(*)
 *
 *   (*)GNU GENERAL PUBLIC LICENSE is stored in the file named COPYING
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
$*/
/* Generated by Interface Builder */

#import "IFolderScrollMatrix.h"
#import "IFolderCell.h"
#import "IFolderScrollView.h"
#import "TransparentWindow.h"
#import "IDataGroupBrowser.h"
#import "INXBrowserCellWithLinkedObject.h"
#import "IUifNode.h"
#import "InfoD.h"
#import "ITreeNodeD.h"
#import "errdebug.h"
#import "data_types.h"

#import <appkit/NXImage.h>
#import <appkit/ClipView.h>
#import <appkit/ButtonCell.h>
#import <appkit/Font.h>
#import <string.h>

// this IfolderScrollMatrix class is asumed that it is set to a docment
// view of ScrollView(or its subclass)

@implementation IFolderScrollMatrix

- initFrame:(NXRect *)frameRect
{
    NXSize	folderCellSize;
    id		folderProto, titleFont;
    
    [super initFrame:frameRect];
    
    folderCellSize.width = frameRect->size.width;
    folderCellSize.height = frameRect->size.height;
    [self setCellSize:&folderCellSize];
    [self setBackgroundGray:NX_LTGRAY];
    [self setCellBackgroundGray:NX_LTGRAY];
    [self setEnabled:YES];
    [self notifyAncestorWhenFrameChanged:YES];
    [self setMode:NX_TRACKMODE];	// mode is need
    
    folderProto = [[IFolderCell allocFromZone:[self zone]] 
    					initIconCell:CLOSEDFOLDER];
    [folderProto setBordered:NO];
    [folderProto setIconPosition:NX_ICONABOVE];
    [folderProto setTarget:self];
    [folderProto setAction:@selector(selectNewsgroupInBrowser:)];
    titleFont = [Font newFont:TITLEFONT size:TITLEFONTSIZE];
    [folderProto setFont:titleFont];
    [self setPrototype:folderProto];
    
    isPalette = YES;
    return self;
}

- newsgroupClicked:sender
{
    id		node;
    id		iconImage;
    id		folderCell;
    const char	*groupname;
    char	newsgroupName[512];
    NXRect	contRect, docRect;
    id		contView;
    id		groupInfoD;
    id		bcellList, bcell;
    int		i, hiliCellCount;
    id		lastColumnMatrix;

    DBG(1,fprintf(stderr,"-- newsgroup is clicked"));
    // get selected column
    lastColumnMatrix = [sender matrixInColumn:[sender selectedColumn]];
    if ([[lastColumnMatrix selectedCell] isLeaf]==NO) {
        [superview display];
      return (self);
    }
    
    // count hilighted cell in browser and setup newsgorupname for cell's title
    bcellList = [lastColumnMatrix cellList];
    hiliCellCount=0;
    newsgroupName[0] = '\0';
    for (i=0; bcell=[bcellList objectAt:i]; ++i) {
	if ([bcell isHighlighted]) {
	    // node is a obj. of IUifNode, which is linked to 
	    // NXBrowserCellWithLinkedObject(subclass of NXBrowserCell)
	    node = [bcell node];
	    // get newsgroup name
	    groupInfoD = [[node linkedData] dataForKey:GROUPINFO];
	    groupname = [groupInfoD infoForKey:GROUPNAME];

	    if (hiliCellCount != 0) {
		// multiple cell is selected
		// groupname will be separated by ','
		strncat (newsgroupName, ",", (512-strlen(newsgroupName)-1));
	    }
	    // copy news group name 
	    strncat (newsgroupName, groupname, (512-strlen(newsgroupName)-1));
	    ++hiliCellCount;
	}
    }

    [window disableFlushWindow];

    // set up folderCell
    [self _removeFolders];
    [self addCol];

    [self sizeToCells];
    [superview sizeTo:frame.size.width :frame.size.height];
    
    folderCell = [self cellAt:0 :1];	// root folder[0,0], news folder[0,1]

    // set up cell's image
    if (hiliCellCount > 1) {
	// cell in browser is multiple selected
	iconImage = [NXImage findImageNamed:CLOSEDMULTIFOLDER];
    } else {
	iconImage = [NXImage findImageNamed:CLOSEDFOLDER];
    }

    [folderCell setImage:iconImage];
    [folderCell setTitle:newsgroupName];
    [folderCell setLeaf:YES];

    // [superview superview]-> content view of scroll view
    contView = [oScrollDocView superview];

    [contView getFrame:&contRect];    
    [oScrollDocView getFrame:&docRect];

    docRect.size.width = frame.size.width;
    // resize docView as same size as IFolderScrollMatrix
    // ??this statement has side efect that docView will translate up a little
    // (by 2 pixels) when you try to scroll in ScrollView??
    [oScrollDocView setFrame:&docRect];
    if (contRect.size.width < docRect.size.width) {
	// document view width is larger than clip view, so move scroller
	contRect.origin.x = docRect.size.width - contRect.size.width;
	// ??move visible frame 2 pixels because of the side effect above??
//	contRect.origin.y -= 2.0;
	contRect.origin.y = 0.0;
	[contView rawScroll:&(contRect.origin)];
	[oScrollView reflectScroll:contView];
    }

    [self display];
    [window reenableFlushWindow];
    return self;
}

- registerCells
{
    int		i;
    NXRect	iconRect, visibleRect;
    NXPoint	iconCenter;
    
    // register allCells, it will get frame of cell rect and register it.
    [super registerCells];
    
    // unregister unvisible cells
    for (i = 0; i < numCols; ++i) {
	// check if the center of icon is visible
	[self getCellFrame:&iconRect at:0 :i];
	[[self cellAt:0 :i] getIconRect:&iconRect];
	iconCenter.x = iconRect.origin.x + iconRect.size.width/2;
	iconCenter.y = iconRect.origin.y + iconRect.size.height/2;
	
	[[[superview superview] superview] getDocVisibleRect:&visibleRect];
	DBG(10, fprintf(stderr,"-- iconCenter.x=%f\t y=%f\n", 
				iconCenter.x, iconCenter.y));
	DBG(10, fprintf(stderr,"-- visibleRect.origin.x=%f\t y=%f\n    size.width=%f\t height=%f\n", visibleRect.origin.x,visibleRect.origin.y, visibleRect.size.width, visibleRect.size.height));

	if (NXPointInRect(&iconCenter, &visibleRect) == NO) {
	    [super unregisterCellAt:0 :i window:window];
	}
    }
    return self;
}

- setRootName:(char *)rootName
{
    id		rootCell;
    
    rootCell = [super setRootName:rootName];
    [rootCell setLeaf:NO];
    return self;
}

- (void)_removeFolders
{
    // remove folder icons, leave only root icon(nextStation)
    int		rowNum, colNum, i;

    // selected cell in newsgroup browser is directory
    [self getNumRows:&rowNum numCols:&colNum];
    // there are root folder and other folders, remove only folders
    for (i=colNum; i > 1; --i) {
	[self unregisterCellAt:0 :(i-1) window:window];
	[self removeColAt:(i-1) andFree:YES];
	[self sizeToCells];
	[superview sizeTo:frame.size.width :frame.size.height];
    }
}

- selectNewsgroupInBrowser:sender
{    
    [super selectNewsgroupInBrowser:sender];
    
    if ([self selectedCol] == 0) {
	[self _removeFolders];
	[self display];
    }
    return (self);
}

@end
