4 * Created by Toshi Nagata on 08/12/09.
5 * Copyright 2008-2009 Toshi Nagata. All rights reserved.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation version 2 of the License.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
18 #ifndef __MyListCtrl_h__
19 #define __MyListCtrl_h__
21 #include "wx/scrolwin.h"
22 #include "wx/listctrl.h" // some constants are included for compatibility
24 #include "wx/string.h"
25 #include "wx/textctrl.h"
32 extern const wxEventType MyListCtrlEvent;
34 MyListCtrlEvent_tableSelectionChanged,
35 MyListCtrlEvent_enableTableSelectionNotification
38 enum MyListColumnFormat {
44 /* Data source protocol */
45 class MyListCtrlDataSource {
47 virtual int GetItemCount(MyListCtrl *ctrl) { return 0; }
48 virtual wxString GetItemText(MyListCtrl *ctrl, long row, long column) const { return _T(""); }
49 virtual int SetItemText(MyListCtrl *ctrl, long row, long column, const wxString &value) { return 0; }
50 virtual void DragSelectionToRow(MyListCtrl *ctrl, long row) {}
51 virtual bool IsItemEditable(MyListCtrl *ctrl, long row, long column) { return false; }
52 virtual bool IsDragAndDropEnabled(MyListCtrl *ctrl, long row = -1) { return false; }
53 virtual void OnSelectionChanged(MyListCtrl *ctrl) {}
54 virtual bool IsRowSelectable(MyListCtrl *ctrl, long row) { return true; }
56 // If a popup menu is attached to the cell, then returns a positive integer, and *menu_titles should
57 // contain a malloc()'ed array of char* pointers (that are also malloc()'ed or strdup()'ed)
58 virtual int HasPopUpMenu(MyListCtrl *ctrl, long row, long column, char ***menu_titles) { return 0; }
59 virtual void OnPopUpMenuSelected(MyListCtrl *ctrl, long row, long column, int selected_index) {}
61 // Return 1 if foreground color should be modified, 2 if background color should be modified, 3 if both
62 virtual int SetItemColor(MyListCtrl *ctrl, long row, long col, float *fg, float *bg) { return 0; }
66 class MyListCtrl: public wxWindow {
69 MyListCtrlDataSource *dataSource;
72 wxScrolledWindow *scroll;
75 std::vector<int> colWidths;
76 std::vector<int> colFormats;
77 wxArrayString colNames;
81 int pageWidth, pageHeight;
82 std::vector<int> selection;
84 int editRow, editColumn;
90 std::vector<int> oldSelection;
93 virtual ~MyListCtrl();
95 bool Create(wxWindow* parent, wxWindowID wid, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize);
97 void SetDataSource(MyListCtrlDataSource *source);
98 MyListCtrlDataSource *GetDataSource() { return dataSource; }
99 wxWindow *GetHeaderWindow() { return header; }
100 wxScrolledWindow *GetScrolledWindow() { return scroll; }
102 void SetNeedsReload(bool flag = true);
103 void RefreshTable(bool refreshWindow = true);
105 void PrepareSelectionChangeNotification();
107 // wxListCtrl compatibility: only wxLIST_STATE_SELECTED is implemented
108 int GetItemCount() { return (dataSource ? dataSource->GetItemCount(this) : 0); }
109 int GetItemState(int item, int stateMask);
110 bool SetItemState(int item, int state, int stateMask);
112 bool IsRowSelected(int row);
113 bool SelectRow(int row);
114 bool UnselectRow(int row);
115 void UnselectAllRows();
117 void DragRows(int x, int y);
119 bool FindItemAtPosition(const wxPoint &pos, int *col, int *row);
121 void GetScrollPosition(int *xpos, int *ypos);
122 bool SetScrollPosition(int xpos, int ypos);
124 bool GetItemRectForRowAndColumn(wxRect &rect, int row, int column);
125 bool EnsureVisible(int row, int col = -1);
127 void StartEditText(int col, int row);
128 void EndEditTextAndRestart(bool setValueFlag, int newCol, int newRow);
129 void EndEditText(bool setValueFlag = true);
132 int GetColumnCount();
133 bool DeleteColumn(int col);
134 bool InsertColumn(int col, const wxString &heading, int format = MyLIST_FORMAT_LEFT, int width = -1);
135 void SetHeaderHeight(int headerHeight);
136 int GetHeaderHeight() { return headerHeight; }
137 void SetColumnWidth(int col, int width);
139 void OnPaintHeader(wxPaintEvent &event);
140 void OnPaint(wxPaintEvent &event);
141 void OnLeftDown(wxMouseEvent &event);
142 void OnLeftUp(wxMouseEvent &event);
143 void OnLeftDClick(wxMouseEvent &event);
144 void OnMotion(wxMouseEvent &event);
145 void OnScrollWin(wxScrollWinEvent &event);
146 void OnCharInText(wxKeyEvent &event);
147 void OnCharHookInText(wxKeyEvent &event);
148 void OnCharInScroll(wxKeyEvent &event);
149 void OnSetFocusInScroll(wxFocusEvent &event);
150 void OnKillFocusInScroll(wxFocusEvent &event);
152 void EnableSelectionChangeNotification(bool flag) { selectionChangeNotificationEnabled = flag; }
154 void OnPopUpMenuSelected(wxCommandEvent &event);
156 void PostSelectionChangeNotification();
158 bool selectionChangeNotificationRequired;
159 bool selectionChangeNotificationEnabled;
161 bool isPaintActive; // Flag to show whether the scroll had focus in the last OnPaint call
162 int lastPopUpColumn, lastPopUpRow;
167 DECLARE_DYNAMIC_CLASS(MyListCtrl)
168 DECLARE_EVENT_TABLE()
171 #endif /* __MyListCtrl_h__ */