[f17e6dc6] | 1 | // |
---|
| 2 | // aventreectrl.cc |
---|
| 3 | // |
---|
| 4 | // Tree control used for the survey tree. |
---|
| 5 | // |
---|
| 6 | // Copyright (C) 2001, Mark R. Shinwell. |
---|
[866a9a5] | 7 | // Copyright (C) 2001-2003,2005,2010 Olly Betts |
---|
[168df28] | 8 | // Copyright (C) 2005 Martin Green |
---|
[f17e6dc6] | 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 |
---|
[866a9a5] | 22 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[f17e6dc6] | 23 | // |
---|
| 24 | |
---|
[17aa816] | 25 | #ifdef HAVE_CONFIG_H |
---|
| 26 | #include <config.h> |
---|
| 27 | #endif |
---|
| 28 | |
---|
[f17e6dc6] | 29 | #include "aventreectrl.h" |
---|
| 30 | #include "mainfrm.h" |
---|
| 31 | |
---|
| 32 | BEGIN_EVENT_TABLE(AvenTreeCtrl, wxTreeCtrl) |
---|
| 33 | EVT_MOTION(AvenTreeCtrl::OnMouseMove) |
---|
[168df28] | 34 | EVT_LEAVE_WINDOW(AvenTreeCtrl::OnLeaveWindow) |
---|
[f17e6dc6] | 35 | EVT_TREE_SEL_CHANGED(-1, AvenTreeCtrl::OnSelChanged) |
---|
[5901b62] | 36 | EVT_CHAR(AvenTreeCtrl::OnKeyPress) |
---|
[f17e6dc6] | 37 | END_EVENT_TABLE() |
---|
| 38 | |
---|
| 39 | AvenTreeCtrl::AvenTreeCtrl(MainFrm* parent, wxWindow* window_parent) : |
---|
[5e3a4aa] | 40 | wxTreeCtrl(window_parent), |
---|
[b623b3e] | 41 | m_Parent(parent), |
---|
[2dfd768] | 42 | m_Enabled(false), |
---|
[168df28] | 43 | m_LastItem(), |
---|
[2dfd768] | 44 | m_BackgroundColour(GetBackgroundColour()), |
---|
| 45 | m_SelValid(false) |
---|
[f17e6dc6] | 46 | { |
---|
| 47 | } |
---|
| 48 | |
---|
[97dd0d2] | 49 | #define TREE_MASK (wxTREE_HITTEST_ONITEMLABEL | wxTREE_HITTEST_ONITEMRIGHT) |
---|
| 50 | |
---|
[f17e6dc6] | 51 | void AvenTreeCtrl::OnMouseMove(wxMouseEvent& event) |
---|
| 52 | { |
---|
| 53 | if (m_Enabled) { |
---|
[421b7d2] | 54 | int flags; |
---|
| 55 | wxTreeItemId pos = HitTest(event.GetPosition(), flags); |
---|
[168df28] | 56 | if (!(flags & TREE_MASK)) { |
---|
| 57 | pos = wxTreeItemId(); |
---|
| 58 | } |
---|
[05d1163] | 59 | if (pos != m_LastItem) { |
---|
[168df28] | 60 | if (m_LastItem.IsOk()) { |
---|
[8992c65] | 61 | SetItemBackgroundColour(m_LastItem, m_BackgroundColour); |
---|
| 62 | } |
---|
[168df28] | 63 | if (pos.IsOk()) { |
---|
[05d1163] | 64 | SetItemBackgroundColour(pos, wxColour(180, 180, 180)); |
---|
| 65 | m_Parent->DisplayTreeInfo(GetItemData(pos)); |
---|
| 66 | } else { |
---|
| 67 | m_Parent->DisplayTreeInfo(NULL); |
---|
[f17e6dc6] | 68 | } |
---|
[168df28] | 69 | m_LastItem = pos; |
---|
[f17e6dc6] | 70 | } |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
[168df28] | 74 | void AvenTreeCtrl::OnLeaveWindow(wxMouseEvent&) |
---|
| 75 | { |
---|
| 76 | if (m_LastItem.IsOk()) { |
---|
| 77 | SetItemBackgroundColour(m_LastItem, m_BackgroundColour); |
---|
| 78 | m_LastItem = wxTreeItemId(); |
---|
| 79 | } |
---|
| 80 | m_Parent->DisplayTreeInfo(NULL); |
---|
| 81 | } |
---|
| 82 | |
---|
[f17e6dc6] | 83 | void AvenTreeCtrl::SetEnabled(bool enabled) |
---|
| 84 | { |
---|
| 85 | m_Enabled = enabled; |
---|
| 86 | } |
---|
| 87 | |
---|
[2dfd768] | 88 | void AvenTreeCtrl::OnSelChanged(wxTreeEvent&) |
---|
[f17e6dc6] | 89 | { |
---|
| 90 | if (m_Enabled) { |
---|
[421b7d2] | 91 | m_Parent->TreeItemSelected(GetItemData(GetSelection())); |
---|
[f17e6dc6] | 92 | } |
---|
[b623b3e] | 93 | |
---|
| 94 | m_SelValid = true; |
---|
[f17e6dc6] | 95 | } |
---|
| 96 | |
---|
| 97 | bool AvenTreeCtrl::GetSelectionData(wxTreeItemData** data) |
---|
| 98 | { |
---|
| 99 | assert(m_Enabled); |
---|
[168df28] | 100 | assert(data); |
---|
[b623b3e] | 101 | |
---|
| 102 | if (!m_SelValid) { |
---|
[421b7d2] | 103 | return false; |
---|
[b623b3e] | 104 | } |
---|
[421b7d2] | 105 | |
---|
[f17e6dc6] | 106 | wxTreeItemId id = GetSelection(); |
---|
| 107 | if (id.IsOk()) { |
---|
[421b7d2] | 108 | *data = GetItemData(id); |
---|
[f17e6dc6] | 109 | } |
---|
| 110 | |
---|
[2d9ed8ad] | 111 | return id.IsOk() && *data; |
---|
[f17e6dc6] | 112 | } |
---|
[b623b3e] | 113 | |
---|
| 114 | void AvenTreeCtrl::UnselectAll() |
---|
| 115 | { |
---|
| 116 | m_SelValid = false; |
---|
| 117 | wxTreeCtrl::UnselectAll(); |
---|
| 118 | } |
---|
| 119 | |
---|
[30987bd] | 120 | void AvenTreeCtrl::DeleteAllItems() |
---|
| 121 | { |
---|
[168df28] | 122 | m_Enabled = false; |
---|
| 123 | m_LastItem = wxTreeItemId(); |
---|
[30987bd] | 124 | m_SelValid = false; |
---|
[4eab06e5] | 125 | wxTreeCtrl::DeleteAllItems(); |
---|
[30987bd] | 126 | } |
---|
| 127 | |
---|
[5901b62] | 128 | void AvenTreeCtrl::OnKeyPress(wxKeyEvent &e) |
---|
| 129 | { |
---|
[168df28] | 130 | switch (e.m_keyCode) { |
---|
| 131 | case WXK_ESCAPE: |
---|
| 132 | m_Parent->ClearTreeSelection(); |
---|
| 133 | break; |
---|
| 134 | case WXK_RETURN: { |
---|
| 135 | wxTreeItemId id = GetSelection(); |
---|
| 136 | if (id.IsOk()) { |
---|
| 137 | if (ItemHasChildren(id)) { |
---|
| 138 | // If on a branch, expand/contract it. |
---|
| 139 | if (IsExpanded(id)) { |
---|
| 140 | Collapse(id); |
---|
| 141 | } else { |
---|
| 142 | Expand(id); |
---|
| 143 | } |
---|
| 144 | } else { |
---|
| 145 | // FIXME if on a station, show information on that station |
---|
| 146 | // or something? |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | break; |
---|
| 150 | } |
---|
| 151 | case WXK_LEFT: case WXK_RIGHT: case WXK_UP: case WXK_DOWN: |
---|
| 152 | case WXK_HOME: case WXK_END: case WXK_PAGEUP: case WXK_PAGEDOWN: |
---|
[866a9a5] | 153 | // On wx 2.6 and earlier, PRIOR/NEXT seem to actually be |
---|
| 154 | // PAGEUP/PAGEDOWN (on wxGtk at least). In wx 2.7 and later |
---|
| 155 | // they're just compatibility aliases, so either they have the |
---|
| 156 | // same value or aren't defined - either way the code won't |
---|
| 157 | // compile. |
---|
| 158 | #if !wxCHECK_VERSION(2,7,0) |
---|
[168df28] | 159 | case WXK_PRIOR: case WXK_NEXT: |
---|
[866a9a5] | 160 | #endif |
---|
[168df28] | 161 | e.Skip(); |
---|
| 162 | break; |
---|
| 163 | default: |
---|
| 164 | // Pass key event to MainFrm which will pass to GfxCore which will |
---|
| 165 | // pass to GUIControl. |
---|
| 166 | m_Parent->OnKeyPress(e); |
---|
| 167 | break; |
---|
[5901b62] | 168 | } |
---|
| 169 | } |
---|