patch 9.1.1425: tabpanel: there are still some problems with the tabpanel

Problem:  tabpanel: there are still some problems with the tabpanel with
          column handling
Solution: fix the problems and refactor Tabpanel feature (Hirohito
          Higashi).

fixes: #17423
fixes: #17332
closes: #17336

Signed-off-by: Hirohito Higashi <h.east.727@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/popupwin.c b/src/popupwin.c
index a9a2843..199ffaf 100644
--- a/src/popupwin.c
+++ b/src/popupwin.c
@@ -86,7 +86,7 @@
     if (STRCMP(key, "line") == 0)
 	n = screen_screenrow() + 1 + n;
     else // "col"
-	n = screen_screencol() + 1 + n - TPL_LCOL(NULL);
+	n = screen_screencol() + 1 + n;
 
     // Zero means "not set", use -1 instead.
     if (n == 0)
@@ -1354,15 +1354,15 @@
 	{
 	    wp->w_wincol = wantcol - 1;
 	    // Need to see at least one character after the decoration.
-	    if (wp->w_wincol > Columns - left_extra - 1)
-		wp->w_wincol = Columns - left_extra - 1;
+	    if (wp->w_wincol > firstwin->w_wincol + topframe->fr_width - left_extra - 1)
+		wp->w_wincol = firstwin->w_wincol + topframe->fr_width - left_extra - 1;
 	}
     }
 
     // When centering or right aligned, use maximum width.
     // When left aligned use the space available, but shift to the left when we
     // hit the right of the screen.
-    maxspace = Columns - wp->w_wincol - left_extra;
+    maxspace = firstwin->w_wincol + topframe->fr_width - wp->w_wincol - left_extra;
     maxwidth = maxspace;
     if (wp->w_maxwidth > 0 && maxwidth > wp->w_maxwidth)
     {
@@ -1545,7 +1545,7 @@
     }
     if (center_hor)
     {
-	wp->w_wincol = (Columns - wp->w_width - extra_width - TPL_LCOL(NULL)) / 2;
+	wp->w_wincol = (firstwin->w_wincol + topframe->fr_width - wp->w_width - extra_width) / 2;
 	if (wp->w_wincol < 0)
 	    wp->w_wincol = 0;
     }
@@ -1581,9 +1581,9 @@
 	// try to show the right border and any scrollbar
 	want_col = left_extra + wp->w_width + right_extra;
 	if (want_col > 0 && wp->w_wincol > 0
-					 && wp->w_wincol + want_col >= Columns)
+					 && wp->w_wincol + want_col >= firstwin->w_wincol + topframe->fr_width)
 	{
-	    wp->w_wincol = Columns - want_col;
+	    wp->w_wincol = firstwin->w_wincol + topframe->fr_width - want_col;
 	    if (wp->w_wincol < 0)
 		wp->w_wincol = 0;
 	}
@@ -1673,6 +1673,13 @@
     else if (wp->w_winrow < 0)
 	wp->w_winrow = 0;
 
+    if (wp->w_wincol + wp->w_width > firstwin->w_wincol + topframe->fr_width)
+	wp->w_wincol = firstwin->w_wincol + topframe->fr_width - wp->w_width;
+    else if (wp->w_wincol < firstwin->w_wincol)
+	wp->w_wincol = firstwin->w_wincol;
+    if (wp->w_wincol < 0)
+	wp->w_wincol = 0;
+
     if (wp->w_height != org_height)
 	win_comp_scroll(wp);
 
@@ -4081,7 +4088,7 @@
 	// win_update() doesn't handle them.
 	top_off = popup_top_extra(wp);
 	left_extra = wp->w_popup_padding[3] + wp->w_popup_border[3]
-		+ TPL_LCOL(NULL) - wp->w_popup_leftoff;
+							- wp->w_popup_leftoff;
 	if (wp->w_wincol + left_extra < 0)
 	    left_extra = -wp->w_wincol;
 	wp->w_winrow += top_off;
@@ -4163,7 +4170,7 @@
 	}
 
 	// Title goes on top of border or padding.
-	title_wincol = wp->w_wincol + 1 + TPL_LCOL(NULL);
+	title_wincol = wp->w_wincol + 1;
 	if (wp->w_popup_title != NULL)
 	{
 	    title_len = vim_strsize(wp->w_popup_title);
@@ -4191,7 +4198,7 @@
 		      wp->w_popup_border[0] > 0 ? border_attr[0] : popup_attr);
 	}
 
-	wincol = wp->w_wincol - wp->w_popup_leftoff + TPL_LCOL(NULL);
+	wincol = wp->w_wincol - wp->w_popup_leftoff;
 	top_padding = wp->w_popup_padding[0];
 	if (wp->w_popup_border[0] > 0)
 	{
@@ -4229,7 +4236,7 @@
 	{
 	    padcol = wincol + wp->w_popup_border[3];
 	    padendcol = wp->w_wincol + total_width - wp->w_popup_border[1]
-		    + TPL_LCOL(NULL) - wp->w_has_scrollbar;
+							- wp->w_has_scrollbar;
 	    if (padcol < 0)
 	    {
 		padendcol += padcol;
@@ -4327,7 +4334,7 @@
 	    if (wp->w_has_scrollbar)
 	    {
 		int line = i - top_off;
-		int scroll_col = wp->w_wincol + total_width - 1 + TPL_LCOL(NULL)
+		int scroll_col = wp->w_wincol + total_width - 1
 						       - wp->w_popup_border[1];
 
 		if (line >= 0 && line < wp->w_height)