Index: functions.c
===================================================================
--- functions.c	(revision 13ebf929c89a2f68717a74de950e3479e82546c6)
+++ functions.c	(revision 68f63a2ed999ed960962a275c6230741ed1af603)
@@ -1285,9 +1285,5 @@
   owl_popwin_up(pw);
   owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless");
-  owl_viewwin_init_text(v, owl_popwin_get_curswin(pw),
-			owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
-			text);
-  owl_viewwin_redisplay(v);
-  owl_global_set_needrefresh(&g);
+  owl_viewwin_init_text(v, owl_popwin_get_content(pw), text);
 }
 
@@ -1302,9 +1298,5 @@
   owl_popwin_up(pw);
   owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless");
-  owl_viewwin_init_fmtext(v, owl_popwin_get_curswin(pw),
-		   owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
-		   fm);
-  owl_viewwin_redisplay(v);
-  owl_global_set_needrefresh(&g);
+  owl_viewwin_init_fmtext(v, owl_popwin_get_content(pw), fm);
 }
 
Index: global.c
===================================================================
--- global.c	(revision 449af721064c2d51b95b251acb10834dd0536c8d)
+++ global.c	(revision 68f63a2ed999ed960962a275c6230741ed1af603)
@@ -511,7 +511,4 @@
 
 void owl_global_relayout(owl_global *g) {
-  owl_popwin *pw;
-  owl_viewwin *vw;
-
   if (!g->relayoutpending) return;
   g->relayoutpending = 0;
@@ -528,20 +525,4 @@
    * screen */
   owl_function_calculate_topmsg(OWL_DIRECTION_NONE);
-
-  /* recreate the popwin */
-  pw = owl_global_get_popwin(g);
-  if (owl_popwin_is_active(pw)) {
-    /*
-     * This is somewhat hacky; we probably want a proper windowing layer. We
-     * destroy the popwin and recreate it. Then the viewwin is redirected to
-     * the new window.
-     */
-    vw = owl_global_get_viewwin(g);
-    owl_popwin_close(pw);
-    owl_popwin_up(pw);
-    owl_viewwin_set_curswin(vw, owl_popwin_get_curswin(pw),
-	owl_popwin_get_lines(pw), owl_popwin_get_cols(pw));
-    owl_viewwin_redisplay(vw);
-  }
 
   /* refresh stuff */
Index: keys.c
===================================================================
--- keys.c	(revision 44cc9abbc2c15abaf4cd5fe0d1fc62b0e0936913)
+++ keys.c	(revision 68f63a2ed999ed960962a275c6230741ed1af603)
@@ -342,11 +342,4 @@
 
 void owl_keys_popless_postalways(owl_input j) {
-  owl_viewwin *v = owl_global_get_viewwin(&g);
-  const owl_popwin *pw = owl_global_get_popwin(&g);
-
-  if (pw && owl_popwin_is_active(pw) && v) {
-    owl_viewwin_redisplay(v);
-    owl_global_set_needrefresh(&g);
-  }  
 }
 
Index: owl.h
===================================================================
--- owl.h	(revision 449af721064c2d51b95b251acb10834dd0536c8d)
+++ owl.h	(revision 68f63a2ed999ed960962a275c6230741ed1af603)
@@ -404,5 +404,5 @@
   int rightshift;
   int winlines, wincols;
-  WINDOW *curswin;
+  owl_window *window;
   void (*onclose_hook) (struct _owl_viewwin *vwin, void *data);
   void *onclose_hook_data;
@@ -410,6 +410,6 @@
   
 typedef struct _owl_popwin {
-  PANEL *borderpanel;
-  PANEL *poppanel;
+  owl_window *border;
+  owl_window *content;
   int lines;
   int cols;
Index: popexec.c
===================================================================
--- popexec.c	(revision 125fd21c2d564a4a7d0a027552b7156a4d694243)
+++ popexec.c	(revision 68f63a2ed999ed960962a275c6230741ed1af603)
@@ -28,9 +28,5 @@
   owl_popwin_up(pw);
   owl_global_push_context(&g, OWL_CTX_POPLESS, v, "popless");
-  owl_viewwin_init_text(v, owl_popwin_get_curswin(pw),
-			owl_popwin_get_lines(pw), owl_popwin_get_cols(pw),
-			"");
-  owl_viewwin_redisplay(v);
-  owl_global_set_needrefresh(&g);
+  owl_viewwin_init_text(v, owl_popwin_get_content(pw), "");
   owl_viewwin_set_onclose_hook(v, owl_popexec_viewwin_onclose, pe);
   pe->refcount++;
@@ -111,6 +107,4 @@
     if (pe->winactive) { 
       owl_viewwin_append_text(pe->vwin, "\n");
-      owl_viewwin_redisplay(pe->vwin);
-      owl_global_set_needrefresh(&g);
     }
     owl_select_remove_io_dispatch(d);
@@ -139,6 +133,4 @@
   if (pe->winactive) {
     owl_viewwin_append_text(pe->vwin, buf);
-    owl_viewwin_redisplay(pe->vwin);
-    owl_global_set_needrefresh(&g);
   }
   owl_free(buf);
Index: popwin.c
===================================================================
--- popwin.c	(revision 0881cdda52baa4677179208b39a3392f06182cf5)
+++ popwin.c	(revision 68f63a2ed999ed960962a275c6230741ed1af603)
@@ -11,24 +11,48 @@
 int owl_popwin_up(owl_popwin *pw)
 {
-  int glines, gcols, startcol, startline;
-  WINDOW *popwin, *borderwin;
+  /* make them zero-size for now */
+  pw->border = owl_window_new(0, 0, 0, 0, 0);
+  pw->content = owl_window_new(pw->border, 0, 0, 0, 0);
 
-  /* calculate the size of the popwin */
-  glines=owl_global_get_lines(&g);
-  gcols=owl_global_get_cols(&g);
+  owl_window_set_redraw_cb(pw->border, owl_popwin_draw_border, pw, 0);
+  owl_window_set_resize_cb(pw->border, owl_popwin_resize_content, pw, 0);
+  /* HACK */
+  owl_window_set_resize_cb(owl_window_get_screen(), owl_popwin_reposition, pw, 0);
 
-  pw->lines = owl_util_min(glines,24)*3/4 + owl_util_max(glines-24,0)/2;
-  startline = (glines-pw->lines)/2;
+  /* calculate position, THEN map the windows maybe setting a resize hook
+   * should just cause it to get called? */
+  owl_popwin_reposition(owl_window_get_screen(), pw);
+  owl_window_map(pw->border, 1);
 
-  pw->cols = owl_util_min(gcols,90)*15/16 + owl_util_max(gcols-90,0)/2;
-  startcol = (gcols-pw->cols)/2;
+  pw->active=1;
+  return(0);
+}
 
-  borderwin = newwin(pw->lines, pw->cols, startline, startcol);
-  pw->borderpanel = new_panel(borderwin);
-  popwin = newwin(pw->lines-2, pw->cols-2, startline+1, startcol+1);
-  pw->poppanel = new_panel(popwin);
+void owl_popwin_reposition(owl_window *screen, void *user_data)
+{
+  owl_popwin *pw = user_data;
+  int lines, cols, startline, startcol;
+  int glines, gcols;
 
-  werase(popwin);
-  werase(borderwin);
+  owl_window_get_position(screen, &glines, &gcols, 0, 0);
+
+  lines = owl_util_min(glines,24)*3/4 + owl_util_max(glines-24,0)/2;
+  startline = (glines-lines)/2;
+  cols = owl_util_min(gcols,90)*15/16 + owl_util_max(gcols-90,0)/2;
+  startcol = (gcols-cols)/2;
+
+  owl_window_set_position(pw->border, lines, cols, startline, startcol);
+}
+
+void owl_popwin_resize_content(owl_window *w, void *user_data)
+{
+  owl_popwin *pw = user_data;
+  owl_window_get_position(w, &pw->lines, &pw->cols, 0, 0);
+  owl_window_set_position(pw->content, pw->lines-2, pw->cols-2, 1, 1);
+}
+
+void owl_popwin_draw_border(owl_window *w, WINDOW *borderwin, void *user_data)
+{
+  owl_popwin *pw = user_data;
   if (owl_global_is_fancylines(&g)) {
     box(borderwin, 0, 0);
@@ -44,24 +68,13 @@
     waddch(borderwin, '+');
   }
-    
-  owl_global_set_needrefresh(&g);
-  pw->active=1;
-  return(0);
 }
 
 int owl_popwin_close(owl_popwin *pw)
 {
-  WINDOW *popwin, *borderwin;
-
-  popwin = panel_window(pw->poppanel);
-  borderwin = panel_window(pw->borderpanel);
-
-  del_panel(pw->poppanel);
-  del_panel(pw->borderpanel);
-  delwin(popwin);
-  delwin(borderwin);
-
+  owl_window_delete(pw->border);
+  pw->border = 0;
+  pw->content = 0;
   pw->active=0;
-  owl_global_set_needrefresh(&g);
+  owl_window_set_resize_cb(owl_window_get_screen(), 0, 0, 0);
   return(0);
 }
@@ -72,7 +85,7 @@
 }
 
-WINDOW *owl_popwin_get_curswin(const owl_popwin *pw)
+owl_window *owl_popwin_get_content(const owl_popwin *pw)
 {
-  return panel_window(pw->poppanel);
+  return pw->content;
 }
 
Index: viewwin.c
===================================================================
--- viewwin.c	(revision fe4c786091ae4f138f1edc26a13b2b8d0b3a034d)
+++ viewwin.c	(revision 68f63a2ed999ed960962a275c6230741ed1af603)
@@ -7,5 +7,5 @@
  * window that will be used by viewwin
  */
-void owl_viewwin_init_text(owl_viewwin *v, WINDOW *win, int winlines, int wincols, const char *text)
+void owl_viewwin_init_text(owl_viewwin *v, owl_window *win, const char *text)
 {
   owl_fmtext_init_null(&(v->fmtext));
@@ -19,8 +19,7 @@
   v->topline=0;
   v->rightshift=0;
-  v->winlines=winlines;
-  v->wincols=wincols;
-  v->curswin=win;
   v->onclose_hook = NULL;
+
+  owl_viewwin_set_window(v, win);
 }
 
@@ -28,4 +27,5 @@
     owl_fmtext_append_normal(&(v->fmtext), text);
     v->textlines=owl_fmtext_num_lines(&(v->fmtext));  
+    owl_window_dirty(v->window);
 }
 
@@ -33,5 +33,5 @@
  * window that will be used by viewwin
  */
-void owl_viewwin_init_fmtext(owl_viewwin *v, WINDOW *win, int winlines, int wincols, const owl_fmtext *fmtext)
+void owl_viewwin_init_fmtext(owl_viewwin *v, owl_window *win, const owl_fmtext *fmtext)
 {
   char *text;
@@ -46,14 +46,20 @@
   v->topline=0;
   v->rightshift=0;
-  v->winlines=winlines;
-  v->wincols=wincols;
-  v->curswin=win;
+
+  owl_viewwin_set_window(v, win);
 }
 
-void owl_viewwin_set_curswin(owl_viewwin *v, WINDOW *w, int winlines, int wincols)
+void owl_viewwin_set_window(owl_viewwin *v, owl_window *w)
 {
-  v->curswin=w;
-  v->winlines=winlines;
-  v->wincols=wincols;
+  if (v->window) {
+    owl_window_set_redraw_cb(v->window, 0, 0, 0);
+    owl_window_set_resize_cb(v->window, 0, 0, 0);
+  }
+  v->window = w;
+  if (w) {
+    owl_window_get_position(w, &v->winlines, &v->wincols, 0, 0);
+    owl_window_set_redraw_cb(w, owl_viewwin_redisplay, v, 0);
+    owl_window_set_resize_cb(w, owl_viewwin_resize_hook, v, 0);
+  }
 }
 
@@ -63,15 +69,18 @@
 }
 
+void owl_viewwin_resize_hook(owl_window *w, void *user_data)
+{
+  owl_viewwin *v = user_data;
+  owl_window_get_position(w, &v->winlines, &v->wincols, 0, 0);
+}
+
 /* regenerate text on the curses window. */
-void owl_viewwin_redisplay(owl_viewwin *v)
+void owl_viewwin_redisplay(owl_window *w, WINDOW *curswin, void *user_data)
 {
   owl_fmtext fm1, fm2;
-
-  /* avoid segfault when screen too small to create curswin */
-  if (v->curswin == NULL)
-    return;
+  owl_viewwin *v = user_data;
   
-  werase(v->curswin);
-  wmove(v->curswin, 0, 0);
+  werase(curswin);
+  wmove(curswin, 0, 0);
 
   owl_fmtext_init_null(&fm1);
@@ -81,15 +90,15 @@
   owl_fmtext_truncate_cols(&fm1, v->rightshift, v->wincols-1+v->rightshift, &fm2);
 
-  owl_fmtext_curs_waddstr_without_search(&fm2, v->curswin);
+  owl_fmtext_curs_waddstr_without_search(&fm2, curswin);
 
   /* print the message at the bottom */
-  wmove(v->curswin, v->winlines-1, 0);
-  wattrset(v->curswin, A_REVERSE);
+  wmove(curswin, v->winlines-1, 0);
+  wattrset(curswin, A_REVERSE);
   if (v->textlines - v->topline > v->winlines-BOTTOM_OFFSET) {
-    waddstr(v->curswin, "--More-- (Space to see more, 'q' to quit)");
+    waddstr(curswin, "--More-- (Space to see more, 'q' to quit)");
   } else {
-    waddstr(v->curswin, "--End-- (Press 'q' to quit)");
+    waddstr(curswin, "--End-- (Press 'q' to quit)");
   }
-  wattroff(v->curswin, A_REVERSE);
+  wattroff(curswin, A_REVERSE);
 
   owl_fmtext_cleanup(&fm1);
@@ -103,4 +112,5 @@
     v->topline = v->textlines - v->winlines + BOTTOM_OFFSET;
   }
+  owl_window_dirty(v->window);
 }
 
@@ -111,4 +121,5 @@
     v->topline = v->textlines - v->winlines + BOTTOM_OFFSET;
   }
+  owl_window_dirty(v->window);
 }
 
@@ -117,4 +128,5 @@
   v->topline-=v->winlines;
   if (v->topline<0) v->topline=0;
+  owl_window_dirty(v->window);
 }
 
@@ -123,4 +135,5 @@
   v->topline--;
   if (v->topline<0) v->topline=0;
+  owl_window_dirty(v->window);
 }
 
@@ -128,4 +141,5 @@
 {
   v->rightshift+=n;
+  owl_window_dirty(v->window);
 }
 
@@ -134,4 +148,5 @@
   v->rightshift-=n;
   if (v->rightshift<0) v->rightshift=0;
+  owl_window_dirty(v->window);
 }
 
@@ -140,4 +155,5 @@
   v->topline=0;
   v->rightshift=0;
+  owl_window_dirty(v->window);
 }
 
@@ -145,4 +161,5 @@
 {
   v->topline = v->textlines - v->winlines + BOTTOM_OFFSET;
+  owl_window_dirty(v->window);
 }
 
