Next: , Previous: , Up: Classes   [Index]


X11CheckBoxPane

X11CheckBoxPane Class

A single X11CheckBoxPane object draws a checkable box on the main window. The class has methods for initialization, drawing, changing the box’s clicked/unclicked state, and the instance data for retrieving the state.

Here is an example program that displays a X11CheckBoxPane object.

/*
 *  X11CheckBoxPane demo.
 */	

/* 
 *  To display bevels, uncomment the following line. 
 */
/* #define BEVEL */

/* 
 * To draw a checkbox with rounded corners, uncomment the following
 * lines and set FRAME_RADIUS and FILL_RADIUS to non-zero values.  The
 * checkboxes use different radius' for the frame and the filled
 * interior of a checked checkbox because most radius are a
 * significant percent of the boxes' sides, which can make the frame
 * look like a rectangle with rounded corners, and also make the fill
 * rectangle look like an oval, if the shapes use the same radius.
 */
/* #define RADIUS */
/* #define FRAME_RADIUS 0 */
/* #define FILL_RADIUS 0 */

/* 
 * To change the width, height, and internal margin of the
 * check box, edit these #defines.  The measurements are in
 * pixels.
 */
#define CHECKBOX_WIDTH   15
#define CHECKBOX_HEIGHT  15
#define CHECKBOX_MARGIN  1
#define CHECKBOX_BORDER  1

#include <ctalk/ctalkdefs.h>

int main (int argv, char **argc) @{
  X11Pane new xPane;
  X11PaneDispatcher new xTopLevelPane;
  X11CheckBoxPane new xCheckBoxPane;
  X11LabelPane new label;
  X11CanvasPane new canvas;
  InputEvent new e;
  Exception new ex;
  Pen new arrowPen;

  xPane ftFontVar initFontLib;

  xPane initialize 0, 0, 300, 300, "Checkbox Demo";
  xPane inputStream eventMask = BUTTONPRESS|BUTTONRELEASE|WINDELETE|EXPOSE;
  xTopLevelPane attachTo xPane;

  /* Note that the geometry string in the, "attachTo," call below
     contains only the X,Y origin of the check box.  In this case, the
     size of the check box is taken from the object's resources, which
     we can adjust any time before, "attachTo," sizes the actual
     drawing surfaces. */
  xCheckBoxPane resources replaceAt "width", CHECKBOX_WIDTH;
  xCheckBoxPane resources replaceAt "height", CHECKBOX_HEIGHT;
  xCheckBoxPane resources replaceAt "margin", CHECKBOX_MARGIN;
  xCheckBoxPane resources replaceAt "borderWidth", CHECKBOX_BORDER;
#ifdef RADIUS
  xCheckBoxPane resources replaceAt "frameRadius", FRAME_RADIUS;
  xCheckBoxPane resources replaceAt "fillRadius", FILL_RADIUS;
#endif  

  label ftFontVar selectFontFromFontConfig "URW Gothic L-12";
  label ftFontVar saveSelectedFont;
  label justify = LABEL_LEFT;
  label text "Please Click";
  label resources replaceAt "borderWidth", 0;
  
  canvas attachTo xTopLevelPane, "280x280+10+10";
  xCheckBoxPane attachTo xTopLevelPane, "+20%+40%";
  label attachTo xTopLevelPane, "160x40+35%+50";

#ifdef BEVEL
  xCheckBoxPane resources replaceAt "bevel", true;
#endif

  xPane map;
  xPane raiseWindow;

  xPane openEventStream;

  xPane setWMTitle "Checkbox Demo";
  canvas background "white";
  label draw;
  xCheckBoxPane draw;
  arrowPen width = 1;
  arrowPen colorName = "black";
  
  while (TRUE) @{
    xPane inputStream queueInput;
    if (xPane inputStream eventPending) @{
      e become xPane inputStream inputQueue unshift;
      xPane subPaneNotify e;  /* Call the classes' event handlers. */
      if (ex pending)
	ex handle;
      
      switch (e eventClass value)
     	@{
	case BUTTONPRESS:
	  if (e eventData == xCheckBoxPane xWindowID) @{
	    if (xCheckBoxPane clicked) @{
	      printf ("clicked\n");
	    @} else @{
	      printf ("unclicked\n");
	    @}
	  @}
	  break;
	case EXPOSE:
	  label draw;
	  canvas paneBuffer drawRectangle 10, 10, 260, 260, false,
	    1, "black", 0;
	  canvas paneBuffer drawLine 110, 45, 70, 100, arrowPen;
	  canvas paneBuffer drawLine 70, 100, 70, 90, arrowPen;
	  canvas paneBuffer drawLine 70, 100, 81, 97, arrowPen;
	  canvas refresh;
	  break;
	case WINDELETE:
      	  xPane deleteAndClose;
     	  exit (0);
     	  break;
     	default:
     	  break;
     	@}
    @}
  @}
@}

Retrieving a X11CheckBox Object’s State

The X11CheckBoxPane class declares the checked instance variable, which is either true or false depending on when and how many times a user has clicked on the checkbox. Successive clicks change the checked instance variable’s state from false to true and back again. The draw method draws the checkbox filled or empty depending on the state of the checked instance variable.

Here is the portion of the program above that retrieves the checkbox’s state. The check box object is named xCheckBoxPane. The condition, ‘if (e eventData == xCheckBoxPane xWindowID)’ insures that the program uses only pointer clicks that fall within xCheckBoxPane's boundaries.


if (e eventData == xCheckBoxPane xWindowID) {
  if (xCheckBoxPane clicked) {
    printf ("clicked\n");
  } else {
    printf ("unclicked\n");
  }
}

Resources

backgroundColor

A String that contains the color name of the checkbox’s background. The default is ‘white’.

bevel

A Boolean that determines whether to draw beveled edges on the checkbox’s interior. The default is ‘false’.

borderWidth

An Integer that specifies the width in pixels of the checkbox’s border. The default is 1px.

clickColor

A String that contains the color name of the checkbox’s border and interior when checked. The default is ‘darkslategray’.

fillRadius
frameRadius

These are Integer objects which, if nonzero, cause the checkbox to be drawn with rounded corners. There is a separate dimension for the margin and the checkbox interior because the radius comprises a significant amount of a checkbox’s size, so the margin might appear like a rectangle with rounded corners, while the interior might look like a circle or oval, if the shapes use the same corner radius.

darkShadowColor
lightShadowColor

The names of the colors of the light and dark shadows that the checkbox displays inside its border if the ‘bevel’ resource is true.

height
width

These are Integer values that contain the width and height of the checkbox, in pixels. Their default value, ‘15px’, is set when the checkbox object is created, and may be changed before the checkbox is attached to its parent pane. If the checkbox’s attachTo method is given a geometry that also contains a width and height, then this latest dimension setting determines the checkbox’s size when displayed.

margin

The distance in pixels between the inner edge of the border and the edge of the check in a filled checkbox. The default is 1px.

Instance Variables

clicked

A Boolean that changes between true and false every time the check box is clicked. The variable’s value also determines whether the checkbox’s interior is filled.

Instance Methods

attachTo (Object parentPane, String geometry)

Attaches the receiver X11CheckBoxPane to its parentPane using the dimensions given by geometry. The method also creates the drawing surfaces with the correct dimensions.

The geometry argument may optionally omit the checkbox’s width and height, which causes the method to use the width and height given by the checkbox object’s resources. For example, if the program contains an expression like the following:


xCheckBoxPane attachTo xTopLevelPane, "+25%+25%";

Then the method will create the checkbox with the dimensions preset by the object’s ‘width’ and ‘height’ resources. If the checkbox’s dimensions are determined in this manner, a program my change the checkbox’s size any time before the attachTo method is called. However, if the program contains a statement like the following,


xCheckBoxPane attachTo xTopLevelPane, "20x20+25%+25%";

Then the program uses the ‘20x20’ size given as the argument, which is the most recent dimension that the program has defined..

draw (void)

This method draws the checkbox. It uses the clicked instance variable state to determine whether to draw a filled checkbox, as well as the classes’ resources that have been set in the object to determine check checkbox’s style; i.e., the checkbox’s size, whether its edges are beveled or rounded, and the checkbox’s color.

new (String paneName)

The X11CheckBoxPane constructor. Creates a new X11CheckBox with the name given by paneName, and makes it available to the method that contains the statement, or to the entire program if the new X11CheckBoxPane is declared in a global scope. The method also initializes the event handlers and resources that are defined in the class.

onClick (Object subPane, InputEvent event)

This method receives a ‘BUTTONPRESS’ event from the display system, and toggles the receiver checkbox’s clicked instance variable between true and false. The method changes the clicked state only in response to ‘BUTTONPRESS’ events; a corresponding ‘BUTTONRELEASE’ event is ignored.


Next: , Previous: , Up: Classes   [Index]