[7bb8184] | 1 | // |
---|
| 2 | // aventreectrl.cc |
---|
| 3 | // |
---|
| 4 | // Tree control used for the survey tree. |
---|
| 5 | // |
---|
| 6 | // Copyright (C) 2001, Mark R. Shinwell. |
---|
| 7 | // Copyright (C) 2001-2003,2005,2006 Olly Betts |
---|
| 8 | // Copyright (C) 2005 Martin Green |
---|
| 9 | // |
---|
| 10 | // This program is free software; you can redistribute it and/or modify |
---|
| 11 | // it under the terms of the GNU General Public License as published by |
---|
| 12 | // the Free Software Foundation; either version 2 of the License, or |
---|
| 13 | // (at your option) any later version. |
---|
| 14 | // |
---|
| 15 | // This program is distributed in the hope that it will be useful, |
---|
| 16 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | // GNU General Public License for more details. |
---|
| 19 | // |
---|
| 20 | // You should have received a copy of the GNU General Public License |
---|
| 21 | // along with this program; if not, write to the Free Software |
---|
| 22 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 23 | // |
---|
| 24 | |
---|
| 25 | #ifdef HAVE_CONFIG_H |
---|
| 26 | #include <config.h> |
---|
| 27 | #endif |
---|
| 28 | |
---|
| 29 | #include "aventreectrl.h" |
---|
| 30 | #include "mainfrm.h" |
---|
| 31 | |
---|
| 32 | BEGIN_EVENT_TABLE(AvenTreeCtrl, wxTreeCtrl) |
---|
| 33 | EVT_MOTION(AvenTreeCtrl::OnMouseMove) |
---|
| 34 | EVT_LEAVE_WINDOW(AvenTreeCtrl::OnLeaveWindow) |
---|
| 35 | EVT_TREE_SEL_CHANGED(-1, AvenTreeCtrl::OnSelChanged) |
---|
| 36 | EVT_TREE_ITEM_ACTIVATED(-1, AvenTreeCtrl::OnItemActivated) |
---|
| 37 | EVT_CHAR(AvenTreeCtrl::OnKeyPress) |
---|
| 38 | END_EVENT_TABLE() |
---|
| 39 | |
---|
| 40 | AvenTreeCtrl::AvenTreeCtrl(MainFrm* parent, wxWindow* window_parent) : |
---|
| 41 | wxTreeCtrl(window_parent, -1), |
---|
| 42 | m_Parent(parent), |
---|
| 43 | m_Enabled(false), |
---|
| 44 | m_LastItem(), |
---|
| 45 | m_BackgroundColour(), |
---|
| 46 | m_SelValid(false) |
---|
| 47 | { |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | #define TREE_MASK (wxTREE_HITTEST_ONITEMLABEL | wxTREE_HITTEST_ONITEMRIGHT) |
---|
| 51 | |
---|
| 52 | void AvenTreeCtrl::OnMouseMove(wxMouseEvent& event) |
---|
| 53 | { |
---|
| 54 | if (m_Enabled) { |
---|
| 55 | int flags; |
---|
| 56 | wxTreeItemId pos = HitTest(event.GetPosition(), flags); |
---|
| 57 | if (!(flags & TREE_MASK)) { |
---|
| 58 | pos = wxTreeItemId(); |
---|
| 59 | } |
---|
| 60 | if (pos == m_LastItem) return; |
---|
| 61 | if (pos.IsOk()) { |
---|
| 62 | m_Parent->DisplayTreeInfo(GetItemData(pos)); |
---|
| 63 | } else { |
---|
| 64 | m_Parent->DisplayTreeInfo(NULL); |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | void AvenTreeCtrl::SetHere(wxTreeItemId pos) |
---|
| 70 | { |
---|
| 71 | if (pos == m_LastItem) return; |
---|
| 72 | |
---|
| 73 | if (m_LastItem.IsOk()) { |
---|
| 74 | SetItemBackgroundColour(m_LastItem, m_BackgroundColour); |
---|
| 75 | } |
---|
| 76 | if (pos.IsOk()) { |
---|
| 77 | m_BackgroundColour = GetItemBackgroundColour(pos); |
---|
| 78 | SetItemBackgroundColour(pos, wxColour(180, 180, 180)); |
---|
| 79 | } |
---|
| 80 | m_LastItem = pos; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | void AvenTreeCtrl::OnLeaveWindow(wxMouseEvent&) |
---|
| 84 | { |
---|
| 85 | if (m_LastItem.IsOk()) { |
---|
| 86 | SetItemBackgroundColour(m_LastItem, m_BackgroundColour); |
---|
| 87 | m_LastItem = wxTreeItemId(); |
---|
| 88 | } |
---|
| 89 | m_Parent->DisplayTreeInfo(NULL); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | void AvenTreeCtrl::SetEnabled(bool enabled) |
---|
| 93 | { |
---|
| 94 | m_Enabled = enabled; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void AvenTreeCtrl::OnSelChanged(wxTreeEvent& e) |
---|
| 98 | { |
---|
| 99 | if (m_Enabled) { |
---|
| 100 | m_Parent->TreeItemSelected(GetItemData(e.GetItem()), false); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | m_SelValid = true; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void AvenTreeCtrl::OnItemActivated(wxTreeEvent& e) |
---|
| 107 | { |
---|
| 108 | if (m_Enabled) { |
---|
| 109 | m_Parent->TreeItemSelected(GetItemData(e.GetItem()), true); |
---|
| 110 | // Need to skip to allow double-clicking to work on wxMSW >= 2.8.11. |
---|
| 111 | e.Skip(); |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | bool AvenTreeCtrl::GetSelectionData(wxTreeItemData** data) const |
---|
| 116 | { |
---|
| 117 | assert(m_Enabled); |
---|
| 118 | assert(data); |
---|
| 119 | |
---|
| 120 | if (!m_SelValid) { |
---|
| 121 | return false; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | wxTreeItemId id = GetSelection(); |
---|
| 125 | if (id.IsOk()) { |
---|
| 126 | *data = GetItemData(id); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | return id.IsOk() && *data; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | void AvenTreeCtrl::UnselectAll() |
---|
| 133 | { |
---|
| 134 | m_SelValid = false; |
---|
| 135 | wxTreeCtrl::UnselectAll(); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void AvenTreeCtrl::DeleteAllItems() |
---|
| 139 | { |
---|
| 140 | m_Enabled = false; |
---|
| 141 | m_LastItem = wxTreeItemId(); |
---|
| 142 | m_SelValid = false; |
---|
| 143 | wxTreeCtrl::DeleteAllItems(); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | void AvenTreeCtrl::OnKeyPress(wxKeyEvent &e) |
---|
| 147 | { |
---|
| 148 | switch (e.GetKeyCode()) { |
---|
| 149 | case WXK_ESCAPE: |
---|
| 150 | m_Parent->ClearTreeSelection(); |
---|
| 151 | break; |
---|
| 152 | case WXK_RETURN: { |
---|
| 153 | wxTreeItemId id = GetSelection(); |
---|
| 154 | if (id.IsOk()) { |
---|
| 155 | if (ItemHasChildren(id)) { |
---|
| 156 | // If on a branch, expand/contract it. |
---|
| 157 | if (IsExpanded(id)) { |
---|
| 158 | Collapse(id); |
---|
| 159 | } else { |
---|
| 160 | Expand(id); |
---|
| 161 | } |
---|
| 162 | } else { |
---|
| 163 | // FIXME if on a station, show information on that station |
---|
| 164 | // or something? |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | break; |
---|
| 168 | } |
---|
| 169 | case WXK_LEFT: case WXK_RIGHT: case WXK_UP: case WXK_DOWN: |
---|
| 170 | case WXK_HOME: case WXK_END: case WXK_PAGEUP: case WXK_PAGEDOWN: |
---|
| 171 | // On wx 2.6 and earlier, PRIOR/NEXT seem to actually be |
---|
| 172 | // PAGEUP/PAGEDOWN (on wxGTK at least). In wx 2.7 and later |
---|
| 173 | // they're just compatibility aliases, so either they have the |
---|
| 174 | // same value or aren't defined - either way the code won't |
---|
| 175 | // compile. |
---|
| 176 | #if !wxCHECK_VERSION(2,7,0) |
---|
| 177 | case WXK_PRIOR: case WXK_NEXT: |
---|
| 178 | #endif |
---|
| 179 | e.Skip(); |
---|
| 180 | break; |
---|
| 181 | default: |
---|
| 182 | // Pass key event to MainFrm which will pass to GfxCore which will |
---|
| 183 | // pass to GUIControl. |
---|
| 184 | m_Parent->OnKeyPress(e); |
---|
| 185 | break; |
---|
| 186 | } |
---|
| 187 | } |
---|