00001 00002 #include "VMDTkinterMenu.h" 00003 00004 // do the equivalent of 00005 // root.wm_protocol('WM_DELETE_WINDOW', root.wm_withdraw) 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 // func failed; don't bother calling it again. 00041 Py_DECREF(func); 00042 func = NULL; 00043 return; 00044 } 00045 } 00046 if (root) preserve_window(root); 00047 } 00048 00049 // root.wm_deiconify() 00050 if (root) { 00051 Py_XDECREF(PyObject_CallMethod(root, (char *)"wm_deiconify", NULL)); 00052 } 00053 } 00054 00055 void VMDTkinterMenu::do_off() { 00056 // root.wm_withdraw() 00057 if (root) { 00058 Py_XDECREF(PyObject_CallMethod(root, (char *)"wm_withdraw", NULL)); 00059 } 00060 } 00061 00062 void VMDTkinterMenu::move(int x, int y) { 00063 // root.wm_geometry(+x+y) 00064 if (root) { 00065 char buf[100]; 00066 sprintf(buf, "+%d+%d", x, y); 00067 Py_XDECREF(PyObject_CallMethod(root, (char *)"wm_geometry", "s", buf)); 00068 } 00069 } 00070 00071 void VMDTkinterMenu::where(int &x, int &y) { 00072 // root.wm_geometry() 00073 if (root) { 00074 PyObject *result = PyObject_CallMethod(root, (char *)"wm_geometry", NULL); 00075 if (result) { 00076 #if PY_MAJOR_VERSION >= 3 00077 const char *str = PyUnicode_AsUTF8(result); 00078 #else 00079 char *str = PyString_AsString(result); 00080 #endif 00081 int w, h; 00082 if (sscanf(str, "%dx%d+%d+%d", &w, &h, &x, &y) != 4) { 00083 fprintf(stderr, "couldn't parse output of geometry: %s\n", str); 00084 } 00085 Py_XDECREF(result); 00086 } 00087 } else { 00088 // give default values 00089 x=y=0; 00090 } 00091 } 00092