/***********************************************************************
 * FXRuby -- the Ruby language bindings for the FOX GUI toolkit.
 * Copyright (c) 2001-2003 by J. Lyle Johnson. All Rights Reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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
 *
 * For further information please contact the author by e-mail
 * at "lyle@users.sourceforge.net".
 ***********************************************************************/

class FXIcon;
class FXFont;
class FXHeader;

 
/// Header style options
enum {
  HEADER_BUTTON     = 0x00008000,     /// Button style can be clicked
  HEADER_HORIZONTAL = 0,              /// Horizontal header control (default)
  HEADER_VERTICAL   = 0x00010000,     /// Vertical header control
  HEADER_TRACKING   = 0x00020000,     /// Tracks continuously while moving
  HEADER_NORMAL     = HEADER_HORIZONTAL|FRAME_NORMAL
  };
  


/// Header item
class FXHeaderItem : public FXObject {
protected:
  FXString  label;
  FXIcon   *icon;
  FXint     size;
  void     *data;
  FXbool    arrow;
protected:
  FXHeaderItem(){}
  virtual void draw(const FXHeader* header,FXDC& dc,FXint x,FXint y,FXint w,FXint h);
public:
  %extend {
    /// Construct new item with given text, icon, size, and user-data
    FXHeaderItem(const FXString& text,FXIcon* ic=NULL,FXint s=0,void* ptr=NULL){
      return new FXRbHeaderItem(text,ic,s,ptr);
      }
    }

  /// Return item's text label
  FXString getText() const;

  /// Return item's icon
  FXIcon* getIcon() const;

#ifdef SWIGRUBY
  %extend {
    /// Change item's user data
    void setData(VALUE ptr) {
      self->setData((void*) ptr);
      }

    /// Get item's user data
    VALUE getData() const {
      return self->getData() ? (VALUE) self->getData() : Qnil;
      }
  }
#endif
#ifdef SWIGPYTHON
  %extend {
    void setData(PyObject* ptr) {
      FXPySetData(self, ptr);
    }
    PyObject* getData() const {
      return FXPyGetData(self);
    }
  }
#endif

  /// Change size
  void setSize(FXint s);

  /// Obtain current size
  FXint getSize() const;

  /// Change sort direction (FALSE, TRUE, MAYBE)
  void setArrowDir(FXuint dir=MAYBE);

  /// Return sort direction (FALSE, TRUE, MAYBE)
  FXuint getArrowDir() const;

  /// Destructor
  virtual ~FXHeaderItem();
  };


DECLARE_FXOBJECT_VIRTUALS(FXHeaderItem)
DECLARE_FXHEADERITEM_VIRTUALS(FXHeaderItem)

%typemap(check) FXint HEADER_ITEM_INDEX {
  if ($1 < 0 || $1 >= arg1->getNumItems()) {
    rb_raise(rb_eIndexError, "header item index out of bounds");
  }
}

%apply FXint HEADER_ITEM_INDEX { FXint index };

/**
* Header control may be placed over a table of other
* widgets to provide a caption and icon.  
* It also provides the means for the columns to be interactively
* resized.
*/
class FXHeader : public FXFrame {
protected:
  FXHeaderItem **items;               // Item list
  FXint          nitems;              // Number of items
  FXColor        textColor;           // Text color
  FXFont*        font;                // Text font
  FXTimer       *timer;               // Tip hover timer
  FXString       help;                // Help text
  FXbool         state;               // Button state
  FXint          active;              // Active button
  FXint          activepos;           // Position of active item
  FXint          activesize;          // Size of active item
  FXint          off;
protected:
  FXHeader();
  void drawSplit(FXint pos);
  virtual FXHeaderItem *createItem(const FXString& text,FXIcon* icon,FXint size,void* ptr);
  virtual void layout();
public:
  long onPaint(FXObject*,FXSelector,void* PTR_EVENT);
  long onLeftBtnPress(FXObject*,FXSelector,void* PTR_EVENT);
  long onLeftBtnRelease(FXObject*,FXSelector,void* PTR_EVENT);
  long onUngrabbed(FXObject*,FXSelector,void* PTR_EVENT);
  long onMotion(FXObject*,FXSelector,void* PTR_EVENT);
  long onTipTimer(FXObject*,FXSelector,void* PTR_IGNORE);
  long onQueryTip(FXObject*,FXSelector,void* PTR_IGNORE);
  long onQueryHelp(FXObject*,FXSelector,void* PTR_IGNORE);
public:
  enum {
    ID_TIPTIMER=FXFrame::ID_LAST,
    ID_LAST
    };
public:
  %extend {
    /// Construct new header control
    FXHeader(FXComposite* p,FXObject* tgt=NULL,FXSelector sel=0,FXuint opts=HEADER_NORMAL,FXint x=0,FXint y=0,FXint w=0,FXint h=0,FXint pl=DEFAULT_PAD,FXint pr=DEFAULT_PAD,FXint pt=DEFAULT_PAD,FXint pb=DEFAULT_PAD){
      return new FXRbHeader(p,tgt,sel,opts,x,y,w,h,pl,pr,pt,pb);
      }
    }

  /// Return number of items
  FXint getNumItems() const;

  /// Return item at given index
  FXHeaderItem *retrieveItem(FXint index) const;

#ifdef SWIGRUBY
  %extend {
    /// Replace the item with a [possibly subclassed] item
    FXint replaceItem(FXint index,FXHeaderItem* item,FXbool notify=FALSE){
      // Save pointer to the soon-to-be-destroyed item
      FXHeaderItem* oldItem=self->retrieveItem(index);

      // Do the deed
      if(item->isMemberOf(FXMETACLASS(FXRbHeaderItem)))
        ((FXRbHeaderItem*)item)->owned=TRUE;
      FXint result=self->replaceItem(index,item,notify);

      // Now zero-out pointers held by still-alive Ruby objects
      FXRbUnregisterRubyObj(oldItem);

      // Done
      return result;
      }

    /// Replace items text, icon, and user-data pointer
    FXint replaceItem(FXint index,const FXString& text,FXIcon *icon=NULL,FXint size=0,void* ITEMDATA=NULL,FXbool notify=FALSE){
      // Save pointer to the soon-to-be-destroyed item
      FXHeaderItem* oldItem=self->retrieveItem(index);

      // Do the deed
      FXint result=self->replaceItem(index,text,icon,size,ITEMDATA,notify);

      // Now zero-out pointers held by still-alive Ruby objects
      FXRbUnregisterRubyObj(oldItem);

      // Done
      return result;
      }
  }


  %extend {
    /// Insert a new [possibly subclassed] item at the given index
    FXint insertItem(FXint index,FXHeaderItem* item,FXbool notify=FALSE){
      if(item->isMemberOf(FXMETACLASS(FXRbHeaderItem)))
        ((FXRbHeaderItem*)item)->owned=TRUE;
      return self->insertItem(index,item,notify);
      }
  }

  /// Insert item at index with given text, icon, and user-data pointer
  FXint insertItem(FXint index,const FXString& text,FXIcon *icon=NULL,FXint size=0,void* ITEMDATA=NULL,FXbool notify=FALSE);

  %extend {
    /// Append a [possibly subclassed] item to the list
    FXint appendItem(FXHeaderItem* item,FXbool notify=FALSE){
      if(item->isMemberOf(FXMETACLASS(FXRbHeaderItem)))
        ((FXRbHeaderItem*)item)->owned=TRUE;
      return self->appendItem(item,notify);
      }
  }

  /// Append new item with given text and optional icon, and user-data pointer
  FXint appendItem(const FXString& text,FXIcon *icon=NULL,FXint size=0,void* ITEMDATA=NULL,FXbool notify=FALSE);

  %extend {
    /// Prepend a [possibly subclassed] item to the list
    FXint prependItem(FXHeaderItem* item,FXbool notify=FALSE){
      if(item->isMemberOf(FXMETACLASS(FXRbHeaderItem)))
        ((FXRbHeaderItem*)item)->owned=TRUE;
      return self->prependItem(item,notify);
      }
  }

  /// Prepend new item with given text and optional icon, and user-data pointer
  FXint prependItem(const FXString& text,FXIcon *icon=NULL,FXint size=0,void* ITEMDATA=NULL,FXbool notify=FALSE);

#endif
#ifdef SWIGPYTHON
  /// Replace the item with a [possibly subclassed] item
  FXint replaceItem(FXint index,FXHeaderItem* item,FXbool notify=FALSE);

  /// Replace items text, icon, and user-data pointer
  FXint replaceItem(FXint index,const FXString& text,FXIcon *icon=NULL,FXint size=0,void* ptr=NULL,FXbool notify=FALSE);

  /// Insert a new [possibly subclassed] item at the give index
  FXint insertItem(FXint index,FXHeaderItem* item,FXbool notify=FALSE);

  /// Insert item at index with given text, icon, and user-data pointer
  FXint insertItem(FXint index,const FXString& text,FXIcon *icon=NULL,FXint size=0,void* ptr=NULL,FXbool notify=FALSE);

  /// Append a [possibly subclassed] item to the list
  FXint appendItem(FXHeaderItem* item,FXbool notify=FALSE);

  /// Append new item with given text and optional icon, and user-data pointer
  FXint appendItem(const FXString& text,FXIcon *icon=NULL,FXint size=0,void* ptr=NULL,FXbool notify=FALSE);

  /// Prepend a [possibly subclassed] item to the list
  FXint prependItem(FXHeaderItem* item,FXbool notify=FALSE);

  /// Prepend new item with given text and optional icon, and user-data pointer
  FXint prependItem(const FXString& text,FXIcon *icon=NULL,FXint size=0,void* ptr=NULL,FXbool notify=FALSE);
#endif

  %extend {
    /// Remove item at index
    void removeItem(FXint index,FXbool notify=FALSE){
      // Save pointer to the soon-to-be-destroyed item
      FXHeaderItem* item=self->retrieveItem(index);

      // Do the deed
      self->removeItem(index,notify);

      // Now zero-out pointers held by still-alive Ruby objects
      FXRbUnregisterRubyObj(item);
      }

    /// Remove all items
    void clearItems(FXbool notify=FALSE){
      // Save pointers to the soon-to-be-destroyed items
      FXint numItems=self->getNumItems();
      FXHeaderItem** items=0;
      if(FXMALLOC(&items,FXHeaderItem*,numItems)){
        for(FXint i=0; i<numItems; i++){
          items[i]=self->retrieveItem(i);
          }
        }

      // Do the deed
      self->clearItems(notify);

      // Now zero-out pointers held by still-alive Ruby objects
      if(items){
        for(FXint i=0; i<numItems; i++){
          FXRbUnregisterRubyObj(items[i]);
          }
        FXFREE(&items);
        }
      }
  }

  /// Return item-index given pixel-offset from left
  FXint getItemAt(FXint offset) const;

  /// Change text label for item at index
  void setItemText(FXint index,const FXString& text);

  /// Get text of item at index
  FXString getItemText(FXint index) const;

  /// Change icon of item at index
  void setItemIcon(FXint index,FXIcon* icon);

  /// Return icon of item at index
  FXIcon* getItemIcon(FXint index) const;

  /// Change size of item at index
  void setItemSize(FXint index,FXint size);

  /// Return size of item at index
  FXint getItemSize(FXint index) const;

  /// Compute offset from the left side of item at index
  FXint getItemOffset(FXint index) const;

#ifdef SWIGRUBY
  %extend {
    /// Change data of item at index
    void setItemData(FXint index, VALUE ptr){
      self->setItemData(index, (void*) ptr);
      }

    /// Return data of item at index 
    VALUE getItemData(FXint index) const {
      return self->getItemData(index) ? (VALUE) self->getItemData(index) : Qnil;
      }
  }
#endif

  /// Change sort direction (FALSE, TRUE, MAYBE)
  void setArrowDir(FXint index,FXbool dir=MAYBE);
  
  /// Return sort direction (FALSE, TRUE, MAYBE)
  FXuint getArrowDir(FXint index) const;

  /// Change text font
  void setFont(FXFont* fnt);

  /// return text font
  FXFont* getFont() const;

  /// Return text color
  FXColor getTextColor() const;

  /// Change text color
  void setTextColor(FXColor clr);

  /// Set header style options
  void setHeaderStyle(FXuint style);

  /// Get header style options
  FXuint getHeaderStyle() const;

  /// Set the status line help text for this header
  void setHelpText(const FXString& text);

  /// Get the status line help text for this header
  FXString getHelpText() const;

  /// Destructor
  virtual ~FXHeader();
  };

%clear FXint index;

DECLARE_FXOBJECT_VIRTUALS(FXHeader)
DECLARE_FXID_VIRTUALS(FXHeader)
DECLARE_FXDRAWABLE_VIRTUALS(FXHeader)
DECLARE_FXWINDOW_VIRTUALS(FXHeader)

