1 #   include	"appFrameConfig.h"
2 
3 #   include	<stdlib.h>
4 #   include	<stdio.h>
5 #   include	<string.h>
6 
7 #   include	<appDebugon.h>
8 #   include	<utilMemoryBuffer.h>
9 
10 #   include	"guiTextUtil.h"
11 #   include	"guiWidgets.h"
12 #   include	<geoString.h>
13 
14 /************************************************************************/
15 /*									*/
16 /*  Get a 'Length', or an 'integer' from a text widget.			*/
17 /*									*/
18 /************************************************************************/
19 
appGetLengthFromTextWidget(APP_WIDGET w,int * pValue,int * pChanged,int defaultUnit,int minValue,int adaptToMin,int maxValue,int adaptToMax)20 int appGetLengthFromTextWidget(		APP_WIDGET	w,
21 					int *		pValue,
22 					int *		pChanged,
23 					int		defaultUnit,
24 					int		minValue,
25 					int		adaptToMin,
26 					int		maxValue,
27 					int		adaptToMax )
28     {
29     int		value;
30 
31     char	scratch[50];
32     char *	s;
33 
34     s= appGetStringFromTextWidget( w );
35     if  ( geoLengthFromString( s, defaultUnit, &value )	||
36 	  ( ! adaptToMin && value < minValue )		||
37 	  ( ! adaptToMax && value > maxValue )		)
38 	{
39 	appFreeStringFromTextWidget( s );
40 
41 	appRefuseTextValue( w ); return -1;
42 	}
43 
44     geoLengthToString( scratch, *pValue, defaultUnit );
45     if  ( ! strcmp( scratch, s ) )
46 	{ value= *pValue;	}
47 
48     appFreeStringFromTextWidget( s );
49 
50     if  ( adaptToMin && value < minValue )
51 	{ value=  minValue;	}
52     if  ( adaptToMax && value > maxValue )
53 	{ value=  maxValue;	}
54 
55     *pChanged= ( value != *pValue );
56     *pValue= value;
57 
58     return 0;
59     }
60 
appGetIntegerFromTextWidget(APP_WIDGET w,int * pValue,int minValue,int adaptToMin,int maxValue,int adaptToMax)61 int appGetIntegerFromTextWidget(	APP_WIDGET	w,
62 					int *		pValue,
63 					int		minValue,
64 					int		adaptToMin,
65 					int		maxValue,
66 					int		adaptToMax )
67     {
68     int		value;
69 
70     char *	s;
71     char *	past;
72 
73     s= appGetStringFromTextWidget( w );
74 
75     value= strtol( s, &past, 10 );
76 
77     if  ( past != s )
78 	{
79 	while( *past == ' ' )
80 	    { past++;	}
81 	}
82 
83     if  ( past == s						||
84 	  *past							||
85 	  ( ! adaptToMin && value < minValue )			||
86 	  ( ! adaptToMax && value > maxValue )			)
87 	{
88 	appFreeStringFromTextWidget( s );
89 
90 	appRefuseTextValue( w ); return -1;
91 	}
92 
93     appFreeStringFromTextWidget( s );
94 
95     if  ( adaptToMin && value < minValue )
96 	{ value=  minValue;	}
97     if  ( adaptToMax && value > maxValue )
98 	{ value=  maxValue;	}
99 
100     *pValue= value;
101 
102     return 0;
103     }
104 
appGetDoubleFromTextWidget(APP_WIDGET w,double * pValue,double minValue,int adaptToMin,double maxValue,int adaptToMax)105 int appGetDoubleFromTextWidget(		APP_WIDGET	w,
106 					double *	pValue,
107 					double		minValue,
108 					int		adaptToMin,
109 					double		maxValue,
110 					int		adaptToMax )
111     {
112     double	value;
113 
114     char *	s;
115     char *	past;
116 
117     s= appGetStringFromTextWidget( w );
118 
119     value= strtod( s, &past );
120 
121     if  ( past != s )
122 	{
123 	while( *past == ' ' )
124 	    { past++;	}
125 	}
126 
127     if  ( past == s						||
128 	  *past							||
129 	  ( ! adaptToMin && value < minValue )			||
130 	  ( ! adaptToMax && value > maxValue )			)
131 	{
132 	appFreeStringFromTextWidget( s );
133 
134 	appRefuseTextValue( w ); return -1;
135 	}
136 
137     appFreeStringFromTextWidget( s );
138 
139     if  ( value < minValue )
140 	{ value=  minValue;	}
141     if  ( value > maxValue )
142 	{ value=  maxValue;	}
143 
144     *pValue= value;
145 
146     return 0;
147     }
148 
149 /************************************************************************/
150 /*									*/
151 /*  Insert an integer in a text widget.					*/
152 /*									*/
153 /************************************************************************/
154 
appLengthToTextWidget(APP_WIDGET w,int l,int unit)155 void appLengthToTextWidget(		APP_WIDGET	w,
156 					int		l,
157 					int		unit )
158     {
159     char	scratch[30];
160 
161     geoLengthToString( scratch, l, unit );
162     appStringToTextWidget( w, scratch );
163 
164     return;
165     }
166 
appIntegerToTextWidget(APP_WIDGET w,int n)167 void appIntegerToTextWidget(		APP_WIDGET	w,
168 					int		n )
169     {
170     char	scratch[20];
171 
172     sprintf( scratch, "%d", n );
173     appStringToTextWidget( w, scratch );
174 
175     return;
176     }
177 
appIntervalToTextWidget(APP_WIDGET w,int n1,int n2)178 void appIntervalToTextWidget(		APP_WIDGET	w,
179 					int		n1,
180 					int		n2 )
181     {
182     char	scratch[30];
183 
184     if  ( n1 == n2 )
185 	{ sprintf( scratch, "%d", n1 );			}
186     else{ sprintf( scratch, "%d - %d", n1, n2 );	}
187 
188     appStringToTextWidget( w, scratch );
189 
190     return;
191     }
192 
appRectangleToTextWidget(APP_WIDGET w,int row1,int row2,int col1,int col2)193 extern void appRectangleToTextWidget(	APP_WIDGET		w,
194 					int			row1,
195 					int			row2,
196 					int			col1,
197 					int			col2 )
198     {
199     char	scratch[50];
200     char *	to= scratch;
201 
202     if  ( row1 == row2 )
203 	{ sprintf( to, "%d", row1 );		}
204     else{ sprintf( to, "%d - %d", row1, row2 );	}
205 
206     strcat( scratch, ", " );
207     to= scratch+ strlen( scratch );
208 
209     if  ( col1 == col2 )
210 	{ sprintf( to, "%d", col1 );		}
211     else{ sprintf( to, "%d - %d", col1, col2 );	}
212 
213     appStringToTextWidget( w, scratch );
214     }
215 
appDoubleToTextWidget(APP_WIDGET w,double d)216 void appDoubleToTextWidget(		APP_WIDGET	w,
217 					double		d )
218     {
219     char	scratch[30];
220 
221     sprintf( scratch, "%g", d );
222     appStringToTextWidget( w, scratch );
223 
224     return;
225     }
226 
appBufferFromTextWidget(MemoryBuffer * mb,APP_WIDGET w)227 int appBufferFromTextWidget(		MemoryBuffer *	mb,
228 					APP_WIDGET	w )
229     {
230     char *	s= appGetStringFromTextWidget( w );
231 
232     if  ( utilMemoryBufferSetString( mb, s ) )
233 	{ LDEB(1); return -1;	}
234 
235     appFreeStringFromTextWidget( s );
236 
237     return 0;
238     }
239 
guiBufferToText(APP_WIDGET w,const MemoryBuffer * mb)240 void guiBufferToText(	APP_WIDGET		w,
241 			const MemoryBuffer *	mb )
242     {
243     appStringToTextWidget( w, utilMemoryBufferGetString( mb ) );
244 
245     return;
246     }
247