-fpermissive to allow GCC to compile old C++
[oweals/cde.git] / cde / programs / dtmail / dtmail / Fonts.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*  $TOG: Fonts.C /main/8 1997/09/04 08:06:26 mgreess $ */
24 /*
25  *  (c) Copyright 1993, 1994 Hewlett-Packard Company
26  *  (c) Copyright 1993, 1994 International Business Machines Corp.
27  *  (c) Copyright 1993, 1994 Novell, Inc.
28  *  (c) Copyright 1993, 1994 Sun Microsystems, Inc.
29  *
30  * This code was borrowed from dtcm (Calendar Manager).
31  * Its purpose is to find a font in a particular family that is
32  * the same size as the user font being used.
33  */
34
35 #include <X11/Xlib.h>
36 #include <Xm/Xm.h>
37 #include <Xm/AtomMgr.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include "Fonts.h"
41
42
43 /*
44  * Walk a font_list looking for a FontSet with the 
45  * XmFONTLIST_DEFAULT_TAG set.  If we fail to find a FontSet with this tag,
46  * return the first FontSet found.  If we fail to find a FontSet return
47  * the first font found.
48  *
49  * This function returns either a XFontStruct or a XFontSet.  The type can
50  * be determined by the value of type_return which will equal either
51  * XmFONT_IS_FONTSET or XmFONT_IS_FONT.
52  *
53  * The XFontStruct or XFontSet that is returned is not a copy and should
54  * not be freed.
55  */
56 static XtPointer
57 get_font(
58     XmFontList   font_list,
59     XmFontType  *type_return)
60 {
61     XmFontContext        fl_context;
62     XmFontListEntry      fl_entry;
63     XtPointer            fl_entry_font = NULL, font_to_use = NULL;
64     char                *fl_entry_font_tag;
65     Boolean              found_font_set = False,
66                          found_font_struct = False,
67                          do_break = False;
68
69     *type_return = XmFONT_IS_FONT;
70
71     if (!XmFontListInitFontContext(&fl_context, font_list))
72                 return (XtPointer)NULL;
73
74     do
75     {
76         fl_entry = XmFontListNextEntry(fl_context);
77         if (fl_entry)
78         {
79             fl_entry_font = XmFontListEntryGetFont(fl_entry, type_return);
80             if (*type_return == XmFONT_IS_FONTSET)
81             {
82                 fl_entry_font_tag = XmFontListEntryGetTag(fl_entry);
83
84                 /* 
85                  * Save the first font set found in-
86                  * case the default tag is not set.
87                  */
88                 if (!found_font_set)
89                 {
90                     font_to_use = fl_entry_font;
91                     found_font_set = True;
92                     found_font_struct = True;
93
94                     if (!strcmp(XmFONTLIST_DEFAULT_TAG, fl_entry_font_tag))
95                       do_break = True;
96                 }
97                 else if (!strcmp(XmFONTLIST_DEFAULT_TAG, fl_entry_font_tag))
98                 {
99                     /* Found right font set */
100                     font_to_use = fl_entry_font;
101                     do_break = True;
102                 }
103
104                 if (fl_entry_font_tag != NULL)
105                   XtFree((char*) fl_entry_font_tag);
106
107                 if (do_break)
108                   break;
109             }
110             else if (!found_font_struct)
111             {
112                 font_to_use = fl_entry_font;
113                 found_font_struct = True;
114             }
115         }
116     } while (fl_entry != NULL);
117
118     XmFontListFreeFontContext(fl_context);
119
120     if (!found_font_set && !found_font_struct)
121       return (XtPointer)NULL;
122
123     return (XtPointer)font_to_use;
124 }
125
126 /*
127  * Strip the font out of a give fontlist.
128  */
129 Boolean
130 fontlist_to_font(
131         XmFontList       font_list,
132         FontType        *new_font)
133 {
134         XmFontType       type_return;
135         XtPointer        font_data;
136
137         if (!font_list) return False;
138
139         font_data = get_font(font_list, &type_return);
140
141         if (!font_data) 
142                 return False;
143
144         new_font->cf_type = type_return;
145
146         if (type_return == XmFONT_IS_FONTSET)
147                 new_font->f.cf_fontset = (XFontSet)font_data;
148         else
149                 new_font->f.cf_font = (XFontStruct *)font_data;
150
151         return True;
152 }
153
154 /*
155  * Determine the pixel size of the user font.  Try to match a symbol
156  * font to that size.  If one can't be found, return NULL, calling
157  * function will probably default to the user font.
158  */
159 void
160 load_app_font(
161         Widget          w, 
162         FontType        *userfont,
163         char            **fontname)
164 {
165         unsigned long    pixel_size;
166         Display         *dpy = XtDisplay(w);
167         char             font_name[128],
168                         *font_name_ptr = font_name,
169                        **font_names;
170         int              nnames;
171         Atom             pixel_atom = XmInternAtom(dpy, "PIXEL_SIZE", FALSE);
172
173         /* First get the pixel size from the User Font */
174         if (userfont->cf_type == XmFONT_IS_FONT) {
175                 /* If we can't get the pixel size from the user font we
176                  * default to a 12 pixel font.
177                  */
178                 if (!XGetFontProperty(userfont->f.cf_font, pixel_atom, 
179                                                         &pixel_size))
180                         pixel_size = 12;
181         } else {
182                 XFontStruct     **font_struct_list;
183                 char            **font_name_list;
184                 int               list_size;
185
186                 if (!(list_size = XFontsOfFontSet(userfont->f.cf_fontset,
187                                             &font_struct_list,
188                                             &font_name_list))) {
189                         pixel_size = 12;
190                 } else {
191                         if (!XGetFontProperty(font_struct_list[0],
192                                               pixel_atom, 
193                                               &pixel_size))
194                                 pixel_size = 12;
195                 }
196         }
197
198         /* See if the font exists */
199         sprintf (font_name,
200                 "-dt-application-medium-r-normal--%ld-*-*-*-*-*-dtsymbol-*",
201                 pixel_size);
202         font_names = XListFonts(dpy, font_name, 1, &nnames);
203
204         if (!nnames) {
205             /* Try again */
206             sprintf (font_name,
207                 "-*-symbol-medium-r-normal--%ld-*-*-*-*-*", pixel_size);
208             font_names = XListFonts(dpy, font_name, 1, &nnames);
209         }
210         if (!nnames) {
211             /* Try again with the default size */
212             pixel_size = 12;
213             sprintf (font_name,
214                 "-dt-application-medium-r-normal--%ld-*-*-*-*-*-dtsymbol-*",
215                 pixel_size);
216             font_names = XListFonts(dpy, font_name, 1, &nnames);
217         }
218         if (!nnames) {
219             *fontname = NULL;
220             return;
221         }
222
223         XFreeFontNames(font_names);
224         *fontname = XtNewString (font_name);
225 }
226