2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / gnu / gcj / xlib / natColormap.cc
blob83c897e8a17bffdc3def4d5857e4a3962fe6a2d0
1 /* Copyright (C) 2000 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 // Needed to avoid linking in libstdc++
10 #ifndef __STL_USE_EXCEPTIONS
11 # include <java/lang/OutOfMemoryError.h>
12 # define __THROW_BAD_ALLOC throw new java::lang::OutOfMemoryError()
13 #endif
15 #include <vector>
17 #include <X11/Xlib.h>
19 #include <gcj/cni.h>
20 #include <java/lang/RuntimeException.h>
21 #include <gnu/gcj/xlib/Display.h>
22 #include <gnu/gcj/xlib/Screen.h>
23 #include <gnu/gcj/xlib/Colormap.h>
24 #include <gnu/gcj/xlib/XColor.h>
25 #include <gnu/gcj/RawData.h>
27 jlong gnu::gcj::xlib::Colormap::allocateColorPixel(XColor* color)
29 ::Display* dpy = (::Display*) (screen->getDisplay()->display);
30 ::XColor* col = (::XColor*) (color->structure);
31 Status result = XAllocColor(dpy, xid, col);
32 if (result == 0)
33 throw new java::lang::RuntimeException(
34 JvNewStringLatin1("Unable to allocate color pixel."));
36 return col->pixel;
39 typedef JArray<gnu::gcj::xlib::XColor*>* xcolorarray;
41 xcolorarray gnu::gcj::xlib::Colormap::getSharedColors()
43 ::Display* dpy = (::Display*) (screen->getDisplay()->display);
44 unsigned int nCells = CellsOfScreen(ScreenOfDisplay(dpy, screen->screenNumber));
46 typedef ::XColor xcolor;
47 std::vector<xcolor> colors(nCells);
48 for (unsigned int i=0; i<nCells; i++)
49 colors[i].pixel = i;
50 ::XColor* cols = colors.get_allocator().address(colors.front());
51 XQueryColors(dpy, xid, cols,
52 nCells);
54 int nShared = 0;
55 for (unsigned int i=0; i<nCells; i++)
57 ::XColor color = colors[i];
59 if (!XAllocColor(dpy, xid, &color))
60 continue;
62 /* FIXME: In some cases this algorithm may identify a free
63 color cell as a shared one. */
64 if (color.pixel != i)
66 // Oops, the color wasn't shared. Free it.
67 XFreeColors(dpy, xid, &(color.pixel), 1, 0);
68 colors[i].flags = FLAG_NOT_SHARED;
69 continue;
72 // FIXME: Shared or free?
74 nShared++;
75 colors[i].flags = FLAG_SHARED;
78 JArray<XColor*>* shared = newXColorArray(nShared);
79 int si=0;
80 for (unsigned int i=0; i<nCells; i++)
82 if (colors[i].flags != FLAG_SHARED)
83 continue;
85 XColor* col = elements(shared)[si++];
86 gnu::gcj::RawData* colorData = col->structure;
87 ::XColor* colStruct = reinterpret_cast<xcolor*>(colorData);
88 *colStruct = colors[i];
91 return shared;
94 xcolorarray gnu::gcj::xlib::Colormap::getXColors()
96 ::Display* dpy = (::Display*) (screen->getDisplay()->display);
97 unsigned int nCells =
98 CellsOfScreen(ScreenOfDisplay(dpy, screen->screenNumber));
100 typedef ::XColor xcolor;
101 std::vector<xcolor> colors(nCells);
103 JArray<XColor*>* colArray = newXColorArray(nCells);
105 for (unsigned int i=0; i<nCells; i++)
106 colors[i].pixel = i;
108 XQueryColors(dpy, xid, &(colors.front()), nCells);
110 /* TODO: The current problem with this code is that it relies on
111 (color.pixel == i) as an indicator that the color is
112 shared. However, (color.pixel == i), may also occur simply
113 because color cell i simply was the next free in the list of
114 unallocated color cells. IDEA: run through the list both
115 backwards and forwards, and only pick out the colorcells that
116 have been identified as shared during both passes. Reversing the
117 traversal direction might prevent i from corresponding to the
118 next free colorcell, atleast in one of the passes. */
119 for (unsigned int i=0; i<nCells; i++)
121 ::XColor color = colors[i];
123 char flag = FLAG_NOT_SHARED;
124 if (XAllocColor(dpy, xid, &color))
126 if (color.pixel == i)
128 flag = FLAG_SHARED;
130 else
132 // Oops, the color wasn't shared. Free it.
133 XFreeColors(dpy, xid, &(color.pixel), 1, 0);
137 // Copy color data into object in array
138 XColor* col = elements(colArray)[i];
139 gnu::gcj::RawData* colorData = col->structure;
140 ::XColor* colStruct = reinterpret_cast<xcolor*>(colorData);
141 *colStruct = colors[i];
142 colStruct->flags = flag;
145 return colArray;