summaryrefslogtreecommitdiff
path: root/options.c
blob: c9295058306e2d4d7ba4c37c46aec60459535f67 (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
/*
 * Copyright (c) 1999 by The XFree86 Project, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * 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 XFREE86 PROJECT 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 XFree86 Project 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
 * XFree86 Project.
 *
 * Author: Paulo César Pereira de Andrade
 */

/* $XFree86: xc/programs/xedit/options.c,v 1.14 2002/11/10 23:21:56 paulo Exp $ */

#include <stdio.h>
#include <stdlib.h>
#include "xedit.h"

#include <X11/Xaw/SmeBSB.h>
#include <X11/Xaw/SimpleMenu.h>

/*
 * Prototypes
 */
static void SetColumns(Widget, XEvent*, String*, Cardinal*);
static void ChangeField(Widget, XEvent*, String*, Cardinal*);
static void EditCallback(Widget, XtPointer, XtPointer);
static void PopupColumnsCallback(Widget, XtPointer, XtPointer);
static void CreateColumnsShell(void);
static void ProcessColumnsCallback(Widget, XtPointer, XtPointer);

/*
 * Initialization
 */
extern Widget texts[3];

static Widget edit_popup, wrap_popup, justify_popup, scroll_popup,
	      columns_shell, left_text, right_text;

static XFontStruct *fonts[3];
static Pixel foregrounds[3], backgrounds[3];

static XtActionsRec actions[] = {
    {"set-columns", SetColumns},
    {"change-field", ChangeField},
};

#define WRAP_NEVER	1
#define WRAP_LINE	2
#define WRAP_WORD	3
#define	AUTO_FILL	4
#define JUST_LEFT	5
#define JUST_RIGHT	6
#define JUST_CENTER	7
#define JUST_FULL	8
#define SCROLL_VERT	9
#define SCROLL_HORIZ	10

static Widget autoFill, wrapNever, wrapLine, wrapWord,
	      justifyLeft, justifyRight, justifyCenter, justifyFull,
	      breakColumns, scrollVert, scrollHoriz;

void
CreateEditPopup(void)
{
    Arg args[1];

    edit_popup		= XtCreatePopupShell("editMenu", simpleMenuWidgetClass,
					     topwindow, NULL, 0);
    XtRealizeWidget(edit_popup);

    wrap_popup		= XtCreatePopupShell("wrapMenu", simpleMenuWidgetClass,
					     edit_popup, NULL, 0);
    XtRealizeWidget(wrap_popup);

    XtSetArg(args[0], XtNmenuName, "wrapMenu");
    XtCreateManagedWidget("wrapMenuItem", smeBSBObjectClass, edit_popup, args, 1);

    wrapNever		= XtCreateManagedWidget("never", smeBSBObjectClass,
						wrap_popup, NULL, 0);
    XtAddCallback(wrapNever, XtNcallback, EditCallback, (XtPointer)WRAP_NEVER);
    wrapLine		= XtCreateManagedWidget("line", smeBSBObjectClass,
						wrap_popup, NULL, 0);
    XtAddCallback(wrapLine, XtNcallback, EditCallback, (XtPointer)WRAP_LINE);
    wrapWord		= XtCreateManagedWidget("word", smeBSBObjectClass,
						wrap_popup, NULL, 0);
    XtAddCallback(wrapWord, XtNcallback, EditCallback, (XtPointer)WRAP_WORD);

    autoFill		= XtCreateManagedWidget("autoFill", smeBSBObjectClass,
						edit_popup, NULL, 0);
    XtAddCallback(autoFill, XtNcallback, EditCallback, (XtPointer)AUTO_FILL);

    justify_popup	= XtCreatePopupShell("justifyMenu", simpleMenuWidgetClass,
					     edit_popup, NULL, 0);
    XtRealizeWidget(justify_popup);

    XtSetArg(args[0], XtNmenuName, "justifyMenu");
    XtCreateManagedWidget("justifyMenuItem", smeBSBObjectClass, edit_popup, args, 1);

    justifyLeft		= XtCreateManagedWidget("left", smeBSBObjectClass,
						justify_popup, NULL, 0);
    XtAddCallback(justifyLeft, XtNcallback, EditCallback, (XtPointer)JUST_LEFT);
    justifyRight	= XtCreateManagedWidget("right", smeBSBObjectClass,
						justify_popup, NULL, 0);
    XtAddCallback(justifyRight, XtNcallback, EditCallback, (XtPointer)JUST_RIGHT);
    justifyCenter	= XtCreateManagedWidget("center", smeBSBObjectClass,
						justify_popup, NULL, 0);
    XtAddCallback(justifyCenter, XtNcallback, EditCallback, (XtPointer)JUST_CENTER);
    justifyFull		= XtCreateManagedWidget("full", smeBSBObjectClass,
						justify_popup, NULL, 0);
    XtAddCallback(justifyFull, XtNcallback, EditCallback, (XtPointer)JUST_FULL);

    breakColumns	= XtCreateManagedWidget("breakColumns", smeBSBObjectClass,
						edit_popup, NULL, 0);
    XtAddCallback(breakColumns, XtNcallback, PopupColumnsCallback, NULL);

    scroll_popup	= XtCreatePopupShell("scrollMenu", simpleMenuWidgetClass,
					     edit_popup, NULL, 0);
    XtRealizeWidget(scroll_popup);

    XtSetArg(args[0], XtNmenuName, "scrollMenu");
    XtCreateManagedWidget("scrollMenuItem", smeBSBObjectClass, edit_popup, args, 1);

    scrollVert		= XtCreateManagedWidget("vertical", smeBSBObjectClass,
						scroll_popup, NULL, 0);
    XtAddCallback(scrollVert, XtNcallback, EditCallback, (XtPointer)SCROLL_VERT);
    scrollHoriz		= XtCreateManagedWidget("horizontal", smeBSBObjectClass,
						scroll_popup, NULL, 0);
    XtAddCallback(scrollHoriz, XtNcallback, EditCallback, (XtPointer)SCROLL_HORIZ);

    CreateEditModePopup(edit_popup);
}

void
SetEditMenu(void)
{
    Arg args[7];
    Cardinal num_args;
    Boolean auto_fill;
    XawTextWrapMode wrap_mode;
    XawTextJustifyMode justify;
    XawTextScrollMode vscroll, hscroll;
    short left, right;

    num_args = 0;
    XtSetArg(args[num_args], XtNwrap, &wrap_mode);		++num_args;
    XtSetArg(args[num_args], XtNautoFill, &auto_fill);		++num_args;
    XtSetArg(args[num_args], XtNjustifyMode, &justify);		++num_args;
    XtSetArg(args[num_args], XtNleftColumn, &left);		++num_args;
    XtSetArg(args[num_args], XtNrightColumn, &right);		++num_args;
    XtSetArg(args[num_args], XtNscrollVertical, &vscroll);	++num_args;
    XtSetArg(args[num_args], XtNscrollHorizontal, &hscroll);	++num_args;
    XtGetValues(textwindow, args, num_args);

    if (flist.pixmap) {
	XtSetArg(args[0], XtNleftBitmap, None);
	XtSetArg(args[1], XtNleftBitmap, flist.pixmap);
	if (!auto_fill)
	    XtSetValues(autoFill, &args[0], 1);
	else
	    XtSetValues(autoFill, &args[1], 1);
	switch (wrap_mode) {
	    case XawtextWrapNever:
		XtSetValues(wrapLine, &args[0], 1);
		XtSetValues(wrapWord, &args[0], 1);
		XtSetValues(wrapNever, &args[1], 1);
		break;
	    case XawtextWrapLine:
		XtSetValues(wrapNever, &args[0], 1);
		XtSetValues(wrapWord, &args[0], 1);
		XtSetValues(wrapLine, &args[1], 1);
		break;
	    case XawtextWrapWord:
		XtSetValues(wrapNever, &args[0], 1);
		XtSetValues(wrapLine, &args[0], 1);
		XtSetValues(wrapWord, &args[1], 1);
		break;
	}
	switch (justify) {
	    case XawjustifyLeft:
		XtSetValues(justifyRight, &args[0], 1);
		XtSetValues(justifyCenter, &args[0], 1);
		XtSetValues(justifyFull, &args[0], 1);
		XtSetValues(justifyLeft, &args[1], 1);
		break;
	    case XawjustifyRight:
		XtSetValues(justifyLeft, &args[0], 1);
		XtSetValues(justifyCenter, &args[0], 1);
		XtSetValues(justifyFull, &args[0], 1);
		XtSetValues(justifyRight, &args[1], 1);
		break;
	    case XawjustifyCenter:
		XtSetValues(justifyLeft, &args[0], 1);
		XtSetValues(justifyRight, &args[0], 1);
		XtSetValues(justifyFull, &args[0], 1);
		XtSetValues(justifyCenter, &args[1], 1);
		break;
	    case XawjustifyFull:
		XtSetValues(justifyLeft, &args[0], 1);
		XtSetValues(justifyRight, &args[0], 1);
		XtSetValues(justifyCenter, &args[0], 1);
		XtSetValues(justifyFull, &args[1], 1);
		break;
	}
	if (!vscroll)
	    XtSetValues(scrollVert, &args[0], 1);
	else
	    XtSetValues(scrollVert, &args[1], 1);
	if (!hscroll)
	    XtSetValues(scrollHoriz, &args[0], 1);
	else
	    XtSetValues(scrollHoriz, &args[1], 1);
    }
    if (!auto_fill) {
	XtSetSensitive(wrapNever, True);
	XtSetSensitive(wrapLine, True);
	XtSetSensitive(wrapWord, True);

	XtSetSensitive(justifyLeft, False);
	XtSetSensitive(justifyRight, False);
	XtSetSensitive(justifyCenter, False);
	XtSetSensitive(justifyFull, False);
	XtSetSensitive(breakColumns, False);
    }
    else {
	XtSetSensitive(wrapNever, False);
	XtSetSensitive(wrapLine, False);
	XtSetSensitive(wrapWord, False);

	XtSetSensitive(justifyLeft, left < right);
	XtSetSensitive(justifyRight, left < right);
	XtSetSensitive(justifyCenter, left < right);
	XtSetSensitive(justifyFull, left < right);
	XtSetSensitive(breakColumns, True);
    }

    SetEditModeMenu();
}

/*ARGSUSED*/
static void
EditCallback(Widget sme, XtPointer client_data, XtPointer call_data)
{
    Arg args[1];
    Boolean auto_fill;
    XawTextScrollMode scroll;
    xedit_flist_item *item = FindTextSource(XawTextGetSource(textwindow), NULL);
    Bool foreach = False;

    switch ((long)client_data) {
	case WRAP_NEVER:
	    XtSetArg(args[0], XtNwrap, XawtextWrapNever);
	    if (item) {
		item->flags |= WRAP_BIT;
		item->wrap = XawtextWrapNever;
		foreach = True;
	    }
	    break;
	case WRAP_LINE:
	    XtSetArg(args[0], XtNwrap, XawtextWrapLine);
	    if (item) {
		item->flags |= WRAP_BIT;
		item->wrap = XawtextWrapLine;
		foreach = True;
	    }
	    break;
	case WRAP_WORD:
	    XtSetArg(args[0], XtNwrap, XawtextWrapWord);
	    if (item) {
		item->flags |= WRAP_BIT;
		item->wrap = XawtextWrapWord;
		foreach = True;
	    }
	    break;
	case AUTO_FILL:
	    XtSetArg(args[0], XtNautoFill, &auto_fill);
	    XtGetValues(textwindow, args, 1);
	    XtSetArg(args[0], XtNautoFill, !auto_fill);
	    break;
	case JUST_LEFT:
	    XtSetArg(args[0], XtNjustifyMode, XawjustifyLeft);
	    break;
	case JUST_RIGHT:
	    XtSetArg(args[0], XtNjustifyMode, XawjustifyRight);
	    break;
	case JUST_CENTER:
	    XtSetArg(args[0], XtNjustifyMode, XawjustifyCenter);
	    break;
	case JUST_FULL:
	    XtSetArg(args[0], XtNjustifyMode, XawjustifyFull);
	    break;
	case SCROLL_VERT:
	    XtSetArg(args[0], XtNscrollVertical, &scroll);
	    XtGetValues(textwindow, args, 1);
	    XtSetArg(args[0], XtNscrollVertical, scroll == XawtextScrollNever ?
		     XawtextScrollAlways : XawtextScrollNever);
	    break;
	case SCROLL_HORIZ:
	    XtSetArg(args[0], XtNscrollHorizontal, &scroll);
	    XtGetValues(textwindow, args, 1);
	    XtSetArg(args[0], XtNscrollHorizontal, scroll == XawtextScrollNever ?
		     XawtextScrollAlways : XawtextScrollNever);
	    break;
    }

    if (foreach) {
	int i;
 
	for (i = 0; i < 3; i++)
	    XtSetValues(texts[i], args, 1);
    }
    else
	XtSetValues(textwindow, args, 1);
}

static void
CreateColumnsShell(void)
{
    Atom delete_window;
    Widget form, ok, cancel;

    if (columns_shell)
	return;

    XtAppAddActions(XtWidgetToApplicationContext(topwindow),
		    actions, XtNumber(actions));

    columns_shell	= XtCreatePopupShell("columns", transientShellWidgetClass,
					     topwindow, NULL, 0);
    form		= XtCreateManagedWidget("form", formWidgetClass,
						columns_shell, NULL, 0);
    XtCreateManagedWidget("leftLabel", labelWidgetClass, form, NULL, 0);
    left_text		= XtVaCreateManagedWidget("left", asciiTextWidgetClass,
						  form, XtNeditType, XawtextEdit,
						  NULL, 0);
    XtCreateManagedWidget("rightLabel", labelWidgetClass, form, NULL, 0);
    right_text		= XtVaCreateManagedWidget("right", asciiTextWidgetClass,
						  form, XtNeditType, XawtextEdit,
						  NULL, 0);
    ok			= XtCreateManagedWidget("ok", commandWidgetClass,
						form, NULL, 0);
    XtAddCallback(ok, XtNcallback, ProcessColumnsCallback, (XtPointer)True);
    cancel		= XtCreateManagedWidget("cancel", commandWidgetClass,
						form, NULL, 0);
    XtAddCallback(cancel, XtNcallback, ProcessColumnsCallback, (XtPointer)False);

    XtRealizeWidget(columns_shell);
    delete_window = XInternAtom(XtDisplay(columns_shell), "WM_DELETE_WINDOW", False);
    XSetWMProtocols(XtDisplay(columns_shell), XtWindow(columns_shell), &delete_window, 1);

    XtSetKeyboardFocus(columns_shell, left_text);
}

/*ARGSUSED*/
static void
PopupColumnsCallback(Widget w, XtPointer client_data, XtPointer call_data)
{
    Arg args[3];
    char sleft[6], sright[6];
    short left, right;
    Dimension width, height, b_width;
    Window r, c;
    int x, y, wx, wy, max_x, max_y;
    unsigned mask;

    CreateColumnsShell();

    XQueryPointer(XtDisplay(columns_shell), XtWindow(columns_shell),
		  &r, &c, &x, &y, &wx, &wy, &mask);

    XtSetArg(args[0], XtNwidth, &width);
    XtSetArg(args[1], XtNheight, &height);
    XtSetArg(args[2], XtNborderWidth, &b_width);
    XtGetValues(columns_shell, args, 3);

    width += b_width << 1;
    height += b_width << 1;

    x -= (Position)(width >> 1);
    if (x < 0)
	x = 0;
    if (x > (max_x = (Position)(XtScreen(columns_shell)->width - width)))
	x = max_x;

    y -= (Position)(height >> 1);
    if (y < 0)
	y = 0;
    if (y > (max_y = (Position)(XtScreen(columns_shell)->height - height)))
	y = max_y;

    XtSetArg(args[0], XtNx, x);
    XtSetArg(args[1], XtNy, y);
    XtSetValues(columns_shell, args, 2);

    XtSetArg(args[0], XtNleftColumn, &left);
    XtSetArg(args[1], XtNrightColumn, &right);
    XtGetValues(textwindow, args, 2);
    XmuSnprintf(sleft, sizeof(sleft), "%d", left);
    XmuSnprintf(sright, sizeof(sright), "%d", right);
    XtSetArg(args[0], XtNstring, sleft);
    XtSetValues(left_text, args, 1);
    XtSetArg(args[0], XtNstring, sright);
    XtSetValues(right_text, args, 1);
    XtPopup(columns_shell, XtGrabExclusive);
}

/*ARGSUSED*/
static void
ProcessColumnsCallback(Widget w, XtPointer client_data, XtPointer call_data)
{
    if (client_data) {
	Arg args[2];
	char *left, *right;
	short leftc, rightc;

	left = GetString(left_text);
	right = GetString(right_text);

	leftc = atoi(left);
	rightc = atoi(right);
	XtSetArg(args[0], XtNleftColumn, leftc);
	XtSetArg(args[1], XtNrightColumn, rightc);

	XtSetValues(textwindow, args, 2);
    }

    XtPopdown(columns_shell);
}

/*ARGSUSED*/
static void
SetColumns(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
    Bool ok = False;

    if (*num_params &&
	(params[0][0] == 'o' || params[0][0] == 'O'))
	ok = True;

    ProcessColumnsCallback(w, (XtPointer)(long)ok, NULL);
}

/*ARGSUSED*/
static void
ChangeField(Widget w, XEvent *event, String *params, Cardinal *num_params)
{
    Widget focus = XtGetKeyboardFocusWidget(columns_shell);

    XtSetKeyboardFocus(columns_shell,
		       focus == left_text ? right_text : left_text);
}

void
UpdateTextProperties(int force)
{
    Arg args[4];
    Cardinal num_args;
    xedit_flist_item *item;
    XawTextPropertyList *prop;
    Widget text, source, sink;
    int i;

    /* save default information */
    if (fonts[0] == NULL) {
	for (i = 0; i < 3; i++) {
	    num_args = 0;
	    XtSetArg(args[num_args], XtNfont, &fonts[i]);	      ++num_args;
	    XtSetArg(args[num_args], XtNforeground, &foregrounds[i]); ++num_args;
	    XtSetArg(args[num_args], XtNbackground, &backgrounds[i]); ++num_args;
	    XtGetValues(XawTextGetSink(texts[i]), args, num_args);
	}
    }

    for (i = 0; i < 3; i++) {
	text = texts[i];
	source = XawTextGetSource(text);
	sink = XawTextGetSink(text);
	item = FindTextSource(source, NULL);

	XtSetArg(args[0], XawNtextProperties, &prop);
	XtGetValues(sink, args, 1);

	if (item == NULL || (!force && prop == item->properties))
	    continue;

	XtSetArg(args[0], XawNtextProperties, item->properties);
	num_args = 1;
	if (item->properties == NULL) {
	    XtSetArg(args[num_args], XtNfont, fonts[i]);	     ++num_args;
	    XtSetArg(args[num_args], XtNforeground, foregrounds[i]); ++num_args;
	    XtSetArg(args[num_args], XtNbackground, backgrounds[i]); ++num_args;
	}
	XtSetValues(sink, args, num_args);

	if (text == textwindow) {
	    XtSetArg(args[0], XtNdisplayCaret, False);
	    XtSetValues(text, args, 1);
	}
	_XawTextBuildLineTable((TextWidget)text,
			       XawTextTopPosition(text), True);
	XawTextDisplay(text);
	if (text == textwindow) {
	    XtSetArg(args[0], XtNdisplayCaret, True);
	    XtSetValues(text, args, 1);
	}
    }
}