File: appRuler.c

package info (click to toggle)
ted 2.11-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,064 kB
  • ctags: 13,935
  • sloc: ansic: 120,446; makefile: 7,469; sh: 253
file content (406 lines) | stat: -rw-r--r-- 8,841 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/************************************************************************/
/*									*/
/*  Common resources used by Rulers.					*/
/*									*/
/************************************************************************/

#   include	"appFrameConfig.h"

#   include	<stdio.h>
#   include	<string.h>

#   define	y0	math_y0
#   define	y1	math_y1
#   include	<math.h>
#   undef	y0
#   undef	y1

#   include	<appUnit.h>
#   include	<appRuler.h>

#   include	<appDebugon.h>

#   ifndef	M_LN10
#	define	M_LN10	2.30258509299404568402
#   endif

void appInitRulerData(	RulerData *	rd )
    {
    rd->rdFontName= (const char *)0;

    rd->rdSizeAcross= 0;
    rd->rdSizeAlong= 0;
    rd->rdFontHeight= 0;

    rd->rdMinUsed= 0;
    rd->rdMaxUsed= 0;
    rd->rdExtraAfterMaxUsed= 0;

    rd->rdDocumentC0= 0;
    rd->rdDocumentC1= 0;
    rd->rdVisibleC0= 0;
    rd->rdVisibleC1= 0;
    rd->rdRulerC1= 0;

    rd->rdFontStruct= (APP_FONT *)0;
    appInitVerticalXFont( &(rd->rdVerticalFont) );
    appInitDrawingData( &(rd->rdDrawingData) );
    rd->rdDrawingDataAllocated= 0;
#   ifdef USE_MOTIF
    rd->rdBackStipple= None;
#   endif

    return;
    }

void appCleanRulerData(	RulerData *	rd )
    {
    if  ( rd->rdFontStruct )
	{ appDrawFreeFont( &(rd->rdDrawingData), rd->rdFontStruct );	}

    appCleanVerticalXFont( &(rd->rdVerticalFont), &(rd->rdDrawingData) );

#   ifdef USE_MOTIF
    if  ( rd->rdBackStipple )
	{
	XFreePixmap( rd->rdDrawingData.addDisplay, rd->rdBackStipple );
	}
#   endif

    appCleanDrawingData( &(rd->rdDrawingData) );

    return;
    }

void appRulerDrawBackground(	RulerData *	rd,
				int		x,
				int		y,
				int		wide,
				int		high )
    {
    AppDrawingData *	add= &(rd->rdDrawingData);

    appDrawSetForegroundColor( add, &(add->addBackColor) );

#   ifdef USE_MOTIF
    if  ( rd->rdBackStipple )
	{ XSetFillStyle( add->addDisplay, add->addGc, FillStippled ); }
#   endif

    appDrawFillRectangle( add, x, y, wide, high );

#   ifdef USE_MOTIF
    if  ( rd->rdBackStipple )
	{ XSetFillStyle( add->addDisplay, add->addGc, FillSolid ); }
#   endif

    return;
    }

void appRulerAllocateDrawingData(	RulerData *		rd,
					int			vertical,
					int			fontHeight,
					double			magnification,
					APP_WIDGET		w )
    {
    AppDrawingData *	add= &(rd->rdDrawingData);
    char		name[128];

    rd->rdFontHeight= fontHeight;

    if  ( appSetDrawingDataForWidget( w, magnification, add ) )
	{ LDEB(1);	}

#   ifdef USE_MOTIF
    XSetLineAttributes( add->addDisplay, add->addGc,
				    1, LineSolid, CapProjecting, JoinMiter );
#   endif

#   ifdef USE_GTK
    gdk_gc_set_line_attributes( add->addGc,
		    1, GDK_LINE_SOLID, GDK_CAP_PROJECTING, GDK_JOIN_MITER );
#   endif

    if  ( rd->rdFontName )
	{
	sprintf( name, rd->rdFontName, fontHeight );

	rd->rdFontStruct= appDrawOpenFont( add, name );
	}
    else{ strcpy( name, "(None)" );	}

    if  ( ! rd->rdFontStruct )
	{
	SXDEB(name,rd->rdFontStruct);
	sprintf( name, "-*-*-medium-r-*-*-%d-*-*-*-m-*-iso8859-*",
							    fontHeight );

	rd->rdFontStruct= appDrawOpenFont( add, name );
	}

    if  ( vertical							&&
	  rd->rdFontStruct						&&
	  appOpenVerticalXFont( &(rd->rdVerticalFont), add, name, -1,
						&(add->addBackColor) )	)
	{ SDEB(name);	}

    if  ( rd->rdFontStruct )
	{ appDrawSetFont( add, rd->rdFontStruct );	}
    else{ SXDEB(name,rd->rdFontStruct);			}

#   ifdef USE_MOTIF
    if  ( DefaultDepth( add->addDisplay, add->addScreen ) <= 2 )
	{
	int		i;

	for ( i= 0; i < 16; i++ )
	    {
	    memset( name+ 8* i+ 0, 0xaa, 4 );
	    memset( name+ 8* i+ 4, 0x55, 4 );
	    }

	rd->rdBackStipple= XCreateBitmapFromData( add->addDisplay,
					    add->addDrawable, name, 32, 32 );
	if  ( ! rd->rdBackStipple )
	    { XDEB(rd->rdBackStipple);	}

	appDrawBlackColor( add, &(add->addBackColor) );

	XSetStipple( add->addDisplay, add->addGc, rd->rdBackStipple );
	}
#   endif

    rd->rdDrawingDataAllocated= 1;

    return;
    }

int appRulerTextWidth(	RulerData *	rd,
			const char *	s,
			int		len )
    {
    if  ( rd->rdFontStruct )
	{
	AppDrawingData *	add= &(rd->rdDrawingData);

	int			wide;
	int			fontAscent;
	int			fontDescent;

	appDrawTextExtents( &wide, &fontAscent, &fontDescent, add,
						rd->rdFontStruct, s, len );

	return  wide;
	}
    else{ return  (6*rd->rdFontHeight*len)/10;			}
    }

void appRulerDrawVerticalString(	RulerData *	rd,
					int		x,
					int		y,
					char *		s,
					int		len )
    {
    AppDrawingData *	add= &(rd->rdDrawingData);

    appDrawVerticalString( &(rd->rdVerticalFont), add, x, y, s, len );

    return;
    }

/************************************************************************/
/*									*/
/*  Calculate suitable distances for Texts, tags, ticks			*/
/*									*/
/*  unitsPerTick= unitsPerTag/ticksPerTag;				*/
/*  screenUnitsPerTick= unitsPerTick* magnification.			*/
/*  pixelsPerTick= FAC* screenUnitsPerTick.				*/
/*									*/
/************************************************************************/

static double appRulerPerItem(	double		unitsPerItem )
    {
    double aa;
    double bb;

    aa= log( unitsPerItem )/ M_LN10;
    bb= (int)( aa+ 20 )- 20;

    if  ( aa- bb > log( 5.0 )/ M_LN10	)
	{ aa= bb+ 1;	}
    else{
	if  ( aa- bb > log( 2.0 )/ M_LN10	)
	    { aa= bb+ log( 5.0 )/ M_LN10;	}
	else{
	    if  ( aa- bb > 0.0			)
		{ aa= bb+ log( 2.0 )/ M_LN10;	}
	    }
	}

    return exp( M_LN10* aa );
    }

void appRulerCalculateIncrements(	RulerData *	rd,
					double		magnifiedPixelsPerTwip,
					double		magnification )
    {
    double		mmPerUnit;

    double		unitsPerText;
    double		unitsPerTag;
    double		unitsPerTick;

    int			tagsPerText;

    switch( rd->rdUnit )
	{
	case UNITtyMM:		mmPerUnit= 1.0;	break;
	case UNITtyCM:		mmPerUnit= 10.0;	break;
	case UNITtyINCH:	mmPerUnit= 24.4;	break;
	case UNITtyPOINTS:	mmPerUnit= 25.4/ 72.0;	break;
	case UNITtyPICAS:	mmPerUnit= 25.4/ 6.0;	break;
	default:
	    LDEB(rd->rdUnit);
	    rd->rdUnit= UNITtyMM;
	    mmPerUnit= 1.0;
	    break;
	}

    rd->rdTwipsPerUnit= appUnitToTwips( 1.0, rd->rdUnit );
    rd->rdMagnifiedPixelsPerTwip= magnifiedPixelsPerTwip;

    unitsPerText= appRulerPerItem( ( 30.0/ mmPerUnit )/ magnification );
    unitsPerTag=  appRulerPerItem( (  8.0/ mmPerUnit )/ magnification );
    unitsPerTick= appRulerPerItem( (  3.0/ mmPerUnit )/ magnification );

    tagsPerText= unitsPerText/ unitsPerTag+ 0.49;
    rd->rdTicksPerTag= unitsPerTag/ unitsPerTick+ 0.49;

    rd->rdTicksPerText= tagsPerText* rd->rdTicksPerTag;
    rd->rdUnitsPerTick= unitsPerText/ rd->rdTicksPerText;

    return;
    }

void appRulerTagText(	char *		to,
			int *		pWide,
			int *		pLen,
			RulerData *	rd,
			double		units )
    {
    char	scratch[30];
    char *	s;

    int		wide;
    int		len;

    sprintf ( scratch, "%12.3g", units );
    s= scratch; while( *s == ' ' ) { s++;	}
    len= strlen( s );

    wide= appRulerTextWidth( rd, s, len );

    strcpy( to, s ); *pWide= wide; *pLen= len;

    return;
    }

void appScrollHorizontalRuler(		RulerData *		rd,
					DocumentRectangle *	drClip,
					int			d )
    {
    int				wide= rd->rdMaxUsed+ 1;
    int				high= rd->rdSizeAcross;

    AppDrawingData *		add= &(rd->rdDrawingData);

    int				x0;

    rd->rdVisibleC0 += d;
    rd->rdVisibleC1 += d;

    x0= rd->rdMinUsed;

    drClip->drY0= 0;
    drClip->drY1= high;

    if  ( d > 0 )
	{
	if  ( wide > d )
	    {
	    appDrawMoveArea( add, d+ x0, 0, wide- d- x0, high, x0, 0 );

	    drClip->drX0= rd->rdVisibleC1- d;
	    drClip->drX1= rd->rdVisibleC1;
	    }
	else{
	    drClip->drX0= rd->rdVisibleC0;
	    drClip->drX1= rd->rdVisibleC1;
	    }
	}
    else{
	if  ( high+ d > 0 )
	    {
	    appDrawMoveArea( add, x0, 0, wide+ d- x0, high, x0- d, 0 );

	    drClip->drX0= rd->rdVisibleC0;
	    drClip->drX1= rd->rdVisibleC0- d;
	    }
	else{
	    drClip->drX0= rd->rdVisibleC0;
	    drClip->drX1= rd->rdVisibleC1;
	    }
	}

    return;
    }

void appScrollVerticalRuler(		RulerData *		rd,
					DocumentRectangle *	drClip,
					int			d )
    {
    int				high= rd->rdMaxUsed+ 1;
    int				wide= rd->rdSizeAcross;

    AppDrawingData *		add= &(rd->rdDrawingData);

    int				y0;

    rd->rdVisibleC0 += d;
    rd->rdVisibleC1 += d;

    y0= rd->rdMinUsed;

    drClip->drX0= 0;
    drClip->drX1= wide;

    if  ( d > 0 )
	{
	if  ( high > d )
	    {
	    appDrawMoveArea( add, 0, d+ y0, wide, high- d- y0, 0, y0 );

	    drClip->drY0= rd->rdVisibleC1- d;
	    drClip->drY1= rd->rdVisibleC1;
	    }
	else{
	    drClip->drY0= rd->rdVisibleC0;
	    drClip->drY1= rd->rdVisibleC1;
	    }
	}
    else{
	if  ( high+ d > 0 )
	    {
	    appDrawMoveArea( add, 0, y0, wide, high+ d- y0, 0, y0- d );

	    drClip->drY0= rd->rdVisibleC0;
	    drClip->drY1= rd->rdVisibleC0- d;
	    }
	else{
	    drClip->drY0= rd->rdVisibleC0;
	    drClip->drY1= rd->rdVisibleC1;
	    }
	}

    return;
    }