xref: /trunk/main/svx/source/dialog/relfld.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svx.hxx"
30 
31 // include ---------------------------------------------------------------
32 
33 #include <tools/ref.hxx>
34 #include "svx/relfld.hxx"
35 
36 // -----------------------------------------------------------------------
37 
38 SvxRelativeField::SvxRelativeField( Window* pParent, WinBits nWinSize ) :
39     MetricField( pParent, nWinSize )
40 {
41     bNegativeEnabled = sal_False;
42     bRelativeMode = sal_False;
43     bRelative     = sal_False;
44 
45     SetDecimalDigits( 2 );
46     SetMin( 0 );
47     SetMax( 9999 );
48 }
49 
50 // -----------------------------------------------------------------------
51 
52 SvxRelativeField::SvxRelativeField( Window* pParent, const ResId& rResId ) :
53     MetricField( pParent, rResId )
54 {
55     bNegativeEnabled = sal_False;
56     bRelativeMode = sal_False;
57     bRelative     = sal_False;
58 
59     SetDecimalDigits( 2 );
60     SetMin( 0 );
61     SetMax( 9999 );
62 }
63 
64 // -----------------------------------------------------------------------
65 
66 void SvxRelativeField::Modify()
67 {
68     MetricField::Modify();
69 
70     if ( bRelativeMode )
71     {
72         String  aStr = GetText();
73         sal_Bool    bNewMode = bRelative;
74 
75         if ( bRelative )
76         {
77             const sal_Unicode* pStr = aStr.GetBuffer();
78 
79             while ( *pStr )
80             {
81                 if( ( ( *pStr < sal_Unicode( '0' ) ) || ( *pStr > sal_Unicode( '9' ) ) ) &&
82                     ( *pStr != sal_Unicode( '%' ) ) )
83                 {
84                     bNewMode = sal_False;
85                     break;
86                 }
87                 pStr++;
88             }
89         }
90         else
91         {
92             xub_StrLen nPos = aStr.Search( sal_Unicode( '%' ) );
93 
94             if ( nPos != STRING_NOTFOUND )
95                 bNewMode = sal_True;
96         }
97 
98         if ( bNewMode != bRelative )
99             SetRelative( bNewMode );
100 
101         MetricField::Modify();
102     }
103 }
104 
105 // -----------------------------------------------------------------------
106 
107 void SvxRelativeField::EnableRelativeMode( sal_uInt16 nMin,
108                                            sal_uInt16 nMax, sal_uInt16 nStep )
109 {
110     bRelativeMode = sal_True;
111     nRelMin       = nMin;
112     nRelMax       = nMax;
113     nRelStep      = nStep;
114     SetUnit( FUNIT_CM );
115 }
116 
117 // -----------------------------------------------------------------------
118 
119 void SvxRelativeField::SetRelative( sal_Bool bNewRelative )
120 {
121     Selection aSelection = GetSelection();
122     String aStr = GetText();
123 
124     if ( bNewRelative )
125     {
126         bRelative = sal_True;
127         SetDecimalDigits( 0 );
128         SetMin( nRelMin );
129         SetMax( nRelMax );
130         SetCustomUnitText( String( sal_Unicode( '%' ) ) );
131         SetUnit( FUNIT_CUSTOM );
132     }
133     else
134     {
135         bRelative = sal_False;
136         SetDecimalDigits( 2 );
137         SetMin( bNegativeEnabled ? -9999 : 0 );
138         SetMax( 9999 );
139         SetUnit( FUNIT_CM );
140     }
141 
142     SetText( aStr );
143     SetSelection( aSelection );
144 }
145 
146 
147