Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include "svx/relfld.hxx"
21 :
22 : // -----------------------------------------------------------------------
23 :
24 0 : SvxRelativeField::SvxRelativeField( Window* pParent, const ResId& rResId ) :
25 0 : MetricField( pParent, rResId )
26 : {
27 0 : bNegativeEnabled = sal_False;
28 0 : bRelativeMode = sal_False;
29 0 : bRelative = sal_False;
30 :
31 0 : SetDecimalDigits( 2 );
32 0 : SetMin( 0 );
33 0 : SetMax( 9999 );
34 0 : }
35 :
36 : // -----------------------------------------------------------------------
37 :
38 0 : void SvxRelativeField::Modify()
39 : {
40 0 : MetricField::Modify();
41 :
42 0 : if ( bRelativeMode )
43 : {
44 0 : String aStr = GetText();
45 0 : sal_Bool bNewMode = bRelative;
46 :
47 0 : if ( bRelative )
48 : {
49 0 : const sal_Unicode* pStr = aStr.GetBuffer();
50 :
51 0 : while ( *pStr )
52 : {
53 0 : if( ( ( *pStr < sal_Unicode( '0' ) ) || ( *pStr > sal_Unicode( '9' ) ) ) &&
54 : ( *pStr != sal_Unicode( '%' ) ) )
55 : {
56 0 : bNewMode = sal_False;
57 0 : break;
58 : }
59 0 : pStr++;
60 : }
61 : }
62 : else
63 : {
64 0 : xub_StrLen nPos = aStr.Search( sal_Unicode( '%' ) );
65 :
66 0 : if ( nPos != STRING_NOTFOUND )
67 0 : bNewMode = sal_True;
68 : }
69 :
70 0 : if ( bNewMode != bRelative )
71 0 : SetRelative( bNewMode );
72 :
73 0 : MetricField::Modify();
74 : }
75 0 : }
76 :
77 : // -----------------------------------------------------------------------
78 :
79 0 : void SvxRelativeField::EnableRelativeMode( sal_uInt16 nMin,
80 : sal_uInt16 nMax, sal_uInt16 nStep )
81 : {
82 0 : bRelativeMode = sal_True;
83 0 : nRelMin = nMin;
84 0 : nRelMax = nMax;
85 0 : nRelStep = nStep;
86 0 : SetUnit( FUNIT_CM );
87 0 : }
88 :
89 : // -----------------------------------------------------------------------
90 :
91 0 : void SvxRelativeField::SetRelative( sal_Bool bNewRelative )
92 : {
93 0 : Selection aSelection = GetSelection();
94 0 : String aStr = GetText();
95 :
96 0 : if ( bNewRelative )
97 : {
98 0 : bRelative = sal_True;
99 0 : SetDecimalDigits( 0 );
100 0 : SetMin( nRelMin );
101 0 : SetMax( nRelMax );
102 0 : SetCustomUnitText( rtl::OUString('%') );
103 0 : SetUnit( FUNIT_CUSTOM );
104 : }
105 : else
106 : {
107 0 : bRelative = sal_False;
108 0 : SetDecimalDigits( 2 );
109 0 : SetMin( bNegativeEnabled ? -9999 : 0 );
110 0 : SetMax( 9999 );
111 0 : SetUnit( FUNIT_CM );
112 : }
113 :
114 0 : SetText( aStr );
115 0 : SetSelection( aSelection );
116 0 : }
117 :
118 :
119 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|