Planeshift

pawslistbox.h

Go to the documentation of this file.
00001 /*
00002  * pawslistbox.h - Author: Andrew Craig
00003  *
00004  * Copyright (C) 2003 Atomic Blue ([email protected], http://www.atomicblue.org)
00005  *
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 (version 2 of the License)
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017  *
00018  */
00019 
00020 #ifndef PAWS_LIST_BOX_HEADER
00021 #define PAWS_LIST_BOX_HEADER
00022 
00023 #include "pawswidget.h"
00024 #include "pawsmanager.h"
00025 #include "pawstextbox.h"
00026 #include "util/log.h"
00027 
00028 class pawsScrollBar;
00029 
00035 typedef int (*listBoxSortingFunc)(pawsWidget* a, pawsWidget* b);
00036 
00038 int textBoxSortFunc(pawsWidget* widgetA, pawsWidget* widgetB);
00039 
00041 int textBoxSortFunc_number(pawsWidget * widgetA, pawsWidget * widgetB);
00042 
00043 //-----------------------------------------------------------------------------
00044 //                            struct ColumnInfo
00045 //-----------------------------------------------------------------------------
00046 
00053 struct ColumnDef
00054 {
00055     ColumnDef()
00056     {
00057         widgetNode  =  NULL;
00058         sortFunc    =  NULL;
00059         sortable = true;
00060         width = 0;
00061         height = 0;
00062     }
00063 
00064     int width;
00065     int height;
00066     csRef<iDocumentNode> widgetNode;    
00067     csString xmlbinding;
00068     bool sortable;
00069 
00070     listBoxSortingFunc sortFunc;        
00071 };
00072 
00073 
00074 #define LISTBOX_HIGHLIGHTED  0x01  /// Single click event
00075 #define LISTBOX_SELECTED     0x02
00076 
00077 //-----------------------------------------------------------------------------
00078 //                            class pawsListBoxRow
00079 //-----------------------------------------------------------------------------
00080 
00081 
00085 class pawsListBoxRow : public pawsWidget
00086 {
00087 public:
00088     pawsListBoxRow();
00089     pawsListBoxRow(const pawsListBoxRow &origin);
00091     pawsWidget* GetColumn(size_t column);
00092 
00094     size_t GetTotalColumns()
00095     {
00096         return columns.GetSize();
00097     }
00098 
00100     void AddColumn(int column, ColumnDef* def);
00101 
00103     void AddTitleColumn(int column, ColumnDef* def);
00104 
00105     bool OnKeyDown(utf32_char keyCode, utf32_char keyChar, int modifiers);
00106 
00108     bool OnMouseDown(int button, int modifiers, int x, int y);
00109 
00111     bool OnDoubleClick(int button, int modifiers, int x, int y);
00112 
00114     void SetHeading(bool flag);
00115 
00117     void SetTitleRow(bool flag);
00118 
00120     int GetLastIndex()const
00121     {
00122         return lastIndex;
00123     }
00124 
00126     void SetLastIndex(int index)
00127     {
00128         lastIndex = index;
00129     }
00130 
00135     void Hide();
00136 
00137     bool IsHeading()
00138     {
00139         return isHeading;
00140     }
00141 
00142 private:
00143 
00144     bool isHeading;  
00145     bool isTitleRow; 
00146 
00147     int lastIndex; 
00148 
00150     csArray<pawsWidget*> columns;
00151 
00152 };
00153 
00154 
00155 //-----------------------------------------------------------------------------
00156 //                            class pawsListBox
00157 //-----------------------------------------------------------------------------
00158 
00159 
00160 #define LISTBOX_MOUSE_SCROLL_AMOUNT 3
00161 
00202 class pawsListBox : public pawsWidget
00203 {
00204 public:
00205     pawsListBox();
00206     pawsListBox(const pawsListBox &origin);
00207     bool Setup(iDocumentNode* node);
00208     bool PostSetup();
00209 
00210     virtual ~pawsListBox();
00211 
00216     int GetSelection()
00217     {
00218         return selected;
00219     }
00220 
00222     pawsListBoxRow* GetSelectedRow();
00223 
00225     const char* GetSelectedText(size_t columnId);
00226 
00228     int GetSelectedRowNum();
00229 
00231     pawsListBoxRow* RemoveSelected();
00232 
00234     void Remove(int id);
00235 
00237     void Remove(pawsListBoxRow* rowToRemove);
00238 
00239     void AddRow(pawsListBoxRow* row);
00240 
00242     size_t GetRowCount();
00243 
00245     pawsListBoxRow* GetRow(size_t x);
00246 
00248     pawsListBoxRow* NewRow(size_t position = (size_t)-1);
00250     pawsListBoxRow* NewTextBoxRow(csList<csString> &rowEntry,size_t position = (size_t)-1);
00251 
00252     void Clear();
00253 
00255     void SendOnListAction(int status);
00256 
00258     bool Select(pawsListBoxRow* row, bool notify = true);
00259 
00261     bool SelectByIndex(int index, bool notify = true);
00262 
00264     void CreateTitleRow();
00265 
00266     virtual bool OnScroll(int direction, pawsScrollBar* widget);
00267 
00268     bool OnMouseDown(int button, int modifiers, int x, int y);
00269 
00270     int GetTotalColumns()
00271     {
00272         return totalColumns;
00273     }
00274 
00281     void SetTotalColumns(int numCols);
00282 
00293     void SetColumnDef(int col, int width, int height, const char* widgetDesc);
00294 
00295     void UseTitleRow(bool yes);
00296 
00300     virtual bool SelfPopulate(iDocumentNode* node);
00301 
00302     bool IsSelectable()
00303     {
00304         return selectable;
00305     }
00306 
00310     bool IsAutoID()
00311     {
00312         return autoID;
00313     }
00314 
00318     bool ConvertFromAutoID(int id, int &row, int &col);
00319 
00323     void MoveSelectBar(bool direction);
00324 
00325     void CalculateDrawPositions();
00326 
00331     bool OnKeyDown(utf32_char keyCode, utf32_char keyChar, int modifiers);
00332 
00333     void Resize();
00334 
00340     void SetNotify(pawsWidget* target)
00341     {
00342         notifyTarget = target;
00343     };
00344 
00349     int  GetSortedColumn();
00350     void SetSortedColumn(int colNum);
00351 
00352     bool GetSortOrder();
00353     void SetSortOrder(bool ascOrder);
00354 
00359     void SetSortingFunc(int colNum, listBoxSortingFunc sortFunc);
00360 
00365     void SortRows();
00366 
00367 
00371     pawsTextBox* GetTextCell(int rowNum, int colNum);
00372 
00376     csString GetTextCellValue(int rowNum, int colNum);
00377 
00381     void SetTextCellValue(int rowNum, int colNum, const csString &value);
00382 
00383     void AutoScrollUpdate(bool v)
00384     {
00385         autoUpdateScroll = v;
00386     }
00387 
00388     void SetScrollBarMaxValue();
00389     void MoveRow(int rownr,int dest);
00390 
00391 protected:
00392 
00399     static const int BORDER_SIZE = 5;
00400 
00401     void CreateSortingArrow(int colNum);
00402     void SetSortingArrow(int colNum, bool ascOrder);
00403     void CheckSortingArrow(int colNum, bool ascOrder);
00404     void DeleteSortingArrow(int colNum);
00405 
00406     pawsWidget* GetColumnTitle(int colNum);
00407 
00413     int GetUnborderedHeight();
00414 
00415     bool usingTitleRow;
00416 
00417     pawsScrollBar* scrollBar;
00418     pawsScrollBar* horzscrollBar;
00419 
00420     int totalColumns;
00421     int totalRows;
00422     int columnHeight;
00423     int rowWidth;
00424     int topRow;
00425     int selected;
00426     int xMod;
00427     bool autoID;
00428     bool autoResize;
00429     bool autoUpdateScroll;
00430 
00431     ColumnDef* columnDef;
00432 
00434     pawsWidget* notifyTarget;
00435 
00436     csArray<pawsListBoxRow*> rows;
00437     pawsListBoxRow* titleRow;
00438     csString xmlbinding_row;
00439 
00440     int sortColNum;    // number of sorted column (or -1)
00441     bool ascOrder;
00442 
00447     static int sort_cmp(const void* rowA, const void* rowB);
00448     static listBoxSortingFunc sort_sortFunc;
00449     static int sort_sortColNum;
00450     static bool sort_ascOrder;
00451 
00452     csString highlightImage;
00453     unsigned int highlightAlpha;
00454 
00455     int         arrowSize;
00456     int         scrollbarWidth;
00457     int         scrollbarHeightMod;
00458     bool        useBorder;
00459     bool        selectable;     // can the user select listbox rows ?
00460     csString    arrowUp;
00461     csString    arrowDown;
00462 };
00463 
00464 CREATE_PAWS_FACTORY(pawsListBox);
00465 
00466 
00469 #endif