Switched to git
[ttodo.git] / ilist.cc
blobb7a085f049b8dec2c235de30d85100ca826b2dd8
1 // This file is part of a terminal todo application.
2 //
3 // Copyright (C) 2006 by Mike Sharov <msharov@users.sourceforge.net>
4 // This file is free software, distributed under the MIT License.
5 //
6 // ilist.cc
7 //
9 #include "ilist.h"
10 #include <stdio.h>
12 //----------------------------------------------------------------------
14 const CTodoList::SCommandKey CTodoList::c_CmdKeys[] = {
15 { 'q', cmd_File_Quit },
16 { 'S', cmd_File_Save },
17 { 'l', cmd_List_Enter },
18 { kv_Enter, cmd_List_Enter },
19 { kv_Right, cmd_List_Enter },
20 { kv_Left, cmd_List_Leave },
21 { 'h', cmd_List_Leave },
22 { 'o', cmd_List_OldItems },
23 { 'c', cmd_List_Copy },
24 { 'v', cmd_List_Paste },
25 { kv_Insert, cmd_Item_New },
26 { 'n', cmd_Item_New },
27 { kv_Delete, cmd_Item_Delete },
28 { 'D', cmd_Item_Delete },
29 { kv_Space, cmd_Item_Complete },
30 { 'e', cmd_Item_Edit },
31 { '1', cmd_Item_Priority_Highest },
32 { '2', cmd_Item_Priority_High },
33 { '3', cmd_Item_Priority_Medium },
34 { '4', cmd_Item_Priority_Low },
35 { '5', cmd_Item_Priority_Lowest }
38 /// Constructs a list displaying \p rctdl
39 CTodoList::CTodoList (void)
40 : CListbox (),
41 m_pTodos (NULL),
42 m_CopiedId (0)
44 SetCommandKeys (VectorBlock (c_CmdKeys));
47 /// Sets the list to display.
48 void CTodoList::SetList (pctdevec_t pl)
50 if ((m_pTodos = pl))
51 SetListSize (m_pTodos->size());
54 /// Sets internal variables when the document is updated.
55 void CTodoList::OnUpdate (void)
57 CListbox::OnUpdate();
58 SetList (&Document()->List());
59 SetSelection (Document()->Selection());
62 /// Draws list item \p ii at \p pos onto \p gc.
63 void CTodoList::OnDrawItem (CGC& gc, rcpos_t pos, uint32_t ii)
65 CListbox::OnDrawItem (gc, pos, ii);
66 if (!m_pTodos)
67 return;
68 const CTodoItem& e ((*m_pTodos)[ii]);
69 char progBuf [5];
70 gc.Char (pos, "+ "[!e.HasSublist()]);
71 snprintf (progBuf, 5, "%3u%%", e.Progress());
72 gc.Text (pos[0] + 1, pos[1], e.Progress() ? progBuf : " ");
73 static const EColor c_PriorityColors [2][CTodoItem::priority_Last] = {
74 { white, lightred, yellow, green, cyan, lightblue },
75 { white, lightred, yellow, lightgreen, lightcyan, lightblue }
77 gc.FgColor (c_PriorityColors [ii == Selection()][e.Priority()]);
78 gc.Text (pos[0] + 6, pos[1], e.Text());
81 /// Causes the current entry to be edited.
82 inline void CTodoList::EditItem (void)
84 SetFlag (f_OffersFocus);
87 /// Processes keystroke \p key.
88 void CTodoList::OnKey (wchar_t key)
90 CListbox::OnKey (key);
91 if (Document()->Selection() != Selection())
92 Document()->SetSelection (Selection());
95 /// Executes command \p c.
96 void CTodoList::OnCommand (cmd_t c)
98 CListbox::OnCommand (c);
99 pdoc_t pDoc = Document();
100 switch (c) {
101 case cmd_Item_New: pDoc->AppendItem();
102 case cmd_Item_Edit: EditItem(); break;
103 case cmd_List_Copy: m_CopiedId = pDoc->CurrentItem().Id(); break;
104 case cmd_List_Paste: pDoc->PasteLinkToItem (m_CopiedId); break;
108 void CTodoList::OnUpdateCommandUI (rcmd_t rc) const
110 CListbox::OnUpdateCommandUI (rc);
111 pcdoc_t pDoc = Document();
112 const bool bHaveSelection (pDoc->Selection() < pDoc->List().size());
113 const bool bReadOnly (Flag (f_ReadOnly));
114 bool bActive = true;
115 switch (rc.cmd) {
116 case cmd_Item_New: break;
117 case cmd_Item_Edit: bActive = bHaveSelection && !bReadOnly; break;
118 case cmd_List_Copy: bActive = bHaveSelection; break;
119 case cmd_List_Paste: bActive = !bReadOnly && m_CopiedId; break;
120 default:
121 bActive = !(rc.flags & SCmd::cf_Grayed);
122 break;
124 rc.SetFlag (SCmd::cf_Grayed, !bActive);