xref: /aoo42x/main/svx/source/dialog/relfld.cxx (revision 2a8f684d)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 // include ---------------------------------------------------------------
28 
29 #include <tools/ref.hxx>
30 #include "svx/relfld.hxx"
31 
32 // -----------------------------------------------------------------------
33 
SvxRelativeField(Window * pParent,WinBits nWinSize)34 SvxRelativeField::SvxRelativeField( Window* pParent, WinBits nWinSize ) :
35 	MetricField( pParent, nWinSize )
36 {
37     bNegativeEnabled = sal_False;
38     bRelativeMode = sal_False;
39 	bRelative 	  = sal_False;
40 
41 	SetDecimalDigits( 2 );
42 	SetMin( 0 );
43 	SetMax( 9999 );
44 }
45 
46 // -----------------------------------------------------------------------
47 
SvxRelativeField(Window * pParent,const ResId & rResId)48 SvxRelativeField::SvxRelativeField( Window* pParent, const ResId& rResId ) :
49 	MetricField( pParent, rResId )
50 {
51     bNegativeEnabled = sal_False;
52     bRelativeMode = sal_False;
53 	bRelative	  = sal_False;
54 
55 	SetDecimalDigits( 2 );
56 	SetMin( 0 );
57 	SetMax( 9999 );
58 }
59 
60 // -----------------------------------------------------------------------
61 
Modify()62 void SvxRelativeField::Modify()
63 {
64 	MetricField::Modify();
65 
66 	if ( bRelativeMode )
67 	{
68 		String  aStr = GetText();
69 		sal_Bool    bNewMode = bRelative;
70 
71 		if ( bRelative )
72 		{
73 			const sal_Unicode* pStr = aStr.GetBuffer();
74 
75 			while ( *pStr )
76 			{
77 				if( ( ( *pStr < sal_Unicode( '0' ) ) || ( *pStr > sal_Unicode( '9' ) ) ) &&
78 					( *pStr != sal_Unicode( '%' ) ) )
79 				{
80 					bNewMode = sal_False;
81 					break;
82 				}
83 				pStr++;
84 			}
85 		}
86 		else
87 		{
88 			xub_StrLen nPos = aStr.Search( sal_Unicode( '%' ) );
89 
90 			if ( nPos != STRING_NOTFOUND )
91 				bNewMode = sal_True;
92 		}
93 
94 		if ( bNewMode != bRelative )
95 			SetRelative( bNewMode );
96 
97 		MetricField::Modify();
98 	}
99 }
100 
101 // -----------------------------------------------------------------------
102 
EnableRelativeMode(sal_uInt16 nMin,sal_uInt16 nMax,sal_uInt16 nStep)103 void SvxRelativeField::EnableRelativeMode( sal_uInt16 nMin,
104 										   sal_uInt16 nMax, sal_uInt16 nStep )
105 {
106 	bRelativeMode = sal_True;
107 	nRelMin       = nMin;
108 	nRelMax       = nMax;
109 	nRelStep      = nStep;
110 	SetUnit( FUNIT_CM );
111 }
112 
113 // -----------------------------------------------------------------------
114 
SetRelative(sal_Bool bNewRelative)115 void SvxRelativeField::SetRelative( sal_Bool bNewRelative )
116 {
117 	Selection aSelection = GetSelection();
118 	String aStr = GetText();
119 
120 	if ( bNewRelative )
121 	{
122 		bRelative = sal_True;
123 		SetDecimalDigits( 0 );
124 		SetMin( nRelMin );
125 		SetMax( nRelMax );
126         SetUnit( FUNIT_PERCENT );
127 	}
128 	else
129 	{
130 		bRelative = sal_False;
131 		SetDecimalDigits( 2 );
132         SetMin( bNegativeEnabled ? -9999 : 0 );
133         SetMax( 9999 );
134 		SetUnit( FUNIT_CM );
135 	}
136 
137 	SetText( aStr );
138 	SetSelection( aSelection );
139 }
140 
141 
142