00001
00002 #include "VMDTkinterMenu.h"
00003
00004
00005
00006
00007 static void preserve_window(PyObject *root) {
00008 PyObject *cb = PyObject_GetAttrString(root, (char *)"wm_withdraw");
00009 if (cb) {
00010 Py_XDECREF(PyObject_CallMethod(root, (char *)"wm_protocol", (char *)"sO",
00011 "WM_DELETE_WINDOW", cb));
00012 Py_DECREF(cb);
00013 }
00014 }
00015
00016 VMDTkinterMenu::VMDTkinterMenu(const char *menuname, PyObject *theroot,
00017 VMDApp *vmdapp)
00018 : VMDMenu(menuname, vmdapp), root(theroot), func(NULL) {
00019 if (root) {
00020 Py_INCREF(root);
00021 preserve_window(root);
00022 }
00023 }
00024
00025 VMDTkinterMenu::~VMDTkinterMenu() {
00026 Py_XDECREF(root);
00027 Py_XDECREF(func);
00028 }
00029
00030 void VMDTkinterMenu::register_windowproc(PyObject *newfunc) {
00031 Py_XDECREF(func);
00032 func = newfunc;
00033 Py_INCREF(func);
00034 }
00035
00036 void VMDTkinterMenu::do_on() {
00037 if (!root) {
00038 if (func) {
00039 if (!(root = PyObject_CallObject(func, NULL))) {
00040
00041 Py_DECREF(func);
00042 func = NULL;
00043 return;
00044 }
00045 }
00046 if (root) preserve_window(root);
00047 }
00048
00049 if (root)
00050 Py_XDECREF(PyObject_CallMethod(root, (char *)"wm_deiconify", NULL));
00051 }
00052
00053 void VMDTkinterMenu::do_off() {
00054
00055 if (root)
00056 Py_XDECREF(PyObject_CallMethod(root, (char *)"wm_withdraw", NULL));
00057 }
00058
00059 void VMDTkinterMenu::move(int x, int y) {
00060
00061 if (root) {
00062 char buf[100];
00063 sprintf(buf, "+%d+%d", x, y);
00064 Py_XDECREF(PyObject_CallMethod(root, (char *)"wm_geometry", "s", buf));
00065 }
00066 }
00067
00068 void VMDTkinterMenu::where(int &x, int &y) {
00069
00070 if (root) {
00071 PyObject *result = PyObject_CallMethod(root, (char *)"wm_geometry", NULL);
00072 if (result) {
00073 char *str = PyString_AsString(result);
00074 int w, h;
00075 if (sscanf(str, "%dx%d+%d+%d", &w, &h, &x, &y) != 4) {
00076 fprintf(stderr, "couldn't parse output of geometry: %s\n", str);
00077 }
00078 Py_XDECREF(result);
00079 }
00080 } else {
00081
00082 x=y=0;
00083 }
00084 }
00085