Converted OnKey into a key-command mapping table
[ttodo.git] / elist.cc
blob89335be09230596cf464430e61f1b373782a5805
1 // elist.cc
2 //
4 #include "elist.h"
5 #include <stdio.h>
7 //----------------------------------------------------------------------
9 const CTodoList::SCommandKey CTodoList::c_CmdKeys[] = {
10 { 'q', cmd_File_Quit },
11 { 'S', cmd_File_Save },
12 { 'l', cmd_List_Enter },
13 { kv_Enter, cmd_List_Enter },
14 { kv_Right, cmd_List_Enter },
15 { kv_Left, cmd_List_Leave },
16 { 'h', cmd_List_Leave },
17 { 'o', cmd_List_OldItems },
18 { 'c', cmd_List_Copy },
19 { 'v', cmd_List_Paste },
20 { kv_Insert, cmd_Item_New },
21 { 'n', cmd_Item_New },
22 { kv_Delete, cmd_Item_Delete },
23 { 'D', cmd_Item_Delete },
24 { kv_Space, cmd_Item_Complete },
25 { 'e', cmd_Item_Edit },
26 { '1', cmd_Item_Priority_Highest },
27 { '2', cmd_Item_Priority_High },
28 { '3', cmd_Item_Priority_Medium },
29 { '4', cmd_Item_Priority_Low },
30 { '5', cmd_Item_Priority_Lowest }
33 /// Constructs a list displaying \p rctdl
34 CTodoList::CTodoList (void)
35 : CListbox (),
36 m_pTodos (NULL),
37 m_CopiedId (0)
41 /// Sets the list to display.
42 void CTodoList::SetList (pctodolist_t pl)
44 SetCommandKeys (VectorBlock (c_CmdKeys));
45 m_pTodos = pl;
46 if (m_pTodos)
47 SetListSize (pl->size());
50 /// Sets internal variables when the document is updated.
51 void CTodoList::OnUpdate (void)
53 CListbox::OnUpdate();
54 SetList (&Document()->List());
55 SetSelection (Document()->Selection());
58 /// Draws list item \p ii at \p pos onto \p gc.
59 void CTodoList::OnDrawItem (CGC& gc, rcpos_t pos, uint32_t ii)
61 CListbox::OnDrawItem (gc, pos, ii);
62 if (!m_pTodos)
63 return;
64 const CTodoEntry& e ((*m_pTodos)[ii]);
65 char progBuf [5];
66 gc.Char (pos, "+ "[!e.HasSublist()]);
67 snprintf (progBuf, 5, "%3u%%", e.Progress());
68 gc.Text (pos[0] + 1, pos[1], e.Progress() ? progBuf : " ");
69 static const EColor c_PriorityColors [2][CTodoEntry::priority_Last] = {
70 { white, lightred, yellow, green, cyan, lightblue },
71 { white, lightred, yellow, lightgreen, lightcyan, lightblue }
73 gc.FgColor (c_PriorityColors [ii == Selection()][e.Priority()]);
74 gc.Text (pos[0] + 6, pos[1], e.Text());
77 /// Causes the current entry to be edited.
78 inline void CTodoList::EditEntry (void)
80 SetFlag (f_OffersFocus);
83 /// Processes keystroke \p key.
84 void CTodoList::OnKey (wchar_t key)
86 CListbox::OnKey (key);
87 pdoc_t pDoc = Document();
88 if (pDoc->Selection() != Selection())
89 pDoc->SetSelection (Selection());
92 /// Executes command \p c.
93 void CTodoList::OnCommand (cmd_t c)
95 CListbox::OnCommand (c);
96 pdoc_t pDoc = Document();
97 switch (c) {
98 case cmd_List_Leave: pDoc->LeaveCurrentEntry(); break;
99 case cmd_List_OldItems: pDoc->ToggleCompleteVisible(); break;
100 case cmd_List_Paste: pDoc->PasteLinkToEntry (m_CopiedId); break;
101 case cmd_List_Enter: pDoc->EnterCurrentEntry(); break;
102 case cmd_Item_Delete: pDoc->RemoveCurrentEntry(); break;
103 case cmd_Item_Complete: pDoc->MarkEntryComplete(); break;
104 case cmd_Item_New: pDoc->AppendEntry();
105 case cmd_Item_Edit: EditEntry(); break;
106 case cmd_List_Copy: m_CopiedId = pDoc->CurrentEntry().Id(); break;
107 case cmd_Item_Priority_Highest:
108 case cmd_Item_Priority_High:
109 case cmd_Item_Priority_Medium:
110 case cmd_Item_Priority_Low:
111 case cmd_Item_Priority_Lowest:
112 pDoc->SetCurrentEntryPriority (CTodoEntry::EPriority (c - cmd_Item_Priority_Highest + 1));
113 break;
117 void CTodoList::OnUpdateCommandUI (rcmd_t rc) const
119 CListbox::OnUpdateCommandUI (rc);
120 pcdoc_t pDoc = Document();
121 assert (pDoc && "The frame should stop UpdateCommandUI if there is no document");
122 const bool bHaveSelection (pDoc->Selection() < pDoc->ListSize());
123 const bool bReadOnly (Flag (f_ReadOnly));
124 bool bActive = true;
125 switch (rc.cmd) {
126 case cmd_List_Leave: bActive = pDoc->NestingDepth(); break;
127 case cmd_List_OldItems:
128 rc.SetFlag (SCmd::cf_Checked, pDoc->Flag (CTodoDocument::f_CompleteVisible));
129 break;
130 case cmd_List_Paste: bActive = !bReadOnly && m_CopiedId; break;
131 case cmd_Item_New: break;
132 case cmd_List_Enter: bActive = bHaveSelection; break;
133 case cmd_Item_Delete: bActive = bHaveSelection && !bReadOnly; break;
134 case cmd_Item_Complete: bActive = bHaveSelection && !bReadOnly; break;
135 case cmd_Item_Edit: bActive = bHaveSelection && !bReadOnly; break;
136 case cmd_List_Copy: bActive = bHaveSelection; break;
137 case cmd_Item_Priority: bActive = bHaveSelection; break;
138 case cmd_Item_Priority_Highest:
139 case cmd_Item_Priority_High:
140 case cmd_Item_Priority_Medium:
141 case cmd_Item_Priority_Low:
142 case cmd_Item_Priority_Lowest:
143 bActive = bHaveSelection && !bReadOnly;
144 rc.SetFlag (SCmd::cf_Checked, rc.cmd == pDoc->CurrentEntry().Priority() + cmd_Item_Priority_Highest - 1);
145 break;
146 default:
147 bActive = !(rc.flags & SCmd::cf_Grayed);
148 break;
150 rc.SetFlag (SCmd::cf_Grayed, !bActive);