core/vgui/vgui_enhance_tableau.cxx
Go to the documentation of this file.
00001 // This is core/vgui/vgui_enhance_tableau.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \brief  See vgui_enhance_tableau.h for a description of this file.
00008 // \author Philip C. Pritchett, RRG, University of Oxford
00009 // \date   17 Nov 1999
00010 //
00011 // \verbatim
00012 //  Modifications
00013 //   17-NOV-1999 P.Pritchett - Initial version.
00014 // \endverbatim
00015 
00016 #include "vgui_enhance_tableau.h"
00017 
00018 #include <vcl_iostream.h>
00019 
00020 #include <vnl/vnl_matrix_fixed.h>
00021 
00022 #include <vgui/vgui_gl.h>
00023 #include <vgui/vgui_tableau.h>
00024 #include <vgui/vgui_event.h>
00025 #include <vgui/vgui_matrix_state.h>
00026 
00027 
00028 vgui_enhance_tableau::vgui_enhance_tableau()
00029   : slot1(this)
00030   , slot2(this)
00031   , enhancing_(false)
00032   , size(50)
00033   , zoom_factor(1.5)
00034   , enable_key_bindings(false)
00035 {
00036 }
00037 
00038 vgui_enhance_tableau::vgui_enhance_tableau(vgui_tableau_sptr const&t)
00039   : slot1(this,t)
00040   , slot2(this)
00041   , enhancing_(false)
00042   , size(50)
00043   , zoom_factor(1.5)
00044   , enable_key_bindings(false)
00045 {
00046 }
00047 
00048 vgui_enhance_tableau::vgui_enhance_tableau(vgui_tableau_sptr const&t1, vgui_tableau_sptr const&t2)
00049   : slot1(this,t1)
00050   , slot2(this,t2)
00051   , enhancing_(false)
00052   , size(50)
00053   , zoom_factor(1.0)
00054   , enable_key_bindings(false)
00055 {
00056 }
00057 
00058 vgui_enhance_tableau::~vgui_enhance_tableau()
00059 {
00060 }
00061 
00062 void vgui_enhance_tableau::set_child(vgui_tableau_sptr const&t)
00063 {
00064   slot1 = vgui_parent_child_link(this, t);
00065 }
00066 
00067 vcl_string vgui_enhance_tableau::file_name() const {return slot1->file_name();}
00068 vcl_string vgui_enhance_tableau::type_name() const {return "vgui_enhance_tableau";}
00069 
00070 
00071 bool vgui_enhance_tableau::handle(const vgui_event& e)
00072 {
00073   if (!enhancing_ && e.type == vgui_BUTTON_DOWN && e.button == vgui_LEFT)
00074   {
00075     enhancing_ = true;
00076     x = (int)e.wx;
00077     y = (int)e.wy;
00078     post_redraw();
00079     return true;
00080   }
00081 
00082   if (enhancing_ && e.type == vgui_BUTTON_UP && e.button == vgui_LEFT)
00083   {
00084     enhancing_ = false;
00085     post_redraw();
00086     return true;
00087   }
00088 
00089   if (enhancing_ && e.type == vgui_MOTION)
00090   {
00091     x = (int)e.wx;
00092     y = (int)e.wy;
00093     post_redraw();
00094     return true;
00095   }
00096 
00097   if (enable_key_bindings && e.type == vgui_KEY_PRESS)
00098   {
00099     switch (e.key)
00100     {
00101      case '[':
00102       size -= 10;
00103       size = (size <10) ? 10 : size;
00104       post_redraw();
00105       return true;
00106      case ']':
00107       size += 10;
00108       post_redraw();
00109       return true;
00110      case '{':
00111       zoom_factor -= 0.1f;
00112       vcl_cerr << "enhance : zoom_factor = " << zoom_factor << vcl_endl;
00113       post_redraw();
00114       return true;
00115      case '}':
00116       zoom_factor += 0.1f;
00117       vcl_cerr << "enhance : zoom_factor = " << zoom_factor << vcl_endl;
00118       post_redraw();
00119       return true;
00120      default:
00121       break; // quell warning
00122     };
00123   }
00124 
00125   if (e.type == vgui_DRAW)
00126   {
00127     // first draw the child
00128     slot1->handle(e);
00129 
00130     if (enhancing_)
00131     {
00132       // get original offsets and scales
00133       vgui_matrix_state ms;
00134 #if defined(VCL_SGI_CC)
00135       // expect the spurious warning:
00136       //"vgui_enhance_tableau.cxx", line 113: remark(1552): variable "ms" was set but never used
00137       // here.
00138       {
00139         vgui_matrix_state *ptr = &ms;
00140         ptr = ptr;
00141       }
00142 #endif
00143       vnl_matrix_fixed<double,4,4> M = ms.modelview_matrix();
00144       float sx = (float)M(0,0);
00145       float sy = (float)M(0,0);
00146       float ox = (float)M(0,3);
00147       float oy = (float)M(1,3);
00148 
00149       glEnable(GL_SCISSOR_TEST);
00150       int size_2 = size+size;
00151       glScissor(x-size, y-size, size_2, size_2);
00152 
00153       glMatrixMode(GL_MODELVIEW);
00154       glPushMatrix();
00155 
00156       glLoadIdentity();
00157 
00158       sx *= zoom_factor;
00159       sy *= zoom_factor;
00160 
00161       GLint vp[4]; glGetIntegerv(GL_VIEWPORT,vp);
00162       float dx = (        x) - ox;
00163       float dy = (vp[3]-1-y) - oy;
00164       float tmpx = zoom_factor*dx - dx;
00165       float tmpy = zoom_factor*dy - dy;
00166 
00167       glTranslatef(ox-tmpx, oy-tmpy, 0);
00168       glScalef(sx, sy, 1);
00169 
00170       if (slot2.child())
00171         slot2->handle(e);
00172       else
00173         slot1->handle(e);
00174 
00175       glMatrixMode(GL_MODELVIEW);
00176       glPopMatrix();
00177       glDisable(GL_SCISSOR_TEST);
00178     }
00179     return true;
00180   }
00181   bool retv = slot1->handle(e);
00182   if (!retv && slot2)
00183     retv = slot2->handle(e);
00184 
00185   return retv;
00186 }