[PATCH 2/6] Use an inner window in tui_py_window

Tom Tromey tom@tromey.com
Sat Jun 27 21:20:47 GMT 2020


This changes tui_py_window to create an inner curses window.  This
greatly simplifies tui_py_window::output, beacuse it no longer needs
to be careful to avoid overwriting the window's border.  This patch
also makes it a bit easier for a later patch to rewrite
tui_copy_source_line.

gdb/ChangeLog
2020-06-27  Tom Tromey  <tom@tromey.com>

	* python/py-tui.c (class tui_py_window) <refresh_window>: New
	method.
	<erase>: Update.
	<cursor_x, cursor_y>: Remove.
	<m_inner_window>: New member.
	(tui_py_window::rerender): Create inner window.
	(tui_py_window::output): Write to inner window.
---
 gdb/ChangeLog       | 10 ++++++++
 gdb/python/py-tui.c | 59 ++++++++++++++++++++++++---------------------
 2 files changed, 42 insertions(+), 27 deletions(-)

diff --git a/gdb/python/py-tui.c b/gdb/python/py-tui.c
index 95c71f1d2dd..3f44cfc3d12 100644
--- a/gdb/python/py-tui.c
+++ b/gdb/python/py-tui.c
@@ -86,15 +86,23 @@ class tui_py_window : public tui_win_info
   void do_scroll_vertical (int num_to_scroll) override;
   void do_scroll_horizontal (int num_to_scroll) override;
 
+  void refresh_window () override
+  {
+    tui_win_info::refresh_window ();
+    if (m_inner_window != nullptr)
+      {
+	touchwin (m_inner_window.get ());
+	tui_wrefresh (m_inner_window.get ());
+      }
+  }
+
   /* Erase and re-box the window.  */
   void erase ()
   {
-    if (is_visible ())
+    if (is_visible () && m_inner_window != nullptr)
       {
-	werase (handle.get ());
+	werase (m_inner_window.get ());
 	check_and_display_highlight_if_needed ();
-	cursor_x = 0;
-	cursor_y = 0;
       }
   }
 
@@ -115,13 +123,13 @@ class tui_py_window : public tui_win_info
 
 private:
 
-  /* Location of the cursor.  */
-  int cursor_x = 0;
-  int cursor_y = 0;
-
   /* The name of this window.  */
   std::string m_name;
 
+  /* We make our own inner window, so that it is easy to print without
+     overwriting the border.  */
+  std::unique_ptr<WINDOW, curses_deleter> m_inner_window;
+
   /* The underlying Python window object.  */
   gdbpy_ref<> m_window;
 
@@ -155,8 +163,21 @@ tui_py_window::~tui_py_window ()
 void
 tui_py_window::rerender ()
 {
+  tui_win_info::rerender ();
+
   gdbpy_enter enter_py (get_current_arch (), current_language);
 
+  int h = viewport_height ();
+  int w = viewport_width ();
+  if (h == 0 || w == 0)
+    {
+      /* The window would be too small, so just remove the
+	 contents.  */
+      m_inner_window.reset (nullptr);
+      return;
+    }
+  m_inner_window.reset (newwin (h, w, y + 1, x + 1));
+
   if (PyObject_HasAttrString (m_window.get (), "render"))
     {
       gdbpy_ref<> result (PyObject_CallMethod (m_window.get (), "render",
@@ -197,27 +218,11 @@ tui_py_window::do_scroll_vertical (int num_to_scroll)
 void
 tui_py_window::output (const char *text)
 {
-  int vwidth = viewport_width ();
-
-  while (cursor_y < viewport_height () && *text != '\0')
+  if (m_inner_window != nullptr)
     {
-      wmove (handle.get (), cursor_y + 1, cursor_x + 1);
-
-      std::string line = tui_copy_source_line (&text, 0, 0,
-					       vwidth - cursor_x, 0);
-      tui_puts (line.c_str (), handle.get ());
-
-      if (*text == '\n')
-	{
-	  ++text;
-	  ++cursor_y;
-	  cursor_x = 0;
-	}
-      else
-	cursor_x = getcurx (handle.get ()) - 1;
+      tui_puts (text, m_inner_window.get ());
+      tui_wrefresh (m_inner_window.get ());
     }
-
-  wrefresh (handle.get ());
 }
 
 
-- 
2.17.2



More information about the Gdb-patches mailing list