From cf1565648bde10103b1aae14a253b3e13bb380db Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <calcurse@cryptocrack.de>
Date: Thu, 15 May 2014 21:49:32 +0200
Subject: ui-calendar: Use scroll window implementation

Make use of the generic scroll window implementation for the calendar
view. Note that this is useful despite the panel not needing a scroll
bar, since the scroll window functions can be used to draw the panel
border and take care of relative positions.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
---
 src/calcurse.h    |   3 +-
 src/ui-calendar.c | 103 ++++++++++++++++++++++++++----------------------------
 src/ui-todo.c     |   1 -
 src/wins.c        |  19 +++++-----
 4 files changed, 60 insertions(+), 66 deletions(-)

(limited to 'src')

diff --git a/src/calcurse.h b/src/calcurse.h
index d123269..2fc97c8 100644
--- a/src/calcurse.h
+++ b/src/calcurse.h
@@ -667,7 +667,7 @@ void ui_calendar_init_slctd_day(void);
 struct date *ui_calendar_get_slctd_day(void);
 long ui_calendar_get_slctd_day_sec(void);
 void ui_calendar_monthly_view_cache_set_invalid(void);
-void ui_calendar_update_panel(struct window *);
+void ui_calendar_update_panel(void);
 void ui_calendar_goto_today(void);
 void ui_calendar_change_day(int);
 void ui_calendar_move(enum move, int);
@@ -1056,6 +1056,7 @@ void vars_init(void);
 
 /* wins.c */
 extern struct window win[NBWINS];
+extern struct scrollwin sw_cal;
 extern struct listbox lb_todo;
 unsigned wins_nbar_lock(void);
 void wins_nbar_unlock(void);
diff --git a/src/ui-calendar.c b/src/ui-calendar.c
index f23d578..5fa8741 100644
--- a/src/ui-calendar.c
+++ b/src/ui-calendar.c
@@ -71,9 +71,9 @@ static unsigned ui_calendar_view, week_begins_on_monday;
 static pthread_mutex_t date_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_t ui_calendar_t_date;
 
-static void draw_monthly_view(struct window *, struct date *, unsigned);
-static void draw_weekly_view(struct window *, struct date *, unsigned);
-static void (*draw_calendar[CAL_VIEWS]) (struct window *, struct date *,
+static void draw_monthly_view(struct scrollwin *, struct date *, unsigned);
+static void draw_weekly_view(struct scrollwin *, struct date *, unsigned);
+static void (*draw_calendar[CAL_VIEWS]) (struct scrollwin *, struct date *,
 					 unsigned) = {
 draw_monthly_view, draw_weekly_view};
 
@@ -120,7 +120,7 @@ static void *ui_calendar_date_thread(void *arg)
 			sleep(tomorrow - actual);
 
 		ui_calendar_set_current_date();
-		ui_calendar_update_panel(&win[CAL]);
+		ui_calendar_update_panel();
 	}
 
 	return NULL;
@@ -277,14 +277,13 @@ void ui_calendar_monthly_view_cache_set_invalid(void)
 
 /* Draw the monthly view inside calendar panel. */
 static void
-draw_monthly_view(struct window *cwin, struct date *current_day,
+draw_monthly_view(struct scrollwin *sw, struct date *current_day,
 		  unsigned sunday_first)
 {
-	const int OFFY = CALHEIGHT / 2 - (conf.compact_panels ? 3 : 1);
 	struct date check_day;
 	int c_day, c_day_1, day_1_sav, numdays, j;
 	unsigned yr, mo;
-	int OFFX, SBAR_WIDTH, ofs_x, ofs_y;
+	int OFFY, OFFX, SBAR_WIDTH, ofs_x, ofs_y;
 	int item_this_day = 0;
 
 	mo = slctd_day.mm;
@@ -292,6 +291,7 @@ draw_monthly_view(struct window *cwin, struct date *current_day,
 
 	/* offset for centering calendar in window */
 	SBAR_WIDTH = wins_sbar_width();
+	OFFY = 0;
 	OFFX = (SBAR_WIDTH - 27) / 2;
 	ofs_y = OFFY;
 	ofs_x = OFFX;
@@ -311,20 +311,20 @@ draw_monthly_view(struct window *cwin, struct date *current_day,
 
 	/* Write the current month and year on top of the calendar */
 	WINS_CALENDAR_LOCK;
-	custom_apply_attr(cwin->p, ATTR_HIGHEST);
-	mvwprintw(cwin->p, ofs_y,
+	custom_apply_attr(sw->inner, ATTR_HIGHEST);
+	mvwprintw(sw->inner, ofs_y,
 		  (SBAR_WIDTH - (strlen(_(monthnames[mo - 1])) + 5)) / 2,
 		  "%s %d", _(monthnames[mo - 1]), slctd_day.yyyy);
-	custom_remove_attr(cwin->p, ATTR_HIGHEST);
+	custom_remove_attr(sw->inner, ATTR_HIGHEST);
 	++ofs_y;
 
 	/* print the days, with regards to the first day of the week */
-	custom_apply_attr(cwin->p, ATTR_HIGHEST);
+	custom_apply_attr(sw->inner, ATTR_HIGHEST);
 	for (j = 0; j < WEEKINDAYS; j++) {
-		mvwaddstr(cwin->p, ofs_y, ofs_x + 4 * j,
+		mvwaddstr(sw->inner, ofs_y, ofs_x + 4 * j,
 			  _(daynames[1 + j - sunday_first]));
 	}
-	custom_remove_attr(cwin->p, ATTR_HIGHEST);
+	custom_remove_attr(sw->inner, ATTR_HIGHEST);
 	WINS_CALENDAR_UNLOCK;
 
 	day_1_sav = (c_day_1 + 1) * 3 + c_day_1 - 7;
@@ -360,27 +360,27 @@ draw_monthly_view(struct window *cwin, struct date *current_day,
 		    && current_day->yyyy == slctd_day.yyyy
 		    && current_day->dd != slctd_day.dd) {
 			/* This is today, so print it in yellow. */
-			custom_apply_attr(cwin->p, ATTR_LOWEST);
-			mvwprintw(cwin->p, ofs_y + 1,
+			custom_apply_attr(sw->inner, ATTR_LOWEST);
+			mvwprintw(sw->inner, ofs_y + 1,
 				  ofs_x + day_1_sav + 4 * c_day + 1, "%2d",
 				  c_day);
-			custom_remove_attr(cwin->p, ATTR_LOWEST);
+			custom_remove_attr(sw->inner, ATTR_LOWEST);
 		} else if (c_day == slctd_day.dd) {
 			/* This is the selected day, print it according to user's theme. */
-			custom_apply_attr(cwin->p, ATTR_HIGHEST);
-			mvwprintw(cwin->p, ofs_y + 1,
+			custom_apply_attr(sw->inner, ATTR_HIGHEST);
+			mvwprintw(sw->inner, ofs_y + 1,
 				  ofs_x + day_1_sav + 4 * c_day + 1, "%2d",
 				  c_day);
-			custom_remove_attr(cwin->p, ATTR_HIGHEST);
+			custom_remove_attr(sw->inner, ATTR_HIGHEST);
 		} else if (item_this_day) {
-			custom_apply_attr(cwin->p, ATTR_LOW);
-			mvwprintw(cwin->p, ofs_y + 1,
+			custom_apply_attr(sw->inner, ATTR_LOW);
+			mvwprintw(sw->inner, ofs_y + 1,
 				  ofs_x + day_1_sav + 4 * c_day + 1, "%2d",
 				  c_day);
-			custom_remove_attr(cwin->p, ATTR_LOW);
+			custom_remove_attr(sw->inner, ATTR_LOW);
 		} else {
 			/* otherwise, print normal days in black */
-			mvwprintw(cwin->p, ofs_y + 1,
+			mvwprintw(sw->inner, ofs_y + 1,
 				  ofs_x + day_1_sav + 4 * c_day + 1, "%2d",
 				  c_day);
 		}
@@ -465,15 +465,15 @@ static int ISO8601weeknum(const struct tm *t)
 
 /* Draw the weekly view inside calendar panel. */
 static void
-draw_weekly_view(struct window *cwin, struct date *current_day,
+draw_weekly_view(struct scrollwin *sw, struct date *current_day,
 		 unsigned sunday_first)
 {
 #define DAYSLICESNO  6
 	const int WCALWIDTH = 30;
-	const int OFFY = CALHEIGHT / 2 - (conf.compact_panels ? 3 : 1);
 	struct tm t;
-	int OFFX, j, c_wday, days_to_remove, weeknum;
+	int OFFY, OFFX, j, c_wday, days_to_remove, weeknum;
 
+	OFFY = 0;
 	OFFX = (wins_sbar_width() - WCALWIDTH) / 2 + 1;
 
 	/* Fill in a tm structure with the first day of the selected week. */
@@ -493,10 +493,10 @@ draw_weekly_view(struct window *cwin, struct date *current_day,
 	/* Print the week number. */
 	weeknum = ISO8601weeknum(&t);
 	WINS_CALENDAR_LOCK;
-	custom_apply_attr(cwin->p, ATTR_HIGHEST);
-	mvwprintw(cwin->p, conf.compact_panels ? 0 : 2, cwin->w - 9,
+	custom_apply_attr(sw->inner, ATTR_HIGHEST);
+	mvwprintw(sw->win, conf.compact_panels ? 0 : 2, sw->w - 9,
 		  "(# %02d)", weeknum);
-	custom_remove_attr(cwin->p, ATTR_HIGHEST);
+	custom_remove_attr(sw->inner, ATTR_HIGHEST);
 	WINS_CALENDAR_UNLOCK;
 
 	/* Now draw calendar view. */
@@ -506,10 +506,10 @@ draw_weekly_view(struct window *cwin, struct date *current_day,
 		int i, slices[DAYSLICESNO];
 
 		/* print the day names, with regards to the first day of the week */
-		custom_apply_attr(cwin->p, ATTR_HIGHEST);
-		mvwaddstr(cwin->p, OFFY, OFFX + 4 * j,
+		custom_apply_attr(sw->inner, ATTR_HIGHEST);
+		mvwaddstr(sw->inner, OFFY, OFFX + 4 * j,
 			  _(daynames[1 + j - sunday_first]));
-		custom_remove_attr(cwin->p, ATTR_HIGHEST);
+		custom_remove_attr(sw->inner, ATTR_HIGHEST);
 
 		/* Check if the day to be printed has an item or not. */
 		date.dd = t.tm_mday;
@@ -532,11 +532,11 @@ draw_weekly_view(struct window *cwin, struct date *current_day,
 
 		WINS_CALENDAR_LOCK;
 		if (attr)
-			custom_apply_attr(cwin->p, attr);
-		mvwprintw(cwin->p, OFFY + 1, OFFX + 1 + 4 * j, "%02d",
+			custom_apply_attr(sw->inner, attr);
+		mvwprintw(sw->inner, OFFY + 1, OFFX + 1 + 4 * j, "%02d",
 			  t.tm_mday);
 		if (attr)
-			custom_remove_attr(cwin->p, attr);
+			custom_remove_attr(sw->inner, attr);
 		WINS_CALENDAR_UNLOCK;
 
 		/* Draw slices indicating appointment times. */
@@ -546,7 +546,7 @@ draw_weekly_view(struct window *cwin, struct date *current_day,
 				if (j != WEEKINDAYS - 1
 				    && i != DAYSLICESNO - 1) {
 					WINS_CALENDAR_LOCK;
-					mvwhline(cwin->p, OFFY + 2 + i,
+					mvwhline(sw->inner, OFFY + 2 + i,
 						 OFFX + 3 + 4 * j, ACS_S9,
 						 2);
 					WINS_CALENDAR_UNLOCK;
@@ -559,16 +559,16 @@ draw_weekly_view(struct window *cwin, struct date *current_day,
 					     slctd_day.dd) ? 1 : 0;
 					WINS_CALENDAR_LOCK;
 					if (highlight)
-						custom_apply_attr(cwin->p,
+						custom_apply_attr(sw->inner,
 								  attr);
-					wattron(cwin->p, A_REVERSE);
-					mvwaddstr(cwin->p, OFFY + 2 + i,
+					wattron(sw->inner, A_REVERSE);
+					mvwaddstr(sw->inner, OFFY + 2 + i,
 						  OFFX + 1 + 4 * j, " ");
-					mvwaddstr(cwin->p, OFFY + 2 + i,
+					mvwaddstr(sw->inner, OFFY + 2 + i,
 						  OFFX + 2 + 4 * j, " ");
-					wattroff(cwin->p, A_REVERSE);
+					wattroff(sw->inner, A_REVERSE);
 					if (highlight)
-						custom_remove_attr(cwin->p,
+						custom_remove_attr(sw->inner,
 								   attr);
 					WINS_CALENDAR_UNLOCK;
 				}
@@ -581,18 +581,18 @@ draw_weekly_view(struct window *cwin, struct date *current_day,
 
 	/* Draw marks to indicate midday on the sides of the calendar. */
 	WINS_CALENDAR_LOCK;
-	custom_apply_attr(cwin->p, ATTR_HIGHEST);
-	mvwhline(cwin->p, OFFY + 1 + DAYSLICESNO / 2, OFFX, ACS_S9, 1);
-	mvwhline(cwin->p, OFFY + 1 + DAYSLICESNO / 2,
+	custom_apply_attr(sw->inner, ATTR_HIGHEST);
+	mvwhline(sw->inner, OFFY + 1 + DAYSLICESNO / 2, OFFX, ACS_S9, 1);
+	mvwhline(sw->inner, OFFY + 1 + DAYSLICESNO / 2,
 		 OFFX + WCALWIDTH - 3, ACS_S9, 1);
-	custom_remove_attr(cwin->p, ATTR_HIGHEST);
+	custom_remove_attr(sw->inner, ATTR_HIGHEST);
 	WINS_CALENDAR_UNLOCK;
 
 #undef DAYSLICESNO
 }
 
 /* Function used to display the calendar panel. */
-void ui_calendar_update_panel(struct window *cwin)
+void ui_calendar_update_panel(void)
 {
 	struct date current_day;
 	unsigned sunday_first;
@@ -600,17 +600,14 @@ void ui_calendar_update_panel(struct window *cwin)
 	ui_calendar_store_current_date(&current_day);
 
 	WINS_CALENDAR_LOCK;
-	erase_window_part(cwin->p, 1, conf.compact_panels ? 1 : 3,
-			  cwin->w - 2, cwin->h - 2);
-	if (!conf.compact_panels)
-		mvwhline(cwin->p, 2, 1, ACS_HLINE, cwin->w - 2);
+	werase(sw_cal.inner);
 	WINS_CALENDAR_UNLOCK;
 
 	sunday_first = ui_calendar_week_begins_on_monday()? 0 : 1;
 
-	draw_calendar[ui_calendar_view] (cwin, &current_day, sunday_first);
+	draw_calendar[ui_calendar_view] (&sw_cal, &current_day, sunday_first);
 
-	wnoutrefresh(cwin->p);
+	wins_scrollwin_display(&sw_cal);
 }
 
 /* Set the selected day in calendar to current day. */
diff --git a/src/ui-todo.c b/src/ui-todo.c
index 930b595..c7b29f4 100644
--- a/src/ui-todo.c
+++ b/src/ui-todo.c
@@ -53,7 +53,6 @@ void ui_todo_add(void)
 			ch = wgetch(win[KEY].p);
 		}
 		todo_add(todo_input, ch - '0', NULL);
-		ui_todo_sel_move(1);
 		ui_todo_load_items();
 	}
 }
diff --git a/src/wins.c b/src/wins.c
index 30d6529..a690fe2 100644
--- a/src/wins.c
+++ b/src/wins.c
@@ -50,6 +50,7 @@
 
 /* Variables to handle calcurse windows. */
 struct window win[NBWINS];
+struct scrollwin sw_cal;
 struct listbox lb_todo;
 
 /* User-configurable side bar width. */
@@ -243,9 +244,9 @@ void wins_slctd_next(void)
 
 static void wins_init_panels(void)
 {
-	win[CAL].p = newwin(CALHEIGHT + (conf.compact_panels ? 2 : 4),
-			    wins_sbar_width(), win[CAL].y, win[CAL].x);
-	wins_show(win[CAL].p, _("Calendar"));
+	wins_scrollwin_init(&sw_cal, win[CAL].y, win[CAL].x,
+			    CALHEIGHT + (conf.compact_panels ? 2 : 4),
+			    wins_sbar_width(), _("Calendar"));
 
 	win[APP].p =
 	    newwin(win[APP].h, win[APP].w, win[APP].y, win[APP].x);
@@ -258,7 +259,6 @@ static void wins_init_panels(void)
 	ui_todo_load_items();
 
 	/* Enable function keys (i.e. arrow keys) in those windows */
-	keypad(win[CAL].p, TRUE);
 	keypad(win[APP].p, TRUE);
 }
 
@@ -400,7 +400,7 @@ void wins_scrollwin_ensure_visible(struct scrollwin *sw, unsigned line)
 
 void wins_reinit_panels(void)
 {
-	delwin(win[CAL].p);
+	wins_scrollwin_delete(&sw_cal);
 	delwin(win[APP].p);
 	delwin(apad.ptrwin);
 	listbox_delete(&lb_todo);
@@ -414,7 +414,7 @@ void wins_reinit_panels(void)
  */
 void wins_reinit(void)
 {
-	delwin(win[CAL].p);
+	wins_scrollwin_delete(&sw_cal);
 	delwin(win[APP].p);
 	delwin(apad.ptrwin);
 	listbox_delete(&lb_todo);
@@ -544,10 +544,7 @@ void wins_update_border(int flags)
 {
 	if (flags & FLAG_CAL) {
 		WINS_CALENDAR_LOCK;
-		if (slctd_win == CAL)
-			border_color(win[CAL].p);
-		else
-			border_nocolor(win[CAL].p);
+		wins_scrollwin_draw_deco(&sw_cal, (slctd_win == CAL));
 		WINS_CALENDAR_UNLOCK;
 	}
 	if (flags & FLAG_APP) {
@@ -567,7 +564,7 @@ void wins_update_panels(int flags)
 	if (flags & FLAG_TOD)
 		ui_todo_update_panel(slctd_win);
 	if (flags & FLAG_CAL)
-		ui_calendar_update_panel(&win[CAL]);
+		ui_calendar_update_panel();
 }
 
 /*
-- 
cgit v1.2.3-70-g09d2