File: insert.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 (425 lines) | stat: -rw-r--r-- 9,794 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
#ifndef lint
static char	*RCSid = "$Id$";
#endif

/*
 * insert.c - routines to handle special inserts into a note
 *
 * Michael J. Hammel
 * Contractor
 * 1150 Inca St. TH 70
 * Denver, CO 80204
 * mjhammel@csn.net
 *
 * $Log$
 * 
 */
#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 <time.h>

#include "xpostit.h"

/* globals */

/* local variables */
static Widget 		insert_widget;
static Widget		insert_boxes[12];
static Widget		insert_buttons[3];
static Widget		insert_leftarrows[11];
static int			active_item;

/* external variables */
extern Widget			toplevel;

/* external routines */
extern PostItNote	*FindNote();
extern InsertText();
void InsertPopDown();
void InsertAccept();
void InsertCancel();
void InsertLeftArrowCB();

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

	Widget		insert_form;
	Widget		insert_forms[12];
	Widget		insert_labels[11];
	Dimension	width;
	Window 		root, child;
	int 			root_x, root_y, child_x, child_y;
	int			i;
	unsigned int 	buttons;

	/* initialize marker */
	active_item=0;

	/* 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, PostItInsertPrompt);
	SetArg(XtNx, root_x);
	SetArg(XtNy, root_y);
	insert_widget = XtCreatePopupShell(
			"InsertShell", 
			transientShellWidgetClass,
			toplevel, 
			args, nargs);

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

	/* the boxes, one for each line of input */
	for ( i=0; i<12; 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, 0 ); 
		SetArg( XtNhSpace, 0 ); 
		if ( i!= 0 )
		{
			SetArg( XtNfromVert, insert_boxes[i-1] ); 
		}
		insert_boxes[i] = XtCreateManagedWidget(
				"InsertBox", 
				boxWidgetClass,
				insert_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<11; i++)
	{
		insert_forms[i] = XtCreateManagedWidget(
			"InsertForm", 
			formWidgetClass,
			insert_boxes[i], 
			args, nargs);
	}

	/*
	 * The selection button in each box
	 */
	for ( i=0; i<11; i++)
	{
		bzero(callbacks, sizeof(callbacks));
		SetCallback(InsertLeftArrowCB, (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( XtNvertDistance, 0 ); 
		SetArg( XtNhorizDistance, 0 ); 
		SetArg( XtNdefaultDistance, 0 ); 
		SetArg( XtNshapeStyle, XmuShapeRectangle ); 
		insert_leftarrows[i] = XtCreateManagedWidget(
			"InsertArrow", 
			commandWidgetClass,
			insert_forms[i], 
			args, nargs);
	}

	/* The labels of each box */
	for ( i=0; i<11; i++)
	{
		nargs = 0;
		SetArg( XtNborderWidth, 0);
		SetArg( XtNtop, XtChainTop ); 
		SetArg( XtNbottom, XtChainTop ); 
		SetArg( XtNleft, XtChainLeft ); 
		SetArg( XtNright, XtChainLeft ); 
		SetArg( XtNfromHoriz, insert_leftarrows[i] ); 
		SetArg( XtNvertDistance, 0 ); 
		SetArg( XtNhorizDistance, 0 ); 
		SetArg( XtNdefaultDistance, 0 ); 
		SetArg( XtNshapeStyle, XmuShapeRectangle ); 
		switch(i)
		{
			case 0: SetArg(XtNlabel, "mm/dd/yy"); break;
			case 1: SetArg(XtNlabel, "<mm/dd/yy>"); break;
			case 2: SetArg(XtNlabel, "(mm/dd/yy)"); break;
			case 3: SetArg(XtNlabel, "mm/dd/yy - hh:mm"); break;
			case 4: SetArg(XtNlabel, "mm/dd/yy(hh:mm)"); break;
			case 5: SetArg(XtNlabel, "Jan 1, 1996"); break;
			case 6: SetArg(XtNlabel, "Jan 1, 1996 - hh:mm"); break;
			case 7: SetArg(XtNlabel, "Jan 1, 1996 (hh:mm)"); break;
			case 8: SetArg(XtNlabel, "Tuesday Jan 1, 1996"); break;
			case 9: SetArg(XtNlabel, "Tuesday Jan 1, 1996 - hh:mm"); break;
			case 10: SetArg(XtNlabel, "Tuesday Jan 1, 1996 (hh:mm)"); break;
		}
		insert_labels[i] = XtCreateManagedWidget(
			"InsertLabel", 
			labelWidgetClass,
			insert_forms[i], 
			args, nargs);
	}

	/*
	 * now make all the labels the same width
	 */
	nargs = 0;
	SetArg( XtNwidth, &width);

	/* ugly - hard code the longest box! */
	XtGetValues ( insert_labels[9], args, nargs );

	nargs = 0;
	SetArg( XtNwidth, width);
	SetArg( XtNjustify, XtJustifyLeft);
	for ( i=0; i<11; i++)
	{
		XtSetValues ( insert_labels[i], args, nargs );
	}
	

	/*
	 * create the Cancel/Accept command buttons 
	 */
	bzero(callbacks, sizeof(callbacks));
	SetCallback(InsertAccept, (XtPointer)pn);
	nargs = 0;
	SetArg(XtNcallback, callbacks);
	SetArg(XtNlabel, "Accept");
	insert_buttons[1] = XtCreateManagedWidget(
		"InsertButtons", 
		commandWidgetClass,
		insert_boxes[11], 
		args, nargs);

	bzero(callbacks, sizeof(callbacks));
	SetCallback(InsertCancel, (XtPointer)pn);
	nargs = 0;
	SetArg(XtNcallback, callbacks);
	SetArg(XtNlabel, "Cancel");
	insert_buttons[0] = XtCreateManagedWidget(
		"InsertButtons", 
		commandWidgetClass,
		insert_boxes[11], 
		args, nargs);

	XtPopup( insert_widget, XtGrabNonexclusive );
}

/*
 * InsertAccept - Accept selected date value
 *	This routine has the potential to be very OS specific.
 */
void
InsertAccept(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	PostItNote 	*pn = (PostItNote *)client_data;
	char			cmd[1024];
	time_t		time_tp;
	struct tm	*tm_tp;
	char			dayname[12];
	char			year[5];
	char			month[10];

	/* get todays date and current time */
	time(&time_tp);
	tm_tp = localtime(&time_tp);

	/*
	 * convert numeric values to strings now, even if we don't use them,
	 * since we need these in more than one place.
	 */
	switch ( tm_tp->tm_wday )
	{
		case 0:	sprintf(dayname, "Sunday"); break;
		case 1:	sprintf(dayname, "Monday"); break;
		case 2:	sprintf(dayname, "Tuesday"); break;
		case 3:	sprintf(dayname, "Wednesday"); break;
		case 4:	sprintf(dayname, "Thursday"); break;
		case 5:	sprintf(dayname, "Friday"); break;
		case 6:	sprintf(dayname, "Saturday"); break;
	}
	switch ( tm_tp->tm_mon )
	{
		case 0: sprintf(month, "Jan"); break;
		case 1: sprintf(month, "Feb"); break;
		case 2: sprintf(month, "Mar"); break;
		case 3: sprintf(month, "Apr"); break;
		case 4: sprintf(month, "May"); break;
		case 5: sprintf(month, "Jun"); break;
		case 6: sprintf(month, "Jul"); break;
		case 7: sprintf(month, "Aug"); break;
		case 8: sprintf(month, "Sept"); break;
		case 9: sprintf(month, "Oct"); break;
		case 10: sprintf(month, "Nov"); break;
		case 11: sprintf(month, "Dec"); break;
	}

	/*
	 * increment the numeric month, since localtime()
	 * returns the month from 0 to 11.
	 */
	tm_tp->tm_mon++;

	switch (active_item)
	{
		case 0:
			sprintf (cmd, 
				"%02d/%02d/%02d", tm_tp->tm_mon, tm_tp->tm_mday, tm_tp->tm_year%100 );
			break;

		case 1:
			sprintf (cmd, 
				"<%02d/%02d/%02d>", tm_tp->tm_mon, tm_tp->tm_mday, tm_tp->tm_year%100 );
			break;

		case 2:
			sprintf (cmd, 
				"(%02d/%02d/%02d)", tm_tp->tm_mon, tm_tp->tm_mday, tm_tp->tm_year%100 );
			break;

		case 3:
			sprintf (cmd, 
				"%02d/%02d/%02d - %02d:%02d", 
				tm_tp->tm_mon, tm_tp->tm_mday, tm_tp->tm_year%100, 
				tm_tp->tm_hour, tm_tp->tm_min );
			break;

		case 4:
			sprintf (cmd, 
				"%02d/%02d/%02d(%02d:%02d)", 
				tm_tp->tm_mon, tm_tp->tm_mday, tm_tp->tm_year%100, 
				tm_tp->tm_hour, tm_tp->tm_min );
			break;

		case 5:
			sprintf (cmd, 
				"%s %d, %d", month, tm_tp->tm_mday, year, tm_tp->tm_year+1900);
			break;

		case 6:
			sprintf (cmd, 
				"%s %d, %d - %02d:%02d", 
				month, tm_tp->tm_mday, year, tm_tp->tm_year+1900,
				tm_tp->tm_hour, tm_tp->tm_min );
			break;

		case 7:
			sprintf (cmd, 
				"%s %d, %d (%02d:%02d)", 
				month, tm_tp->tm_mday, year, tm_tp->tm_year+1900,
				tm_tp->tm_hour, tm_tp->tm_min );
			break;

		case 8:
			sprintf (cmd, 
				"%s %s %d, %d", 
				dayname, month, tm_tp->tm_mday, year, tm_tp->tm_year+1900);
			break;

		case 9:
			sprintf (cmd, 
				"%s %s %d, %d - %02d:%02d", 
				dayname, month, tm_tp->tm_mday, year, tm_tp->tm_year+1900,
				tm_tp->tm_hour, tm_tp->tm_min );
			break;

		case 10:
			sprintf (cmd, 
				"%s %s %d, %d (%02d:%02d)", 
				dayname, month, tm_tp->tm_mday, year, tm_tp->tm_year+1900,
				tm_tp->tm_hour, tm_tp->tm_min );
			break;

	}

	/* stuff the output into the note */
	InsertText(pn, cmd, False, TEXT_TYPE);

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

/*
 * InsertCancel - Cancel an insert
 */
void
InsertCancel(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	InsertPopDown();
}

/*
 * InsertPopDown - removes the Insert Dialog box
 */
void
InsertPopDown()
{
	XtPopdown( insert_widget );
	XtDestroyWidget(insert_widget);

}


/*
 * InsertLeftArrowcB - handle a left arrow click
 */
void
InsertLeftArrowCB(w, client_data, call_data)
caddr_t client_data, call_data;
Widget w;
{
	int	nargs;
	Arg	args[3];

	nargs=0;
	SetArg( XtNlabel, ">");
	XtSetValues ( insert_leftarrows[(int)client_data], args, nargs );

	active_item=(int)client_data;
}