/* main.cpp */ #include #include #include #include #include #include #define ___safe #define ___bool int #define ___out static int qt_char_encoding = 1; // 0=latin1, 1=utf8, 2=ascii class SimpleReceiver: public QObject { Q_OBJECT void *thunk; public: SimpleReceiver(char *name, C_word proc) { setObjectName(name); thunk = CHICKEN_new_gc_root(); CHICKEN_gc_root_set(thunk, proc); } ~SimpleReceiver() { CHICKEN_delete_gc_root(thunk); } public slots: void slot() { C_callback(CHICKEN_gc_root_ref(thunk), 0); } }; class OpenGLWidget: public QOpenGLWidget { void *thunk; public: OpenGLWidget(char *name, QWidget *parent, C_word proc) : QOpenGLWidget(parent) { setObjectName(name); thunk = CHICKEN_new_gc_root(); CHICKEN_gc_root_set(thunk, proc); } ~OpenGLWidget() { CHICKEN_delete_gc_root(thunk); } protected: // Set up the rendering context, define display lists etc.: void initializeGL() { C_save(C_fix(0)); C_callback(CHICKEN_gc_root_ref(thunk), 1); } // setup viewport, projection etc.: void resizeGL(int w, int h) { C_save(C_fix(1)); C_save(C_fix(w)); C_save(C_fix(h)); C_callback(CHICKEN_gc_root_ref(thunk), 3); } // draw the scene: void paintGL() { C_save(C_fix(2)); C_callback(CHICKEN_gc_root_ref(thunk), 1); } }; #define qtobject QObject * #define qtapplication QApplication * #define qtreceiver SimpleReceiver * #define qtwidget QWidget * #define qtpixmap QPixmap * #define qticon QIcon * #define qttimer QTimer * #define qtsound QSoundEffect * #define qttextedit QTextEdit * #define qtaction QAction * extern "C" { #include "prototypes.h" } #include "main.moc" QApplication *qt_init() { QApplication *app = new QApplication(C_main_argc, C_main_argv); QObject::connect(app, SIGNAL(lastWindowClosed()), app, SLOT(quit())); return qApp; } QWidget *qt_create(char *string, QWidget *parent) { QUiLoader loader; QBuffer buf; buf.open(QBuffer::ReadWrite); buf.write(string); buf.seek(0); QWidget *w = loader.load(&buf, parent); buf.close(); return w; } ___bool qt_run(___bool once) { if(once) { qApp->processEvents(); return 1; } else return qApp->exec(); } void qt_show(QWidget *w) { w->show(); } void qt_hide(QWidget *w) { w->show(); } void qt_deleteobject(QObject *o) { delete o; } void qt_deletepixmap(QPixmap *o) { delete o; } void qt_deletepixmap(QIcon *o) { delete o; } ___bool qt_connect(QWidget *w1, char *sig, QObject *w2, char *slot) { return bool(QObject::connect(w1, sig, w2, slot)); } QObject *qt_find(QObject *parent, char *name) { return parent->findChild(QString(name)); } QObject *qt_receiver(char *name, C_word proc) { return new SimpleReceiver(name, proc); } char *qstrdata(const QString &str) { static char *strbuf = NULL; static int strbuflen = 0; int len = str.size(); if(strbuf == NULL || strbuflen < len) { strbuf = (char *)realloc(strbuf, strbuflen = len * 2); assert(strbuf != NULL); } char *ptr; switch(qt_char_encoding) { case 1: ptr = str.toLatin1().data(); break; case 2: ptr = str.toUtf8().data(); break; case 3: ptr = str.toLatin1().data(); break; } memcpy(strbuf, ptr, len + 1); return strbuf; } int qchrdata(const QChar chr) { switch(qt_char_encoding) { case 1: return chr.toLatin1(); break; case 2: return chr.unicode(); break; default: return chr.toLatin1(); break; } } qtpixmap qt_pixmap(char *filename) { QPixmap *px = new QPixmap(filename); if(px->isNull()) { delete px; return 0; } return px; } qticon qt_icon(char *filename) { QIcon *icon = new QIcon(filename); if(icon->isNull()) { delete icon; return 0; } return icon; } qticon qt_themeicon(char *theme_icon_name) { QIcon *icon = new QIcon(QIcon::fromTheme(theme_icon_name)); if(icon->isNull()) { delete icon; return 0; } return icon; } qtpixmap qt_icontopixmap(QIcon *icon, int w, int h) { QPixmap *px = new QPixmap(icon->pixmap(w, h)); if(px->isNull()) { delete px; return 0; } return px; } qticon qt_pixmaptoicon(QPixmap* px) { QIcon* icon = new QIcon(*px); if(icon->isNull()) { delete icon; return 0; } return icon; } enum { INFORMATION = 0, QUESTION = 1, WARNING = 2, CRITICAL = 3 }; int qt_message(char *caption, char *text, QWidget *parent, char *b0, char *b1, char *b2, int msg_type) { switch(msg_type) { case QUESTION: return QMessageBox::question(parent, caption, text, b0, b1, b2); case WARNING: return QMessageBox::warning(parent, caption, text, b0, b1, b2); case CRITICAL: return QMessageBox::critical(parent, caption, text, b0, b1, b2); } return QMessageBox::information(parent, caption, text, b0, b1, b2); } #define propsetter(name, type, metatype) \ ___bool qt_set ## name ## property(QWidget *w, char *prop, type val) \ { \ const QMetaObject *mo = w->metaObject(); \ int i = mo->indexOfProperty(prop); \ if(i == -1) return 0; \ else return mo->property(i).write(w, QVariant(QMetaType(metatype), &val)); \ } propsetter(bool, ___bool, QMetaType::Bool) propsetter(int, int, QMetaType::Int) propsetter(float, double, QMetaType::Double) propsetter(char, char, QMetaType::Char) ___bool qt_setstringproperty(QWidget *w, char *prop, char* val) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); if(i == -1) return 0; else return mo->property(i).write(w, QVariant((const char*)val)); } ___bool qt_setpixmapproperty(QWidget *w, char *prop, qtpixmap val) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); if(i == -1) return 0; else return mo->property(i).write(w, *val); } ___bool qt_setpointproperty(QWidget *w, char *prop, int *val) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); if(i == -1) return 0; else { switch(mo->property(i).typeId()) { case QMetaType::QPoint: return mo->property(i).write(w, QPoint(val[ 0 ], val[ 1 ])); case QMetaType::QSize: return mo->property(i).write(w, QSize(val[ 0 ], val[ 1 ])); default: return false; } } } ___bool qt_setpointfproperty(QWidget *w, char *prop, double *val) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); if(i == -1) return 0; else { switch(mo->property(i).typeId()) { case QMetaType::QPointF: return mo->property(i).write(w, QPointF(val[ 0 ], val[ 1 ])); case QMetaType::QSizeF: return mo->property(i).write(w, QSizeF(val[ 0 ], val[ 1 ])); default: return false; } } } ___bool qt_setrectproperty(QWidget *w, char *prop, int *val) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); if(i == -1) return 0; else return mo->property(i).write(w, QRect(val[ 0 ], val[ 1 ], val[ 2 ], val[ 3 ])); } ___bool qt_setrectfproperty(QWidget *w, char *prop, double *val) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); if(i == -1) return 0; else return mo->property(i).write(w, QRectF(val[ 0 ], val[ 1 ], val[ 2 ], val[ 3 ])); } char *qt_getstringproperty(QWidget *w, char *prop) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); return qstrdata(mo->property(i).read(w).toString()); } int qt_getcharproperty(QWidget *w, char *prop) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); return qchrdata(mo->property(i).read(w).toChar()); } int qt_getintproperty(QWidget *w, char *prop) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); return mo->property(i).read(w).toInt(); } double qt_getfloatproperty(QWidget *w, char *prop) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); return mo->property(i).read(w).toDouble(); } ___bool qt_getboolproperty(QWidget *w, char *prop) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); return mo->property(i).read(w).toBool(); } qtpixmap qt_getpixmapproperty(QWidget *w, char *prop) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); return new QPixmap(mo->property(i).read(w).value()); } C_word qt_getpointfproperty(QWidget *w, char *prop, C_word pt) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); QPointF qpt = mo->property(i).read(w).toPointF(); *((double *)C_data_pointer(C_block_item(pt, 1))) = qpt.x(); ((double *)C_data_pointer(C_block_item(pt, 1)))[ 1 ] = qpt.y(); return pt; } C_word qt_getpointproperty(QWidget *w, char *prop, C_word pt) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); QPoint qpt = mo->property(i).read(w).toPoint(); *((int *)C_data_pointer(C_block_item(pt, 1))) = qpt.x(); ((int *)C_data_pointer(C_block_item(pt, 1)))[ 1 ] = qpt.y(); return pt; } C_word qt_getrectfproperty(QWidget *w, char *prop, C_word pt) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); QRectF qpt = mo->property(i).read(w).toRectF(); *((double *)C_data_pointer(C_block_item(pt, 1))) = qpt.x(); ((double *)C_data_pointer(C_block_item(pt, 1)))[ 1 ] = qpt.y(); ((double *)C_data_pointer(C_block_item(pt, 1)))[ 2 ] = qpt.width(); ((double *)C_data_pointer(C_block_item(pt, 1)))[ 3 ] = qpt.height(); return pt; } C_word qt_getrectproperty(QWidget *w, char *prop, C_word pt) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); QRect qpt = mo->property(i).read(w).toRect(); *((int *)C_data_pointer(C_block_item(pt, 1))) = qpt.x(); ((int *)C_data_pointer(C_block_item(pt, 1)))[ 1 ] = qpt.y(); ((int *)C_data_pointer(C_block_item(pt, 1)))[ 2 ] = qpt.width(); ((int *)C_data_pointer(C_block_item(pt, 1)))[ 3 ] = qpt.height(); return pt; } C_word qt_getsizefproperty(QWidget *w, char *prop, C_word pt) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); QSizeF qpt = mo->property(i).read(w).toSizeF(); *((double *)C_data_pointer(C_block_item(pt, 1))) = qpt.width(); ((double *)C_data_pointer(C_block_item(pt, 1)))[ 1 ] = qpt.height(); return pt; } C_word qt_getsizeproperty(QWidget *w, char *prop, C_word pt) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); QSize qpt = mo->property(i).read(w).toSize(); *((int *)C_data_pointer(C_block_item(pt, 1))) = qpt.width(); ((int *)C_data_pointer(C_block_item(pt, 1)))[ 1 ] = qpt.height(); return pt; } int qt_propertytype(qtwidget w, char *prop) { const QMetaObject *mo = w->metaObject(); int i = mo->indexOfProperty(prop); if(i == -1) return 0; else { switch(mo->property(i).typeId()) { case QMetaType::Bool: return 1; case QMetaType::Char: return 2; case QMetaType::Double: return 3; case QMetaType::Int: case QMetaType::UInt: return 4; case QMetaType::LongLong: case QMetaType::ULongLong: return 3; case QMetaType::QString: return 5; case QMetaType::QPixmap: return 6; case QMetaType::QPointF: return 7; case QMetaType::QRectF: return 8; case QMetaType::QSizeF: return 9; case QMetaType::QPoint: return 10; case QMetaType::QSize: return 11; case QMetaType::QRect: return 12; default: return 0; } } } const char *qt_classname(qtobject w) { return w->metaObject()->className(); } qtwidget qt_gl(char *name, qtwidget parent, C_word proc) { return new OpenGLWidget(name, parent, proc); } void qt_update(qtwidget w) { w->update(); } qttimer qt_timer(double secs) { QTimer *tm = new QTimer(); tm->setInterval((int)(secs * 1000)); return tm; } void qt_start(qttimer t) { t->start(); } void qt_stoptimer(qttimer t) { t->stop(); } void qt_stopsound(qtsound t) { t->stop(); } void qt_clearlistwidget(qtwidget w) { ((QListWidget *)w)->clear(); } void qt_addcomboboxitem(qtwidget w, char *s) { ((QComboBox *)w)->addItem(s); } void qt_addlistwidgetitem(qtwidget w, char *s) { ((QListWidget *)w)->addItem(s); } void qt_addtreewidgetitem(qtwidget w, char *s) { QStringList lst = QString(s).split("|"); ((QTreeWidget *)w)->addTopLevelItem(new QTreeWidgetItem(lst)); } char *qt_listwidgetitem(qtwidget w, int i) { return qstrdata(((QListWidget *)w)->item(i)->text()); } qtsound qt_sound(char *filename) { QSoundEffect* sound = new QSoundEffect(); sound->setSource(QUrl::fromLocalFile(filename)); return sound; } void qt_play(qtsound s) { s->play(); } char *qt_getexistingdirectory(qtwidget p, char *cap, char *dir, int opts) { return qstrdata(QFileDialog::getExistingDirectory(p, cap, dir, (QFileDialog::Option)opts)); } char *qt_getopenfilename(qtwidget p, char *cap, char *dir, char *filter, int opts) { return qstrdata(QFileDialog::getOpenFileName(p, cap, dir, filter, 0, (QFileDialog::Options)opts)); } char *qt_getsavefilename(qtwidget p, char *cap, char *dir, char *filter, int opts) { return qstrdata(QFileDialog::getSaveFileName(p, cap, dir, filter, 0, (QFileDialog::Options)opts)); } void qt_setheaders(qtwidget w, char *s) { ((QTreeWidget *)w)->setHeaderLabels(QString(s).split("|")); } char *qt_selection(qttextedit t) { QString txt = ((QTextEdit *)t)->textCursor().selectedText(); txt.replace(QChar(QChar::ParagraphSeparator), '\n'); return qstrdata(txt); } void qt_insert(qttextedit t, char *s) { QTextEdit *te = (QTextEdit *)t; QTextCursor c = te->textCursor(); c.insertText(s); } qtaction qt_shortcut(qtwidget w, char *key) { QAction *a = new QAction(w); a->setShortcut(QKeySequence(key)); return a; } void qt_addaction(qtwidget w, qtaction a) { ((QWidget *)w)->addAction((QAction *)a); } void qt_removeaction(qtwidget w, qtaction a) { ((QWidget *)w)->removeAction((QAction *)a); } int qt_charencoding(int mode) { if(mode) return qt_char_encoding = mode; else return qt_char_encoding; }