File: timer.c

package info (click to toggle)
xpostitplus 2.3.1-10
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 464 kB
  • ctags: 661
  • sloc: ansic: 4,831; makefile: 527; sh: 70
file content (524 lines) | stat: -rw-r--r-- 11,633 bytes parent folder | download | duplicates (3)
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
#ifndef lint
static char	*RCSid = "$Id: list.c,v 2.0 1995/03/27 18:56:22 mjhammel Exp $";
#endif

/*
 * timer.c - routines to handle the alarm feature.
 *
 * Michael J. Hammel
 * Contractor
 * 1150 Inca St. TH 70
 * Denver, CO 80204
 * mjhammel@csn.net
 *
 * $Log: list.c,v $
 * Revision 2.0  1995/03/27  18:56:22  mjhammel
 * Initial update to 2.0
 *
 * 
 */
#include <X11/StringDefs.h>
#include <X11/Intrinsic.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Label.h>
#include <X11/Xaw/Command.h>
#include <X11/Xaw/Text.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Shell.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "xpostit.h"

/* globals */

/* local variables */
static Widget 		timer_widget;
static Widget		timer_texts[5];
static Widget		timer_boxes[5];
static Widget		timer_buttons[3];

/* external variables */
extern Widget			toplevel;
extern Pixmap			alarmon_pixmap;
extern Pixmap			alarmoff_pixmap;

/* external routines */
extern PostItNote	*FindNote();
void TimerPopDown();
void TimerAccept();
void TimerCancel();
void TimerLeftArrowCB();
void TimerRightArrowCB();

/*
 * CreateTimerPrompt - create a prompt for alarm information
 */
void
CreateTimerPrompt(pn)
PostItNote 	*pn;
{
	Arg 		args[30];
	register int 	nargs;
	XtCallbackRec	callbacks[2];

	Widget		timer_form;
	Widget		timer_forms[5];
	Widget		timer_labels[5];
	Widget		timer_leftarrows[5], timer_rightarrows[5];
	Dimension	width, height;
	char		buf[10];
	Window 		root, child;
	unsigned int 	buttons;
	int 		root_x, root_y, child_x, child_y;

	int		i, maxwidth;

	/* get mouse location */
	XQueryPointer(display, XtWindow(toplevel), &root, &child,
		      &root_x, &root_y, &child_x, &child_y, &buttons);

	/*
	 * Create a popup shell widget
	 */
	nargs = 0;
	SetArg(XtNtitle, PostItTimerPrompt);
	SetArg(XtNx, root_x);
	SetArg(XtNy, root_y);
	timer_widget = XtCreatePopupShell(
			"TimerShell", 
			transientShellWidgetClass,
			toplevel, 
			args, nargs);

	/* the form inside which all other windows will be put */
	nargs = 0;
	SetArg(XtNborderWidth, 0);
	timer_form = XtCreateManagedWidget(
			"TimerForm", 
			formWidgetClass,
			timer_widget, 
			args, nargs);

	/* the boxes, one for each line of input */
	for ( i=0; i<5; i++)
	{
		nargs = 0;
		SetArg( XtNborderWidth, 0);
		SetArg( XtNtop, XtChainTop ); 
		SetArg( XtNbottom, XtChainTop ); 
		SetArg( XtNleft, XtChainLeft ); 
		SetArg( XtNright, XtChainLeft ); 
		SetArg( XtNvertDistance, 0 ); 
		SetArg( XtNhorizDistance, 0 ); 
		SetArg( XtNdefaultDistance, 0 ); 
		SetArg( XtNorientation, XtorientHorizontal ); 
		SetArg( XtNvSpace, 2 ); 
		SetArg( XtNhSpace, 2 ); 
		if ( i!= 0 )
		{
			SetArg( XtNfromVert, timer_boxes[i-1] ); 
		}
		timer_boxes[i] = XtCreateManagedWidget(
				"TimerBox", 
				boxWidgetClass,
				timer_form, 
				args, nargs);
	}


	/*
	 * the forms, one for each box, which provides constraints
	 * for other windows inside that box.
	 */
	nargs = 0;
	SetArg(XtNborderWidth, 0);
	for ( i=0; i<4; i++)
	{
		timer_forms[i] = XtCreateManagedWidget(
			"TimerForm", 
			formWidgetClass,
			timer_boxes[i], 
			args, nargs);
	}

	/* The labels of each box */
	for ( i=0; i<4; i++)
	{
		nargs = 0;
		SetArg( XtNborderWidth, 0);
		SetArg( XtNtop, XtChainTop ); 
		SetArg( XtNbottom, XtChainTop ); 
		SetArg( XtNleft, XtChainLeft ); 
		SetArg( XtNright, XtChainLeft ); 
		switch(i)
		{
			case 0: SetArg(XtNlabel, "Month"); break;
			case 1: SetArg(XtNlabel, "Day"); break;
			case 2: SetArg(XtNlabel, "Hour"); break;
			case 3: SetArg(XtNlabel, "Minute"); break;
		}
		timer_labels[i] = XtCreateManagedWidget(
			"TimerLabel", 
			labelWidgetClass,
			timer_forms[i], 
			args, nargs);
	}

	/*
	 * now make all the labels the same width
	 */
	nargs = 0;
	SetArg( XtNwidth, &width);
	XtGetValues ( timer_labels[3], args, nargs );
	nargs = 0;
	SetArg( XtNwidth, width);
	SetArg( XtNjustify, XtJustifyCenter);
	for ( i=0; i<4; i++)
	{
		XtSetValues ( timer_labels[i], args, nargs );
	}
	

	/*
	 * The arrow buttons and text fields in each box
	 */
	maxwidth=0;
	for ( i=0; i<4; i++)
	{
		bzero(callbacks, sizeof(callbacks));
		SetCallback(TimerLeftArrowCB, (XtPointer)i);
		nargs = 0;
		SetArg( XtNcallback, callbacks);
		SetArg( XtNlabel, "<");
		SetArg( XtNborderWidth, 1);
		SetArg( XtNtop, XtChainTop ); 
		SetArg( XtNbottom, XtChainTop ); 
		SetArg( XtNleft, XtChainLeft ); 
		SetArg( XtNright, XtChainLeft ); 
		SetArg( XtNfromHoriz, timer_labels[i] ); 
		SetArg( XtNvertDistance, 0 ); 
		SetArg( XtNhorizDistance, 0 ); 
		SetArg( XtNdefaultDistance, 0 ); 
		SetArg( XtNshapeStyle, XmuShapeRectangle ); 
		timer_leftarrows[i] = XtCreateManagedWidget(
			"TimerArrow", 
			commandWidgetClass,
			timer_forms[i], 
			args, nargs);

		nargs = 0 ;
		SetArg ( XtNheight, &height ); 
		XtGetValues ( timer_leftarrows[i], args, nargs );
 
		/*
		 * Set the text fields to the notes stored values
		 */
		switch (i)
		{
			case 0: sprintf(buf, "%d", pn->pn_alarm_mon+1); break;
			case 1: sprintf(buf, "%d", pn->pn_alarm_day); break;
			case 2: sprintf(buf, "%d", pn->pn_alarm_hour); break;
			case 3: sprintf(buf, "%d", pn->pn_alarm_min); break;
			default : sprintf(buf, "   "); break;
		}
	
		nargs = 0;
		SetArg( XtNlabel, buf);
		SetArg( XtNborderWidth, 1 ); 
		SetArg( XtNtop, XtChainTop ); 
		SetArg( XtNbottom, XtChainTop ); 
		SetArg( XtNleft, XtChainLeft ); 
		SetArg( XtNright, XtChainLeft ); 
		SetArg( XtNfromHoriz, timer_leftarrows[i] ); 
		SetArg( XtNvertDistance, 0 ); 
		SetArg( XtNhorizDistance, 0 ); 
		SetArg( XtNdefaultDistance, 0 ); 
		SetArg( XtNleftMargin, 6 ); 
		SetArg( XtNrightMargin, 6 ); 
		timer_texts[i] = XtCreateManagedWidget(
			"TimerText",
			labelWidgetClass,
			timer_forms[i],
			args, nargs );
		nargs = 0;
		SetArg( XtNwidth, &width);
		XtGetValues(timer_texts[i], args, nargs);
		if ( width > maxwidth )
			maxwidth=width;

		bzero(callbacks, sizeof(callbacks));
		SetCallback(TimerRightArrowCB, (XtPointer)i);
		nargs = 0;
		SetArg( XtNcallback, callbacks);
		SetArg( XtNheight, height ); 
		SetArg( XtNborderWidth, 1);
		SetArg( XtNlabel, ">");
		SetArg( XtNtop, XtChainTop ); 
		SetArg( XtNbottom, XtChainTop ); 
		SetArg( XtNleft, XtChainLeft ); 
		SetArg( XtNright, XtChainLeft ); 
		SetArg( XtNfromHoriz, timer_texts[i] ); 
		SetArg( XtNvertDistance, 0 ); 
		SetArg( XtNhorizDistance, 0 ); 
		SetArg( XtNdefaultDistance, 0 ); 
		SetArg( XtNshapeStyle, XmuShapeRectangle ); 
		timer_rightarrows[i] = XtCreateManagedWidget(
			"TimerArrow", 
			commandWidgetClass,
			timer_forms[i], 
			args, nargs);
	}

	/* readjust labels and buttons to look consistant */
	nargs = 0;
	SetArg( XtNheight, &height);
	XtGetValues(timer_texts[0], args, nargs);

	for ( i=0; i<4; i++)
	{
		nargs = 0;
		SetArg( XtNwidth, width);
		XtSetValues(timer_texts[i], args, nargs);
	}
	for ( i=0; i<4; i++)
	{
		nargs = 0;
		SetArg( XtNheight, height);
		XtSetValues(timer_texts[i], args, nargs);
		XtSetValues(timer_leftarrows[i], args, nargs);
		XtSetValues(timer_rightarrows[i], args, nargs);
		XtSetValues(timer_labels[i], args, nargs);
	}

	/*
	 * create the Cancel/Accept command buttons 
	 */
	bzero(callbacks, sizeof(callbacks));
	SetCallback(TimerAccept, (XtPointer)pn);
	nargs = 0;
	SetArg(XtNcallback, callbacks);
	SetArg(XtNlabel, "Accept");
	timer_buttons[1] = XtCreateManagedWidget(
		"TimerButtons", 
		commandWidgetClass,
		timer_boxes[4], 
		args, nargs);

	bzero(callbacks, sizeof(callbacks));
	SetCallback(TimerCancel, (XtPointer)pn);
	nargs = 0;
	SetArg(XtNcallback, callbacks);
	SetArg(XtNlabel, "Cancel");
	timer_buttons[0] = XtCreateManagedWidget(
		"TimerButtons", 
		commandWidgetClass,
		timer_boxes[4], 
		args, nargs);

	XtPopup( timer_widget, XtGrabNonexclusive );
}

/*
 * TimerAccept - Accept a new alarm
 */
void
TimerAccept(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote 	*pn = (PostItNote *)client_data;
	int			i, value, nargs;
	Arg			args[3];
	String		string;

	for (i=0; i<4; i++)
	{
		nargs = 0;
		SetArg(XtNlabel, &string);
		XtGetValues ( timer_texts[i], args, nargs );
		value = (int)atoi(string);
		switch (i)
		{
			/* Month has to be adjusted to what localtime() uses. */
			case 0: pn->pn_alarm_mon = value-1; break;
			case 1: pn->pn_alarm_day = value; break;
			case 2: pn->pn_alarm_hour = value; break;
			case 3: pn->pn_alarm_min = value; break;
		}
	}

	pn->pn_alarm = True;

	XtSetSensitive ( pn->pn_unsettimewidget, True );
	XtSetSensitive ( pn->pn_settimewidget, False );
	XtSetSensitive ( pn->pn_savewidget, True );

	/* set the alarm icon on for this note */
	nargs = 0;
	SetArg(XtNlabel, " ");
	SetArg(XtNbitmap, alarmon_pixmap);
	XtSetValues(pn->pn_alarmwidget, args, nargs);

	XtSetSensitive ( pn->pn_savewidget, True );
	TimerPopDown();
}

/*
 * TimerCancel - Cancel an alarm
 */
void
TimerCancel(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	TimerPopDown();
}

/*
 * TimerPopDown - removes the Error Dialog box
 */
void
TimerPopDown()
{
	XtPopdown( timer_widget );
	XtDestroyWidget(timer_widget);

}


/*
 * TimerRightArrowcB - handle a right arrow click
 */
void
TimerRightArrowCB(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	int	index = (int) client_data;
	int	max=0, min=0;
	String	string;
	char	buf[10];
	int	nargs, value;
	Arg	args[3];

	/* set limits based on which field is being updated */
	switch ( index )
	{
		case 0: max=12; min=1; break; /* month */
		case 2: max=23; min=0; break; /* hour */
		case 3: max=59; min=0; break; /* minute */

		/*
		 * setting the day max is dependent on which month
		 * is currently set
		 */
		case 1: 
		min = 1;
		nargs = 0;
		/* SetArg(XtNstring, &string);
		 */
		SetArg(XtNlabel, &string);
		XtGetValues ( timer_texts[0], args, nargs );
		value = (int)atoi(string);
		switch ( value )
		{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12: max=31; break;
			case 2: max=29; break;
			default: max=30; break;
		}
	}

	/* increment text field */
	nargs = 0;
	/* SetArg(XtNstring, &string);
	 */
	SetArg(XtNlabel, &string);
	XtGetValues ( timer_texts[index], args, nargs );
	value = (int)atoi(string) + 1;
	if ( value > max )
		value = min;
	sprintf ( buf, "%d", value);
	nargs = 0;
	/* SetArg(XtNstring, buf);
	 */
	SetArg(XtNlabel, buf);
	XtSetValues ( timer_texts[index], args, nargs );
		

}

/*
 * TimerLeftArrowcB - handle a left arrow click
 */
void
TimerLeftArrowCB(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	int	index = (int) client_data;
	int	max=0, min=0;
	String	string;
	char	buf[10];
	int	nargs, value;
	Arg	args[3];

	/* set limits based on which field is being updated */
	switch ( index )
	{
		case 0: max=12; min=1; break; /* month */
		case 2: max=23; min=0; break; /* hour */
		case 3: max=59; min=0; break; /* minute */

		/*
		 * setting the day max is dependent on which month
		 * is currently set
		 */
		case 1:
		min=1;  
		nargs = 0;
		/* SetArg(XtNstring, &string);
		 */
		SetArg(XtNlabel, &string);
		XtGetValues ( timer_texts[0], args, nargs );
		value = (int)atoi(string);
		switch ( value )
		{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12: max=31; break;
			case 2: max=29; break;
			default: max=30; break;
		}
	}

	/* decrement text field */
	nargs = 0;
	/* SetArg(XtNstring, &string);
	 */
	SetArg(XtNlabel, &string);
	XtGetValues ( timer_texts[index], args, nargs );
	value = (int)atoi(string) - 1;
	if ( value < min )
		value = max;
	sprintf ( buf, "%d", value);
	nargs = 0;
	/* SetArg(XtNstring, buf);
	 */
	SetArg(XtNlabel, buf);
	XtSetValues ( timer_texts[index], args, nargs );
		

}