00001 /*************************************************************************** 00002 *cr 00003 *cr (C) Copyright 1995-2019 The Board of Trustees of the 00004 *cr University of Illinois 00005 *cr All Rights Reserved 00006 *cr 00007 ***************************************************************************/ 00012 00013 #ifndef Interp_EVENT_H 00014 #define Interp_EVENT_H 00015 00016 #include "Command.h" 00017 #include "JString.h" 00018 #include "TextInterp.h" 00019 #include "utilities.h" 00020 #include "DisplayDevice.h" 00021 00022 #include <ctype.h> 00023 00024 class TextInterp; 00025 00027 class InterpEvent : public Command { 00028 public: 00029 InterpEvent() 00030 : Command(INTERP_EVENT) {} 00031 virtual ~InterpEvent() {} 00032 virtual void do_callback(TextInterp *) = 0; 00033 }; 00034 00036 class UserKeyEvent : public InterpEvent { 00037 private: 00038 char key_desc[128]; 00039 00040 // want to make the key command like the shift_state-less version so 00041 // force the shift_state change in the character, if set, and record 00042 // only the other terms of the shift_state by putting on either a prefix 00043 // such as Alt- or Ctrl- 00044 void canonicalize(DisplayDevice::EventCodes ev, char c, int shiftstate) { 00045 key_desc[0] = 0; 00046 char tmp[2]; 00047 tmp[1] = '0円'; 00048 00049 // Spaceball and other AUX devices ignore Keyboard/Mouse meta key state 00050 if (shiftstate & DisplayDevice::AUX) { 00051 sprintf(key_desc, "Aux-%c", c); 00052 return; 00053 } 00054 00055 if (ev == DisplayDevice::WIN_KBD) { 00056 // Guarantee case of incoming characters from display device 00057 if (shiftstate & DisplayDevice::SHIFT) { 00058 tmp[0] = toupper(c); 00059 } else { 00060 tmp[0] = tolower(c); 00061 } 00062 00063 if (shiftstate & DisplayDevice::ALT) { 00064 strcat(key_desc, "Alt-"); 00065 } 00066 if (shiftstate & DisplayDevice::CONTROL) { 00067 strcat(key_desc, "Control-"); 00068 // however, if control is pressed then I have to adjust some of 00069 // the key information to get it in range. Eg, Control-a == 0x01 00070 if (0 < tmp[0] && tmp[0] <= 26) { // a-Z 00071 tmp[0] += int('a') - 1; 00072 // and correct the case 00073 if (shiftstate & DisplayDevice::SHIFT) { 00074 tmp[0] = tolower(tmp[0]); 00075 } 00076 } else { 00077 // fix these by hand: 00078 switch (tmp[0]) { 00079 case 0: tmp[0] = '2'; break; 00080 case 27: tmp[0] = '3'; break; 00081 case 28: tmp[0] = '4'; break; 00082 case 29: tmp[0] = '5'; break; 00083 case 30: tmp[0] = '6'; break; 00084 case 31: tmp[0] = '7'; break; 00085 case 127: tmp[0] = '8'; break; 00086 default: break; 00087 } 00088 } 00089 } 00090 // and put on the character 00091 strcat(key_desc, tmp); 00092 } else { 00093 switch (ev) { 00094 case DisplayDevice::WIN_KBD_ESCAPE: strcpy(key_desc, "Escape"); break; 00095 case DisplayDevice::WIN_KBD_UP: strcpy(key_desc, "Up"); break; 00096 case DisplayDevice::WIN_KBD_DOWN: strcpy(key_desc, "Down"); break; 00097 case DisplayDevice::WIN_KBD_LEFT: strcpy(key_desc, "Left"); break; 00098 case DisplayDevice::WIN_KBD_RIGHT: strcpy(key_desc, "Right"); break; 00099 case DisplayDevice::WIN_KBD_PAGE_UP: strcpy(key_desc, "Page_Up"); break; 00100 case DisplayDevice::WIN_KBD_PAGE_DOWN:strcpy(key_desc, "Page_Down"); break; 00101 case DisplayDevice::WIN_KBD_HOME: strcpy(key_desc, "Home"); break; 00102 case DisplayDevice::WIN_KBD_END: strcpy(key_desc, "End"); break; 00103 case DisplayDevice::WIN_KBD_INSERT: strcpy(key_desc, "Insert"); break; 00104 case DisplayDevice::WIN_KBD_DELETE: strcpy(key_desc, "Delete"); break; 00105 case DisplayDevice::WIN_KBD_F1: strcpy(key_desc, "F1"); break; 00106 case DisplayDevice::WIN_KBD_F2: strcpy(key_desc, "F2"); break; 00107 case DisplayDevice::WIN_KBD_F3: strcpy(key_desc, "F3"); break; 00108 case DisplayDevice::WIN_KBD_F4: strcpy(key_desc, "F4"); break; 00109 case DisplayDevice::WIN_KBD_F5: strcpy(key_desc, "F5"); break; 00110 case DisplayDevice::WIN_KBD_F6: strcpy(key_desc, "F6"); break; 00111 case DisplayDevice::WIN_KBD_F7: strcpy(key_desc, "F7"); break; 00112 case DisplayDevice::WIN_KBD_F8: strcpy(key_desc, "F8"); break; 00113 case DisplayDevice::WIN_KBD_F9: strcpy(key_desc, "F9"); break; 00114 case DisplayDevice::WIN_KBD_F10: strcpy(key_desc, "F10"); break; 00115 case DisplayDevice::WIN_KBD_F11: strcpy(key_desc, "F11"); break; 00116 case DisplayDevice::WIN_KBD_F12: strcpy(key_desc, "F12"); break; 00117 default: 00118 ; // Do nothing for any other event codes 00119 } 00120 } 00121 } 00122 00123 public: 00124 UserKeyEvent(DisplayDevice::EventCodes ev, char thekey, int theshiftstate) { 00125 canonicalize(ev, thekey, theshiftstate); 00126 } 00127 virtual void do_callback(TextInterp *interp) { 00128 interp->userkey_cb(key_desc); 00129 } 00130 }; 00131 00133 class TclEvalEvent : public InterpEvent { 00134 private: 00135 JString keystring; 00136 public: 00137 TclEvalEvent(const char *k) 00138 : keystring(k) {} 00139 virtual void do_callback(TextInterp *interp) { 00140 interp->tcl_cb((const char *)keystring); 00141 } 00142 }; 00143 00146 class PythonEvalEvent : public InterpEvent { 00147 private: 00148 JString keystring; 00149 public: 00150 PythonEvalEvent(const char *k) 00151 : keystring(k) {} 00152 virtual void do_callback(TextInterp *interp) { 00153 interp->python_cb((const char *)keystring); 00154 } 00155 }; 00156 00157 00159 class FrameEvent : public InterpEvent { 00160 private: 00161 int mol, frame; 00162 public: 00163 FrameEvent(int m, int f) 00164 : mol(m), frame(f) {} 00165 virtual void do_callback(TextInterp *interp) { 00166 interp->frame_cb(mol, frame); 00167 } 00168 }; 00169 00170 00172 class InitializeStructureEvent : public InterpEvent { 00173 private: 00174 int mol, code; 00175 public: 00176 InitializeStructureEvent(int m, int c) 00177 : mol(m), code(c) {} 00178 virtual void do_callback(TextInterp *interp) { 00179 interp->initialize_structure_cb(mol, code); 00180 } 00181 }; 00182 00185 class MoleculeEvent : public InterpEvent { 00186 private: 00187 int mol, code; 00188 public: 00189 enum MolEvent { MOL_DELETE=0, MOL_NEW=1, MOL_RENAME=2, MOL_REGEN=3, 00190 MOL_TOP=4 }; 00191 MoleculeEvent(int m, MolEvent c) 00192 : mol(m), code((int) c) {} 00193 virtual void do_callback(TextInterp *interp) { 00194 interp->molecule_changed_cb(mol, code); 00195 } 00196 }; 00197 00198 00200 class MouseModeEvent : public InterpEvent { 00201 private: 00202 JString mode; 00203 int submode; 00204 00205 public: 00206 MouseModeEvent(const char *m, int sm) : mode(m), submode(sm) {} 00207 virtual void do_callback(TextInterp *interp) { 00208 interp->mousemode_cb((const char *)mode, submode); 00209 } 00210 }; 00211 00212 00214 class MousePositionEvent : public InterpEvent { 00215 private: 00216 float x, y; 00217 int buttondown; 00218 00219 public: 00220 MousePositionEvent(float xv, float yv, int bdown) : x(xv), y(yv), buttondown(bdown) {} 00221 virtual void do_callback(TextInterp *interp) { 00222 interp->mouse_pos_cb(x, y, buttondown); 00223 } 00224 }; 00225 00226 00228 class MobileEvent : public InterpEvent { 00229 private: 00230 float tx; 00231 float ty; 00232 float tz; 00233 float rx; 00234 float ry; 00235 float rz; 00236 int buttondown; 00237 00238 public: 00239 MobileEvent(float utx, float uty, float utz, 00240 float urx, float ury, float urz, 00241 int bdown) : tx(utx), ty(uty), tz(utz), 00242 rx(urx), ry(ury), rz(urz), buttondown(bdown) {} 00243 virtual void do_callback(TextInterp *interp) { 00244 interp->mobile_cb(tx, ty, tz, rx, ry, rz, buttondown); 00245 } 00246 }; 00247 00249 class MobileStateChangedEvent : public InterpEvent { 00250 public: 00251 virtual void do_callback(TextInterp *interp) { 00252 interp->mobile_state_changed_cb(); 00253 } 00254 }; 00255 00257 class MobileDeviceCommandEvent : public InterpEvent { 00258 private: 00259 JString str; 00260 public: 00261 MobileDeviceCommandEvent(const char *event) 00262 : str(event) {} 00263 virtual void do_callback(TextInterp *interp) { 00264 interp->mobile_device_command_cb((const char *)str); 00265 } 00266 }; 00267 00268 00270 class SpaceballEvent : public InterpEvent { 00271 private: 00272 float tx; 00273 float ty; 00274 float tz; 00275 float rx; 00276 float ry; 00277 float rz; 00278 int buttondown; 00279 00280 public: 00281 SpaceballEvent(float utx, float uty, float utz, 00282 float urx, float ury, float urz, 00283 int bdown) : tx(utx), ty(uty), tz(utz), 00284 rx(urx), ry(ury), rz(urz), buttondown(bdown) {} 00285 virtual void do_callback(TextInterp *interp) { 00286 interp->spaceball_cb(tx, ty, tz, rx, ry, rz, buttondown); 00287 } 00288 }; 00289 00290 00292 class PickValueEvent : public InterpEvent { 00293 private: 00294 double val; 00295 public: 00296 PickValueEvent(double v) : val(v) {} 00297 virtual void do_callback(TextInterp *interp) { 00298 interp->pick_value_cb((float) val); 00299 } 00300 }; 00301 00302 00304 class PickAtomEvent : public InterpEvent { 00305 private: 00306 int mol, atom, key_shift_state; 00307 bool is_pick; 00308 public: 00309 PickAtomEvent(int m, int a , int ss, bool ispick=false) 00310 : mol(m), atom(a), key_shift_state(ss), is_pick(ispick) {} 00311 virtual void do_callback(TextInterp *interp) { 00312 interp->pick_atom_cb(mol, atom, key_shift_state, is_pick); 00313 } 00314 }; 00315 00317 class PickGraphicsEvent : public InterpEvent { 00318 private: 00319 int mol, tag, btn, key_shift_state; 00320 public: 00321 PickGraphicsEvent(int m, int t , int b, int ss) 00322 : mol(m), tag(t), btn(b), key_shift_state(ss) {} 00323 virtual void do_callback(TextInterp *interp) { 00324 interp->pick_graphics_cb(mol, tag, btn, key_shift_state); 00325 } 00326 }; 00327 00329 class PickAtomCallbackEvent : public InterpEvent { 00330 private: 00331 int mol, atom; 00332 JString client; 00333 public: 00334 PickAtomCallbackEvent(int m, int a, const char *c) 00335 : mol(m), atom(a), client(c) {} 00336 virtual void do_callback(TextInterp *interp) { 00337 interp->pick_atom_callback_cb(mol, atom, (const char *)client); 00338 } 00339 }; 00340 00341 00343 class TrajectoryReadEvent : public InterpEvent { 00344 private: 00345 int id; 00346 char *name; 00347 public: 00348 TrajectoryReadEvent(int m, const char *n) { 00349 id = m; 00350 name = stringdup(n); 00351 } 00352 ~TrajectoryReadEvent() { delete [] name; } 00353 virtual void do_callback(TextInterp *interp) { 00354 interp->trajectory_cb(id, name); 00355 } 00356 }; 00357 00358 00360 class TimestepEvent : public InterpEvent { 00361 private: 00362 int id, frame; 00363 public: 00364 TimestepEvent(int m, int f) : id(m), frame(f) {} 00365 virtual void do_callback(TextInterp *interp) { 00366 interp->timestep_cb(id, frame); 00367 } 00368 }; 00369 00370 00372 class HelpEvent : public InterpEvent { 00373 private: 00374 JString str; 00375 public: 00376 HelpEvent(const char *topic) 00377 : str(topic) {} 00378 virtual void do_callback(TextInterp *interp) { 00379 interp->help_cb((const char *)str); 00380 } 00381 }; 00382 00383 00385 class LogfileEvent : public InterpEvent { 00386 private: 00387 JString str; 00388 public: 00389 LogfileEvent(const char *event) 00390 : str(event) {} 00391 virtual void do_callback(TextInterp *interp) { 00392 interp->logfile_cb((const char *)str); 00393 } 00394 }; 00395 00396 00399 class GraphLabelEvent : public InterpEvent { 00400 private: 00401 char *type; // Atoms, Bonds, etc. 00402 int *ids; 00403 int nlabels; 00404 public: 00405 GraphLabelEvent(const char *labeltype, const int *labels, int n) { 00406 type = stringdup(labeltype); 00407 nlabels = n; 00408 if (n > 0) { 00409 ids = new int[n]; 00410 memcpy(ids, labels, n*sizeof(int)); 00411 } else { 00412 ids = NULL; 00413 } 00414 } 00415 virtual void do_callback(TextInterp *interp) { 00416 interp->graph_label_cb(type, ids, nlabels); 00417 } 00418 ~GraphLabelEvent() { 00419 delete [] type; 00420 delete [] ids; 00421 } 00422 }; 00423 00424 00426 class PickSelectionEvent : public InterpEvent { 00427 private: 00428 int num; 00429 int *atoms; 00430 public: 00431 PickSelectionEvent(int n, const int *a) : num(n) { 00432 if (n > 0) { 00433 atoms = new int[n]; 00434 memcpy(atoms, a, n*sizeof(int)); 00435 } else { 00436 atoms = NULL; 00437 } 00438 } 00439 ~PickSelectionEvent() { delete [] atoms; } 00440 virtual void do_callback(TextInterp *interp) { 00441 interp->pick_selection_cb(num, atoms); 00442 } 00443 }; 00444 00445 #endif