1 ///////////////////////////////////////// 2 // 3 // OpenLieroX 4 // 5 // code under LGPL, based on JasonBs work, 6 // enhanced by Dark Charlie and Albert Zeyer 7 // 8 // 9 ///////////////////////////////////////// 10 11 12 // gui list view class 13 // Created 30/5/02 14 // Jason Boettcher 15 16 17 #ifndef __CLISTVIEW_H__DEPRECATED_GUI__ 18 #define __CLISTVIEW_H__DEPRECATED_GUI__ 19 20 #include <string> 21 #include "DeprecatedGUI/CWidget.h" 22 #include "DeprecatedGUI/CScrollbar.h" 23 #include "DynDraw.h" 24 #include "Utils.h" 25 26 namespace DeprecatedGUI { 27 28 // Event msg's for this control 29 enum { 30 LV_NONE=-1, 31 LV_CHANGED=0, 32 LV_RIGHTCLK, 33 LV_DOUBLECLK, 34 LV_DELETE, 35 LV_ENTER, 36 LV_WIDGETEVENT, 37 LV_MOUSEOVER 38 }; 39 40 41 // Listview messages 42 enum { 43 LVS_ADDCOLUMN=0, 44 LVS_ADDITEM, 45 LVM_REMOVEITEM, 46 LVM_GETCURINDEX, 47 LVS_GETCURSINDEX, 48 LVM_GETITEMCOUNT, 49 LVM_CLEAR, 50 LVM_GETITEMINDEX, 51 LVM_GETINDEX, 52 LVM_GETCURITEM, 53 LVM_SETOLDSTYLE, 54 LVM_GETCOLUMNWIDTH 55 }; 56 57 58 // Sub-item types 59 enum { 60 LVS_IMAGE=0, 61 LVS_TEXT, 62 LVS_WIDGET 63 }; 64 65 // Subitem vertical aligns 66 enum { 67 VALIGN_TOP=0, 68 VALIGN_MIDDLE, 69 VALIGN_BOTTOM 70 }; 71 72 73 74 // Column structure 75 struct lv_column_t : DontCopyTag { 76 std::string sText; 77 int iWidth; 78 bool bDown; 79 int iSorted; // -1 = unsorted, 0 = descending, 1 = ascending 80 Color iColour; 81 82 lv_column_t *tNext; 83 84 }; 85 86 87 // Sub item structure 88 struct lv_subitem_t : DontCopyTag { lv_subitem_tlv_subitem_t89 lv_subitem_t() : iType(0), tWidget(NULL), fMouseOverTime(0), tNext(NULL) {} // safety ~lv_subitem_tlv_subitem_t90 ~lv_subitem_t() { if (tWidget) delete tWidget; } 91 92 int iType; 93 std::string sText; 94 std::string sTooltip; 95 SmartPointer<DynDrawIntf> bmpImage; 96 CWidget *tWidget; 97 bool bVisible; 98 int iExtra; 99 int iValign; 100 Color iColour; 101 Color iBgColour; 102 103 TimeDiff fMouseOverTime; 104 lv_subitem_t *tNext; 105 106 }; 107 108 109 // Item structure 110 struct lv_item_t : DontCopyTag { lv_item_tlv_item_t111 lv_item_t() : iIndex(0), tSubitems(NULL), tNext(NULL) {} 112 113 std::string sIndex; 114 int iIndex; 115 int _iID; 116 bool bSelected; 117 int iHeight; 118 Color iColour; 119 Color iBgColour; 120 121 lv_subitem_t *tSubitems; 122 123 lv_item_t *tNext; 124 125 }; 126 127 // Listview control class 128 class CListview: public CWidget, DontCopyTag { 129 public: 130 // Constructors CListview()131 CListview() { 132 tColumns = NULL; 133 iNumColumns = 0; 134 tItems = NULL; 135 tLastItem = NULL; 136 tSelected = NULL; 137 tPreviousMouseSelection = NULL; 138 iItemCount=0; 139 bGotScrollbar = false; 140 iType = wid_Listview; 141 fLastMouseUp = AbsTime(); 142 iContentHeight = 0; 143 iItemID = 0; 144 bShowSelect = true; 145 iLastMouseX = 0; 146 iGrabbed = 0; 147 bOldStyle = false; 148 iSavedScrollbarPos = 0; 149 bFingerDragged = false; 150 iFingerDraggedPos = 0; 151 iLastChar = 0; 152 bDrawBorder = true; 153 bNeedsRepaint = true; 154 bCustomScrollbarSetup = false; 155 bAlwaysVisibleScrollbar = false; 156 tFocusedSubWidget = NULL; 157 tMouseOverSubWidget = NULL; 158 bSubItemsAreAligned = false; 159 bMouseOverEventEnabled = false; 160 bTooltipVisible = false; 161 tMouseOver = NULL; 162 holdedWidget = NULL; 163 } 164 ~CListview()165 ~CListview() { 166 Destroy(); 167 } 168 169 170 private: 171 // Attributes 172 bool bOldStyle; 173 bool bDrawBorder; 174 bool bShowSelect; 175 bool bCustomScrollbarSetup; 176 bool bAlwaysVisibleScrollbar; 177 178 // Columns 179 int iNumColumns; 180 lv_column_t *tColumns; 181 int iGrabbed; 182 int iLastMouseX; 183 184 // Items 185 lv_item_t *tItems; 186 lv_item_t *tLastItem; 187 lv_item_t *tSelected; 188 int iItemCount; 189 int iItemID; 190 int iContentHeight; 191 bool bSubItemsAreAligned; // if the left item is too long, subitems are shifted right 192 193 AbsTime fLastMouseUp; 194 int iClickedSub; 195 lv_item_t *tPreviousMouseSelection; 196 197 // Scrollbar 198 CScrollbar cScrollbar; 199 bool bGotScrollbar; 200 int iSavedScrollbarPos; 201 bool bFingerDragged; 202 int iFingerDraggedPos; 203 204 // Tooltips 205 bool bTooltipVisible; 206 std::string sTooltipText; 207 int iTooltipX; 208 int iTooltipY; 209 210 // Other 211 bool bNeedsRepaint; 212 UnicodeChar iLastChar; 213 gui_event_t tLastWidgetEvent; 214 CWidget *holdedWidget; // moused pressed and not released 215 CWidget *tFocusedSubWidget; 216 CWidget *tMouseOverSubWidget; 217 218 bool bMouseOverEventEnabled; 219 lv_item_t *tMouseOver; 220 221 private: 222 void ShowTooltip(const std::string& text, int ms_x, int ms_y); 223 void UpdateItemIDs(); 224 void MoveMouseToCurrentItem(); 225 226 public: 227 // Methods 228 229 void Create(); 230 void Destroy(); 231 232 //These events return an event id, otherwise they return -1 233 int MouseOver(mouse_t *tMouse); 234 int MouseUp(mouse_t *tMouse, int nDown); 235 int MouseDown(mouse_t *tMouse, int nDown); 236 int MouseClicked(mouse_t *tMouse, int nDown); 237 int MouseWheelDown(mouse_t *tMouse); 238 int MouseWheelUp(mouse_t *tMouse); 239 int KeyDown(UnicodeChar c, int keysym, const ModifiersState& modstate); 240 int KeyUp(UnicodeChar c, int keysym, const ModifiersState& modstate); 241 242 void Draw(SDL_Surface * bmpDest); 243 244 DWORD SendMessage(int iMsg, DWORD Param1, DWORD Param2); 245 DWORD SendMessage(int iMsg, const std::string& sStr, DWORD Param); 246 DWORD SendMessage(int iMsg, std::string *sStr, DWORD Param); 247 248 void ReadjustScrollbar(); 249 void SetupScrollbar(int x, int y, int h, bool always_visible); 250 251 void Clear(); 252 253 void SortBy(int column, bool ascending); // One-time sort 254 void ReSort(); 255 void SetSortColumn(int column, bool ascending); // Permanent sort 256 int GetSortColumn(); 257 258 void AddColumn(const std::string& sText, int iWidth); 259 void AddColumn(const std::string& sText, int iWidth, Color iColour); 260 lv_item_t* AddItem(const std::string& sIndex, int iIndex, Color iColour); 261 void AddSubitem(int iType, const std::string& sText, const SmartPointer<DynDrawIntf> & img, CWidget *wid, int iVAlign = VALIGN_MIDDLE, const std::string& tooltip = ""); 262 void AddSubitem(int iType, const std::string& sText, const SmartPointer<DynDrawIntf> & img, CWidget *wid, int iVAlign, Color iColour, const std::string& tooltip = ""); 263 void AddSubitem(int iType, const std::string& sText, const SmartPointer<SDL_Surface> & img, CWidget *wid, int iVAlign = VALIGN_MIDDLE, const std::string& tooltip = "") { 264 AddSubitem(iType, sText, DynDrawFromSurface(img), wid, iVAlign, tooltip); 265 } 266 void AddSubitem(int iType, const std::string& sText, const SmartPointer<SDL_Surface> & img, CWidget *wid, int iVAlign, Color iColour, const std::string& tooltip = "") { 267 AddSubitem(iType, sText, DynDrawFromSurface(img), wid, iVAlign, iColour, tooltip); 268 } 269 void AddSubitem(const std::string& sText, int iVAlign = VALIGN_MIDDLE, const std::string& tooltip = "") { 270 AddSubitem(LVS_TEXT, sText, (DynDrawIntf*)NULL, NULL, iVAlign, tooltip); 271 } 272 void AddSubitem(const std::string& sText, int iVAlign, Color iColour, const std::string& tooltip = "") { 273 AddSubitem(LVS_TEXT, sText, (DynDrawIntf*)NULL, NULL, iVAlign, iColour, tooltip); 274 } 275 276 void RemoveItem(int iIndex); 277 int getIndex(int count); 278 279 int GetColumnWidth(int id); 280 281 int getSelectedID(); 282 void setSelectedID(int id); 283 setOldStyle(bool _s)284 void setOldStyle(bool _s) { bOldStyle = _s; } getOldStyle()285 bool getOldStyle() { return bOldStyle; } 286 subItemsAreAligned()287 bool& subItemsAreAligned() { return bSubItemsAreAligned; } 288 getNumItems()289 int getNumItems() { return iItemCount; } 290 291 void scrollLast(); 292 getCurIndex()293 int getCurIndex() { if(tSelected) return tSelected->iIndex; else return -1; } getCurSIndex()294 std::string getCurSIndex() { if(tSelected) return tSelected->sIndex; else return ""; } 295 lv_subitem_t *getCurSubitem(int index); 296 297 298 lv_subitem_t *getCurSub(); 299 getItemCount()300 int getItemCount() { return iItemCount; } getItems()301 lv_item_t *getItems() { return tItems; } getLastItem()302 lv_item_t *getLastItem() { return tLastItem; } 303 lv_item_t* getItem(int index); 304 lv_item_t* getItem(const std::string& name); 305 lv_subitem_t *getSubItem(int item_index, int subitem_index); 306 lv_subitem_t *getSubItem(lv_item_t *it, int subitem_index); 307 getClickedSub()308 int getClickedSub() { return iClickedSub; } getWidgetEvent()309 gui_event_t *getWidgetEvent() { return &tLastWidgetEvent; } 310 setShowSelect(bool s)311 void setShowSelect(bool s) { bShowSelect = s; } setDrawBorder(bool _d)312 void setDrawBorder(bool _d) { bDrawBorder = _d; } 313 SaveScrollbarPos()314 void SaveScrollbarPos() { iSavedScrollbarPos = cScrollbar.getValue(); } RestoreScrollbarPos()315 void RestoreScrollbarPos() { cScrollbar.setValue(iSavedScrollbarPos); iSavedScrollbarPos = 0; } 316 NeedsRepaint()317 inline bool NeedsRepaint() {return bNeedsRepaint; } SetRepaint(bool _r)318 inline void SetRepaint(bool _r) { bNeedsRepaint = _r; } // Explicitly set this listview needs to be repainted 319 setMouseOverEventEnabled(bool b)320 void setMouseOverEventEnabled(bool b) { bMouseOverEventEnabled = b; } getMouseOverIndex()321 int getMouseOverIndex() { if(tMouseOver) return tMouseOver->iIndex; else return -1; } getMouseOverSIndex()322 std::string getMouseOverSIndex() { if(tMouseOver) return tMouseOver->sIndex; else return ""; } 323 }; 324 325 } // namespace DeprecatedGUI 326 327 #endif // __CLISTVIEW_H__DEPRECATED_GUI__ 328