File: ctrlgc.c

package info (click to toggle)
saoimage 1.19-5
  • links: PTS
  • area: main
  • in suites: slink
  • size: 3,256 kB
  • ctags: 3,610
  • sloc: ansic: 36,050; makefile: 215; sh: 11
file content (293 lines) | stat: -rw-r--r-- 8,693 bytes parent folder | download | duplicates (5)
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
#ifndef lint
static char SccsId[] = "%W%  %G%";
#endif

/* Module:	ctrlgc.c (Control Graphics Context)
 * Purpose:	Create a GC and set the GC params when needed
 * Subroutine:	init_gc()			returns: void
 * Subroutine:	free_gc()			returns: void
 * Subroutine:	set_gc()			returns: GC
 * Subroutine:	set_gc_with_background()	returns: GC
 * Subroutine:	set_text_gc()			returns: GC
 * Subroutine:	set_edit_gc()			returns: GC
 * Subroutine:	get_fontstruct()		returns: XFontStruct *
 * Xlib calls:	XCreateGC(), XSetMask(), XSetFunction(), XSetForeground()
 * Xlib calls:	XChangeGC(), XLoadQueryFont()
 * Copyright:	1989 Smithsonian Astrophysical Observatory
 *		You may do anything you like with this file except remove
 *		this copyright.  The Smithsonian Astrophysical Observatory
 *		makes no representations about the suitability of this
 *		software for any purpose.  It is provided "as is" without
 *		express or implied warranty.
 * Modified:	{0} Michael VanHilst	initial version		  29 May 1989
 *		{n} <who> -- <does what> -- <when>
 */

#include <stdio.h>		/* get stderr, NULL, etc */
#include <X11/Xlib.h>		/* X window stuff */
#include <X11/Xutil.h>		/* X window manager stuff */
#include "hfiles/define.h"	/* define DONT_CARE, U_DONT_CARE */
#include "hfiles/color.h"	/* color and GCspec param structure */
extern struct colorRec color;

static GC main_gc = NULL;
static Display *main_display;
static XGCValues gcvalue;

#define OPTION_COUNT 6
#define NAME_COUNT 17
static char *fontlist[NAME_COUNT] = {
  NULL,				/* user preference 0 */
  NULL,				/* user preference 1 */
  NULL,				/* user preference 2 */
  NULL,						/* 3 */
  "6x10",					/* 4 */
  "6x13",					/* 5 */
  "8x13",					/* 6 */
  "9x15",					/* 7 */
  "*-courier-medium-r-normal-*-10-*",		/* 8 */
  "*-helvetica-medium-r-normal-*-10-*",		/* 9 */
  "*-times-medium-r-normal-*-10-*",		/* 10 */
  "*-courier-medium-r-normal-*-12-*",		/* 11 */
  "*-helvetica-medium-r-normal-*-12-*",		/* 12 */
  "*-times-medium-r-normal-*-12-*",		/* 13 */
  "*-courier-medium-r-normal-*-14-*",		/* 14 */
  "*-helvetica-medium-r-normal-*-14-*",		/* 15 */
  "*-times-medium-r-normal-*-14-*" };		/* 16 */
static XFontStruct *fontstruct[NAME_COUNT] =
  { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, NULL, NULL, NULL, NULL };
/* order to try fonts from list for particular application type */ 
static int preference[3][OPTION_COUNT] = {
  { 0,  5,  9,  10,  4, 8 },	/* small for color graph */
  { 1,  5, 11, 12, 13,  6 },	/* medium for cursor label */
  { 2,  5,  7, 15, 16, 14 } }; 	/* large for popup editor */
static XFontStruct *app_font[3] = { NULL, NULL, NULL };

/*
 * Subroutine:	init_gc()
 * Purpose:	Create the gc and set initial values
 * Xlib calls:	XCreateGC()
 */
void init_gc ( display, window )
     Display *display;
     Window window;
{
  unsigned long valuemask;

  main_display = display;
  gcvalue.function = GXcopy;
  gcvalue.plane_mask = AllPlanes;
  gcvalue.foreground = color.gcset.menu.foreground;
  gcvalue.background = color.gcset.menu.background;
  /* set mask for values which are not same as default */
  valuemask = GCForeground | GCBackground;
  main_gc = XCreateGC(display, window, valuemask, &gcvalue);
}

/*
 * Subroutine:	free_gc
 * Purpose:	free resources loaded by server before exiting
 */
void free_gc ( )
{
  int i;

  for( i=0; i<NAME_COUNT; i++ ) {
    if( fontstruct[i] != NULL )
      XFreeFont (main_display, fontstruct[i]);
  }
  if( main_gc != NULL )
    XFreeGC(main_display, main_gc);
}
  
/*
 * Subroutine:	set_gc()
 * Purpose:	Adjust gc params as specified for simple drawing
 * Xlib calls:	XSetMask(), XSetFunction(), XSetForeground()
 */
GC set_gc ( spec )
     GCspec *spec;
{
  if( spec != NULL ) {
    if( spec->mask != gcvalue.plane_mask ) {
      gcvalue.plane_mask = spec->mask;
      XSetPlaneMask(main_display, main_gc, gcvalue.plane_mask);
    }
    if( spec->func != gcvalue.function ) {
      gcvalue.function = spec->func;
      XSetFunction(main_display, main_gc, gcvalue.function);
    }
    if( (spec->foreground != U_DONT_CARE) &&
       (spec->foreground != gcvalue.foreground) ) {
      gcvalue.foreground = spec->foreground;
      XSetForeground(main_display, main_gc, gcvalue.foreground);
    }
  }
  return( main_gc );
}

/*
 * Subroutine:	set_gc_with_background
 * Purpose:	Adjust gc params as specified for drawing with a background
 * Xlib calls:	XSetMask(), XSetFunction(), XSetForeground(), XSetBackground()
 */
GC set_gc_with_background ( spec, background )
     GCspec *spec;
     unsigned long background;
{
  if( spec->mask != gcvalue.plane_mask ) {
    gcvalue.plane_mask = spec->mask;
    XSetPlaneMask(main_display, main_gc, gcvalue.plane_mask);
  }
  if( spec->func != gcvalue.function ) {
    gcvalue.function = spec->func;
    XSetFunction(main_display, main_gc, gcvalue.function);
  }
  if( (spec->foreground != U_DONT_CARE) &&
      (spec->foreground != gcvalue.foreground) ) {
    gcvalue.foreground = spec->foreground;
    XSetForeground(main_display, main_gc, gcvalue.foreground);
  }
  if( (background != U_DONT_CARE) && (background != gcvalue.background) ) {
    gcvalue.background = background;
    XSetBackground(main_display, main_gc, gcvalue.background);
  }
  return( main_gc );
}

/*
 * Subroutine:	set_text_gc
 * Purpose:	Adjust gc params as specified for basic text
 * Xlib calls:	XChangeGC()
 * Note:	Most gc values are cached, but new font flushes vals to server
 */
GC set_text_gc ( font, foreground, background, func, mask )
     Font font;
     unsigned long foreground;
     unsigned long background;
     int func;
     unsigned long mask;
{
  unsigned long valuemask = 0;

  if( font != gcvalue.font ) {
    gcvalue.font = font;
    valuemask = GCFont;
  }
  if( foreground != gcvalue.foreground ) {
    gcvalue.foreground = foreground;
    valuemask |= GCForeground;
  }
  if( (background != U_DONT_CARE) && (background != gcvalue.background) ) {
    gcvalue.background = background;
    XSetBackground(main_display, main_gc, gcvalue.background);
  }
  if( mask != gcvalue.plane_mask ) {
    gcvalue.plane_mask = mask;
    valuemask |= GCPlaneMask;
  }
  if( func != gcvalue.function ) {
    gcvalue.function = func;
    valuemask |= GCFunction;
  }
  if( valuemask )
    XChangeGC(main_display, main_gc, valuemask, &gcvalue);
  return( main_gc );
}

/*
 * Subroutine:	set_edit_gc
 * Purpose:	Adjust gc params as specified for text with a background
 * Xlib calls:	XChangeGC()
 */
GC set_edit_gc ( font, foreground, background )
     Font font;
     unsigned long foreground;
     unsigned long background;
{
  unsigned long valuemask = 0;

  if( font != gcvalue.font ) {
    gcvalue.font = font;
    valuemask = GCFont;
  }
  if( AllPlanes != gcvalue.plane_mask ) {
    gcvalue.plane_mask = AllPlanes;
    valuemask |= GCPlaneMask;
  }
  if( GXcopy != gcvalue.function ) {
    gcvalue.function = GXcopy;
    valuemask |= GCFunction;
  }
  if( foreground != gcvalue.foreground ) {
    gcvalue.foreground = foreground;
    valuemask |= GCForeground;
  }
  if( background != gcvalue.background ) {
    gcvalue.background = background;
    valuemask |= GCBackground;
  }
  if( valuemask )
    XChangeGC(main_display, main_gc, valuemask, &gcvalue);
  return( main_gc );
}

/*
 * Subroutine:	get_fontstruct
 * Returns:	Pointer to the specified fontstruct
 */
XFontStruct *get_fontstruct ( app_code )
     int app_code;	/* i: see comments above */
{
  static int init_font();

  if( (app_font[app_code] == NULL) && (init_font(app_code) == NULL) )
    return( NULL );
  else
    return( app_font[app_code] );
}

/*
 * Subroutine:	init_font
 * Purpose:	Load the fonts used by this program
 * Returns:	Font on success, else 0
 */
static int init_font ( app_code )
     int app_code;	/* i: font application type index */
{
  int i, name_index;
  static int open_font();

  for( i=0; i<OPTION_COUNT; i++ ) {
    name_index = preference[app_code][i];
    if( (fontstruct[name_index] != NULL) || open_font(name_index) ) {
      app_font[app_code] = fontstruct[name_index];
      return( 1 );
    }
  }
  return( 0 );
}

/*
 * Subroutine:	open_font
 * Purpose:	Load a font or announce failure
 * Returns:	1 on success, else 0
 * Xlib calls:	XLoadQueryFont()
 */
static int open_font ( index )
     int index;		/* i: index of font name in font_name array */
{
  char *name;

  if( (name = fontlist[index]) != NULL ) {
    if( (fontstruct[index] = XLoadQueryFont(main_display, name)) != NULL ) {
      return( 1 );
    } else {
      /* report failure and scratch name off of list */
      (void)fprintf(stderr, "Could not open font: %s\n", name);
      fontlist[index] = NULL;
    }
  }
  return( 0 );
}