Revised file format to conform to standard IFF layout
[ttodo.git] / ilist.cc
blobed713a4b5c5c8c9666989b94088b151421f2f5f6
1 // ilist.cc
2 //
4 #include "ilist.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)
39 SetCommandKeys (VectorBlock (c_CmdKeys));
42 /// Sets the list to display.
43 void CTodoList::SetList (pctdevec_t pl)
45 if ((m_pTodos = pl))
46 SetListSize (m_pTodos->size());
49 /// Sets internal variables when the document is updated.
50 void CTodoList::OnUpdate (void)
52 CListbox::OnUpdate();
53 SetList (&Document()->List());
54 SetSelection (Document()->Selection());
57 /// Draws list item \p ii at \p pos onto \p gc.
58 void CTodoList::OnDrawItem (CGC& gc, rcpos_t pos, uint32_t ii)
60 CListbox::OnDrawItem (gc, pos, ii);
61 if (!m_pTodos)
62 return;
63 const CTodoItem& e ((*m_pTodos)[ii]);
64 char progBuf [5];
65 gc.Char (pos, "+ "[!e.HasSublist()]);
66 snprintf (progBuf, 5, "%3u%%", e.Progress());
67 gc.Text (pos[0] + 1, pos[1], e.Progress() ? progBuf : " ");
68 static const EColor c_PriorityColors [2][CTodoItem::priority_Last] = {
69 { white, lightred, yellow, green, cyan, lightblue },
70 { white, lightred, yellow, lightgreen, lightcyan, lightblue }
72 gc.FgColor (c_PriorityColors [ii == Selection()][e.Priority()]);
73 gc.Text (pos[0] + 6, pos[1], e.Text());
76 /// Causes the current entry to be edited.
77 inline void CTodoList::EditItem (void)
79 SetFlag (f_OffersFocus);
82 /// Processes keystroke \p key.
83 void CTodoList::OnKey (wchar_t key)
85 CListbox::OnKey (key);
86 if (Document()->Selection() != Selection())
87 Document()->SetSelection (Selection());
90 /// Executes command \p c.
91 void CTodoList::OnCommand (cmd_t c)
93 CListbox::OnCommand (c);
94 pdoc_t pDoc = Document();
95 switch (c) {
96 case cmd_Item_New: pDoc->AppendItem();
97 case cmd_Item_Edit: EditItem(); break;
98 case cmd_List_Copy: m_CopiedId = pDoc->CurrentItem().Id(); break;
99 case cmd_List_Paste: pDoc->PasteLinkToItem (m_CopiedId); break;
103 void CTodoList::OnUpdateCommandUI (rcmd_t rc) const
105 CListbox::OnUpdateCommandUI (rc);
106 pcdoc_t pDoc = Document();
107 const bool bHaveSelection (pDoc->Selection() < pDoc->List().size());
108 const bool bReadOnly (Flag (f_ReadOnly));
109 bool bActive = true;
110 switch (rc.cmd) {
111 case cmd_Item_New: break;
112 case cmd_Item_Edit: bActive = bHaveSelection && !bReadOnly; break;
113 case cmd_List_Copy: bActive = bHaveSelection; break;
114 case cmd_List_Paste: bActive = !bReadOnly && m_CopiedId; break;
115 default:
116 bActive = !(rc.flags & SCmd::cf_Grayed);
117 break;
119 rc.SetFlag (SCmd::cf_Grayed, !bActive);