12a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52a97ec55SAndrew Rist  * distributed with this work for additional information
62a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
82a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
92a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
102a97ec55SAndrew Rist  *
112a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
122a97ec55SAndrew Rist  *
132a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
142a97ec55SAndrew Rist  * software distributed under the License is distributed on an
152a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
172a97ec55SAndrew Rist  * specific language governing permissions and limitations
182a97ec55SAndrew Rist  * under the License.
192a97ec55SAndrew Rist  *
202a97ec55SAndrew Rist  *************************************************************/
212a97ec55SAndrew Rist 
22cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
23cdf0e10cSrcweir #include "precompiled_extensions.hxx"
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #include <stdio.h>
26cdf0e10cSrcweir #include <stdlib.h>
27cdf0e10cSrcweir #include <tools/config.hxx>
28cdf0e10cSrcweir #include <vcl/msgbox.hxx>
29cdf0e10cSrcweir #include <sanedlg.hxx>
30cdf0e10cSrcweir #include <sanedlg.hrc>
31cdf0e10cSrcweir #include <grid.hxx>
32cdf0e10cSrcweir #include <math.h>
33*45fd3b9aSArmin Le Grand #include <vcl/dibtools.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #define USE_SAVE_STATE
36cdf0e10cSrcweir #undef  SAVE_ALL_STATES
37cdf0e10cSrcweir 
SaneResId(sal_uInt32 nID)38cdf0e10cSrcweir ResId SaneResId( sal_uInt32 nID )
39cdf0e10cSrcweir {
40cdf0e10cSrcweir 	static ResMgr* pResMgr = ResMgr::CreateResMgr( "san" );
41cdf0e10cSrcweir 	return ResId( nID, *pResMgr );
42cdf0e10cSrcweir }
43cdf0e10cSrcweir 
SaneDlg(Window * pParent,Sane & rSane)44cdf0e10cSrcweir SaneDlg::SaneDlg( Window* pParent, Sane& rSane ) :
45cdf0e10cSrcweir 		ModalDialog( pParent, SaneResId( RID_SANE_DIALOG ) ),
46cdf0e10cSrcweir 		mrSane( rSane ),
47cdf0e10cSrcweir 		mbIsDragging( sal_False ),
48cdf0e10cSrcweir 		mbDragDrawn( sal_False ),
49cdf0e10cSrcweir 		maMapMode( MAP_APPFONT ),
50cdf0e10cSrcweir 		maOKButton( this, SaneResId( RID_SCAN_OK ) ),
51cdf0e10cSrcweir 		maCancelButton( this, SaneResId( RID_SCAN_CANCEL ) ),
52cdf0e10cSrcweir 		maDeviceInfoButton( this, SaneResId( RID_DEVICEINFO_BTN ) ),
53cdf0e10cSrcweir 		maPreviewButton( this, SaneResId( RID_PREVIEW_BTN ) ),
54cdf0e10cSrcweir 		maButtonOption( this, SaneResId( RID_SCAN_BUTTON_OPTION_BTN ) ),
55cdf0e10cSrcweir 		maOptionsTxt( this, SaneResId( RID_SCAN_OPTION_TXT ) ),
56cdf0e10cSrcweir 		maOptionTitle( this, SaneResId( RID_SCAN_OPTIONTITLE_TXT ) ),
57cdf0e10cSrcweir 		maOptionDescTxt( this, SaneResId( RID_SCAN_OPTION_DESC_TXT ) ),
58cdf0e10cSrcweir 		maVectorTxt( this, SaneResId( RID_SCAN_NUMERIC_VECTOR_TXT ) ),
59cdf0e10cSrcweir 		maScanLeftTxt( this, SaneResId( RID_SCAN_LEFT_TXT ) ),
60cdf0e10cSrcweir 		maLeftField( this, SaneResId( RID_SCAN_LEFT_BOX ) ),
61cdf0e10cSrcweir 		maScanTopTxt( this, SaneResId( RID_SCAN_TOP_TXT ) ),
62cdf0e10cSrcweir 		maTopField( this, SaneResId( RID_SCAN_TOP_BOX ) ),
63cdf0e10cSrcweir 		maRightTxt( this, SaneResId( RID_SCAN_RIGHT_TXT ) ),
64cdf0e10cSrcweir 		maRightField( this, SaneResId( RID_SCAN_RIGHT_BOX ) ),
65cdf0e10cSrcweir 		maBottomTxt( this, SaneResId( RID_SCAN_BOTTOM_TXT ) ),
66cdf0e10cSrcweir 		maBottomField( this, SaneResId( RID_SCAN_BOTTOM_BOX ) ),
67cdf0e10cSrcweir 		maDeviceBoxTxt( this, SaneResId( RID_DEVICE_BOX_TXT ) ),
68cdf0e10cSrcweir 		maDeviceBox( this, SaneResId( RID_DEVICE_BOX ) ),
69cdf0e10cSrcweir 		maReslTxt( this, SaneResId( RID_SCAN_RESOLUTION_TXT ) ),
70cdf0e10cSrcweir 		maReslBox( this, SaneResId( RID_SCAN_RESOLUTION_BOX ) ),
71cdf0e10cSrcweir 		maAdvancedTxt( this, SaneResId( RID_SCAN_ADVANCED_TXT ) ),
72cdf0e10cSrcweir 		maAdvancedBox( this, SaneResId( RID_SCAN_ADVANCED_BOX ) ),
73cdf0e10cSrcweir 		maVectorBox( this, SaneResId( RID_SCAN_NUMERIC_VECTOR_BOX ) ),
74cdf0e10cSrcweir 		maQuantumRangeBox( this, SaneResId( RID_SCAN_QUANTUM_RANGE_BOX ) ),
75cdf0e10cSrcweir 		maStringRangeBox( this, SaneResId( RID_SCAN_STRING_RANGE_BOX ) ),
76cdf0e10cSrcweir 		maPreviewBox( this, SaneResId( RID_PREVIEW_BOX ) ),
77cdf0e10cSrcweir 		maAreaBox( this, SaneResId( RID_SCANAREA_BOX ) ),
78cdf0e10cSrcweir 		maBoolCheckBox( this, SaneResId( RID_SCAN_BOOL_OPTION_BOX ) ),
79cdf0e10cSrcweir 		maStringEdit( this, SaneResId( RID_SCAN_STRING_OPTION_EDT ) ),
80cdf0e10cSrcweir 		maNumericEdit( this, SaneResId( RID_SCAN_NUMERIC_OPTION_EDT ) ),
81cdf0e10cSrcweir 		maOptionBox( this, SaneResId( RID_SCAN_OPTION_BOX ) ),
82cdf0e10cSrcweir 		mpRange( 0 )
83cdf0e10cSrcweir {
84cdf0e10cSrcweir 	if( Sane::IsSane() )
85cdf0e10cSrcweir 	{
86cdf0e10cSrcweir 		InitDevices(); // opens first sane device
87cdf0e10cSrcweir 		DisableOption();
88cdf0e10cSrcweir 		InitFields();
89cdf0e10cSrcweir 	}
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	maDeviceInfoButton.SetClickHdl( LINK( this, SaneDlg, ClickBtnHdl ) );
92cdf0e10cSrcweir 	maPreviewButton.SetClickHdl( LINK( this, SaneDlg, ClickBtnHdl ) );
93cdf0e10cSrcweir 	maButtonOption.SetClickHdl( LINK( this, SaneDlg, ClickBtnHdl ) );
94cdf0e10cSrcweir 	maDeviceBox.SetSelectHdl( LINK( this, SaneDlg, SelectHdl ) );
95cdf0e10cSrcweir 	maOptionBox.SetSelectHdl( LINK( this, SaneDlg, OptionsBoxSelectHdl ) );
96cdf0e10cSrcweir 	maOKButton.SetClickHdl( LINK( this, SaneDlg, ClickBtnHdl ) );
97cdf0e10cSrcweir 	maCancelButton.SetClickHdl( LINK( this, SaneDlg, ClickBtnHdl ) );
98cdf0e10cSrcweir 	maBoolCheckBox.SetClickHdl( LINK( this, SaneDlg, ClickBtnHdl ) );
99cdf0e10cSrcweir 	maStringEdit.SetModifyHdl( LINK( this, SaneDlg, ModifyHdl ) );
100cdf0e10cSrcweir 	maNumericEdit.SetModifyHdl( LINK( this, SaneDlg, ModifyHdl ) );
101cdf0e10cSrcweir 	maVectorBox.SetModifyHdl( LINK( this, SaneDlg, ModifyHdl ) );
102cdf0e10cSrcweir 	maReslBox.SetModifyHdl( LINK( this, SaneDlg, ModifyHdl ) );
103cdf0e10cSrcweir 	maStringRangeBox.SetSelectHdl( LINK( this, SaneDlg, SelectHdl ) );
104cdf0e10cSrcweir 	maQuantumRangeBox.SetSelectHdl( LINK( this, SaneDlg, SelectHdl ) );
105cdf0e10cSrcweir 	maLeftField.SetModifyHdl( LINK( this, SaneDlg, ModifyHdl ) );
106cdf0e10cSrcweir 	maRightField.SetModifyHdl( LINK( this, SaneDlg, ModifyHdl ) );
107cdf0e10cSrcweir 	maTopField.SetModifyHdl( LINK( this, SaneDlg, ModifyHdl ) );
108cdf0e10cSrcweir 	maBottomField.SetModifyHdl( LINK( this, SaneDlg, ModifyHdl ) );
109cdf0e10cSrcweir 	maAdvancedBox.SetClickHdl( LINK( this, SaneDlg, ClickBtnHdl ) );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir 	maOldLink = mrSane.SetReloadOptionsHdl( LINK( this, SaneDlg, ReloadSaneOptionsHdl ) );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	maOptionBox.SetNodeBitmaps(
114cdf0e10cSrcweir 		Bitmap( SaneResId( RID_SCAN_BITMAP_PLUS ) ),
115cdf0e10cSrcweir 		Bitmap( SaneResId( RID_SCAN_BITMAP_MINUS ) )
116cdf0e10cSrcweir 		);
117cdf0e10cSrcweir 	maOptionBox.SetStyle( maOptionBox.GetStyle()|
118cdf0e10cSrcweir                           WB_HASLINES           |
119cdf0e10cSrcweir 						  WB_HASBUTTONS         |
120cdf0e10cSrcweir 						  WB_NOINITIALSELECTION |
121cdf0e10cSrcweir 						  WB_HASBUTTONSATROOT   |
122cdf0e10cSrcweir 						  WB_HASLINESATROOT
123cdf0e10cSrcweir 						);
124cdf0e10cSrcweir 	FreeResource();
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
~SaneDlg()127cdf0e10cSrcweir SaneDlg::~SaneDlg()
128cdf0e10cSrcweir {
129cdf0e10cSrcweir }
130cdf0e10cSrcweir 
Execute()131cdf0e10cSrcweir short SaneDlg::Execute()
132cdf0e10cSrcweir {
133cdf0e10cSrcweir 	if( ! Sane::IsSane() )
134cdf0e10cSrcweir 	{
135cdf0e10cSrcweir 		ErrorBox aErrorBox( NULL, WB_OK | WB_DEF_OK,
136cdf0e10cSrcweir 							String( SaneResId( RID_SANE_NOSANELIB_TXT ) ) );
137cdf0e10cSrcweir 		aErrorBox.Execute();
138cdf0e10cSrcweir 		return sal_False;
139cdf0e10cSrcweir 	}
140cdf0e10cSrcweir 	LoadState();
141cdf0e10cSrcweir 	return ModalDialog::Execute();
142cdf0e10cSrcweir }
143cdf0e10cSrcweir 
InitDevices()144cdf0e10cSrcweir void SaneDlg::InitDevices()
145cdf0e10cSrcweir {
146cdf0e10cSrcweir 	if( ! Sane::IsSane() )
147cdf0e10cSrcweir 		return;
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 	if( mrSane.IsOpen() )
150cdf0e10cSrcweir 		mrSane.Close();
151cdf0e10cSrcweir 	mrSane.ReloadDevices();
152cdf0e10cSrcweir 	maDeviceBox.Clear();
153cdf0e10cSrcweir 	for( int i = 0; i < Sane::CountDevices(); i++ )
154cdf0e10cSrcweir 		maDeviceBox.InsertEntry( Sane::GetName( i ) );
155cdf0e10cSrcweir 	if( Sane::CountDevices() )
156cdf0e10cSrcweir 	{
157cdf0e10cSrcweir 		mrSane.Open( 0 );
158cdf0e10cSrcweir 		maDeviceBox.SelectEntry( Sane::GetName( 0 ) );
159cdf0e10cSrcweir 
160cdf0e10cSrcweir 	}
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
InitFields()163cdf0e10cSrcweir void SaneDlg::InitFields()
164cdf0e10cSrcweir {
165cdf0e10cSrcweir 	if( ! Sane::IsSane() )
166cdf0e10cSrcweir 		return;
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 	int nOption, i, nValue;
169cdf0e10cSrcweir 	double fValue;
170cdf0e10cSrcweir 	sal_Bool bSuccess = sal_False;
171cdf0e10cSrcweir 	const char *ppSpecialOptions[] = {
172cdf0e10cSrcweir 		"resolution",
173cdf0e10cSrcweir 		"tl-x",
174cdf0e10cSrcweir 		"tl-y",
175cdf0e10cSrcweir 		"br-x",
176cdf0e10cSrcweir 		"br-y",
177cdf0e10cSrcweir 		"preview"
178cdf0e10cSrcweir 	};
179cdf0e10cSrcweir 
180cdf0e10cSrcweir     mbDragEnable = sal_True;
181cdf0e10cSrcweir 	maReslBox.Clear();
182cdf0e10cSrcweir 	maMinTopLeft = Point( 0, 0 );
183cdf0e10cSrcweir 	maMaxBottomRight = Point( PREVIEW_WIDTH,  PREVIEW_HEIGHT );
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 	if( ! mrSane.IsOpen() )
186cdf0e10cSrcweir 		return;
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 	// set Resolution
189cdf0e10cSrcweir 	nOption = mrSane.GetOptionByName( "resolution" );
190cdf0e10cSrcweir 	if( nOption != -1 )
191cdf0e10cSrcweir 	{
192cdf0e10cSrcweir 		double fRes;
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 		bSuccess = mrSane.GetOptionValue( nOption, fRes );
195cdf0e10cSrcweir 		if( bSuccess )
196cdf0e10cSrcweir 		{
197cdf0e10cSrcweir 			maReslBox.Enable( sal_True );
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 			maReslBox.SetValue( (long)fRes );
200cdf0e10cSrcweir 			double *pDouble = NULL;
201cdf0e10cSrcweir 			nValue = mrSane.GetRange( nOption, pDouble );
202cdf0e10cSrcweir 			if( nValue > -1 )
203cdf0e10cSrcweir 			{
204cdf0e10cSrcweir 				if( nValue )
205cdf0e10cSrcweir 				{
206cdf0e10cSrcweir 					maReslBox.SetMin( (long)pDouble[0] );
207cdf0e10cSrcweir 					maReslBox.SetMax( (long)pDouble[ nValue-1 ] );
208cdf0e10cSrcweir 					for( i=0; i<nValue; i++ )
209cdf0e10cSrcweir 					{
210cdf0e10cSrcweir 						if( i == 0 || i == nValue-1 || ! ( ((int)pDouble[i]) % 20) )
211cdf0e10cSrcweir 							maReslBox.InsertValue( (long)pDouble[i] );
212cdf0e10cSrcweir 					}
213cdf0e10cSrcweir 				}
214cdf0e10cSrcweir 				else
215cdf0e10cSrcweir 				{
216cdf0e10cSrcweir 					maReslBox.SetMin( (long)pDouble[0] );
217cdf0e10cSrcweir 					maReslBox.SetMax( (long)pDouble[1] );
218cdf0e10cSrcweir 					maReslBox.InsertValue( (long)pDouble[0] );
219cdf0e10cSrcweir 					// mh@openoffice.org: issue 68557: Can only select 75 and 2400 dpi in Scanner dialogue
220cdf0e10cSrcweir 					// scanner allows random setting of dpi resolution, a slider might be useful
221cdf0e10cSrcweir 					// support that
222cdf0e10cSrcweir 					// workaround: offer at least some more standard dpi resolution between
223cdf0e10cSrcweir 					// min and max value
224cdf0e10cSrcweir 					int bGot300 = 0;
225cdf0e10cSrcweir 					for ( int nRes = (long) pDouble[0] * 2; nRes < (long) pDouble[1]; nRes = nRes * 2 )
226cdf0e10cSrcweir 					{
227cdf0e10cSrcweir 						if ( !bGot300 && nRes > 300 ) {
228cdf0e10cSrcweir 							nRes = 300; bGot300 = 1;
229cdf0e10cSrcweir 						}
230cdf0e10cSrcweir 						maReslBox.InsertValue(nRes);
231cdf0e10cSrcweir 					}
232cdf0e10cSrcweir 					maReslBox.InsertValue( (long)pDouble[1] );
233cdf0e10cSrcweir 				}
234cdf0e10cSrcweir 				if( pDouble )
235cdf0e10cSrcweir 					delete [] pDouble;
236cdf0e10cSrcweir 			}
237cdf0e10cSrcweir 			else
238cdf0e10cSrcweir 				maReslBox.Enable( sal_False );
239cdf0e10cSrcweir 		}
240cdf0e10cSrcweir 	}
241cdf0e10cSrcweir 	else
242cdf0e10cSrcweir 		maReslBox.Enable( sal_False );
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 	// set scan area
245cdf0e10cSrcweir 	for( i = 0; i < 4; i++ )
246cdf0e10cSrcweir 	{
247cdf0e10cSrcweir 		char const *pOptionName = NULL;
248cdf0e10cSrcweir 		MetricField* pField = NULL;
249cdf0e10cSrcweir 		switch( i )
250cdf0e10cSrcweir 		{
251cdf0e10cSrcweir 			case 0:
252cdf0e10cSrcweir 				pOptionName = "tl-x";
253cdf0e10cSrcweir 				pField = &maLeftField;
254cdf0e10cSrcweir 				break;
255cdf0e10cSrcweir 			case 1:
256cdf0e10cSrcweir 				pOptionName = "tl-y";
257cdf0e10cSrcweir 				pField = &maTopField;
258cdf0e10cSrcweir 				break;
259cdf0e10cSrcweir 			case 2:
260cdf0e10cSrcweir 				pOptionName = "br-x";
261cdf0e10cSrcweir 				pField = &maRightField;
262cdf0e10cSrcweir 				break;
263cdf0e10cSrcweir 			case 3:
264cdf0e10cSrcweir 				pOptionName = "br-y";
265cdf0e10cSrcweir 				pField = &maBottomField;
266cdf0e10cSrcweir 		}
267cdf0e10cSrcweir 		nOption = pOptionName ? mrSane.GetOptionByName( pOptionName ) : -1;
268cdf0e10cSrcweir 		bSuccess = sal_False;
269cdf0e10cSrcweir 		if( nOption != -1 )
270cdf0e10cSrcweir 		{
271cdf0e10cSrcweir 			bSuccess = mrSane.GetOptionValue( nOption, fValue, 0 );
272cdf0e10cSrcweir 			if( bSuccess )
273cdf0e10cSrcweir 			{
274cdf0e10cSrcweir 				if( mrSane.GetOptionUnit( nOption ) == SANE_UNIT_MM )
275cdf0e10cSrcweir 				{
276cdf0e10cSrcweir 					pField->SetUnit( FUNIT_MM );
277cdf0e10cSrcweir 					pField->SetValue( (int)fValue, FUNIT_MM );
278cdf0e10cSrcweir 				}
279cdf0e10cSrcweir 				else // SANE_UNIT_PIXEL
280cdf0e10cSrcweir 				{
281cdf0e10cSrcweir 					pField->SetValue( (int)fValue, FUNIT_CUSTOM );
282cdf0e10cSrcweir 					pField->SetCustomUnitText( String::CreateFromAscii( "Pixel" ) );
283cdf0e10cSrcweir 				}
284cdf0e10cSrcweir 				switch( i ) {
285cdf0e10cSrcweir 					case 0: maTopLeft.X() = (int)fValue;break;
286cdf0e10cSrcweir 					case 1:	maTopLeft.Y() = (int)fValue;break;
287cdf0e10cSrcweir 					case 2:	maBottomRight.X() = (int)fValue;break;
288cdf0e10cSrcweir 					case 3: maBottomRight.Y() = (int)fValue;break;
289cdf0e10cSrcweir 				}
290cdf0e10cSrcweir 			}
291cdf0e10cSrcweir 			double *pDouble = NULL;
292cdf0e10cSrcweir 			nValue = mrSane.GetRange( nOption, pDouble );
293cdf0e10cSrcweir 			if( nValue > -1 )
294cdf0e10cSrcweir 			{
295cdf0e10cSrcweir 				if( pDouble )
296cdf0e10cSrcweir 				{
297cdf0e10cSrcweir 					pField->SetMin( (long)pDouble[0] );
298cdf0e10cSrcweir 					if( nValue )
299cdf0e10cSrcweir 						pField->SetMax( (long)pDouble[ nValue-1 ] );
300cdf0e10cSrcweir 					else
301cdf0e10cSrcweir 						pField->SetMax( (long)pDouble[ 1 ] );
302cdf0e10cSrcweir 					delete [] pDouble;
303cdf0e10cSrcweir 				}
304cdf0e10cSrcweir 				switch( i ) {
305cdf0e10cSrcweir 					case 0: maMinTopLeft.X() = pField->GetMin();break;
306cdf0e10cSrcweir 					case 1: maMinTopLeft.Y() = pField->GetMin();break;
307cdf0e10cSrcweir 					case 2: maMaxBottomRight.X() = pField->GetMax();break;
308cdf0e10cSrcweir 					case 3: maMaxBottomRight.Y() = pField->GetMax();break;
309cdf0e10cSrcweir 				}
310cdf0e10cSrcweir 			}
311cdf0e10cSrcweir 			else
312cdf0e10cSrcweir 			{
313cdf0e10cSrcweir 				switch( i ) {
314cdf0e10cSrcweir 					case 0: maMinTopLeft.X() = (int)fValue;break;
315cdf0e10cSrcweir 					case 1: maMinTopLeft.Y() = (int)fValue;break;
316cdf0e10cSrcweir 					case 2: maMaxBottomRight.X() = (int)fValue;break;
317cdf0e10cSrcweir 					case 3: maMaxBottomRight.Y() = (int)fValue;break;
318cdf0e10cSrcweir 				}
319cdf0e10cSrcweir 			}
320cdf0e10cSrcweir 			pField->Enable( sal_True );
321cdf0e10cSrcweir 		}
322cdf0e10cSrcweir 		else
323cdf0e10cSrcweir         {
324cdf0e10cSrcweir             mbDragEnable = sal_False;
325cdf0e10cSrcweir             pField->SetMin( 0 );
326cdf0e10cSrcweir             switch( i ) {
327cdf0e10cSrcweir                 case 0:
328cdf0e10cSrcweir                     maMinTopLeft.X() = 0;
329cdf0e10cSrcweir                     maTopLeft.X() = 0;
330cdf0e10cSrcweir                     pField->SetMax( PREVIEW_WIDTH );
331cdf0e10cSrcweir                     pField->SetValue( 0 );
332cdf0e10cSrcweir                     break;
333cdf0e10cSrcweir                 case 1:
334cdf0e10cSrcweir                     maMinTopLeft.Y() = 0;
335cdf0e10cSrcweir                     maTopLeft.Y() = 0;
336cdf0e10cSrcweir                     pField->SetMax( PREVIEW_HEIGHT );
337cdf0e10cSrcweir                     pField->SetValue( 0 );
338cdf0e10cSrcweir                     break;
339cdf0e10cSrcweir                 case 2:
340cdf0e10cSrcweir                     maMaxBottomRight.X() = PREVIEW_WIDTH;
341cdf0e10cSrcweir                     maBottomRight.X() = PREVIEW_WIDTH;
342cdf0e10cSrcweir                     pField->SetMax( PREVIEW_WIDTH );
343cdf0e10cSrcweir                     pField->SetValue( PREVIEW_WIDTH );
344cdf0e10cSrcweir                     break;
345cdf0e10cSrcweir                 case 3:
346cdf0e10cSrcweir                     maMaxBottomRight.Y() = PREVIEW_HEIGHT;
347cdf0e10cSrcweir                     maBottomRight.Y() = PREVIEW_HEIGHT;
348cdf0e10cSrcweir                     pField->SetMax( PREVIEW_HEIGHT );
349cdf0e10cSrcweir                     pField->SetValue( PREVIEW_HEIGHT );
350cdf0e10cSrcweir                     break;
351cdf0e10cSrcweir             }
352cdf0e10cSrcweir 			pField->Enable( sal_False );
353cdf0e10cSrcweir         }
354cdf0e10cSrcweir 	}
355cdf0e10cSrcweir 	maTopLeft = GetPixelPos( maTopLeft );
356cdf0e10cSrcweir 	maBottomRight = GetPixelPos( maBottomRight );
357cdf0e10cSrcweir 	maPreviewRect = Rectangle( maTopLeft,
358cdf0e10cSrcweir 							   Size( maBottomRight.X() - maTopLeft.X(),
359cdf0e10cSrcweir 									 maBottomRight.Y() - maTopLeft.Y() )
360cdf0e10cSrcweir 							   );
361cdf0e10cSrcweir 	// fill OptionBox
362cdf0e10cSrcweir 	maOptionBox.Clear();
363cdf0e10cSrcweir 	SvLBoxEntry* pParentEntry = 0;
364cdf0e10cSrcweir 	sal_Bool bGroupRejected = sal_False;
365cdf0e10cSrcweir 	for( i = 1; i < mrSane.CountOptions(); i++ )
366cdf0e10cSrcweir 	{
367cdf0e10cSrcweir 		String aOption=mrSane.GetOptionName( i );
368cdf0e10cSrcweir 		sal_Bool bInsertAdvanced =
369cdf0e10cSrcweir 			mrSane.GetOptionCap( i ) & SANE_CAP_ADVANCED &&
370cdf0e10cSrcweir 			! maAdvancedBox.IsChecked() ? sal_False : sal_True;
371cdf0e10cSrcweir 		if( mrSane.GetOptionType( i ) == SANE_TYPE_GROUP )
372cdf0e10cSrcweir 		{
373cdf0e10cSrcweir 			if( bInsertAdvanced )
374cdf0e10cSrcweir 			{
375cdf0e10cSrcweir 				aOption = mrSane.GetOptionTitle( i );
376cdf0e10cSrcweir 				pParentEntry = maOptionBox.InsertEntry( aOption );
377cdf0e10cSrcweir 				bGroupRejected = sal_False;
378cdf0e10cSrcweir 			}
379cdf0e10cSrcweir 			else
380cdf0e10cSrcweir 				bGroupRejected = sal_True;
381cdf0e10cSrcweir 		}
382cdf0e10cSrcweir 		else if( aOption.Len() &&
383cdf0e10cSrcweir 				 ! ( mrSane.GetOptionCap( i ) &
384cdf0e10cSrcweir 					 (
385cdf0e10cSrcweir 						 SANE_CAP_HARD_SELECT |
386cdf0e10cSrcweir 						 SANE_CAP_INACTIVE
387cdf0e10cSrcweir 						 ) ) &&
388cdf0e10cSrcweir 				 bInsertAdvanced && ! bGroupRejected )
389cdf0e10cSrcweir 		{
390cdf0e10cSrcweir 			sal_Bool bIsSpecial = sal_False;
391cdf0e10cSrcweir 			for( size_t n = 0; !bIsSpecial &&
392cdf0e10cSrcweir 					 n < sizeof(ppSpecialOptions)/sizeof(ppSpecialOptions[0]); n++ )
393cdf0e10cSrcweir 			{
394cdf0e10cSrcweir 				if( aOption.EqualsAscii( ppSpecialOptions[n] ) )
395cdf0e10cSrcweir 					bIsSpecial=sal_True;
396cdf0e10cSrcweir 			}
397cdf0e10cSrcweir 			if( ! bIsSpecial )
398cdf0e10cSrcweir 			{
399cdf0e10cSrcweir 				if( pParentEntry )
400cdf0e10cSrcweir 					maOptionBox.InsertEntry( aOption, pParentEntry );
401cdf0e10cSrcweir 				else
402cdf0e10cSrcweir 					maOptionBox.InsertEntry( aOption );
403cdf0e10cSrcweir 			}
404cdf0e10cSrcweir 		}
405cdf0e10cSrcweir 	}
406cdf0e10cSrcweir }
407cdf0e10cSrcweir 
IMPL_LINK(SaneDlg,ClickBtnHdl,Button *,pButton)408cdf0e10cSrcweir IMPL_LINK( SaneDlg, ClickBtnHdl, Button*, pButton )
409cdf0e10cSrcweir {
410cdf0e10cSrcweir 	if( mrSane.IsOpen() )
411cdf0e10cSrcweir 	{
412cdf0e10cSrcweir 		if( pButton == &maDeviceInfoButton )
413cdf0e10cSrcweir 		{
414cdf0e10cSrcweir 			String aString( SaneResId( RID_SANE_DEVICEINFO_TXT ) );
415cdf0e10cSrcweir 			String aSR( RTL_CONSTASCII_USTRINGPARAM( "%s" ) );
416cdf0e10cSrcweir 			aString.SearchAndReplace( aSR, Sane::GetName( mrSane.GetDeviceNumber() ) );
417cdf0e10cSrcweir 			aString.SearchAndReplace( aSR, Sane::GetVendor( mrSane.GetDeviceNumber() ) );
418cdf0e10cSrcweir 			aString.SearchAndReplace( aSR, Sane::GetModel( mrSane.GetDeviceNumber() ) );
419cdf0e10cSrcweir 			aString.SearchAndReplace( aSR, Sane::GetType( mrSane.GetDeviceNumber() ) );
420cdf0e10cSrcweir 			InfoBox aInfoBox( this, aString );
421cdf0e10cSrcweir 			aInfoBox.Execute();
422cdf0e10cSrcweir 		}
423cdf0e10cSrcweir 		else if( pButton == &maPreviewButton )
424cdf0e10cSrcweir 			AcquirePreview();
425cdf0e10cSrcweir 		else if( pButton == &maBoolCheckBox )
426cdf0e10cSrcweir 		{
427cdf0e10cSrcweir 			mrSane.SetOptionValue( mnCurrentOption,
428cdf0e10cSrcweir 								   maBoolCheckBox.IsChecked() ?
429cdf0e10cSrcweir 								   (sal_Bool)sal_True : (sal_Bool)sal_False );
430cdf0e10cSrcweir 		}
431cdf0e10cSrcweir 		else if( pButton == &maButtonOption )
432cdf0e10cSrcweir 		{
433cdf0e10cSrcweir 
434cdf0e10cSrcweir 			SANE_Value_Type nType = mrSane.GetOptionType( mnCurrentOption );
435cdf0e10cSrcweir             switch( nType )
436cdf0e10cSrcweir             {
437cdf0e10cSrcweir                 case SANE_TYPE_BUTTON:
438cdf0e10cSrcweir                     mrSane.ActivateButtonOption( mnCurrentOption );
439cdf0e10cSrcweir                     break;
440cdf0e10cSrcweir                 case SANE_TYPE_FIXED:
441cdf0e10cSrcweir                 case SANE_TYPE_INT:
442cdf0e10cSrcweir                 {
443cdf0e10cSrcweir 					int nElements = mrSane.GetOptionElements( mnCurrentOption );
444cdf0e10cSrcweir                     double* x = new double[ nElements ];
445cdf0e10cSrcweir                     double* y = new double[ nElements ];
446cdf0e10cSrcweir                     for( int i = 0; i < nElements; i++ )
447cdf0e10cSrcweir                         x[ i ] = (double)i;
448cdf0e10cSrcweir                     mrSane.GetOptionValue( mnCurrentOption, y );
449cdf0e10cSrcweir 
450cdf0e10cSrcweir                     GridWindow aGrid( x, y, nElements, this );
451cdf0e10cSrcweir                     aGrid.SetText( mrSane.GetOptionName( mnCurrentOption ) );
452cdf0e10cSrcweir                     aGrid.setBoundings( 0, mfMin, nElements, mfMax );
453cdf0e10cSrcweir                     if( aGrid.Execute() && aGrid.getNewYValues() )
454cdf0e10cSrcweir                         mrSane.SetOptionValue( mnCurrentOption, aGrid.getNewYValues() );
455cdf0e10cSrcweir 
456cdf0e10cSrcweir                     delete [] x;
457cdf0e10cSrcweir                     delete [] y;
458cdf0e10cSrcweir                 }
459cdf0e10cSrcweir                 break;
460cdf0e10cSrcweir                 case SANE_TYPE_BOOL:
461cdf0e10cSrcweir                 case SANE_TYPE_STRING:
462cdf0e10cSrcweir                 case SANE_TYPE_GROUP:
463cdf0e10cSrcweir                     break;
464cdf0e10cSrcweir             }
465cdf0e10cSrcweir 		}
466cdf0e10cSrcweir 		else if( pButton == &maAdvancedBox )
467cdf0e10cSrcweir 		{
468cdf0e10cSrcweir 			ReloadSaneOptionsHdl( NULL );
469cdf0e10cSrcweir 		}
470cdf0e10cSrcweir 	}
471cdf0e10cSrcweir 	if( pButton == &maOKButton )
472cdf0e10cSrcweir 	{
473cdf0e10cSrcweir 		double fRes = (double)maReslBox.GetValue();
474cdf0e10cSrcweir 		SetAdjustedNumericalValue( "resolution", fRes );
475cdf0e10cSrcweir 		mrSane.SetReloadOptionsHdl( maOldLink );
476cdf0e10cSrcweir 		UpdateScanArea( sal_True );
477cdf0e10cSrcweir 		SaveState();
478cdf0e10cSrcweir 		EndDialog( mrSane.IsOpen() ? 1 : 0 );
479cdf0e10cSrcweir 	}
480cdf0e10cSrcweir 	else if( pButton == &maCancelButton )
481cdf0e10cSrcweir 	{
482cdf0e10cSrcweir 		mrSane.SetReloadOptionsHdl( maOldLink );
483cdf0e10cSrcweir 		mrSane.Close();
484cdf0e10cSrcweir 		EndDialog( 0 );
485cdf0e10cSrcweir 	}
486cdf0e10cSrcweir 	return 0;
487cdf0e10cSrcweir }
488cdf0e10cSrcweir 
IMPL_LINK(SaneDlg,SelectHdl,ListBox *,pListBox)489cdf0e10cSrcweir IMPL_LINK( SaneDlg, SelectHdl, ListBox*, pListBox )
490cdf0e10cSrcweir {
491cdf0e10cSrcweir 	if( pListBox == &maDeviceBox && Sane::IsSane() && Sane::CountDevices() )
492cdf0e10cSrcweir 	{
493cdf0e10cSrcweir 		String aNewDevice = maDeviceBox.GetSelectEntry();
494cdf0e10cSrcweir 		int nNumber;
495cdf0e10cSrcweir 		if( aNewDevice.Equals( Sane::GetName( nNumber = mrSane.GetDeviceNumber() ) ) )
496cdf0e10cSrcweir 		{
497cdf0e10cSrcweir 			mrSane.Close();
498cdf0e10cSrcweir 			mrSane.Open( nNumber );
499cdf0e10cSrcweir 			InitFields();
500cdf0e10cSrcweir 		}
501cdf0e10cSrcweir 	}
502cdf0e10cSrcweir 	if( mrSane.IsOpen() )
503cdf0e10cSrcweir 	{
504cdf0e10cSrcweir 		if( pListBox == &maQuantumRangeBox )
505cdf0e10cSrcweir 		{
506cdf0e10cSrcweir 			ByteString aValue( maQuantumRangeBox.GetSelectEntry(), osl_getThreadTextEncoding() );
507cdf0e10cSrcweir 			double fValue = atof( aValue.GetBuffer() );
508cdf0e10cSrcweir 			mrSane.SetOptionValue( mnCurrentOption, fValue, mnCurrentElement );
509cdf0e10cSrcweir 		}
510cdf0e10cSrcweir 		else if( pListBox == &maStringRangeBox )
511cdf0e10cSrcweir 		{
512cdf0e10cSrcweir 			mrSane.SetOptionValue( mnCurrentOption, maStringRangeBox.GetSelectEntry() );
513cdf0e10cSrcweir 		}
514cdf0e10cSrcweir 	}
515cdf0e10cSrcweir 	return 0;
516cdf0e10cSrcweir }
517cdf0e10cSrcweir 
IMPL_LINK(SaneDlg,OptionsBoxSelectHdl,SvTreeListBox *,pBox)518cdf0e10cSrcweir IMPL_LINK( SaneDlg, OptionsBoxSelectHdl, SvTreeListBox*, pBox )
519cdf0e10cSrcweir {
520cdf0e10cSrcweir 	if( pBox == &maOptionBox && Sane::IsSane() )
521cdf0e10cSrcweir 	{
522cdf0e10cSrcweir 		String aOption =
523cdf0e10cSrcweir 			maOptionBox.GetEntryText( maOptionBox.FirstSelected() );
524cdf0e10cSrcweir 		int nOption = mrSane.GetOptionByName( ByteString( aOption, osl_getThreadTextEncoding() ).GetBuffer() );
525cdf0e10cSrcweir 		if( nOption != -1 && nOption != mnCurrentOption )
526cdf0e10cSrcweir 		{
527cdf0e10cSrcweir 			DisableOption();
528cdf0e10cSrcweir 			mnCurrentOption = nOption;
529cdf0e10cSrcweir 			maOptionTitle.SetText( mrSane.GetOptionTitle( mnCurrentOption ) );
530cdf0e10cSrcweir 			SANE_Value_Type nType = mrSane.GetOptionType( mnCurrentOption );
531cdf0e10cSrcweir 			SANE_Constraint_Type nConstraint;
532cdf0e10cSrcweir 			switch( nType )
533cdf0e10cSrcweir 			{
534cdf0e10cSrcweir 				case SANE_TYPE_BOOL:	EstablishBoolOption();break;
535cdf0e10cSrcweir 				case SANE_TYPE_STRING:
536cdf0e10cSrcweir 					nConstraint = mrSane.GetOptionConstraintType( mnCurrentOption );
537cdf0e10cSrcweir 					if( nConstraint == SANE_CONSTRAINT_STRING_LIST )
538cdf0e10cSrcweir 						EstablishStringRange();
539cdf0e10cSrcweir 					else
540cdf0e10cSrcweir 						EstablishStringOption();
541cdf0e10cSrcweir 					break;
542cdf0e10cSrcweir 				case SANE_TYPE_FIXED:
543cdf0e10cSrcweir 				case SANE_TYPE_INT:
544cdf0e10cSrcweir 				{
545cdf0e10cSrcweir 					nConstraint = mrSane.GetOptionConstraintType( mnCurrentOption );
546cdf0e10cSrcweir 					int nElements = mrSane.GetOptionElements( mnCurrentOption );
547cdf0e10cSrcweir 					mnCurrentElement = 0;
548cdf0e10cSrcweir                     if( nConstraint == SANE_CONSTRAINT_RANGE ||
549cdf0e10cSrcweir                         nConstraint == SANE_CONSTRAINT_WORD_LIST )
550cdf0e10cSrcweir                         EstablishQuantumRange();
551cdf0e10cSrcweir                     else
552cdf0e10cSrcweir                     {
553cdf0e10cSrcweir                         mfMin = mfMax = 0.0;
554cdf0e10cSrcweir                         EstablishNumericOption();
555cdf0e10cSrcweir                     }
556cdf0e10cSrcweir 					if( nElements > 1 )
557cdf0e10cSrcweir 					{
558cdf0e10cSrcweir 						if( nElements <= 10 )
559cdf0e10cSrcweir 						{
560cdf0e10cSrcweir 							maVectorBox.SetValue( 1 );
561cdf0e10cSrcweir 							maVectorBox.SetMin( 1 );
562cdf0e10cSrcweir 							maVectorBox.SetMax(
563cdf0e10cSrcweir 								mrSane.GetOptionElements( mnCurrentOption ) );
564cdf0e10cSrcweir 							maVectorBox.Show( sal_True );
565cdf0e10cSrcweir 							maVectorTxt.Show( sal_True );
566cdf0e10cSrcweir 						}
567cdf0e10cSrcweir 						else
568cdf0e10cSrcweir 						{
569cdf0e10cSrcweir                             DisableOption();
570cdf0e10cSrcweir                             // bring up dialog only on button click
571cdf0e10cSrcweir                             EstablishButtonOption();
572cdf0e10cSrcweir 						}
573cdf0e10cSrcweir 					}
574cdf0e10cSrcweir 				}
575cdf0e10cSrcweir 				break;
576cdf0e10cSrcweir 				case SANE_TYPE_BUTTON:
577cdf0e10cSrcweir 					EstablishButtonOption();
578cdf0e10cSrcweir 					break;
579cdf0e10cSrcweir 				default: break;
580cdf0e10cSrcweir 			}
581cdf0e10cSrcweir 		}
582cdf0e10cSrcweir 	}
583cdf0e10cSrcweir 	return 0;
584cdf0e10cSrcweir }
585cdf0e10cSrcweir 
IMPL_LINK(SaneDlg,ModifyHdl,Edit *,pEdit)586cdf0e10cSrcweir IMPL_LINK( SaneDlg, ModifyHdl, Edit*, pEdit )
587cdf0e10cSrcweir {
588cdf0e10cSrcweir 	if( mrSane.IsOpen() )
589cdf0e10cSrcweir 	{
590cdf0e10cSrcweir 		if( pEdit == &maStringEdit )
591cdf0e10cSrcweir 		{
592cdf0e10cSrcweir 			mrSane.SetOptionValue( mnCurrentOption, maStringEdit.GetText() );
593cdf0e10cSrcweir 		}
594cdf0e10cSrcweir 		else if( pEdit == &maReslBox )
595cdf0e10cSrcweir 		{
596cdf0e10cSrcweir 			double fRes = (double)maReslBox.GetValue();
597cdf0e10cSrcweir 			int nOption = mrSane.GetOptionByName( "resolution" );
598cdf0e10cSrcweir 			if( nOption != -1 )
599cdf0e10cSrcweir 			{
600cdf0e10cSrcweir 				double* pDouble = NULL;
601cdf0e10cSrcweir 				int nValues = mrSane.GetRange( nOption, pDouble );
602cdf0e10cSrcweir 				if( nValues > 0 )
603cdf0e10cSrcweir 				{
604cdf0e10cSrcweir 					int i;
605cdf0e10cSrcweir 					for( i = 0; i < nValues; i++ )
606cdf0e10cSrcweir 					{
607cdf0e10cSrcweir 						if( fRes == pDouble[i] )
608cdf0e10cSrcweir 							break;
609cdf0e10cSrcweir 					}
610cdf0e10cSrcweir 					if( i >= nValues )
611cdf0e10cSrcweir 						fRes = pDouble[0];
612cdf0e10cSrcweir 				}
613cdf0e10cSrcweir 				else if( nValues == 0 )
614cdf0e10cSrcweir 				{
615cdf0e10cSrcweir 					if( fRes < pDouble[ 0 ] )
616cdf0e10cSrcweir 						fRes = pDouble[ 0 ];
617cdf0e10cSrcweir 					if( fRes > pDouble[ 1 ] )
618cdf0e10cSrcweir 						fRes = pDouble[ 1 ];
619cdf0e10cSrcweir 				}
620cdf0e10cSrcweir 				maReslBox.SetValue( (sal_uLong)fRes );
621cdf0e10cSrcweir 			}
622cdf0e10cSrcweir 		}
623cdf0e10cSrcweir 		else if( pEdit == &maNumericEdit )
624cdf0e10cSrcweir 		{
625cdf0e10cSrcweir 			double fValue;
626cdf0e10cSrcweir 			char pBuf[256];
627cdf0e10cSrcweir 			ByteString aContents( maNumericEdit.GetText(), osl_getThreadTextEncoding() );
628cdf0e10cSrcweir 			fValue = atof( aContents.GetBuffer() );
629cdf0e10cSrcweir 			if( mfMin != mfMax && ( fValue < mfMin || fValue > mfMax ) )
630cdf0e10cSrcweir 			{
631cdf0e10cSrcweir 				if( fValue < mfMin )
632cdf0e10cSrcweir 					fValue = mfMin;
633cdf0e10cSrcweir 				else if( fValue > mfMax )
634cdf0e10cSrcweir 				fValue = mfMax;
635cdf0e10cSrcweir 				sprintf( pBuf, "%g", fValue );
636cdf0e10cSrcweir 				maNumericEdit.SetText( String( pBuf, osl_getThreadTextEncoding() ) );
637cdf0e10cSrcweir 			}
638cdf0e10cSrcweir 			mrSane.SetOptionValue( mnCurrentOption, fValue, mnCurrentElement );
639cdf0e10cSrcweir 		}
640cdf0e10cSrcweir 		else if( pEdit == &maVectorBox )
641cdf0e10cSrcweir 		{
642cdf0e10cSrcweir 			char pBuf[256];
643cdf0e10cSrcweir 			mnCurrentElement = maVectorBox.GetValue()-1;
644cdf0e10cSrcweir 			double fValue;
645cdf0e10cSrcweir 			mrSane.GetOptionValue( mnCurrentOption, fValue, mnCurrentElement );
646cdf0e10cSrcweir 			sprintf( pBuf, "%g", fValue );
647cdf0e10cSrcweir 			String aValue( pBuf, osl_getThreadTextEncoding() );
648cdf0e10cSrcweir 			maNumericEdit.SetText( aValue );
649cdf0e10cSrcweir 			maQuantumRangeBox.SelectEntry( aValue );
650cdf0e10cSrcweir 		}
651cdf0e10cSrcweir 		else if( pEdit == &maTopField )
652cdf0e10cSrcweir 		{
653cdf0e10cSrcweir 			Point aPoint( 0, maTopField.GetValue() );
654cdf0e10cSrcweir 			aPoint = GetPixelPos( aPoint );
655cdf0e10cSrcweir 			maTopLeft.Y() = aPoint.Y();
656cdf0e10cSrcweir 			DrawDrag();
657cdf0e10cSrcweir 		}
658cdf0e10cSrcweir 		else if( pEdit == &maLeftField )
659cdf0e10cSrcweir 		{
660cdf0e10cSrcweir 			Point aPoint( maLeftField.GetValue(), 0 );
661cdf0e10cSrcweir 			aPoint = GetPixelPos( aPoint );
662cdf0e10cSrcweir 			maTopLeft.X() = aPoint.X();
663cdf0e10cSrcweir 			DrawDrag();
664cdf0e10cSrcweir 		}
665cdf0e10cSrcweir 		else if( pEdit == &maBottomField )
666cdf0e10cSrcweir 		{
667cdf0e10cSrcweir 			Point aPoint( 0, maBottomField.GetValue() );
668cdf0e10cSrcweir 			aPoint = GetPixelPos( aPoint );
669cdf0e10cSrcweir 			maBottomRight.Y() = aPoint.Y();
670cdf0e10cSrcweir 			DrawDrag();
671cdf0e10cSrcweir 		}
672cdf0e10cSrcweir 		else if( pEdit == &maRightField )
673cdf0e10cSrcweir 		{
674cdf0e10cSrcweir 			Point aPoint( maRightField.GetValue(), 0 );
675cdf0e10cSrcweir 			aPoint = GetPixelPos( aPoint );
676cdf0e10cSrcweir 			maBottomRight.X() = aPoint.X();
677cdf0e10cSrcweir 			DrawDrag();
678cdf0e10cSrcweir 		}
679cdf0e10cSrcweir 	}
680cdf0e10cSrcweir 	return 0;
681cdf0e10cSrcweir }
682cdf0e10cSrcweir 
683cdf0e10cSrcweir IMPL_LINK( SaneDlg, ReloadSaneOptionsHdl, Sane*, /*pSane*/ )
684cdf0e10cSrcweir {
685cdf0e10cSrcweir  	mnCurrentOption = -1;
686cdf0e10cSrcweir  	mnCurrentElement = 0;
687cdf0e10cSrcweir  	DisableOption();
688cdf0e10cSrcweir     // #92024# preserve preview rect, should only be set
689cdf0e10cSrcweir     // initially or in AcquirePreview
690cdf0e10cSrcweir     Rectangle aPreviewRect = maPreviewRect;
691cdf0e10cSrcweir 	InitFields();
692cdf0e10cSrcweir     maPreviewRect = aPreviewRect;
693cdf0e10cSrcweir 	Rectangle aDummyRect( Point( 0, 0 ), GetSizePixel() );
694cdf0e10cSrcweir 	Paint( aDummyRect );
695cdf0e10cSrcweir 	return 0;
696cdf0e10cSrcweir }
697cdf0e10cSrcweir 
AcquirePreview()698cdf0e10cSrcweir void SaneDlg::AcquirePreview()
699cdf0e10cSrcweir {
700cdf0e10cSrcweir 	if( ! mrSane.IsOpen() )
701cdf0e10cSrcweir 		return;
702cdf0e10cSrcweir 
703cdf0e10cSrcweir 	UpdateScanArea( sal_True );
704cdf0e10cSrcweir 	// set small resolution for preview
705cdf0e10cSrcweir 	double fResl = (double)maReslBox.GetValue();
706cdf0e10cSrcweir 	SetAdjustedNumericalValue( "resolution", 30.0 );
707cdf0e10cSrcweir 
708cdf0e10cSrcweir 	int nOption = mrSane.GetOptionByName( "preview" );
709cdf0e10cSrcweir 	if( nOption == -1 )
710cdf0e10cSrcweir 	{
711cdf0e10cSrcweir 		String aString( SaneResId( RID_SANE_NORESOLUTIONOPTION_TXT ) );
712cdf0e10cSrcweir 		WarningBox aBox( this, WB_OK_CANCEL | WB_DEF_OK, aString );
713cdf0e10cSrcweir 		if( aBox.Execute() == RET_CANCEL )
714cdf0e10cSrcweir 			return;
715cdf0e10cSrcweir 	}
716cdf0e10cSrcweir 	else
717cdf0e10cSrcweir 		mrSane.SetOptionValue( nOption, (sal_Bool)sal_True );
718cdf0e10cSrcweir 
719cdf0e10cSrcweir 	BitmapTransporter aTransporter;
720cdf0e10cSrcweir 	if( ! mrSane.Start( aTransporter ) )
721cdf0e10cSrcweir 	{
722cdf0e10cSrcweir 		ErrorBox aErrorBox( this, WB_OK | WB_DEF_OK,
723cdf0e10cSrcweir 							String( SaneResId( RID_SANE_SCANERROR_TXT ) ) );
724cdf0e10cSrcweir 		aErrorBox.Execute();
725cdf0e10cSrcweir 	}
726cdf0e10cSrcweir 	else
727cdf0e10cSrcweir 	{
728cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
729cdf0e10cSrcweir 		aTransporter.getStream().Seek( STREAM_SEEK_TO_END );
730cdf0e10cSrcweir 		fprintf( stderr, "Previewbitmapstream contains %d bytes\n", (int)aTransporter.getStream().Tell() );
731cdf0e10cSrcweir #endif
732cdf0e10cSrcweir 		aTransporter.getStream().Seek( STREAM_SEEK_TO_BEGIN );
733*45fd3b9aSArmin Le Grand         ReadDIB(maPreviewBitmap, aTransporter.getStream(), true);
734cdf0e10cSrcweir 	}
735cdf0e10cSrcweir 
736cdf0e10cSrcweir 	SetAdjustedNumericalValue( "resolution", fResl );
737cdf0e10cSrcweir 	maReslBox.SetValue( (sal_uLong)fResl );
738cdf0e10cSrcweir 
739cdf0e10cSrcweir     if( mbDragEnable )
740cdf0e10cSrcweir         maPreviewRect = Rectangle( maTopLeft,
741cdf0e10cSrcweir                                    Size( maBottomRight.X() - maTopLeft.X(),
742cdf0e10cSrcweir                                          maBottomRight.Y() - maTopLeft.Y() )
743cdf0e10cSrcweir                                    );
744cdf0e10cSrcweir     else
745cdf0e10cSrcweir     {
746cdf0e10cSrcweir         Size aBMSize( maPreviewBitmap.GetSizePixel() );
747cdf0e10cSrcweir         if( aBMSize.Width() > aBMSize.Height() )
748cdf0e10cSrcweir         {
749cdf0e10cSrcweir             int nVHeight = (maBottomRight.X() - maTopLeft.X()) * aBMSize.Height() / aBMSize.Width();
750cdf0e10cSrcweir             maPreviewRect = Rectangle( Point( maTopLeft.X(), ( maTopLeft.Y() + maBottomRight.Y() )/2 - nVHeight/2 ),
751cdf0e10cSrcweir                                        Size( maBottomRight.X() - maTopLeft.X(),
752cdf0e10cSrcweir                                              nVHeight ) );
753cdf0e10cSrcweir         }
754cdf0e10cSrcweir         else
755cdf0e10cSrcweir         {
756cdf0e10cSrcweir             int nVWidth = (maBottomRight.Y() - maTopLeft.Y()) * aBMSize.Width() / aBMSize.Height();
757cdf0e10cSrcweir             maPreviewRect = Rectangle( Point( ( maTopLeft.X() + maBottomRight.X() )/2 - nVWidth/2, maTopLeft.Y() ),
758cdf0e10cSrcweir                                        Size( nVWidth,
759cdf0e10cSrcweir                                              maBottomRight.Y() - maTopLeft.Y() ) );
760cdf0e10cSrcweir         }
761cdf0e10cSrcweir     }
762cdf0e10cSrcweir 
763cdf0e10cSrcweir 	Paint( Rectangle( Point( 0, 0 ), GetSizePixel() ) );
764cdf0e10cSrcweir }
765cdf0e10cSrcweir 
Paint(const Rectangle & rRect)766cdf0e10cSrcweir void SaneDlg::Paint( const Rectangle& rRect )
767cdf0e10cSrcweir {
768cdf0e10cSrcweir 	SetMapMode( maMapMode );
769cdf0e10cSrcweir 	SetFillColor( Color( COL_WHITE ) );
770cdf0e10cSrcweir 	SetLineColor( Color( COL_WHITE ) );
771cdf0e10cSrcweir 	DrawRect( Rectangle( Point( PREVIEW_UPPER_LEFT, PREVIEW_UPPER_TOP ),
772cdf0e10cSrcweir 						 Size( PREVIEW_WIDTH, PREVIEW_HEIGHT ) ) );
773cdf0e10cSrcweir 	SetMapMode( MapMode( MAP_PIXEL ) );
774cdf0e10cSrcweir 	// check for sane values
775cdf0e10cSrcweir 	DrawBitmap( maPreviewRect.TopLeft(), maPreviewRect.GetSize(),
776cdf0e10cSrcweir 				maPreviewBitmap );
777cdf0e10cSrcweir 
778cdf0e10cSrcweir 	mbDragDrawn = sal_False;
779cdf0e10cSrcweir 	DrawDrag();
780cdf0e10cSrcweir 
781cdf0e10cSrcweir 	ModalDialog::Paint( rRect );
782cdf0e10cSrcweir }
783cdf0e10cSrcweir 
DisableOption()784cdf0e10cSrcweir void SaneDlg::DisableOption()
785cdf0e10cSrcweir {
786cdf0e10cSrcweir 	maBoolCheckBox.Show( sal_False );
787cdf0e10cSrcweir 	maStringEdit.Show( sal_False );
788cdf0e10cSrcweir 	maNumericEdit.Show( sal_False );
789cdf0e10cSrcweir 	maQuantumRangeBox.Show( sal_False );
790cdf0e10cSrcweir 	maStringRangeBox.Show( sal_False );
791cdf0e10cSrcweir 	maButtonOption.Show( sal_False );
792cdf0e10cSrcweir 	maVectorBox.Show( sal_False );
793cdf0e10cSrcweir 	maVectorTxt.Show( sal_False );
794cdf0e10cSrcweir 	maOptionDescTxt.Show( sal_False );
795cdf0e10cSrcweir }
796cdf0e10cSrcweir 
EstablishBoolOption()797cdf0e10cSrcweir void SaneDlg::EstablishBoolOption()
798cdf0e10cSrcweir {
799cdf0e10cSrcweir 	sal_Bool bSuccess, bValue;
800cdf0e10cSrcweir 
801cdf0e10cSrcweir 	bSuccess = mrSane.GetOptionValue( mnCurrentOption, bValue );
802cdf0e10cSrcweir 	if( bSuccess )
803cdf0e10cSrcweir 	{
804cdf0e10cSrcweir 		maOptionDescTxt.SetText( mrSane.GetOptionName( mnCurrentOption ) );
805cdf0e10cSrcweir 		maOptionDescTxt.Show( sal_True );
806cdf0e10cSrcweir 		maBoolCheckBox.Check( bValue );
807cdf0e10cSrcweir 		maBoolCheckBox.Show( sal_True );
808cdf0e10cSrcweir 	}
809cdf0e10cSrcweir }
810cdf0e10cSrcweir 
EstablishStringOption()811cdf0e10cSrcweir void SaneDlg::EstablishStringOption()
812cdf0e10cSrcweir {
813cdf0e10cSrcweir 	sal_Bool bSuccess;
814cdf0e10cSrcweir 	ByteString aValue;
815cdf0e10cSrcweir 
816cdf0e10cSrcweir 	bSuccess = mrSane.GetOptionValue( mnCurrentOption, aValue );
817cdf0e10cSrcweir 	if( bSuccess )
818cdf0e10cSrcweir 	{
819cdf0e10cSrcweir 		maOptionDescTxt.SetText( mrSane.GetOptionName( mnCurrentOption ) );
820cdf0e10cSrcweir 		maOptionDescTxt.Show( sal_True );
821cdf0e10cSrcweir 		maStringEdit.SetText( String( aValue, osl_getThreadTextEncoding() ) );
822cdf0e10cSrcweir 		maStringEdit.Show( sal_True );
823cdf0e10cSrcweir 	}
824cdf0e10cSrcweir }
825cdf0e10cSrcweir 
EstablishStringRange()826cdf0e10cSrcweir void SaneDlg::EstablishStringRange()
827cdf0e10cSrcweir {
828cdf0e10cSrcweir 	const char** ppStrings = mrSane.GetStringConstraint( mnCurrentOption );
829cdf0e10cSrcweir 	maStringRangeBox.Clear();
830cdf0e10cSrcweir 	for( int i = 0; ppStrings[i] != 0; i++ )
831cdf0e10cSrcweir 		maStringRangeBox.InsertEntry( String( ppStrings[i], osl_getThreadTextEncoding() ) );
832cdf0e10cSrcweir 	ByteString aValue;
833cdf0e10cSrcweir 	mrSane.GetOptionValue( mnCurrentOption, aValue );
834cdf0e10cSrcweir 	maStringRangeBox.SelectEntry( String( aValue, osl_getThreadTextEncoding() ) );
835cdf0e10cSrcweir 	maStringRangeBox.Show( sal_True );
836cdf0e10cSrcweir 	maOptionDescTxt.SetText( mrSane.GetOptionName( mnCurrentOption ) );
837cdf0e10cSrcweir 	maOptionDescTxt.Show( sal_True );
838cdf0e10cSrcweir }
839cdf0e10cSrcweir 
EstablishQuantumRange()840cdf0e10cSrcweir void SaneDlg::EstablishQuantumRange()
841cdf0e10cSrcweir {
842cdf0e10cSrcweir 	if( mpRange )
843cdf0e10cSrcweir 	{
844cdf0e10cSrcweir 		delete [] mpRange;
845cdf0e10cSrcweir 		mpRange = 0;
846cdf0e10cSrcweir 	}
847cdf0e10cSrcweir 	int nValues = mrSane.GetRange( mnCurrentOption, mpRange );
848cdf0e10cSrcweir 	if( nValues == 0 )
849cdf0e10cSrcweir 	{
850cdf0e10cSrcweir 		mfMin = mpRange[ 0 ];
851cdf0e10cSrcweir 		mfMax = mpRange[ 1 ];
852cdf0e10cSrcweir 		delete [] mpRange;
853cdf0e10cSrcweir 		mpRange = 0;
854cdf0e10cSrcweir 		EstablishNumericOption();
855cdf0e10cSrcweir 	}
856cdf0e10cSrcweir 	else if( nValues > 0 )
857cdf0e10cSrcweir 	{
858cdf0e10cSrcweir 		char pBuf[ 256 ];
859cdf0e10cSrcweir 		maQuantumRangeBox.Clear();
860cdf0e10cSrcweir 		mfMin = mpRange[ 0 ];
861cdf0e10cSrcweir 		mfMax = mpRange[ nValues-1 ];
862cdf0e10cSrcweir 		for( int i = 0; i < nValues; i++ )
863cdf0e10cSrcweir 		{
864cdf0e10cSrcweir 			sprintf( pBuf, "%g", mpRange[ i ] );
865cdf0e10cSrcweir 			maQuantumRangeBox.InsertEntry( String( pBuf, osl_getThreadTextEncoding() ) );
866cdf0e10cSrcweir 		}
867cdf0e10cSrcweir 		double fValue;
868cdf0e10cSrcweir 		if( mrSane.GetOptionValue( mnCurrentOption, fValue, mnCurrentElement ) )
869cdf0e10cSrcweir 		{
870cdf0e10cSrcweir 			sprintf( pBuf, "%g", fValue );
871cdf0e10cSrcweir 			maQuantumRangeBox.SelectEntry( String( pBuf, osl_getThreadTextEncoding() ) );
872cdf0e10cSrcweir 		}
873cdf0e10cSrcweir 		maQuantumRangeBox.Show( sal_True );
874cdf0e10cSrcweir 		String aText( mrSane.GetOptionName( mnCurrentOption ) );
875cdf0e10cSrcweir 		aText += ' ';
876cdf0e10cSrcweir 		aText += mrSane.GetOptionUnitName( mnCurrentOption );
877cdf0e10cSrcweir 		maOptionDescTxt.SetText( aText );
878cdf0e10cSrcweir 		maOptionDescTxt.Show( sal_True );
879cdf0e10cSrcweir 	}
880cdf0e10cSrcweir }
881cdf0e10cSrcweir 
EstablishNumericOption()882cdf0e10cSrcweir void SaneDlg::EstablishNumericOption()
883cdf0e10cSrcweir {
884cdf0e10cSrcweir 	sal_Bool bSuccess;
885cdf0e10cSrcweir 	double fValue;
886cdf0e10cSrcweir 
887cdf0e10cSrcweir 	bSuccess = mrSane.GetOptionValue( mnCurrentOption, fValue );
888cdf0e10cSrcweir 	if( ! bSuccess )
889cdf0e10cSrcweir 		return;
890cdf0e10cSrcweir 
891cdf0e10cSrcweir 	char pBuf[256];
892cdf0e10cSrcweir 	String aText( mrSane.GetOptionName( mnCurrentOption ) );
893cdf0e10cSrcweir 	aText += ' ';
894cdf0e10cSrcweir 	aText += mrSane.GetOptionUnitName( mnCurrentOption );
895cdf0e10cSrcweir 	if( mfMin != mfMax )
896cdf0e10cSrcweir 	{
897cdf0e10cSrcweir 		sprintf( pBuf, " < %g ; %g >", mfMin, mfMax );
898cdf0e10cSrcweir 		aText += String( pBuf, osl_getThreadTextEncoding() );
899cdf0e10cSrcweir 	}
900cdf0e10cSrcweir 	maOptionDescTxt.SetText( aText );
901cdf0e10cSrcweir 	maOptionDescTxt.Show( sal_True );
902cdf0e10cSrcweir 	sprintf( pBuf, "%g", fValue );
903cdf0e10cSrcweir 	maNumericEdit.SetText( String( pBuf, osl_getThreadTextEncoding() ) );
904cdf0e10cSrcweir 	maNumericEdit.Show( sal_True );
905cdf0e10cSrcweir }
906cdf0e10cSrcweir 
EstablishButtonOption()907cdf0e10cSrcweir void SaneDlg::EstablishButtonOption()
908cdf0e10cSrcweir {
909cdf0e10cSrcweir 	maOptionDescTxt.SetText( mrSane.GetOptionName( mnCurrentOption ) );
910cdf0e10cSrcweir 	maOptionDescTxt.Show( sal_True );
911cdf0e10cSrcweir 	maButtonOption.Show( sal_True );
912cdf0e10cSrcweir }
913cdf0e10cSrcweir 
914cdf0e10cSrcweir #define RECT_SIZE_PIX 7
915cdf0e10cSrcweir 
MouseMove(const MouseEvent & rMEvt)916cdf0e10cSrcweir void SaneDlg::MouseMove( const MouseEvent& rMEvt )
917cdf0e10cSrcweir {
918cdf0e10cSrcweir 	if( mbIsDragging )
919cdf0e10cSrcweir 	{
920cdf0e10cSrcweir 		Point aMousePos = rMEvt.GetPosPixel();
921cdf0e10cSrcweir 		// move into valid area
922cdf0e10cSrcweir 		Point aLogicPos = GetLogicPos( aMousePos );
923cdf0e10cSrcweir 		aMousePos = GetPixelPos( aLogicPos );
924cdf0e10cSrcweir 		switch( meDragDirection )
925cdf0e10cSrcweir 		{
926cdf0e10cSrcweir 			case TopLeft:		maTopLeft = aMousePos; break;
927cdf0e10cSrcweir 			case Top:			maTopLeft.Y() = aMousePos.Y(); break;
928cdf0e10cSrcweir 			case TopRight:
929cdf0e10cSrcweir 				maTopLeft.Y() = aMousePos.Y();
930cdf0e10cSrcweir 				maBottomRight.X() = aMousePos.X();
931cdf0e10cSrcweir 				break;
932cdf0e10cSrcweir 			case Right:			maBottomRight.X() = aMousePos.X(); break;
933cdf0e10cSrcweir 			case BottomRight:	maBottomRight = aMousePos; break;
934cdf0e10cSrcweir 			case Bottom:		maBottomRight.Y() = aMousePos.Y(); break;
935cdf0e10cSrcweir 			case BottomLeft:
936cdf0e10cSrcweir 				maTopLeft.X() = aMousePos.X();
937cdf0e10cSrcweir 				maBottomRight.Y() = aMousePos.Y();
938cdf0e10cSrcweir 				break;
939cdf0e10cSrcweir 			case Left:			maTopLeft.X() = aMousePos.X(); break;
940cdf0e10cSrcweir 			default: break;
941cdf0e10cSrcweir 		}
942cdf0e10cSrcweir 		int nSwap;
943cdf0e10cSrcweir 		if( maTopLeft.X() > maBottomRight.X() )
944cdf0e10cSrcweir 		{
945cdf0e10cSrcweir 			nSwap = maTopLeft.X();
946cdf0e10cSrcweir 			maTopLeft.X() = maBottomRight.X();
947cdf0e10cSrcweir 			maBottomRight.X() = nSwap;
948cdf0e10cSrcweir 		}
949cdf0e10cSrcweir 		if( maTopLeft.Y() > maBottomRight.Y() )
950cdf0e10cSrcweir 		{
951cdf0e10cSrcweir 			nSwap = maTopLeft.Y();
952cdf0e10cSrcweir 			maTopLeft.Y() = maBottomRight.Y();
953cdf0e10cSrcweir 			maBottomRight.Y() = nSwap;
954cdf0e10cSrcweir 		}
955cdf0e10cSrcweir 		DrawDrag();
956cdf0e10cSrcweir 		UpdateScanArea( sal_False );
957cdf0e10cSrcweir 	}
958cdf0e10cSrcweir 	ModalDialog::MouseMove( rMEvt );
959cdf0e10cSrcweir }
960cdf0e10cSrcweir 
MouseButtonDown(const MouseEvent & rMEvt)961cdf0e10cSrcweir void SaneDlg::MouseButtonDown( const MouseEvent& rMEvt )
962cdf0e10cSrcweir {
963cdf0e10cSrcweir 	Point aMousePixel = rMEvt.GetPosPixel();
964cdf0e10cSrcweir 
965cdf0e10cSrcweir 	if( ! mbIsDragging  && mbDragEnable )
966cdf0e10cSrcweir 	{
967cdf0e10cSrcweir 		int nMiddleX = ( maBottomRight.X() - maTopLeft.X() ) / 2 - RECT_SIZE_PIX/2 + maTopLeft.X();
968cdf0e10cSrcweir 		int nMiddleY = ( maBottomRight.Y() - maTopLeft.Y() ) / 2 - RECT_SIZE_PIX/2 + maTopLeft.Y();
969cdf0e10cSrcweir 		if( aMousePixel.Y() >= maTopLeft.Y() &&
970cdf0e10cSrcweir 			aMousePixel.Y() < maTopLeft.Y() + RECT_SIZE_PIX )
971cdf0e10cSrcweir 		{
972cdf0e10cSrcweir 			if( aMousePixel.X() >= maTopLeft.X() &&
973cdf0e10cSrcweir 				aMousePixel.X() < maTopLeft.X() + RECT_SIZE_PIX )
974cdf0e10cSrcweir 			{
975cdf0e10cSrcweir 				meDragDirection = TopLeft;
976cdf0e10cSrcweir 				aMousePixel = maTopLeft;
977cdf0e10cSrcweir 				mbIsDragging = sal_True;
978cdf0e10cSrcweir 			}
979cdf0e10cSrcweir 			else if( aMousePixel.X() >= nMiddleX &&
980cdf0e10cSrcweir 					 aMousePixel.X() < nMiddleX + RECT_SIZE_PIX )
981cdf0e10cSrcweir 			{
982cdf0e10cSrcweir 				meDragDirection = Top;
983cdf0e10cSrcweir 				aMousePixel.Y() = maTopLeft.Y();
984cdf0e10cSrcweir 				mbIsDragging = sal_True;
985cdf0e10cSrcweir 			}
986cdf0e10cSrcweir 			else if( aMousePixel.X() > maBottomRight.X() - RECT_SIZE_PIX &&
987cdf0e10cSrcweir 					 aMousePixel.X() <= maBottomRight.X() )
988cdf0e10cSrcweir 			{
989cdf0e10cSrcweir 				meDragDirection = TopRight;
990cdf0e10cSrcweir 				aMousePixel = Point( maBottomRight.X(), maTopLeft.Y() );
991cdf0e10cSrcweir 				mbIsDragging = sal_True;
992cdf0e10cSrcweir 			}
993cdf0e10cSrcweir 		}
994cdf0e10cSrcweir 		else if( aMousePixel.Y() >= nMiddleY &&
995cdf0e10cSrcweir 				 aMousePixel.Y() < nMiddleY + RECT_SIZE_PIX )
996cdf0e10cSrcweir 		{
997cdf0e10cSrcweir 			if( aMousePixel.X() >= maTopLeft.X() &&
998cdf0e10cSrcweir 				aMousePixel.X() < maTopLeft.X() + RECT_SIZE_PIX )
999cdf0e10cSrcweir 			{
1000cdf0e10cSrcweir 				meDragDirection = Left;
1001cdf0e10cSrcweir 				aMousePixel.X() = maTopLeft.X();
1002cdf0e10cSrcweir 				mbIsDragging = sal_True;
1003cdf0e10cSrcweir 			}
1004cdf0e10cSrcweir 			else if( aMousePixel.X() > maBottomRight.X() - RECT_SIZE_PIX &&
1005cdf0e10cSrcweir 					 aMousePixel.X() <= maBottomRight.X() )
1006cdf0e10cSrcweir 			{
1007cdf0e10cSrcweir 				meDragDirection = Right;
1008cdf0e10cSrcweir 				aMousePixel.X() = maBottomRight.X();
1009cdf0e10cSrcweir 				mbIsDragging = sal_True;
1010cdf0e10cSrcweir 			}
1011cdf0e10cSrcweir 		}
1012cdf0e10cSrcweir 		else if( aMousePixel.Y() <= maBottomRight.Y() &&
1013cdf0e10cSrcweir 				 aMousePixel.Y() > maBottomRight.Y() - RECT_SIZE_PIX )
1014cdf0e10cSrcweir 		{
1015cdf0e10cSrcweir 			if( aMousePixel.X() >= maTopLeft.X() &&
1016cdf0e10cSrcweir 				aMousePixel.X() < maTopLeft.X() + RECT_SIZE_PIX )
1017cdf0e10cSrcweir 			{
1018cdf0e10cSrcweir 				meDragDirection = BottomLeft;
1019cdf0e10cSrcweir 				aMousePixel = Point( maTopLeft.X(), maBottomRight.Y() );
1020cdf0e10cSrcweir 				mbIsDragging = sal_True;
1021cdf0e10cSrcweir 			}
1022cdf0e10cSrcweir 			else if( aMousePixel.X() >= nMiddleX &&
1023cdf0e10cSrcweir 					 aMousePixel.X() < nMiddleX + RECT_SIZE_PIX )
1024cdf0e10cSrcweir 			{
1025cdf0e10cSrcweir 				meDragDirection = Bottom;
1026cdf0e10cSrcweir 				aMousePixel.Y() = maBottomRight.Y();
1027cdf0e10cSrcweir 				mbIsDragging = sal_True;
1028cdf0e10cSrcweir 			}
1029cdf0e10cSrcweir 			else if( aMousePixel.X() > maBottomRight.X() - RECT_SIZE_PIX &&
1030cdf0e10cSrcweir 					 aMousePixel.X() <= maBottomRight.X() )
1031cdf0e10cSrcweir 			{
1032cdf0e10cSrcweir 				meDragDirection = BottomRight;
1033cdf0e10cSrcweir 				aMousePixel = maBottomRight;
1034cdf0e10cSrcweir 				mbIsDragging = sal_True;
1035cdf0e10cSrcweir 			}
1036cdf0e10cSrcweir 		}
1037cdf0e10cSrcweir 	}
1038cdf0e10cSrcweir 	if( mbIsDragging )
1039cdf0e10cSrcweir 	{
1040cdf0e10cSrcweir 		SetPointerPosPixel( aMousePixel );
1041cdf0e10cSrcweir 		DrawDrag();
1042cdf0e10cSrcweir 	}
1043cdf0e10cSrcweir 	ModalDialog::MouseButtonDown( rMEvt );
1044cdf0e10cSrcweir }
1045cdf0e10cSrcweir 
MouseButtonUp(const MouseEvent & rMEvt)1046cdf0e10cSrcweir void SaneDlg::MouseButtonUp( const MouseEvent& rMEvt )
1047cdf0e10cSrcweir {
1048cdf0e10cSrcweir 	if( mbIsDragging )
1049cdf0e10cSrcweir 	{
1050cdf0e10cSrcweir 		UpdateScanArea( sal_True );
1051cdf0e10cSrcweir 	}
1052cdf0e10cSrcweir 	mbIsDragging = sal_False;
1053cdf0e10cSrcweir 
1054cdf0e10cSrcweir 	ModalDialog::MouseButtonUp( rMEvt );
1055cdf0e10cSrcweir }
1056cdf0e10cSrcweir 
DrawRectangles(Point & rUL,Point & rBR)1057cdf0e10cSrcweir void SaneDlg::DrawRectangles( Point& rUL, Point& rBR )
1058cdf0e10cSrcweir {
1059cdf0e10cSrcweir 	int nMiddleX, nMiddleY;
1060cdf0e10cSrcweir 	Point aBL, aUR;
1061cdf0e10cSrcweir 
1062cdf0e10cSrcweir 	aUR = Point( rBR.X(), rUL.Y() );
1063cdf0e10cSrcweir 	aBL = Point( rUL.X(), rBR.Y() );
1064cdf0e10cSrcweir 	nMiddleX = ( rBR.X() - rUL.X() ) / 2 + rUL.X();
1065cdf0e10cSrcweir 	nMiddleY = ( rBR.Y() - rUL.Y() ) / 2 + rUL.Y();
1066cdf0e10cSrcweir 
1067cdf0e10cSrcweir 	DrawLine( rUL, aBL );
1068cdf0e10cSrcweir 	DrawLine( aBL, rBR );
1069cdf0e10cSrcweir 	DrawLine( rBR, aUR );
1070cdf0e10cSrcweir 	DrawLine( aUR, rUL );
1071cdf0e10cSrcweir 	DrawRect( Rectangle( rUL, Size( RECT_SIZE_PIX,RECT_SIZE_PIX ) ) );
1072cdf0e10cSrcweir 	DrawRect( Rectangle( aBL, Size( RECT_SIZE_PIX, -RECT_SIZE_PIX ) ) );
1073cdf0e10cSrcweir 	DrawRect( Rectangle( rBR, Size( -RECT_SIZE_PIX, -RECT_SIZE_PIX ) ) );
1074cdf0e10cSrcweir 	DrawRect( Rectangle( aUR, Size( -RECT_SIZE_PIX, RECT_SIZE_PIX ) ) );
1075cdf0e10cSrcweir 	DrawRect( Rectangle( Point( nMiddleX - RECT_SIZE_PIX/2, rUL.Y() ), Size( RECT_SIZE_PIX, RECT_SIZE_PIX ) ) );
1076cdf0e10cSrcweir 	DrawRect( Rectangle( Point( nMiddleX - RECT_SIZE_PIX/2, rBR.Y() ), Size( RECT_SIZE_PIX, -RECT_SIZE_PIX ) ) );
1077cdf0e10cSrcweir 	DrawRect( Rectangle( Point( rUL.X(), nMiddleY - RECT_SIZE_PIX/2 ), Size( RECT_SIZE_PIX, RECT_SIZE_PIX ) ) );
1078cdf0e10cSrcweir 	DrawRect( Rectangle( Point( rBR.X(), nMiddleY - RECT_SIZE_PIX/2 ), Size( -RECT_SIZE_PIX, RECT_SIZE_PIX ) ) );
1079cdf0e10cSrcweir }
1080cdf0e10cSrcweir 
DrawDrag()1081cdf0e10cSrcweir void SaneDlg::DrawDrag()
1082cdf0e10cSrcweir {
1083cdf0e10cSrcweir 	static Point aLastUL, aLastBR;
1084cdf0e10cSrcweir 
1085cdf0e10cSrcweir     if( ! mbDragEnable )
1086cdf0e10cSrcweir         return;
1087cdf0e10cSrcweir 
1088cdf0e10cSrcweir 	RasterOp eROP = GetRasterOp();
1089cdf0e10cSrcweir 	SetRasterOp( ROP_INVERT );
1090cdf0e10cSrcweir 	SetMapMode( MapMode( MAP_PIXEL ) );
1091cdf0e10cSrcweir 
1092cdf0e10cSrcweir 	if( mbDragDrawn )
1093cdf0e10cSrcweir 		DrawRectangles( aLastUL, aLastBR );
1094cdf0e10cSrcweir 
1095cdf0e10cSrcweir 	aLastUL = maTopLeft;
1096cdf0e10cSrcweir 	aLastBR = maBottomRight;
1097cdf0e10cSrcweir 	DrawRectangles( maTopLeft, maBottomRight );
1098cdf0e10cSrcweir 
1099cdf0e10cSrcweir 	mbDragDrawn = sal_True;
1100cdf0e10cSrcweir 	SetRasterOp( eROP );
1101cdf0e10cSrcweir 	SetMapMode( maMapMode );
1102cdf0e10cSrcweir }
1103cdf0e10cSrcweir 
GetPixelPos(const Point & rIn)1104cdf0e10cSrcweir Point SaneDlg::GetPixelPos( const Point& rIn )
1105cdf0e10cSrcweir {
1106cdf0e10cSrcweir 	Point aConvert(
1107cdf0e10cSrcweir 		( ( rIn.X() * PREVIEW_WIDTH ) /
1108cdf0e10cSrcweir 		  ( maMaxBottomRight.X() - maMinTopLeft.X() ) )
1109cdf0e10cSrcweir 		+ PREVIEW_UPPER_LEFT,
1110cdf0e10cSrcweir 		( ( rIn.Y() * PREVIEW_HEIGHT )
1111cdf0e10cSrcweir 		  / ( maMaxBottomRight.Y() - maMinTopLeft.Y() ) )
1112cdf0e10cSrcweir 		+ PREVIEW_UPPER_TOP );
1113cdf0e10cSrcweir 
1114cdf0e10cSrcweir 	return LogicToPixel( aConvert, maMapMode );
1115cdf0e10cSrcweir }
1116cdf0e10cSrcweir 
GetLogicPos(const Point & rIn)1117cdf0e10cSrcweir Point SaneDlg::GetLogicPos( const Point& rIn )
1118cdf0e10cSrcweir {
1119cdf0e10cSrcweir 	Point aConvert = PixelToLogic( rIn, maMapMode );
1120cdf0e10cSrcweir 	aConvert.X() -= PREVIEW_UPPER_LEFT;
1121cdf0e10cSrcweir 	aConvert.Y() -= PREVIEW_UPPER_TOP;
1122cdf0e10cSrcweir 	if( aConvert.X() < 0 )
1123cdf0e10cSrcweir 		aConvert.X() = 0;
1124cdf0e10cSrcweir 	if( aConvert.X() >= PREVIEW_WIDTH )
1125cdf0e10cSrcweir 		aConvert.X() = PREVIEW_WIDTH-1;
1126cdf0e10cSrcweir 	if( aConvert.Y() < 0 )
1127cdf0e10cSrcweir 		aConvert.Y() = 0;
1128cdf0e10cSrcweir 	if( aConvert.Y() >= PREVIEW_HEIGHT )
1129cdf0e10cSrcweir 		aConvert.Y() = PREVIEW_HEIGHT-1;
1130cdf0e10cSrcweir 
1131cdf0e10cSrcweir 	aConvert.X() *= ( maMaxBottomRight.X() - maMinTopLeft.X() );
1132cdf0e10cSrcweir 	aConvert.X() /= PREVIEW_WIDTH;
1133cdf0e10cSrcweir 	aConvert.Y() *= ( maMaxBottomRight.Y() - maMinTopLeft.Y() );
1134cdf0e10cSrcweir 	aConvert.Y() /= PREVIEW_HEIGHT;
1135cdf0e10cSrcweir 	return aConvert;
1136cdf0e10cSrcweir }
1137cdf0e10cSrcweir 
UpdateScanArea(sal_Bool bSend)1138cdf0e10cSrcweir void SaneDlg::UpdateScanArea( sal_Bool bSend )
1139cdf0e10cSrcweir {
1140cdf0e10cSrcweir     if( ! mbDragEnable )
1141cdf0e10cSrcweir         return;
1142cdf0e10cSrcweir 
1143cdf0e10cSrcweir 	Point aUL = GetLogicPos( maTopLeft );
1144cdf0e10cSrcweir 	Point aBR = GetLogicPos( maBottomRight );
1145cdf0e10cSrcweir 
1146cdf0e10cSrcweir 	maLeftField.SetValue( aUL.X() );
1147cdf0e10cSrcweir 	maTopField.SetValue( aUL.Y() );
1148cdf0e10cSrcweir 	maRightField.SetValue( aBR.X() );
1149cdf0e10cSrcweir 	maBottomField.SetValue( aBR.Y() );
1150cdf0e10cSrcweir 
1151cdf0e10cSrcweir 	if( ! bSend )
1152cdf0e10cSrcweir 		return;
1153cdf0e10cSrcweir 
1154cdf0e10cSrcweir 	if( mrSane.IsOpen() )
1155cdf0e10cSrcweir 	{
1156cdf0e10cSrcweir 		SetAdjustedNumericalValue( "tl-x", (double)aUL.X() );
1157cdf0e10cSrcweir 		SetAdjustedNumericalValue( "tl-y", (double)aUL.Y() );
1158cdf0e10cSrcweir 		SetAdjustedNumericalValue( "br-x", (double)aBR.X() );
1159cdf0e10cSrcweir 		SetAdjustedNumericalValue( "br-y", (double)aBR.Y() );
1160cdf0e10cSrcweir 	}
1161cdf0e10cSrcweir }
1162cdf0e10cSrcweir 
LoadState()1163cdf0e10cSrcweir sal_Bool SaneDlg::LoadState()
1164cdf0e10cSrcweir {
1165cdf0e10cSrcweir #ifdef USE_SAVE_STATE
1166cdf0e10cSrcweir 	int i;
1167cdf0e10cSrcweir 
1168cdf0e10cSrcweir 	if( ! Sane::IsSane() )
1169cdf0e10cSrcweir 		return sal_False;
1170cdf0e10cSrcweir 
1171cdf0e10cSrcweir 	const char* pEnv = getenv("HOME");
1172cdf0e10cSrcweir 	String aFileName( pEnv ? pEnv : "", osl_getThreadTextEncoding() );
1173cdf0e10cSrcweir 	aFileName += String( RTL_CONSTASCII_USTRINGPARAM( "/.so_sane_state" ) );
1174cdf0e10cSrcweir 	Config aConfig( aFileName );
1175cdf0e10cSrcweir 	if( ! aConfig.HasGroup( "SANE" ) )
1176cdf0e10cSrcweir 		return sal_False;
1177cdf0e10cSrcweir 
1178cdf0e10cSrcweir 	aConfig.SetGroup( "SANE" );
1179cdf0e10cSrcweir 	ByteString aString = aConfig.ReadKey( "SO_LastSaneDevice" );
1180cdf0e10cSrcweir 	for( i = 0; i < Sane::CountDevices() && ! aString.Equals( ByteString( Sane::GetName( i ), osl_getThreadTextEncoding() ) ); i++ ) ;
1181cdf0e10cSrcweir 	if( i == Sane::CountDevices() )
1182cdf0e10cSrcweir 		return sal_False;
1183cdf0e10cSrcweir 
1184cdf0e10cSrcweir 	mrSane.Close();
1185cdf0e10cSrcweir 	mrSane.Open( aString.GetBuffer() );
1186cdf0e10cSrcweir 
1187cdf0e10cSrcweir 	DisableOption();
1188cdf0e10cSrcweir 	InitFields();
1189cdf0e10cSrcweir 
1190cdf0e10cSrcweir 	if( mrSane.IsOpen() )
1191cdf0e10cSrcweir 	{
1192cdf0e10cSrcweir 		int iMax = aConfig.GetKeyCount();
1193cdf0e10cSrcweir 		for( i = 0; i < iMax; i++ )
1194cdf0e10cSrcweir 		{
1195cdf0e10cSrcweir 			aString = aConfig.GetKeyName( i );
1196cdf0e10cSrcweir 			ByteString aValue = aConfig.ReadKey( i );
1197cdf0e10cSrcweir 			int nOption = mrSane.GetOptionByName( aString.GetBuffer() );
1198cdf0e10cSrcweir 			if( nOption != -1 )
1199cdf0e10cSrcweir 			{
1200cdf0e10cSrcweir 				if( aValue.CompareTo( "BOOL=", 5 ) == COMPARE_EQUAL )
1201cdf0e10cSrcweir 				{
1202cdf0e10cSrcweir 					aValue.Erase( 0, 5 );
1203cdf0e10cSrcweir 					sal_Bool aBOOL = (sal_Bool)aValue.ToInt32();
1204cdf0e10cSrcweir 					mrSane.SetOptionValue( nOption, aBOOL );
1205cdf0e10cSrcweir 				}
1206cdf0e10cSrcweir 				else if( aValue.CompareTo( "STRING=", 7 ) == COMPARE_EQUAL )
1207cdf0e10cSrcweir 				{
1208cdf0e10cSrcweir 					aValue.Erase( 0, 7 );
1209cdf0e10cSrcweir 					mrSane.SetOptionValue( nOption, String( aValue, osl_getThreadTextEncoding() ) );
1210cdf0e10cSrcweir 				}
1211cdf0e10cSrcweir 				else if( aValue.CompareTo( "NUMERIC=", 8 ) == COMPARE_EQUAL )
1212cdf0e10cSrcweir 				{
1213cdf0e10cSrcweir 					aValue.Erase( 0, 8 );
1214cdf0e10cSrcweir 					int nMax = aValue.GetTokenCount( ':' );
1215cdf0e10cSrcweir 					double fValue=0.0;
1216cdf0e10cSrcweir 					for( int n = 0; n < nMax ; n++ )
1217cdf0e10cSrcweir 					{
1218cdf0e10cSrcweir 						ByteString aSub = aValue.GetToken( n, ':' );
1219cdf0e10cSrcweir 						sscanf( aSub.GetBuffer(), "%lg", &fValue );
1220cdf0e10cSrcweir 						SetAdjustedNumericalValue( aString.GetBuffer(), fValue, n );
1221cdf0e10cSrcweir 					}
1222cdf0e10cSrcweir 				}
1223cdf0e10cSrcweir 			}
1224cdf0e10cSrcweir 		}
1225cdf0e10cSrcweir 	}
1226cdf0e10cSrcweir 
1227cdf0e10cSrcweir 	DisableOption();
1228cdf0e10cSrcweir 	InitFields();
1229cdf0e10cSrcweir 
1230cdf0e10cSrcweir 	return sal_True;
1231cdf0e10cSrcweir #else
1232cdf0e10cSrcweir 	return sal_False;
1233cdf0e10cSrcweir #endif
1234cdf0e10cSrcweir }
1235cdf0e10cSrcweir 
SaveState()1236cdf0e10cSrcweir void SaneDlg::SaveState()
1237cdf0e10cSrcweir {
1238cdf0e10cSrcweir #ifdef USE_SAVE_STATE
1239cdf0e10cSrcweir 	if( ! Sane::IsSane() )
1240cdf0e10cSrcweir 		return;
1241cdf0e10cSrcweir 
1242cdf0e10cSrcweir 	const char* pEnv = getenv( "HOME" );
1243cdf0e10cSrcweir 	String aFileName( pEnv ? pEnv : "", osl_getThreadTextEncoding() );
1244cdf0e10cSrcweir 	aFileName.AppendAscii( "/.so_sane_state" );
1245cdf0e10cSrcweir 
1246cdf0e10cSrcweir 	Config aConfig( aFileName );
1247cdf0e10cSrcweir 	aConfig.DeleteGroup( "SANE" );
1248cdf0e10cSrcweir 	aConfig.SetGroup( "SANE" );
1249cdf0e10cSrcweir 	aConfig.WriteKey( "SO_LastSANEDevice", ByteString( maDeviceBox.GetSelectEntry(), RTL_TEXTENCODING_UTF8 ) );
1250cdf0e10cSrcweir 
1251cdf0e10cSrcweir #ifdef SAVE_ALL_STATES
1252cdf0e10cSrcweir 	for( int i = 1; i < mrSane.CountOptions(); i++ )
1253cdf0e10cSrcweir 	{
1254cdf0e10cSrcweir 		String aOption=mrSane.GetOptionName( i );
1255cdf0e10cSrcweir 		SANE_Value_Type nType = mrSane.GetOptionType( i );
1256cdf0e10cSrcweir 		switch( nType )
1257cdf0e10cSrcweir 		{
1258cdf0e10cSrcweir 			case SANE_TYPE_BOOL:
1259cdf0e10cSrcweir 			{
1260cdf0e10cSrcweir 				sal_Bool bValue;
1261cdf0e10cSrcweir 				if( mrSane.GetOptionValue( i, bValue ) )
1262cdf0e10cSrcweir 				{
1263cdf0e10cSrcweir 					ByteString aString( "BOOL=" );
1264cdf0e10cSrcweir 					aString += (sal_uLong)bValue;
1265cdf0e10cSrcweir 					aConfig.WriteKey( aOption, aString );
1266cdf0e10cSrcweir 				}
1267cdf0e10cSrcweir 			}
1268cdf0e10cSrcweir 			break;
1269cdf0e10cSrcweir 			case SANE_TYPE_STRING:
1270cdf0e10cSrcweir 			{
1271cdf0e10cSrcweir 				String aString( "STRING=" );
1272cdf0e10cSrcweir 				String aValue;
1273cdf0e10cSrcweir 				if( mrSane.GetOptionValue( i, aValue ) )
1274cdf0e10cSrcweir 				{
1275cdf0e10cSrcweir 					aString += aValue;
1276cdf0e10cSrcweir 					aConfig.WriteKey( aOption, aString );
1277cdf0e10cSrcweir 				}
1278cdf0e10cSrcweir 			}
1279cdf0e10cSrcweir 			break;
1280cdf0e10cSrcweir 			case SANE_TYPE_FIXED:
1281cdf0e10cSrcweir 			case SANE_TYPE_INT:
1282cdf0e10cSrcweir 			{
1283cdf0e10cSrcweir 				String aString( "NUMERIC=" );
1284cdf0e10cSrcweir 				double fValue;
1285cdf0e10cSrcweir 				char buf[256];
1286cdf0e10cSrcweir 				for( int n = 0; n < mrSane.GetOptionElements( i ); n++ )
1287cdf0e10cSrcweir 				{
1288cdf0e10cSrcweir 					if( ! mrSane.GetOptionValue( i, fValue, n ) )
1289cdf0e10cSrcweir 						break;
1290cdf0e10cSrcweir 					if( n > 0 )
1291cdf0e10cSrcweir 						aString += ":";
1292cdf0e10cSrcweir 					sprintf( buf, "%lg", fValue );
1293cdf0e10cSrcweir 					aString += buf;
1294cdf0e10cSrcweir 				}
1295cdf0e10cSrcweir 				if( n >= mrSane.GetOptionElements( i ) )
1296cdf0e10cSrcweir 					aConfig.WriteKey( aOption, aString );
1297cdf0e10cSrcweir 			}
1298cdf0e10cSrcweir 			break;
1299cdf0e10cSrcweir 			default:
1300cdf0e10cSrcweir 				break;
1301cdf0e10cSrcweir 		}
1302cdf0e10cSrcweir  	}
1303cdf0e10cSrcweir #else
1304cdf0e10cSrcweir 	static char const* pSaveOptions[] = {
1305cdf0e10cSrcweir 		"resolution",
1306cdf0e10cSrcweir 		"tl-x",
1307cdf0e10cSrcweir 		"tl-y",
1308cdf0e10cSrcweir 		"br-x",
1309cdf0e10cSrcweir 		"br-y"
1310cdf0e10cSrcweir 	};
1311cdf0e10cSrcweir 	for( size_t i = 0;
1312cdf0e10cSrcweir 		 i < (sizeof(pSaveOptions)/sizeof(pSaveOptions[0]));
1313cdf0e10cSrcweir 		 i++ )
1314cdf0e10cSrcweir 	{
1315cdf0e10cSrcweir 		ByteString aOption = pSaveOptions[i];
1316cdf0e10cSrcweir 		int nOption = mrSane.GetOptionByName( pSaveOptions[i] );
1317cdf0e10cSrcweir 		if( nOption > -1 )
1318cdf0e10cSrcweir 		{
1319cdf0e10cSrcweir 			SANE_Value_Type nType = mrSane.GetOptionType( nOption );
1320cdf0e10cSrcweir 			switch( nType )
1321cdf0e10cSrcweir 			{
1322cdf0e10cSrcweir 				case SANE_TYPE_BOOL:
1323cdf0e10cSrcweir 				{
1324cdf0e10cSrcweir 					sal_Bool bValue;
1325cdf0e10cSrcweir 					if( mrSane.GetOptionValue( nOption, bValue ) )
1326cdf0e10cSrcweir 					{
1327cdf0e10cSrcweir 						ByteString aString( "BOOL=" );
1328cdf0e10cSrcweir 						aString += ByteString::CreateFromInt32(bValue);
1329cdf0e10cSrcweir 						aConfig.WriteKey( aOption, aString );
1330cdf0e10cSrcweir 					}
1331cdf0e10cSrcweir 				}
1332cdf0e10cSrcweir 				break;
1333cdf0e10cSrcweir 				case SANE_TYPE_STRING:
1334cdf0e10cSrcweir 				{
1335cdf0e10cSrcweir 					ByteString aString( "STRING=" );
1336cdf0e10cSrcweir 					ByteString aValue;
1337cdf0e10cSrcweir 					if( mrSane.GetOptionValue( nOption, aValue ) )
1338cdf0e10cSrcweir 					{
1339cdf0e10cSrcweir 						aString += aValue;
1340cdf0e10cSrcweir 						aConfig.WriteKey( aOption, aString );
1341cdf0e10cSrcweir 					}
1342cdf0e10cSrcweir 				}
1343cdf0e10cSrcweir 				break;
1344cdf0e10cSrcweir 				case SANE_TYPE_FIXED:
1345cdf0e10cSrcweir 				case SANE_TYPE_INT:
1346cdf0e10cSrcweir 				{
1347cdf0e10cSrcweir 					ByteString aString( "NUMERIC=" );
1348cdf0e10cSrcweir 					double fValue;
1349cdf0e10cSrcweir 					char buf[256];
1350cdf0e10cSrcweir 					int n;
1351cdf0e10cSrcweir 
1352cdf0e10cSrcweir 					for( n = 0; n < mrSane.GetOptionElements( nOption ); n++ )
1353cdf0e10cSrcweir 					{
1354cdf0e10cSrcweir 						if( ! mrSane.GetOptionValue( nOption, fValue, n ) )
1355cdf0e10cSrcweir 							break;
1356cdf0e10cSrcweir 						if( n > 0 )
1357cdf0e10cSrcweir 							aString += ":";
1358cdf0e10cSrcweir 						sprintf( buf, "%lg", fValue );
1359cdf0e10cSrcweir 						aString += buf;
1360cdf0e10cSrcweir 					}
1361cdf0e10cSrcweir 					if( n >= mrSane.GetOptionElements( nOption ) )
1362cdf0e10cSrcweir 						aConfig.WriteKey( aOption, aString );
1363cdf0e10cSrcweir 				}
1364cdf0e10cSrcweir 				break;
1365cdf0e10cSrcweir 				default:
1366cdf0e10cSrcweir 					break;
1367cdf0e10cSrcweir 			}
1368cdf0e10cSrcweir 		}
1369cdf0e10cSrcweir 	}
1370cdf0e10cSrcweir #endif
1371cdf0e10cSrcweir #endif
1372cdf0e10cSrcweir }
1373cdf0e10cSrcweir 
SetAdjustedNumericalValue(const char * pOption,double fValue,int nElement)1374cdf0e10cSrcweir sal_Bool SaneDlg::SetAdjustedNumericalValue(
1375cdf0e10cSrcweir 	const char* pOption,
1376cdf0e10cSrcweir 	double fValue,
1377cdf0e10cSrcweir 	int nElement )
1378cdf0e10cSrcweir {
1379cdf0e10cSrcweir 	int nOption;
1380cdf0e10cSrcweir 	if( ! Sane::IsSane() || ! mrSane.IsOpen() || ( nOption = mrSane.GetOptionByName( pOption ) ) == -1 )
1381cdf0e10cSrcweir 		return sal_False;
1382cdf0e10cSrcweir 
1383cdf0e10cSrcweir 	if( nElement < 0 || nElement >= mrSane.GetOptionElements( nOption ) )
1384cdf0e10cSrcweir 		return sal_False;
1385cdf0e10cSrcweir 
1386cdf0e10cSrcweir 	double* pValues = NULL;
1387cdf0e10cSrcweir 	int nValues;
1388cdf0e10cSrcweir 	if( ( nValues = mrSane.GetRange( nOption, pValues ) ) < 0 )
1389cdf0e10cSrcweir 		return sal_False;
1390cdf0e10cSrcweir 
1391cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
1392cdf0e10cSrcweir 	fprintf( stderr, "SaneDlg::SetAdjustedNumericalValue( \"%s\", %lg ) ",
1393cdf0e10cSrcweir 			 pOption, fValue );
1394cdf0e10cSrcweir #endif
1395cdf0e10cSrcweir 
1396cdf0e10cSrcweir 	if( nValues )
1397cdf0e10cSrcweir 	{
1398cdf0e10cSrcweir 		int nNearest = 0;
1399cdf0e10cSrcweir 		double fNearest = 1e6;
1400cdf0e10cSrcweir 		for( int i = 0; i < nValues; i++ )
1401cdf0e10cSrcweir 		{
1402cdf0e10cSrcweir 			if( fabs( fValue - pValues[ i ] ) < fNearest )
1403cdf0e10cSrcweir 			{
1404cdf0e10cSrcweir 				fNearest = fabs( fValue - pValues[ i ] );
1405cdf0e10cSrcweir 				nNearest = i;
1406cdf0e10cSrcweir 			}
1407cdf0e10cSrcweir 		}
1408cdf0e10cSrcweir 		fValue = pValues[ nNearest ];
1409cdf0e10cSrcweir 	}
1410cdf0e10cSrcweir 	else
1411cdf0e10cSrcweir 	{
1412cdf0e10cSrcweir 		if( fValue < pValues[0] )
1413cdf0e10cSrcweir 			fValue = pValues[0];
1414cdf0e10cSrcweir 		if( fValue > pValues[1] )
1415cdf0e10cSrcweir 			fValue = pValues[1];
1416cdf0e10cSrcweir 	}
1417cdf0e10cSrcweir 	delete [] pValues;
1418cdf0e10cSrcweir 	mrSane.SetOptionValue( nOption, fValue, nElement );
1419cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
1420cdf0e10cSrcweir 	fprintf( stderr, "yields %lg\n", fValue );
1421cdf0e10cSrcweir #endif
1422cdf0e10cSrcweir 
1423cdf0e10cSrcweir 
1424cdf0e10cSrcweir 	return sal_True;
1425cdf0e10cSrcweir }
1426