home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / YellowBox / Kits / MiscTableScroll-138.1 / Palettes / MiscTableScroll / Framework / MiscTableBorder.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-31  |  16.7 KB  |  424 lines

  1. #ifndef __MiscTableBorder_h
  2. #define __MiscTableBorder_h
  3. #ifdef __GNUC__
  4. # pragma interface
  5. #endif
  6. //=============================================================================
  7. //
  8. //  Copyright (C) 1995,1996,1997,1998 by Paul S. McCarthy and Eric Sunshine.
  9. //        Written by Paul S. McCarthy and Eric Sunshine.
  10. //                All Rights Reserved.
  11. //
  12. //    This notice may not be removed from this source code.
  13. //
  14. //    This object is included in the MiscKit by permission from the authors
  15. //    and its use is governed by the MiscKit license, found in the file
  16. //    "License.rtf" in the MiscKit distribution.  Please refer to that file
  17. //    for a list of all applicable permissions and restrictions.
  18. //    
  19. //=============================================================================
  20. //-----------------------------------------------------------------------------
  21. // MiscTableBorder.h
  22. //
  23. //    Structure describing border of an MiscTableView.
  24. //
  25. //    NOTE: Many of the sub-arrays in an MiscTableBorder are conditional.
  26. //    They are not guaranteed to be allocated for every instance.  They are
  27. //    only allocated when the caller tries to store a value in them.    Doing
  28. //    a "set" creates the array, even if the value is the default value.
  29. //
  30. //    "Springy" grow when the total size exceeds the global min total size.
  31. //
  32. //    NOTE *1* These methods return true if the new value is different than
  33. //    the old value, and the display needs updating.
  34. //
  35. //-----------------------------------------------------------------------------
  36. //-----------------------------------------------------------------------------
  37. // $Id: MiscTableBorder.h,v 1.19 98/03/29 23:47:14 sunshine Exp $
  38. // $Log:    MiscTableBorder.h,v $
  39. //  Revision 1.19  98/03/29  23:47:14  sunshine
  40. //  v138.1: #import was missing "MiscTableScroll/" for public header.
  41. //  
  42. //  Revision 1.18  98/03/23  21:39:55  sunshine
  43. //  v137.1: Added clearSortDirection().
  44. //  
  45. //  Revision 1.17  98/03/23  08:42:21  sunshine
  46. //  v135.1: Dropped clearVMap().  Added swapSlots().
  47. //-----------------------------------------------------------------------------
  48. #include <MiscTableScroll/MiscTableScroll.h>
  49. #include "MiscSparseSet.h"
  50. #include "MiscTableUtil.h"
  51. @class NSArray, NSCell, NSCoder;
  52.  
  53. struct MiscTableSlot
  54.     {
  55.     MiscPixels    offset;        // Offset of this slot.
  56.     MiscPixels    adj_size;    // Size adjusted for springy-ness.
  57.     MiscPixels    size;        // Target size.
  58.     MiscPixels    min_size;    // Minimum size.
  59.     MiscPixels    max_size;    // Maximum size.
  60.     MiscTableSizing    sizing;        // Sizing options.
  61.     bool isSpringy() const    { return ::isSpringy(sizing); }
  62.     void initWithCoder( NSCoder*, int ver );
  63.     void encodeWithCoder( NSCoder* );
  64.     };
  65.  
  66.  
  67. class MiscTableBorder
  68.     {
  69. private:
  70.     MiscBorderType    type;        // Row / Col.
  71.     MiscTableScroll* owner;
  72.     MiscTableSlot    def_slot;    // Default slot configuration.
  73.     int        max_slots;    // Capacity.  Allocation size of arrays.
  74.     int        num_slots;    // Active number of rows/cols.
  75.     MiscTableSlot*    slots;        // Indexed by visual coords.
  76.     MiscCoord_P*    v2p;        // Visual -> Physical coord map.
  77.     MiscCoord_V*    p2v;        // Physical -> Visual coord map.
  78.     int        def_tag;    // Default tag value.
  79.     int*        tags;        // Indexed by physical coords.
  80.     MiscPixels    min_uniform_size;// Limits for user resizing.
  81.     MiscPixels    max_uniform_size;//
  82.     MiscPixels    uniform_size;    // 0 = not uniform.
  83.     MiscPixels    min_total_size; // Min size for the entire border.
  84.     int        num_springy;    // Number of "springy" slots.
  85.     MiscTableTitleMode title_mode;
  86.     NSString**    titles;        // Indexed by physical coords.
  87.     MiscTableCellStyle def_style;
  88.     MiscTableCellStyle* styles;    // Indexed by physical coords.
  89.     id*        prototypes;    // Indexed by physical coords.
  90.     MiscCompareEntryFunc* sort_funcs;//Indexed by physical coords.
  91.     int*        sort_info;    // ((sort_type << 1) | sort_dir)
  92.     MiscSparseSet    selection;    // Selection (visual coordinates).
  93.     MiscCoord_V    selected_slot;    // Selected slot.
  94.     MiscCoord_V    cursor;        // Keyboard cursor.
  95.     MiscCoord_V    clicked_slot;    // Clicked slot.
  96.     bool        selectable;    // User can select slot-titles.
  97.     bool        sizeable;    // User can resize slots.
  98.     bool        draggable;    // User can rearrange slots.
  99.     bool        modifier_drag;    // Use modifier key to drag slots.
  100.     bool        needs_recalc;    // Offset values are "stale".
  101.  
  102.     void        global_grow( MiscPixels );
  103.     void        init_slot( MiscCoord_V, MiscCoord_P );
  104.     void         destroy_slot( MiscCoord_V, MiscCoord_P );
  105.     void        do_shift( void*, int, int, int );
  106.     void        do_shift( MiscCoord_V, MiscCoord_V );
  107.     void*        do_remap( void*, int, MiscCoord_V const* );
  108.     void        do_remap( MiscCoord_V const* );
  109.     MiscCoord_V    find_slot_for_offset( MiscPixels );
  110.  
  111.     void        perform_recalc();
  112.     void        do_recalc();
  113.     void        recalc_if_needed();
  114.     int        alloc_size( int rec_size );
  115.     void*        do_alloc( int );
  116.     void*        do_alloc_init( int );
  117.     void*        do_realloc( void*, int );
  118.     void        do_realloc();
  119.     void        do_delete( void*, int, int );
  120.     void        do_delete( MiscCoord_V, MiscCoord_P );
  121.     void        do_insert( void*, int, int );
  122.     void        do_insert( MiscCoord_V, MiscCoord_P );
  123.     void        alloc_slots();
  124.     void        alloc_vmap();
  125.     void        alloc_titles();
  126.     void        dealloc_titles();
  127.     void        alloc_tags();
  128.     void        alloc_styles();
  129.     void        alloc_prototypes();
  130.     id        new_prototype( MiscCoord_P );
  131.     void        alloc_sort_funcs();
  132.     void        alloc_sort_info();
  133.  
  134.     NSArray*    selected_slots( bool do_tags ) const;
  135.     void        select_slots( NSArray*, bool clear, bool set );
  136.     void        select_tags( NSArray*, bool clear, bool set );
  137.  
  138. public:
  139.     MiscTableBorder( MiscBorderType );
  140.     ~MiscTableBorder();
  141.  
  142.     MiscTableScroll* getOwner() const    { return owner; }
  143.     void    setOwner( MiscTableScroll* x )    { owner = x; }
  144.  
  145.     int    count() const        { return num_slots; }
  146.     int    capacity() const    { return max_slots; }
  147.     void    setCount( int );
  148.     void    setCapacity( int );
  149.     void    freeExtraCapacity();
  150.     void    empty();
  151.     void    emptyAndFree()        { empty(); freeExtraCapacity(); }
  152.     bool    goodPos( int x ) const    { return 0 <= x && x < count(); }
  153.     int    numSpringy() const    { return num_springy; }
  154.  
  155.     MiscTableSlot const* getSlots() const    { return slots; }
  156.     MiscCoord_V const* getP2VMap() const    { return p2v; }
  157.     MiscCoord_P const* getV2PMap() const    { return v2p; }
  158.     void    setSlotSizes( MiscPixels const* );
  159.     bool    setP2VMap( MiscCoord_P const* );
  160.     bool    setV2PMap( MiscCoord_V const* );
  161.     bool    good_map( int const* map, bool onescomp ) const;
  162.     void    swapSlots( MiscCoord_P, MiscCoord_P );
  163.  
  164.     MiscBorderType getType() const    { return type; }
  165.     bool    isColBorder() const    { return type == MISC_COL_BORDER; }
  166.     bool    isRowBorder() const    { return type == MISC_ROW_BORDER; }
  167.     bool    isSelectable() const    { return selectable; }
  168.     bool    isSizeable() const    { return sizeable; }
  169.     bool    isDraggable() const    { return draggable; }
  170.     bool    isModifierDrag() const    { return modifier_drag; }
  171.     bool    isUniformSize() const    { return uniform_size != 0; }
  172.     MiscPixels getMinUniformSize() const    { return min_uniform_size; }
  173.     MiscPixels getMaxUniformSize() const    { return max_uniform_size; }
  174.     MiscPixels getUniformSize() const    { return uniform_size; }
  175.     MiscTableTitleMode getTitleMode() const { return title_mode; }
  176.  
  177.     void    setType( MiscBorderType );
  178.     void    setDraggable( bool x )    { draggable = x; }
  179.     void    setModifierDrag( bool x ) { modifier_drag = x; }
  180.     void    setSelectable( bool x ) { selectable = x; }
  181.     void    setSizeable( bool x )    { sizeable = x; }
  182.     void    setMinUniformSize( MiscPixels x ) { min_uniform_size = x; }
  183.     void    setMaxUniformSize( MiscPixels x ) { max_uniform_size = x; }
  184.     bool    setUniformSize( MiscPixels );        // NOTE *1*
  185.     bool    setTitleMode( MiscTableTitleMode );    // NOTE *1*
  186.  
  187.     MiscPixels totalSize();
  188.     MiscPixels getMinTotalSize() const { return min_total_size; }
  189.     void    setMinTotalSize( MiscPixels );
  190.  
  191.     MiscCoord_P visualToPhysical( MiscCoord_V x ) const
  192.         { return (x >= 0 && v2p != 0) ? v2p[x] : (MiscCoord_P)x; }
  193.     MiscCoord_V physicalToVisual( MiscCoord_P x ) const
  194.         { return (x >= 0 && p2v != 0) ? p2v[x] : (MiscCoord_V)x; }
  195.     void visualToPhysical( MiscSparseSet const&, MiscSparseSet& ) const;
  196.     void physicalToVisual( MiscSparseSet const&, MiscSparseSet& ) const;
  197.  
  198.     MiscCoord_V visualForOffset( MiscPixels );
  199.     bool    needsRecalc() const    { return needs_recalc; }
  200.     void    recalcOffsets();
  201.  
  202.     void moveFromTo( MiscCoord_V, MiscCoord_V );
  203.     void insertAt( MiscCoord_V, MiscCoord_P );
  204.     void add()            { insertAt(count(),count()); }
  205.     void deleteAt( MiscCoord_V );
  206.     void deleteAt_P( MiscCoord_P x ){ deleteAt( physicalToVisual(x) ); }
  207.  
  208.     MiscPixels getOffset( MiscCoord_V );
  209.     MiscPixels getOffset_P( MiscCoord_P x )
  210.         { return getOffset( physicalToVisual(x) ); }
  211.     MiscPixels effectiveSize( MiscCoord_V );
  212.     MiscPixels effectiveSize_P( MiscCoord_P x )
  213.         { return effectiveSize( physicalToVisual(x) ); }
  214.     MiscPixels effectiveMinSize( MiscCoord_V );
  215.     MiscPixels effectiveMinSize_P( MiscCoord_P x )
  216.         { return effectiveMinSize( physicalToVisual(x) ); }
  217.  
  218.     MiscPixels getSize( MiscCoord_V ) const;    // target size.
  219.     MiscPixels getSize_P( MiscCoord_P x ) const
  220.         { return getSize( physicalToVisual(x) ); }
  221.     void setSize( MiscCoord_V, MiscPixels );
  222.     void setSize_P( MiscCoord_P x, MiscPixels y )
  223.         { setSize( physicalToVisual(x), y ); }
  224.  
  225.     MiscPixels getMinSize( MiscCoord_V ) const;
  226.     MiscPixels getMinSize_P( MiscCoord_P x ) const
  227.         { return getMinSize( physicalToVisual(x) ); }
  228.     void setMinSize( MiscCoord_V, MiscPixels );
  229.     void setMinSize_P( MiscCoord_P x, MiscPixels y )
  230.         { setMinSize( physicalToVisual(x), y ); }
  231.  
  232.     MiscPixels getMaxSize( MiscCoord_V ) const;
  233.     MiscPixels getMaxSize_P( MiscCoord_P x ) const
  234.         { return getMaxSize( physicalToVisual(x) ); }
  235.     void setMaxSize( MiscCoord_V, MiscPixels );
  236.     void setMaxSize_P( MiscCoord_P x, MiscPixels y )
  237.         { setMaxSize( physicalToVisual(x), y ); }
  238.  
  239.     MiscTableSizing getSizing( MiscCoord_V ) const;
  240.     MiscTableSizing getSizing_P( MiscCoord_P x ) const
  241.         { return getSizing( physicalToVisual(x) ); }
  242.     void setSizing( MiscCoord_V, MiscTableSizing );
  243.     void setSizing_P( MiscCoord_P x, MiscTableSizing y )
  244.         { setSizing( physicalToVisual(x), y ); }
  245.  
  246.     bool isSortable( MiscCoord_V x ) const
  247.         { return getSortFunc(x) != 0 ||
  248.             getSortType(x) != MISC_SORT_SKIP; }
  249.     bool isSortable_P( MiscCoord_P x ) const
  250.         { return isSortable( physicalToVisual(x) ); }
  251.  
  252.     bool isFixed( MiscCoord_V x ) const
  253.         { return ::isFixed(getSizing(x)); }
  254.     bool isFixed_P( MiscCoord_P x ) const
  255.         { return ::isFixed(getSizing_P(x)); }
  256.  
  257.     bool isSpringy( MiscCoord_V x ) const
  258.         { return ::isSpringy(getSizing(x)); }
  259.     bool isSpringy_P( MiscCoord_P x ) const
  260.         { return ::isSpringy(getSizing_P(x)); }
  261.     void setSpringy( MiscCoord_V x, bool b )
  262.         { setSizing( x, ::setSpringy(getSizing(x),b) ); }
  263.     void setSpringy_P( MiscCoord_P x, bool y )
  264.         { setSpringy( physicalToVisual(x), y ); }
  265.  
  266.     bool isSizeable( MiscCoord_V x ) const
  267.         { return ::isSizeable(getSizing(x)); }
  268.     bool isSizeable_P( MiscCoord_P x ) const
  269.         { return ::isSizeable(getSizing_P(x)); }
  270.     void setSizeable( MiscCoord_V x, bool b )
  271.         { setSizing( x, ::setSizeable(getSizing(x),b) ); }
  272.     void setSizeable_P( MiscCoord_P x, bool y )
  273.         { setSizeable( physicalToVisual(x), y ); }
  274.  
  275.     int getTag_P( MiscCoord_P ) const;
  276.     int getTag( MiscCoord_V x ) const
  277.         { return getTag_P( visualToPhysical(x) ); }
  278.     void setTag_P( MiscCoord_P, int );
  279.     void setTag( MiscCoord_V x, int y )
  280.         { setTag_P( visualToPhysical(x), y ); }
  281.  
  282.     NSString* getTitle_P( MiscCoord_P ) const;
  283.     NSString* getTitle( MiscCoord_V x ) const
  284.         { return getTitle_P( visualToPhysical(x) ); }
  285.     bool setTitle_P( MiscCoord_P, NSString* );     // NOTE *1*
  286.     bool setTitle( MiscCoord_V x, NSString* y )    // NOTE *1*
  287.         { return setTitle_P( visualToPhysical(x), y ); }
  288.  
  289.     MiscTableCellStyle getStyle_P( MiscCoord_P ) const;
  290.     MiscTableCellStyle getStyle( MiscCoord_V x ) const
  291.         { return getStyle_P( visualToPhysical(x) ); }
  292.     void setStyle_P( MiscCoord_P, MiscTableCellStyle );
  293.     void setStyle( MiscCoord_V x, MiscTableCellStyle y )
  294.         { setStyle_P( visualToPhysical(x), y ); }
  295.  
  296.     id getPrototype_P( MiscCoord_V );
  297.     id getPrototype( MiscCoord_V x )
  298.         { return getPrototype_P( visualToPhysical(x) ); }
  299.     void setPrototype_P( MiscCoord_P, id );
  300.     void setPrototype( MiscCoord_V x, id y )
  301.         { setPrototype_P( visualToPhysical(x), y ); }
  302.  
  303.     MiscCompareEntryFunc getSortFunc_P( MiscCoord_P ) const;
  304.     MiscCompareEntryFunc getSortFunc( MiscCoord_V x ) const
  305.         { return getSortFunc_P( visualToPhysical(x) ); }
  306.     void setSortFunc_P( MiscCoord_P, MiscCompareEntryFunc );
  307.     void setSortFunc( MiscCoord_V x, MiscCompareEntryFunc y )
  308.         { setSortFunc_P( visualToPhysical(x), y ); }
  309.  
  310.     MiscSortDirection getSortDirection_P( MiscCoord_P ) const;
  311.     MiscSortDirection getSortDirection( MiscCoord_V x ) const
  312.         { return getSortDirection_P( visualToPhysical(x) ); }
  313.     void setSortDirection_P( MiscCoord_P, MiscSortDirection );
  314.     void setSortDirection( MiscCoord_V x, MiscSortDirection y )
  315.         { setSortDirection_P( visualToPhysical(x), y ); }
  316.     void clearSortDirection();    // Set all slots to ascending.
  317.  
  318.     MiscSortType getSortType_P( MiscCoord_P ) const;
  319.     MiscSortType getSortType( MiscCoord_V x ) const
  320.         { return getSortType_P( visualToPhysical(x) ); }
  321.     void setSortType_P( MiscCoord_P, MiscSortType );
  322.     void setSortType( MiscCoord_V x, MiscSortType y )
  323.         { setSortType_P( visualToPhysical(x), y ); }
  324.  
  325.     // DEFAULT VALUES ARE USED TO INITIALIZE NEW SLOTS.
  326.     MiscPixels    getDefaultSize() const      { return def_slot.size; }
  327.     MiscPixels    getDefaultMinSize() const { return def_slot.min_size; }
  328.     MiscPixels    getDefaultMaxSize() const { return def_slot.min_size; }
  329.     MiscTableSizing getDefaultSizing() const  { return def_slot.sizing; }
  330.     int        getDefaultTag() const      { return def_tag; }
  331.     MiscTableCellStyle getDefaultStyle() const{ return def_style; }
  332.  
  333.     void setDefaultSize( MiscPixels x )
  334.         { needs_recalc = true; def_slot.size = x; }
  335.     void setDefaultMinSize( MiscPixels x )
  336.         { needs_recalc = true; def_slot.min_size = x; }
  337.     void setDefaultMaxSize( MiscPixels x )
  338.         { needs_recalc = true; def_slot.max_size = x; }
  339.     void setDefaultSizing( MiscTableSizing x )
  340.         { needs_recalc = true; def_slot.sizing = x; }
  341.     void setDefaultTag( int x )            { def_tag = x; }
  342.     void setDefaultStyle( MiscTableCellStyle x )    { def_style = x; }
  343.  
  344.     // SELECTION
  345.     MiscSparseSet const& selectionSet() const    { return selection; }
  346.     bool hasSelection() const    { return !selection.isEmpty(); }
  347.     bool hasMultipleSelection() const;
  348.     unsigned int numSelected() const { return selection.count(); }
  349.     MiscCoord_V selectedSlot() const { return selected_slot; }
  350.     void clearSelectedSlot()    { selected_slot = -1; }
  351.     void setSelectedSlot( MiscCoord_V x )
  352.                     { selected_slot = selection.coerce(x);}
  353.     void fixSelectedSlot()        { setSelectedSlot(selectedSlot()); }
  354.  
  355.     bool isSelected( MiscCoord_V x ) const { return selection.contains(x);}
  356.     void toggle( MiscCoord_V x ) {selection.toggle(x); setSelectedSlot(x);}
  357.     void select( MiscCoord_V x ) {selection.add(x); setSelectedSlot(x);}
  358.     void select( MiscCoord_V lo, MiscCoord_V hi )
  359.                 { selection.add(lo,hi); setSelectedSlot(hi); }
  360.     void unselect( MiscCoord_V x )
  361.                 { selection.remove(x); fixSelectedSlot(); }
  362.     void unselect( MiscCoord_V lo, MiscCoord_V hi )
  363.                 { selection.remove(lo,hi); fixSelectedSlot(); }
  364.  
  365.     void selectOne( MiscCoord_V x ) { selection.empty(); select(x); }
  366.     void selectAll() { if (count() > 0) { selection.add( 0, count() - 1 );
  367.                 setSelectedSlot( count() - 1 ); } }
  368.     void selectNone() { selection.empty(); clearSelectedSlot(); }
  369.     void selectSlots( NSArray* l, bool extend )    // Physical coords.
  370.                 { select_slots( l, !extend, true ); }
  371.     NSArray* selectedSlots() const { return selected_slots( false ); }
  372.     void selectTags( NSArray* l, bool extend )
  373.                 { select_tags( l, !extend, true ); }
  374.     NSArray* selectedTags() const { return selected_slots( true ); }
  375.  
  376.     void unselectSlots( NSArray* l )    // Physical coords.
  377.                 { select_slots( l, false, false ); }
  378.     void unselectTags( NSArray* l ) { select_tags( l, false, false ); }
  379.  
  380.     // KEYBOARD CURSOR
  381.     MiscCoord_V getCursor() const { return cursor; }
  382.     void setCursor( MiscCoord_V c )
  383.         { if (c >= 0 && c < count()) cursor = c; }
  384.     void clearCursor() { setCursor(-1); }
  385.     bool hasValidCursor() const { return cursor >= 0 && cursor < count(); }
  386.  
  387.  
  388.     // CLICKED SLOT
  389.     MiscCoord_V clickedSlot() const { return clicked_slot; }
  390.     void setClickedSlot( MiscCoord_V s ) { clicked_slot = s; }
  391.     void clearClickedSlot() { setClickedSlot(-1); }
  392.  
  393.  
  394.     // PHYSICAL-COORDINATE CONVENIENCE METHODS.
  395.  
  396.     MiscCoord_P selectedSlot_P() const
  397.         { return visualToPhysical(selected_slot); }
  398.     void setSelectedSlot_P( MiscCoord_P x )
  399.         { setSelectedSlot( physicalToVisual(x) ); }
  400.     void toggle_P( MiscCoord_P x )    { toggle( physicalToVisual(x) ); }
  401.     void select_P( MiscCoord_P x )    { select( physicalToVisual(x) ); }
  402.     void unselect_P( MiscCoord_P x ){ unselect( physicalToVisual(x) ); }
  403.     bool isSelected_P( MiscCoord_P x ) const
  404.         { return isSelected( physicalToVisual(x) ); }
  405.     void selectOne_P( MiscCoord_P x ) { selectOne(physicalToVisual(x)); }
  406.  
  407.     void setCursor_P( MiscCoord_P x ) { setCursor(physicalToVisual(x)); }
  408.     MiscCoord_P getCursor_P() const
  409.         { return visualToPhysical(getCursor()); }
  410.  
  411.     MiscCoord_P clickedSlot_P() const
  412.         { return visualToPhysical( clickedSlot() ); }
  413.     void setClickedSlot_P( MiscCoord_P x )
  414.         { setClickedSlot( physicalToVisual(x) ); }
  415.  
  416.     // ARCHIVING SUPPORT.
  417.     void encodeWithCoder( NSCoder* );
  418.     void initWithCoder( NSCoder*, int ver );
  419.     void initWithCoder_v1( NSCoder*, int ver );
  420.     void initWithCoder_v2( NSCoder*, int ver );
  421.     };
  422.  
  423. #endif // __MiscTableBorder_h
  424.