sbFrequencyPropertyUnitConverter.cpp
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 // Property Unit Converter for samplerate values, ie: Hz, kHz
28 
30 #include <math.h>
31 #include <stdlib.h>
32 
33 // ctor - register all units
35 {
36  SetStringBundle(NS_LITERAL_STRING("chrome://songbird/locale/songbird.properties"));
38  NS_LITERAL_STRING("hz"),
39  NS_LITERAL_STRING("&frequency.unit.hertz"),
40  NS_LITERAL_STRING("&frequency.unit.hertz.short"),
41  PR_TRUE);
43  NS_LITERAL_STRING("khz"),
44  NS_LITERAL_STRING("&frequency.unit.kilohertz"),
45  NS_LITERAL_STRING("&frequency.unit.kilohertz.short"));
46 }
47 
48 // dtor
50 {
51 }
52 
53 // convert from the native unit to any of the exposed units
54 NS_IMETHODIMP
56  PRUint32 aUnitID,
57  PRFloat64 &_retVal)
58 {
59  switch (aUnitID) {
60  case FREQUENCY_UNIT_HZ:
61  // native unit, nothing to do
62  break;
63  case FREQUENCY_UNIT_KHZ:
64  aValue /= 1000.0;
65  break;
66  default:
67  return NS_ERROR_INVALID_ARG;
68  }
69  _retVal = aValue;
70  return NS_OK;
71 }
72 
73 // convert from any of the exposed units back to the native unit
74 NS_IMETHODIMP
76  PRUint32 aUnitID,
77  PRFloat64 &_retVal)
78 {
79  switch (aUnitID) {
80  case FREQUENCY_UNIT_HZ:
81  // native unit, nothing to do
82  break;
83  case FREQUENCY_UNIT_KHZ:
84  aValue *= 1000.0;
85  break;
86  default:
87  return NS_ERROR_INVALID_ARG;
88  }
89  _retVal = aValue;
90  return NS_OK;
91 }
92 
94  // get number of digits
95  PRUint32 d;
96  if (aValue == 0) d = 1;
97  else d = (PRUint32)(log10(fabs(aValue)) + 1);
98 
99  // and pick most suitable unit
100  if (d < 4) return FREQUENCY_UNIT_HZ;
101  return FREQUENCY_UNIT_KHZ;
102 }
return NS_OK
NS_IMETHOD ConvertFromNativeToUnit(PRFloat64 aValue, PRUint32 aUnitID, PRFloat64 &_retVal)
NS_IMETHOD ConvertFromUnitToNative(PRFloat64 aValue, PRUint32 aUnitID, PRFloat64 &_retVal)
onPageChanged aValue
Definition: FeedWriter.js:1395
virtual PRInt32 GetAutoUnit(PRFloat64 aValue)
function d(s)
void RegisterUnit(PRUint32 aUnitInternalID, const nsAString &aUnitExternalID, const nsAString &aUnitName, const nsAString &aUnitShortName, PRBool isNative=PR_FALSE)
void SetStringBundle(const nsAString &aStringBundle)