Skip to content

Instantly share code, notes, and snippets.

@unix-junkie
Created October 20, 2015 09:39
Show Gist options
  • Save unix-junkie/6d19825780d814ac788c to your computer and use it in GitHub Desktop.
Save unix-junkie/6d19825780d814ac788c to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <Xm/Xm.h>
#include <Xm/MwmUtil.h>
#include <Xm/AtomMgr.h>
#include <Xm/PushB.h>
#include <Xm/Protocols.h>
Widget topLevel;
void customMenuItemAActivated(const Widget w, const XtPointer data, const XtPointer cbs) {
printf("Custom menu item A activated\n");
}
void customMenuItemBActivated(const Widget w, const XtPointer data, const XtPointer cbs) {
printf("Custom menu item B activated\n");
}
void buttonActivated(const Widget w, const XtPointer data, const XtPointer cbs) {
Display *display = XtDisplay(topLevel);
const Atom xaMwmMenu = XmInternAtom(display, _XA_MWM_MENU, False);
Atom actualType;
int actualFormat;
unsigned long nItems;
unsigned long bytesAfter;
unsigned char *property;
if (XGetWindowProperty(display, XtWindow(topLevel),
xaMwmMenu, 0L, 64L, False, AnyPropertyType,
&actualType, &actualFormat, &nItems, &bytesAfter,
&property) == Success) {
printf("XA_MWM_MENU = %ld; actualType = %ld; actualFormat = %d; nItems = %ld; bytesAfter = %ld; property = \"%s\"\n",
xaMwmMenu, actualType, actualFormat, nItems, bytesAfter, property);
} else {
printf("XA_MWM_MENU property not found\n");
}
}
int main(int argc, char *argv[]) {
XtAppContext appContext;
XtSetLanguageProc(NULL, NULL, NULL);
const char *applicationClass = "XmWindowMenuTest";
topLevel = XtVaAppInitialize(&appContext, applicationClass, NULL, 0, &argc, argv, NULL,
XmNtitle, "Window Menu Test, " XmVERSION_STRING,
XmNiconName, "Window Menu Test, " XmVERSION_STRING " (Icon)",
NULL);
const XmString buttonText = XmStringCreateLocalized("Print menu details to standard output");
const Widget button = XtVaCreateManagedWidget("Button", xmPushButtonWidgetClass, topLevel,
XmNlabelString, buttonText,
NULL);
XtAddCallback(button, XmNactivateCallback, buttonActivated, NULL);
Display *display = XtDisplay(topLevel);
Atom xaMwmMessages = XmInternAtom(display, _XA_MWM_MESSAGES, False);
XmAddWMProtocols(topLevel, &xaMwmMessages, 1);
const char *customMenu =
"no-label f.separator\n" \
"Custom\\ Menu\\ Title f.title\n" \
"XTerm f.exec \"xterm &\"\n" \
"Custom\\ Menu\\ Action\\ A Shift Alt<Key>F1 f.send_msg %d\n" \
"Custom\\ Menu\\ Action\\ B Shift Alt<Key>F2 f.send_msg %d\n";
char *customMenuAtomName0 = "CUSTOM_MENU_ACTION_A";
const Atom customMenuAtom0 = XmInternAtom(display, customMenuAtomName0, False);
XmAddProtocolCallback(topLevel, xaMwmMessages, customMenuAtom0, customMenuItemAActivated, NULL);
char *customMenuAtomName1 = "CUSTOM_MENU_ACTION_B";
const Atom customMenuAtom1 = XmInternAtom(display, customMenuAtomName1, False);
XmAddProtocolCallback(topLevel, xaMwmMessages, customMenuAtom1, customMenuItemBActivated, NULL);
char buf[256];
sprintf(buf, customMenu, (int) customMenuAtom0, (int) customMenuAtom1);
XtVaSetValues(topLevel, XmNmwmMenu, buf, NULL);
XtRealizeWidget(topLevel);
XtAppMainLoop(appContext);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment