home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / mac / UserInterface / UTearOffPalette.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  10.4 KB  |  315 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  20. //  author: atotic
  21. // ╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤╤
  22. #pragma once
  23.  
  24. /************************************************************************
  25.  Floating palette classes
  26.  
  27.  Implements tearable tool palletes
  28.  
  29.  ### Standard usage:
  30.  
  31.      Include UTearOffPalette.r in your project for floating window templates
  32.      
  33.      Initialization
  34.         call InitUTearOffPalette(your-application)    on startup
  35.  
  36.     To create an instance of a palette:
  37.         Create a stand-alone PPob that contains the palette as it appears in the floating
  38.         window and one as it appears imbedded in place. These may be the same PPob If you
  39.         want special shading in the palette, do it in its topmost view
  40.  
  41.         In the window containing the palette:
  42.             a) create CTearOffBar, empty view and of the same size as stand-alone palette.
  43.                 CTearOffBar contains ID of the stand-alone and imbedded palettes
  44.             b) in window::FinishCreateSelf, add window as AddListener to CTearOffBar
  45.  
  46.             c) inside window, handle the palette messages:
  47.             virtual void        ListenToMessage(MessageT inMessage, void *ioParam)
  48.             {
  49.                 switch (inMessage)
  50.                 {
  51.                     case msg_SetTearOffPalette:
  52.                         // Link in the palette controls
  53.                         LView * newView = (LView*)ioParam;
  54.                         if (newView->GetPaneID() == p1_ID)
  55.                             fP1 = newView;
  56.                         RefreshControls();
  57.                         break;
  58.  
  59.                     case msg_RemoveTearOffPalette:
  60.                         // Unlink the palette controls
  61.                         PaneIDT paneID = (PaneIDT)ioParam;
  62.                         if (paneID == p1_ID)
  63.                             fP1 = NULL;
  64.                         break;
  65.                 }
  66.             }
  67.  
  68.             d) since your window will not always have controls linked up,
  69.              make sure that you can deal with your control panes being NULL.
  70.  
  71.             e) If you want to reposition views in response to palette being torn off,
  72.              subclass CTearOffBar and override ShowPalette and HidePalette methods.
  73.              You can also check set the fAutoResize flag in CTearOffBar, and view will
  74.              shrink to 0,0 when palette is torn out. 
  75.             
  76.             f) When frontmost, your window will get broadcasted messages from 
  77.                 broadcasters in the palette
  78.  
  79.             g) The manager can be used as public message relay. It is both an
  80.                 LListener and an LBroadcaster. It generates no messages of it's
  81.                 own, but passes on every message that it receives.
  82.  
  83.  ### Design notes:
  84.      CTearOffBar (FBar) and TearOffWindow (FWindow)
  85.      do not know anything about each other, communications happens through
  86.      CTearOffManager (FManager).
  87.      
  88.      FBar and its listener (usually the container window), register with FManager.
  89.      FBar & FWindow broadcast all their messages through the manager, 
  90.      and manager dispatches them.
  91.  
  92.       Since FWindow is always created from a single PPob template, we do not put
  93.       any palette customization flags in the window custom template. All flags
  94.       are in the palette PPob. This causes some things to be initialized in 
  95.       weird ways (fVisibility, fTransparentPane).
  96.       
  97.       More to come...
  98.       
  99.  ************************************************************************/
  100.  
  101. // We are reusing mail menu commands
  102. const MessageT msg_SetTearOffPalette = 500;   // ioParam is topmost palette view
  103. const MessageT msg_RemoveTearOffPalette = 501;    // ioParam is the view ID of the topmost palette view
  104.  
  105. #include <LView.h>
  106. #include <LBroadcaster.h>
  107. #include <LListener.h>
  108. #include <LComparator.h>
  109.  
  110.  
  111. /************************************************************************
  112.  * Initialization function for the whole group
  113.  ************************************************************************/
  114. void InitUTearOffPalette(LCommander * defaultCommander);
  115.  
  116. class CTearOffManager;
  117. class CTearOffWindow;
  118. class LArray;
  119.  
  120. #pragma mark CTearOffBar
  121.  
  122. /************************************************************************
  123.  * class CTearOffBar
  124.  ************************************************************************/
  125. class CTearOffBar : public LView,
  126.                         public LBroadcaster,    // Broadcasts messages to window
  127.                         public LListener        // Listens to messages 
  128. {
  129.     friend class CTearOffManager;
  130.     public:
  131.         enum { class_ID = 'DRPL' };
  132.         
  133. // Constructors
  134.  
  135.                             CTearOffBar(LStream* inStream);
  136.  
  137.         virtual                ~CTearOffBar();
  138.         virtual void        FinishCreateSelf();
  139.  
  140.  
  141. // Event handling
  142.         
  143.         virtual void        Click( SMouseDownEvent    &inMouseDown );
  144.         virtual void        ClickSelf(const SMouseDownEvent    &inMouseDown);
  145.         virtual    void        AddListener(LListener    *inListener);
  146.         virtual void        ListenToMessage(MessageT inMessage, void *ioParam);
  147.         virtual void        ActivateSelf();
  148.         virtual void        DeactivateSelf();
  149.  
  150. // Floater interface
  151.         virtual void        HidePalette();    // If you are overriding, beware that HidePalette can be
  152.                                             // called on an already hiden palette
  153.         virtual void        ShowPalette();    // Also, ShowPalette can be called on a shown palette
  154.  
  155.         Boolean                IsShown() {return fShown;}
  156.         void                SetShown(Boolean state) {fShown = state;}
  157.         Boolean                IsFloaterOnSide() {return fSideTitle;}
  158.         Boolean                DisablePaletteWindow() {return fDisablePaletteWithWindow;}
  159.     private:
  160.         ResIDT                fFloatingSubpaneID;    // View to be loaded
  161.                             // when the floating window is shown
  162.         ResIDT                fInPlaceSubpaneID;    // View to be loaded
  163.                             // when the imbedded pallete is shown
  164.  
  165.         Boolean                fAutoResize; // Set view width/height to 0,0 on hide
  166.                                         // Resize to original width on show
  167.         SDimension16        fOriginalFrameSize;    // Original view size 
  168.         Boolean                fShown;
  169.         UInt8                fVisibility;    // 0 -- always, 
  170.                                             // 1 -- if at least 1 window exists, 
  171.                                             // 2 -- if window is in front
  172.         Str255                fTitle;
  173.         
  174.         Boolean                fSideTitle;    // title bar of the floating window is either
  175.                                         // horizontal or vertical
  176.     protected:
  177.         PaneIDT                fTransparentPane;    // The pane that gets no clicks,
  178.                                     // it is the top pane of the palette
  179.         Boolean                fAllowClickDrag;    // If this is true, then the window
  180.                                                 // is created even if the user just
  181.                                                 // clicks and doesn't drag.
  182.         Boolean                fDisablePaletteWithWindow;    // If this is true then the
  183.                                                         // palette is disabled if the
  184.                                                         // front most window does not
  185.                                                         // have the corresponding button
  186.                                                         // bar.
  187.                                                         // If this is false then the
  188.                                                         // palette will always work.
  189. };
  190.  
  191.  
  192. #pragma mark CTearOffManager
  193. /************************************************************************
  194.  * class CTearOffManager
  195.  * One-instance class
  196.  ************************************************************************/
  197. class CTearOffManager: public LListener, public LBroadcaster
  198. {
  199.     friend void InitUTearOffPalette(LCommander * defCommander);
  200.  
  201. private:
  202.                             CTearOffManager(LCommander * defaultCommander);
  203.  
  204.                             ~CTearOffManager();
  205.  
  206.     static CTearOffManager *  sDefaultFloatManager;        
  207.  
  208. public:
  209.  
  210.     static CTearOffManager * GetDefaultManager() {return sDefaultFloatManager;}
  211.  
  212.     
  213.     virtual void    ListenToMessage(
  214.                             MessageT        inMessage,
  215.                             void            *ioParam);
  216. // CTearOffBar interface
  217.     
  218.     // registration
  219.                         
  220.     void                    RegisterBar(CTearOffBar * inBar);
  221.  
  222.     void                    UnregisterBar(CTearOffBar * inBar);
  223.     
  224.     // messaging
  225.     
  226.     void                    RegisterListener(CTearOffBar * inBar, 
  227.                                             LListener * listener);
  228.     
  229.     void                    BarReceivedMessage(CTearOffBar * inBar,
  230.                                             MessageT inMessage,
  231.                                              void *ioParam);
  232.  
  233.     // dragging
  234.     
  235.     void                    DragBar( CTearOffBar * inBar,
  236.                                 Int16 inH,
  237.                                 Int16 inV,
  238.                                 Boolean usePosition);
  239.  
  240.     // activation
  241.                     
  242.     void                    BarActivated( CTearOffBar * inBar);
  243.     
  244.     void                    BarDeactivated( CTearOffBar * inBar);
  245.  
  246. // CFloatPalleteWindow interface
  247.  
  248.     void                    UnregisterWindow(CTearOffWindow * inWin);
  249.     
  250.     void                    WindowReceivedMessage(CTearOffWindow * inWin,
  251.                                             MessageT inMessage,
  252.                                              void *ioParam);
  253.     void                    CloseFloatingWindow(ResIDT inWinID);
  254.     void                    OpenFloatingWindow(    ResIDT inWinID,
  255.                                                 Point inTopLeft,
  256.                                                 Boolean inSideways,
  257.                                                 UInt8 visibility,
  258.                                                 Str255 &title);
  259.     Boolean                    IsFloatingWindow(ResIDT inWinID);
  260.  
  261. private:
  262.     
  263.     CTearOffWindow *    AssertBarWindowExists(CTearOffBar * inBar);
  264.     CTearOffWindow *    AssertBarWindowExists(    ResIDT inWinID,
  265.                                                 Boolean inSideways,
  266.                                                 UInt8 visibility,
  267.                                                 Str255 &title);
  268.     
  269.     void                    SetBarsView( CTearOffBar * inBar,
  270.                                         LView * view);
  271.  
  272.     void                    RemoveBarsView( CTearOffBar * inBar, PaneIDT resID);
  273.  
  274. private:
  275.     LCommander *        fDefaultCommander;    // Default commander for floaters, usually the app
  276.     LArray *            fPaletteBars;        // list of ButtonBarRecords
  277.     LArray *            fPaletteWindows;    // list of FloatWindowRecords
  278.  
  279.     // Compare bars for search -- should really be templated
  280.     class CPaletteBarComparator    : public LComparator
  281.     {
  282.         public:
  283.                             CPaletteBarComparator(){}
  284.             virtual            ~CPaletteBarComparator(){}
  285.             Int32            Compare(const void*        inItemOne,
  286.                                     const void*        inItemTwo,
  287.                                     Uint32            inSizeOne,
  288.                                     Uint32            inSizeTwo) const;
  289.             Int32            CompareToKey(const void*            inItem,
  290.                                 Uint32                inSize,
  291.                                 const void*            inKey) const
  292.                     {return Compare(inItem, inKey, inSize, inSize);}
  293.     };
  294.  
  295.     // Compare windows for search -- template this?
  296.     class CPaletteWindowComparator    : public LComparator
  297.     {
  298.         public:
  299.                             CPaletteWindowComparator(){}
  300.             virtual            ~CPaletteWindowComparator(){}
  301.             Int32            Compare(const void*        inItemOne,
  302.                                     const void*        inItemTwo,
  303.                                     Uint32            inSizeOne,
  304.                                     Uint32            inSizeTwo) const;
  305.             Int32            CompareToKey(const void*            inItem,
  306.                                 Uint32                inSize,
  307.                                 const void*            inKey) const
  308.                             {return Compare(inItem, inKey, inSize, inSize);}
  309.     };
  310.  
  311.     CPaletteBarComparator         fBarCompare;
  312.     CPaletteWindowComparator     fWindowCompare;
  313.     
  314. };
  315.