00001 /************************************************************************ 00002 filename: CEGUIComboDropList.cpp 00003 created: 13/6/2004 00004 author: Paul D Turner 00005 00006 purpose: Implements the Combobox Drop-List widget base class 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/CEGUIComboDropList.h" 00027 #include "elements/CEGUIScrollbar.h" 00028 00029 00030 // Start of CEGUI namespace section 00031 namespace CEGUI 00032 { 00033 const String ComboDropList::EventNamespace("ComboDropList"); 00034 00035 /************************************************************************* 00036 Constants 00037 *************************************************************************/ 00038 // Event names 00039 const String ComboDropList::EventListSelectionAccepted( (utf8*)"ListSelectionAccepted" ); 00040 00041 00042 /************************************************************************* 00043 Constructor for ComboDropList base class 00044 *************************************************************************/ 00045 ComboDropList::ComboDropList(const String& type, const String& name) : 00046 Listbox(type, name) 00047 { 00048 d_autoArm = false; 00049 d_armed = false; 00050 00051 addComboDropListEvents(); 00052 hide(); 00053 00054 // pass captured inputs to children to enable scrollbars 00055 setDistributesCapturedInputs(true); 00056 } 00057 00058 00059 /************************************************************************* 00060 Destructor for ComboDropList base class 00061 *************************************************************************/ 00062 ComboDropList::~ComboDropList(void) 00063 { 00064 } 00065 00066 00067 /************************************************************************* 00068 Initialise the Window based object ready for use. 00069 *************************************************************************/ 00070 void ComboDropList::initialise(void) 00071 { 00072 Listbox::initialise(); 00073 00074 // set-up scroll bars so they return capture to us. 00075 d_vertScrollbar->setRestoreCapture(true); 00076 d_horzScrollbar->setRestoreCapture(true); 00077 } 00078 00079 00080 /************************************************************************* 00081 Add drop-list specific events 00082 *************************************************************************/ 00083 void ComboDropList::addComboDropListEvents(void) 00084 { 00085 addEvent(EventListSelectionAccepted); 00086 } 00087 00088 00089 /************************************************************************* 00090 Handler for when list selection is confirmed. 00091 *************************************************************************/ 00092 void ComboDropList::onListSelectionAccepted(WindowEventArgs& e) 00093 { 00094 fireEvent(EventListSelectionAccepted, e, EventNamespace); 00095 } 00096 00097 00098 /************************************************************************* 00099 Handler for mouse movement events 00100 *************************************************************************/ 00101 void ComboDropList::onMouseMove(MouseEventArgs& e) 00102 { 00103 Listbox::onMouseMove(e); 00104 00105 // if mouse is within our area (but not our children) 00106 if (isHit(e.position)) 00107 { 00108 if (getChildAtPosition(e.position) == NULL) 00109 { 00110 // handle auto-arm 00111 if (d_autoArm) 00112 { 00113 d_armed = true; 00114 } 00115 00116 if (d_armed) 00117 { 00118 // 00119 // Convert mouse position to absolute window pixels 00120 // 00121 Point localPos(screenToWindow(e.position)); 00122 00123 if (getMetricsMode() == Relative) 00124 { 00125 localPos = relativeToAbsolute(localPos); 00126 } 00127 00128 // check for an item under the mouse 00129 ListboxItem* selItem = getItemAtPoint(localPos); 00130 00131 // if an item is under mouse, select it 00132 if (selItem != NULL) 00133 { 00134 setItemSelectState(selItem, true); 00135 } 00136 else 00137 { 00138 clearAllSelections(); 00139 } 00140 00141 } 00142 } 00143 00144 e.handled = true; 00145 } 00146 // not within the list area 00147 else 00148 { 00149 // if left mouse button is down, clear any selection 00150 if (e.sysKeys & LeftMouse) 00151 { 00152 clearAllSelections(); 00153 } 00154 00155 } 00156 00157 } 00158 00159 00160 /************************************************************************* 00161 Handler for mouse button pressed events 00162 *************************************************************************/ 00163 void ComboDropList::onMouseButtonDown(MouseEventArgs& e) 00164 { 00165 Listbox::onMouseButtonDown(e); 00166 00167 if (e.button == LeftButton) 00168 { 00169 if (!isHit(e.position)) 00170 { 00171 clearAllSelections(); 00172 releaseInput(); 00173 } 00174 else 00175 { 00176 d_armed = true; 00177 } 00178 00179 e.handled = true; 00180 } 00181 00182 } 00183 00184 00185 /************************************************************************* 00186 Handler for mouse button release events 00187 *************************************************************************/ 00188 void ComboDropList::onMouseButtonUp(MouseEventArgs& e) 00189 { 00190 Listbox::onMouseButtonUp(e); 00191 00192 if (e.button == LeftButton) 00193 { 00194 if (d_armed && (getChildAtPosition(e.position) == NULL)) 00195 { 00196 releaseInput(); 00197 00198 // if something was selected, confirm that selection. 00199 if (getSelectedCount() > 0) 00200 { 00201 WindowEventArgs args(this); 00202 onListSelectionAccepted(args); 00203 } 00204 00205 } 00206 // if we are not already armed, in response to a left button up event, we auto-arm. 00207 else 00208 { 00209 d_armed = true; 00210 } 00211 00212 e.handled = true; 00213 } 00214 00215 } 00216 00217 00218 /************************************************************************* 00219 Handler for when input capture is lost 00220 *************************************************************************/ 00221 void ComboDropList::onCaptureLost(WindowEventArgs& e) 00222 { 00223 Listbox::onCaptureLost(e); 00224 d_armed = false; 00225 hide(); 00226 e.handled = true; 00227 } 00228 00229 00230 /************************************************************************* 00231 Handler for when window is activated 00232 *************************************************************************/ 00233 void ComboDropList::onActivated(ActivationEventArgs& e) 00234 { 00235 Listbox::onActivated(e); 00236 } 00237 00238 } // End of CEGUI namespace section