GLSubWindow.cpp

00001 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
00002 /* 
00003  * Copyright (C) 2008 Micha Hersch, EPFL
00004  * RobotCub Consortium, European Commission FP6 Project IST-004370
00005  * email:   micha.hersch@robotcub.org
00006  * website: www.robotcub.org
00007  * Permission is granted to copy, distribute, and/or modify this program
00008  * under the terms of the GNU General Public License, version 2 or any
00009  * later version published by the Free Software Foundation.
00010  *
00011  * A copy of the license can be found at
00012  * http://www.robotcub.org/icub/license/gpl.txt
00013  *
00014  * This program is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00017  * Public License for more details
00018  */
00019 #include "GLSubWindow.h"
00020 #include "GLTools.h"
00021 
00022 #define TRUE  1
00023 #define FALSE 0
00024 
00025 Rect::Rect(){
00026   Set(0,0,0,0);
00027 }
00028 
00029 Rect::Rect(int x, int y, int w, int h){
00030   Set(x,y,w,h);
00031 }
00032 
00033 void Rect::Set(int x, int y, int w, int h){
00034   m_X       = x;
00035   m_Y       = y; 
00036   m_Width   = w; 
00037   m_Height  = h;
00038 }
00039 void Rect::Copy(pRect rect){
00040   if(rect!=NULL)
00041     Set(rect->m_X,rect->m_Y,rect->m_Width,rect->m_Height);
00042 }
00043 
00044 bool Rect::IsInside(int px, int py){
00045   return ((px>=m_X)&&(px<=m_X+m_Width)&&(py>=m_Y)&&(py<=m_Y+m_Height));
00046 }
00047 
00048 
00049 GLSubWindow::GLSubWindow(pGLSubWindow parentWindow){
00050   m_ParentWindow = parentWindow;
00051   SetSize(100,100);
00052   SetPos(10,10);
00053   m_Miminized = false;
00054   m_BackgroundColor[0]  = 0.0;
00055   m_BackgroundColor[1]  = 0.0;
00056   m_BackgroundColor[2]  = 0.0;
00057   m_BackgroundAlpha     = 0.8;
00058   m_Title               = "---";
00059   m_Move                = false;
00060   m_MoveStartX          = 0;
00061   m_MoveStartY          = 0;
00062   m_MouseButton         = -1;
00063   m_Resize              = false;
00064   m_ResizeStartX        = 0;
00065   m_ResizeStartY        = 0;
00066   m_HasFocus            = false;
00067   if(parentWindow!=NULL)
00068     parentWindow->m_ChildWindows.push_back(this);
00069 }
00070 
00071 GLSubWindow::~GLSubWindow(){
00072   int i;
00073   for(i=0;i<m_ChildWindows.size();i++){
00074     delete m_ChildWindows[i];
00075   }
00076 }
00077 void  GLSubWindow::SetTitle(string title){
00078   m_Title = title;
00079 }
00080 void  GLSubWindow::GetAbsoluteRect(pRect rect){
00081   if(m_ParentWindow!=NULL){
00082     m_ParentWindow->GetAbsoluteRect(rect);
00083     rect->m_X += m_WindowRect.m_X;
00084     rect->m_Y += m_WindowRect.m_Y;
00085   }else{
00086     rect->m_X = m_WindowRect.m_X;
00087     rect->m_Y = m_WindowRect.m_Y;
00088   }
00089   rect->m_Width   = m_WindowRect.m_Width;
00090   rect->m_Height  = m_WindowRect.m_Height;
00091 }
00092 
00093 void  GLSubWindow::SetSize(int w, int h){
00094   m_WindowRect.m_Width  = w;
00095   m_WindowRect.m_Height = h;
00096 
00097   m_ClientRect.m_Width  = w;
00098   m_ClientRect.m_Height = h - GLSW_TITLE_HEIGHT;
00099   Resize(w,h - GLSW_TITLE_HEIGHT);
00100 }
00101 void  GLSubWindow::Resize(int w, int h){
00102 }
00103 
00104 void  GLSubWindow::SetPos (int x, int y){
00105   m_WindowRect.m_X = x;
00106   m_WindowRect.m_Y = y;
00107   m_ClientRect.m_X = x;
00108   m_ClientRect.m_Y = y + GLSW_TITLE_HEIGHT;
00109 }
00110 
00111 void  GLSubWindow::Minimize(bool state){
00112   m_Miminized = state;
00113 }
00114 int GLSubWindow::IsParentMinimized(){
00115   if(m_ParentWindow!=NULL){
00116     if(m_ParentWindow->m_Miminized)
00117       return TRUE;
00118     return m_ParentWindow->IsParentMinimized();
00119   }else{
00120     return FALSE;
00121   }
00122 }
00123 void  GLSubWindow::RenderWindow(){
00124 
00125   Rect absRect;
00126   GetAbsoluteRect(&absRect);
00127 
00128   int wHeight = glutGet(GLUT_WINDOW_HEIGHT);
00129 
00130   if(m_Miminized){
00131     glViewport(absRect.m_X,wHeight-(absRect.m_Y+m_WindowRect.m_Height-m_ClientRect.m_Height),m_WindowRect.m_Width,m_WindowRect.m_Height-m_ClientRect.m_Height);
00132   }else{
00133     glViewport(absRect.m_X,wHeight-(absRect.m_Y+m_WindowRect.m_Height),m_WindowRect.m_Width,m_WindowRect.m_Height);
00134   }
00135 
00136         glMatrixMode(GL_PROJECTION);
00137         glPushMatrix();
00138         glLoadIdentity();
00139   if(m_Miminized){
00140           gluOrtho2D(0, m_WindowRect.m_Width, 0, m_WindowRect.m_Height-m_ClientRect.m_Height);
00141   }else{
00142           gluOrtho2D(0, m_WindowRect.m_Width, 0, m_WindowRect.m_Height);
00143   }
00144         glScalef(1.0f, -1.0f, 1.0f);
00145   if(m_Miminized){
00146         glTranslatef(0.0f, -(m_WindowRect.m_Height-m_ClientRect.m_Height)+1, 0.0f);
00147   }else{
00148         glTranslatef(0.0f, -m_WindowRect.m_Height+1, 0.0f);
00149   }
00150         glMatrixMode(GL_MODELVIEW);
00151         glPushMatrix();
00152         glLoadIdentity();
00153 
00154   glDisable(GL_DEPTH_TEST);
00155 
00156   if(!m_Miminized){
00157     glColor4f(m_BackgroundColor[0],
00158               m_BackgroundColor[1],
00159               m_BackgroundColor[2],
00160               m_BackgroundAlpha);
00161     glBegin(GL_QUADS);
00162                   glVertex2i(                      0,                       0);
00163             glVertex2i( m_WindowRect.m_Width-1,                       0);
00164             glVertex2i( m_WindowRect.m_Width-1, m_WindowRect.m_Height-1);
00165             glVertex2i(                      0, m_WindowRect.m_Height-1);
00166     glEnd();
00167   }
00168 
00169   if(m_HasFocus){
00170     glColor4f(0.7f,
00171               m_BackgroundColor[1],
00172               m_BackgroundColor[2],
00173               1.0f);
00174   }else{
00175     glColor4f(m_BackgroundColor[0],
00176               m_BackgroundColor[1],
00177               m_BackgroundColor[2],
00178               m_BackgroundAlpha);
00179   }
00180 
00181   glBegin(GL_QUADS);
00182     glVertex2i(                      0,                                             0);
00183           glVertex2i( m_WindowRect.m_Width-1,                                             0);
00184           glVertex2i( m_WindowRect.m_Width-1, m_WindowRect.m_Height-m_ClientRect.m_Height-1);
00185           glVertex2i(                      0, m_WindowRect.m_Height-m_ClientRect.m_Height-1);
00186   glEnd();
00187   
00188   GLTools::DisplayText(3,m_WindowRect.m_Height-m_ClientRect.m_Height-3,m_Title.c_str());
00189   
00190   glColor4f(0.0,0.0,0.0,1.0);
00191         glBegin(GL_LINE_LOOP);
00192                 glVertex2i(                      0,                                             0);
00193                 glVertex2i( m_WindowRect.m_Width-1,                                             0);
00194     glVertex2i( m_WindowRect.m_Width-1, m_WindowRect.m_Height-m_ClientRect.m_Height-1);
00195     glVertex2i(                      0, m_WindowRect.m_Height-m_ClientRect.m_Height-1);
00196         glEnd();
00197   if(!m_Miminized){
00198           glBegin(GL_LINE_LOOP);
00199                   glVertex2i(                      0,                       0);
00200             glVertex2i( m_WindowRect.m_Width-1,                       0);
00201             glVertex2i( m_WindowRect.m_Width-1, m_WindowRect.m_Height-1);
00202             glVertex2i(                      0, m_WindowRect.m_Height-1);
00203           glEnd();
00204     
00205     glViewport(absRect.m_X,wHeight-(absRect.m_Y+m_WindowRect.m_Height),m_ClientRect.m_Width,m_ClientRect.m_Height);
00206 
00207           glMatrixMode(GL_PROJECTION);
00208           glPushMatrix();
00209           glLoadIdentity();
00210     gluOrtho2D(0, m_ClientRect.m_Width, 0, m_ClientRect.m_Height);
00211           glScalef(1.0f, -1.0f, 1.0f);
00212           glTranslatef(0.0f, -m_ClientRect.m_Height+1, 0.0f);
00213           glMatrixMode(GL_MODELVIEW);
00214           glPushMatrix();
00215           glLoadIdentity();
00216         
00217     Render();
00218 
00219     glMatrixMode(GL_PROJECTION);
00220           glPopMatrix();
00221           glMatrixMode(GL_MODELVIEW);
00222           glPopMatrix();
00223 
00224     for(int i=0;i<m_ChildWindows.size();i++)
00225       m_ChildWindows[i]->RenderWindow();
00226   }
00227 
00228 
00229         glEnable(GL_DEPTH_TEST);
00230 
00231         glMatrixMode(GL_PROJECTION);
00232         glPopMatrix();
00233         glMatrixMode(GL_MODELVIEW);
00234         glPopMatrix();
00235 }
00236 
00237 void  GLSubWindow::Render(){}
00238 void  GLSubWindow::OnNormalKey(char key){}
00239 void  GLSubWindow::OnSpecialKey(int key){}
00240 
00241 int   GLSubWindow::OnClick(int button, int state, int x, int y){  
00242   return FALSE;
00243 }
00244 int  GLSubWindow::OnDoubleClick(int button, int x, int y){
00245   return FALSE;
00246 }
00247 int  GLSubWindow::OnMove(int x, int y){
00248   return FALSE;
00249 }
00250 
00251 
00252 void  GLSubWindow::OnWindowNormalKey(char key){
00253   int i;
00254   for(i=0;i<m_ChildWindows.size();i++)
00255     m_ChildWindows[i]->OnWindowNormalKey(key);
00256 
00257   if(m_HasFocus)
00258     OnNormalKey(key);
00259 }
00260 void  GLSubWindow::OnWindowSpecialKey(int key){
00261   int i;
00262   for(i=0;i<m_ChildWindows.size();i++)
00263     m_ChildWindows[i]->OnWindowSpecialKey(key);
00264 
00265   if(m_HasFocus)
00266     OnSpecialKey(key);
00267 }
00268 
00269 int   GLSubWindow::OnWindowClick(int button, int state, int x, int y){  
00270   int cFlag = FALSE;
00271   int i;
00272 
00273   if(IsParentMinimized())
00274     return FALSE;
00275 
00276   for(i=0;i<m_ChildWindows.size();i++)
00277     cFlag |= m_ChildWindows[i]->OnWindowClick(button,state,x,y);
00278 
00279   Rect rect;
00280   GetAbsoluteRect(&rect);
00281   if(m_Miminized)
00282     rect.m_Height -= m_ClientRect.m_Height;
00283   if(state==GLUT_DOWN){
00284     if(rect.IsInside(x,y)){
00285       m_HasFocus = true & (!cFlag);
00286     }else{
00287       m_HasFocus = false;
00288     }
00289   }
00290 
00291   if(cFlag)
00292     return TRUE;
00293 
00294   
00295   if(state == GLUT_DOWN){
00296     m_MouseButton = button;
00297     GetAbsoluteRect(&rect);
00298     rect.m_Height -= m_ClientRect.m_Height;
00299     if(rect.IsInside(x,y)){
00300       if(m_MouseButton == GLUT_LEFT_BUTTON){    
00301         m_MoveStartX = m_WindowRect.m_X-x;
00302         m_MoveStartY = m_WindowRect.m_Y-y;
00303         m_Move = true;
00304         return TRUE;
00305       }
00306       if(m_MouseButton == GLUT_RIGHT_BUTTON){    
00307         m_Resize = true;
00308         m_MoveStartX    = m_WindowRect.m_X;
00309         m_MoveStartY    = m_WindowRect.m_Y      - y;
00310         m_ResizeStartX  = m_WindowRect.m_Width  - x;
00311         m_ResizeStartY  = m_WindowRect.m_Height + y;
00312         return TRUE;
00313       }
00314     }
00315   }else{
00316     m_MouseButton = -1;
00317     m_Resize      = false;
00318     m_Move        = false;
00319   }
00320 
00321   OnClick(button,state,x,y);
00322 
00323   return FALSE | m_HasFocus;
00324 }
00325 int  GLSubWindow::OnWindowDoubleClick(int button, int x, int y){
00326   int cFlag = FALSE;
00327   int i;
00328   for(i=0;i<m_ChildWindows.size();i++)
00329     cFlag |= m_ChildWindows[i]->OnWindowDoubleClick(button,x,y);
00330 
00331   if(cFlag)
00332     return TRUE;
00333 
00334   if(button == GLUT_LEFT_BUTTON){
00335     Rect rect;
00336     GetAbsoluteRect(&rect);//.Copy(&m_WindowRect);
00337     rect.m_Height -= m_ClientRect.m_Height;
00338     if(rect.IsInside(x,y)){
00339       Minimize(!m_Miminized);
00340       return TRUE;
00341     }
00342   }
00343   OnDoubleClick(button,x,y);
00344   return FALSE;
00345 }
00346 
00347 int  GLSubWindow::OnWindowMove(int x, int y){
00348   int cFlag = FALSE;
00349   int i;
00350   for(i=0;i<m_ChildWindows.size();i++)
00351     cFlag |= m_ChildWindows[i]->OnWindowMove(x,y);
00352 
00353   if(cFlag)
00354     return TRUE;
00355 
00356   if(m_Move){
00357     SetPos(m_MoveStartX+x,m_MoveStartY+y);
00358     return TRUE;
00359   }
00360 
00361   if(m_Resize && !m_Miminized){
00362     SetPos(m_MoveStartX,m_MoveStartY+y);
00363     SetSize(m_ResizeStartX+x,m_ResizeStartY-y);
00364     return TRUE;
00365   }
00366   return FALSE;
00367 }
 All Data Structures Functions Variables

Generated on Wed Sep 22 16:51:26 2010 for Body_Schema_Learning by  doxygen 1.6.1