/*
3D C++ Toolkit (TDT)
Application Class
Copyright (c) 2008
John Ryland
*/
#include <TDT/application.h>
#include <TDT/window.h>
#include <GLUT/glut.h>
using namespace TDT;
Window::Window()
{
init(100, 100, 640, 480);
mWinId = glutCreateWindow("Window");
Application::instance()->insertWindow(this);
Application::instance()->installCallbacks();
setWindowTitle("Window");
setWindowIconText("Icon text");
}
Window::~Window()
{
glutDestroyWindow(winId());
Application::instance()->removeWindow(this);
}
void Window::init(int x, int y, int w, int h)
{
mX = x;
mY = y;
mWidth = w;
mHeight = h;
mVisible = true;
mMinimized = false;
mMaximized = false;
mFullScreen = false;
mBackingStore = malloc(sizeof(int)*mWidth*mHeight);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(mX, mY);
glutInitWindowSize(mWidth, mHeight);
}
void Window::setWindowTitle(const char *title)
{
mWindowTitle = title;
glutSetWindow(winId());
glutSetWindowTitle(title);
}
void Window::setWindowIconText(const char *text)
{
mWindowIconText = text;
glutSetWindow(winId());
glutSetIconTitle(text);
}
void Window::move(int x, int y)
{
glutSetWindow(winId());
if ( mX != x || mY != y ) {
mX = x;
mY = y;
glutPositionWindow(x, y);
}
}
void Window::resize(int width, int height)
{
glutSetWindow(winId());
if ( mWidth != width || mHeight != height ) {
mWidth = width;
mHeight = height;
glutReshapeWindow(width, height);
}
}
void Window::setGeometry(int x, int y, int width, int height)
{
resize(width, height);
move(x, y);
}
void Window::show()
{
setVisible(true);
}
void Window::hide()
{
setVisible(false);
}
void Window::setVisible(bool visible)
{
if ( visible == mVisible )
return;
mVisible = visible;
glutSetWindow(winId());
if ( visible )
glutShowWindow();
else
glutHideWindow();
}
void Window::showNormal()
{
if ( mMinimized || mMaximized || mFullScreen )
setGeometry(mRestoreX, mRestoreY, mRestoreWidth, mRestoreHeight);
}
void Window::saveGeometry()
{
mRestoreX = mX;
mRestoreY = mY;
mRestoreWidth = mWidth;
mRestoreHeight = mHeight;
mMinimized = false;
mMaximized = false;
mFullScreen = false;
}
void Window::showMinimized()
{
saveGeometry();
glutSetWindow(winId());
glutIconifyWindow();
mMinimized = true;
}
void Window::showMaximized()
{
saveGeometry();
glutSetWindow(winId());
setGeometry(0, 0, glutGet(GLUT_SCREEN_WIDTH), glutGet(GLUT_SCREEN_HEIGHT));
mMaximized = true;
}
void Window::showFullScreen()
{
saveGeometry();
glutSetWindow(winId());
glutFullScreen();
mFullScreen = true;
}
void Window::repaint()
{
glutSetWindow(winId());
glutPostRedisplay();
}
void Window::setCursor(int cursor)
{
mCursor = cursor;
glutSetWindow(winId());
glutSetCursor(cursor);
}
bool Window::event(Event ev)
{
switch (ev.type()) {
case Event::Paint:
paintEvent();
break;
case Event::Show:
showEvent();
break;
case Event::Hide:
hideEvent();
break;
case Event::Close:
glutDestroyWindow(winId());
closeEvent();
break;
case Event::Resize:
mX = glutGet(GLUT_WINDOW_X);
mY = glutGet(GLUT_WINDOW_Y);
mWidth = ev.width();
mHeight = ev.height();
mBackingStore = realloc(mBackingStore, sizeof(int) * mWidth * mHeight);
resizeEvent(ev.width(), ev.height());
break;
case Event::KeyPress:
keyPressEvent(ev.key(), ev.x(), ev.y());
break;
case Event::KeyRelease:
keyReleaseEvent(ev.key(), ev.x(), ev.y());
break;
case Event::MousePress:
mousePressEvent(ev.button(), ev.x(), ev.y());
break;
case Event::MouseRelease:
mouseReleaseEvent(ev.button(), ev.x(), ev.y());
break;
case Event::MouseMove:
mouseMoveEvent(ev.x(), ev.y());
break;
case Event::FocusIn:
focusInEvent();
break;
case Event::FocusOut:
focusOutEvent();
break;
case Event::Idle:
idleEvent();
break;
case Event::Timer:
timerEvent(ev.timerId());
break;
case Event::OverlayDisplay:
overlayDisplayedEvent();
break;
default:
return false;
}
return true;
}
void Window::paintEvent()
{
glutSwapBuffers();
}
void Window::showEvent()
{
}
void Window::hideEvent()
{
}
void Window::closeEvent()
{
}
void Window::resizeEvent(int width, int height)
{
}
void Window::keyPressEvent(int key, int x, int y)
{
}
void Window::keyReleaseEvent(int key, int x, int y)
{
}
void Window::mousePressEvent(int button, int x, int y)
{
}
void Window::mouseReleaseEvent(int button, int x, int y)
{
}
void Window::mouseMoveEvent(int x, int y)
{
}
void Window::focusInEvent()
{
}
void Window::focusOutEvent()
{
}
void Window::idleEvent()
{
}
void Window::timerEvent(int timerId)
{
}
void Window::overlayDisplayedEvent()
{
}