// Modifiedy by Yen-Ju Chen <yjchenx@hotmail.com>
/* Document.m Subclass of NSDocument for Ink application

   Copyright (C) 2000 Free Software Foundation, Inc.

   Author:  Fred Kiefer <fredkiefer@gmx.de>
   Date: 2000
   
   This file is part of GNUstep.
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program 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 General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <AppKit/AppKit.h>
#include <Toolbox/BundleLoader.h>
#include <Toolbox/ToolboxBundleProtocol.h>
#include "ItemHelpPanel.h"

@implementation ItemHelpPanel

- (void) setBundleName: (NSString *) name
{
  int index;

  if (name)
    {
      index = [[loader allNamesOfBundles] indexOfObject: name];
    }
  else
    {
      index = -1;
    }
  [tableView selectRow: index+1 byExtendingSelection: NO];
}

- (id) initWithBundleName: (NSString *) name
{
  NSRect frame;
  NSScrollView *scrollView, *textScrollView;
  NSTableColumn *toolColumn;

  loader = [BundleLoader defaultLoader];
  
  self = [super initWithContentRect: NSMakeRect (100, 100, 400, 300)
                styleMask: (NSTitledWindowMask | 
                         NSResizableWindowMask |
                          NSClosableWindowMask) 
                backing: NSBackingStoreBuffered defer: YES];

  frame = [[self contentView] bounds];

  scrollView = [[NSScrollView alloc] initWithFrame: NSMakeRect(0, 0, frame.size.width/3, frame.size.height)];
  [scrollView setHasHorizontalScroller: NO];
  [scrollView setHasVerticalScroller: YES];
  [scrollView setBorderType: NSBezelBorder];
  [scrollView setAutoresizingMask: NSViewHeightSizable];

  toolColumn = [[NSTableColumn alloc] initWithIdentifier: @"tool"];
  [toolColumn setEditable: NO];
  [[toolColumn headerCell] setStringValue: _(@"Tool")];

  tableView = [[NSTableView alloc] initWithFrame: [scrollView frame]];
  [tableView setDrawsGrid: NO];
  [tableView addTableColumn: toolColumn];
  [tableView setAutoresizesAllColumnsToFit: YES];
  [tableView setAllowsColumnSelection: NO];
  [tableView setAllowsMultipleSelection: NO];
  [tableView setAllowsEmptySelection: NO];
  [tableView setTarget: self];
  [tableView setDataSource: self];
  [tableView setDelegate: self];
  [tableView setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
  RELEASE(toolColumn);

  [scrollView setDocumentView: tableView];
  RELEASE(tableView);

  [[self contentView] addSubview: scrollView];
  RELEASE(scrollView);

  textScrollView = [[NSScrollView alloc] initWithFrame: NSMakeRect(frame.size.width/3, 0, frame.size.width*2/3, frame.size.height)];
  [textScrollView setHasHorizontalScroller: NO];
  [textScrollView setHasVerticalScroller: YES]; 
  [textScrollView setBorderType: NSBezelBorder];
  [textScrollView setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
  [[textScrollView contentView] setAutoresizingMask: NSViewHeightSizable
                                                   | NSViewWidthSizable];
  [[textScrollView contentView] setAutoresizesSubviews:YES];


  // Build up the text network
  frame = [[textScrollView contentView] frame];
  textView = [[NSTextView alloc] initWithFrame: frame];
  [textView setEditable: NO];
  [textView setBackgroundColor: [NSColor whiteColor]];
  [textView setRichText: YES];
//  [textView setUsesFontPanel: YES];
  [textView setDelegate: self];
  [textView setHorizontallyResizable: NO];
  [textView setVerticallyResizable: YES];
  [textView setMinSize: NSMakeSize (0, 0)];
  [textView setMaxSize: NSMakeSize (1E7, 1E7)];
  [textView setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
  [[textView textContainer] setContainerSize: NSMakeSize (frame.size.width,
                                                          1e7)];
//  [[textView textContainer] setWidthTracksTextView: YES];
//  [textView setUsesRuler: YES];

  [textScrollView setDocumentView: textView];
  RELEASE(textView);

//  [scrollView setHasHorizontalRuler: YES];
  [[self contentView] addSubview: textScrollView];
  RELEASE(textScrollView);

// Don't do this. NSPanel will die.
//  [self setDelegate: self];

  // Make the text view the first responder
//  [self makeFirstResponder: textView];
  [self setTitle: @"Toolbox Help"];

  [self setBundleName: name];

  return self;
}

- (void) dealloc
{
  [super dealloc];
}

- (int) numberOfRowsInTableView: (NSTableView *) view
{
  return [[loader allNamesOfBundles] count] + 1;
}

- (id) tableView: (NSTableView *) view
       objectValueForTableColumn: (NSTableColumn *) column
             row: (int)row
{
  if (row == 0)
    return @"Toolbox";
  else
    {
      Class bundleClass = [loader classNameOf: [[loader allNamesOfBundles] objectAtIndex: row-1]];
      return [bundleClass bundleDescription];
    }
}

- (void) tableViewSelectionDidChange: (NSNotification *) not
{
  NSString *path;
  NSAttributedString *attr;
  int selectedRow = [tableView selectedRow];

  if (selectedRow == 0)
    {
      path = [[NSBundle mainBundle] pathForResource: @"ToolboxHelp"
                                     ofType: @"rtf"];
    }
  else
    {
      NSArray *bundles = [loader allNamesOfBundles];
      path = [loader pathForResource: [[[bundles objectAtIndex: selectedRow-1] stringByDeletingPathExtension] stringByAppendingString: @"Help"]
                              ofType: @"rtf"
                            ofBundle: [bundles objectAtIndex: selectedRow-1]];
    }

  attr = [[NSAttributedString alloc] initWithPath: path
                               documentAttributes: NULL];

  if (attr != nil)
    [[textView textStorage] setAttributedString: attr];
  RELEASE(attr);

  [[textView superview] setNeedsDisplay: YES];
}

@end
