Skip to content

Commit

Permalink
support selection and activation in tree controls
Browse files Browse the repository at this point in the history
  • Loading branch information
oltolm committed Nov 10, 2024
1 parent c88bb3b commit a3e840b
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 31 deletions.
2 changes: 1 addition & 1 deletion src/chmapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,4 @@ END_EVENT_TABLE()
#endif

// Apparently this macro gets main() pumping.
IMPLEMENT_APP(CHMApp)
wxIMPLEMENT_APP(CHMApp);
22 changes: 19 additions & 3 deletions src/chmframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
MA 02110-1301, USA.
*/

#include <algorithm>
#include <chmfontdialog.h>
#include <chmframe.h>
#include <chmhtmlnotebook.h>
Expand All @@ -35,11 +34,13 @@
#include <wx/artprov.h>
#include <wx/bitmap.h>
#include <wx/busyinfo.h>
#include <wx/config.h>
#include <wx/filesys.h>
#include <wx/fs_mem.h>
#include <wx/imaglist.h>
#include <wx/mimetype.h>
#include <wx/statbox.h>
#include <wx/treebase.h>
#include <wx/utils.h>
#include <wx/version.h>

Expand Down Expand Up @@ -407,11 +408,25 @@ void CHMFrame::OnBookmarkSel(wxCommandEvent& event)
_nbhtml->LoadPageInCurrentView(wxT("file:") + chmf->ArchiveName() + wxT("#xchm:/") + *url);
}

void CHMFrame::OnItemActivated(wxTreeEvent& event)
{
event.Skip();

LoadSelected(event.GetItem());
_nbhtml->GetCurrentPage()->SetFocus();
}

void CHMFrame::OnSelectionChanged(wxTreeEvent& event)
{
auto id = event.GetItem();
auto chmf = CHMInputStream::GetCache();
event.Skip();

LoadSelected(event.GetItem());
}

void CHMFrame::LoadSelected(wxTreeItemId id)
{
auto chmf = CHMInputStream::GetCache();

if (id == _tcl->GetRootItem() || !chmf || !id.IsOk())
return;

Expand Down Expand Up @@ -933,6 +948,7 @@ EVT_MENU(ID_ToggleToolbar, CHMFrame::OnToggleToolbar)
EVT_BUTTON(ID_Add, CHMFrame::OnAddBookmark)
EVT_BUTTON(ID_Remove, CHMFrame::OnRemoveBookmark)
EVT_TREE_SEL_CHANGED(ID_TreeCtrl, CHMFrame::OnSelectionChanged)
EVT_TREE_ITEM_ACTIVATED(ID_TreeCtrl, CHMFrame::OnItemActivated)
EVT_COMBOBOX(ID_Bookmarks, CHMFrame::OnBookmarkSel)
EVT_TEXT_ENTER(ID_Bookmarks, CHMFrame::OnBookmarkSel)
EVT_CLOSE(CHMFrame::OnCloseWindow)
Expand Down
7 changes: 6 additions & 1 deletion src/chmframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,12 @@ class CHMFrame : public wxFrame {
//! Called when the user chooses a bookmark from the wxChoice control.
void OnBookmarkSel(wxCommandEvent& event);

//! Called when an item in the contents tree is clicked.
//! Called when an item in the contents tree is selected.
void OnSelectionChanged(wxTreeEvent& event);

//! Called when an item in the contents tree is activated.
void OnItemActivated(wxTreeEvent& event);

//! Cleanup code. This saves the window position and last open dir.
void OnCloseWindow(wxCloseEvent& event);

Expand Down Expand Up @@ -229,6 +232,8 @@ class CHMFrame : public wxFrame {
//! Helper. Saves exit information (size, history, etc.)
void SaveExitInfo();

void LoadSelected(wxTreeItemId id);

private:
CHMHtmlNotebook* _nbhtml;
wxTreeCtrl* _tcl {nullptr};
Expand Down
1 change: 1 addition & 0 deletions src/chmhtmlwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <hhcparser.h>
#include <memory>
#include <wx/clipbrd.h>
#include <wx/event.h>
#include <wx/dnd.h>
#include <wx/filename.h>
#include <wx/log.h>
Expand Down
1 change: 1 addition & 0 deletions src/chmhtmlwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <chmfinddialog.h>
#include <memory>
#include <wx/event.h>
#include <wx/html/htmlwin.h>
#include <wx/menu.h>
#include <wx/notebook.h>
Expand Down
18 changes: 12 additions & 6 deletions src/chmindexpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
MA 02110-1301, USA.
*/

#include <chmhtmlnotebook.h>
#include <chmhtmlwindow.h>
#include <chmindexpanel.h>
#include <chmlistctrl.h>
#include <wx/sizer.h>

CHMIndexPanel::CHMIndexPanel(wxWindow* parent, CHMHtmlNotebook* nbhtml) : wxPanel(parent)
CHMIndexPanel::CHMIndexPanel(wxWindow* parent, CHMHtmlNotebook* nbhtml) : _nbhtml(nbhtml), wxPanel(parent)
{
auto sizer = new wxBoxSizer(wxVERTICAL);

Expand All @@ -47,16 +48,21 @@ void CHMIndexPanel::SetNewFont(const wxFont& font)
_lc->SetFont(font);
}

void CHMIndexPanel::OnIndexSelRet(wxCommandEvent&)
void CHMIndexPanel::OnIndexSel(wxListEvent& event)
{
event.Skip();

if (_navigate)
_lc->LoadSelected();
_lc->LoadSelected(event.GetIndex());
}

void CHMIndexPanel::OnIndexSel(wxListEvent&)
void CHMIndexPanel::OnItemActivated(wxListEvent& event)
{
event.Skip();

if (_navigate)
_lc->LoadSelected();
_lc->LoadSelected(event.GetIndex());
_nbhtml->GetCurrentPage()->SetFocus();
}

void CHMIndexPanel::OnText(wxCommandEvent&)
Expand All @@ -68,6 +74,6 @@ void CHMIndexPanel::OnText(wxCommandEvent&)

BEGIN_EVENT_TABLE(CHMIndexPanel, wxPanel)
EVT_TEXT(ID_SearchIndex, CHMIndexPanel::OnText)
EVT_TEXT_ENTER(ID_SearchIndex, CHMIndexPanel::OnIndexSelRet)
EVT_LIST_ITEM_SELECTED(ID_IndexClicked, CHMIndexPanel::OnIndexSel)
EVT_LIST_ITEM_ACTIVATED(ID_IndexClicked, CHMIndexPanel::OnItemActivated)
END_EVENT_TABLE()
8 changes: 5 additions & 3 deletions src/chmindexpanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef __CHMINDEXPANEL_H_
#define __CHMINDEXPANEL_H_

#include <wx/app.h>
#include <wx/listctrl.h>
#include <wx/panel.h>
#include <wx/textctrl.h>
Expand Down Expand Up @@ -61,11 +62,11 @@ class CHMIndexPanel : public wxPanel {
void SetNewFont(const wxFont& font);

protected:
//! This gets called when the user clicks on a list item.
//! This gets called when the user selects a list item.
void OnIndexSel(wxListEvent& event);

//! This gets called when the user presses enter on a list item.
void OnIndexSelRet(wxCommandEvent& event);
//! This gets called when the user activates a list item.
void OnItemActivated(wxListEvent& event);

//! Called whenever the user types a letter in the textbox.
void OnText(wxCommandEvent& event);
Expand All @@ -74,6 +75,7 @@ class CHMIndexPanel : public wxPanel {
wxTextCtrl* _text;
CHMListCtrl* _lc {nullptr};
bool _navigate {true};
CHMHtmlNotebook* _nbhtml;

private:
DECLARE_EVENT_TABLE()
Expand Down
14 changes: 4 additions & 10 deletions src/chmlistctrl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int CompareItemPairs(CHMListPairItem* item1, CHMListPairItem* item2)
// CHMListCtrl implementation

CHMListCtrl::CHMListCtrl(wxWindow* parent, CHMHtmlNotebook* nbhtml, wxWindowID id)
: wxListCtrl(parent, id, wxDefaultPosition, wxDefaultSize,
: wxListView(parent, id, wxDefaultPosition, wxDefaultSize,
wxLC_VIRTUAL | wxLC_REPORT | wxLC_NO_HEADER | wxLC_SINGLE_SEL | wxLC_SORT_ASCENDING | wxSUNKEN_BORDER),
_items(CompareItemPairs), _nbhtml(nbhtml)
{
Expand Down Expand Up @@ -70,14 +70,8 @@ void CHMListCtrl::AddPairItem(const wxString& title, const wxString& url)
_items.Add(new CHMListPairItem(title, url));
}

void CHMListCtrl::LoadSelected()
void CHMListCtrl::LoadSelected(long item)
{
auto item = -1L;
item = GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);

if (item == -1L || item > static_cast<long>(_items.GetCount()) - 1)
return;

auto chmf = CHMInputStream::GetCache();

if (chmf) {
Expand All @@ -100,8 +94,8 @@ void CHMListCtrl::FindBestMatch(const wxString& title)
for (size_t i = 0; i < _items.size(); ++i) {
if (!_items[i]->_title.Left(title.length()).CmpNoCase(title)) {
EnsureVisible(i);
SetItemState(i, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
SetItemState(i, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
Select(i);
Focus(i);
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/chmlistctrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int CompareItemPairs(CHMListPairItem* item1, CHMListPairItem* item2);
*/

//! List control class meant to emulate the look and feel of a wxListBox.
class CHMListCtrl : public wxListCtrl {
class CHMListCtrl : public wxListView {

public:
/*!
Expand Down Expand Up @@ -85,7 +85,7 @@ class CHMListCtrl : public wxListCtrl {
void AddPairItem(const wxString& title, const wxString& url);

//! Loads the page that corresponds to the item currently selected.
void LoadSelected();
void LoadSelected(long item);

//! Should be called each time the list control's state changes.
void UpdateUI();
Expand Down
18 changes: 15 additions & 3 deletions src/chmsearchpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <algorithm>
#include <chmhtmlnotebook.h>
#include <chmhtmlwindow.h>
#include <chminputstream.h>
#include <chmlistctrl.h>
#include <chmsearchpanel.h>
Expand All @@ -31,7 +32,7 @@
#include <wx/wx.h>

CHMSearchPanel::CHMSearchPanel(wxWindow* parent, wxTreeCtrl* topics, CHMHtmlNotebook* nbhtml)
: wxPanel(parent), _tcl(topics)
: wxPanel(parent), _tcl(topics), _nbhtml(nbhtml)
{
auto sizer = new wxBoxSizer(wxVERTICAL);

Expand Down Expand Up @@ -203,9 +204,19 @@ bool CHMSearchPanel::TitleSearch(const wxString& title, const wxString& text, bo
return processedTokens;
}

void CHMSearchPanel::OnSearchSel(wxListEvent&)
void CHMSearchPanel::OnSearchSel(wxListEvent& event)
{
_results->LoadSelected();
event.Skip();

_results->LoadSelected(event.GetIndex());
}

void CHMSearchPanel::OnItemActivated(wxListEvent& event)
{
event.Skip();

_results->LoadSelected(event.GetIndex());
_nbhtml->GetCurrentPage()->SetFocus();
}

void CHMSearchPanel::Reset()
Expand Down Expand Up @@ -241,6 +252,7 @@ void CHMSearchPanel::GetConfig()

BEGIN_EVENT_TABLE(CHMSearchPanel, wxPanel)
EVT_LIST_ITEM_SELECTED(ID_Results, CHMSearchPanel::OnSearchSel)
EVT_LIST_ITEM_ACTIVATED(ID_Results, CHMSearchPanel::OnItemActivated)
EVT_BUTTON(ID_SearchButton, CHMSearchPanel::OnSearch)
EVT_TEXT_ENTER(ID_SearchText, CHMSearchPanel::OnSearch)
END_EVENT_TABLE()
6 changes: 5 additions & 1 deletion src/chmsearchpanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,12 @@ class CHMSearchPanel : public wxPanel {
*/
void OnSearch(wxCommandEvent& event);

//! This gets called when the user clicks on a result.
//! This gets called when the user selects a result.
void OnSearchSel(wxListEvent& event);

//! This gets called when the user activates a result.
void OnItemActivated(wxListEvent& event);

private:
//! Helper. Searches through the tree recursively.
void PopulateList(wxTreeItemId root, const wxString& text, bool wholeWords);
Expand All @@ -101,6 +104,7 @@ class CHMSearchPanel : public wxPanel {
wxCheckBox* _titles;
wxButton* _search;
CHMListCtrl* _results;
CHMHtmlNotebook* _nbhtml;

private:
DECLARE_EVENT_TABLE()
Expand Down
1 change: 0 additions & 1 deletion src/hhcparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <chmlistctrl.h>
#include <ctype.h>
#include <hhcparser.h>
#include <map>
#include <stdlib.h>
#include <wx/wx.h>
#include <wxstringutils.h>
Expand Down

0 comments on commit a3e840b

Please sign in to comment.