Window.h

Go to the documentation of this file.
00001 //**********************************************************************
00002 //
00003 //  REGCOM: Regimental Command
00004 //  Copyright (C) 2008 Randi J. Relander
00005 //    <rjrelander@users.sourceforge.net>
00006 //
00007 //  This program is free software; you can redistribute it and/or
00008 //  modify it under the terms of the GNU General Public License
00009 //  as published by the Free Software Foundation; either version 3
00010 //  of the License, or (at your option) any later version.
00011 //  
00012 //  This program is distributed in the hope that it will be useful,
00013 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 //  GNU General Public License for more details.
00016 //  
00017 //  You should have received a copy of the GNU General Public
00018 //  License along with this program.  If not, see 
00019 //  <http://www.gnu.org/licenses/>.
00020 //  
00021 //**********************************************************************
00022 
00023 #ifndef REGCOM_GUI_WINDOW_H
00024 #define REGCOM_GUI_WINDOW_H
00025 
00026 // SYSTEM INCLUDES
00027 #include <string>
00028 
00029 // LIBRARY INCLUDES
00030 #include "SDL.h"
00031 
00032 // PROJECT INCLUDES
00033 #include "regcom/gui/Rect.h"
00034 #include "regcom/gui/Point.h"
00035 
00036 // NAMESPACES
00037 namespace regcom {
00038 namespace gui {
00039 
00040 // FORWARD REFERENCES
00041 class Surface;
00042 class Color;
00043 
00045 //  CLASS: Window
00051 class Window
00052 {
00053 public:
00054 
00055     // PUBLIC METHODS
00056 
00058     Window();
00059 
00061     virtual ~Window();
00062 
00064     virtual void Update(Rect rect);
00065 
00067     virtual void UpdateChild(Window* child, Rect rect);
00068 
00070     virtual void Draw(Rect rect);
00071     
00073     void DrawSurface(Surface* surface, Rect rect);
00074 
00076     void DrawSurface(Surface* surface, Rect src, int x, int y);
00077 
00079     void FillRect(const Rect& rRect, const Color& rColor);
00080 
00082     virtual void OnMouseMove(Point point);
00083 
00085     virtual void OnMouseDown(int n, Point point);
00086 
00088     virtual void OnMouseUp(int n, Point point);
00089 
00091     virtual void Invalidate(Rect rect);
00092 
00094     virtual void Invalidate();
00095 
00097     Rect Bounds() {return Rect(0,0,m_Frame.w,m_Frame.h); }
00098 
00100     Rect Frame() { return m_Frame; }
00101 
00103     void ResizeTo(int w, int h);
00104 
00106     void ResizeBy(int w, int h);
00107 
00109     void OffsetTo(int x, int y);
00110 
00112     void OffsetBy(int x, int y);
00113 
00115     virtual void FrameMoved(Point point);
00116 
00118     virtual void FrameResized(int w, int h);
00119 
00121     int Width() { return m_Frame.w; }
00122 
00124     int Height() { return m_Frame.h; }
00125 
00127     virtual int Contains(Point point) { return m_Frame.Contains(point); }
00128 
00130     void AddChild(Window* window);
00131 
00133     virtual Window* FindWindow(Point point);
00134 
00136     Point ConvertFromScreen(Point point)
00137     {
00138         int x = point.GetX() - m_Frame.x;
00139         int y = point.GetY() - m_Frame.y;
00140         
00141         if (m_pParent)
00142             return m_pParent->ConvertFromScreen(Point(x,y));
00143         else return Point(x,y);
00144     }
00145 
00147     Point ConvertToScreen(Point point)
00148     {
00149         int x = point.GetX() + m_Frame.x;
00150         int y = point.GetY() + m_Frame.y;
00151         
00152         if (m_pParent)
00153             return m_pParent->ConvertToScreen(Point(x,y));
00154         else return Point(x,y);
00155     }
00156 
00158     void CaptureMouse(Rect rect);
00159 
00161     void ReleaseMouse();
00162 
00164     Window* GetCaptureView();
00165 
00167     Rect GetCaptureRect();
00168 
00170     void SetMouseOverride(bool override);
00171 
00173     bool GetMouseOverride();
00174 
00176     void SetMousePosition(Point position);
00177 
00179     Point GetMousePosition();
00180 
00182     Window* GetParent() { return m_pParent; }
00183 
00185     void SetParent(Window* window) { m_pParent = window; }
00186 
00188     virtual bool CanFocus() { return false; }
00189 
00191     virtual void OnFocus(bool hasFocus) { }
00192 
00194     virtual void OnMouseButtonDown(SDL_MouseButtonEvent sdlEvent) { }
00195 
00197     virtual void OnMouseButtonUp(SDL_MouseButtonEvent sdlEvent) { }
00198 
00200     virtual void OnKeyDown(SDL_KeyboardEvent sdlEvent) { }
00201 
00203     virtual void OnKeyUp(SDL_KeyboardEvent sdlEvent) { }
00204 
00206     virtual void OnTimer(float deltaTime);
00207 
00209     Window* GetNextWindow()
00210     {
00211         if (m_pFirstChild) return m_pFirstChild;
00212         if (m_pNextSibling) return m_pNextSibling;
00213 
00214         Window* pWindow = this;
00215 
00216         while (pWindow->m_pParent)
00217         {
00218             pWindow = pWindow->m_pParent;
00219 
00220             if (pWindow->m_pNextSibling) return pWindow->m_pNextSibling;
00221         }
00222 
00223         return pWindow;
00224     }
00225 
00226 private:
00227 
00228     // MEMBER VARIABLES
00229 
00231     Rect m_Frame;
00232 
00234     static Window* s_pCaptureView;
00235 
00237     static Rect s_CaptureRect;
00238 
00240     static Point s_MousePosition;
00241 
00243     static bool s_MouseOverride;
00244 
00246     Window* m_pParent;
00247 
00249     Window* m_pFirstChild;
00250 
00252     Window* m_pLastChild;
00253 
00255     Window* m_pPrevSibling;
00256 
00258     Window* m_pNextSibling;
00259 };
00260 
00261 } // namespace gui
00262 } // namespace regcom
00263 
00264 #endif