00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef VMDPYTHON
00029 #include "PythonTextInterp.h"
00030 #endif
00031
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <ctype.h>
00035
00036 #include "UIText.h"
00037 #include "TextEvent.h"
00038 #include "CommandQueue.h"
00039 #include "Inform.h"
00040 #include "config.h"
00041 #include "utilities.h"
00042 #include "PlainTextInterp.h"
00043 #include "VMDApp.h"
00044 #include "TclTextInterp.h"
00045 #include "Molecule.h"
00046 #include "MoleculeList.h"
00047 #include "SymbolTable.h"
00048
00049 #if defined(VMDTKCON)
00050 #include "vmdconsole.h"
00051 #endif
00052
00054
00055
00056 UIText::UIText(VMDApp *vmdapp, int guienabled, int mpienabled)
00057 #ifdef VMDVRJUGGLER
00058 : UIObject(vmdapp), _isInitialized(false) {
00059 #else
00060 : UIObject(vmdapp) {
00061 #endif
00062
00063
00064 tclinterp = NULL;
00065 pythoninterp = NULL;
00066
00067 #if defined(VMDTKCON)
00068 cmdbufstr = new Inform("", VMDCON_ALWAYS);
00069 #else
00070 cmdbufstr = new Inform("");
00071 #endif
00072
00073
00074 for (int i=0; i<Command::TOTAL; i++) command_wanted(i);
00075
00076
00077 #ifdef VMDTCL
00078 tclinterp = new TclTextInterp(app, guienabled, mpienabled);
00079 #ifdef VMDTKCON
00080
00081 vmdcon_set_status(vmdcon_get_status(), ((TclTextInterp *)tclinterp)->get_interp());
00082 #endif
00083 #endif
00084
00085
00086
00087
00088 #ifdef VMDPYTHON
00089 if (!tclinterp)
00090 pythoninterp = new PythonTextInterp(app);
00091 #endif
00092
00093
00094
00095
00096 if (tclinterp)
00097 interp = tclinterp;
00098 else if (pythoninterp)
00099 interp = pythoninterp;
00100 else
00101 interp = new PlainTextInterp;
00102
00103 reset();
00104 }
00105
00106
00107
00108
00109 void UIText::read_init(void) {
00110 interp->doInit();
00111 #ifdef VMDVRJUGGLER
00112 _isInitialized = true;
00113 #endif
00114 }
00115
00116
00117 UIText::~UIText(void) {
00118 if (tclinterp) tclinterp->logfile_cb("exit");
00119 #ifdef VMDTCL
00120 delete tclinterp;
00121 #endif
00122
00123 #ifdef VMDPYTHON
00124 delete pythoninterp;
00125 #endif
00126 delete cmdbufstr;
00127 }
00128
00129 #ifdef VMDVRJUGGLER
00130 bool UIText::isInitialized(){
00131
00132 return _isInitialized;
00133 }
00134 #endif
00135
00136 int UIText::save_state(const char *fname) {
00137 if (tclinterp) {
00138 JString cmd("save_state ");
00139 cmd += "{";
00140 cmd += fname;
00141 cmd += "}";
00142 return tclinterp->evalString((const char *)cmd);
00143 }
00144 return FALSE;
00145 }
00146
00147
00148 void UIText::read_from_file(const char *fname) {
00149 #ifdef VMDVRJUGGLER
00150 if (isInitialized()) {
00151
00152 } else {
00153 msgInfo << "is not Initialized" << sendmsg;
00154 }
00155
00156 if (_isInitialized) {
00157
00158
00159 interp->evalFile(fname);
00160 } else {
00161 msgInfo << "not ready to read " << fname << sendmsg;
00162 }
00163 #else
00164 interp->evalFile(fname);
00165 #endif
00166 }
00167
00168
00169 int UIText::check_event(void) {
00170
00171
00172
00173 #if defined(VMD_SHARED)
00174 return FALSE;
00175 #endif
00176
00177
00178
00179
00180
00181
00182
00183
00184 if (!pythoninterp || (pythoninterp && !pythoninterp->doTkUpdate())) {
00185 if (tclinterp) {
00186 tclinterp->doTkUpdate();
00187 } else {
00188
00189 interp->doTkUpdate();
00190 }
00191 }
00192 interp->doEvent();
00193 return TRUE;
00194 }
00195
00196
00197 int UIText::act_on_command(int cmdtype, Command *cmd) {
00198 int action=1;
00199
00200 #if defined(VMDNVTX)
00201
00202 int profile_pushed=0;
00203 if (cmd->has_text(cmdbufstr)) {
00204 profile_pushed=1;
00205 PROFILE_PUSH_RANGE(cmdbufstr->text(), 1);
00206 }
00207 #endif
00208
00209 if (tclinterp) {
00210
00211 if (cmd->has_text(cmdbufstr)) {
00212 tclinterp->logfile_cb(cmdbufstr->text());
00213 #ifdef VMDVRJUGGLER
00214 if (app->jugglerMode == VRJ_MASTER) {
00215 app->logfile_juggler(cmdbufstr->text());
00216 }
00217 #endif
00218 cmdbufstr->reset();
00219 }
00220 }
00221
00222 if (cmdtype == Command::INTERP_EVENT) {
00223
00224 InterpEvent *event = (InterpEvent *)cmd;
00225 if (tclinterp)
00226 event->do_callback(tclinterp);
00227
00228
00229 if (pythoninterp)
00230 event->do_callback(pythoninterp);
00231 } else {
00232 action=0;
00233 }
00234
00235 #if defined(VMDNVTX)
00236 if (profile_pushed) {
00237 PROFILE_POP_RANGE();
00238 }
00239 #endif
00240
00241 return action;
00242 }
00243
00244
00245 int UIText::change_interp(const char *interpname) {
00246 if (!interpname) return FALSE;
00247 if (!strupcmp(interpname, "tcl")) {
00248 if (tclinterp) {
00249 msgInfo << "Text interpreter now Tcl" << sendmsg;
00250 interp = tclinterp;
00251 return TRUE;
00252 } else {
00253 msgErr << "Sorry, no Tcl text interpreter available" << sendmsg;
00254
00255 interpname = "python";
00256 }
00257 }
00258
00259 if (!strupcmp(interpname, "python")) {
00260 if (pythoninterp) {
00261 msgInfo << "Text interpreter now Python" << sendmsg;
00262 interp = pythoninterp;
00263
00264
00265
00266
00267 clearerr(stdin);
00268 return TRUE;
00269 } else {
00270 #if defined(VMDPYTHON)
00271 pythoninterp = new PythonTextInterp(app);
00272 if (pythoninterp) {
00273 msgInfo << "Text interpreter now Python" << sendmsg;
00274 interp = pythoninterp;
00275 return TRUE;
00276 } else {
00277 msgErr << "Sorry, unable to start Python text interpreter" << sendmsg;
00278 }
00279 #else
00280 msgErr << "Sorry, this version of VMD was compiled with Python support disabled" << sendmsg;
00281 #endif
00282 }
00283 } else {
00284 msgErr << "Unsupported text interpreter requested" << sendmsg;
00285 }
00286 return FALSE;
00287 }
00288