From 002adf2a5656a127c34225f1d405fcad6dc3fb42 Mon Sep 17 00:00:00 2001 From: msharov Date: Wed, 16 Aug 2006 22:46:38 +0000 Subject: [PATCH] Separated document from view in frame --- elist.cc | 19 +++++++++++---- elist.h | 8 +++---- frame.cc | 82 ++++++++++++++++++++++------------------------------------------ frame.h | 16 ++++++++----- tddoc.cc | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ tddoc.h | 35 +++++++++++++++++++++++++++ 6 files changed, 153 insertions(+), 69 deletions(-) create mode 100644 tddoc.cc create mode 100644 tddoc.h diff --git a/elist.cc b/elist.cc index 6fd6770..7d802bb 100644 --- a/elist.cc +++ b/elist.cc @@ -6,19 +6,28 @@ //---------------------------------------------------------------------- /// Constructs a list displaying \p rctdl -CTodoList::CTodoList (rctodolist_t rctdl) -: CListbox (), - m_Todos (rctdl) +CTodoList::CTodoList (void) +: CListbox () { - SetListSize (m_Todos.size()); + SetList (NULL); +} + +/// Sets the list to display. +void CTodoList::SetList (pctodolist_t pl) +{ + m_pTodos = pl; + if (m_pTodos) + SetListSize (pl->size()); } /// Draws list item \p ii at \p pos onto \p gc. void CTodoList::OnDrawItem (CGC& gc, rcpos_t pos, uint32_t ii) { CListbox::OnDrawItem (gc, pos, ii); + if (!m_pTodos) + return; string line; - const CTodoEntry& e (m_Todos[ii]); + const CTodoEntry& e ((*m_pTodos)[ii]); line.format ("%c%3u%% %s", "+ "[!e.m_SublistId], e.Progress(), e.m_Text.c_str()); gc.Text (pos, line); } diff --git a/elist.h b/elist.h index 6875b3e..57b9859 100644 --- a/elist.h +++ b/elist.h @@ -10,13 +10,13 @@ /// List of todo entries. class CTodoList : public CListbox { public: - typedef const todolist_t& rctodolist_t; + typedef const todolist_t* pctodolist_t; public: - CTodoList (rctodolist_t rctdl); - inline void Update (rctodolist_t) { SetListSize (m_Todos.size()); } + CTodoList (void); + void SetList (pctodolist_t pl); virtual void OnDrawItem (CGC& gc, rcpos_t pos, uint32_t ii); private: - rctodolist_t m_Todos; ///< Link to the data. + pctodolist_t m_pTodos; ///< Link to the data. }; #endif diff --git a/frame.cc b/frame.cc index b9fbe4e..f3d519e 100644 --- a/frame.cc +++ b/frame.cc @@ -9,37 +9,20 @@ /// Default constructor. CTodoFrame::CTodoFrame (void) : CWindow (), - m_Todos (), - m_Selection (SIZE_MAX) + m_Doc () { + m_Doc.RegisterView (this); } /// Creates all child windows. void CTodoFrame::OnCreate (void) { if (access (".todo", R_OK) == 0) - LoadData(); - AddChild (new CTodoList (m_Todos)); + Document()->LoadData(); + AddChild (new CTodoList); AddChild (new CEntryEditDialog); assert (pane_Last == Children().size()); CWindow::OnCreate(); - OnSelChanged (0); -} - -void CTodoFrame::LoadData (void) -{ - memblock buf; - buf.read_file (".todo"); - istream is (buf); - is >> m_Todos; -} - -void CTodoFrame::SaveData (void) const -{ - memblock buf (stream_size_of (m_Todos)); - ostream os (buf); - os << m_Todos; - buf.write_file (".todo"); } void CTodoFrame::OnResize (rcrect_t wr) @@ -53,49 +36,42 @@ void CTodoFrame::EditorPaneKey (wchar_t) { if (!EntryEditor().Flag (flag_OffersFocus)) return; - if (EntryEditor().Flag (flag_Changed)) { - m_Todos[m_Selection] = EntryEditor().Entry(); - SetFlag (flag_Changed); - } else if (m_Todos[m_Selection].m_Text.empty()) { - m_Todos.erase (m_Todos.iat (m_Selection)); - OnSelChanged (min (m_Selection - 1, uoff_t (m_Todos.size() - 1))); - } + if (EntryEditor().Flag (flag_Changed)) + Document()->UpdateCurrentEntry (EntryEditor().Entry()); + else if (Document()->CurrentEntry().m_Text.empty()) + Document()->RemoveCurrentEntry(); SetFocus (pane_Entries); } void CTodoFrame::EntryPaneKey (wchar_t key) { - if (m_Selection != TodoList().Selection()) - OnSelChanged (TodoList().Selection()); + if (Document()->Selection() != TodoList().Selection()) + Document()->SetSelection (TodoList().Selection()); if (key == 'S') { - SaveData(); + Document()->SaveData(); Commit(); } else if (key == 'R') { - LoadData(); - OnSelChanged (0); + Document()->LoadData(); } else if (key == 'q') { Close(); } else if (key == 'n') { - m_Todos.push_back(); - OnSelChanged (m_Todos.size() - 1); + Document()->SetSelection (Document()->ListSize() - 1); + Document()->AppendEntry(); EditEntry(); } - if (m_Selection >= m_Todos.size()) + if (Document()->Selection() >= Document()->ListSize()) return; - if (key == kv_Space) { // Toggle complete flag - m_Todos[m_Selection].MarkComplete (!m_Todos[m_Selection].Complete() * 100); - OnSelChanged (m_Selection); - } else if (key == kv_Enter || key == 'e') { + if (key == kv_Space) // Toggle complete flag + Document()->MarkEntryComplete(); + else if (key == kv_Enter || key == 'e') EditEntry(); - } else if (key == kv_Delete || key == 'd') { - m_Todos.erase (m_Todos.iat (m_Selection)); - OnSelChanged (min (m_Selection, uoff_t (m_Todos.size() - 1))); - } else if (key == kv_Insert || key == 'i') { - m_Todos.insert (m_Todos.iat (m_Selection) + 1); - OnSelChanged (m_Selection + 1); + else if (key == kv_Delete || key == 'd') + Document()->RemoveCurrentEntry(); + else if (key == kv_Insert || key == 'i') { + Document()->AppendEntry(); EditEntry(); } } @@ -113,15 +89,13 @@ void CTodoFrame::OnKey (wchar_t key) (this->*s_PaneHandler[Focus()])(key); } -void CTodoFrame::OnSelChanged (uoff_t sel) +void CTodoFrame::OnUpdate (void) { - m_Selection = sel; - TodoList().Update (m_Todos); - TodoList().SetSelection (m_Selection); - if (sel < m_Todos.size()) - EntryEditor().SetEntry (m_Todos[sel]); - else - EntryEditor().SetEntry (CTodoEntry()); + if (Children().size() != pane_Last) + return; + TodoList().SetList (&Document()->List()); + TodoList().SetSelection (Document()->Selection()); + EntryEditor().SetEntry (Document()->CurrentEntry()); } void CTodoFrame::EditEntry (void) diff --git a/frame.h b/frame.h index 3e4661e..58cfd83 100644 --- a/frame.h +++ b/frame.h @@ -8,6 +8,7 @@ #include "elist.h" #include "eedit.h" +#include "tddoc.h" /// \class CTodoFrame tdframe.h tdframe.h class CTodoFrame : public CWindow { @@ -19,23 +20,26 @@ public: private: typedef CTodoList& relist_t; typedef CEntryEditDialog& reedit_t; + typedef CTodoDocument* pdoc_t; + typedef const CTodoDocument* pcdoc_t; enum EPane { pane_Entries, pane_Editor, pane_Last }; private: - void LoadData (void); - void SaveData (void) const; void OnSelChanged (uoff_t sel); void EditEntry (void); - inline relist_t TodoList (void) { return (static_cast(CW(pane_Entries))); } - inline reedit_t EntryEditor (void) { return (static_cast(CW(pane_Editor))); } + inline relist_t TodoList (void) { return (TCW(pane_Entries)); } + inline reedit_t EntryEditor (void) { return (TCW(pane_Editor)); } void EntryPaneKey (wchar_t key); void EditorPaneKey (wchar_t key); + virtual void OnUpdate (void); private: - todolist_t m_Todos; ///< List of entries. - uoff_t m_Selection; ///< Index of the selected entry. + inline pdoc_t Document (void) { return (TDocument()); } + inline pcdoc_t Document (void) const { return (TDocument()); } +private: + CTodoDocument m_Doc; }; #endif diff --git a/tddoc.cc b/tddoc.cc new file mode 100644 index 0000000..6b5b295 --- /dev/null +++ b/tddoc.cc @@ -0,0 +1,62 @@ +// Copyright (c) 2006 by Mike Sharov +// +// tddoc.cc +// + +#include "tddoc.h" + +/// Default constructor. +CTodoDocument::CTodoDocument (void) +: CDocument (), + m_Todos (), + m_Selection (SIZE_MAX) +{ +} + +void CTodoDocument::LoadData (void) +{ + memblock buf; + buf.read_file (".todo"); + istream is (buf); + is >> m_Todos; + SetSelection (0); +} + +void CTodoDocument::SaveData (void) const +{ + memblock buf (stream_size_of (m_Todos)); + ostream os (buf); + os << m_Todos; + buf.write_file (".todo"); +} + +void CTodoDocument::UpdateCurrentEntry (rcentry_t e) +{ + m_Todos[m_Selection] = e; + SetFlag (flag_Changed); +} + +CTodoDocument::rcentry_t CTodoDocument::CurrentEntry (void) const +{ + static const CTodoEntry s_NullEntry; + return (m_Selection < m_Todos.size() ? m_Todos[m_Selection] : s_NullEntry); +} + +void CTodoDocument::RemoveCurrentEntry (void) +{ + m_Todos.erase (m_Todos.iat (m_Selection)); + SetSelection (min (m_Selection, uoff_t (m_Todos.size() - 1))); +} + +void CTodoDocument::AppendEntry (void) +{ + m_Todos.insert (m_Todos.iat (m_Selection + 1)); + SetSelection (m_Selection + 1); +} + +void CTodoDocument::MarkEntryComplete (void) +{ + m_Todos[m_Selection].MarkComplete (!m_Todos[m_Selection].Complete() * 100); + SetSelection (m_Selection); +} + diff --git a/tddoc.h b/tddoc.h new file mode 100644 index 0000000..f897b71 --- /dev/null +++ b/tddoc.h @@ -0,0 +1,35 @@ +// Copyright (c) 2006 by Mike Sharov +// +// tddoc.h +// + +#ifndef TDDOC_H_31474BC008E2711A4DD60C960CFF3439 +#define TDDOC_H_31474BC008E2711A4DD60C960CFF3439 + +#include "tde.h" +#include "ui/doc.h" + +/// \class CTodoDocument tddoc.h tddoc.h +class CTodoDocument : public CDocument { +public: + typedef const todolist_t& rctodolist_t; + typedef const CTodoEntry& rcentry_t; +public: + CTodoDocument (void); + void LoadData (void); + void SaveData (void) const; + void UpdateCurrentEntry (rcentry_t e); + rcentry_t CurrentEntry (void) const; + inline uoff_t Selection (void) const { return (m_Selection); } + inline size_t ListSize (void) const { return (m_Todos.size()); } + inline rctodolist_t List (void) const { return (m_Todos); } + inline void SetSelection (uoff_t v) { m_Selection = v; UpdateAllViews(); } + void RemoveCurrentEntry (void); + void AppendEntry (void); + void MarkEntryComplete (void); +private: + todolist_t m_Todos; ///< List of entries. + uoff_t m_Selection; ///< Index of the selected entry. +}; + +#endif -- 2.11.4.GIT