Add support for large files, plus some locale improvements.
[bpt/emacs.git] / lwlib / lwlib-Xaw.c
CommitLineData
b70cfce2
FP
1/* The lwlib interface to Athena widgets.
2 Copyright (C) 1993 Chuck Thompson <cthomp@cs.uiuc.edu>
3
4This file is part of the Lucid Widget Library.
5
6The Lucid Widget Library is free software; you can redistribute it and/or
7modify it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 1, or (at your option)
9any later version.
10
11The Lucid Widget Library is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
3b7ad313
EN
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA. */
b70cfce2 20
0f0912e6
PE
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
b70cfce2
FP
25#include <stdio.h>
26
27#include "lwlib-Xaw.h"
28
29#include <X11/StringDefs.h>
30#include <X11/IntrinsicP.h>
31#include <X11/CoreP.h>
32#include <X11/Shell.h>
33
34#include <X11/Xaw/Scrollbar.h>
35#include <X11/Xaw/Paned.h>
36#include <X11/Xaw/Dialog.h>
37#include <X11/Xaw/Form.h>
38#include <X11/Xaw/Command.h>
39#include <X11/Xaw/Label.h>
40
41#include <X11/Xatom.h>
42
79140ef0 43static void xaw_generic_callback (/*Widget, XtPointer, XtPointer*/);
b70cfce2
FP
44
45
46Boolean
e14e13a4
FP
47lw_xaw_widget_p (widget)
48 Widget widget;
b70cfce2
FP
49{
50 return (XtIsSubclass (widget, scrollbarWidgetClass) ||
51 XtIsSubclass (widget, dialogWidgetClass));
52}
53
54static void
e14e13a4
FP
55xaw_update_scrollbar (instance, widget, val)
56 widget_instance *instance;
57 Widget widget;
58 widget_value *val;
b70cfce2
FP
59{
60#if 0
61 if (val->scrollbar_data)
62 {
63 scrollbar_values *data = val->scrollbar_data;
64 Dimension height, width;
65 Dimension pos_x, pos_y;
66 int widget_shown, widget_topOfThumb;
67 float new_shown, new_topOfThumb;
68
69 XtVaGetValues (widget,
70 XtNheight, &height,
71 XtNwidth, &width,
72 XtNx, &pos_x,
73 XtNy, &pos_y,
74 XtNtopOfThumb, &widget_topOfThumb,
75 XtNshown, &widget_shown,
76 0);
77
78 /*
79 * First size and position the scrollbar widget.
80 * We need to position it to second-guess the Paned widget's notion
81 * of what should happen when the WMShell gets resized.
82 */
83 if (height != data->scrollbar_height || pos_y != data->scrollbar_pos)
84 {
85 XtConfigureWidget (widget, pos_x, data->scrollbar_pos,
86 width, data->scrollbar_height, 0);
87
88 XtVaSetValues (widget,
89 XtNlength, data->scrollbar_height,
90 XtNthickness, width,
91 0);
92 }
93
94 /*
95 * Now the size the scrollbar's slider.
96 */
97 new_shown = (float) data->slider_size /
98 (float) (data->maximum - data->minimum);
99
100 new_topOfThumb = (float) (data->slider_position - data->minimum) /
101 (float) (data->maximum - data->minimum);
102
103 if (new_shown > 1.0)
104 new_shown = 1.0;
105 if (new_shown < 0)
106 new_shown = 0;
107
108 if (new_topOfThumb > 1.0)
109 new_topOfThumb = 1.0;
110 if (new_topOfThumb < 0)
111 new_topOfThumb = 0;
112
113 if (new_shown != widget_shown || new_topOfThumb != widget_topOfThumb)
114 XawScrollbarSetThumb (widget, new_topOfThumb, new_shown);
115 }
116#endif
117}
118
119void
e14e13a4
FP
120xaw_update_one_widget (instance, widget, val, deep_p)
121 widget_instance *instance;
122 Widget widget;
123 widget_value *val;
124 Boolean deep_p;
b70cfce2 125{
13162c0e 126#if 0
b70cfce2
FP
127 if (XtIsSubclass (widget, scrollbarWidgetClass))
128 {
129 xaw_update_scrollbar (instance, widget, val);
130 }
13162c0e
RS
131#endif
132 if (XtIsSubclass (widget, dialogWidgetClass))
b70cfce2 133 {
05041ba0
FP
134 Arg al[1];
135 int ac = 0;
136 XtSetArg (al[ac], XtNlabel, val->contents->value); ac++;
137 XtSetValues (widget, al, ac);
b70cfce2
FP
138 }
139 else if (XtIsSubclass (widget, commandWidgetClass))
140 {
141 Dimension bw = 0;
13162c0e
RS
142 Arg al[3];
143
b70cfce2
FP
144 XtVaGetValues (widget, XtNborderWidth, &bw, 0);
145 if (bw == 0)
146 /* Don't let buttons end up with 0 borderwidth, that's ugly...
147 Yeah, all this should really be done through app-defaults files
148 or fallback resources, but that's a whole different can of worms
149 that I don't feel like opening right now. Making Athena widgets
150 not look like shit is just entirely too much work.
151 */
13162c0e
RS
152 {
153 XtSetArg (al[0], XtNborderWidth, 1);
154 XtSetValues (widget, al, 1);
155 }
b70cfce2 156
13162c0e
RS
157 XtSetArg (al[0], XtNlabel, val->value);
158 XtSetArg (al[1], XtNsensitive, val->enabled);
159 /* Force centered button text. Se above. */
160 XtSetArg (al[2], XtNjustify, XtJustifyCenter);
161 XtSetValues (widget, al, 3);
b70cfce2
FP
162 XtRemoveAllCallbacks (widget, XtNcallback);
163 XtAddCallback (widget, XtNcallback, xaw_generic_callback, instance);
164 }
165}
166
167void
e14e13a4
FP
168xaw_update_one_value (instance, widget, val)
169 widget_instance *instance;
170 Widget widget;
171 widget_value *val;
b70cfce2
FP
172{
173 /* This function is not used by the scrollbars and those are the only
174 Athena widget implemented at the moment so do nothing. */
175 return;
176}
177
178void
e14e13a4
FP
179xaw_destroy_instance (instance)
180 widget_instance *instance;
b70cfce2
FP
181{
182 if (XtIsSubclass (instance->widget, dialogWidgetClass))
183 /* Need to destroy the Shell too. */
184 XtDestroyWidget (XtParent (instance->widget));
185 else
186 XtDestroyWidget (instance->widget);
187}
188
189void
e14e13a4
FP
190xaw_popup_menu (widget)
191 Widget widget;
b70cfce2
FP
192{
193 /* An Athena menubar has not been implemented. */
194 return;
195}
196
197void
e14e13a4
FP
198xaw_pop_instance (instance, up)
199 widget_instance *instance;
200 Boolean up;
b70cfce2
FP
201{
202 Widget widget = instance->widget;
203
204 if (up)
205 {
206 if (XtIsSubclass (widget, dialogWidgetClass))
207 {
208 /* For dialogs, we need to call XtPopup on the parent instead
209 of calling XtManageChild on the widget.
210 Also we need to hack the shell's WM_PROTOCOLS to get it to
211 understand what the close box is supposed to do!!
212 */
213 Display *dpy = XtDisplay (widget);
214 Widget shell = XtParent (widget);
215 Atom props [2];
216 int i = 0;
217 props [i++] = XInternAtom (dpy, "WM_DELETE_WINDOW", False);
218 XChangeProperty (dpy, XtWindow (shell),
219 XInternAtom (dpy, "WM_PROTOCOLS", False),
220 XA_ATOM, 32, PropModeAppend,
221 (unsigned char *) props, i);
222
223 /* Center the widget in its parent. Why isn't this kind of crap
224 done automatically? I thought toolkits were supposed to make
225 life easier?
226 */
227 {
e363091e 228 unsigned int x, y, w, h;
b70cfce2 229 Widget topmost = instance->parent;
2af91681
PR
230 Arg args[2];
231
b70cfce2
FP
232 w = shell->core.width;
233 h = shell->core.height;
234 while (topmost->core.parent && XtIsRealized (topmost->core.parent))
235 topmost = topmost->core.parent;
236 if (topmost->core.width < w) x = topmost->core.x;
237 else x = topmost->core.x + ((topmost->core.width - w) / 2);
238 if (topmost->core.height < h) y = topmost->core.y;
239 else y = topmost->core.y + ((topmost->core.height - h) / 2);
2af91681
PR
240 /* Using XtMoveWidget caused the widget to come
241 out in the wrong place with vtwm.
242 Question of virtual vs real coords, perhaps. */
243 XtSetArg (args[0], XtNx, x);
244 XtSetArg (args[1], XtNy, y);
245 XtSetValues (shell, args, 2);
b70cfce2
FP
246 }
247
248 /* Finally, pop it up. */
249 XtPopup (shell, XtGrabNonexclusive);
250 }
251 else
252 XtManageChild (widget);
253 }
254 else
255 {
256 if (XtIsSubclass (widget, dialogWidgetClass))
257 XtUnmanageChild (XtParent (widget));
258 else
259 XtUnmanageChild (widget);
260 }
261}
262
263\f
264/* Dialog boxes */
265
266static char overrideTrans[] =
267 "<Message>WM_PROTOCOLS: lwlib_delete_dialog()";
268static void wm_delete_window();
269static XtActionsRec xaw_actions [] = {
270 {"lwlib_delete_dialog", wm_delete_window}
271};
272static Boolean actions_initted = False;
273
274static Widget
e14e13a4
FP
275make_dialog (name, parent, pop_up_p, shell_title, icon_name, text_input_slot, radio_box, list, left_buttons, right_buttons)
276 char* name;
277 Widget parent;
278 Boolean pop_up_p;
279 char* shell_title;
280 char* icon_name;
281 Boolean text_input_slot;
282 Boolean radio_box;
283 Boolean list;
284 int left_buttons;
285 int right_buttons;
b70cfce2
FP
286{
287 Arg av [20];
288 int ac = 0;
289 int i, bc;
290 char button_name [255];
291 Widget shell;
292 Widget dialog;
293 Widget button;
294 XtTranslations override;
295
296 if (! pop_up_p) abort (); /* not implemented */
297 if (text_input_slot) abort (); /* not implemented */
298 if (radio_box) abort (); /* not implemented */
299 if (list) abort (); /* not implemented */
300
301 if (! actions_initted)
302 {
303 XtAppContext app = XtWidgetToApplicationContext (parent);
304 XtAppAddActions (app, xaw_actions,
305 sizeof (xaw_actions) / sizeof (xaw_actions[0]));
306 actions_initted = True;
307 }
308
309 override = XtParseTranslationTable (overrideTrans);
310
311 ac = 0;
312 XtSetArg (av[ac], XtNtitle, shell_title); ac++;
313 XtSetArg (av[ac], XtNallowShellResize, True); ac++;
dc49df96
FP
314
315 /* Don't allow any geometry request from the user. */
316 XtSetArg (av[ac], XtNgeometry, 0); ac++;
317
b70cfce2
FP
318 shell = XtCreatePopupShell ("dialog", transientShellWidgetClass,
319 parent, av, ac);
320 XtOverrideTranslations (shell, override);
321
322 ac = 0;
323 dialog = XtCreateManagedWidget (name, dialogWidgetClass, shell, av, ac);
324
325 bc = 0;
326 button = 0;
327 for (i = 0; i < left_buttons; i++)
328 {
329 ac = 0;
330 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
331 XtSetArg (av [ac], XtNleft, XtChainLeft); ac++;
332 XtSetArg (av [ac], XtNright, XtChainLeft); ac++;
333 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
334 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
335 XtSetArg (av [ac], XtNresizable, True); ac++;
336 sprintf (button_name, "button%d", ++bc);
337 button = XtCreateManagedWidget (button_name, commandWidgetClass,
338 dialog, av, ac);
339 }
340 if (right_buttons)
341 {
342 /* Create a separator
343
344 I want the separator to take up the slack between the buttons on
345 the right and the buttons on the left (that is I want the buttons
346 after the separator to be packed against the right edge of the
347 window) but I can't seem to make it do it.
348 */
349 ac = 0;
350 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
351/* XtSetArg (av [ac], XtNfromVert, XtNameToWidget (dialog, "label")); ac++; */
352 XtSetArg (av [ac], XtNleft, XtChainLeft); ac++;
353 XtSetArg (av [ac], XtNright, XtChainRight); ac++;
354 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
355 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
356 XtSetArg (av [ac], XtNlabel, ""); ac++;
357 XtSetArg (av [ac], XtNwidth, 30); ac++; /* #### aaack!! */
358 XtSetArg (av [ac], XtNborderWidth, 0); ac++;
359 XtSetArg (av [ac], XtNshapeStyle, XmuShapeRectangle); ac++;
360 XtSetArg (av [ac], XtNresizable, False); ac++;
361 XtSetArg (av [ac], XtNsensitive, False); ac++;
362 button = XtCreateManagedWidget ("separator",
363 /* labelWidgetClass, */
364 /* This has to be Command to fake out
365 the Dialog widget... */
366 commandWidgetClass,
367 dialog, av, ac);
368 }
369 for (i = 0; i < right_buttons; i++)
370 {
371 ac = 0;
372 XtSetArg (av [ac], XtNfromHoriz, button); ac++;
373 XtSetArg (av [ac], XtNleft, XtChainRight); ac++;
374 XtSetArg (av [ac], XtNright, XtChainRight); ac++;
375 XtSetArg (av [ac], XtNtop, XtChainBottom); ac++;
376 XtSetArg (av [ac], XtNbottom, XtChainBottom); ac++;
377 XtSetArg (av [ac], XtNresizable, True); ac++;
378 sprintf (button_name, "button%d", ++bc);
379 button = XtCreateManagedWidget (button_name, commandWidgetClass,
380 dialog, av, ac);
381 }
382
383 return dialog;
384}
385
386Widget
e14e13a4
FP
387xaw_create_dialog (instance)
388 widget_instance* instance;
b70cfce2
FP
389{
390 char *name = instance->info->type;
391 Widget parent = instance->parent;
392 Widget widget;
393 Boolean pop_up_p = instance->pop_up_p;
394 char *shell_name = 0;
395 char *icon_name;
396 Boolean text_input_slot = False;
397 Boolean radio_box = False;
398 Boolean list = False;
399 int total_buttons;
400 int left_buttons = 0;
401 int right_buttons = 1;
402
403 switch (name [0]) {
404 case 'E': case 'e':
405 icon_name = "dbox-error";
406 shell_name = "Error";
407 break;
408
409 case 'I': case 'i':
410 icon_name = "dbox-info";
411 shell_name = "Information";
412 break;
413
414 case 'L': case 'l':
415 list = True;
416 icon_name = "dbox-question";
417 shell_name = "Prompt";
418 break;
419
420 case 'P': case 'p':
421 text_input_slot = True;
422 icon_name = "dbox-question";
423 shell_name = "Prompt";
424 break;
425
426 case 'Q': case 'q':
427 icon_name = "dbox-question";
428 shell_name = "Question";
429 break;
430 }
431
432 total_buttons = name [1] - '0';
433
434 if (name [3] == 'T' || name [3] == 't')
435 {
436 text_input_slot = False;
437 radio_box = True;
438 }
439 else if (name [3])
440 right_buttons = name [4] - '0';
441
442 left_buttons = total_buttons - right_buttons;
443
444 widget = make_dialog (name, parent, pop_up_p,
445 shell_name, icon_name, text_input_slot, radio_box,
446 list, left_buttons, right_buttons);
447
448 return widget;
449}
450
451
452static void
e14e13a4
FP
453xaw_generic_callback (widget, closure, call_data)
454 Widget widget;
455 XtPointer closure;
456 XtPointer call_data;
b70cfce2
FP
457{
458 widget_instance *instance = (widget_instance *) closure;
459 Widget instance_widget;
460 LWLIB_ID id;
461 XtPointer user_data;
462
463 lw_internal_update_other_instances (widget, closure, call_data);
464
465 if (! instance)
466 return;
467 if (widget->core.being_destroyed)
468 return;
469
470 instance_widget = instance->widget;
471 if (!instance_widget)
472 return;
473
474 id = instance->info->id;
475
476#if 0
477 user_data = NULL;
478 XtVaGetValues (widget, XtNuserData, &user_data, 0);
479#else
480 /* Damn! Athena doesn't give us a way to hang our own data on the
481 buttons, so we have to go find it... I guess this assumes that
482 all instances of a button have the same call data. */
483 {
484 widget_value *val = instance->info->val->contents;
485 char *name = XtName (widget);
486 while (val)
487 {
488 if (val->name && !strcmp (val->name, name))
489 break;
490 val = val->next;
491 }
492 if (! val) abort ();
493 user_data = val->call_data;
494 }
495#endif
496
497 if (instance->info->selection_cb)
498 instance->info->selection_cb (widget, id, user_data);
499}
500
501static void
e14e13a4
FP
502wm_delete_window (shell, closure, call_data)
503 Widget shell;
504 XtPointer closure;
505 XtPointer call_data;
b70cfce2
FP
506{
507 LWLIB_ID id;
8dce2ddc
RS
508 Cardinal nkids;
509 int i;
b70cfce2
FP
510 Widget *kids = 0;
511 Widget widget;
512 if (! XtIsSubclass (shell, shellWidgetClass))
513 abort ();
8dce2ddc 514 XtVaGetValues (shell, XtNnumChildren, &nkids, 0);
b70cfce2
FP
515 XtVaGetValues (shell, XtNchildren, &kids, 0);
516 if (!kids || !*kids)
517 abort ();
8dce2ddc
RS
518 for (i = 0; i < nkids; i++)
519 {
520 widget = kids[i];
521 if (XtIsSubclass (widget, dialogWidgetClass))
522 break;
523 }
b70cfce2
FP
524 id = lw_get_widget_id (widget);
525 if (! id) abort ();
526
527 {
528 widget_info *info = lw_get_widget_info (id);
529 if (! info) abort ();
530 if (info->selection_cb)
531 info->selection_cb (widget, id, (XtPointer) -1);
532 }
533
534 lw_destroy_all_widgets (id);
535}
536
537\f
538/* Scrollbars */
539
540static void
e14e13a4
FP
541xaw_scrollbar_scroll (widget, closure, call_data)
542 Widget widget;
543 XtPointer closure;
544 XtPointer call_data;
b70cfce2
FP
545{
546#if 0
547 widget_instance *instance = (widget_instance *) closure;
548 LWLIB_ID id;
549 scroll_event event_data;
550
551 if (!instance || widget->core.being_destroyed)
552 return;
553
554 id = instance->info->id;
555 event_data.slider_value = 0;
556 event_data.time = 0;
557
558 if ((int) call_data > 0)
559 event_data.action = SCROLLBAR_PAGE_DOWN;
560 else
561 event_data.action = SCROLLBAR_PAGE_UP;
562
563 if (instance->info->pre_activate_cb)
564 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
565#endif
566}
567
568static void
e14e13a4
FP
569xaw_scrollbar_jump (widget, closure, call_data)
570 Widget widget;
571 XtPointer closure;
572 XtPointer call_data;
b70cfce2
FP
573{
574#if 0
575 widget_instance *instance = (widget_instance *) closure;
576 LWLIB_ID id;
577 scroll_event event_data;
578 scrollbar_values *val =
579 (scrollbar_values *) instance->info->val->scrollbar_data;
580 float percent;
581
582 if (!instance || widget->core.being_destroyed)
583 return;
584
585 id = instance->info->id;
586
587 percent = * (float *) call_data;
588 event_data.slider_value =
589 (int) (percent * (float) (val->maximum - val->minimum)) + val->minimum;
590
591 event_data.time = 0;
592 event_data.action = SCROLLBAR_DRAG;
593
594 if (instance->info->pre_activate_cb)
595 instance->info->pre_activate_cb (widget, id, (XtPointer) &event_data);
596#endif
597}
598
599static Widget
e14e13a4
FP
600xaw_create_scrollbar (instance)
601 widget_instance *instance;
b70cfce2
FP
602{
603#if 0
604 Arg av[20];
605 int ac = 0;
606 Dimension width;
607 Widget scrollbar;
608
609 XtVaGetValues (instance->parent, XtNwidth, &width, 0);
610
611 XtSetArg (av[ac], XtNshowGrip, 0); ac++;
612 XtSetArg (av[ac], XtNresizeToPreferred, 1); ac++;
613 XtSetArg (av[ac], XtNallowResize, True); ac++;
614 XtSetArg (av[ac], XtNskipAdjust, True); ac++;
615 XtSetArg (av[ac], XtNwidth, width); ac++;
616 XtSetArg (av[ac], XtNmappedWhenManaged, True); ac++;
617
618 scrollbar =
619 XtCreateWidget (instance->info->name, scrollbarWidgetClass,
620 instance->parent, av, ac);
621
622 /* We have to force the border width to be 0 otherwise the
623 geometry manager likes to start looping for awhile... */
624 XtVaSetValues (scrollbar, XtNborderWidth, 0, 0);
625
626 XtRemoveAllCallbacks (scrollbar, "jumpProc");
627 XtRemoveAllCallbacks (scrollbar, "scrollProc");
628
629 XtAddCallback (scrollbar, "jumpProc", xaw_scrollbar_jump,
630 (XtPointer) instance);
631 XtAddCallback (scrollbar, "scrollProc", xaw_scrollbar_scroll,
632 (XtPointer) instance);
633
634 return scrollbar;
635#endif
636}
637
2af91681
PR
638static Widget
639xaw_create_main (instance)
640 widget_instance *instance;
641{
642 Arg al[1];
643 int ac;
644
645 /* Create a vertical Paned to hold menubar */
646 ac = 0;
647 XtSetArg (al[ac], XtNborderWidth, 0); ac++;
648 return XtCreateWidget (instance->info->name, panedWidgetClass,
649 instance->parent, al, ac);
650}
651
b70cfce2
FP
652widget_creation_entry
653xaw_creation_table [] =
654{
655 {"scrollbar", xaw_create_scrollbar},
2af91681 656 {"main", xaw_create_main},
b70cfce2
FP
657 {NULL, NULL}
658};