aboutsummaryrefslogtreecommitdiffstats
path: root/src/day.c
diff options
context:
space:
mode:
authorLars Henriksen <LarsHenriksen@get2net.dk>2018-12-19 08:59:05 +0100
committerLukas Fleischer <lfleischer@calcurse.org>2019-05-22 01:56:59 -0400
commit1ccfe128cce8d670d12c350326bb13fbc0276d0a (patch)
tree116fd3e8861e13cc22060ce2eb47593381a8f61c /src/day.c
parent066df02cbf44d58a9b1e3a30b1310dc0f460e4c4 (diff)
downloadcalcurse-1ccfe128cce8d670d12c350326bb13fbc0276d0a.tar.gz
calcurse-1ccfe128cce8d670d12c350326bb13fbc0276d0a.zip
Redesign selected-item implementation for the APP panel
The day vector, day_items, is displayed in the appointments panel; the selected day_item object is highlighted (when the panel has the focus). When items are inserted, edited, moved etc., and when the day is changed, the day vector is rebuilt and displayed anew. Problem: How shall the selection be set automatically in the context of the new day vector? In previous versions all of the above is mostly handled by the function do_storage() in calcurse.c The function saves data about the selection as needed, rebuilds the day vector, loads the listbox and sets the selection from the saved selection data. This works well in "single day" calcurse in cases where the selected item is present in the day vector both before and after the rebuild, or when the item ordering in the listbox is unaffected by the changes. But when a new item is added the selection cannot be set to the new object by do_storage(). Instead the necessary operations are performed by ui_day_item_add(), and do_storage() is bypassed. In general, when an item cannot be found in the new vector, the item which occupies the old place in the list gets selected, e.g. when an item is deleted. When an item is turned into a repeating one, the old item is deleted and a new is created. Here the new selection is not always the affected item, but in any case not far away. Generally, with only one day in the panel an erronous selection might not be noticed or be accurate by chance. In "multiple day" calcurse the existing scheme works less well; in addition the day vector may now contain more than one object that refer to the same event or appointment (recurrent items or multi-day appointments). The scheme has therefore been modified. The do_storage() function is no longer bypassed, but handles day vector rebuild, load of listbox and item selection exclusively. To make that possible, data about the selected item is no longer saved in a local automatic variable, private to do_storage(), but in an external static variable in day.c, which may be set not only by do_storage(). The variable is declared as static struct day_item sel_data; and used as follows: 1. On startup sel_data is initialized to empty (i.e. no selection). 2. In any operation involving the appointments panel: 2.1 Do the work and if necessary set sel_data. This is the case when deleting, adding or pasting an item, and when turning an ordinary item into a recurrent one. 2.2 Call do_storage(). 3. In do_storage(): 3.1 If sel_data is empty, set it to the current selection. 3.2 Rebuild the day vector. 3.3 Set the selection from sel_data. 3.4 Set sel_data to empty. Further remarks --------------- The selection is found in the new day vector by searching for the saved (order, item.<pointer>) pair. Previously the item.<pointer> alone sufficed and in some cases it still does. In case the item cannot be found, the selection stays in the same day as before the rebuild. An attempt at more consistently named APP-related functions has led to: ui_day_sel_date() replaces ui_day_sel_day() ui_day_get_sel() replaces ui_day_selitem() Signed-off-by: Lars Henriksen <LarsHenriksen@get2net.dk> Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
Diffstat (limited to 'src/day.c')
-rw-r--r--src/day.c83
1 files changed, 62 insertions, 21 deletions
diff --git a/src/day.c b/src/day.c
index e480732..4c6e1a1 100644
--- a/src/day.c
+++ b/src/day.c
@@ -46,6 +46,67 @@ static unsigned day_nb = 7;
static vector_t day_items;
static unsigned day_items_nb = 0;
+struct day_item empty_day = { 0, 0, 0, {NULL}};
+
+/*
+ * The day vector, day_items, is continuously rebuilt for display as the
+ * selected day changes, items are added, edited or deleted etc. To keep track
+ * of the selected item of the day vector across rebuilds, data are saved in
+ * here that may later be used to refind the item in the rebuilt day
+ * vector.
+ */
+static struct day_item sel_data = { 0, 0, 0, {NULL}};
+
+/*
+ * Save the item to become the selected APP item.
+ * Public function used to override the setting in do_storage().
+ */
+int day_set_sel_data(struct day_item *d)
+{
+ if (!d)
+ return 0;
+
+ sel_data = *d;
+ return 1;
+}
+
+/*
+ * Return selection data if available.
+ */
+int day_check_sel_data()
+{
+ return (sel_data.order || sel_data.item.apt) ? 1 : 0;
+}
+
+/*
+ * Return the position of the saved selection in the day vector.
+ */
+int day_sel_index(void)
+{
+ int i = 0;
+ struct day_item *p;
+
+ VECTOR_FOREACH(&day_items, i) {
+ p = VECTOR_NTH(&day_items, i);
+ if (p->order == sel_data.order &&
+ p->item.apt == sel_data.item.apt)
+ return i;
+ }
+ /* Needed as long as ui_day_item_paste() does not set order. */
+ VECTOR_FOREACH(&day_items, i) {
+ p = VECTOR_NTH(&day_items, i);
+ if (p->item.apt == sel_data.item.apt)
+ return i;
+ }
+ /* If still not found, stay on the same day. */
+ VECTOR_FOREACH(&day_items, i) {
+ p = VECTOR_NTH(&day_items, i);
+ if (p->order == update_time_in_date(sel_data.order, 0, 0))
+ return i;
+ }
+ return -1;
+}
+
int day_get_nb(void)
{
return day_nb;
@@ -510,7 +571,7 @@ void day_popup_item(struct day_item *day)
struct apoint apt_tmp;
apt_tmp.start = day->start;
apt_tmp.dur = day_item_get_duration(day);
- apoint_sec2str(&apt_tmp, ui_day_selday(), a_st, a_end);
+ apoint_sec2str(&apt_tmp, ui_day_sel_date(), a_st, a_end);
item_in_popup(a_st, a_end, day_item_get_mesg(day),
_("Appointment:"));
} else {
@@ -734,26 +795,6 @@ int day_paste_item(struct day_item *p, time_t date)
return p->type;
}
-/* Returns the position corresponding to a given item. */
-int day_get_position_by_aptev_ptr(union aptev_ptr aptevp)
-{
- int n = 0;
-
- VECTOR_FOREACH(&day_items, n) {
- struct day_item *p = VECTOR_NTH(&day_items, n);
- /* Compare pointers. */
- if (p->item.ev == aptevp.ev)
- return n;
- }
-
- return -1;
-}
-
-int day_get_position(struct day_item *needle)
-{
- return day_get_position_by_aptev_ptr(needle->item);
-}
-
/* Returns a structure containing the selected item. */
struct day_item *day_get_item(int item_number)
{