From 9ad0d0ff529222ae352c5b3aee87c5afdf6fcd1b Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Fri, 26 May 2006 02:59:32 +0000
Subject: [PATCH] A few autosavnum fixups... should be working much better
 now... also, you no longer need to re-display previous WIN_SAV|WIN_ACT menus
 for them to be erased properly.

---
 src/uifc/uifc.h   |  1 +
 src/uifc/uifc32.c | 98 +++++++++++++++++++++++++++--------------------
 2 files changed, 58 insertions(+), 41 deletions(-)

diff --git a/src/uifc/uifc.h b/src/uifc/uifc.h
index f33736a785..b746bd72ac 100644
--- a/src/uifc/uifc.h
+++ b/src/uifc/uifc.h
@@ -242,6 +242,7 @@ enum {
 
 typedef struct {
 	int		left,top,right,bot;
+	int		*cur,*bar;
     uchar*	buf;
 } win_t;
 
diff --git a/src/uifc/uifc32.c b/src/uifc/uifc32.c
index 2a3c025d8f..3e410a9612 100644
--- a/src/uifc/uifc32.c
+++ b/src/uifc/uifc32.c
@@ -488,7 +488,6 @@ int ulist(int mode, int left, int top, int width, int *cur, int *bar
 	int height,y;
 	int i,j,opts=0,s=0; /* s=search index into options */
 	int	is_redraw=0;
-	int	is_lastwin=0;
 	int s_top=SCRN_TOP;
 	int s_left=SCRN_LEFT;
 	int s_right=SCRN_RIGHT;
@@ -505,8 +504,6 @@ int ulist(int mode, int left, int top, int width, int *cur, int *bar
 	int	a,b,c,longopt;
 	int	optheight=0;
 	uchar	hclr,lclr,bclr,cclr,lbclr;
-	static int	*oldcur=NULL;
-	static int	*oldbar=NULL;
 
 	hclr=api->hclr;
 	lclr=api->lclr;
@@ -617,12 +614,6 @@ int ulist(int mode, int left, int top, int width, int *cur, int *bar
 			&& save_menu_opts==opts) {
 		is_redraw=1;
 	}
-	if(mode&WIN_SAV) {
-		if(cur==oldcur && bar==oldbar)
-			is_lastwin=1;
-		oldcur=cur;
-		oldbar=bar;
-	}
 
 	if(mode&WIN_DYN && mode&WIN_REDRAW)
 		is_redraw=1;
@@ -632,34 +623,61 @@ int ulist(int mode, int left, int top, int width, int *cur, int *bar
 	if(mode&WIN_ORG) {		/* Clear all save buffers on WIN_ORG */
 		for(i=0; i< MAX_BUFS; i++)
 			FREE_AND_NULL(sav[i].buf);
+		api->savnum=0;
 	}
 
 	if(mode&WIN_SAV) {
-		if(is_lastwin && api->savnum>0 && sav[api->savnum].buf==NULL)
-			api->savnum--;
-		if(sav[api->savnum].buf==NULL) {
-			if((sav[api->savnum].buf=(char *)malloc((width+3)*(height+2)*2))==NULL) {
-				cprintf("UIFC line %d: error allocating %u bytes."
-					,__LINE__,(width+3)*(height+2)*2);
-				free(title);
-				uifc_mouse_enable();
-				return(-1);
+		/* Check if this screen (by cur/bar) is already saved */
+		for(i=0; i<MAX_BUFS; i++) {
+			if(sav[i].buf!=NULL) {
+				if(cur==sav[i].cur && bar==sav[i].bar) {
+					/* Yes, it is... */
+					for(j=api->savnum-1; j>i; j--) {
+						/* Retore old screens */
+						puttext(sav[j].left,sav[j].top,sav[j].right,sav[j].bot
+							,sav[j].buf);	/* put original window back */
+						FREE_AND_NULL(sav[j].buf);
+					}
+					api->savnum=i;
+				}
+			}
+		}
+		/* savnum not the next one - must be a dynamic window or we popped back up the stack */
+		if(sav[api->savnum].buf != NULL) {
+			/* Is this even the right window? */
+			if(sav[api->savnum].cur==cur
+					&& sav[api->savnum].bar==bar) {
+				if((sav[api->savnum].left!=s_left+left
+					|| sav[api->savnum].top!=s_top+top
+					|| sav[api->savnum].right!=s_left+left+width+1
+					|| sav[api->savnum].bot!=s_top+top+height)) { /* dimensions have changed */
+					puttext(sav[api->savnum].left,sav[api->savnum].top,sav[api->savnum].right,sav[api->savnum].bot
+						,sav[api->savnum].buf);	/* put original window back */
+					FREE_AND_NULL(sav[api->savnum].buf);
+					if((sav[api->savnum].buf=(char *)malloc((width+3)*(height+2)*2))==NULL) {
+						cprintf("UIFC line %d: error allocating %u bytes."
+							,__LINE__,(width+3)*(height+2)*2);
+						free(title);
+						uifc_mouse_enable();
+						return(-1);
+					}
+					gettext(s_left+left,s_top+top,s_left+left+width+1
+						,s_top+top+height,sav[api->savnum].buf);	  /* save again */
+					sav[api->savnum].left=s_left+left;
+					sav[api->savnum].top=s_top+top;
+					sav[api->savnum].right=s_left+left+width+1;
+					sav[api->savnum].bot=s_top+top+height;
+					sav[api->savnum].cur=cur;
+					sav[api->savnum].bar=bar;
+				}
+			}
+			else {
+				/* Find something available... */
+				while(sav[api->savnum].buf!=NULL)
+					api->savnum++;
 			}
-			gettext(s_left+left,s_top+top,s_left+left+width+1
-				,s_top+top+height,sav[api->savnum].buf);
-			sav[api->savnum].left=s_left+left;
-			sav[api->savnum].top=s_top+top;
-			sav[api->savnum].right=s_left+left+width+1;
-			sav[api->savnum].bot=s_top+top+height;
 		}
-		else if(sav[api->savnum].buf != NULL
-			&& (sav[api->savnum].left!=s_left+left
-			|| sav[api->savnum].top!=s_top+top
-			|| sav[api->savnum].right!=s_left+left+width+1
-			|| sav[api->savnum].bot!=s_top+top+height)) { /* dimensions have changed */
-			puttext(sav[api->savnum].left,sav[api->savnum].top,sav[api->savnum].right,sav[api->savnum].bot
-				,sav[api->savnum].buf);	/* put original window back */
-			FREE_AND_NULL(sav[api->savnum].buf);
+		else {
 			if((sav[api->savnum].buf=(char *)malloc((width+3)*(height+2)*2))==NULL) {
 				cprintf("UIFC line %d: error allocating %u bytes."
 					,__LINE__,(width+3)*(height+2)*2);
@@ -668,11 +686,13 @@ int ulist(int mode, int left, int top, int width, int *cur, int *bar
 				return(-1);
 			}
 			gettext(s_left+left,s_top+top,s_left+left+width+1
-				,s_top+top+height,sav[api->savnum].buf);	  /* save again */
+				,s_top+top+height,sav[api->savnum].buf);
 			sav[api->savnum].left=s_left+left;
 			sav[api->savnum].top=s_top+top;
 			sav[api->savnum].right=s_left+left+width+1;
 			sav[api->savnum].bot=s_top+top+height;
+			sav[api->savnum].cur=cur;
+			sav[api->savnum].bar=bar;
 		}
 	}
 
@@ -990,8 +1010,6 @@ int ulist(int mode, int left, int top, int width, int *cur, int *bar
 								,sav[api->savnum].buf);
 							uifc_mouse_enable();
 							FREE_AND_NULL(sav[api->savnum].buf);
-							if(api->savnum)
-								api->savnum--;
 						}
 						if(mode&WIN_XTR && (*cur)==opts-1)
 							return(MSK_INS|*cur);
@@ -1533,16 +1551,15 @@ int ulist(int mode, int left, int top, int width, int *cur, int *bar
 									,sav[api->savnum].right,sav[api->savnum].bot
 									,sav[api->savnum].buf);
 								FREE_AND_NULL(sav[api->savnum].buf);
-								if(api->savnum)
-									api->savnum--;
 							}
 							if(mode&WIN_XTR && (*cur)==opts-1)
 								return(MSK_INS|*cur);
 							return(*cur);
 						case 3:
 						case ESC:
-							if((mode&WIN_ESC || (mode&WIN_CHE && api->changes))
-								&& !(mode&WIN_SAV)) {
+							if(mode&WIN_SAV)
+								api->savnum++;
+							if(mode&WIN_ESC || (mode&WIN_CHE && api->changes)) {
 								gettext(s_left+left,s_top+top,s_left
 									+left+width-1,s_top+top+height-1,tmp_buffer);
 								for(i=1;i<(width*height*2);i+=2)
@@ -1551,12 +1568,11 @@ int ulist(int mode, int left, int top, int width, int *cur, int *bar
 									+left+width-1,s_top+top+height-1,tmp_buffer);
 							}
 							else if(mode&WIN_SAV) {
+								api->savnum--;
 								puttext(sav[api->savnum].left,sav[api->savnum].top
 									,sav[api->savnum].right,sav[api->savnum].bot
 									,sav[api->savnum].buf);
 								FREE_AND_NULL(sav[api->savnum].buf);
-								if(api->savnum)
-									api->savnum--;
 							}
 							return(-1);
 						default:
-- 
GitLab