source: git/src/aventreectrl.cc @ ecbc6c18

RELEASE/1.1RELEASE/1.2debug-cidebug-ci-sanitisersstereowalls-data
Last change on this file since ecbc6c18 was ecbc6c18, checked in by Olly Betts <olly@…>, 14 years ago

src/: Update FSF address in licence notices.

git-svn-id: file:///home/survex-svn/survex/branches/survex-1_1@3417 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 4.5 KB
Line 
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
32BEGIN_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)
38END_EVENT_TABLE()
39
40AvenTreeCtrl::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
52void 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
69void 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
83void 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
92void AvenTreeCtrl::SetEnabled(bool enabled)
93{
94    m_Enabled = enabled;
95}
96
97void 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
106void AvenTreeCtrl::OnItemActivated(wxTreeEvent& e)
107{
108    if (m_Enabled) {
109        m_Parent->TreeItemSelected(GetItemData(e.GetItem()), true);
110    }
111}
112
113bool AvenTreeCtrl::GetSelectionData(wxTreeItemData** data) const
114{
115    assert(m_Enabled);
116    assert(data);
117
118    if (!m_SelValid) {
119        return false;
120    }
121
122    wxTreeItemId id = GetSelection();
123    if (id.IsOk()) {
124        *data = GetItemData(id);
125    }
126
127    return id.IsOk() && *data;
128}
129
130void AvenTreeCtrl::UnselectAll()
131{
132    m_SelValid = false;
133    wxTreeCtrl::UnselectAll();
134}
135
136void AvenTreeCtrl::DeleteAllItems()
137{
138    m_Enabled = false;
139    m_LastItem = wxTreeItemId();
140    m_SelValid = false;
141    wxTreeCtrl::DeleteAllItems();
142}
143
144void AvenTreeCtrl::OnKeyPress(wxKeyEvent &e)
145{
146    switch (e.m_keyCode) {
147        case WXK_ESCAPE:
148            m_Parent->ClearTreeSelection();
149            break;
150        case WXK_RETURN: {
151            wxTreeItemId id = GetSelection();
152            if (id.IsOk()) {
153                if (ItemHasChildren(id)) {
154                    // If on a branch, expand/contract it.
155                    if (IsExpanded(id)) {
156                        Collapse(id);
157                    } else {
158                        Expand(id);
159                    }
160                } else {
161                    // FIXME if on a station, show information on that station
162                    // or something?
163                }
164            }
165            break;
166        }
167        case WXK_LEFT: case WXK_RIGHT: case WXK_UP: case WXK_DOWN:
168        case WXK_HOME: case WXK_END: case WXK_PAGEUP: case WXK_PAGEDOWN:
169        // On wx 2.6 and earlier, PRIOR/NEXT seem to actually be
170        // PAGEUP/PAGEDOWN (on wxGtk at least).  In wx 2.7 and later
171        // they're just compatibility aliases, so either they have the
172        // same value or aren't defined - either way the code won't
173        // compile.
174#if !wxCHECK_VERSION(2,7,0)
175        case WXK_PRIOR: case WXK_NEXT:
176#endif
177            e.Skip();
178            break;
179        default:
180            // Pass key event to MainFrm which will pass to GfxCore which will
181            // pass to GUIControl.
182            m_Parent->OnKeyPress(e);
183            break;
184    }
185}
Note: See TracBrowser for help on using the repository browser.