XtCvtrs.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 libraries 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. /* $XConsortium: XtCvtrs.c /main/3 1995/07/14 13:25:38 drk $ */
  24. /*LINTLIBRARY*/
  25. /***********************************************************
  26. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  27. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  28. All Rights Reserved
  29. Permission to use, copy, modify, and distribute this software and its
  30. documentation for any purpose and without fee is hereby granted,
  31. provided that the above copyright notice appear in all copies and that
  32. both that copyright notice and this permission notice appear in
  33. supporting documentation, and that the names of Digital or MIT not be
  34. used in advertising or publicity pertaining to distribution of the
  35. software without specific, written prior permission.
  36. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  37. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  38. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  39. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  40. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  41. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  42. SOFTWARE.
  43. ******************************************************************/
  44. /* Conversion.c - implementations of resource type conversion procs */
  45. #include <X11/Xlib.h>
  46. #include <X11/Intrinsic.h>
  47. #include <X11/IntrinsicP.h>
  48. #include <X11/StringDefs.h>
  49. #include "stdio.h"
  50. #include <X11/keysym.h>
  51. #include <X11/Xlocale.h>
  52. #include "msgs.h"
  53. #include "XtCvtrs.h"
  54. #define done(type, value) \
  55. { \
  56. if (toVal->addr != NULL) { \
  57. if (toVal->size < sizeof(type)) { \
  58. toVal->size = sizeof(type); \
  59. return False; \
  60. } \
  61. *(type*)(toVal->addr) = (value); \
  62. } \
  63. else { \
  64. static type static_val; \
  65. static_val = (value); \
  66. toVal->addr = (XPointer)&static_val; \
  67. } \
  68. toVal->size = sizeof(type); \
  69. return True; \
  70. }
  71. static void FreePixel(
  72. XtAppContext app,
  73. XrmValuePtr toVal,
  74. XtPointer closure,
  75. XrmValuePtr args,
  76. Cardinal *num_args) ;
  77. static XtConvertArgRec xtColorConvertArgs[] = {
  78. {XtWidgetBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.screen),
  79. sizeof(Screen *)},
  80. {XtWidgetBaseOffset, (XtPointer)XtOffsetOf(WidgetRec, core.colormap),
  81. sizeof(Colormap)}
  82. };
  83. Boolean
  84. DtkshCvtStringToPixel(
  85. Display *dpy,
  86. XrmValuePtr args,
  87. Cardinal *num_args,
  88. XrmValuePtr fromVal,
  89. XrmValuePtr toVal,
  90. XtPointer *closure_ret )
  91. {
  92. String str = (String)fromVal->addr;
  93. XColor screenColor;
  94. XColor exactColor;
  95. Screen *screen;
  96. Colormap colormap;
  97. Status status;
  98. char *errmsg;
  99. if (*num_args != 2)
  100. return False;
  101. screen = *((Screen **) args[0].addr);
  102. colormap = *((Colormap *) args[1].addr);
  103. if (DtCompareISOLatin1(str, XtDefaultBackground)) {
  104. *closure_ret = False;
  105. done(Pixel, WhitePixelOfScreen(screen));
  106. }
  107. if (DtCompareISOLatin1(str, XtDefaultForeground)) {
  108. *closure_ret = False;
  109. done(Pixel, BlackPixelOfScreen(screen));
  110. }
  111. status = XAllocNamedColor(DisplayOfScreen(screen), colormap,
  112. (char*)str, &screenColor, &exactColor);
  113. if (status == 0) {
  114. String type;
  115. /* Server returns a specific error code but Xlib discards it. Ugh */
  116. if (XLookupColor(DisplayOfScreen(screen), colormap, (char*)str,
  117. &exactColor, &screenColor)) {
  118. *closure_ret = (char*)True;
  119. done(Pixel, screenColor.pixel);
  120. }
  121. else {
  122. /* One last chance; see if it is a pixel number */
  123. char * p;
  124. Pixel pixelValue;
  125. pixelValue = strtoul(str, &p, 0);
  126. if (p == str)
  127. {
  128. char * errbuf;
  129. errmsg = GETMESSAGE(1, 1,
  130. "DtkshCvtStringToPixel: The color '%s' is not defined");
  131. errbuf = XtMalloc(strlen(errmsg) + strlen(str) + 10);
  132. sprintf(errbuf, errmsg, str);
  133. XtWarning(errbuf);
  134. XtFree(errbuf);
  135. *closure_ret = False;
  136. return False;
  137. }
  138. else
  139. {
  140. *closure_ret = False;
  141. done(Pixel, pixelValue);
  142. }
  143. }
  144. } else {
  145. *closure_ret = (char*)True;
  146. done(Pixel, screenColor.pixel);
  147. }
  148. }
  149. /* ARGSUSED */
  150. static void
  151. FreePixel(
  152. XtAppContext app,
  153. XrmValuePtr toVal,
  154. XtPointer closure,
  155. XrmValuePtr args,
  156. Cardinal *num_args )
  157. {
  158. Screen *screen;
  159. Colormap colormap;
  160. if (*num_args != 2)
  161. return;
  162. screen = *((Screen **) args[0].addr);
  163. colormap = *((Colormap *) args[1].addr);
  164. if (closure) {
  165. XFreeColors( DisplayOfScreen(screen), colormap,
  166. (unsigned long*)toVal->addr, 1, (unsigned long)0
  167. );
  168. }
  169. }
  170. Boolean
  171. DtCompareISOLatin1(
  172. char *first,
  173. char *second )
  174. {
  175. unsigned char *ap, *bp;
  176. for (ap = (unsigned char *) first, bp = (unsigned char *) second;
  177. *ap && *bp; ap++, bp++) {
  178. unsigned char a, b;
  179. if ((a = *ap) != (b = *bp)) {
  180. /* try lowercasing and try again */
  181. if ((a >= XK_A) && (a <= XK_Z))
  182. a += (XK_a - XK_A);
  183. else if ((a >= XK_Agrave) && (a <= XK_Odiaeresis))
  184. a += (XK_agrave - XK_Agrave);
  185. else if ((a >= XK_Ooblique) && (a <= XK_Thorn))
  186. a += (XK_oslash - XK_Ooblique);
  187. if ((b >= XK_A) && (b <= XK_Z))
  188. b += (XK_a - XK_A);
  189. else if ((b >= XK_Agrave) && (b <= XK_Odiaeresis))
  190. b += (XK_agrave - XK_Agrave);
  191. else if ((b >= XK_Ooblique) && (b <= XK_Thorn))
  192. b += (XK_oslash - XK_Ooblique);
  193. if (a != b) break;
  194. }
  195. }
  196. return ((((int) *bp) - ((int) *ap)) == 0);
  197. }
  198. void
  199. RegisterXtOverrideConverters( void )
  200. {
  201. XtSetTypeConverter(XtRString, XtRPixel,
  202. (XtTypeConverter)DtkshCvtStringToPixel,
  203. xtColorConvertArgs, XtNumber(xtColorConvertArgs),
  204. XtCacheNone, FreePixel);
  205. }