dtmail: resolve 'deference before null check' errors related to if(!NULL) checks...
[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                 XtFree((char*) fl_entry_font_tag);
105
106                 if (do_break)
107                   break;
108             }
109             else if (!found_font_struct)
110             {
111                 font_to_use = fl_entry_font;
112                 found_font_struct = True;
113             }
114         }
115     } while (fl_entry != NULL);
116
117     XmFontListFreeFontContext(fl_context);
118
119     if (!found_font_set && !found_font_struct)
120       return (XtPointer)NULL;
121
122     return (XtPointer)font_to_use;
123 }
124
125 /*
126  * Strip the font out of a give fontlist.
127  */
128 Boolean
129 fontlist_to_font(
130         XmFontList       font_list,
131         FontType        *new_font)
132 {
133         XmFontType       type_return;
134         XtPointer        font_data;
135
136         if (!font_list) return False;
137
138         font_data = get_font(font_list, &type_return);
139
140         if (!font_data) 
141                 return False;
142
143         new_font->cf_type = type_return;
144
145         if (type_return == XmFONT_IS_FONTSET)
146                 new_font->f.cf_fontset = (XFontSet)font_data;
147         else
148                 new_font->f.cf_font = (XFontStruct *)font_data;
149
150         return True;
151 }
152
153 /*
154  * Determine the pixel size of the user font.  Try to match a symbol
155  * font to that size.  If one can't be found, return NULL, calling
156  * function will probably default to the user font.
157  */
158 void
159 load_app_font(
160         Widget          w, 
161         FontType        *userfont,
162         char            **fontname)
163 {
164         unsigned long    pixel_size;
165         Display         *dpy = XtDisplay(w);
166         char             font_name[128],
167                         *font_name_ptr = font_name,
168                        **font_names;
169         int              nnames;
170         Atom             pixel_atom = XmInternAtom(dpy, "PIXEL_SIZE", FALSE);
171
172         /* First get the pixel size from the User Font */
173         if (userfont->cf_type == XmFONT_IS_FONT) {
174                 /* If we can't get the pixel size from the user font we
175                  * default to a 12 pixel font.
176                  */
177                 if (!XGetFontProperty(userfont->f.cf_font, pixel_atom, 
178                                                         &pixel_size))
179                         pixel_size = 12;
180         } else {
181                 XFontStruct     **font_struct_list;
182                 char            **font_name_list;
183                 int               list_size;
184
185                 if (!(list_size = XFontsOfFontSet(userfont->f.cf_fontset,
186                                             &font_struct_list,
187                                             &font_name_list))) {
188                         pixel_size = 12;
189                 } else {
190                         if (!XGetFontProperty(font_struct_list[0],
191                                               pixel_atom, 
192                                               &pixel_size))
193                                 pixel_size = 12;
194                 }
195         }
196
197         /* See if the font exists */
198         sprintf (font_name,
199                 "-dt-application-medium-r-normal--%ld-*-*-*-*-*-dtsymbol-*",
200                 pixel_size);
201         font_names = XListFonts(dpy, font_name, 1, &nnames);
202
203         if (!nnames) {
204             /* Try again */
205             sprintf (font_name,
206                 "-*-symbol-medium-r-normal--%ld-*-*-*-*-*", pixel_size);
207             font_names = XListFonts(dpy, font_name, 1, &nnames);
208         }
209         if (!nnames) {
210             /* Try again with the default size */
211             pixel_size = 12;
212             sprintf (font_name,
213                 "-dt-application-medium-r-normal--%ld-*-*-*-*-*-dtsymbol-*",
214                 pixel_size);
215             font_names = XListFonts(dpy, font_name, 1, &nnames);
216         }
217         if (!nnames) {
218             *fontname = NULL;
219             return;
220         }
221
222         XFreeFontNames(font_names);
223         *fontname = XtNewString (font_name);
224 }
225