home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / utility / MPreference.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  31.2 KB  |  785 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. #include "MPreference.h"
  20.  
  21. #include "PascalString.h"
  22.  
  23. #include <LControl.h>
  24. #include <LGAPopup.h>
  25.  
  26. #include "xp_mcom.h"
  27. #include "prefapi.h"
  28. #include "macutil.h" // for StringParamText
  29. #include "prefwutil.h" // for CColorButton
  30.  
  31. #include "StSetBroadcasting.h"
  32. #include "CTooltipAttachment.h"
  33.  
  34. Boolean MPreferenceBase::sWriteOnDestroy = false; // must be one for all instantiations of the template.
  35. const char* MPreferenceBase::sReplacementString = nil;
  36.  
  37. #pragma mark ---CDebugPrefToolTipAttachment---
  38. //========================================================================================
  39. class CDebugPrefToolTipAttachment : public CToolTipAttachment
  40. //========================================================================================
  41. {
  42.     public:
  43.         enum { class_ID = 'X%W@' };
  44.  
  45.                             CDebugPrefToolTipAttachment(MPreferenceBase* b);
  46.     protected:
  47.         virtual void        CalcTipText(
  48.                                     LWindow*                inOwningWindow,
  49.                                     LPane*                    inOwningPane,
  50.                                     const EventRecord&        inMacEvent,
  51.                                     StringPtr                outTipText);
  52.     MPreferenceBase*        mPreferenceBase;
  53. }; // class CDebugPrefToolTipAttachment
  54.  
  55. //----------------------------------------------------------------------------------------
  56. CDebugPrefToolTipAttachment::CDebugPrefToolTipAttachment(MPreferenceBase* b)
  57. //----------------------------------------------------------------------------------------
  58.     :    CToolTipAttachment(60, 11507)
  59.     ,    mPreferenceBase(b)
  60. {
  61. }
  62.  
  63. //----------------------------------------------------------------------------------------
  64. void CDebugPrefToolTipAttachment::CalcTipText(
  65. //----------------------------------------------------------------------------------------
  66.     LWindow*    /* inOwningWindow */,
  67.     LPane*        /* inOwningPane */,
  68.     const EventRecord&    /* inMacEvent */,
  69.     StringPtr    outTipText)
  70. {
  71.     *(CStr255*)outTipText = mPreferenceBase->mName;
  72. }
  73.  
  74. #pragma mark ---MPreferenceBase---
  75. //----------------------------------------------------------------------------------------
  76. MPreferenceBase::MPreferenceBase(
  77.     LPane*    inPane
  78. ,    LStream* inStream)
  79. //----------------------------------------------------------------------------------------
  80. :    mName(nil)
  81. ,    mLocked(false)
  82. ,    mPaneSelf(inPane)
  83. {
  84.     CStr255 text;
  85.     inStream->ReadPString(text);
  86.     SetPrefName((const char*)text, false);
  87.     *inStream >> mOrdinal;
  88. } // MPreferenceBase::MPreferenceBase
  89.  
  90. //----------------------------------------------------------------------------------------
  91. MPreferenceBase::~MPreferenceBase()
  92. //----------------------------------------------------------------------------------------
  93. {
  94.     XP_FREEIF(const_cast<char*>(mName));
  95. } // MPreferenceBase::~MPreferenceBase
  96.  
  97. //----------------------------------------------------------------------------------------
  98. void MPreferenceBase::SetPrefName(const char* inNewName, Boolean inReread)
  99. //----------------------------------------------------------------------------------------
  100. {
  101.     const char* oldName = mName; // so that inNewName == mName works
  102.     CStr255 text(inNewName);
  103.     if (sReplacementString && *sReplacementString)
  104.         ::StringParamText(text, sReplacementString);
  105.     mName = XP_STRDUP((const char*)text);
  106.     XP_FREEIF(const_cast<char*>(oldName));
  107.     if (inReread)
  108.     {
  109.         ReadLockState();
  110.         ReadSelf();
  111.     }
  112. } // MPreferenceBase::ReadLockState
  113.  
  114. //----------------------------------------------------------------------------------------
  115. void MPreferenceBase::ChangePrefName(LView* inSuperView, PaneIDT inPaneID, const char* inNewName)
  116. //----------------------------------------------------------------------------------------
  117. {
  118.     LPane* p = inSuperView->FindPaneByID(inPaneID);
  119.     MPreferenceBase* pb = dynamic_cast<MPreferenceBase*>(p);
  120.     SignalIf_(!pb);
  121.     if (pb)
  122.         pb->SetPrefName(inNewName);
  123. }
  124.  
  125. //----------------------------------------------------------------------------------------
  126. const char* MPreferenceBase::GetPrefName(LView* inSuperView, PaneIDT inPaneID)
  127. //----------------------------------------------------------------------------------------
  128. {
  129.     LPane* p = inSuperView->FindPaneByID(inPaneID);
  130.     MPreferenceBase* pb = dynamic_cast<MPreferenceBase*>(p);
  131.     SignalIf_(!pb);
  132.     if (pb)
  133.         return pb->GetPrefName();
  134.     return nil;
  135. }
  136.  
  137. //----------------------------------------------------------------------------------------
  138. const char* MPreferenceBase::GetPrefName() const
  139. //----------------------------------------------------------------------------------------
  140. {
  141.     return XP_STRDUP(mName);
  142. }
  143.  
  144. //----------------------------------------------------------------------------------------
  145. void MPreferenceBase::ReadLockState()
  146. //----------------------------------------------------------------------------------------
  147. {
  148.     mLocked = PREF_PrefIsLocked(mName);
  149.     if (mLocked)
  150.         mPaneSelf->Disable();
  151. } // MPreferenceBase::ReadLockState
  152.  
  153. //----------------------------------------------------------------------------------------
  154. void MPreferenceBase::FinishCreate()
  155. //----------------------------------------------------------------------------------------
  156. {
  157.     ReadLockState();
  158.     ReadSelf();
  159. #ifdef DEBUG
  160.     LAttachable::SetDefaultAttachable(mPaneSelf);
  161.     CDebugPrefToolTipAttachment* a = new CDebugPrefToolTipAttachment(this);
  162.     mPaneSelf->AddAttachment(a);
  163. #endif
  164. } // MPreferenceBase::FinishCreate
  165.  
  166.  
  167. //----------------------------------------------------------------------------------------
  168. Boolean MPreferenceBase::ShouldWrite() const
  169. //----------------------------------------------------------------------------------------
  170. {
  171.     if (!sWriteOnDestroy || mLocked)
  172.         return false;
  173.     if (strstr(mName, "^0") != nil) // yow! unreplaced strings
  174.     {
  175.         // Check if a replacement has become possible
  176.         Assert_(sReplacementString && *sReplacementString);
  177.         if (!sReplacementString || !*sReplacementString)
  178.             return false;
  179.         const_cast<MPreferenceBase*>(this)->SetPrefName(mName, false); // don't read
  180.     }
  181.     return true;
  182.         // Note: don't worry about testing Changed(), since preflib does that.
  183. } // MPreferenceBase::ShouldWrite
  184.  
  185.  
  186. #pragma mark ---MPreference---
  187. //----------------------------------------------------------------------------------------
  188. template <class TPane, class TData> MPreference<TPane,TData>::MPreference(
  189.                         LPane* inPane,
  190.                         LStream* inStream)
  191. //----------------------------------------------------------------------------------------
  192. :    MPreferenceBase(inPane, inStream)
  193. {
  194. } // MPreference::MPreference
  195.  
  196. //----------------------------------------------------------------------------------------
  197. template <class TPane, class TData> MPreference<TPane,TData>::~MPreference()
  198. //----------------------------------------------------------------------------------------
  199. {
  200. } // MPreference::~MPreference
  201.  
  202. #pragma mark -
  203.  
  204. enum // what the ordinal means in this case:
  205. {
  206.     kOrdinalXORBit = 1<<0
  207. ,    kOrdinalIntBit = 1<<1
  208. };
  209.  
  210. //----------------------------------------------------------------------------------------
  211. XP_Bool MPreference<LControl,XP_Bool>::GetPaneValue() const
  212. //----------------------------------------------------------------------------------------
  213. {
  214.     return ((LControl*)mPaneSelf)->GetValue();
  215. } // MPreference<LControl,XP_Bool>::GetPaneValue
  216.  
  217. //----------------------------------------------------------------------------------------
  218. void MPreference<LControl,XP_Bool>::SetPaneValue(XP_Bool inData)
  219. //----------------------------------------------------------------------------------------
  220. {
  221.     ((LControl*)mPaneSelf)->SetValue(inData);
  222. } // MPreference<LControl,XP_Bool>::SetPaneValue
  223.  
  224. //----------------------------------------------------------------------------------------
  225. Boolean MPreference<LControl, XP_Bool>::Changed() const
  226. //----------------------------------------------------------------------------------------
  227. {
  228.     return GetPaneValue() != mInitialControlValue;
  229. } // MPreference<LControl,XP_Bool>::Changed
  230.  
  231. //----------------------------------------------------------------------------------------
  232. void MPreference<LControl,XP_Bool>::InitializeUsing(PrefReadFunc inFunc)
  233. //----------------------------------------------------------------------------------------
  234. {
  235.     XP_Bool value;
  236.     int    prefResult;
  237.     if (mOrdinal & 2)
  238.     {
  239.         int32 intValue;
  240.         typedef int    (*IntPrefReadFunc)(const char*, int32*);
  241.         prefResult = ((IntPrefReadFunc)inFunc)(mName, &intValue);
  242.         value = intValue;
  243.     }
  244.     else
  245.         prefResult = inFunc(mName, &value);
  246.     if (prefResult == PREF_NOERROR)
  247.         SetPaneValue(value ^ (mOrdinal & kOrdinalXORBit));
  248. } // MPreference<LControl,XP_Bool>::InitializeUsing
  249.  
  250. //----------------------------------------------------------------------------------------
  251. void MPreference<LControl,XP_Bool>::ReadSelf()
  252. //----------------------------------------------------------------------------------------
  253. {
  254.     if (mOrdinal & kOrdinalIntBit) // this bit indicates it's an int conversion
  255.         InitializeUsing((PrefReadFunc)PREF_GetIntPref);
  256.     else
  257.         InitializeUsing(PREF_GetBoolPref);
  258.     mInitialControlValue = GetPaneValue();
  259. } // MPreference<LControl,XP_Bool>::ReadSelf
  260.  
  261. //----------------------------------------------------------------------------------------
  262. void MPreference<LControl,XP_Bool>::ReadDefaultSelf()
  263. //----------------------------------------------------------------------------------------
  264. {
  265.     if (!IsLocked())
  266.         if (mOrdinal & kOrdinalIntBit) // this bit indicates it's an int conversion
  267.             InitializeUsing((PrefReadFunc)PREF_GetDefaultIntPref);
  268.         else
  269.             InitializeUsing(PREF_GetDefaultBoolPref);
  270. } // MPreference<LControl,XP_Bool>::ReadDefaultSelf
  271.  
  272. //----------------------------------------------------------------------------------------
  273. void MPreference<LControl,XP_Bool>::WriteSelf()
  274. //----------------------------------------------------------------------------------------
  275. {
  276.     if (ShouldWrite())
  277.     {
  278.         if (mOrdinal & kOrdinalIntBit) // this bit indicates it's an int conversion
  279.             PREF_SetIntPref(mName, GetPrefValue());
  280.         else
  281.             PREF_SetBoolPref(mName, GetPrefValue());
  282.     }
  283. } // MPreference<LControl,XP_Bool>::WriteSelf
  284.  
  285. //----------------------------------------------------------------------------------------
  286. XP_Bool MPreference<LControl,XP_Bool>::GetPrefValue() const
  287. //----------------------------------------------------------------------------------------
  288. {
  289.     // xor the boolean value with the low-order bit of the ordinal
  290.     return (XP_Bool)(mPaneSelf->GetValue() ^ (mOrdinal & kOrdinalXORBit));
  291. } // MPreference<LControl,XP_Bool>::GetPrefValue
  292.  
  293. template class MPreference<LControl,XP_Bool>;
  294.  
  295. #pragma mark -
  296.  
  297. //----------------------------------------------------------------------------------------
  298. int32 MPreference<LControl,int32>::GetPaneValue() const
  299. //----------------------------------------------------------------------------------------
  300. {
  301.     return ((LControl*)mPaneSelf)->GetValue();
  302. } // MPreference<LControl,int32>::GetPaneValue
  303.  
  304. //----------------------------------------------------------------------------------------
  305. void MPreference<LControl,int32>::SetPaneValue(int32 inData)
  306. //----------------------------------------------------------------------------------------
  307. {
  308.     ((LControl*)mPaneSelf)->SetValue(inData);
  309. } // MPreference<LControl,int32>::SetPaneValue
  310.  
  311. //----------------------------------------------------------------------------------------
  312. Boolean MPreference<LControl, int32>::Changed() const
  313. //----------------------------------------------------------------------------------------
  314. {
  315.     return GetPaneValue() != mInitialControlValue;
  316. } // MPreference<LControl,int32>::Changed
  317.  
  318. //----------------------------------------------------------------------------------------
  319. void MPreference<LControl,int32>::InitializeUsing(PrefReadFunc inFunc)
  320. //----------------------------------------------------------------------------------------
  321. {
  322.     int32 value;
  323.     int    prefResult = inFunc(mName, &value);
  324.     if (prefResult == PREF_NOERROR)
  325.     {
  326.         if (value == mOrdinal)
  327.             SetPaneValue(1); // tab group will turn others off.
  328.     }
  329. } // MPreference<LControl,int32>::InitializeUsing
  330.  
  331. //----------------------------------------------------------------------------------------
  332. void MPreference<LControl,int32>::ReadSelf()
  333. //----------------------------------------------------------------------------------------
  334. {
  335.     InitializeUsing(PREF_GetIntPref);
  336.     mInitialControlValue = GetPaneValue();
  337. } // MPreference<LControl,int32>::ReadSelf
  338.  
  339. //----------------------------------------------------------------------------------------
  340. void MPreference<LControl,int32>::ReadDefaultSelf()
  341. //----------------------------------------------------------------------------------------
  342. {
  343.     if (!IsLocked())
  344.         InitializeUsing(PREF_GetDefaultIntPref);
  345. } // MPreference<LControl,int32>::ReadDefaultSelf
  346.  
  347. //----------------------------------------------------------------------------------------
  348. void MPreference<LControl,int32>::WriteSelf()
  349. //----------------------------------------------------------------------------------------
  350. {
  351.     if (ShouldWrite())
  352.         PREF_SetIntPref(mName, mOrdinal);
  353. } // MPreference<int>::WriteSelf
  354.  
  355. //----------------------------------------------------------------------------------------
  356. int32 MPreference<LControl,int32>::GetPrefValue() const
  357. //----------------------------------------------------------------------------------------
  358. {
  359.     return mOrdinal;
  360. } // MPreference<int>::GetPrefValue
  361.  
  362. template class MPreference<LControl,int32>;
  363.  
  364. #pragma mark -
  365.  
  366. //----------------------------------------------------------------------------------------
  367. MPreference<LTextEdit,char*>::~MPreference()
  368. //----------------------------------------------------------------------------------------
  369. {
  370.     XP_FREEIF(mInitialControlValue);
  371. } // MPreference<LTextEdit,char*>::CleanUpData
  372.  
  373. //----------------------------------------------------------------------------------------
  374. char* MPreference<LTextEdit,char*>::GetPaneValue() const
  375. //----------------------------------------------------------------------------------------
  376. {
  377.     CStr255 value;
  378.     mPaneSelf->GetDescriptor(value);
  379.     return (char*)value;
  380. } // MPreference<LTextEdit,char*>::GetPaneValue
  381.  
  382. //----------------------------------------------------------------------------------------
  383. void MPreference<LTextEdit,char*>::SetPaneValue(char* inData)
  384. //----------------------------------------------------------------------------------------
  385. {
  386.     ((LTextEdit*)mPaneSelf)->SetDescriptor(CStr255(inData));
  387. } // MPreference<LTextEdit,char*>:SetPaneValue
  388.  
  389. //----------------------------------------------------------------------------------------
  390. Boolean MPreference<LTextEdit,char*>::Changed() const
  391. //----------------------------------------------------------------------------------------
  392. {
  393.     char* value = GetPaneValue();
  394.     if (value && *value)
  395.         return (strcmp(value, mInitialControlValue) != 0);
  396.     return true;
  397. } // MPreference<LTextEdit,char*>::Changed
  398.  
  399. //----------------------------------------------------------------------------------------
  400. void MPreference<LTextEdit,char*>::InitializeUsing(PrefReadFunc inFunc)
  401. //----------------------------------------------------------------------------------------
  402. {
  403.     char* value;
  404.     int    prefResult = inFunc(mName, &value);
  405.     if (prefResult == PREF_NOERROR)
  406.     {
  407.         SetPaneValue(value);
  408.         XP_FREEIF(value);
  409.     }
  410. } // MPreference<LTextEdit,char*>::InitializeUsing
  411.  
  412. //----------------------------------------------------------------------------------------
  413. void MPreference<LTextEdit,char*>::ReadSelf()
  414. //----------------------------------------------------------------------------------------
  415. {
  416.     InitializeUsing(PREF_CopyCharPref);
  417.     mInitialControlValue = XP_STRDUP(GetPaneValue());
  418. } // MPreference<LTextEdit,char*>::ReadSelf
  419.  
  420. //----------------------------------------------------------------------------------------
  421. void MPreference<LTextEdit,char*>::ReadDefaultSelf()
  422. //----------------------------------------------------------------------------------------
  423. {
  424.     if (!IsLocked())
  425.         InitializeUsing(PREF_CopyDefaultCharPref);
  426. } // MPreference<LTextEdit,char*>::ReadDefaultSelf
  427.  
  428. //----------------------------------------------------------------------------------------
  429. void MPreference<LTextEdit,char*>::WriteSelf()
  430. //----------------------------------------------------------------------------------------
  431. {
  432.     if (ShouldWrite())
  433.         PREF_SetCharPref(mName, GetPaneValue());
  434. } // MPreference<LTextEdit,char*>::WriteSelf
  435.  
  436. //----------------------------------------------------------------------------------------
  437. char* MPreference<LTextEdit,char*>::GetPrefValue() const
  438. //----------------------------------------------------------------------------------------
  439. {
  440.     return GetPaneValue();
  441. } // MPreference<LTextEdit,char*>::GetPrefValue
  442.  
  443. template class MPreference<LTextEdit,char*>;
  444.  
  445. #pragma mark -
  446.  
  447. // This is used for captions, and for mixing in with another pref control (eg, to
  448. // control the descriptor of a checkbox).
  449.  
  450. //----------------------------------------------------------------------------------------
  451. MPreference<LPane,char*>::~MPreference()
  452. //----------------------------------------------------------------------------------------
  453. {
  454.     XP_FREEIF(mInitialControlValue);
  455. } // MPreference<LPane,char*>::CleanUpData
  456.  
  457. //----------------------------------------------------------------------------------------
  458. char* MPreference<LPane,char*>::GetPaneValue() const
  459. //----------------------------------------------------------------------------------------
  460. {
  461.     CStr255 value;
  462.     mPaneSelf->GetDescriptor(value);
  463.     return (char*)value;
  464. } // MPreference<LPane,char*>::GetPaneValue
  465.  
  466. //----------------------------------------------------------------------------------------
  467. void MPreference<LPane,char*>::SetPaneValue(char* inData)
  468. //----------------------------------------------------------------------------------------
  469. {
  470.     ((LPane*)mPaneSelf)->SetDescriptor(CStr255(inData));
  471. } // MPreference<LPane,char*>:SetPaneValue
  472.  
  473. //----------------------------------------------------------------------------------------
  474. Boolean MPreference<LPane,char*>::Changed() const
  475. //----------------------------------------------------------------------------------------
  476. {
  477.     char* value = GetPaneValue();
  478.     if (value && *value)
  479.         return (strcmp(value, mInitialControlValue) != 0);
  480.     return true;
  481. } // MPreference<LPane,char*>::Changed
  482.  
  483. //----------------------------------------------------------------------------------------
  484. void MPreference<LPane,char*>::InitializeUsing(PrefReadFunc inFunc)
  485. //----------------------------------------------------------------------------------------
  486. {
  487.     char* value;
  488.     int    prefResult = inFunc(mName, &value);
  489.     if (prefResult == PREF_NOERROR)
  490.     {
  491.         SetPaneValue(value);
  492.         XP_FREEIF(value);
  493.     }
  494. } // MPreference<LPane,char*>::InitializeUsing
  495.  
  496. //----------------------------------------------------------------------------------------
  497. void MPreference<LPane,char*>::ReadSelf()
  498. //----------------------------------------------------------------------------------------
  499. {
  500.     InitializeUsing(PREF_CopyCharPref);
  501.     mInitialControlValue = XP_STRDUP(GetPaneValue());
  502. } // MPreference<LPane,char*>::ReadSelf
  503.  
  504. //----------------------------------------------------------------------------------------
  505. void MPreference<LPane,char*>::ReadDefaultSelf()
  506. //----------------------------------------------------------------------------------------
  507. {
  508.     if (!IsLocked())
  509.         InitializeUsing(PREF_CopyDefaultCharPref);
  510. } // MPreference<LPane,char*>::ReadDefaultSelf
  511.  
  512. //----------------------------------------------------------------------------------------
  513. void MPreference<LPane,char*>::WriteSelf()
  514. //----------------------------------------------------------------------------------------
  515. {
  516. } // MPreference<LPane,char*>::WriteSelf
  517.  
  518. //----------------------------------------------------------------------------------------
  519. char* MPreference<LPane,char*>::GetPrefValue() const
  520. //----------------------------------------------------------------------------------------
  521. {
  522.     return GetPaneValue();
  523. } // MPreference<LPane,char*>::GetPrefValue
  524.  
  525. template class MPreference<LPane,char*>;
  526.  
  527. #pragma mark -
  528.  
  529. //----------------------------------------------------------------------------------------
  530. MPreference<LGAPopup,char*>::~MPreference()
  531. //----------------------------------------------------------------------------------------
  532. {
  533.     XP_FREEIF(mInitialControlValue);
  534. } // MPreference<LGAPopup,char*>::CleanUpData
  535.  
  536. //----------------------------------------------------------------------------------------
  537. char* MPreference<LGAPopup,char*>::GetPaneValue() const
  538. //----------------------------------------------------------------------------------------
  539. {
  540.     Int32 itemNumber = ((LGAPopup*)mPaneSelf)->GetValue();
  541.     CStr255 itemString;
  542.     if (itemNumber > 0)
  543.     {
  544.         MenuHandle menuH = ((LGAPopup*)mPaneSelf)->GetMacMenuH();
  545.         ::GetMenuItemText(menuH, itemNumber, itemString);
  546.     }
  547.     return (char*)itemString;
  548. } // MPreference<LGAPopup,char*>::GetPaneValue
  549.  
  550. //----------------------------------------------------------------------------------------
  551. void MPreference<LGAPopup,char*>::SetPaneValue(char* inData)
  552. //----------------------------------------------------------------------------------------
  553. {
  554.     if (!inData || !*inData)
  555.         return;
  556.     MenuHandle menuH = ((LGAPopup*)mPaneSelf)->GetMacMenuH();
  557.     short menuSize = ::CountMItems(menuH);
  558.     CStr255    itemString;
  559.     for (short menuItem = 1; menuItem <= menuSize; ++menuItem)
  560.     {
  561.         ::GetMenuItemText(menuH, menuItem, itemString);
  562.         if (itemString == inData)
  563.         {
  564.             StSetBroadcasting dontBroadcast((LGAPopup*)mPaneSelf, false);
  565.             ((LGAPopup*)mPaneSelf)->SetValue(menuItem);
  566.             return;
  567.         }
  568.     }
  569. } // MPreference<LGAPopup,char*>:SetPaneValue
  570.  
  571. //----------------------------------------------------------------------------------------
  572. Boolean MPreference<LGAPopup,char*>::Changed() const
  573. //----------------------------------------------------------------------------------------
  574. {
  575.     char* value = GetPaneValue();
  576.     if (value && *value)
  577.         return (strcmp(value, mInitialControlValue) != 0);
  578.     return true;
  579. } // MPreference<LGAPopup,char*>::Changed
  580.  
  581. //----------------------------------------------------------------------------------------
  582. void MPreference<LGAPopup,char*>::InitializeUsing(PrefReadFunc inFunc)
  583. //----------------------------------------------------------------------------------------
  584. {
  585.     char* value;
  586.     int    prefResult = inFunc(mName, &value);
  587.     if (prefResult == PREF_NOERROR)
  588.     {
  589.         SetPaneValue(value);
  590.         XP_FREEIF(value);
  591.     }
  592. } // MPreference<LGAPopup,char*>::InitializeUsing
  593.  
  594. //----------------------------------------------------------------------------------------
  595. void MPreference<LGAPopup,char*>::ReadSelf()
  596. //----------------------------------------------------------------------------------------
  597. {
  598.     InitializeUsing(PREF_CopyCharPref);
  599.     mInitialControlValue = XP_STRDUP(GetPaneValue());
  600. } // MPreference<LGAPopup,char*>::ReadSelf
  601.  
  602. //----------------------------------------------------------------------------------------
  603. void MPreference<LGAPopup,char*>::ReadDefaultSelf()
  604. //----------------------------------------------------------------------------------------
  605. {
  606.     if (!IsLocked())
  607.         InitializeUsing(PREF_CopyDefaultCharPref);
  608. } // MPreference<LGAPopup,char*>::ReadDefaultSelf
  609.  
  610. //----------------------------------------------------------------------------------------
  611. void MPreference<LGAPopup,char*>::WriteSelf()
  612. //----------------------------------------------------------------------------------------
  613. {
  614.     if (ShouldWrite())
  615.         PREF_SetCharPref(mName, GetPaneValue());
  616. } // MPreference<LGAPopup,char*>::WriteSelf
  617.  
  618. //----------------------------------------------------------------------------------------
  619. char* MPreference<LGAPopup,char*>::GetPrefValue() const
  620. //----------------------------------------------------------------------------------------
  621. {
  622.     return GetPaneValue();
  623. } // MPreference<LGAPopup,char*>::GetPrefValue
  624.  
  625. template class MPreference<LGAPopup,char*>;
  626.  
  627. #pragma mark -
  628.  
  629. //----------------------------------------------------------------------------------------
  630. MPreference<LTextEdit,int32>::~MPreference()
  631. //----------------------------------------------------------------------------------------
  632. {
  633. } // MPreference<LTextEdit,int32>::CleanUpData
  634.  
  635. //----------------------------------------------------------------------------------------
  636. int32 MPreference<LTextEdit,int32>::GetPaneValue() const
  637. //----------------------------------------------------------------------------------------
  638. {
  639.     return ((LTextEdit*)mPaneSelf)->GetValue();
  640. } // MPreference<LTextEdit,int32>::GetPaneValue
  641.  
  642. //----------------------------------------------------------------------------------------
  643. void MPreference<LTextEdit,int32>::SetPaneValue(int32 inData)
  644. //----------------------------------------------------------------------------------------
  645. {
  646.     ((LTextEdit*)mPaneSelf)->SetValue(inData);
  647. } // MPreference<LTextEdit,int32>:SetPaneValue
  648.  
  649. //----------------------------------------------------------------------------------------
  650. Boolean MPreference<LTextEdit,int32>::Changed() const
  651. //----------------------------------------------------------------------------------------
  652. {
  653.     return GetPaneValue() != mInitialControlValue;
  654. } // MPreference<LTextEdit,int32>::Changed
  655.  
  656. //----------------------------------------------------------------------------------------
  657. void MPreference<LTextEdit,int32>::InitializeUsing(PrefReadFunc inFunc)
  658. //----------------------------------------------------------------------------------------
  659. {
  660.     int32 value;
  661.     int    prefResult = inFunc(mName, &value);
  662.     if (prefResult == PREF_NOERROR)
  663.         SetPaneValue(value);
  664. } // MPreference<LTextEdit,int32>::InitializeUsing
  665.  
  666. //----------------------------------------------------------------------------------------
  667. void MPreference<LTextEdit,int32>::ReadSelf()
  668. //----------------------------------------------------------------------------------------
  669. {
  670.     InitializeUsing(PREF_GetIntPref);
  671.     mInitialControlValue = GetPaneValue();
  672. } // MPreference<LTextEdit,int32>::ReadSelf
  673.  
  674. //----------------------------------------------------------------------------------------
  675. void MPreference<LTextEdit,int32>::ReadDefaultSelf()
  676. //----------------------------------------------------------------------------------------
  677. {
  678.     if (!IsLocked())
  679.         InitializeUsing(PREF_GetDefaultIntPref);
  680. } // MPreference<LTextEdit,int32>::ReadDefaultSelf
  681.  
  682. //----------------------------------------------------------------------------------------
  683. void MPreference<LTextEdit,int32>::WriteSelf()
  684. //----------------------------------------------------------------------------------------
  685. {
  686.     if (ShouldWrite())
  687.         PREF_SetIntPref(mName, GetPaneValue());
  688. } // MPreference<LTextEdit,int32>::WriteSelf
  689.  
  690. //----------------------------------------------------------------------------------------
  691. int32 MPreference<LTextEdit,int32>::GetPrefValue() const
  692. //----------------------------------------------------------------------------------------
  693. {
  694.     return GetPaneValue();
  695. } // MPreference<LTextEdit,int32>::GetPrefValue
  696.  
  697. template class MPreference<LTextEdit,int32>;
  698.  
  699. #pragma mark -
  700.  
  701. // The function signature for reading colors is not like the other types.  Here is
  702. // its prototype, which we use to cast back and forth.
  703. typedef int (*ColorReadFunc)(const char*, uint8*, uint8*, uint8*);
  704.  
  705. //----------------------------------------------------------------------------------------
  706. MPreference<CColorButton,RGBColor>::~MPreference()
  707. //----------------------------------------------------------------------------------------
  708. {
  709. } // MPreference<CColorButton,RGBColor>::CleanUpData
  710.  
  711. //----------------------------------------------------------------------------------------
  712. RGBColor MPreference<CColorButton,RGBColor>::GetPaneValue() const
  713. //----------------------------------------------------------------------------------------
  714. {
  715.     return ((CColorButton*)mPaneSelf)->GetColor();
  716. } // MPreference<CColorButton,RGBColor>::GetPaneValue
  717.  
  718. //----------------------------------------------------------------------------------------
  719. void MPreference<CColorButton,RGBColor>::SetPaneValue(RGBColor inData)
  720. //----------------------------------------------------------------------------------------
  721. {
  722.     ((CColorButton*)mPaneSelf)->SetColor(inData);
  723.     mPaneSelf->Refresh();
  724. } // MPreference<CColorButton,RGBColor>:SetPaneValue
  725.  
  726. //----------------------------------------------------------------------------------------
  727. Boolean MPreference<CColorButton,RGBColor>::Changed() const
  728. //----------------------------------------------------------------------------------------
  729. {
  730.     return memcmp(&GetPaneValue(), &mInitialControlValue, sizeof(RGBColor)) == 0;
  731. } // MPreference<CColorButton,RGBColor>::Changed
  732.  
  733. //----------------------------------------------------------------------------------------
  734. void MPreference<CColorButton,RGBColor>::InitializeUsing(PrefReadFunc inFunc)
  735. //----------------------------------------------------------------------------------------
  736. {
  737.     RGBColor value;
  738.     uint8 red = 0, green = 0, blue = 0;
  739.     int    prefResult = ((ColorReadFunc)inFunc)(mName, &red, &green, &blue);
  740.     if (prefResult == PREF_NOERROR)
  741.     {
  742.         value.red = red << 8;
  743.         value.green = green << 8;
  744.         value.blue = blue << 8;
  745.         SetPaneValue(value);
  746.     }
  747. } // MPreference<CColorButton,RGBColor>::InitializeUsing
  748.  
  749. //----------------------------------------------------------------------------------------
  750. void MPreference<CColorButton,RGBColor>::ReadSelf()
  751. //----------------------------------------------------------------------------------------
  752. {
  753.     InitializeUsing((PrefReadFunc)PREF_GetColorPref);
  754.     mInitialControlValue = GetPaneValue();
  755. } // MPreference<CColorButton,RGBColor>::ReadSelf
  756.  
  757. //----------------------------------------------------------------------------------------
  758. void MPreference<CColorButton,RGBColor>::ReadDefaultSelf()
  759. //----------------------------------------------------------------------------------------
  760. {
  761.     if (!IsLocked())
  762.         InitializeUsing((PrefReadFunc)PREF_GetDefaultColorPref);
  763. } // MPreference<CColorButton,RGBColor>::ReadDefaultSelf
  764.  
  765. //----------------------------------------------------------------------------------------
  766. void MPreference<CColorButton,RGBColor>::WriteSelf()
  767. //----------------------------------------------------------------------------------------
  768. {
  769.     if (ShouldWrite())
  770.     {
  771.         RGBColor value = GetPaneValue();
  772.         PREF_SetColorPref(mName, value.red >> 8, value.green >> 8, value.blue >> 8);
  773.     }
  774. } // MPreference<CColorButton,RGBColor>::WriteSelf
  775.  
  776. //----------------------------------------------------------------------------------------
  777. RGBColor MPreference<CColorButton,RGBColor>::GetPrefValue() const
  778. //----------------------------------------------------------------------------------------
  779. {
  780.     return GetPaneValue();
  781. } // MPreference<CColorButton,RGBColor>::GetPrefValue
  782.  
  783. template class MPreference<CColorButton,RGBColor>;
  784.  
  785.