summaryrefslogtreecommitdiff
path: root/xc/programs/Xserver/PEX5/ddpex/mi/level4/miTraverse.c
blob: 3c4f4195285564a82ca2fe1b292c5a1842d945b2 (plain)
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/* $TOG: miTraverse.c /main/7 1998/02/10 12:43:58 kaleb $ */
/*

Copyright 1989, 1990, 1991, 1998  The Open Group

All Rights Reserved.

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall
not be used in advertising or otherwise to promote the sale, use or
other dealings in this Software without prior written authorization
from The Open Group.


Copyright 1989, 1990, 1991 by Sun Microsystems, Inc. 
All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the name of Sun Microsystems
not be used in advertising or publicity pertaining to distribution
of the software without specific, written prior permission.

SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

*/
/* $XFree86: xc/programs/Xserver/PEX5/ddpex/mi/level4/miTraverse.c,v 1.8 1999/01/31 12:21:29 dawes Exp $ */

#include "miWks.h"
#include "PEXproto.h"
#include "PEXprotost.h"
#include "miRender.h"
#include "miPick.h"
#include "miStruct.h"
#include "miStrMacro.h"
#include "pexos.h"


extern void     InquirePickStatus();
extern void     InquireSearchStatus();

extern ddpex3rtn BeginStructure();
extern ddpex3rtn EndStructure();
extern ocTableType ExecuteOCTable[];

ddpex4rtn traverser();

#ifdef DDTEST
#define ASSURE(test) \
    if (!(test)) {  \
	ErrorF( "test \n"); \
	ErrorF( "Failed: Line %d, File %s\n\n", __LINE__, __FILE__); \
    }
#else
#define ASSURE(test)
#endif				/* DDTEST */

static          ddBOOL
pickES (pRend, p_trav_state, p_str, depth, curr_offset)
    ddRendererPtr	    pRend;
    miTraverserState    *p_trav_state;
    diStructHandle	p_str;	        /* current structure */
    ddSHORT		depth;		/* how far down in structures */
    ddULONG		curr_offset;
{

    if ((p_str->id == p_trav_state->p_curr_pick_el->structure->id) &&
	    (curr_offset == p_trav_state->p_curr_pick_el->offset)) {

	  if (depth < pRend->pickStartPath->numObj) 
	      /* continue following start path */
	      p_trav_state->p_curr_pick_el++;
	  else 
	      /* at end of start path; time to start traversal */
	      p_trav_state->exec_str_flag = ES_YES;

	  return(MI_TRUE);
    }
    return (MI_FALSE);
}

static          ddBOOL
searchES(pSC, p_trav_state, p_str, depth, curr_offset)
    ddSCStr		    *pSC;
    miTraverserState    *p_trav_state;
    diStructHandle	p_str;	        /* current structure */
    ddSHORT		depth;		/* how far down in structures */
    ddULONG		curr_offset;
{
    if ((p_str->id == p_trav_state->p_curr_sc_el->structure->id) &&
	    (curr_offset == p_trav_state->p_curr_sc_el->offset)) {
	/* OK, this element is in the start path. Now if we're
	 * at the ceiling, don't follow the exec-str
	 */
	if ((pSC->ceiling == 1) || (depth < pSC->ceiling)) {
	    if (depth < pSC->startPath->numObj) 
	        /* continue following start path */
                p_trav_state->p_curr_sc_el++;
	    else 
	        /* at end of start path; time to start traversal */
	        p_trav_state->exec_str_flag = ES_YES;
	    return(MI_TRUE);
	}
    }
    return (MI_FALSE);
}

int
miTraverse(pWks )
    diWKSHandle     pWks;
{
    register ddOrdStruct    *pos;
    miWksPtr		    pwks = (miWksPtr) pWks->deviceData;
    ddpex4rtn		    err = Success;
    miTraverserState	    trav_state;
    DrawablePtr             pRealDrawable;


    if (    (pwks->pRend->pDrawable == NULL)
	 || (pwks->pRend->drawableId == PEXAlreadyFreed))
		return (BadDrawable);

    if (!pwks->postedStructs.numStructs || !pwks->pCurDrawable) return (Success);

    /* set exec_str_flag */
    trav_state.exec_str_flag = ES_YES;
    trav_state.p_curr_pick_el = (ddPickPath *) NULL;
    trav_state.p_curr_sc_el = (ddElementRef *) NULL;

    /* save drawable to be restored later */
    pRealDrawable = pwks->pRend->pDrawable;
    pwks->pRend->pDrawable = pwks->pCurDrawable;

    /**  call into ddPEX level III Begin Rendering   **/
    BeginRendering(pwks->pRend, pwks->pCurDrawable);

    /* traverse all posted structs */
    pos = pwks->postedStructs.postruct;
    while ((pos->next) && (err == Success)) {
	pos = pos->next;
	/* reset for each structure */
	trav_state.max_depth = 0;	
	trav_state.pickId = 0;	
        trav_state.ROCoffset =  0;


	if (MISTR_NUM_EL((miStructPtr) pos->pstruct->deviceData)) {
	    BeginStructure(pwks->pRend, pos->pstruct->id);

	    /*
	     * always start at the first element in the
	     * structure
	     */
	    err = traverser(	pwks->pRend, pos->pstruct, (ddULONG) 1,
				MISTR_NUM_EL((miStructPtr) pos->pstruct->deviceData),
				(diPMHandle)NULL, (ddSCStr *) NULL, &trav_state);

	    EndStructure(pwks->pRend);
	    pwks->displaySurface = PEXNotEmpty;
	}
    }

    EndRendering(pwks->pRend);

    pwks->pRend->pDrawable = pRealDrawable;

    if (err != Success) {
	/* do stuff here to return error */
	return(err);
    }

    return (err);
}				/* miTraverse */

/* this traverses server side structures */
/* startel  must be > 0
 * stopel must be <= num els in structure
 */
/*
 * begin/end structure keep track of the current path, 
 * but calling the level II OCs directly does not
 * so do this BEFORE calling the OCs
 */
#define	INCREMENT_OFFSET(pRend)			\
    if (pRend->curPath->numObj) 		\
       ((ddElementRef *)pRend->curPath->pList)[pRend->curPath->numObj-1].offset++

ddpex4rtn
traverser(pRend, pStruct, startel, stopel, pPM, pSC, p_trav_state)
ddRendererPtr	    pRend;
diStructHandle	    pStruct;
ddULONG		    startel;
ddULONG		    stopel;
diPMHandle	    pPM;
ddSCStr		    *pSC;
miTraverserState    *p_trav_state;
{
    register ddULONG	currOffset;
    miPickMeasureStr	*ppm;
    miGenericElementPtr	p_element;
    ddPointer		ddElement;	/* imp. dep. parsed element */
    diStructHandle	p_next_str;	/* execute structure structure */
    ddSHORT		depth;		/* how far down in structures */
    ddULONG		pickId, ROCoffset;
    miStructPtr		pstruct = (miStructPtr)(pStruct->deviceData);
    ddUSHORT		pickStatus, searchStatus;
    ddpex2rtn		err;
    ddPickPath		*pl;
    ddElementRef	*sl;
    int			zap;
    miPPLevel		myPickLevel, *pp;
    int			i;


    if (pPM)
	ppm = (miPickMeasureStr *) pPM->deviceData;
    else
	ppm = (miPickMeasureStr *) NULL;

    /*
     * set depth=MaxDepth here when called on way in, 
     * so depth is current depth on way out
     */
    p_trav_state->max_depth++;
    depth = p_trav_state->max_depth;
    /* same for pick id */
    pickId = p_trav_state->pickId;
    MISTR_FIND_EL(pstruct, startel, p_element);
    currOffset = startel;

    /* when calling traverser from ROC ROCoffset may be non-zero
       to account for prior ROCs, all other times this should
       be 0 and we set it to 0 here so recursion doesn't mess
       up the offsets when processing execute structure
    */
    ROCoffset = p_trav_state->ROCoffset;
    p_trav_state->ROCoffset = 0;

    /* do stuff for following search start path */
    if (pSC) {
        /* if following start path, and its at the last element_ref
         * in the start path, and its after the last element in
         * the element_ref, then its at the end of the start
         * path and searching should begin
         */
        if ( (p_trav_state->exec_str_flag == ES_FOLLOW_SEARCH) &&
             (depth == pSC->startPath->numObj) &&
    	 (currOffset > p_trav_state->p_curr_sc_el->offset) )
    	p_trav_state->exec_str_flag = ES_YES;
    }

    /* do stuff for following pick start path */
    if (pPM) {
        if ( (p_trav_state->exec_str_flag == ES_FOLLOW_PICK) &&
             (depth == pRend->pickStartPath->numObj) &&
	     (currOffset > p_trav_state->p_curr_pick_el->offset) )
	  p_trav_state->exec_str_flag = ES_YES;
    }

    while (currOffset <= stopel) {
	ddElement = (ddPointer) (&(p_element->element));
	
	switch (MISTR_EL_TYPE(p_element)) {
	    case PEXOCExecuteStructure: {

		/*
		 * While inside this traverser, don't call level II execute 
		 * structure OC.  It is used in mixed mode traversals to get
		 * from client-side traversing to server-side traverser
		 */
		ddBOOL          go;

		p_next_str = ((diStructHandle) MISTR_GET_EXSTR_STR(p_element));
		if (p_trav_state->exec_str_flag == ES_FOLLOW_PICK)
		    go = pickES (pRend, p_trav_state, pStruct, depth, currOffset);
		else if (p_trav_state->exec_str_flag == ES_FOLLOW_SEARCH)
		    go = searchES(pSC, p_trav_state, pStruct, depth, currOffset);
		else if (p_trav_state->exec_str_flag == ES_YES)
		    go = MI_TRUE;
		else
		    go = MI_FALSE;

		if (go) {

		    BeginStructure(pRend, p_next_str->id);

		    /* build the pick path as we descend the hierarchy */
		    if (pPM) {
			myPickLevel.up = p_trav_state->p_pick_path;
			myPickLevel.pp.structure = pStruct;
			myPickLevel.pp.offset = currOffset + ROCoffset;
			myPickLevel.pp.pickid = pickId;
			p_trav_state->p_pick_path = &myPickLevel;
		    }

		    err = traverser(	pRend, p_next_str, (ddULONG) 1,
					MISTR_NUM_EL((miStructPtr) p_next_str->deviceData),
					pPM, pSC, p_trav_state);
		    if (err != Success)	return (err);
		    EndStructure(pRend);
		}

		/*
		 * We built the candidate pick path when it was found. We
		 * do nothing on the way out - except restore pointer.
	       */
	       if (pPM) {
		   p_trav_state->p_pick_path = myPickLevel.up; 
	       }


		/* do the same for searching, replacing the start
		 * path with the found path 
		 */
		if (pSC && (pSC->status == PEXFound)) {

		    sl = (ddElementRef *) pSC->startPath->pList;

		    zap = depth -1;

		    sl[zap].structure = pStruct;
		    sl[zap].offset = currOffset;

		    return (Success);
		} else 
		    /* popping out of Not Found */
		    if (pSC && (p_trav_state->exec_str_flag == ES_POP))
			return (Success);

		/*
		 * still picking or searching, so keep
		 * adjusting max_depth
		 */
		if (go)
		    p_trav_state->max_depth--;
		break;
	    }

	    case PEXOCPickId:

		/*
		 * For now, set own pick id and call into level II pick OC.
		 * Could do this in a level 4 pick procedure like execute struct
		 */
		pickId = p_trav_state->pickId = MISTR_GET_LABEL(p_element);

		INCREMENT_OFFSET(pRend);

		pRend->executeOCs[(int) (p_element->element.elementType)]
				(pRend, ddElement);

		break;

	    /* drawing primitives */
	    case PEXOCMarker:
	    case PEXOCMarker2D:
	    case PEXOCText:
	    case PEXOCText2D:
	    case PEXOCAnnotationText:
	    case PEXOCAnnotationText2D:
	    case PEXOCPolyline:
	    case PEXOCPolyline2D:
	    case PEXOCPolylineSet:
	    case PEXOCNurbCurve:
	    case PEXOCFillArea:
	    case PEXOCFillArea2D:
	    case PEXOCExtFillArea:
	    case PEXOCFillAreaSet:
	    case PEXOCFillAreaSet2D:
	    case PEXOCExtFillAreaSet:
	    case PEXOCTriangleStrip:
	    case PEXOCQuadrilateralMesh:
	    case PEXOCSOFAS:
	    case PEXOCNurbSurface:
	    case PEXOCCellArray:
	    case PEXOCCellArray2D:
	    case PEXOCExtCellArray:
	    case PEXOCGdp:
	    case PEXOCGdp2D:
		INCREMENT_OFFSET(pRend);

		/* if following pick or search path, don't call prims */
		if ( (p_trav_state->exec_str_flag == ES_FOLLOW_PICK) ||
		     (p_trav_state->exec_str_flag == ES_FOLLOW_SEARCH) )
		    break;

		if (MI_DDC_DO_PRIMS(pRend)) {

		    pRend->executeOCs[(int) (p_element->element.elementType)]
					(pRend, ddElement);
		    if (pSC) {
			InquireSearchStatus(pRend, &searchStatus);
			/* searchStatus is PEXFound or PEXNotFound */
			pSC->status = searchStatus;
			if (searchStatus == PEXFound) {
		    	    sl = (ddElementRef *) pSC->startPath->pList;

			    pSC->startPath->numObj = p_trav_state->max_depth;

			    if (pSC->startPath->maxObj < p_trav_state->max_depth) {
				pSC->startPath->pList =
				    (ddPointer) xrealloc(
					     pSC->startPath->pList,
					     p_trav_state->max_depth
					     * sizeof(ddElementRef));
				pSC->startPath->maxObj = p_trav_state->max_depth;
			    }
		            zap = depth -1;

		            sl[zap].structure = pStruct;
		            sl[zap].offset = currOffset;

			    p_trav_state->exec_str_flag = ES_POP;
			    return (Success);
			}
		    }
		    if (pPM) {
			InquirePickStatus(pRend, &pickStatus, p_trav_state);
			if (pickStatus == PEXOk) {
                          if (pRend->pickstr.state == DD_PICK_ALL) {
			      myPickLevel.up = p_trav_state->p_pick_path;
			      myPickLevel.pp.structure = pStruct;
			      myPickLevel.pp.offset = currOffset + ROCoffset;
			      myPickLevel.pp.pickid = pickId;

			      AddPickPathToList( pRend, depth, &myPickLevel);
			      ppm->status = pickStatus;

                          } else {


			    pl = (ddPickPath *) ppm->path->pList;

			    /*
			     * then do stuff to update
			     * pick measure path and stop
			     * traversing
			     */
			    ppm->path->numObj = p_trav_state->max_depth;

			    if (ppm->path->maxObj < p_trav_state->max_depth) {
				ppm->path->pList =
				    (ddPointer) xrealloc(ppm->path->pList,
						     p_trav_state->max_depth
						     * sizeof(ddPickPath));
				ppm->path->maxObj = p_trav_state->max_depth;
			    }

			    /*
			     * oh boy, this is where recursion is fun.
			     * In this current iteration of traverser we know 
			     * the last element in the path, so stuff this 
			     * element into the bottom/top of the pick path list
			     * depending on whether the top/bottom part was
			     * requested
			     */
			    if (ppm->pathOrder == PEXTopFirst)
				zap = depth - 1;
			    else	
				zap = p_trav_state->max_depth - depth;

			    pl[zap].structure = pStruct;
			    pl[zap].offset = currOffset + ROCoffset;
			    pl[zap].pickid = pickId;

			    /*
			     * we want to continue on, so we do not want to
			     * pop off to use the recursion.
			     * follow the linked list down
			     */
			    pp = p_trav_state->p_pick_path;
			    for (i = depth-1; i > 0; i-- ) {		
			       if (ppm->pathOrder == PEXTopFirst)
				   zap = i - 1;
			       else	
				   zap = p_trav_state->max_depth - i;
			      
			       pl[zap] = pp->pp; /* structure assignment */
			       pp = pp->up;
			    }
			    ppm->status = pickStatus;

			  }
			}
		    }
		}
		break;

	    default:	/* all others */

		INCREMENT_OFFSET(pRend);

		/* could maybe skip some if doing pick or search */
		if (MI_IS_PEX_OC(MISTR_EL_TYPE(p_element)))
		    pRend->executeOCs[(int) (p_element->element.elementType)]
					(pRend, ddElement);

		    break;	/* default */

	    }		/* end switch */


	/* do stuff for following search start path */
	if ( pSC ) {
	    /* now, if its at the ceiling, forget it */
	    if ( (depth == pSC->ceiling) && (pSC->ceiling != 1) &&
		 (currOffset >=  stopel) ) {
		pSC->status = PEXNotFound;
		p_trav_state->exec_str_flag = ES_POP;
		return (Success);
	    }

            /* do stuff for following search start path */
            if (pSC) {
                /* if following start path, and its at the last element_ref
                 * in the start path, and its after the last element in
                 * the element_ref, then its at the end of the start
                 * path and searching should begin
                 */
                if ( (p_trav_state->exec_str_flag == ES_FOLLOW_SEARCH) &&
                     (depth == pSC->startPath->numObj) &&
            	 (currOffset >= p_trav_state->p_curr_sc_el->offset) )
            	p_trav_state->exec_str_flag = ES_YES;
            }
        
	}

	/* do stuff for following search start path */
	if (pPM) {
	    /* if following start path, and its at the last pick_path 
	     * in the start path, and its after the last element in
	     * the pick_path, then its at the end of the start
	     * path and searching should begin
	     */
	    if ( (p_trav_state->exec_str_flag == ES_FOLLOW_PICK) &&
		 (depth == pRend->pickStartPath->numObj) &&
		 (currOffset >= p_trav_state->p_curr_pick_el->offset) )
	      p_trav_state->exec_str_flag = ES_YES;
	}

	/* go on to the next element */
	currOffset++;
	p_element = MISTR_NEXT_EL(p_element);
    }			/* while loop (while there are still elements
					in the structure) */

    return (Success);

}				/* traverser */


/* for mixed mode traversals, this supports the execute structure OC */
void
execute_structure_OC(pRend, pOC)
ddRendererPtr		pRend;
pexExecuteStructure	*pOC;
{
    diStructHandle	pstruct = *((diStructHandle *)&(pOC->id));
    miTraverserState	trav_state;
    ddpex4rtn		err = Success;
    if (MISTR_NUM_EL((miStructPtr) pstruct->deviceData)) {

	BeginStructure(pRend, pstruct->id);
	trav_state.exec_str_flag = ES_YES;
	trav_state.p_curr_pick_el = NULL;
	trav_state.p_curr_sc_el = NULL;
	trav_state.max_depth = 0;
        trav_state.pickId = 0;
        trav_state.ROCoffset =  0;

	err = traverser(    pRend, pstruct, (ddULONG)0, 
			    MISTR_NUM_EL((miStructPtr)pstruct->deviceData), 
			    (diPMHandle)NULL, (ddSCStr *)NULL, &trav_state);
	EndStructure(pRend);

    }
    return;
}