home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / utility / MPreference.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.4 KB  |  122 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. #pragma once
  20.  
  21. class LControl;
  22. class LStream;
  23.  
  24. //======================================
  25. class MPreferenceBase
  26. //======================================
  27. {
  28. protected:
  29.                     
  30.                         MPreferenceBase(
  31.                             LPane* inPane,
  32.                             LStream* inStream);
  33.     virtual                ~MPreferenceBase();
  34. public:
  35.     Boolean                IsLocked() const { return mLocked; }
  36.     virtual void        ReadDefaultSelf() = 0; // Reset factory default
  37.     virtual Boolean        Changed() const = 0;
  38.     virtual void        SetPrefName(const char* inNewName, Boolean inReread = true);
  39.     virtual const char*    GetPrefName() const; // returns an allocated string.
  40.     
  41.     static void            ChangePrefName(LView* inSuperView, PaneIDT inPaneID, const char* inNewName);
  42.     static const char*    GetPrefName(LView* inSuperView, PaneIDT inPaneID); // returns an allocated string.
  43.     static void            SetWriteOnDestroy(Boolean inWrite) { sWriteOnDestroy = inWrite; }
  44.     static Boolean        GetWriteOnDestroy() { return sWriteOnDestroy; }
  45.     static void            SetReplacementString(const char* s) { sReplacementString = s; }
  46.     struct StReplacementString // temporarily set and then clear the static string
  47.     {
  48.         StReplacementString(const char* s) { MPreferenceBase::sReplacementString = s; }
  49.         ~StReplacementString() { MPreferenceBase::sReplacementString = nil; }
  50.     };
  51.     struct StWriteOnDestroy // temporarily set and then restore the static flag
  52.     {
  53.         StWriteOnDestroy(Boolean inTempValue)
  54.             :    mSavedValue(MPreferenceBase::sWriteOnDestroy)
  55.             {
  56.                 MPreferenceBase::sWriteOnDestroy = inTempValue;
  57.             }
  58.         ~StWriteOnDestroy()
  59.             {
  60.                 MPreferenceBase::sWriteOnDestroy = mSavedValue;
  61.             }
  62.         Boolean mSavedValue;
  63.     };
  64.  
  65. protected:
  66.     void                FinishCreate();
  67.     virtual void        ReadSelf() = 0; // Call this from FinishCreateSelf!
  68.     Boolean                ShouldWrite() const;
  69.     void                ReadLockState();
  70. // data:
  71. protected:
  72.     // From the resource stream:
  73.     const char*            mName;        // string that identifies this resource.
  74.     Int16                mOrdinal;
  75.         // For a boolean pref, this is xored with the value after read and before write.
  76.         // For an int pref, this represents the value to be saved if the control is on.
  77.     // From the prefs db
  78.     Boolean                mLocked;
  79.     LPane*                mPaneSelf; // Control/edit field that we're mixed in with.
  80.     
  81.     // Implementation
  82. private:
  83.     friend class        CDebugPrefToolTipAttachment;
  84.     friend class        StReplacementString;
  85.     friend class        StWriteOnDestroy;
  86.     static Boolean        sWriteOnDestroy; // one for all instantiations of the template.
  87.     static const char*    sReplacementString; // for pref name magic names with ^0 in them.
  88.         // This must only be manipulated using the StReplacementString class.
  89. }; // class MPreferenceBase
  90.  
  91.  
  92. //======================================
  93. template <class TPane,class TData> class MPreference
  94. // This is a mixin class that gets added to the pref control types.
  95. //======================================
  96. :    public MPreferenceBase
  97. {
  98. public:
  99.                     MPreference(
  100.                         LPane* inControl,
  101.                         LStream* inStream);
  102.                     virtual ~MPreference();
  103. public:
  104.     virtual void    ReadSelf(); // Call this from FinishCreateSelf!
  105.     virtual void    ReadDefaultSelf();
  106.     void            WriteSelf();
  107.     TData            GetPrefValue() const;
  108.     TData            GetPaneValue() const;
  109.     void            SetPaneValue(TData);
  110.     virtual Boolean    Changed() const;
  111. protected:
  112.     typedef int        (*PrefReadFunc)(const char*, TData*);
  113.     virtual Boolean    ShouldWrite() const { return MPreferenceBase::ShouldWrite(); }
  114.     virtual void    ReadLockState() { MPreferenceBase::ReadLockState(); }
  115.     void            InitializeUsing(PrefReadFunc inFunc); // used by ReadSelf, ReadDefaultSelf.
  116. // data:
  117. protected:
  118.     // From the constructor:
  119.     TData            mInitialControlValue;
  120. }; // template <class TPane, class TData> class MPreference
  121.  
  122.