File: appPaperChooser.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 (484 lines) | stat: -rw-r--r-- 11,822 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
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
/************************************************************************/
/*									*/
/*  Choose a paper format common functionality.				*/
/*									*/
/************************************************************************/

#   include	"appFrameConfig.h"

#   include	<stdlib.h>
#   include	<string.h>

#   include	<appGeoString.h>
#   include	<appPaper.h>

#   include	"appPaperChooser.h"

#   include	<appDebugon.h>

/************************************************************************/
/*									*/
/*  Update the text widget.						*/
/*									*/
/************************************************************************/

void appPaperChooserShowWidthHeight(	PaperChooser *		pc,
				const DocumentGeometry *	dg,
				int				unitInt )
    {
    char			scratch[50];

    appGeoRectangleToString( scratch,
		    dg->dgPageWideTwips, dg->dgPageHighTwips, unitInt );

    appStringToTextWidget( pc->pcSizeText, scratch );

    return;
    }

/************************************************************************/
/*									*/
/*  Extract a paper size from the Text widget.				*/
/*									*/
/************************************************************************/

int appPaperChooserGetSize(	PropertyMask *		pChgMask,
				PaperChooser *		pc,
				int			unitType,
				DocumentGeometry *	dg )
    {
    char *		s;
    DocumentGeometry	dgHere;

    PropertyMask	updMask;
    PropertyMask	chgMask;

    dgHere= *dg;

    PROPmaskCLEAR( &updMask );
    PROPmaskCLEAR( &chgMask );

    PROPmaskADD( &updMask, DGpropPAGE_WIDTH );
    PROPmaskADD( &updMask, DGpropPAGE_HEIGHT );

    s= appGetStringFromTextWidget( pc->pcSizeText );

    if  ( appGeoRectangleFromString( s, unitType,
				    &(dgHere.dgPageWideTwips),
				    &(dgHere.dgPageHighTwips) )		||
	  dgHere.dgPageWideTwips <= 0					||
	  dgHere.dgPageWideTwips <= 0					)
	{
	appFreeStringFromTextWidget( s );

	appRefuseTextValue( pc->pcSizeText );

	return -1;
	}

    appFreeStringFromTextWidget( s );

    appSetDocumentGeometry( dg, &dgHere, &chgMask, &updMask );

    *pChgMask= chgMask;

    return 0;
    }


/************************************************************************/
/*									*/
/*  Fill the list of paper sizes.					*/
/*									*/
/************************************************************************/

void appPaperChooserFillMenu(	PaperChooser *		pc,
				const char *		customLabel,
				APP_BUTTON_CALLBACK	callback,
				void *			through )
    {
    const char *	label;
    int			i;
    int			gotSome= 0;

    if  ( ! pc->pcSizeOptions )
	{
	i= 0;
	while( ! appPaperGetInformation( i, (int *)0, (int *)0, &label ) )
	    { i++; }

	pc->pcCustomPaperSize= i++;
	pc->pcSizeOptionCount= i;

	pc->pcSizeOptions= realloc( pc->pcSizeOptions,
				pc->pcSizeOptionCount* sizeof(APP_WIDGET) );
	if  ( ! pc->pcSizeOptions )
	    { LXDEB(pc->pcSizeOptionCount,pc->pcSizeOptions); return;	}

	for ( i= 0; i < pc->pcSizeOptionCount; i++ )
	    { pc->pcSizeOptions[i]= (APP_WIDGET)0;	}
	}

    appEmptyOptionmenu( &(pc->pcOptionmenu) );

    i= 0;
    while( ! appPaperGetInformation( i, (int *)0, (int *)0, &label ) )
	{
	pc->pcSizeOptions[i]= appAddItemToOptionmenu( &(pc->pcOptionmenu),
					label, callback, through );

	if  ( i == 0 )
	    { gotSome= 1;	}

	i++;
	}

    pc->pcSizeOptions[i]= appAddItemToOptionmenu( &(pc->pcOptionmenu),
					    customLabel, callback, through );
    if  ( i == 0 )
	{ gotSome= 1;	}

    if  ( gotSome )
	{
	appSetOptionmenu( &(pc->pcOptionmenu), 0 );
	pc->pcSizeChosen= 0;
	}
    else{
	appSetOptionmenu( &(pc->pcOptionmenu), -1 );
	}

    appOptionmenuRefreshWidth( &(pc->pcOptionmenu) );

    return;
    }

/************************************************************************/
/*									*/
/*  Adapt to geometry.							*/
/*									*/
/************************************************************************/

void appPaperChooserAdaptToGeometry(	PaperChooser *		pc,
					int			unitType,
					const DocumentGeometry * dg,
					int			set )
    {
    int		i;

    appPaperChooserShowWidthHeight( pc, dg, unitType );

    if  ( dg->dgPageWideTwips > dg->dgPageHighTwips )
	{
	i= appPaperGetBySize( dg->dgPageHighTwips, dg->dgPageWideTwips );

	pc->pcLandscapeChosen= 1;
	}
    else{
	i= appPaperGetBySize( dg->dgPageWideTwips, dg->dgPageHighTwips );

	pc->pcLandscapeChosen= 0;
	}

    if  ( i < 0 )
	{ i= pc->pcCustomPaperSize;	}

    pc->pcSizeChosen= i;

    appSetOptionmenu( &(pc->pcOptionmenu), pc->pcSizeChosen );

    if  ( pc->pcPortraitRadio && pc->pcLandscapeRadio )
	{ appPageToolShowOrientation( pc );	}

    if  ( set )
	{
	pc->pcLandscapeSet= pc->pcLandscapeChosen;
	pc->pcSizeSet= pc->pcSizeChosen;
	}

    return;
    }

/************************************************************************/
/*									*/
/*  A paper size was chosen in the menu. As far as the paper chooser	*/
/*  is concerned no more needs to be done.				*/
/*									*/
/************************************************************************/

void appPaperSizeChosen(		APP_WIDGET		w,
					PaperChooser *		pc,
					DocumentGeometry *	dg,
					int			unitType )
    {
    int			i= -1;

    int			width;
    int			height;

    for ( i= 0; i < pc->pcSizeOptionCount; i++ )
	{
	if  ( w == pc->pcSizeOptions[i] )
	    { break;	}
	}

    if  ( i < 0 || i >= pc->pcSizeOptionCount )
	{ LLDEB(i,pc->pcSizeOptionCount); return;	}

    pc->pcSizeChosen= i;

    if  ( i == pc->pcCustomPaperSize )
	{
	PropertyMask		updMask;
	DocumentGeometry	dgScratch;

	PROPmaskCLEAR( &updMask );

	if  ( appPaperChooserGetSize( &updMask, pc, unitType, &dgScratch ) )
	    { LDEB(1); return;	}

	width= dgScratch.dgPageWideTwips;
	height= dgScratch.dgPageHighTwips;

	if  ( width > height )
	    { pc->pcLandscapeChosen= 1;	}
	else{ pc->pcLandscapeChosen= 0;	}
	}
    else{
	if  ( appPaperGetInformation( i, &width, &height, (const char **)0 ) )
	    { return;	}

	if  ( pc->pcLandscapeChosen )
	    {
	    int		swap;

	    swap= height; height= width; width= swap;
	    }
	}

    dg->dgPageWideTwips= width;
    dg->dgPageHighTwips= height;

    appPaperChooserShowWidthHeight( pc, dg, unitType );

    return;
    }

/************************************************************************/
/*									*/
/*  The paper size in the text field was activated by the user. As far	*/
/*  as the paper chooser is concerned no more needs to be done.		*/
/*									*/
/************************************************************************/

void appPaperSizeChanged(		PaperChooser *		pc,
					DocumentGeometry *	dg,
					int			unitType )
    {
    DocumentGeometry	dgScratch;
    PropertyMask	updMask;

    PROPmaskCLEAR( &updMask );

    dgScratch= *dg;

    if  ( appPaperChooserGetSize( &updMask, pc, unitType, &dgScratch ) )
	{ LDEB(1); return;	}

    if  ( pc->pcLandscapeChosen )
	{
	dg->dgPageWideTwips= dgScratch.dgPageHighTwips;
	dg->dgPageHighTwips= dgScratch.dgPageWideTwips;
	}
    else{
	dg->dgPageWideTwips= dgScratch.dgPageWideTwips;
	dg->dgPageHighTwips= dgScratch.dgPageHighTwips;
	}

    return;
    }

/************************************************************************/
/*									*/
/*  Show the orientation in the radiogroup.				*/
/*									*/
/************************************************************************/

void appPageToolShowOrientation(	PaperChooser *	pc )
    {
    if  ( ! pc->pcPortraitRadio || ! pc->pcLandscapeRadio )
	{ XXDEB(pc->pcPortraitRadio,pc->pcLandscapeRadio); return;	}

    if  ( pc->pcLandscapeChosen )
	{
	appGuiSetToggleState( pc->pcPortraitRadio,  0 );
	appGuiSetToggleState( pc->pcLandscapeRadio, 1 );
	}
    else{
	appGuiSetToggleState( pc->pcPortraitRadio,  1 );
	appGuiSetToggleState( pc->pcLandscapeRadio, 0 );
	}

    return;
    }

/************************************************************************/
/*									*/
/*  Make the widgets for a paper chooser.				*/
/*									*/
/************************************************************************/

void appMakePaperChooserWidgets(	APP_WIDGET		parent,
					const char *		title,
					PaperChooser *		pc,
					APP_TEXT_ACTIVATE_CALLBACK
								changedCallback,
					void *			through )
    {
    const int	textEnabled= 1;

    appMakeColumnFrameInColumn( &(pc->pcFrame),
				    &(pc->pcVerticalColumn), parent, title );


    appMakeOptionmenuInColumn( &(pc->pcOptionmenu), pc->pcVerticalColumn );

    appMakeTextInColumn( &(pc->pcSizeText), pc->pcVerticalColumn,
							    0, textEnabled );

    appGuiSetGotValueCallbackForText( pc->pcSizeText,
						changedCallback, through );
    
    return;
    }

/************************************************************************/
/*									*/
/*  Add Portrait/Landscape toggles to a paper chooser.			*/
/*									*/
/************************************************************************/

void appPaperChooserAddOrientationToggles( PaperChooser *	pc,
					const char *		portrait,
					const char *		landscape,
					APP_TOGGLE_CALLBACK	callback,
					void *			through )
    {
    const int	heightResizable= 0;

    pc->pcOrientationRow= appMakeRowInColumn( pc->pcVerticalColumn, 2,
							    heightResizable );

    pc->pcPortraitRadio= appMakeToggleInRow( pc->pcOrientationRow,
					    portrait, callback, through, 0 );

    pc->pcLandscapeRadio= appMakeToggleInRow( pc->pcOrientationRow,
					    landscape, callback, through, 1 );

    return;
    }

/************************************************************************/
/*									*/
/*  One of the orientation toggles was activated. As far as the paper	*/
/*  chooser is concerned no more needs to be done.			*/
/*									*/
/*  The contorted logic of this function is dictated by the fact that	*/
/*  GTK invokes callbacks for programmatic changes to the toggle	*/
/*  buttons. This is a curse we have to live with.			*/
/*									*/
/************************************************************************/

void appPaperOrientationChosen(		APP_WIDGET		w,
					void *			voidtbcs,
					int *			pChanged,
					PaperChooser *		pc,
					int			unitType,
					DocumentGeometry *	dg )
    {
    int			change= 0;
    int			set;

    set= appGuiGetToggleStateFromCallback( w, voidtbcs );

    if  ( w == pc->pcPortraitRadio )
	{
	if  ( pc->pcLandscapeChosen )
	    {
	    if  ( set )
		{
		pc->pcLandscapeChosen= 0;
		appGuiSetToggleState( pc->pcLandscapeRadio, 0 );
		change= 1;
		}
	    }
	else{
	    if  ( ! set )
		{ appGuiSetToggleState( pc->pcPortraitRadio, 1 );	}
	    }
	}

    if  ( w == pc->pcLandscapeRadio )
	{
	if  ( pc->pcLandscapeChosen )
	    {
	    if  ( ! set )
		{ appGuiSetToggleState( pc->pcLandscapeRadio, 1 );	}
	    }
	else{
	    if  ( set )
		{
		pc->pcLandscapeChosen= 1;
		appGuiSetToggleState( pc->pcPortraitRadio, 0 );
		change= 1;
		}
	    }
	}

    if  ( change )
	{
	int	swap;

	swap= dg->dgPageWideTwips;
	dg->dgPageWideTwips= dg->dgPageHighTwips;
	dg->dgPageHighTwips= swap;

	appPaperChooserShowWidthHeight( pc, dg, unitType );
	}

    *pChanged= change; return;
    }

/************************************************************************/
/*									*/
/*  Initialize a fresh PaperChooser.					*/
/*									*/
/************************************************************************/

void appCleanPaperChooser(	PaperChooser *	pc )
    {
    if  ( pc->pcSizeOptions )
	{ free( pc->pcSizeOptions );	}
    }

void appInitPaperChooser(	PaperChooser *	pc )
    {
    pc->pcFrame= (APP_WIDGET)0;
    pc->pcVerticalColumn= (APP_WIDGET)0;

    pc->pcSizeText= (APP_WIDGET)0;

    pc->pcOrientationRow= (APP_WIDGET)0;
    pc->pcPortraitRadio= (APP_WIDGET)0;
    pc->pcLandscapeRadio= (APP_WIDGET)0;

    pc->pcCustomPaperSize= -1;
    pc->pcSizeChosen= -1;
    pc->pcSizeSet= -1;
    pc->pcLandscapeChosen= 0;
    pc->pcLandscapeSet= 0;

    pc->pcSizeOptions= (APP_WIDGET *)0;
    pc->pcSizeOptionCount= 0;

    return;
    }