ULabel.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /***********************************************************
  2. Copyright (c) 1987, 1988, 1994 X Consortium
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  15. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  16. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  17. Except as contained in this notice, the name of the X Consortium shall not be
  18. used in advertising or otherwise to promote the sale, use or other dealings
  19. in this Software without prior written authorization from the X Consortium.
  20. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts.
  21. All Rights Reserved
  22. Permission to use, copy, modify, and distribute this software and its
  23. documentation for any purpose and without fee is hereby granted,
  24. provided that the above copyright notice appear in all copies and that
  25. both that copyright notice and this permission notice appear in
  26. supporting documentation, and that the name of Digital not be
  27. used in advertising or publicity pertaining to distribution of the
  28. software without specific, written prior permission.
  29. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  30. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  31. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  32. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  33. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  34. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  35. SOFTWARE.
  36. ******************************************************************/
  37. /*
  38. * ULabel.c - UCSLabel widget
  39. *
  40. */
  41. #include <X11/IntrinsicP.h>
  42. #include <X11/StringDefs.h>
  43. #include <X11/Xos.h>
  44. #include <X11/Xaw/XawInit.h>
  45. #include "ULabelP.h"
  46. #include <X11/Xmu/Converters.h>
  47. #include <X11/Xmu/Drawing.h>
  48. #include <stdio.h>
  49. #include <ctype.h>
  50. /* needed for abs() */
  51. #include <stdlib.h>
  52. #define streq(a,b) (strcmp( (a), (b) ) == 0)
  53. #define MULTI_LINE_LABEL 32767
  54. #ifdef CRAY
  55. #define WORD64
  56. #endif
  57. /****************************************************************
  58. *
  59. * Full class record constant
  60. *
  61. ****************************************************************/
  62. /* Private Data */
  63. #define offset(field) XtOffsetOf(UCSLabelRec, field)
  64. static XtResource resources[] = {
  65. {XtNforeground, XtCForeground, XtRPixel, sizeof(Pixel),
  66. offset(label.foreground), XtRString, XtDefaultForeground},
  67. {XtNfont, XtCFont, XtRFontStruct, sizeof(XFontStruct *),
  68. offset(label.font),XtRString, XtDefaultFont},
  69. {XtNfontSet, XtCFontSet, XtRFontSet, sizeof(XFontSet ),
  70. offset(label.fontset),XtRString, XtDefaultFontSet},
  71. {XtNlabel, XtCLabel, XtRString, sizeof(String),
  72. offset(label.label), XtRString, NULL},
  73. {XtNencoding, XtCEncoding, XtRUnsignedChar, sizeof(unsigned char),
  74. offset(label.encoding), XtRImmediate, (XtPointer)XawTextEncoding8bit},
  75. {XtNjustify, XtCJustify, XtRJustify, sizeof(XtJustify),
  76. offset(label.justify), XtRImmediate, (XtPointer)XtJustifyCenter},
  77. {XtNinternalWidth, XtCWidth, XtRDimension, sizeof(Dimension),
  78. offset(label.internal_width), XtRImmediate, (XtPointer)4},
  79. {XtNinternalHeight, XtCHeight, XtRDimension, sizeof(Dimension),
  80. offset(label.internal_height), XtRImmediate, (XtPointer)2},
  81. {XtNleftBitmap, XtCLeftBitmap, XtRBitmap, sizeof(Pixmap),
  82. offset(label.left_bitmap), XtRImmediate, (XtPointer) None},
  83. {XtNbitmap, XtCPixmap, XtRBitmap, sizeof(Pixmap),
  84. offset(label.pixmap), XtRImmediate, (XtPointer)None},
  85. {XtNresize, XtCResize, XtRBoolean, sizeof(Boolean),
  86. offset(label.resize), XtRImmediate, (XtPointer)True},
  87. };
  88. #undef offset
  89. static void Initialize(Widget request, Widget new,
  90. ArgList args, Cardinal *num_args);
  91. static void Resize(Widget w);
  92. static void Redisplay(Widget gw, XEvent *event, Region region);
  93. static Boolean SetValues(Widget current, Widget request, Widget new,
  94. ArgList args, Cardinal *num_args);
  95. static void ClassInitialize(void);
  96. static void Destroy(Widget w);
  97. static XtGeometryResult QueryGeometry(Widget w, XtWidgetGeometry *intended,
  98. XtWidgetGeometry *preferred);
  99. UCSLabelClassRec ucsLabelClassRec = {
  100. {
  101. /* core_class fields */
  102. /* superclass */ (WidgetClass) &simpleClassRec,
  103. /* class_name */ "UCSLabel",
  104. /* widget_size */ sizeof(UCSLabelRec),
  105. /* class_initialize */ ClassInitialize,
  106. /* class_part_initialize */ NULL,
  107. /* class_inited */ FALSE,
  108. /* initialize */ Initialize,
  109. /* initialize_hook */ NULL,
  110. /* realize */ XtInheritRealize,
  111. /* actions */ NULL,
  112. /* num_actions */ 0,
  113. /* resources */ resources,
  114. /* num_resources */ XtNumber(resources),
  115. /* xrm_class */ NULLQUARK,
  116. /* compress_motion */ TRUE,
  117. /* compress_exposure */ TRUE,
  118. /* compress_enterleave */ TRUE,
  119. /* visible_interest */ FALSE,
  120. /* destroy */ Destroy,
  121. /* resize */ Resize,
  122. /* expose */ Redisplay,
  123. /* set_values */ SetValues,
  124. /* set_values_hook */ NULL,
  125. /* set_values_almost */ XtInheritSetValuesAlmost,
  126. /* get_values_hook */ NULL,
  127. /* accept_focus */ NULL,
  128. /* version */ XtVersion,
  129. /* callback_private */ NULL,
  130. /* tm_table */ NULL,
  131. /* query_geometry */ QueryGeometry,
  132. /* display_accelerator */ XtInheritDisplayAccelerator,
  133. /* extension */ NULL
  134. },
  135. /* Simple class fields initialization */
  136. {
  137. /* change_sensitive */ XtInheritChangeSensitive
  138. },
  139. /* UCSLabel class fields initialization */
  140. {
  141. /* ignore */ 0
  142. }
  143. };
  144. WidgetClass ucsLabelWidgetClass = (WidgetClass)&ucsLabelClassRec;
  145. /****************************************************************
  146. *
  147. * Private Procedures
  148. *
  149. ****************************************************************/
  150. static void ClassInitialize(void)
  151. {
  152. XawInitializeWidgetSet();
  153. XtAddConverter( XtRString, XtRJustify, XmuCvtStringToJustify,
  154. (XtConvertArgList)NULL, 0 );
  155. }
  156. static XChar2b *buf2b;
  157. static int buf2blen = 0;
  158. #ifndef WORD64
  159. #define TXT16 XChar2b
  160. #else
  161. #define TXT16 char
  162. static int _XawLabelWidth16(XFontStruct *fs, char *str, int n)
  163. {
  164. int i;
  165. XChar2b *ptr;
  166. if (n > buf2blen) {
  167. buf2b = (XChar2b *)XtRealloc((char *)buf2b, n * sizeof(XChar2b));
  168. buf2blen = n;
  169. }
  170. for (ptr = buf2b, i = n; --i >= 0; ptr++) {
  171. ptr->byte1 = *str++;
  172. ptr->byte2 = *str++;
  173. }
  174. return XTextWidth16(fs, buf2b, n);
  175. }
  176. static void _XawLabelDraw16(Display *dpy, Drawable d, GC gc,
  177. int x, int y, char *str, int n)
  178. {
  179. int i;
  180. XChar2b *ptr;
  181. if (n > buf2blen) {
  182. buf2b = (XChar2b *)XtRealloc((char *)buf2b, n * sizeof(XChar2b));
  183. buf2blen = n;
  184. }
  185. for (ptr = buf2b, i = n; --i >= 0; ptr++) {
  186. ptr->byte1 = *str++;
  187. ptr->byte2 = *str++;
  188. }
  189. XDrawString16(dpy, d, gc, x, y, buf2b, n);
  190. }
  191. #define XTextWidth16 _XawLabelWidth16
  192. #define XDrawString16 _XawLabelDraw16
  193. #endif /* WORD64 */
  194. static void _XawLabelDrawUCS(Display *dpy, Drawable d, GC gc,
  195. int x, int y, char *str, int n)
  196. {
  197. char *ep;
  198. unsigned short codepoint;
  199. XChar2b *ptr;
  200. /*
  201. * Convert to UCS2 string on the fly.
  202. */
  203. if (n > buf2blen) {
  204. buf2b = (XChar2b *)XtRealloc((char *)buf2b, n * sizeof(XChar2b));
  205. buf2blen = n;
  206. }
  207. ep = str + n;
  208. for (ptr = buf2b; str < ep; ptr++) {
  209. if((str[0]&0x80)==0) {
  210. codepoint=str[0];
  211. str++;
  212. } else if((str[0]&0x20)==0) {
  213. codepoint=(str[0]&0x1F)<<6 | (str[1]&0x3F);
  214. str+=2;
  215. } else if((str[0]&0x10)==0) {
  216. codepoint=(str[0]&0x0F)<<12 | (str[1]&0x3F)<<6 | (str[2]&0x3F);
  217. str+=3;
  218. } else { /* wrong UTF-8 */
  219. codepoint=(unsigned)'?';
  220. str++;
  221. }
  222. ptr->byte1 = (codepoint >> 8) & 0xff;;
  223. ptr->byte2 = codepoint & 0xff;
  224. }
  225. XDrawString16(dpy, d, gc, x, y, buf2b, ptr - buf2b);
  226. }
  227. static int _XawLabelWidthUCS(
  228. XFontStruct *fs,
  229. char *str,
  230. int n
  231. )
  232. {
  233. char *ep;
  234. unsigned short codepoint;
  235. XChar2b *ptr;
  236. /*
  237. * Convert to UCS2 string on the fly.
  238. */
  239. if (n > buf2blen) {
  240. buf2b = (XChar2b *)XtRealloc((char *)buf2b, n * sizeof(XChar2b));
  241. buf2blen = n;
  242. }
  243. ep = str + n;
  244. for (ptr = buf2b; str < ep; ptr++) {
  245. if((str[0]&0x80)==0) {
  246. codepoint=str[0];
  247. str++;
  248. } else if((str[0]&0x20)==0) {
  249. codepoint=(str[0]&0x1F)<<6 | (str[1]&0x3F);
  250. str+=2;
  251. } else if((str[0]&0x10)==0) {
  252. codepoint=(str[0]&0x0F)<<12 | (str[1]&0x3F)<<6 | (str[2]&0x3F);
  253. str+=3;
  254. } else { /* wrong UTF-8 */
  255. codepoint=(unsigned)'?';
  256. str++;
  257. }
  258. ptr->byte1 = (codepoint >> 8) & 0xff;;
  259. ptr->byte2 = codepoint & 0xff;
  260. }
  261. return XTextWidth16(fs, buf2b, ptr - buf2b);
  262. }
  263. #define XTextWidthUCS _XawLabelWidthUCS
  264. #define XDrawStringUCS _XawLabelDrawUCS
  265. /*
  266. * Calculate width and height of displayed text in pixels
  267. */
  268. static void SetTextWidthAndHeight(UCSLabelWidget lw)
  269. {
  270. XFontStruct *fs = lw->label.font;
  271. char *nl;
  272. if (lw->label.pixmap != None) {
  273. Window root;
  274. int x, y;
  275. unsigned int width, height, bw, depth;
  276. if (XGetGeometry(XtDisplay(lw), lw->label.pixmap, &root, &x, &y,
  277. &width, &height, &bw, &depth)) {
  278. lw->label.label_height = height;
  279. lw->label.label_width = width;
  280. lw->label.label_len = depth;
  281. return;
  282. }
  283. }
  284. if ( lw->simple.international == True ) {
  285. XFontSet fset = lw->label.fontset;
  286. XFontSetExtents *ext = XExtentsOfFontSet(fset);
  287. lw->label.label_height = ext->max_ink_extent.height;
  288. if (lw->label.label == NULL) {
  289. lw->label.label_len = 0;
  290. lw->label.label_width = 0;
  291. }
  292. else if ((nl = index(lw->label.label, '\n')) != NULL) {
  293. char *label;
  294. lw->label.label_len = MULTI_LINE_LABEL;
  295. lw->label.label_width = 0;
  296. for (label = lw->label.label; nl != NULL; nl = index(label, '\n')) {
  297. int width = XmbTextEscapement(fset, label, (int)(nl - label));
  298. if (width > (int)lw->label.label_width)
  299. lw->label.label_width = width;
  300. label = nl + 1;
  301. if (*label)
  302. lw->label.label_height +=
  303. ext->max_ink_extent.height;
  304. }
  305. if (*label) {
  306. int width = XmbTextEscapement(fset, label, strlen(label));
  307. if (width > (int) lw->label.label_width)
  308. lw->label.label_width = width;
  309. }
  310. } else {
  311. lw->label.label_len = strlen(lw->label.label);
  312. lw->label.label_width =
  313. XmbTextEscapement(fset, lw->label.label, (int) lw->label.label_len);
  314. }
  315. } else {
  316. lw->label.label_height = fs->max_bounds.ascent + fs->max_bounds.descent;
  317. if (lw->label.label == NULL) {
  318. lw->label.label_len = 0;
  319. lw->label.label_width = 0;
  320. }
  321. else if ((nl = index(lw->label.label, '\n')) != NULL) {
  322. char *label;
  323. lw->label.label_len = MULTI_LINE_LABEL;
  324. lw->label.label_width = 0;
  325. for (label = lw->label.label; nl != NULL; nl = index(label, '\n')) {
  326. int width;
  327. if (lw->label.encoding == XawTextEncodingChar2b)
  328. width = XTextWidth16(fs, (TXT16*)label, (int)(nl - label)/2);
  329. else if (lw->label.encoding == XawTextEncodingUCS)
  330. width = XTextWidthUCS(fs, label, nl - label);
  331. else
  332. width = XTextWidth(fs, label, (int)(nl - label));
  333. if (width > (int)lw->label.label_width)
  334. lw->label.label_width = width;
  335. label = nl + 1;
  336. if (*label)
  337. lw->label.label_height +=
  338. fs->max_bounds.ascent + fs->max_bounds.descent;
  339. }
  340. if (*label) {
  341. int width;
  342. if (lw->label.encoding == XawTextEncodingChar2b)
  343. width = XTextWidth16(fs, (TXT16*)label, (int)strlen(label)/2);
  344. else if (lw->label.encoding == XawTextEncodingUCS)
  345. width = XTextWidthUCS(fs, label, strlen(label));
  346. else
  347. width = XTextWidth(fs, label, strlen(label));
  348. if (width > (int) lw->label.label_width)
  349. lw->label.label_width = width;
  350. }
  351. } else {
  352. lw->label.label_len = strlen(lw->label.label);
  353. if (lw->label.encoding == XawTextEncodingChar2b)
  354. lw->label.label_width =
  355. XTextWidth16(fs, (TXT16*)lw->label.label,
  356. (int) lw->label.label_len/2);
  357. else if (lw->label.encoding == XawTextEncodingUCS)
  358. lw->label.label_width = XTextWidthUCS(fs, lw->label.label,
  359. lw->label.label_len);
  360. else
  361. lw->label.label_width =
  362. XTextWidth(fs, lw->label.label, (int) lw->label.label_len);
  363. }
  364. }
  365. }
  366. static void GetnormalGC(UCSLabelWidget lw)
  367. {
  368. XGCValues values;
  369. values.foreground = lw->label.foreground;
  370. values.background = lw->core.background_pixel;
  371. values.font = lw->label.font->fid;
  372. values.graphics_exposures = False;
  373. if ( lw->simple.international == True )
  374. /* Since Xmb/wcDrawString eats the font, I must use XtAllocateGC. */
  375. lw->label.normal_GC = XtAllocateGC(
  376. (Widget)lw, 0,
  377. (unsigned) GCForeground | GCBackground | GCGraphicsExposures,
  378. &values, GCFont, 0 );
  379. else
  380. lw->label.normal_GC = XtGetGC(
  381. (Widget)lw,
  382. (unsigned) GCForeground | GCBackground | GCFont | GCGraphicsExposures,
  383. &values);
  384. }
  385. static void GetgrayGC(UCSLabelWidget lw)
  386. {
  387. XGCValues values;
  388. values.foreground = lw->label.foreground;
  389. values.background = lw->core.background_pixel;
  390. values.font = lw->label.font->fid;
  391. values.fill_style = FillTiled;
  392. values.tile = XmuCreateStippledPixmap(XtScreen((Widget)lw),
  393. lw->label.foreground,
  394. lw->core.background_pixel,
  395. lw->core.depth);
  396. values.graphics_exposures = False;
  397. lw->label.stipple = values.tile;
  398. if ( lw->simple.international == True )
  399. /* Since Xmb/wcDrawString eats the font, I must use XtAllocateGC. */
  400. lw->label.gray_GC = XtAllocateGC((Widget)lw, 0,
  401. (unsigned) GCForeground | GCBackground |
  402. GCTile | GCFillStyle |
  403. GCGraphicsExposures,
  404. &values, GCFont, 0);
  405. else
  406. lw->label.gray_GC = XtGetGC((Widget)lw,
  407. (unsigned) GCForeground | GCBackground |
  408. GCFont | GCTile | GCFillStyle |
  409. GCGraphicsExposures,
  410. &values);
  411. }
  412. static void compute_bitmap_offsets(UCSLabelWidget lw)
  413. {
  414. /*
  415. * bitmap will be eventually be displayed at
  416. * (internal_width, internal_height + lbm_y)
  417. */
  418. if (lw->label.lbm_height != 0) {
  419. lw->label.lbm_y = (lw->core.height -
  420. (lw->label.internal_height * 2 +
  421. lw->label.lbm_height)) / 2;
  422. } else {
  423. lw->label.lbm_y = 0;
  424. }
  425. }
  426. static void set_bitmap_info(UCSLabelWidget lw)
  427. {
  428. Window root;
  429. int x, y;
  430. unsigned int bw, depth;
  431. if (!(lw->label.left_bitmap &&
  432. XGetGeometry (XtDisplay(lw), lw->label.left_bitmap, &root, &x, &y,
  433. &lw->label.lbm_width, &lw->label.lbm_height,
  434. &bw, &depth))) {
  435. lw->label.lbm_width = lw->label.lbm_height = 0;
  436. }
  437. compute_bitmap_offsets (lw);
  438. }
  439. /* ARGSUSED */
  440. static void
  441. Initialize(Widget request, Widget new, ArgList args, Cardinal *num_args)
  442. {
  443. UCSLabelWidget lw = (UCSLabelWidget) new;
  444. if (lw->label.label == NULL)
  445. lw->label.label = XtNewString(lw->core.name);
  446. else {
  447. lw->label.label = XtNewString(lw->label.label);
  448. }
  449. GetnormalGC(lw);
  450. GetgrayGC(lw);
  451. SetTextWidthAndHeight(lw);
  452. if (lw->core.height == 0)
  453. lw->core.height = lw->label.label_height +
  454. 2 * lw->label.internal_height;
  455. set_bitmap_info (lw); /* need core.height */
  456. if (lw->core.width == 0) /* need label.lbm_width */
  457. lw->core.width = (lw->label.label_width +
  458. 2 * lw->label.internal_width +
  459. LEFT_OFFSET(lw));
  460. lw->label.label_x = lw->label.label_y = 0;
  461. (*XtClass(new)->core_class.resize) ((Widget)lw);
  462. } /* Initialize */
  463. /*
  464. * Repaint the widget window
  465. */
  466. /* ARGSUSED */
  467. static void Redisplay(Widget gw, XEvent *event, Region region)
  468. {
  469. UCSLabelWidget w = (UCSLabelWidget) gw;
  470. GC gc;
  471. /*
  472. * now we'll see if we need to draw the rest of the label
  473. */
  474. if (region != NULL) {
  475. int x = w->label.label_x;
  476. unsigned int width = w->label.label_width;
  477. if (w->label.lbm_width) {
  478. if (w->label.label_x > (x = w->label.internal_width))
  479. width += w->label.label_x - x;
  480. }
  481. if (XRectInRegion(region, x, w->label.label_y,
  482. width, w->label.label_height) == RectangleOut){
  483. return;
  484. }
  485. }
  486. gc = XtIsSensitive(gw) ? w->label.normal_GC : w->label.gray_GC;
  487. #ifdef notdef
  488. if (region != NULL)
  489. XSetRegion(XtDisplay(gw), gc, region);
  490. #endif /*notdef*/
  491. if (w->label.pixmap == None) {
  492. int len = w->label.label_len;
  493. char *label = w->label.label;
  494. Position y = w->label.label_y + w->label.font->max_bounds.ascent;
  495. Position ksy = w->label.label_y;
  496. /* display left bitmap */
  497. if (w->label.left_bitmap && w->label.lbm_width != 0) {
  498. XCopyPlane (XtDisplay(gw), w->label.left_bitmap, XtWindow(gw), gc,
  499. 0, 0, w->label.lbm_width, w->label.lbm_height,
  500. (int) w->label.internal_width,
  501. (int) w->label.internal_height + w->label.lbm_y,
  502. (unsigned long) 1L);
  503. }
  504. if ( w->simple.international == True ) {
  505. XFontSetExtents *ext = XExtentsOfFontSet(w->label.fontset);
  506. ksy += abs(ext->max_ink_extent.y);
  507. if (len == MULTI_LINE_LABEL) {
  508. char *nl;
  509. while ((nl = index(label, '\n')) != NULL) {
  510. XmbDrawString(XtDisplay(w), XtWindow(w), w->label.fontset, gc,
  511. w->label.label_x, ksy, label, (int)(nl - label));
  512. ksy += ext->max_ink_extent.height;
  513. label = nl + 1;
  514. }
  515. len = strlen(label);
  516. }
  517. if (len)
  518. XmbDrawString(XtDisplay(w), XtWindow(w), w->label.fontset, gc,
  519. w->label.label_x, ksy, label, len);
  520. } else { /*international false, so use R5 routine */
  521. if (len == MULTI_LINE_LABEL) {
  522. char *nl;
  523. while ((nl = index(label, '\n')) != NULL) {
  524. if (w->label.encoding == XawTextEncodingChar2b)
  525. XDrawString16(XtDisplay(gw), XtWindow(gw), gc,
  526. w->label.label_x, y,
  527. (TXT16*)label, (int)(nl - label)/2);
  528. else if (w->label.encoding == XawTextEncodingUCS)
  529. XDrawStringUCS(XtDisplay(gw), XtWindow(gw), gc,
  530. w->label.label_x, y, label, (int)(nl - label));
  531. else
  532. XDrawString(XtDisplay(gw), XtWindow(gw), gc,
  533. w->label.label_x, y, label, (int)(nl - label));
  534. y += w->label.font->max_bounds.ascent +
  535. w->label.font->max_bounds.descent;
  536. label = nl + 1;
  537. }
  538. len = strlen(label);
  539. }
  540. if (len) {
  541. if (w->label.encoding == XawTextEncodingChar2b)
  542. XDrawString16(XtDisplay(gw), XtWindow(gw), gc,
  543. w->label.label_x, y, (TXT16*)label, len/2);
  544. else if (w->label.encoding == XawTextEncodingUCS)
  545. XDrawStringUCS(XtDisplay(gw), XtWindow(gw), gc,
  546. w->label.label_x, y, label, len);
  547. else
  548. XDrawString(XtDisplay(gw), XtWindow(gw), gc,
  549. w->label.label_x, y, label, len);
  550. }
  551. } /*endif international*/
  552. } else if (w->label.label_len == 1) { /* depth */
  553. XCopyPlane(XtDisplay(gw), w->label.pixmap, XtWindow(gw), gc,
  554. 0, 0, w->label.label_width, w->label.label_height,
  555. w->label.label_x, w->label.label_y, 1L);
  556. } else {
  557. XCopyArea(XtDisplay(gw), w->label.pixmap, XtWindow(gw), gc,
  558. 0, 0, w->label.label_width, w->label.label_height,
  559. w->label.label_x, w->label.label_y);
  560. }
  561. #ifdef notdef
  562. if (region != NULL)
  563. XSetClipMask(XtDisplay(gw), gc, (Pixmap)None);
  564. #endif /* notdef */
  565. }
  566. static void _Reposition(UCSLabelWidget lw, Dimension width, Dimension height,
  567. Position *dx, Position *dy)
  568. {
  569. Position newPos;
  570. Position leftedge = lw->label.internal_width + LEFT_OFFSET(lw);
  571. switch (lw->label.justify) {
  572. case XtJustifyLeft :
  573. newPos = leftedge;
  574. break;
  575. case XtJustifyRight :
  576. newPos = width -
  577. (lw->label.label_width + lw->label.internal_width);
  578. break;
  579. case XtJustifyCenter :
  580. default:
  581. newPos = (int)(width - lw->label.label_width) / 2;
  582. break;
  583. }
  584. if (newPos < (Position)leftedge)
  585. newPos = leftedge;
  586. *dx = newPos - lw->label.label_x;
  587. lw->label.label_x = newPos;
  588. *dy = (newPos = (int)(height - lw->label.label_height) / 2)
  589. - lw->label.label_y;
  590. lw->label.label_y = newPos;
  591. return;
  592. }
  593. static void Resize(Widget w)
  594. {
  595. UCSLabelWidget lw = (UCSLabelWidget)w;
  596. Position dx, dy;
  597. _Reposition(lw, w->core.width, w->core.height, &dx, &dy);
  598. compute_bitmap_offsets (lw);
  599. }
  600. /*
  601. * Set specified arguments into widget
  602. */
  603. #define PIXMAP 0
  604. #define WIDTH 1
  605. #define HEIGHT 2
  606. #define NUM_CHECKS 3
  607. static Boolean
  608. SetValues(Widget current, Widget request, Widget new,
  609. ArgList args, Cardinal *num_args)
  610. {
  611. UCSLabelWidget curlw = (UCSLabelWidget) current;
  612. UCSLabelWidget reqlw = (UCSLabelWidget) request;
  613. UCSLabelWidget newlw = (UCSLabelWidget) new;
  614. int i;
  615. Boolean was_resized = False, redisplay = False, checks[NUM_CHECKS];
  616. for (i = 0; i < NUM_CHECKS; i++)
  617. checks[i] = FALSE;
  618. for (i = 0; i < *num_args; i++) {
  619. if (streq(XtNbitmap, args[i].name))
  620. checks[PIXMAP] = TRUE;
  621. if (streq(XtNwidth, args[i].name))
  622. checks[WIDTH] = TRUE;
  623. if (streq(XtNheight, args[i].name))
  624. checks[HEIGHT] = TRUE;
  625. }
  626. if (newlw->label.label == NULL) {
  627. newlw->label.label = newlw->core.name;
  628. }
  629. /*
  630. * resize on bitmap change
  631. */
  632. if (curlw->label.left_bitmap != newlw->label.left_bitmap) {
  633. was_resized = True;
  634. }
  635. if (curlw->label.encoding != newlw->label.encoding)
  636. was_resized = True;
  637. if ( (curlw->label.fontset != newlw->label.fontset) &&
  638. curlw->simple.international ){
  639. was_resized = True;
  640. }
  641. if (curlw->label.label != newlw->label.label) {
  642. if (curlw->label.label != curlw->core.name)
  643. XtFree( (char *)curlw->label.label );
  644. if (newlw->label.label != newlw->core.name) {
  645. newlw->label.label = XtNewString( newlw->label.label );
  646. }
  647. was_resized = True;
  648. }
  649. if (was_resized || (curlw->label.font != newlw->label.font) ||
  650. (curlw->label.justify != newlw->label.justify) || checks[PIXMAP]) {
  651. SetTextWidthAndHeight(newlw);
  652. was_resized = True;
  653. }
  654. /* recalculate the window size if something has changed. */
  655. if (newlw->label.resize && was_resized) {
  656. if ((curlw->core.height == reqlw->core.height) && !checks[HEIGHT])
  657. newlw->core.height = (newlw->label.label_height +
  658. 2 * newlw->label.internal_height);
  659. set_bitmap_info (newlw);
  660. if ((curlw->core.width == reqlw->core.width) && !checks[WIDTH])
  661. newlw->core.width = (newlw->label.label_width +
  662. LEFT_OFFSET(newlw) +
  663. 2 * newlw->label.internal_width);
  664. }
  665. if (curlw->label.foreground != newlw->label.foreground
  666. || curlw->core.background_pixel != newlw->core.background_pixel
  667. || curlw->label.font->fid != newlw->label.font->fid ) {
  668. /* The Fontset is not in the GC - don't make a new GC if FS changes! */
  669. XtReleaseGC(new, curlw->label.normal_GC);
  670. XtReleaseGC(new, curlw->label.gray_GC);
  671. XmuReleaseStippledPixmap( XtScreen(current), curlw->label.stipple );
  672. GetnormalGC(newlw);
  673. GetgrayGC(newlw);
  674. redisplay = True;
  675. }
  676. if ((curlw->label.internal_width != newlw->label.internal_width)
  677. || (curlw->label.internal_height != newlw->label.internal_height)
  678. || was_resized) {
  679. /* Resize() will be called if geometry changes succeed */
  680. Position dx, dy;
  681. _Reposition(newlw, curlw->core.width, curlw->core.height, &dx, &dy);
  682. }
  683. return was_resized || redisplay ||
  684. XtIsSensitive(current) != XtIsSensitive(new);
  685. }
  686. static void Destroy(Widget w)
  687. {
  688. UCSLabelWidget lw = (UCSLabelWidget)w;
  689. if ( lw->label.label != lw->core.name )
  690. XtFree( lw->label.label );
  691. XtReleaseGC( w, lw->label.normal_GC );
  692. XtReleaseGC( w, lw->label.gray_GC);
  693. XmuReleaseStippledPixmap( XtScreen(w), lw->label.stipple );
  694. }
  695. static XtGeometryResult
  696. QueryGeometry(Widget w, XtWidgetGeometry *intended, XtWidgetGeometry *preferred)
  697. {
  698. UCSLabelWidget lw = (UCSLabelWidget)w;
  699. preferred->request_mode = CWWidth | CWHeight;
  700. preferred->width = (lw->label.label_width +
  701. 2 * lw->label.internal_width +
  702. LEFT_OFFSET(lw));
  703. preferred->height = lw->label.label_height +
  704. 2 * lw->label.internal_height;
  705. if ( ((intended->request_mode & (CWWidth | CWHeight))
  706. == (CWWidth | CWHeight)) &&
  707. intended->width == preferred->width &&
  708. intended->height == preferred->height)
  709. return XtGeometryYes;
  710. else if (preferred->width == w->core.width &&
  711. preferred->height == w->core.height)
  712. return XtGeometryNo;
  713. else
  714. return XtGeometryAlmost;
  715. }