OSDN Git Service

Handling key and mouse events in listctrl is improved
[molby/Molby.git] / wxSources / MyListCtrl.h
1 /*
2  *  MyListCtrl.h
3  *
4  *  Created by Toshi Nagata on 08/12/09.
5  *  Copyright 2008-2009 Toshi Nagata. All rights reserved.
6
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.
10  
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.
15
16  */
17
18 #ifndef __MyListCtrl_h__
19 #define __MyListCtrl_h__
20
21 #include "wx/scrolwin.h"
22 #include "wx/listctrl.h" //  some constants are included for compatibility
23
24 #include "wx/string.h"
25 #include "wx/textctrl.h"
26 #include "wx/dc.h"
27
28 #undef max
29 #include <vector>
30
31 class MyListCtrl;
32 extern const wxEventType MyListCtrlEvent;
33 enum {
34         MyListCtrlEvent_tableSelectionChanged,
35         MyListCtrlEvent_enableTableSelectionNotification
36 };
37
38 enum MyListColumnFormat {
39     MyLIST_FORMAT_LEFT,
40     MyLIST_FORMAT_RIGHT,
41     MyLIST_FORMAT_CENTRE
42 };
43
44 /*  Data source protocol  */
45 class MyListCtrlDataSource {
46 public:
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; }
55
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) {}
60     
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; }
63
64 };
65
66 class MyListCtrl: public wxWindow {
67
68 public:
69         MyListCtrlDataSource *dataSource;
70         wxTextCtrl *editText;
71     wxWindow *header;
72     wxScrolledWindow *scroll;
73     
74     int ncols, nrows;
75     std::vector<int> colWidths;
76     std::vector<int> colFormats;
77     wxArrayString colNames;
78     
79     int headerHeight;
80     int rowHeight;
81     int pageWidth, pageHeight;
82     std::vector<int> selection;
83     
84         int editRow, editColumn;
85         int dragTargetRow;
86     int mouseMode;
87     int mouseRow;
88     int lastMouseRow;
89     bool draggingRows;
90     std::vector<int> oldSelection;
91     
92         MyListCtrl();
93         virtual ~MyListCtrl();
94         
95         bool Create(wxWindow* parent, wxWindowID wid, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize);
96
97         void SetDataSource(MyListCtrlDataSource *source);
98     MyListCtrlDataSource *GetDataSource() { return dataSource; }
99     wxWindow *GetHeaderWindow() { return header; }
100     wxScrolledWindow *GetScrolledWindow() { return scroll; }
101
102     void SetNeedsReload(bool flag = true);
103         void RefreshTable(bool refreshWindow = true);
104
105     void PrepareSelectionChangeNotification();
106     
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);
111
112     bool IsRowSelected(int row);
113     bool SelectRow(int row);
114     bool UnselectRow(int row);
115     void UnselectAllRows();
116     
117     void DragRows(int x, int y);
118     
119         bool FindItemAtPosition(const wxPoint &pos, int *col, int *row);
120         
121     void GetScrollPosition(int *xpos, int *ypos);
122     bool SetScrollPosition(int xpos, int ypos);
123     
124     bool GetItemRectForRowAndColumn(wxRect &rect, int row, int column);
125     bool EnsureVisible(int row, int col = -1);
126
127         void StartEditText(int col, int row);
128         void EndEditTextAndRestart(bool setValueFlag, int newCol, int newRow);
129         void EndEditText(bool setValueFlag = true);
130     void FinalizeEdit();
131     
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);
138
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);
151     
152         void EnableSelectionChangeNotification(bool flag) { selectionChangeNotificationEnabled = flag; }
153
154     void OnPopUpMenuSelected(wxCommandEvent &event);
155         
156         void PostSelectionChangeNotification();
157         
158         bool selectionChangeNotificationRequired;
159         bool selectionChangeNotificationEnabled;
160     bool needsReload;
161     bool isPaintActive; // Flag to show whether the scroll had focus in the last OnPaint call
162         int lastPopUpColumn, lastPopUpRow;
163     wxFont cellFont;
164     wxFont headerFont;
165
166 private:
167         DECLARE_DYNAMIC_CLASS(MyListCtrl)
168         DECLARE_EVENT_TABLE()
169 };
170
171 #endif /* __MyListCtrl_h__ */