Fix TRANSLATORS comment to match survex.pot version
[survex.git] / src / aventreectrl.cc
blob96f2791839db291dbe563fd8132554aecbf89778
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,2016 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.
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.
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
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
29 #include "aventreectrl.h"
30 #include "mainfrm.h"
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 EVT_TREE_ITEM_MENU(-1, AvenTreeCtrl::OnMenu)
39 EVT_MENU(menu_SURVEY_SHOW_ALL, AvenTreeCtrl::OnRestrict)
40 EVT_MENU(menu_SURVEY_RESTRICT, AvenTreeCtrl::OnRestrict)
41 END_EVENT_TABLE()
43 AvenTreeCtrl::AvenTreeCtrl(MainFrm* parent, wxWindow* window_parent) :
44 wxTreeCtrl(window_parent, -1),
45 m_Parent(parent),
46 m_Enabled(false),
47 m_LastItem(),
48 m_BackgroundColour(),
49 m_SelValid(false),
50 menu_data(NULL)
54 #define TREE_MASK (wxTREE_HITTEST_ONITEMLABEL | wxTREE_HITTEST_ONITEMRIGHT)
56 void AvenTreeCtrl::OnMouseMove(wxMouseEvent& event)
58 if (m_Enabled && !m_Parent->Animating()) {
59 int flags;
60 wxTreeItemId pos = HitTest(event.GetPosition(), flags);
61 if (!(flags & TREE_MASK)) {
62 pos = wxTreeItemId();
64 if (pos == m_LastItem) return;
65 if (pos.IsOk()) {
66 m_Parent->DisplayTreeInfo(GetItemData(pos));
67 } else {
68 m_Parent->DisplayTreeInfo();
73 void AvenTreeCtrl::SetHere(wxTreeItemId pos)
75 if (pos == m_LastItem) return;
77 if (m_LastItem.IsOk()) {
78 SetItemBackgroundColour(m_LastItem, m_BackgroundColour);
80 if (pos.IsOk()) {
81 m_BackgroundColour = GetItemBackgroundColour(pos);
82 SetItemBackgroundColour(pos, wxColour(180, 180, 180));
84 m_LastItem = pos;
87 void AvenTreeCtrl::OnLeaveWindow(wxMouseEvent&)
89 if (m_LastItem.IsOk()) {
90 SetItemBackgroundColour(m_LastItem, m_BackgroundColour);
91 m_LastItem = wxTreeItemId();
93 m_Parent->DisplayTreeInfo();
96 void AvenTreeCtrl::SetEnabled(bool enabled)
98 m_Enabled = enabled;
101 void AvenTreeCtrl::OnSelChanged(wxTreeEvent& e)
103 if (m_Enabled) {
104 m_Parent->TreeItemSelected(GetItemData(e.GetItem()), false);
107 m_SelValid = true;
110 void AvenTreeCtrl::OnItemActivated(wxTreeEvent& e)
112 if (m_Enabled) {
113 m_Parent->TreeItemSelected(GetItemData(e.GetItem()), true);
114 // Need to skip to allow double-clicking to work on wxMSW >= 2.8.11.
115 e.Skip();
119 void AvenTreeCtrl::OnMenu(wxTreeEvent& e)
121 if (m_Enabled) {
122 const TreeData* data = static_cast<const TreeData*>(GetItemData(e.GetItem()));
123 menu_data = data;
124 if (!data) {
125 // Root:
126 wxMenu menu;
127 /* TRANSLATORS: In aven's survey tree, right-clicking on the root
128 * gives a pop-up menu and this is an option (but only enabled if
129 * the view is restricted to a subsurvey). It reloads the current
130 * survey file with the who survey visible.
132 menu.Append(menu_SURVEY_SHOW_ALL, wmsg(/*Show all*/245));
133 if (m_Parent->GetSurvey().empty())
134 menu.Enable(menu_SURVEY_SHOW_ALL, false);
135 PopupMenu(&menu);
136 } else if (data->GetLabel()) {
137 // Station: name is data->GetLabel()->GetText()
138 } else {
139 // Survey:
140 wxMenu menu;
141 /* TRANSLATORS: In aven's survey tree, right-clicking on a survey
142 * name gives a pop-up menu and this is an option. It reloads the
143 * current survey file with the view restricted to the survey
144 * clicked upon.
146 menu.Append(menu_SURVEY_RESTRICT, wmsg(/*Hide others*/246));
147 PopupMenu(&menu);
149 menu_data = NULL;
150 e.Skip();
154 bool AvenTreeCtrl::GetSelectionData(wxTreeItemData** data) const
156 assert(m_Enabled);
157 assert(data);
159 if (!m_SelValid) {
160 return false;
163 wxTreeItemId id = GetSelection();
164 if (id.IsOk()) {
165 *data = GetItemData(id);
168 return id.IsOk() && *data;
171 void AvenTreeCtrl::UnselectAll()
173 m_SelValid = false;
174 wxTreeCtrl::UnselectAll();
177 void AvenTreeCtrl::DeleteAllItems()
179 m_Enabled = false;
180 m_LastItem = wxTreeItemId();
181 m_SelValid = false;
182 wxTreeCtrl::DeleteAllItems();
185 void AvenTreeCtrl::OnKeyPress(wxKeyEvent &e)
187 switch (e.GetKeyCode()) {
188 case WXK_ESCAPE:
189 m_Parent->ClearTreeSelection();
190 break;
191 case WXK_RETURN: {
192 wxTreeItemId id = GetSelection();
193 if (id.IsOk()) {
194 if (ItemHasChildren(id)) {
195 // If on a branch, expand/contract it.
196 if (IsExpanded(id)) {
197 Collapse(id);
198 } else {
199 Expand(id);
201 } else {
202 // If on a station, centre on it.
203 m_Parent->TreeItemSelected(GetItemData(id), true);
206 break;
208 case WXK_LEFT: case WXK_RIGHT: case WXK_UP: case WXK_DOWN:
209 case WXK_HOME: case WXK_END: case WXK_PAGEUP: case WXK_PAGEDOWN:
210 e.Skip();
211 break;
212 default:
213 // Pass key event to MainFrm which will pass to GfxCore which will
214 // pass to GUIControl.
215 m_Parent->OnKeyPress(e);
216 break;
220 void AvenTreeCtrl::OnRestrict(wxCommandEvent& e)
222 m_Parent->RestrictTo(menu_data ? menu_data->GetSurvey() : wxString());
223 e.Skip();