00001 /************************************************************************ 00002 filename: CEGUITitlebar.cpp 00003 created: 25/4/2004 00004 author: Paul D Turner 00005 00006 purpose: Implementation of common Titlebar parts. 00007 *************************************************************************/ 00008 /************************************************************************* 00009 Crazy Eddie's GUI System (http://www.cegui.org.uk) 00010 Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk) 00011 00012 This library is free software; you can redistribute it and/or 00013 modify it under the terms of the GNU Lesser General Public 00014 License as published by the Free Software Foundation; either 00015 version 2.1 of the License, or (at your option) any later version. 00016 00017 This library is distributed in the hope that it will be useful, 00018 but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00020 Lesser General Public License for more details. 00021 00022 You should have received a copy of the GNU Lesser General Public 00023 License along with this library; if not, write to the Free Software 00024 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00025 *************************************************************************/ 00026 #include "elements/CEGUITitlebar.h" 00027 #include "elements/CEGUIFrameWindow.h" 00028 #include "CEGUIMouseCursor.h" 00029 00030 // Start of CEGUI namespace section 00031 namespace CEGUI 00032 { 00033 const String Titlebar::EventNamespace("Titlebar"); 00034 00035 /************************************************************************* 00036 Definition of Properties for this class 00037 *************************************************************************/ 00038 TitlebarProperties::DraggingEnabled Titlebar::d_dragEnabledProperty; 00039 TitlebarProperties::CaptionColour Titlebar::d_captionColourProperty; 00040 00041 00042 /************************************************************************* 00043 Constructor 00044 *************************************************************************/ 00045 Titlebar::Titlebar(const String& type, const String& name) : 00046 Window(type, name) 00047 { 00048 addTitlebarProperties(); 00049 00050 setAlwaysOnTop(true); 00051 00052 // basic initialisation 00053 d_dragging = false; 00054 d_dragEnabled = true; 00055 } 00056 00057 /************************************************************************* 00058 Destructor 00059 *************************************************************************/ 00060 Titlebar::~Titlebar(void) 00061 { 00062 } 00063 00064 00065 /************************************************************************* 00066 Return whether this title bar will respond to dragging. 00067 *************************************************************************/ 00068 bool Titlebar::isDraggingEnabled(void) const 00069 { 00070 return d_dragEnabled; 00071 } 00072 00073 00074 /************************************************************************* 00075 Set whether this title bar widget will respond to dragging. 00076 *************************************************************************/ 00077 void Titlebar::setDraggingEnabled(bool setting) 00078 { 00079 if (d_dragEnabled != setting) 00080 { 00081 d_dragEnabled = setting; 00082 00083 // stop dragging now if the setting has been disabled. 00084 if ((!d_dragEnabled) && d_dragging) 00085 { 00086 releaseInput(); 00087 } 00088 00089 // call event handler. 00090 WindowEventArgs args(this); 00091 onDraggingModeChanged(args); 00092 } 00093 00094 } 00095 00096 00097 /************************************************************************* 00098 Handler for mouse movement events 00099 *************************************************************************/ 00100 void Titlebar::onMouseMove(MouseEventArgs& e) 00101 { 00102 // Base class processing. 00103 Window::onMouseMove(e); 00104 00105 if (d_dragging && (d_parent != NULL)) 00106 { 00107 Vector2 delta(screenToWindow(e.position)); 00108 00109 if (getMetricsMode() == Relative) 00110 { 00111 delta = relativeToAbsolute(delta); 00112 } 00113 00114 // calculate amount that window has been moved 00115 delta -= d_dragPoint; 00116 00117 // move the window. *** Again: Titlebar objects should only be attached to FrameWindow derived classes. *** 00118 ((FrameWindow*)d_parent)->offsetPixelPosition(delta); 00119 00120 e.handled = true; 00121 } 00122 } 00123 00124 00125 /************************************************************************* 00126 Handler for mouse button press events 00127 *************************************************************************/ 00128 void Titlebar::onMouseButtonDown(MouseEventArgs& e) 00129 { 00130 // Base class processing 00131 Window::onMouseButtonDown(e); 00132 00133 if (e.button == LeftButton) 00134 { 00135 if ((d_parent != NULL) && d_dragEnabled) 00136 { 00137 // we want all mouse inputs from now on 00138 if (captureInput()) 00139 { 00140 // initialise the dragging state 00141 d_dragging = true; 00142 d_dragPoint = screenToWindow(e.position); 00143 00144 if (getMetricsMode() == Relative) 00145 { 00146 d_dragPoint = relativeToAbsolute(d_dragPoint); 00147 } 00148 00149 // store old constraint area 00150 d_oldCursorArea = MouseCursor::getSingleton().getConstraintArea(); 00151 00152 // setup new constraint area to be the intersection of the old area and our grand-parent's clipped inner-area 00153 Rect constrainArea; 00154 00155 if ((d_parent == NULL) || (d_parent->getParent() == NULL)) 00156 { 00157 constrainArea = System::getSingleton().getRenderer()->getRect().getIntersection(d_oldCursorArea); 00158 } 00159 else 00160 { 00161 constrainArea = d_parent->getParent()->getInnerRect().getIntersection(d_oldCursorArea); 00162 } 00163 00164 MouseCursor::getSingleton().setConstraintArea(&constrainArea); 00165 } 00166 00167 } 00168 00169 e.handled = true; 00170 } 00171 } 00172 00173 00174 /************************************************************************* 00175 Handler for mouse button release events 00176 *************************************************************************/ 00177 void Titlebar::onMouseButtonUp(MouseEventArgs& e) 00178 { 00179 // Base class processing 00180 Window::onMouseButtonUp(e); 00181 00182 if (e.button == LeftButton) 00183 { 00184 releaseInput(); 00185 e.handled = true; 00186 } 00187 00188 } 00189 00190 00191 /************************************************************************* 00192 Handler for mouse button double-click events 00193 *************************************************************************/ 00194 void Titlebar::onMouseDoubleClicked(MouseEventArgs& e) 00195 { 00196 // Base class processing 00197 Window::onMouseDoubleClicked(e); 00198 00199 if (e.button == LeftButton) 00200 { 00201 // if we do not have a parent window, then obviously nothing should happen. 00202 if (d_parent != NULL) 00203 { 00204 // we should only ever be attached to a FrameWindow (or derived) class 00205 ((FrameWindow*)d_parent)->toggleRollup(); 00206 } 00207 00208 e.handled = true; 00209 } 00210 00211 } 00212 00213 00214 /************************************************************************* 00215 Handler for if the window loses capture of the mouse. 00216 *************************************************************************/ 00217 void Titlebar::onCaptureLost(WindowEventArgs& e) 00218 { 00219 // Base class processing 00220 Window::onCaptureLost(e); 00221 00222 // when we lose out hold on the mouse inputs, we are no longer dragging. 00223 d_dragging = false; 00224 00225 // restore old constraint area 00226 MouseCursor::getSingleton().setConstraintArea(&d_oldCursorArea); 00227 } 00228 00229 00230 /************************************************************************* 00231 Add title bar specific properties 00232 *************************************************************************/ 00233 void Titlebar::addTitlebarProperties(void) 00234 { 00235 addProperty(&d_dragEnabledProperty); 00236 addProperty(&d_captionColourProperty); 00237 } 00238 00239 00240 /************************************************************************* 00241 Return the current colour used for rendering the caption text 00242 *************************************************************************/ 00243 colour Titlebar::getCaptionColour(void) const 00244 { 00245 return d_captionColour; 00246 } 00247 00248 00249 /************************************************************************* 00250 Sets the colour to be used for rendering the caption text. 00251 *************************************************************************/ 00252 void Titlebar::setCaptionColour(const colour& col) 00253 { 00254 d_captionColour = col; 00255 requestRedraw(); 00256 } 00257 00258 } // End of CEGUI namespace section