1 /* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10 /*
11 * (C) 2001,2005 by Marcin Dalecki <martin@dalecki.de>
12 *
13 * Implementation of dialog functions for the Motif GUI variant.
14 *
15 * Note about Lesstif: Apparently lesstif doesn't get the widget layout right,
16 * when using a dynamic scrollbar policy.
17 */
18
19 #include "vim.h"
20
21 #include <Xm/Form.h>
22 #include <Xm/PushBG.h>
23 #include <Xm/Text.h>
24 #include <Xm/TextF.h>
25 #include <Xm/Label.h>
26 #include <Xm/Frame.h>
27 #include <Xm/LabelG.h>
28 #include <Xm/ToggleBG.h>
29 #include <Xm/SeparatoG.h>
30 #include <Xm/DialogS.h>
31 #include <Xm/List.h>
32 #include <Xm/RowColumn.h>
33 #include <Xm/AtomMgr.h>
34 #include <Xm/Protocols.h>
35
36 #include <X11/keysym.h>
37 #include <X11/Xatom.h>
38 #include <X11/StringDefs.h>
39 #include <X11/Intrinsic.h>
40
41 extern Widget vimShell;
42
43 #ifdef FEAT_MENU
44 # define apply_fontlist(w) gui_motif_menu_fontlist(w)
45 #else
46 # define apply_fontlist(w)
47 #endif
48
49 /////////////////////////////////////////////////////////////////////////////
50 // Font selection dialogue implementation.
51
52 static char wild[3] = "*";
53
54 /*
55 * FIXME: This is a generic function, which should be used throughout the whole
56 * application.
57 *
58 * Add close_callback, which will be called when the user selects close from
59 * the window menu. The close menu item usually activates f.kill which sends a
60 * WM_DELETE_WINDOW protocol request for the window.
61 */
62
63 static void
add_cancel_action(Widget shell,XtCallbackProc close_callback,void * arg)64 add_cancel_action(Widget shell, XtCallbackProc close_callback, void *arg)
65 {
66 static Atom wmp_atom = 0;
67 static Atom dw_atom = 0;
68 Display *display = XtDisplay(shell);
69
70 // deactivate the built-in delete response of killing the application
71 XtVaSetValues(shell, XmNdeleteResponse, XmDO_NOTHING, NULL);
72
73 // add a delete window protocol callback instead
74 if (!dw_atom)
75 {
76 wmp_atom = XmInternAtom(display, "WM_PROTOCOLS", True);
77 dw_atom = XmInternAtom(display, "WM_DELETE_WINDOW", True);
78 }
79 XmAddProtocolCallback(shell, wmp_atom, dw_atom, close_callback, arg);
80 }
81
82 #define MAX_FONTS 65535
83 #define MAX_FONT_NAME_LEN 256
84 #define MAX_ENTRIES_IN_LIST 5000
85 #define MAX_DISPLAY_SIZE 150
86 #define TEMP_BUF_SIZE 256
87
88 enum ListSpecifier
89 {
90 ENCODING,
91 NAME,
92 STYLE,
93 SIZE,
94 NONE
95 };
96
97 typedef struct _SharedFontSelData
98 {
99 Widget dialog;
100 Widget ok;
101 Widget cancel;
102 Widget encoding_pulldown;
103 Widget encoding_menu;
104 Widget list[NONE];
105 Widget name;
106 Widget sample;
107 char **names; // font name array of arrays
108 int num; // number of font names
109 String sel[NONE]; // selection category
110 Boolean in_pixels; // toggle state - size in pixels
111 char *font_name; // current font name
112 XFontStruct *old; // font data structure for sample display
113 XmFontList old_list; // font data structure for sample display
114 Boolean exit; // used for program exit control
115 } SharedFontSelData;
116
117 /*
118 * Checking access to the font name array for validity.
119 */
120 static char *
fn(SharedFontSelData * data,int i)121 fn(SharedFontSelData *data, int i)
122 {
123 // Assertion checks:
124 if (data->num < 0)
125 abort();
126 if (i >= data->num)
127 i = data->num - 1;
128 if (i < 0)
129 i = 0;
130
131 return data->names[i];
132 }
133
134 /*
135 * Get a specific substring from a font name.
136 */
137 static void
get_part(char * in,int pos,char * out)138 get_part(char *in, int pos, char *out)
139 {
140 int i;
141 int j;
142
143 *out = '\0';
144
145 for (i = 0; (pos > 0) && (in[i] != '\0'); ++i)
146 if (in[i] == '-')
147 pos--;
148
149 if (in[i] == '\0')
150 return;
151
152 for (j = 0; (in[i] != '-') && (in[i] != '\0'); ++i, ++j)
153 out[j] = in[i];
154 out[j] = '\0';
155 }
156
157 /*
158 * Given a font name this function returns the part used in the first
159 * scroll list.
160 */
161 static void
name_part(char * font,char * buf)162 name_part(char *font, char *buf)
163 {
164 char buf2[TEMP_BUF_SIZE];
165 char buf3[TEMP_BUF_SIZE];
166
167 get_part(font, 2, buf2);
168 get_part(font, 1, buf3);
169
170 if (*buf3 != NUL)
171 vim_snprintf(buf, TEMP_BUF_SIZE, "%s (%s)", buf2, buf3);
172 else
173 vim_snprintf(buf, TEMP_BUF_SIZE, "%s", buf2);
174 }
175
176 /*
177 * Given a font name this function returns the part used in the second scroll list.
178 */
179 static void
style_part(char * font,char * buf)180 style_part(char *font, char *buf)
181 {
182 char buf2[TEMP_BUF_SIZE];
183 char buf3[TEMP_BUF_SIZE];
184
185 get_part(font, 3, buf3);
186 get_part(font, 5, buf2);
187
188 if (!strcmp(buf2, "normal") && !strcmp(buf2, "Normal")
189 && !strcmp(buf2, "NORMAL"))
190 vim_snprintf(buf, TEMP_BUF_SIZE, "%s %s", buf3, buf2);
191 else
192 strcpy(buf, buf3);
193
194 get_part(font, 6, buf2);
195
196 if (buf2[0] != '\0')
197 vim_snprintf(buf3, TEMP_BUF_SIZE, "%s %s", buf, buf2);
198 else
199 strcpy(buf3, buf);
200
201 get_part(font, 4, buf2);
202
203 if (!strcmp(buf2, "o") || !strcmp(buf2, "O"))
204 vim_snprintf(buf, TEMP_BUF_SIZE, "%s oblique", buf3);
205 else if (!strcmp(buf2, "i") || !strcmp(buf2, "I"))
206 vim_snprintf(buf, TEMP_BUF_SIZE, "%s italic", buf3);
207
208 if (!strcmp(buf, " "))
209 strcpy(buf, "-");
210 }
211
212 /*
213 * Given a font name this function returns the part used in the third
214 * scroll list.
215 */
216 static void
size_part(char * font,char * buf,int inPixels)217 size_part(char *font, char *buf, int inPixels)
218 {
219 int size;
220 float temp;
221
222 *buf = '\0';
223
224 if (inPixels)
225 {
226 get_part(font, 7, buf);
227 if (*buf != NUL)
228 {
229 size = atoi(buf);
230 sprintf(buf, "%3d", size);
231 }
232 }
233 else
234 {
235 get_part(font, 8, buf);
236 if (*buf != NUL)
237 {
238 size = atoi(buf);
239 temp = (float)size / 10.0;
240 size = temp;
241 if (buf[strlen(buf) - 1] == '0')
242 sprintf(buf, "%3d", size);
243 else
244 sprintf(buf, "%4.1f", temp);
245 }
246 }
247 }
248
249 /*
250 * Given a font name this function returns the part used in the choice menu.
251 */
252 static void
encoding_part(char * font,char * buf)253 encoding_part(char *font, char *buf)
254 {
255 char buf1[TEMP_BUF_SIZE];
256 char buf2[TEMP_BUF_SIZE];
257
258 *buf = '\0';
259
260 get_part(font, 13, buf1);
261 get_part(font, 14, buf2);
262
263 if (*buf1 != NUL && *buf2 != NUL)
264 vim_snprintf(buf, TEMP_BUF_SIZE, "%s-%s", buf1, buf2);
265 if (!strcmp(buf, " "))
266 strcpy(buf, "-");
267 }
268
269 /*
270 * Inserts a string into correct sorted position in a list.
271 */
272 static void
add_to_list(char ** buf,char * item,int * count)273 add_to_list(char **buf, char *item, int *count)
274 {
275 int i;
276 int j;
277
278 if (*count == MAX_ENTRIES_IN_LIST)
279 return;
280
281 // avoid duplication
282 for (i = 0; i < *count; ++i)
283 {
284 if (!strcmp(buf[i], item))
285 return;
286 }
287
288 // find order place, but make sure that wild card comes first
289 if (!strcmp(item, wild))
290 i = 0;
291 else
292 for (i = 0; i < *count; ++i)
293 if (strcmp(buf[i], item) > 0 && strcmp(buf[i], wild))
294 break;
295
296 // now insert the item
297 for (j = *count; j > i; --j)
298 buf[j] = buf[j-1];
299 buf[i] = XtNewString(item);
300
301 ++(*count);
302 }
303
304 /*
305 * True if the font matches some field.
306 */
307 static Boolean
match(SharedFontSelData * data,enum ListSpecifier l,int i)308 match(SharedFontSelData *data, enum ListSpecifier l, int i)
309 {
310 char buf[TEMP_BUF_SIZE];
311
312 // An empty selection or a wild card matches anything.
313 if (!data->sel[l] || !strcmp(data->sel[l], wild))
314 return True;
315
316 // chunk out the desired part...
317 switch (l)
318 {
319 case ENCODING:
320 encoding_part(fn(data, i), buf);
321 break;
322
323 case NAME:
324 name_part(fn(data, i), buf);
325 break;
326
327 case STYLE:
328 style_part(fn(data, i), buf);
329 break;
330
331 case SIZE:
332 size_part(fn(data, i), buf, data->in_pixels);
333 break;
334 default:
335 ;
336 }
337
338 // ...and chew it now
339
340 return !strcmp(buf, data->sel[l]);
341 }
342
343 static Boolean
proportional(char * font)344 proportional(char *font)
345 {
346 char buf[TEMP_BUF_SIZE];
347
348 get_part(font, 11, buf);
349
350 return !strcmp(buf, "p") || !strcmp(buf, "P");
351 }
352
353
354 static void encoding_callback(Widget w, SharedFontSelData *data,
355 XtPointer dummy);
356
357 /*
358 * Parse through the fontlist data and set up the three scroll lists. The fix
359 * parameter can be used to exclude a list from any changes. This is used for
360 * updates after selections caused by the users actions.
361 */
362 static void
fill_lists(enum ListSpecifier fix,SharedFontSelData * data)363 fill_lists(enum ListSpecifier fix, SharedFontSelData *data)
364 {
365 char *list[NONE][MAX_ENTRIES_IN_LIST];
366 int count[NONE];
367 char buf[TEMP_BUF_SIZE];
368 XmString items[MAX_ENTRIES_IN_LIST];
369 int i;
370 int idx;
371
372 for (idx = (int)ENCODING; idx < (int)NONE; ++idx)
373 count[idx] = 0;
374
375 // First we insert the wild char into every single list.
376 if (fix != ENCODING)
377 add_to_list(list[ENCODING], wild, &count[ENCODING]);
378 if (fix != NAME)
379 add_to_list(list[NAME], wild, &count[NAME]);
380 if (fix != STYLE)
381 add_to_list(list[STYLE], wild, &count[STYLE]);
382 if (fix != SIZE)
383 add_to_list(list[SIZE], wild, &count[SIZE]);
384
385 for (i = 0; i < data->num && i < MAX_ENTRIES_IN_LIST; i++)
386 {
387 if (proportional(fn(data, i)))
388 continue;
389
390 if (fix != ENCODING
391 && match(data, NAME, i)
392 && match(data, STYLE, i)
393 && match(data, SIZE, i))
394 {
395 encoding_part(fn(data, i), buf);
396 add_to_list(list[ENCODING], buf, &count[ENCODING]);
397 }
398
399 if (fix != NAME
400 && match(data, ENCODING, i)
401 && match(data, STYLE, i)
402 && match(data, SIZE, i))
403 {
404 name_part(fn(data, i), buf);
405 add_to_list(list[NAME], buf, &count[NAME]);
406 }
407
408 if (fix != STYLE
409 && match(data, ENCODING, i)
410 && match(data, NAME, i)
411 && match(data, SIZE, i))
412 {
413 style_part(fn(data, i), buf);
414 add_to_list(list[STYLE], buf, &count[STYLE]);
415 }
416
417 if (fix != SIZE
418 && match(data, ENCODING, i)
419 && match(data, NAME, i)
420 && match(data, STYLE, i))
421 {
422 size_part(fn(data, i), buf, data->in_pixels);
423 add_to_list(list[SIZE], buf, &count[SIZE]);
424 }
425 }
426
427 /*
428 * And now do the preselection in all lists where there was one:
429 */
430
431 if (fix != ENCODING)
432 {
433 Cardinal n_items;
434 WidgetList children;
435 Widget selected_button = 0;
436
437 // Get and update the current button list.
438 XtVaGetValues(data->encoding_pulldown,
439 XmNchildren, &children,
440 XmNnumChildren, &n_items,
441 NULL);
442
443 for (i = 0; i < count[ENCODING]; ++i)
444 {
445 Widget button;
446
447 items[i] = XmStringCreateLocalized(list[ENCODING][i]);
448
449 if (i < (int)n_items)
450 {
451 // recycle old button
452 XtVaSetValues(children[i],
453 XmNlabelString, items[i],
454 XmNuserData, i,
455 NULL);
456 button = children[i];
457 }
458 else
459 {
460 // create a new button
461 button = XtVaCreateManagedWidget("button",
462 xmPushButtonGadgetClass,
463 data->encoding_pulldown,
464 XmNlabelString, items[i],
465 XmNuserData, i,
466 NULL);
467 XtAddCallback(button, XmNactivateCallback,
468 (XtCallbackProc) encoding_callback, (XtPointer) data);
469 XtManageChild(button);
470 }
471
472 if (data->sel[ENCODING])
473 {
474 if (!strcmp(data->sel[ENCODING], list[ENCODING][i]))
475 selected_button = button;
476 }
477 XtFree(list[ENCODING][i]);
478 }
479
480 // Destroy all the outstanding menu items.
481 for (i = count[ENCODING]; i < (int)n_items; ++i)
482 {
483 XtUnmanageChild(children[i]);
484 XtDestroyWidget(children[i]);
485 }
486
487 // Preserve the current selection visually.
488 if (selected_button)
489 {
490 XtVaSetValues(data->encoding_menu,
491 XmNmenuHistory, selected_button,
492 NULL);
493 }
494
495 for (i = 0; i < count[ENCODING]; ++i)
496 XmStringFree(items[i]);
497 }
498
499 /*
500 * Now loop through the remaining lists and set them up.
501 */
502 for (idx = (int)NAME; idx < (int)NONE; ++idx)
503 {
504 Widget w;
505
506 if (fix == (enum ListSpecifier)idx)
507 continue;
508
509 switch ((enum ListSpecifier)idx)
510 {
511 case NAME:
512 w = data->list[NAME];
513 break;
514 case STYLE:
515 w = data->list[STYLE];
516 break;
517 case SIZE:
518 w = data->list[SIZE];
519 break;
520 default:
521 w = (Widget)0; // for lint
522 }
523
524 for (i = 0; i < count[idx]; ++i)
525 {
526 items[i] = XmStringCreateLocalized(list[idx][i]);
527 XtFree(list[idx][i]);
528 }
529 XmListDeleteAllItems(w);
530 XmListAddItems(w, items, count[idx], 1);
531 if (data->sel[idx])
532 {
533 XmStringFree(items[0]);
534 items[0] = XmStringCreateLocalized(data->sel[idx]);
535 XmListSelectItem(w, items[0], False);
536 XmListSetBottomItem(w, items[0]);
537 }
538 for (i = 0; i < count[idx]; ++i)
539 XmStringFree(items[i]);
540 }
541 }
542
543 static void
stoggle_callback(Widget w UNUSED,SharedFontSelData * data,XmToggleButtonCallbackStruct * call_data)544 stoggle_callback(Widget w UNUSED,
545 SharedFontSelData *data,
546 XmToggleButtonCallbackStruct *call_data)
547 {
548 int i, do_sel;
549 char newSize[TEMP_BUF_SIZE];
550 XmString str;
551
552 if (call_data->reason != (int)XmCR_VALUE_CHANGED)
553 return;
554
555 do_sel = (data->sel[SIZE] != NULL) && strcmp(data->sel[SIZE], wild);
556
557 for (i = 0; do_sel && (i < data->num); i++)
558 if (match(data, ENCODING, i)
559 && match(data, NAME, i)
560 && match(data, STYLE, i)
561 && match(data, SIZE, i))
562 {
563 size_part(fn(data, i), newSize, !data->in_pixels);
564 break;
565 }
566
567 data->in_pixels = !data->in_pixels;
568
569 if (data->sel[SIZE])
570 XtFree(data->sel[SIZE]);
571 data->sel[SIZE] = NULL;
572 fill_lists(NONE, data);
573
574 if (do_sel)
575 {
576 str = XmStringCreateLocalized(newSize);
577 XmListSelectItem(data->list[SIZE], str, True);
578 XmListSetBottomItem(data->list[SIZE], str);
579 XmStringFree(str);
580 }
581 }
582
583 /*
584 * Show the currently selected font in the sample text label.
585 */
586 static void
display_sample(SharedFontSelData * data)587 display_sample(SharedFontSelData *data)
588 {
589 Arg args[2];
590 int n;
591 XFontStruct * font;
592 XmFontList font_list;
593 Display * display;
594 XmString str;
595
596 display = XtDisplay(data->dialog);
597 font = XLoadQueryFont(display, data->font_name);
598 font_list = gui_motif_create_fontlist(font);
599
600 n = 0;
601 str = XmStringCreateLocalized("AaBbZzYy 0123456789");
602 XtSetArg(args[n], XmNlabelString, str); n++;
603 XtSetArg(args[n], XmNfontList, font_list); n++;
604
605 XtSetValues(data->sample, args, n);
606 XmStringFree(str);
607
608 if (data->old)
609 {
610 XFreeFont(display, data->old);
611 XmFontListFree(data->old_list);
612 }
613 data->old = font;
614 data->old_list = font_list;
615 }
616
617
618 static Boolean
do_choice(Widget w,SharedFontSelData * data,XmListCallbackStruct * call_data,enum ListSpecifier which)619 do_choice(Widget w,
620 SharedFontSelData *data,
621 XmListCallbackStruct *call_data,
622 enum ListSpecifier which)
623 {
624 char *sel;
625
626 XmStringGetLtoR(call_data->item, XmSTRING_DEFAULT_CHARSET, &sel);
627
628 if (!data->sel[which])
629 data->sel[which] = XtNewString(sel);
630 else
631 {
632 if (!strcmp(data->sel[which], sel))
633 {
634 // unselecting current selection
635 XtFree(data->sel[which]);
636 data->sel[which] = NULL;
637 if (w)
638 XmListDeselectItem(w, call_data->item);
639 }
640 else
641 {
642 XtFree(data->sel[which]);
643 data->sel[which] = XtNewString(sel);
644 }
645 }
646 XtFree(sel);
647
648 fill_lists(which, data);
649
650 // If there is a font selection, we display it.
651 if (data->sel[ENCODING]
652 && data->sel[NAME]
653 && data->sel[STYLE]
654 && data->sel[SIZE]
655 && strcmp(data->sel[ENCODING], wild)
656 && strcmp(data->sel[NAME], wild)
657 && strcmp(data->sel[STYLE], wild)
658 && strcmp(data->sel[SIZE], wild))
659 {
660 int i;
661
662 if (data->font_name)
663 XtFree(data->font_name);
664 data->font_name = NULL;
665
666 for (i = 0; i < data->num; i++)
667 {
668 if (match(data, ENCODING, i)
669 && match(data, NAME, i)
670 && match(data, STYLE, i)
671 && match(data, SIZE, i))
672 {
673 data->font_name = XtNewString(fn(data, i));
674 break;
675 }
676 }
677
678 if (data->font_name)
679 {
680 XmTextSetString(data->name, data->font_name);
681 display_sample(data);
682 }
683 else
684 do_dialog(VIM_ERROR,
685 (char_u *)_("Error"),
686 (char_u *)_("Invalid font specification"),
687 (char_u *)_("&Dismiss"), 1, NULL, FALSE);
688
689 return True;
690 }
691 else
692 {
693 int n;
694 XmString str;
695 Arg args[4];
696 char *nomatch_msg = _("no specific match");
697
698 n = 0;
699 str = XmStringCreateLocalized(nomatch_msg);
700 XtSetArg(args[n], XmNlabelString, str); ++n;
701 XtSetValues(data->sample, args, n);
702 apply_fontlist(data->sample);
703 XmTextSetString(data->name, nomatch_msg);
704 XmStringFree(str);
705
706 return False;
707 }
708 }
709
710 static void
encoding_callback(Widget w,SharedFontSelData * data,XtPointer dummy UNUSED)711 encoding_callback(Widget w,
712 SharedFontSelData *data,
713 XtPointer dummy UNUSED)
714 {
715 XmString str;
716 XmListCallbackStruct fake_data;
717
718 XtVaGetValues(w, XmNlabelString, &str, NULL);
719
720 if (!str)
721 return;
722
723 fake_data.item = str;
724
725 do_choice(0, data, &fake_data, ENCODING);
726 }
727
728 static void
name_callback(Widget w,SharedFontSelData * data,XmListCallbackStruct * call_data)729 name_callback(Widget w,
730 SharedFontSelData *data,
731 XmListCallbackStruct *call_data)
732 {
733 do_choice(w, data, call_data, NAME);
734 }
735
736 static void
style_callback(Widget w,SharedFontSelData * data,XmListCallbackStruct * call_data)737 style_callback(Widget w,
738 SharedFontSelData *data,
739 XmListCallbackStruct *call_data)
740 {
741 do_choice(w, data, call_data, STYLE);
742 }
743
744 static void
size_callback(Widget w,SharedFontSelData * data,XmListCallbackStruct * call_data)745 size_callback(Widget w,
746 SharedFontSelData *data,
747 XmListCallbackStruct *call_data)
748 {
749 do_choice(w, data, call_data, SIZE);
750 }
751
752 static void
cancel_callback(Widget w UNUSED,SharedFontSelData * data,XmListCallbackStruct * call_data UNUSED)753 cancel_callback(Widget w UNUSED,
754 SharedFontSelData *data,
755 XmListCallbackStruct *call_data UNUSED)
756 {
757 if (data->sel[ENCODING])
758 {
759 XtFree(data->sel[ENCODING]);
760 data->sel[ENCODING] = NULL;
761 }
762 if (data->sel[NAME])
763 {
764 XtFree(data->sel[NAME]);
765 data->sel[NAME] = NULL;
766 }
767 if (data->sel[STYLE])
768 {
769 XtFree(data->sel[STYLE]);
770 data->sel[STYLE] = NULL;
771 }
772 if (data->sel[SIZE])
773 {
774 XtFree(data->sel[SIZE]);
775 data->sel[SIZE] = NULL;
776 }
777
778 if (data->font_name)
779 XtFree(data->font_name);
780 data->font_name = NULL;
781
782 data->num = 0;
783 XFreeFontNames(data->names);
784 data->names = NULL;
785 data->exit = True;
786 }
787
788 static void
ok_callback(Widget w UNUSED,SharedFontSelData * data,XmPushButtonCallbackStruct * call_data UNUSED)789 ok_callback(Widget w UNUSED,
790 SharedFontSelData *data,
791 XmPushButtonCallbackStruct *call_data UNUSED)
792 {
793 char *pattern;
794 char **name;
795 int i;
796
797 pattern = XmTextGetString(data->name);
798 name = XListFonts(XtDisplay(data->dialog), pattern, 1, &i);
799 XtFree(pattern);
800
801 if (i != 1)
802 {
803 do_dialog(VIM_ERROR,
804 (char_u *)_("Error"),
805 (char_u *)_("Invalid font specification"),
806 (char_u *)_("&Dismiss"), 1, NULL, FALSE);
807 XFreeFontNames(name);
808 }
809 else
810 {
811 if (data->font_name)
812 XtFree(data->font_name);
813 data->font_name = XtNewString(name[0]);
814
815 if (data->sel[ENCODING])
816 {
817 XtFree(data->sel[ENCODING]);
818 data->sel[ENCODING] = NULL;
819 }
820 if (data->sel[NAME])
821 {
822 XtFree(data->sel[NAME]);
823 data->sel[NAME] = NULL;
824 }
825 if (data->sel[STYLE])
826 {
827 XtFree(data->sel[STYLE]);
828 data->sel[STYLE] = NULL;
829 }
830 if (data->sel[SIZE])
831 {
832 XtFree(data->sel[SIZE]);
833 data->sel[SIZE] = NULL;
834 }
835
836 XFreeFontNames(name);
837
838 data->num = 0;
839 XFreeFontNames(data->names);
840 data->names = NULL;
841 data->exit = True;
842 }
843 }
844
845 /*
846 * Returns pointer to an ASCII character string that contains the name of the
847 * selected font (in X format for naming fonts); it is the users responsibility
848 * to free the space allocated to this string.
849 */
850 char_u *
gui_xm_select_font(char_u * current)851 gui_xm_select_font(char_u *current)
852 {
853 static SharedFontSelData _data;
854
855 Widget parent;
856 Widget form;
857 Widget separator;
858 Widget sub_form;
859 Widget size_toggle;
860 Widget name;
861 Widget disp_frame;
862 Widget frame;
863 Arg args[64];
864 int n;
865 XmString str;
866 char big_font[MAX_FONT_NAME_LEN];
867 SharedFontSelData *data;
868
869 data = &_data;
870
871 parent = vimShell;
872 data->names = XListFonts(XtDisplay(parent), "-*-*-*-*-*-*-*-*-*-*-*-*-*-*",
873 MAX_FONTS, &data->num);
874
875 /*
876 * Find the name of the biggest font less than the given limit
877 * MAX_DISPLAY_SIZE used to set up the initial height of the display
878 * widget.
879 */
880
881 {
882 int i;
883 int max;
884 int idx = 0;
885 int size;
886 char buf[128];
887
888 for (i = 0, max = 0; i < data->num; i++)
889 {
890 get_part(fn(data, i), 7, buf);
891 size = atoi(buf);
892 if ((size > max) && (size < MAX_DISPLAY_SIZE))
893 {
894 idx = i;
895 max = size;
896 }
897 }
898 strcpy(big_font, fn(data, idx));
899 }
900 data->old = XLoadQueryFont(XtDisplay(parent), big_font);
901 data->old_list = gui_motif_create_fontlist(data->old);
902
903 // Set the title of the Dialog window.
904 data->dialog = XmCreateDialogShell(parent, "fontSelector", NULL, 0);
905 str = XmStringCreateLocalized(_("Vim - Font Selector"));
906
907 // Create form popup dialog widget.
908 form = XtVaCreateWidget("form",
909 xmFormWidgetClass, data->dialog,
910 XmNdialogTitle, str,
911 XmNautoUnmanage, False,
912 XmNdialogStyle, XmDIALOG_FULL_APPLICATION_MODAL,
913 NULL);
914 XmStringFree(str);
915
916 sub_form = XtVaCreateManagedWidget("subForm",
917 xmFormWidgetClass, form,
918 XmNbottomAttachment, XmATTACH_FORM,
919 XmNbottomOffset, 4,
920 XmNrightAttachment, XmATTACH_FORM,
921 XmNrightOffset, 4,
922 XmNtopAttachment, XmATTACH_FORM,
923 XmNtopOffset, 4,
924 XmNorientation, XmVERTICAL,
925 NULL);
926
927 data->ok = XtVaCreateManagedWidget(_("OK"),
928 xmPushButtonGadgetClass, sub_form,
929 XmNleftAttachment, XmATTACH_FORM,
930 XmNrightAttachment, XmATTACH_FORM,
931 XmNtopAttachment, XmATTACH_FORM,
932 XmNtopOffset, 4,
933 NULL);
934 apply_fontlist(data->ok);
935
936 data->cancel = XtVaCreateManagedWidget(_("Cancel"),
937 xmPushButtonGadgetClass, sub_form,
938 XmNrightAttachment, XmATTACH_FORM,
939 XmNleftAttachment, XmATTACH_FORM,
940 XmNtopAttachment, XmATTACH_WIDGET,
941 XmNtopWidget, data->ok,
942 XmNtopOffset, 4,
943 XmNshowAsDefault, True,
944 NULL);
945 apply_fontlist(data->cancel);
946
947 // Create the separator for beauty.
948 n = 0;
949 XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
950 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
951 XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
952 XtSetArg(args[n], XmNrightAttachment, XmATTACH_WIDGET); n++;
953 XtSetArg(args[n], XmNrightWidget, sub_form); n++;
954 XtSetArg(args[n], XmNrightOffset, 4); n++;
955 separator = XmCreateSeparatorGadget(form, "separator", args, n);
956 XtManageChild(separator);
957
958 // Create font name text widget and the corresponding label.
959 data->name = XtVaCreateManagedWidget("fontName",
960 xmTextWidgetClass, form,
961 XmNbottomAttachment, XmATTACH_FORM,
962 XmNbottomOffset, 4,
963 XmNleftAttachment, XmATTACH_FORM,
964 XmNleftOffset, 4,
965 XmNrightAttachment, XmATTACH_WIDGET,
966 XmNrightWidget, separator,
967 XmNrightOffset, 4,
968 XmNeditable, False,
969 XmNeditMode, XmSINGLE_LINE_EDIT,
970 XmNmaxLength, MAX_FONT_NAME_LEN,
971 XmNcolumns, 60,
972 NULL);
973
974 str = XmStringCreateLocalized(_("Name:"));
975 name = XtVaCreateManagedWidget("fontNameLabel",
976 xmLabelGadgetClass, form,
977 XmNlabelString, str,
978 XmNuserData, data->name,
979 XmNleftAttachment, XmATTACH_OPPOSITE_WIDGET,
980 XmNleftWidget, data->name,
981 XmNbottomAttachment, XmATTACH_WIDGET,
982 XmNbottomWidget, data->name,
983 XmNtopOffset, 1,
984 NULL);
985 XmStringFree(str);
986 apply_fontlist(name);
987
988 // create sample display label widget
989 disp_frame = XtVaCreateManagedWidget("sampleFrame",
990 xmFrameWidgetClass, form,
991 XmNshadowType, XmSHADOW_ETCHED_IN,
992 XmNleftAttachment, XmATTACH_FORM,
993 XmNleftOffset, 4,
994 XmNbottomAttachment, XmATTACH_WIDGET,
995 XmNbottomWidget, name,
996 XmNrightAttachment, XmATTACH_WIDGET,
997 XmNrightWidget, separator,
998 XmNrightOffset, 4,
999 XmNalignment, XmALIGNMENT_BEGINNING,
1000 NULL);
1001
1002 data->sample = XtVaCreateManagedWidget("sampleLabel",
1003 xmLabelWidgetClass, disp_frame,
1004 XmNleftAttachment, XmATTACH_FORM,
1005 XmNtopAttachment, XmATTACH_FORM,
1006 XmNbottomAttachment, XmATTACH_FORM,
1007 XmNrightAttachment, XmATTACH_FORM,
1008 XmNalignment, XmALIGNMENT_BEGINNING,
1009 XmNrecomputeSize, False,
1010 XmNfontList, data->old_list,
1011 NULL);
1012
1013 // create toggle button
1014 str = XmStringCreateLocalized(_("Show size in Points"));
1015 size_toggle = XtVaCreateManagedWidget("sizeToggle",
1016 xmToggleButtonGadgetClass, form,
1017 XmNlabelString, str,
1018 XmNleftAttachment, XmATTACH_FORM,
1019 XmNleftOffset, 4,
1020 XmNbottomAttachment, XmATTACH_WIDGET,
1021 XmNbottomWidget, disp_frame,
1022 XmNbottomOffset, 4,
1023 NULL);
1024 XmStringFree(str);
1025 apply_fontlist(size_toggle);
1026 XtManageChild(size_toggle);
1027
1028 // Encoding pulldown menu.
1029
1030 data->encoding_pulldown = XmCreatePulldownMenu(form,
1031 "encodingPulldown", NULL, 0);
1032 str = XmStringCreateLocalized(_("Encoding:"));
1033 n = 0;
1034 XtSetArg(args[n], XmNsubMenuId, data->encoding_pulldown); ++n;
1035 XtSetArg(args[n], XmNlabelString, str); ++n;
1036 XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); ++n;
1037 XtSetArg(args[n], XmNleftOffset, 4); ++n;
1038 XtSetArg(args[n], XmNbottomAttachment, XmATTACH_WIDGET); ++n;
1039 XtSetArg(args[n], XmNbottomWidget, size_toggle); ++n;
1040 XtSetArg(args[n], XmNbottomOffset, 4); ++n;
1041 XtSetArg(args[n], XmNrightAttachment, XmATTACH_WIDGET); ++n;
1042 XtSetArg(args[n], XmNrightWidget, separator); ++n;
1043 XtSetArg(args[n], XmNrightOffset, 4); ++n;
1044 data->encoding_menu = XmCreateOptionMenu(form, "encodingMenu", args, n);
1045 XmStringFree(str);
1046 XmAddTabGroup(data->encoding_menu);
1047
1048 /*
1049 * Create scroll list widgets in a separate subform used to manage the
1050 * different sizes of the lists.
1051 */
1052
1053 sub_form = XtVaCreateManagedWidget("subForm",
1054 xmFormWidgetClass, form,
1055 XmNbottomAttachment, XmATTACH_WIDGET,
1056 XmNbottomWidget, data->encoding_menu,
1057 XmNbottomOffset, 4,
1058 XmNleftAttachment, XmATTACH_FORM,
1059 XmNleftOffset, 4,
1060 XmNrightAttachment, XmATTACH_WIDGET,
1061 XmNrightWidget, separator,
1062 XmNrightOffset, 4,
1063 XmNtopAttachment, XmATTACH_FORM,
1064 XmNtopOffset, 2,
1065 XmNorientation, XmVERTICAL,
1066 NULL);
1067
1068 // font list
1069 frame = XtVaCreateManagedWidget("frame", xmFrameWidgetClass, sub_form,
1070 XmNshadowThickness, 0,
1071 XmNtopAttachment, XmATTACH_FORM,
1072 XmNbottomAttachment, XmATTACH_FORM,
1073 XmNleftAttachment, XmATTACH_FORM,
1074 XmNrightAttachment, XmATTACH_POSITION,
1075 XmNrightPosition, 50,
1076 NULL);
1077
1078 str = XmStringCreateLocalized(_("Font:"));
1079 name = XtVaCreateManagedWidget("nameListLabel", xmLabelGadgetClass, frame,
1080 XmNchildType, XmFRAME_TITLE_CHILD,
1081 XmNchildVerticalAlignment, XmALIGNMENT_CENTER,
1082 XmNchildHorizontalAlignment, XmALIGNMENT_BEGINNING,
1083 XmNlabelString, str,
1084 NULL);
1085 XmStringFree(str);
1086 apply_fontlist(name);
1087
1088 n = 0;
1089 XtSetArg(args[n], XmNvisibleItemCount, 8); ++n;
1090 XtSetArg(args[n], XmNresizable, True); ++n;
1091 XtSetArg(args[n], XmNlistSizePolicy, XmCONSTANT); ++n;
1092 XtSetArg(args[n], XmNvisualPolicy, XmVARIABLE); ++n;
1093 #ifdef LESSTIF_VERSION
1094 XtSetArg(args[n], XmNscrollBarDisplayPolicy, XmSTATIC); ++n;
1095 #endif
1096 data->list[NAME] = XmCreateScrolledList(frame, "fontList", args, n);
1097 XtVaSetValues(name, XmNuserData, data->list[NAME], NULL);
1098
1099 // style list
1100 frame = XtVaCreateManagedWidget("frame", xmFrameWidgetClass, sub_form,
1101 XmNshadowThickness, 0,
1102 XmNtopAttachment, XmATTACH_FORM,
1103 XmNbottomAttachment, XmATTACH_FORM,
1104 XmNleftAttachment, XmATTACH_POSITION,
1105 XmNleftPosition, 50,
1106 XmNleftOffset, 4,
1107 XmNrightAttachment, XmATTACH_POSITION,
1108 XmNrightPosition, 80,
1109 NULL);
1110
1111 str = XmStringCreateLocalized(_("Style:"));
1112 name = XtVaCreateManagedWidget("styleListLabel", xmLabelWidgetClass, frame,
1113 XmNchildType, XmFRAME_TITLE_CHILD,
1114 XmNchildVerticalAlignment, XmALIGNMENT_CENTER,
1115 XmNchildHorizontalAlignment, XmALIGNMENT_BEGINNING,
1116 XmNlabelString, str,
1117 NULL);
1118 XmStringFree(str);
1119 apply_fontlist(name);
1120
1121 n = 0;
1122 XtSetArg(args[n], XmNvisibleItemCount, 8); ++n;
1123 XtSetArg(args[n], XmNresizable, True); ++n;
1124 XtSetArg(args[n], XmNlistSizePolicy, XmCONSTANT); ++n;
1125 XtSetArg(args[n], XmNvisualPolicy, XmVARIABLE); ++n;
1126 #ifdef LESSTIF_VERSION
1127 XtSetArg(args[n], XmNscrollBarDisplayPolicy, XmSTATIC); ++n;
1128 #endif
1129 data->list[STYLE] = XmCreateScrolledList(frame, "styleList", args, n);
1130 XtVaSetValues(name, XmNuserData, data->list[STYLE], NULL);
1131
1132 // size list
1133 frame = XtVaCreateManagedWidget("frame", xmFrameWidgetClass, sub_form,
1134 XmNshadowThickness, 0,
1135 XmNtopAttachment, XmATTACH_FORM,
1136 XmNbottomAttachment, XmATTACH_FORM,
1137 XmNleftAttachment, XmATTACH_POSITION,
1138 XmNleftPosition, 80,
1139 XmNleftOffset, 4,
1140 XmNrightAttachment, XmATTACH_FORM,
1141 NULL);
1142
1143 str = XmStringCreateLocalized(_("Size:"));
1144 name = XtVaCreateManagedWidget("sizeListLabel", xmLabelGadgetClass, frame,
1145 XmNchildType, XmFRAME_TITLE_CHILD,
1146 XmNchildVerticalAlignment, XmALIGNMENT_CENTER,
1147 XmNchildHorizontalAlignment, XmALIGNMENT_BEGINNING,
1148 XmNlabelString, str,
1149 NULL);
1150 XmStringFree(str);
1151 apply_fontlist(name);
1152
1153 n = 0;
1154 XtSetArg(args[n], XmNvisibleItemCount, 8); ++n;
1155 XtSetArg(args[n], XmNresizable, True); ++n;
1156 XtSetArg(args[n], XmNlistSizePolicy, XmCONSTANT); ++n;
1157 XtSetArg(args[n], XmNvisualPolicy, XmVARIABLE); ++n;
1158 #ifdef LESSTIF_VERSION
1159 XtSetArg(args[n], XmNscrollBarDisplayPolicy, XmSTATIC); ++n;
1160 #endif
1161 data->list[SIZE] = XmCreateScrolledList(frame, "sizeList", args, n);
1162 XtVaSetValues(name, XmNuserData, data->list[SIZE], NULL);
1163
1164 // update form widgets cancel button
1165 XtVaSetValues(form, XmNcancelButton, data->cancel, NULL);
1166
1167 XtAddCallback(size_toggle, XmNvalueChangedCallback,
1168 (XtCallbackProc)stoggle_callback, (XtPointer)data);
1169 XtAddCallback(data->list[NAME], XmNbrowseSelectionCallback,
1170 (XtCallbackProc)name_callback, (XtPointer)data);
1171 XtAddCallback(data->list[STYLE], XmNbrowseSelectionCallback,
1172 (XtCallbackProc)style_callback, (XtPointer)data);
1173 XtAddCallback(data->list[SIZE], XmNbrowseSelectionCallback,
1174 (XtCallbackProc)size_callback, (XtPointer)data);
1175 XtAddCallback(data->ok, XmNactivateCallback,
1176 (XtCallbackProc)ok_callback, (XtPointer)data);
1177 XtAddCallback(data->cancel, XmNactivateCallback,
1178 (XtCallbackProc)cancel_callback, (XtPointer)data);
1179
1180 XmProcessTraversal(data->list[NAME], XmTRAVERSE_CURRENT);
1181
1182 // setup tabgroups
1183
1184 XmAddTabGroup(data->list[NAME]);
1185 XmAddTabGroup(data->list[STYLE]);
1186 XmAddTabGroup(data->list[SIZE]);
1187 XmAddTabGroup(size_toggle);
1188 XmAddTabGroup(data->name);
1189 XmAddTabGroup(data->ok);
1190 XmAddTabGroup(data->cancel);
1191
1192 add_cancel_action(data->dialog, (XtCallbackProc)cancel_callback, data);
1193
1194 // Preset selection data.
1195
1196 data->exit = False;
1197 data->in_pixels= True;
1198 data->sel[ENCODING] = NULL;
1199 data->sel[NAME] = NULL;
1200 data->sel[STYLE] = NULL;
1201 data->sel[SIZE] = NULL;
1202 data->font_name = NULL;
1203
1204 // set up current font parameters
1205 if (current && current[0] != '\0')
1206 {
1207 int i;
1208 char **names;
1209
1210 names = XListFonts(XtDisplay(form), (char *) current, 1, &i);
1211
1212 if (i != 0)
1213 {
1214 char namebuf[TEMP_BUF_SIZE];
1215 char stylebuf[TEMP_BUF_SIZE];
1216 char sizebuf[TEMP_BUF_SIZE];
1217 char encodingbuf[TEMP_BUF_SIZE];
1218 char *found;
1219
1220 found = names[0];
1221
1222 name_part(found, namebuf);
1223 style_part(found, stylebuf);
1224 size_part(found, sizebuf, data->in_pixels);
1225 encoding_part(found, encodingbuf);
1226
1227 if (*namebuf != NUL
1228 && *stylebuf != NUL
1229 && *sizebuf != NUL
1230 && *encodingbuf != NUL)
1231 {
1232 data->sel[NAME] = XtNewString(namebuf);
1233 data->sel[STYLE] = XtNewString(stylebuf);
1234 data->sel[SIZE] = XtNewString(sizebuf);
1235 data->sel[ENCODING] = XtNewString(encodingbuf);
1236 data->font_name = XtNewString(names[0]);
1237 display_sample(data);
1238 XmTextSetString(data->name, data->font_name);
1239 }
1240 else
1241 {
1242 // We can't preset a symbolic name, which isn't a full font
1243 // description. Therefore we just behave the same way as if the
1244 // user didn't have selected anything thus far.
1245 //
1246 // Unfortunately there is no known way to expand an abbreviated
1247 // font name.
1248 data->font_name = NULL;
1249 }
1250 }
1251 XFreeFontNames(names);
1252 }
1253
1254 fill_lists(NONE, data);
1255
1256 // Unfortunately LessTif doesn't align the list widget's properly. I don't
1257 // have currently any idea how to fix this problem.
1258 XtManageChild(data->list[NAME]);
1259 XtManageChild(data->list[STYLE]);
1260 XtManageChild(data->list[SIZE]);
1261 XtManageChild(data->encoding_menu);
1262 manage_centered(form);
1263
1264 // modal event loop
1265 while (!data->exit)
1266 XtAppProcessEvent(XtWidgetToApplicationContext(data->dialog),
1267 (XtInputMask)XtIMAll);
1268
1269 if (data->old)
1270 {
1271 XFreeFont(XtDisplay(data->dialog), data->old);
1272 XmFontListFree(data->old_list);
1273 }
1274 XtDestroyWidget(data->dialog);
1275
1276 gui_motif_synch_fonts();
1277
1278 return (char_u *) data->font_name;
1279 }
1280