Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

XScene.cxx

Go to the documentation of this file.
00001 //  $Header: /nfs/slac/g/glast/ground/cvs/gui/src/XScene.cxx,v 1.1.1.1 2001/01/04 01:01:12 burnett Exp $
00002 //   Author: T. Burnett
00003 //
00004 // Implement XScene functions
00005 
00006 #ifndef WIN32  // stupid to prevent compilation in windows 
00007 #include <string>
00008 
00009 #include "XScene.h"
00010 #include "Xdraw.h"
00011 #include "MotifGUI.h"
00012 #include "gui/Menu.h"  // for key binding
00013 #include "gui/Projector.h"
00014 
00015 #include <X11/keysym.h>
00016 #include <assert.h>
00017 #include <iostream>
00018 
00019 typedef struct {int reason;  XEvent *event;
00020         } XmAnyCallbackStruct;
00021 
00022 static void ExposeCallback       (Widget,XtPointer,XmAnyCallbackStruct*);
00023 static void InputCallback        (Widget,XtPointer,XmAnyCallbackStruct*);
00024 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00025 //                   constructor
00026 XScene::XScene (float size, int initial_view, Widget drawWidget)
00027 : SceneControl ( new Projector(_drawer=new Xdraw), size, initial_view )
00028 , _widget(drawWidget)
00029 {
00030 
00031   if(_widget)
00032   {
00033       XtAddCallback (_widget,"exposeCallback" ,(XtCallbackProc)ExposeCallback,(XtPointer)this);
00034       XtAddCallback (_widget,"inputCallback" ,(XtCallbackProc)InputCallback,(XtPointer)this);
00035   }
00036   else  {
00037       // called without a widget--ceate a display and window
00038 
00039       _display = XOpenDisplay ("");       // connect to the server
00040 
00041       int screen = DefaultScreen (_display);  // get a screen
00042       _window = XCreateSimpleWindow (_display,
00043                                 DefaultRootWindow (_display),
00044                                 50,50,500,500, 2,
00045                                 BlackPixel(_display, screen),
00046                                 WhitePixel(_display, screen)
00047                                 );
00048 
00049 
00050       // tell Window Manager what we are doing
00051       static int argc=0;
00052       static char **argv;
00053       XSizeHints myhint = {  PPosition | PSize, 50,50, 500,500};
00054 
00055       XSetStandardProperties (_display, _window,
00056                            "gui View", "gui",
00057                            None, argv, argc, &myhint);
00058 
00059       XSelectInput(_display, _window, ButtonPressMask | ButtonReleaseMask | PointerMotionMask
00060                 | KeyPressMask | ExposureMask);
00061 
00062       XMapRaised (_display, _window);      // pop this window up on the screen
00063 
00064       /* this doesnt' work? (THB)
00065       // Rebind the Keysyms for the arrow keys and some others, Jones 371
00066       KeySym modlist[1]; // array of modifier Keysyms
00067 #define REBIND(from,to)\
00068         XRebindKeysym( _display, from, modlist, 0, (const unsigned char *)to,1)
00069    REBIND( XK_Left,  "b");
00070    REBIND( XK_Right, "f");
00071    REBIND( XK_Up,    "p");
00072    REBIND( XK_Down,  "n");
00073 
00074    REBIND( XK_Home,  "r");
00075    REBIND( XK_Prior, "s");
00076    REBIND( XK_Next,  "m");
00077    REBIND( XK_Begin, "o");
00078    REBIND( XK_End,   "i");
00079 #undef REBIND
00080 (THB)*/ 
00081    _drawer->setDisplay(_display,_window);
00082 
00083   }
00084 }
00085 XScene::~XScene()
00086 {
00087    delete _drawer;
00088    if(!_widget )
00089    {  // created these things, must close them down
00090      if (_window)
00091       XDestroyWindow (_display, _window);
00092      if (_display)
00093       XCloseDisplay (_display);
00094    }
00095 }
00096 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00097 //                   callbacks
00098 
00099 
00100 static void ExposeCallback ( Widget , XtPointer disp ,XmAnyCallbackStruct*)
00101 {
00102   ((XScene*)disp)->redisplay();
00103 }
00104 static void InputCallback ( Widget , XtPointer xptr ,XmAnyCallbackStruct* cbInfo)
00105 {
00106    XScene& disp = *(XScene*)xptr;
00107    if( !cbInfo->event ) return;
00108    int ret = disp.processEvent(*(cbInfo->event));
00109    if( ret !=0 )  {
00110        // check if is is registered with the Menu
00111        if( Menu::instance()->strike((char) ret)) return;
00112 
00113        char command[] = {(char)ret,'\0'};
00114        ret =disp.do_command(command);
00115        if( ret ==1)
00116             disp.displaycurrent(1);
00117        if( ret==2 )
00118            disp.redisplay();
00119    }
00120 }
00121 
00122 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00123 //               display -- override superclass
00124 void XScene::redisplay()
00125 {
00126   if( !MotifGUI::running ) return;
00127   if( _widget )
00128       _drawer->setDisplay(XtDisplay(_widget),XtWindow(_widget));
00129   _drawer->resize();
00130   _drawer->set_defaults();
00131   SceneControl::redisplay();
00132 }
00133 
00134 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00135 //         This is a displayer for X_Windows Graphics
00136 int XScene::getEvent()
00137 {
00138    static XEvent myevent;
00139    XNextEvent(_display, &myevent);
00140    return processEvent(myevent);
00141 }
00142 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00143 // process events in the window that change or require updates
00144 int XScene::processEvent(XEvent& myevent)
00145 {
00146    char text[2]={'\0','\0'};
00147    int i = 0;
00148    static int buttonState = 0;
00149    static int selectionCleared = 1;
00150    static Xdraw::Rectangle selection;
00151 
00152    switch (myevent.type)
00153       {
00154     case ButtonPress:
00155       if( myevent.xbutton.button!=1 ) break;
00156       selection.x = _drawer->xnorm(myevent.xbutton.x);
00157       selection.y = _drawer->ynorm(myevent.xbutton.y);
00158       buttonState = 1;
00159       break;
00160 
00161     case ButtonRelease:      
00162       if( myevent.xbutton.button!=1 ) break;
00163       selection.dx = _drawer->xnorm(myevent.xbutton.x) - selection.x;
00164       selection.dy = _drawer->ynorm(myevent.xbutton.y) - selection.y;
00165       setViewPort (selection.x, selection.y,
00166                               selection.x + selection.dx,
00167                               selection.y + selection.dy);
00168 
00169      text[0] = 'u';    //force update
00170       i = 1;
00171       buttonState = 0;
00172       selectionCleared = 1;
00173       break;
00174 
00175     case MotionNotify:
00176       if ( buttonState )
00177          {
00178 
00179          // set XOR-mode
00180          _drawer->setXorMode( 1 );
00181 
00182          // delete old rectangle by drawing it again
00183          if (!selectionCleared)
00184             _drawer->drawRect( selection );
00185 
00186          // calc new selection
00187          selection.dx = _drawer->xnorm(myevent.xbutton.x) - selection.x;
00188          selection.dy = _drawer->ynorm(myevent.xbutton.y) - selection.y;
00189 
00190          // draw new rectangle
00191          _drawer->drawRect( selection );
00192          selectionCleared = 0;
00193 
00194          // clear XOR-mode
00195          _drawer->setXorMode( 0 );
00196          }
00197       break;
00198 
00199     case KeyPress:
00200       KeySym mykey;
00201       i = XLookupString ((XKeyEvent*)&myevent, text, 10, &mykey, 0);
00202       if(i==0){ // special key?
00203         i = 1;
00204         switch(mykey){ // if one of these, make translation
00205         case XK_Left:  text[0]='b'; break;
00206         case XK_Right: text[0]='f'; break;
00207         case XK_Up:    text[0]='p'; break;
00208         case XK_Down:  text[0]='n'; break;
00209         case XK_Home:  text[0]='r'; break;
00210         case XK_Prior: text[0]='m'; break;
00211         case XK_Next:  text[0]='s'; break;
00212         default: i=0;
00213         }
00214       }
00215       break;
00216 
00217     case VisibilityNotify:
00218       break;
00219 
00220     case Expose:
00221       {
00222       text[0]='u'; i=1;
00223       }
00224       break;
00225 
00226     case MappingNotify:
00227        XRefreshKeyboardMapping ((XMappingEvent*)&myevent);
00228       break;
00229       }   // end of    switch (myevent.type)
00230 
00231 
00232    if ( i == 0 )
00233       return 0;
00234    else
00235       return text[0];
00236 
00237 }
00238 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00239 //                   interactive viewer loop
00240 int XScene::viewit()
00241 {
00242    print_commands(std::cout);
00243    std::cout << "q to quit\n";
00244    redisplay();
00245 
00246    for (;;)
00247    {
00248      int command =  getEvent();
00249 
00250      switch(command)
00251      {
00252        case 0:   break;
00253 
00254        case 'q': return 0;
00255        case 't': return 1;
00256        case 'g': return 2;
00257        default:    //not recognized here: let SceneControl handle it
00258          char cmdstring[]={command,'\0'};
00259          int j= do_command(cmdstring);
00260          if(j==1) displaycurrent(1);
00261          if(j==2) redisplay();
00262       }        // end of switch (command)
00263 
00264    }   // end of the for (;;)
00265 
00266    return 0;
00267 }
00268 
00269 #endif // NT_MSVCPP

Generated at Mon Nov 26 18:18:12 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000