home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Palettes / MiscTableScroll / MiscTableBorder.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-11  |  15.7 KB  |  390 lines

  1. #ifndef __MiscTableBorder_h
  2. #define __MiscTableBorder_h
  3. #ifdef __GNUC__
  4. # pragma interface
  5. #endif
  6. //=============================================================================
  7. //
  8. //        Copyright (C) 1995, 1996 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" slots get adjusted when the total size exceeds the global
  31. //        min/max size.
  32. //
  33. //        NOTE *1* These methods return true if the new value is different than
  34. //        the old value, and the display needs updating.
  35. //
  36. // MAINTENANCE NOTE:
  37. //        Consider breaking data_size out of slots into a separate array of
  38. //        its own.
  39. //-----------------------------------------------------------------------------
  40. //-----------------------------------------------------------------------------
  41. // $Id: MiscTableBorder.h,v 1.6 96/01/13 23:39:33 zarnuk Exp $
  42. // $Log:        MiscTableBorder.h,v $
  43. //    Revision 1.6  96/01/13    23:39:33  zarnuk
  44. //    MiscCompareCellFunc -> MiscCompareEntryFunc.
  45. //    
  46. //    Revision 1.5  95/11/12    23:19:53  sunshine
  47. //    Fixed bugs where selection manipulation methods were not checking for valid
  48. //    slot.  Fixes bug where selectedSlot() was returning "random" and possibly
  49. //    out-of-range values.
  50. //    
  51. //    Revision 1.4  95/10/08    16:13:14  zarnuk
  52. //    Cell* --> id.
  53. //    
  54. //    Revision 1.3  95/10/04    17:56:48  zarnuk
  55. //    Added do_remap().
  56. //-----------------------------------------------------------------------------
  57. #include <misckit/MiscTableScroll.h>
  58. #include "MiscSparseSet.h"
  59. #include "MiscTableUtil.h"
  60.  
  61. extern "Objective-C" {
  62. #import <objc/Object.h>
  63. #import <objc/typedstream.h>
  64. }
  65.  
  66. @class MiscTableScroll;
  67. @class Cell;
  68.  
  69. struct MiscTableSlot
  70.         {
  71.         MiscPixels        offset;            // Offset of this slot.
  72.         MiscPixels        adj_size;        // Size adjusted for springy-ness.
  73.         MiscPixels        data_size;        // Current size of data.
  74.         MiscPixels        size;            // Target size.
  75.         MiscPixels        min_size;        // Minimum size.
  76.         MiscPixels        max_size;        // Maximum size.
  77.         MiscTableSizing sizing;            // Sizing options.
  78.         bool isSpringy() const    { return ::isSpringy(sizing); }
  79.         bool isData() const        { return ::isData(sizing); }
  80.         void read( NXTypedStream* );
  81.         void write( NXTypedStream* );
  82.         };
  83.  
  84.  
  85. class MiscTableBorder
  86.         {
  87. private:
  88.         MiscBorderType    type;            // Row / Col.
  89.         MiscTableScroll* owner;
  90.         MiscTableSlot    def_slot;        // Default slot configuration.
  91.         int                max_slots;        // Capacity.  Allocation size of arrays.
  92.         int                num_slots;        // Active number of rows/cols.
  93.         MiscTableSlot*    slots;            // Indexed by visual coords.
  94.         MiscCoord_P*    v2p;            // Visual -> Physical coord map.
  95.         MiscCoord_V*    p2v;            // Physical -> Visual coord map.
  96.         int                def_tag;        // Default tag value.
  97.         int*            tags;            // Indexed by physical coords.
  98.         MiscPixels        uniform_size;    // 0 = not uniform.
  99.         MiscPixels        min_total_size; // Min size for the entire border.
  100.         MiscPixels        max_total_size; // Max size for the entire border.
  101.         int                num_springy;    // Number of "springy" slots.
  102.         MiscTableTitleMode title_mode;
  103.         char**            titles;            // Indexed by visual coords.
  104.         MiscTableCellStyle def_style;
  105.         MiscTableCellStyle* styles;
  106.         id*                prototypes;
  107.         MiscCompareEntryFunc* sort_funcs;
  108.         MiscSortDirection* sort_dirs;
  109.         MiscSortType*    sort_types;
  110.         MiscSparseSet    selection;        // Selection (visual coordinates).
  111.         MiscCoord_V        cursor;            // Keyboard cursor.
  112.         bool            selectable;        // User can select slot-titles.
  113.         bool            sizeable;        // User can resize slots.
  114.         bool            draggable;        // User can rearrange slots.
  115.         bool            modifier_drag;    // Use modifier key to drag slots.
  116.         bool            needs_recalc;    // Offset values are "stale".
  117.  
  118.         int                shrink_mode( MiscTableSlot& );
  119.         void            global_shrink( MiscPixels );
  120.         MiscCoord_V        find_springy_slot() const;
  121.         void            global_grow( MiscPixels );
  122.         void            init_slot( MiscCoord_V, MiscCoord_P );
  123.         void            destroy_slot( MiscCoord_V );
  124.         void            do_shift( void*, int, int, int );
  125.         void            do_shift( MiscCoord_V, MiscCoord_V );
  126.         void*            do_remap( void*, int, MiscCoord_V const* );
  127.         void            do_remap( MiscCoord_V const* );
  128.         MiscCoord_V        find_slot_for_offset( MiscPixels );
  129.  
  130.         void            perform_recalc();
  131.         void            do_recalc();
  132.         void            recalc_if_needed();
  133.         int                alloc_size( int rec_size );
  134.         void*            do_alloc( int );
  135.         void*            do_alloc_init( int );
  136.         void*            do_realloc( void*, int );
  137.         void            do_realloc();
  138.         void            do_delete( void*, int, int );
  139.         void            do_delete( MiscCoord_V, MiscCoord_P );
  140.         void            do_insert( void*, int, int );
  141.         void            do_insert( MiscCoord_V, MiscCoord_P );
  142.         void            alloc_slots();
  143.         void            alloc_vmap();
  144.         void            alloc_titles();
  145.         void            dealloc_titles();
  146.         void            alloc_tags();
  147.         void            alloc_styles();
  148.         void            alloc_prototypes();
  149.         id                new_prototype( MiscCoord_V );
  150.         void            alloc_sort_funcs();
  151.         void            alloc_sort_dirs();
  152.         void            alloc_sort_types();
  153.  
  154.         char const*        get_title( MiscCoord_V ) const;
  155.         id                get_prototype( MiscCoord_V ) const;
  156.  
  157.         void            selected_slots(MiscIntList* list, bool do_tags) const;
  158.         bool            good_int_map( int const* map ) const;
  159.  
  160. public:
  161.         MiscTableBorder( MiscBorderType );
  162.         ~MiscTableBorder();
  163.  
  164.         MiscTableScroll* getOwner() const        { return owner; }
  165.         void            setOwner( MiscTableScroll* );
  166.  
  167.         int                count() const            { return num_slots; }
  168.         int                capacity() const        { return max_slots; }
  169.         void            setCount( int );
  170.         void            setCapacity( int );
  171.         void            freeExtraCapacity();
  172.         void            empty();
  173.         void            emptyAndFree()    { empty(); freeExtraCapacity(); }
  174.  
  175.         void            deleteAt( MiscCoord_V );
  176.         void            insertAt( MiscCoord_V, MiscCoord_P );
  177.         void            add() { insertAt(count(),count()); }
  178.         void            moveFromTo( MiscCoord_V, MiscCoord_V );
  179.  
  180.         MiscSparseSet& selectionSet() { return selection; }
  181.         MiscTableSlot const* getSlots() const    { return slots; }
  182.         MiscCoord_V const* getVMap() const        { return p2v; }
  183.         MiscCoord_P const* getPMap() const        { return v2p; }
  184.         void            setSlotSizes( MiscPixels const* );
  185.         bool            goodVMap( MiscCoord_P const* map ) const;
  186.         bool            goodPMap( MiscCoord_P const* map ) const;
  187.         bool            setVMap( MiscCoord_P const* );
  188.         bool            setPMap( MiscCoord_V const* );
  189.         void            clearVMap();
  190.  
  191.         MiscBorderType    getType() const            { return type; }
  192.         bool    isColBorder() const        { return type == MISC_COL_BORDER; }
  193.         bool    isRowBorder() const        { return type == MISC_ROW_BORDER; }
  194.         bool            isSelectable() const    { return selectable; }
  195.         bool            isSizeable() const        { return sizeable; }
  196.         bool            isDraggable() const        { return draggable; }
  197.         bool            isModifierDrag() const    { return modifier_drag; }
  198.         bool            isUniformSize() const    { return uniform_size != 0; }
  199.         MiscPixels        getUniformSize() const    { return uniform_size; }
  200.         MiscTableTitleMode getTitleMode() const { return title_mode; }
  201.         bool            needsRecalc() const        { return needs_recalc; }
  202.  
  203.         void            setType( MiscBorderType );
  204.         void            setDraggable( bool x )    { draggable = x; }
  205.         void            setModifierDrag( bool x ) { modifier_drag = x; }
  206.         void            setSelectable( bool x ) { selectable = x; }
  207.         void            setSizeable( bool x )    { sizeable = x; }
  208.         bool            setUniformSize( MiscPixels );            // NOTE *1*
  209.         bool            setTitleMode( MiscTableTitleMode );        // NOTE *1*
  210.  
  211.         MiscPixels        totalSize();
  212.         MiscPixels        getMinTotalSize() const { return min_total_size; }
  213.         MiscPixels        getMaxTotalSize() const { return max_total_size; }
  214.         void            setMinTotalSize( MiscPixels );
  215.         void            setMaxTotalSize( MiscPixels );
  216.  
  217.         MiscCoord_P        visualToPhysical( MiscCoord_V x ) const
  218.                         { return (v2p != 0) ? v2p[x] : (MiscCoord_P)x; }
  219.         MiscCoord_V        physicalToVisual( MiscCoord_P x ) const
  220.                         { return (p2v != 0) ? p2v[x] : (MiscCoord_V)x; }
  221.         MiscCoord_V        visualForOffset( MiscPixels );
  222.         void            recalcOffsets();
  223.  
  224.         MiscPixels        getOffset( MiscCoord_V );
  225.         MiscPixels        getSize( MiscCoord_V ) const;            // target size.
  226.         MiscPixels        getMinSize( MiscCoord_V ) const;
  227.         MiscPixels        getMaxSize( MiscCoord_V ) const;
  228.         MiscPixels        getDataSize( MiscCoord_V ) const;
  229.         MiscPixels        effectiveSize( MiscCoord_V );            // adj_size.
  230.         MiscPixels        effectiveMaxSize( MiscCoord_V );        // With global
  231.         MiscPixels        effectiveMinSize( MiscCoord_V );        // limts.
  232.         char const*        getTitle( MiscCoord_V ) const;
  233.         int                getTag( MiscCoord_V ) const;
  234.         MiscTableCellStyle getStyle( MiscCoord_V ) const;
  235.         id                getPrototype( MiscCoord_V );
  236.         MiscCompareEntryFunc getSortFunc( MiscCoord_V ) const;
  237.         MiscSortDirection getSortDirection( MiscCoord_V ) const;
  238.         MiscSortType    getSortType( MiscCoord_V ) const;
  239.         MiscTableSizing getSizing( MiscCoord_V ) const;
  240.         bool            isFixed( MiscCoord_V x ) const
  241.                                 { return ::isFixed(getSizing(x)); }
  242.         bool            isSpringy( MiscCoord_V x ) const
  243.                                 { return ::isSpringy(getSizing(x)); }
  244.         bool            isSizeable( MiscCoord_V x ) const
  245.                                 { return ::isSizeable(getSizing(x)); }
  246.         bool            isData( MiscCoord_V x ) const
  247.                                 { return ::isData(getSizing(x)); }
  248.  
  249.         void            setSize( MiscCoord_V, MiscPixels );
  250.         void            setMinSize( MiscCoord_V, MiscPixels );
  251.         void            setMaxSize( MiscCoord_V, MiscPixels );
  252.         void            setDataSize( MiscCoord_V, MiscPixels );
  253.         bool            setTitle( MiscCoord_V, char const* );    // NOTE *1*
  254.         void            setTag( MiscCoord_V, int );
  255.         void            setStyle( MiscCoord_V, MiscTableCellStyle );
  256.         void            setPrototype( MiscCoord_V, id );
  257.         void            setSortFunc( MiscCoord_V, MiscCompareEntryFunc );
  258.         void            setSortDirection( MiscCoord_V, MiscSortDirection );
  259.         void            setSortType( MiscCoord_V, MiscSortType );
  260.         void            setSizing( MiscCoord_V, MiscTableSizing );
  261.         void            setSpringy( MiscCoord_V x, bool b )
  262.                 { setSizing( x, ::setSpringy(getSizing(x),b) ); }
  263.         void            setSizeable( MiscCoord_V x, bool b )
  264.                 { setSizing( x, ::setSizeable(getSizing(x),b) ); }
  265.         void            setData( MiscCoord_V x, bool b )
  266.                 { setSizing( x, ::setData(getSizing(x),b) ); }
  267.  
  268.         // Default values are used to initialize new slots.
  269.         MiscPixels        getDefaultSize() const      { return def_slot.size; }
  270.         MiscPixels        getDefaultMinSize() const { return def_slot.min_size; }
  271.         MiscPixels        getDefaultMaxSize() const { return def_slot.min_size; }
  272.         MiscTableSizing getDefaultSizing() const  { return def_slot.sizing; }
  273.         int                getDefaultTag() const      { return def_tag; }
  274.         MiscTableCellStyle getDefaultStyle() const{ return def_style; }
  275.  
  276.         void            setDefaultSize( MiscPixels x )
  277.                                 { needs_recalc = true; def_slot.size = x; }
  278.         void            setDefaultMinSize( MiscPixels x )
  279.                                 { needs_recalc = true; def_slot.min_size = x; }
  280.         void            setDefaultMaxSize( MiscPixels x )
  281.                                 { needs_recalc = true; def_slot.max_size = x; }
  282.         void            setDefaultSizing( MiscTableSizing x )
  283.                                 { needs_recalc = true; def_slot.sizing = x; }
  284.         void            setDefaultTag( int x ) { def_tag = x; }
  285.         void            setDefaultStyle( MiscTableCellStyle x )
  286.                                 { def_style = x; }
  287.  
  288.         // Programmatic selection interface using physical-cooridnates.
  289.         bool hasSelection() const { return !selection.isEmpty(); }
  290.         bool hasMultipleSelection() const;
  291.         unsigned int numSelected() const { return selection.count(); }
  292.         bool slotIsSelected( MiscCoord_P s ) const;
  293.         MiscCoord_P selectedSlot() const;
  294.         void selectedSlots( MiscIntList* l ) const { selected_slots(l,false); }
  295.         void selectedTags( MiscIntList* l ) const { selected_slots(l,true); }
  296.         void selectSlot( MiscCoord_P s );
  297.         void selectSlots( MiscIntList* );
  298.         void selectTags( MiscIntList* );
  299.         void selectAll() { if (count() > 0) selection.add( 0, count() - 1 ); }
  300.         void selectNone() { selection.empty(); }
  301.  
  302.         // Programmatic keyboard cursor interface using visual-cooridnates.
  303.         MiscCoord_V getCursor() const { return cursor; }
  304.         void setCursor( MiscCoord_V c )
  305.                 { if (c >= 0 && c < count()) cursor = c; }
  306.         void clearCursor() { setCursor(-1); }
  307.         bool hasValidCursor() const { return cursor >= 0 && cursor < count(); }
  308.  
  309.         // Physical-coordinate convenience methods.
  310.         void deleteAt_P( MiscCoord_P x )
  311.                 { deleteAt( physicalToVisual(x) ); }
  312.         MiscPixels getOffset_P( MiscCoord_P x )
  313.                 { return getOffset( physicalToVisual(x) ); }
  314.         MiscPixels effectiveSize_P( MiscCoord_P x )
  315.                 { return effectiveSize( physicalToVisual(x) ); }
  316.         MiscPixels getSize_P( MiscCoord_P x ) const
  317.                 { return getSize( physicalToVisual(x) ); }
  318.         MiscPixels getMinSize_P( MiscCoord_P x ) const
  319.                 { return getMinSize( physicalToVisual(x) ); }
  320.         MiscPixels getMaxSize_P( MiscCoord_P x ) const
  321.                 { return getMaxSize( physicalToVisual(x) ); }
  322.         MiscPixels getDataSize_P( MiscCoord_P x ) const
  323.                 { return getDataSize( physicalToVisual(x) ); }
  324.         char const* getTitle_P( MiscCoord_P x ) const
  325.                 { return getTitle( physicalToVisual(x) ); }
  326.         int getTag_P( MiscCoord_P x ) const
  327.                 { return getTag( physicalToVisual(x) ); }
  328.         MiscTableCellStyle getStyle_P( MiscCoord_P x ) const
  329.                 { return getStyle( physicalToVisual(x) ); }
  330.         id getPrototype_P( MiscCoord_V x )
  331.                 { return getPrototype( physicalToVisual(x) ); }
  332.         MiscCompareEntryFunc getSortFunc_P( MiscCoord_V x ) const
  333.                 { return getSortFunc( physicalToVisual(x) ); }
  334.         MiscSortDirection getSortDirection_P( MiscCoord_V x ) const
  335.                 { return getSortDirection( physicalToVisual(x) ); }
  336.         MiscSortType getSortType_P( MiscCoord_V x ) const
  337.                 { return getSortType( physicalToVisual(x) ); }
  338.         MiscTableSizing getSizing_P( MiscCoord_P x ) const
  339.                 { return getSizing( physicalToVisual(x) ); }
  340.         bool isFixed_P( MiscCoord_P x ) const
  341.                 { return ::isFixed(getSizing_P(x)); }
  342.         bool isSpringy_P( MiscCoord_P x ) const
  343.                 { return ::isSpringy(getSizing_P(x)); }
  344.         bool isSizeable_P( MiscCoord_P x ) const
  345.                 { return ::isSizeable(getSizing_P(x)); }
  346.         bool isData_P( MiscCoord_P x ) const
  347.                 { return ::isData(getSizing_P(x)); }
  348.  
  349.         void setSize_P( MiscCoord_P x, MiscPixels y )
  350.                 { setSize( physicalToVisual(x), y ); }
  351.         void setMinSize_P( MiscCoord_P x, MiscPixels y )
  352.                 { setMinSize( physicalToVisual(x), y ); }
  353.         void setMaxSize_P( MiscCoord_P x, MiscPixels y )
  354.                 { setMaxSize( physicalToVisual(x), y ); }
  355.         void setDataSize_P( MiscCoord_P x, MiscPixels y )
  356.                 { setDataSize( physicalToVisual(x), y ); }
  357.         bool setTitle_P( MiscCoord_P x, char const* y )
  358.                 { return setTitle( physicalToVisual(x), y ); }
  359.         void setTag_P( MiscCoord_P x, int y )
  360.                 { setTag( physicalToVisual(x), y ); }
  361.         void setStyle_P( MiscCoord_P x, MiscTableCellStyle y )
  362.                 { setStyle( physicalToVisual(x), y ); }
  363.         void setPrototype_P( MiscCoord_P x, id y )
  364.                 { setPrototype( physicalToVisual(x), y ); }
  365.         void setSortFunc_P( MiscCoord_P x, MiscCompareEntryFunc y )
  366.                 { setSortFunc( physicalToVisual(x), y ); }
  367.         void setSortDirection_P( MiscCoord_P x, MiscSortDirection y )
  368.                 { setSortDirection( physicalToVisual(x), y ); }
  369.         void setSortType_P( MiscCoord_P x, MiscSortType y )
  370.                 { setSortType( physicalToVisual(x), y ); }
  371.         void setSizing_P( MiscCoord_P x, MiscTableSizing y )
  372.                 { setSizing( physicalToVisual(x), y ); }
  373.         void setSpringy_P( MiscCoord_P x, bool y )
  374.                 { setSpringy( physicalToVisual(x), y ); }
  375.         void setSizeable_P( MiscCoord_P x, bool y )
  376.                 { setSizeable( physicalToVisual(x), y ); }
  377.         void setData_P( MiscCoord_P x, bool y )
  378.                 { setData( physicalToVisual(x), y ); }
  379.  
  380.         void setCursor_P( MiscCoord_P c ) { setCursor(physicalToVisual(c)); }
  381.         MiscCoord_P getCursor_P() const
  382.                 { return visualToPhysical(getCursor()); }
  383.  
  384.         // Archiving support.
  385.         void write( NXTypedStream* );
  386.         void read( NXTypedStream* );
  387.         };
  388.  
  389. #endif // __MiscTableBorder_h
  390.