Hi All,
I messed around with geo's code from here:
http://forums.nekochan.net/viewtopic.php?f=15&t=16726405 to see if i could maybe find what was causing the odd 'jump to the left' artefact he was seeing on IRIX. Unfortunately, I think it's a bug in IRIX Motif for which obviously we'll never see a fix.
My little experiment left me with some code for a basic ViewKit component. So instead of deleting it, I thought I'd post it here. Perhaps somebody might find it interesting or useful.
Counter.C Code:
/*
Counter.C A basic ViewKit component.
(c)2012 under GPL, Avi Bercovich <avi@sillypages.org>
*/
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
#include <Xm/Form.h>
#include <Xm/RowColumn.h>
#include <Xm/Frame.h>
#include <Xm/PushB.h>
#include <Xm/Label.h>
#include <Vk/VkApp.h>
#include <Vk/VkSimpleWindow.h>
// the interval by which the counter increases or decreases
#define COUNTER_STEP 5
class Counter : public VkComponent {
public:
Counter(const char *name, Widget parent);
~Counter() {};
virtual const char *className() { return "Counter"; }
void setCount(int count) { _count = count; }
int getCount() { return _count; }
void updateCounter();
private:
int _count;
Widget ui_frame_counter;
static String ui_defaultResources[];
static void increase_cb(Widget w, XtPointer clientData, XtPointer callData);
static void decrease_cb(Widget w, XtPointer clientData, XtPointer callData);
};
class CounterWindow : public VkSimpleWindow {
public:
CounterWindow(const char *name);
~CounterWindow() {};
virtual const char *className() { return "CounterWindow"; }
};
String Counter::ui_defaultResources[] = {
"*renderTable: normal14, bold24",
"*fontType: FONT_IS_FONT",
"*normal14.fontName: -*-helvetica-medium-r-normal-*-14-*-*-*-*-*-*-*",
"*bold24.fontName: -*-helvetica-bold-r-normal-*-24-*-*-*-*-*-*-*",
NULL
};
Counter::Counter(const char *name, Widget parent) : VkComponent(name) {
XmString _string;
// _baseWidget remains unmanaged. The VkComponent->show() method called in main() takes care of that.
_baseWidget = XtVaCreateWidget("BaseWidget", xmRowColumnWidgetClass, parent,
XmNtopOffset, 5,
XmNrightOffset, 5,
XmNbottomOffset, 5,
XmNleftOffset, 5,
XmNnumColumns, 3,
XmNorientation, XmHORIZONTAL,
NULL);
// make sure ViewKit takes care of the widget destruction of our new VkComponent
installDestroyHandler();
// load font definitions, only works with Motif 2.1
setDefaultResources(_baseWidget, ui_defaultResources);
// create the Counter widgets and add the Xt callbacks for our buttons
_string = XmStringCreate("-", "bold24");
Widget ui_decrease_btn = XtVaCreateManagedWidget("DecreaseButton", xmPushButtonWidgetClass, _baseWidget,
XmNlabelString, _string,
NULL);
XtAddCallback(ui_decrease_btn, XmNactivateCallback, &Counter::decrease_cb, (XtPointer) this );
Widget ui_frame = XtVaCreateManagedWidget("Frame", xmFrameWidgetClass, _baseWidget,
XmNshadowType, XmSHADOW_ETCHED_OUT,
XmNrightOffset, 5,
XmNleftOffset, 5,
NULL);
_string = XmStringCreate("Counter", "normal14");
Widget ui_frame_label = XtVaCreateManagedWidget("FrameLabel", xmLabelWidgetClass, ui_frame,
XmNframeChildType, XmFRAME_TITLE_CHILD,
XmNchildHorizontalAlignment, XmALIGNMENT_END,
XmNlabelString, _string,
NULL);
_string = XmStringCreate("0", "bold24");
ui_frame_counter = XtVaCreateManagedWidget("FrameCounter", xmLabelWidgetClass, ui_frame,
XmNlabelString, _string,
NULL);
_string = XmStringCreate("+", "bold24");
Widget ui_increase_btn = XtVaCreateManagedWidget("IncreaseButton", xmPushButtonWidgetClass, _baseWidget,
XmNlabelString, _string,
NULL);
XtAddCallback(ui_increase_btn, XmNactivateCallback, &Counter::increase_cb, (XtPointer) this );
// housekeeping...
XmStringFree(_string);
// reset internal counter
setCount(0);
}
void Counter::increase_cb(Widget w, XtPointer clientData, XtPointer callData) {
// This is C++ casting magicke to get the actual object instance that triggered the callback
Counter *obj = (Counter *) clientData;
// Now that we have the correct object, we can deal with this event
obj->setCount(obj->getCount() + COUNTER_STEP);
obj->updateCounter();
}
void Counter::decrease_cb(Widget w, XtPointer clientData, XtPointer callData) {
// This is C++ casting magicke to get the actual object instance that triggered the callback
Counter *obj = (Counter *) clientData;
// Now that we have the correct object, we can deal with this event
obj->setCount(obj->getCount() - COUNTER_STEP);
obj->updateCounter();
}
void Counter::updateCounter() {
// There's no way to feed XmStringCreate the 'count' parameter as an integer, So we need this nasty little C typing workaround.
char bla[10];
sprintf(bla, "%d", getCount());
XmString _string = XmStringCreate(bla, "bold24");
XtVaSetValues(ui_frame_counter,
XmNlabelString, _string,
NULL);
XmStringFree(_string);
}
CounterWindow::CounterWindow(const char *name) : VkSimpleWindow(name) {
Widget ui_root = XtCreateWidget("UIRoot", xmFormWidgetClass, mainWindowWidget(), NULL, 0);
Counter *ui_counter = new Counter("Counter", ui_root);
ui_counter->setCount(0);
ui_counter->show();
addView(ui_root);
}
int main(int argc , char *argv[]) {
VkApp *app = new VkApp("Counter", &argc, argv);
CounterWindow *win = new CounterWindow("Counter Window");
win->show();
app->run();
}
MakefileCode:
APP_NAME = counter
APP_OBJS = Counter.o
INCDIRS = -I. -I/usr/local/include -I/usr/nekoware/include
LIBDIRS = -L/usr/local/lib -L/usr/nekoware/lib
STDLIBS = -lm
STDCPPLIBS = -lC -lCio
X11LIBS = -lXt -lX11 -lPW
MOTIFLIBS = -lSgm -lXm
VKLIBS = -lvk
LIBS= $(VKLIBS) \
$(MOTIFLIBS)\
$(X11LIBS)\
$(STDCPPLIBS)\
$(STDLIBS)
CXX = CC
CXXOPTS = -O2 -LANG:std
CC = c99
COPTS = -O2
all : $(APP_NAME)
$(APP_NAME): ${APP_OBJS}
${CXX} ${APP_OBJS} ${LIBDIRS} ${LIBS} -o ${APP_NAME}
%.o : %.c
${CC} ${COPTS} ${INCDIRS} -c $<
%.o : %.C
${CXX} ${CXXOPTS} ${INCDIRS} -c $<
clean :
rm -rf ${APP_NAME} *.o core *~ ii_*