Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

VMDTkinterMenu.C

Go to the documentation of this file.
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   // root.wm_deiconify()
00049   if (root)
00050     Py_XDECREF(PyObject_CallMethod(root, (char *)"wm_deiconify", NULL));
00051 }
00052 
00053 void VMDTkinterMenu::do_off() {
00054   // root.wm_withdraw()
00055   if (root)
00056     Py_XDECREF(PyObject_CallMethod(root, (char *)"wm_withdraw", NULL));
00057 }
00058 
00059 void VMDTkinterMenu::move(int x, int y) {
00060   // root.wm_geometry(+x+y)
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   // root.wm_geometry()
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     // give default values
00082     x=y=0;
00083   }
00084 }
00085 

Generated on Wed May 23 01:50:32 2012 for VMD (current) by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002