source: git/src/aventreectrl.cc @ 3e7c35f

RELEASE/1.0
Last change on this file since 3e7c35f was 3e7c35f, checked in by Olly Betts <olly@…>, 13 years ago

src/aventreectrl.cc: Correctly capitalise "GTK".

git-svn-id: file:///home/survex-svn/survex/branches/1.0@3721 4b37db11-9a0c-4f06-9ece-9ab7cdaee568

  • Property mode set to 100644
File size: 4.2 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,2010 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_CHAR(AvenTreeCtrl::OnKeyPress)
37END_EVENT_TABLE()
38
39AvenTreeCtrl::AvenTreeCtrl(MainFrm* parent, wxWindow* window_parent) :
40    wxTreeCtrl(window_parent),
41    m_Parent(parent),
42    m_Enabled(false),
43    m_LastItem(),
44    m_BackgroundColour(GetBackgroundColour()),
45    m_SelValid(false)
46{
47}
48
49#define TREE_MASK (wxTREE_HITTEST_ONITEMLABEL | wxTREE_HITTEST_ONITEMRIGHT)
50
51void AvenTreeCtrl::OnMouseMove(wxMouseEvent& event)
52{
53    if (m_Enabled) {
54        int flags;
55        wxTreeItemId pos = HitTest(event.GetPosition(), flags);
56        if (!(flags & TREE_MASK)) {
57            pos = wxTreeItemId();
58        }
59        if (pos != m_LastItem) {
60            if (m_LastItem.IsOk()) {
61                SetItemBackgroundColour(m_LastItem, m_BackgroundColour);
62            }
63            if (pos.IsOk()) {
64                SetItemBackgroundColour(pos, wxColour(180, 180, 180));
65                m_Parent->DisplayTreeInfo(GetItemData(pos));
66            } else {
67                m_Parent->DisplayTreeInfo(NULL);
68            }
69            m_LastItem = pos;
70        }
71    }
72}
73
74void 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
83void AvenTreeCtrl::SetEnabled(bool enabled)
84{
85    m_Enabled = enabled;
86}
87
88void AvenTreeCtrl::OnSelChanged(wxTreeEvent&)
89{
90    if (m_Enabled) {
91        m_Parent->TreeItemSelected(GetItemData(GetSelection()));
92    }
93
94    m_SelValid = true;
95}
96
97bool AvenTreeCtrl::GetSelectionData(wxTreeItemData** data)
98{
99    assert(m_Enabled);
100    assert(data);
101
102    if (!m_SelValid) {
103        return false;
104    }
105
106    wxTreeItemId id = GetSelection();
107    if (id.IsOk()) {
108        *data = GetItemData(id);
109    }
110
111    return id.IsOk() && *data;
112}
113
114void AvenTreeCtrl::UnselectAll()
115{
116    m_SelValid = false;
117    wxTreeCtrl::UnselectAll();
118}
119
120void AvenTreeCtrl::DeleteAllItems()
121{
122    m_Enabled = false;
123    m_LastItem = wxTreeItemId();
124    m_SelValid = false;
125    wxTreeCtrl::DeleteAllItems();
126}
127
128void AvenTreeCtrl::OnKeyPress(wxKeyEvent &e)
129{
130    switch (e.GetKeyCode()) {
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:
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)
159        case WXK_PRIOR: case WXK_NEXT:
160#endif
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;
168    }
169}
Note: See TracBrowser for help on using the repository browser.