Skip to content

Instantly share code, notes, and snippets.

@wllmtrng
Last active July 19, 2023 14:30
Show Gist options
  • Save wllmtrng/ede0e0c86acbbca65b3e to your computer and use it in GitHub Desktop.
Save wllmtrng/ede0e0c86acbbca65b3e to your computer and use it in GitHub Desktop.
X11 Motif C++ Widget Base Class
/*
* BasicComponent.cpp
*
* Created on: Oct 4, 2014
* Author: wtruong
*/
#include <cstdio>
#include "UIComponent.h"
UIComponent::~UIComponent() {
if (mWidget) {
XtRemoveCallback(mWidget, XmNdestroyCallback,
UIComponent::widgetDestroyedCallback,
(XtPointer)this);
XtDestroyWidget(mWidget);
}
}
void UIComponent::manage()
{
if (mWidget) {
//TODO: Put a check to see if XmNdestroyCallback is registered
XtManageChild(mWidget);
}
}
void UIComponent::unmanage()
{
if (mWidget) {
XtUnmanageChild(mWidget);
}
}
void UIComponent::widgetDestroyed()
{
mWidget = NULL;
}
UIComponent::UIComponent(const char* n)
: mName(n)
{
mWidget = NULL;
}
void UIComponent::installDestroyHandler()
{
if (mWidget) {
XtAddCallback(mWidget, XmNdestroyCallback,
UIComponent::widgetDestroyedCallback,
(XtPointer)this);
}
}
void UIComponent::setDefaultResources(const Widget,
const char* resourceSpec[])
{
int i;
Display *dpy = XtDisplay(mWidget);
XrmDatabase rdb = NULL;
// Create empty resource database
rdb = XrmGetStringDatabase("");
// Add the Component resources, prepending the name of the component
i = 0;
while (resourceSpec[i] != NULL) {
char buf[1000];
snprintf(buf, 1000, "*%s%s", mName, resourceSpec[i++]);
XrmPutLineResource(&rdb, buf);
}
// Merge them into the Xt database, with lowest precedence
if (rdb) {
XrmDatabase db = XtDatabase(dpy);
XrmCombineDatabase(rdb, &db, FALSE);
}
}
void UIComponent::getResources(const XtResourceList resources,
int numResources)
{
if (mWidget && resources) {
XtGetSubresources(XtParent(mWidget), (XtPointer)this,
mName, className(),
resources, numResources,
NULL, 0);
}
}
void UIComponent::widgetDestroyedCallback(Widget,
XtPointer clientData,
XtPointer)
{
UIComponent* obj = (UIComponent*)clientData;
obj->widgetDestroyed();
}
#ifndef UICOMPONENT_H_
#define UICOMPONENT_H_
#include <string>
#include <Xm/Xm.h>
class UIComponent
{
public:
virtual ~UIComponent();
virtual void manage();
virtual void unmanage();
virtual const char* const className() { return "UIComponent"; }
const Widget BaseWidget() { return (mWidget); }
protected:
const char* mName;
Widget mWidget;
virtual void widgetDestroyed();
UIComponent(const char* name);
// Must install this in the derived class's constructor
// once w_ is instantitated
void installDestroyHandler();
// TODO: Convert const char* to vector to be more safe
void setDefaultResources(const Widget, const char*[]);
void getResources(const XtResourceList, int);
private:
// Interface between XmNdestroyCallback and this class
static void widgetDestroyedCallback(Widget, XtPointer, XtPointer);
};
#endif /* UICOMPONENT_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment