home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / gwm18a.zip / wl_label.c < prev    next >
C/C++ Source or Header  |  1995-07-03  |  4KB  |  160 lines

  1. /* Copyright 1989 GROUPE BULL -- See license conditions in file COPYRIGHT
  2.  * Copyright 1989 Massachusetts Institute of Technology
  3.  */
  4. /***********************\
  5. *                 *
  6. *  WOOL_OBJECT: Label  *
  7. *  BODY                *
  8. *                 *
  9. \***********************/
  10.  
  11. #include "EXTERN.h"
  12. #include <stdio.h>
  13. #include "wool.h"
  14. #include "wl_string.h"
  15. #include "wl_atom.h"
  16. #include "wl_number.h"
  17. #include "gwm.h"
  18. #include <X11/Xatom.h>
  19. #include <X11/Xutil.h>
  20. #include "INTERN.h"
  21. #include "wl_label.h"
  22.  
  23.  
  24.  
  25. WOOL_OBJECT
  26. wool_label_make(argc, argv)
  27. int        argc;
  28. WOOL_Number    argv[];
  29. {
  30.     WOOL_Label      object;
  31.  
  32.     if (argc == 0 || argc > 2)
  33.     return wool_error(BAD_NUMBER_OF_ARGS, argc);
  34.     must_be_string(argv[0], 0);
  35.     object = (WOOL_Label) Malloc(sizeof(struct _WOOL_Label));
  36.     zrt_put(object);
  37.     object -> type = WLLabel;
  38.     object -> color = Context -> pixel.Fore;
  39.     increase_reference(object -> label = (WOOL_String) argv[0]);
  40.     if (argc == 2) {
  41.     must_be_number(argv[1], 1);
  42.     object -> font = argv[1] -> number;
  43.     } else
  44.     object -> font = DefaultFont;
  45.     return (WOOL_OBJECT) object;
  46. }
  47.  
  48. /*
  49.  * WLLabel_print:
  50.  * a label prints as [fontnum]"string"
  51.  */
  52.  
  53. WOOL_OBJECT 
  54. WLLabel_print(obj)
  55. WOOL_Label     obj;
  56. {
  57.     wool_printf("{LABEL %ld,", obj -> font);
  58.     wool_printf(" %s}", obj -> label);
  59.     return (WOOL_OBJECT) obj;
  60. }
  61.  
  62. /*
  63.  * WLLabel_free:
  64.  * The structure is just freed, and string released.
  65.  */
  66.  
  67. WOOL_OBJECT 
  68. WLLabel_free(obj)
  69. WOOL_Label     obj;
  70. {
  71.     decrease_reference(obj -> label);
  72.     Free(obj);
  73.     return NULL;
  74. }
  75.  
  76. /*
  77.  * handles its label as an C string.
  78.  */
  79.  
  80. char *
  81. WLLabel_get_C_value(obj)
  82. WOOL_Label obj;
  83. {
  84.     return (char *) obj->label->string;
  85. }
  86.  
  87. /*
  88.  * returns the dimensions of the strings in the font + margins
  89.  * x,y is the start of the string (baseline) in the box.
  90.  */
  91.  
  92. WOOL_OBJECT
  93. WLLabel_get_dimensions(label, box)
  94. WOOL_Label label;
  95. Box    box;                 /* RETURN the dimensions */
  96. {
  97.     int             up, down, dir;
  98.     XCharStruct     extent;
  99.  
  100.     XQueryTextExtents(dpy, label -> font,
  101.          label -> label -> string, strlen(label -> label -> string),
  102.               &dir, &up, &down, &extent);
  103.     label -> x = DefaultLabelHorizontalMargin - extent.lbearing;
  104.     label -> y = DefaultLabelVerticalMargin + up;
  105.     box -> x = box -> y = 0;
  106.     box -> width = Max(1, extent.width + 2 * DefaultLabelHorizontalMargin);
  107.     box -> height = Max(1, up + down + 2 * DefaultLabelVerticalMargin);
  108.     return NULL;
  109. }
  110.  
  111. /*
  112.  * drawing a string: we don't need to refresh the background since
  113.  * the font occupies all the space
  114.  */
  115.  
  116. WOOL_OBJECT
  117. WLLabel_draw(label, wob)
  118. WOOL_Label    label;
  119. Wob        wob;
  120. {
  121.     XSetFont(dpy, Context->gc.Work, label -> font);
  122.     XSetForeground(dpy, Context->gc.Work, label -> color);
  123.     XClearWindow(dpy, wob -> hook);
  124.     XDrawString(dpy, wob -> hook, Context->gc.Work,
  125.         label -> x, label -> y, label -> label -> string,
  126.         strlen(label -> label -> string));
  127.     return NULL;
  128. }
  129.  
  130. WOOL_OBJECT
  131. WLLabel_open(label, wob)
  132. WOOL_Label label;
  133. Wob wob;
  134. {
  135.     XSetWindowBackground(dpy, wob -> hook, wob ->box.background);
  136.     XClearWindow(dpy,  wob -> hook);
  137.     return (WOOL_OBJECT) ExposureMask;
  138. }
  139.  
  140. /* since this module has the "know-how" (and the good includes), we define
  141.  * this method for strings here
  142.  */
  143.  
  144. WOOL_OBJECT
  145. WLString_get_dimensions(string, box)
  146. WOOL_String string;
  147. Box     box;                    /* RETURN the dimensions */
  148. {
  149.     int             up, down, dir;
  150.     XCharStruct     extent;
  151.  
  152.     XQueryTextExtents(dpy, DefaultFont,
  153.               string -> string, strlen(string -> string),
  154.               &dir, &up, &down, &extent);
  155.     box -> x = box -> y = 0;
  156.     box -> width = Max(1, extent.width + 2 * DefaultLabelHorizontalMargin);
  157.     box -> height = Max(1, up + down + 2 * DefaultLabelVerticalMargin);
  158.     return NULL;
  159. }
  160.