1/* -*- mode:ObjC -*-
2   XGContext - Drawing context using the Xlib Library.
3
4   Copyright (C) 1998,1999,2002 Free Software Foundation, Inc.
5
6   Written by:  Adam Fedor <fedor@boulder.colorado.edu>
7   Date: Nov 1998
8
9   This file is part of the GNU Objective C User Interface Library.
10
11   This library is free software; you can redistribute it and/or
12   modify it under the terms of the GNU Lesser General Public
13   License as published by the Free Software Foundation; either
14   version 2 of the License, or (at your option) any later version.
15
16   This library is distributed in the hope that it will be useful,
17   but WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
19   Lesser General Public License for more details.
20
21   You should have received a copy of the GNU Lesser General Public
22   License along with this library; see the file COPYING.LIB.
23   If not, see <http://www.gnu.org/licenses/> or write to the
24   Free Software Foundation, 51 Franklin Street, Fifth Floor,
25   Boston, MA 02110-1301, USA.
26*/
27
28#include "config.h"
29#include <AppKit/AppKitExceptions.h>
30#include <AppKit/NSAffineTransform.h>
31#include <AppKit/NSBitmapImageRep.h>
32#include <AppKit/NSGraphics.h>
33#include <AppKit/NSColor.h>
34#include <AppKit/NSView.h>
35#include <AppKit/NSWindow.h>
36#include <Foundation/NSException.h>
37#include <Foundation/NSArray.h>
38#include <Foundation/NSDictionary.h>
39#include <Foundation/NSData.h>
40#include <Foundation/NSValue.h>
41#include <Foundation/NSString.h>
42#include <Foundation/NSUserDefaults.h>
43#include <Foundation/NSDebug.h>
44
45#include "x11/XGServer.h"
46#include "xlib/XGContext.h"
47#include "xlib/XGPrivate.h"
48#include "xlib/XGGState.h"
49
50#ifdef HAVE_XFT
51#include "xlib/GSXftFontInfo.h"
52#endif
53
54#include "xlib/XGFontSetFontInfo.h"
55
56#include <X11/Xlib.h>
57#include <X11/Xutil.h>
58#include <X11/keysym.h>
59
60/**
61   <unit>
62   <heading>XGContext</heading>
63   <p>
64   The documentation below mostly describes methods that are specific to
65   this backend and wouldn't necessarily be used in other backends. The methods
66   that this class does implement that would need to be in every backend are
67   the methods of its NSGraphicContext superclass. See the documentation
68   for NSGraphicContext for more information.
69   </p>
70   </unit>
71*/
72@implementation XGContext
73
74/* Initialize AppKit backend */
75+ (void)initializeBackend
76{
77  Class fontClass = Nil;
78  Class fontEnumerator = Nil;
79  BOOL  enableFontSet;
80  NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
81
82  NSDebugLog(@"Initializing GNUstep xlib backend.\n");
83
84  [NSGraphicsContext setDefaultContextClass: [XGContext class]];
85
86#ifdef HAVE_XFT
87  if (([ud objectForKey: @"GSFontAntiAlias"] == nil) ||
88      ([ud boolForKey: @"GSFontAntiAlias"]))
89    {
90      fontClass = [GSXftFontInfo class];
91      fontEnumerator = [GSXftFontEnumerator class];
92    }
93#endif
94  enableFontSet = [ud boolForKey: @"GSXEnableFontSet"];
95  if (fontClass == Nil)
96    {
97      if (enableFontSet == NO)
98	{
99	  fontClass = [XGFontInfo class];
100	}
101      else
102	{
103#ifdef X_HAVE_UTF8_STRING
104	  fontClass = [XGFontSetFontInfo class];
105#else
106	  NSLog(@"Can't use GSXEnableFontSet: You need XFree86 >= 4.0.2");
107	  fontClass = [XGFontInfo class];
108#endif
109	}
110    }
111  [GSFontInfo setDefaultClass: fontClass];
112
113  if (fontEnumerator == Nil)
114    {
115      if (enableFontSet == NO)
116	{
117	  fontEnumerator = [XGFontEnumerator class];
118	}
119      else
120	{
121#ifdef X_HAVE_UTF8_STRING
122	  // Commented out till the implementation of XGFontSetEnumerator
123	  // completes.
124	  //fontEnumerator = [XGFontSetEnumerator class];
125	  fontEnumerator = [XGFontEnumerator class];
126#else
127	  fontEnumerator = [XGFontEnumerator class];
128#endif
129	}
130    }
131  [GSFontEnumerator setDefaultClass: fontEnumerator];
132}
133
134+ (Class) GStateClass
135{
136  return [XGGState class];
137}
138
139- (void) flushGraphics
140{
141  XFlush([(XGServer *)server xDisplay]);
142}
143
144// Try to match the restrictions in XGBitmap
145- (BOOL) isCompatibleBitmap: (NSBitmapImageRep*)bitmap
146{
147  NSString *colorSpaceName;
148  int numColors;
149
150  if ([bitmap bitmapFormat] != 0)
151    {
152      return NO;
153    }
154
155  if ([bitmap bitsPerSample] > 8)
156    {
157      return NO;
158    }
159
160  numColors = [bitmap samplesPerPixel] - ([bitmap hasAlpha] ? 1 : 0);
161  colorSpaceName = [bitmap colorSpaceName];
162  if ([colorSpaceName isEqualToString: NSDeviceRGBColorSpace] ||
163      [colorSpaceName isEqualToString: NSCalibratedRGBColorSpace])
164    {
165      return (numColors == 3);
166    }
167  else if ([colorSpaceName isEqualToString: NSDeviceCMYKColorSpace])
168    {
169      return (numColors == 4);
170    }
171  else if ([colorSpaceName isEqualToString: NSDeviceWhiteColorSpace] ||
172           [colorSpaceName isEqualToString: NSCalibratedWhiteColorSpace])
173    {
174      return (numColors == 1);
175    }
176  else if ([colorSpaceName isEqualToString: NSDeviceBlackColorSpace] ||
177           [colorSpaceName isEqualToString: NSCalibratedBlackColorSpace])
178    {
179      return (numColors == 1);
180    }
181  else
182    {
183      return NO;
184    }
185}
186
187@end
188
189@implementation XGContext (Ops)
190
191/* ----------------------------------------------------------------------- */
192/* Window system ops */
193/* ----------------------------------------------------------------------- */
194- (void) GSCurrentDevice: (void **)device : (int *)x : (int *)y
195{
196  void *windevice = [(XGGState *)gstate windevice];
197  if (device)
198    *device =  windevice;
199  if (x && y)
200    {
201      NSPoint offset = [gstate offset];
202      *x = offset.x;
203      *y = offset.y;
204    }
205}
206
207- (void) GSSetDevice: (void *)device : (int)x : (int)y
208{
209  [(XGGState *)gstate setWindowDevice: device];
210  [gstate setOffset: NSMakePoint(x, y)];
211}
212
213@end
214