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


X11PopupMenu

X11PopupMenu Class

A X11PopupMenu object displays a basic menu at the point where the user has pressed a mouse button in a program window.

At this time the class is still very basic - it doesn’t support menu decorations like titles or separators, nor does it support submenus.

This example program demonstrates how a program might use a basic X11PopupMenu object. It also contains examples of simple helper methods that are called when a user clicks on a menu item.


/* popupmenu.ca - X11PopupMenu Demonstration -*-c-*- */

#include <ctalk/ctalkdefs.h>

/* See the X11FreeTypeFont section of the the Ctalk reference. */
#define FTFONT_BOLD   200
#define FTFONT_MEDIUM 100

X11MessageBoxPane new aboutBox;
X11FileSelectDialog new fileBox;

X11MessageBoxPane instanceMethod configure (void) {
  self resources replaceAt "backgroundColor", "darkblue";
  self resources replaceAt "foregroundColor", "darkblue";
  self resources replaceAt "messageColor", "white";
  self resources replaceAt "messageText",
    "X11PopupMenu Demo\nCtalk Version: 0.0.66";
  self resources replaceAt "iconID", ICON_INFO;

}

/* 
   This is initialized internally when we create the app's
   window. 
*/
extern Atom wm_delete_window;

X11PopupMenu instanceMethod clientExit (X11Pane parentPane) {
  "This is a typical handler to exit an app from a menu entry.
  It simulates a user selecting a close window option on the
  window's title bar. It still needs an independent connection to
  the display server, however."
  XEvent e;
  Display *d_l;
  if ((d_l = XOpenDisplay (getenv ("DISPLAY"))) != NULL) {
    e.type = ClientMessage;
    e.xclient.serial = 0l;
    e.xclient.send_event = true;
    e.xclient.display = d_l;
    e.xclient.window = parentPane xWindowID;
    e.xclient.format = 32;
    e.xclient.data.l[0] = wm_delete_window;
    XSendEvent (d_l, parentPane xWindowID, true, 0l, &e);
    XFlush (d_l);
    XCloseDisplay (d_l);
  }
}

int main (void) {
  X11Pane new mainWin;
  X11PaneDispatcher new dispatcher;
  X11LabelPane new heading, subHeading;
  X11PopupMenu new mainMenu;
  InputEvent new e;
  XEvent ev_ret;
  Display *display;

  mainMenu add "Browse Files...", "fileBox show";
  mainMenu add "About...", "aboutBox show";
  mainMenu add "Exit", "self clientExit parentPane";

  heading canFocus = false;
  heading resources replaceAt "borderWidth", 0;
  subHeading canFocus = false;
  subHeading resources replaceAt "borderWidth", 0;

  heading ftFontVar notifyLevel XFT_NOTIFY_NONE;

  heading ftFontVar initFontLib;

  aboutBox configure;

  mainWin backgroundColor = "blue";
  heading resources replaceAt "backgroundColor", "blue";
  subHeading resources replaceAt "backgroundColor", "blue";
  
  mainWin initialize 255, 200;
  /*
   * NOTE - Make a note in the docs about this, or add it to the
   * parent window's event mask automagically.
   */
  mainWin inputStream eventMask = 
    EXPOSE|ENTERWINDOWNOTIFY \
    |LEAVEWINDOWNOTIFY|BUTTONPRESS \
    |BUTTONRELEASE|KEYPRESS|KEYRELEASE \
    |MOVENOTIFY|WINDELETE \
    |WMFOCUSCHANGENOTIFY;

  dispatcher attachTo mainWin;
  heading attachTo dispatcher, "177x60+c+25";
  subHeading attachTo dispatcher, "190x60+c+75";
  /* The dialogs, which are launched by the menu, still need
     to be attached to the menu's parent window, even if it's
     only to set their dimensions to something similar to 
     the program's dimensions. */
  aboutBox attachTo dispatcher, "300x200";
  fileBox attachTo dispatcher, "250x350";

  mainWin map;
  mainWin raiseWindow;

  mainWin openEventStream;

  mainWin setWMTitle "X11PopupMenu Demo";

  heading multiLine "X11PopupMenu\nDemo";
  heading resources replaceAt "textColor", "lightgray";
  heading resources replaceAt "foregroundColor", "blue";

  subHeading resources replaceAt "ftFont", "DejaVu Sans-8";
  subHeading resources replaceAt foregroundColor, "blue";
  subHeading resources replaceAt textColor, "lightgray";
  subHeading multiLine "Click anywhere\nto display the menu.";

  while (TRUE) {
    mainWin inputStream queueInput;
    if (mainWin inputStream eventPending) {
      e become mainWin inputStream inputQueue unshift;

      mainWin subPaneNotify e;

      switch (e eventClass value)
	{
	case EXPOSE:
	  heading subPaneExpose (heading, e);
	  subHeading subPaneExpose (subHeading, e);
	  break;
	case BUTTONPRESS:
	  mainMenu popup mainWin, e xEventData5, e xEventData6;
	  display = mainMenu displayPtr;
	  while (XCheckTypedEvent (display, ButtonPress, &ev_ret))
	    ;
	  break;
	case WINDELETE:
 	  mainWin deleteAndClose;
	  exit (0);
	  break;
	}
    } else {
      usleep (1000);
    }
  }

}

Constructing Menus

The structure of method items is simple: each item contains the text of the label that is visible to the user, and also the text of the Ctalk expression that the class will execute when the user selects the item. The add method, which is used to add the items, also calculates the entry’s geometry within the menu.

Here is the part of the demo program above that adds items to the menu.


mainMenu add "Browse Files...", "fileBox show";
mainMenu add "About...", "aboutBox show";
mainMenu add "Exit", "self clientExit parentPane";

In addition, the X11PopupMenu class provides these parameters that can be used in the expressions that the menu class calls.


parentPane     # The main window object that the menu is popped
               # up over.
itemName       # A String object that contains the item's visible
               # text.
exprText       # A String that contains the text of the menu item's
               # expression.
itemIndex      # An Integer that contains the ordinal position of
               # the menu item; the topmost item's index is 0, and
               # the index of the bottom item is <number_of_items> - 1.

The labels are actually parameters of the X11PopupMenu : execItem method, and expressions can use them verbatim. There’s more information in the execItem method’s documentation.

Resources

backgroundColor

The name of the color used as the menu’s background.

bottomMargin

The vertical distance between the bottom menu item’s text and the bottom of the menu.

cursorHPad

The number of pixels between the start of a menu item’s text and the left edge of the menu’s selection bar.

font

The Fontconfig descriptor of the font used to display the menu’s text.

gadgetRMargin

The distance in pixels between a gadget to the right of a menu entry’s text, and the right edge of the menu.

gadgetSpace

The distance between the right edge of the longest menu item’s text, and a gadget on the right of the menu.

hlColor

The name of the background color used to highlight menu entries.

hlTextColor

The text color of highlighted items.

hMargins

The distance in pixels between the edge of the menu and the left edge of the text, and between the right edge of the longest menu item’s text and the right edge of the menu.

itemSpace

The vertical distance in pixels between menu items.

textColor

The name of the color used to display non-highlighted text.

topMargin

The distance in pixels between the top of the menu and the first menu item.

Instance Variables

bgPixel
hlPixel
hlTextPixel

The X pixel values of the menu’s background color, and highlighted background and text colors.

gadgetSpace

The distance between the right edge of the longest menu entry’s text and any decorations on the right side of the menu.

itemHeight
textHeight

The height in pixels of a menu item’s text, and the total height of each item.

items

A AssociativeArray that maintains the list of menu entries.

Instance Methods

add (String menuText, String expression)

Add an item to the receiver menu. The menuText argument contains the text that will appear in the menu entry, and the expression argument is the Ctalk expression that the program executes when the user selects the item.

draw (void)

Draw the menu items, the highlight bar, and any decorations on the side of the menu.

execItem (X11Pane parentPane, String itemName, String exprText, Integer itemIndex)

Executes the selected menu item’s expression. The method makes avaiable parameters that contain information about the item. The parameters, which expressions can use verbatim, are listed here.


parentPane     # The main window object that the menu is popped
               # up over.
itemName       # A String object that contains the item's visible
               # text.
exprText       # A String that contains the text of the menu item's
               # expression.
itemIndex      # An Integer that contains the ordinal position of
               # the menu item; the topmost item's index is 0, and
               # the index of the bottom item is <number_of_items> - 1.

new (String objectName)

The X11PopupMenu constructor. Creates a X11PopupMenu object and its instance variables, and initializes the object’s resources and event handlers.

subMenuGadget (Integer baseline Boolean hilight)

Draws submenu indicator to the right of a menu entry.

popup (X11Pane parentPane, Integer displayX, Integer displayY)

Display the menu at the pointer’s position, relative to the upper-right corner of the display.

This method also withdraws the menu when the user selects an item, and whenever the pointer moves outside of the menu or the window defined in parentPane.


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