diff --git a/gdb/tui/tui-data.h b/gdb/tui/tui-data.h
index 570b55b1962..a5e4940666c 100644
--- a/gdb/tui/tui-data.h
+++ b/gdb/tui/tui-data.h
@@ -231,74 +231,16 @@ extern struct tui_win_info *tui_win_list[MAX_MAJOR_WINDOWS];
 #define TUI_DATA_WIN    ((tui_data_window *) tui_win_list[DATA_WIN])
 #define TUI_CMD_WIN     ((tui_cmd_window *) tui_win_list[CMD_WIN])
 
-/* An iterator that iterates over all windows.  */
+/* All the windows that are currently instantiated, in layout
+   order.  */
+extern std::vector<tui_win_info *> tui_windows;
 
-class tui_window_iterator
+/* Return a range adapter for iterating over TUI windows.  */
+static inline std::vector<tui_win_info *> &
+all_tui_windows ()
 {
-public:
-
-  typedef tui_window_iterator self_type;
-  typedef struct tui_win_info *value_type;
-  typedef struct tui_win_info *&reference;
-  typedef struct tui_win_info **pointer;
-  typedef std::forward_iterator_tag iterator_category;
-  typedef int difference_type;
-
-  explicit tui_window_iterator (enum tui_win_type type)
-    : m_type (type)
-  {
-    advance ();
-  }
-
-  tui_window_iterator ()
-    : m_type (MAX_MAJOR_WINDOWS)
-  {
-  }
-
-  bool operator!= (const self_type &other) const
-  {
-    return m_type != other.m_type;
-  }
-
-  value_type operator* () const
-  {
-    gdb_assert (m_type < MAX_MAJOR_WINDOWS);
-    return tui_win_list[m_type];
-  }
-
-  self_type &operator++ ()
-  {
-    ++m_type;
-    advance ();
-    return *this;
-  }
-
-private:
-
-  void advance ()
-  {
-    while (m_type < MAX_MAJOR_WINDOWS && tui_win_list[m_type] == nullptr)
-      ++m_type;
-  }
-
-  int m_type;
-};
-
-/* A range adapter for iterating over TUI windows.  */
-
-struct all_tui_windows
-{
-  tui_window_iterator begin () const
-  {
-    return tui_window_iterator (SRC_WIN);
-  }
-
-  tui_window_iterator end () const
-  {
-    return tui_window_iterator ();
-  }
-};
-
+  return tui_windows;
+}
 
 /* Data Manipulation Functions.  */
 extern int tui_term_height (void);
diff --git a/gdb/tui/tui-layout.c b/gdb/tui/tui-layout.c
index f33317beee8..9ad91ce713b 100644
--- a/gdb/tui/tui-layout.c
+++ b/gdb/tui/tui-layout.c
@@ -61,11 +61,15 @@ static tui_layout_split *applied_skeleton;
 static tui_layout_split *src_regs_layout;
 static tui_layout_split *asm_regs_layout;
 
+/* See tui-data.h.  */
+std::vector<tui_win_info *> tui_windows;
+
 /* See tui-layout.h.  */
 
 void
 tui_apply_current_layout ()
 {
+  tui_windows.clear ();
   applied_layout->apply (0, 0, tui_term_width (), tui_term_height ());
 }
 
@@ -350,6 +354,8 @@ tui_layout_window::apply (int x_, int y_, int width_, int height_)
   height = height_;
   gdb_assert (m_window != nullptr);
   m_window->resize (height, width, x, y);
+  if (dynamic_cast<tui_win_info *> (m_window) != nullptr)
+    tui_windows.push_back ((tui_win_info *) m_window);
 }
 
 /* See tui-layout.h.  */
diff --git a/gdb/tui/tui-winsource.h b/gdb/tui/tui-winsource.h
index 87bd3b61d97..cae535fab07 100644
--- a/gdb/tui/tui-winsource.h
+++ b/gdb/tui/tui-winsource.h
@@ -153,6 +153,8 @@ struct tui_source_window_iterator
 {
 public:
 
+  typedef std::vector<tui_win_info *>::iterator inner_iterator;
+
   typedef tui_source_window_iterator self_type;
   typedef struct tui_source_window_base *value_type;
   typedef struct tui_source_window_base *&reference;
@@ -160,14 +162,16 @@ public:
   typedef std::forward_iterator_tag iterator_category;
   typedef int difference_type;
 
-  explicit tui_source_window_iterator (bool dummy)
-    : m_iter (SRC_WIN)
+  explicit tui_source_window_iterator (const inner_iterator &it,
+				       const inner_iterator &end)
+    : m_iter (it),
+      m_end (end)
   {
     advance ();
   }
 
-  tui_source_window_iterator ()
-    : m_iter (tui_win_type (DISASSEM_WIN + 1))
+  explicit tui_source_window_iterator (const inner_iterator &it)
+    : m_iter (it)
   {
   }
 
@@ -178,7 +182,7 @@ public:
 
   value_type operator* () const
   {
-    return (value_type) *m_iter;
+    return dynamic_cast<tui_source_window_base *> (*m_iter);
   }
 
   self_type &operator++ ()
@@ -192,12 +196,13 @@ private:
 
   void advance ()
   {
-    tui_window_iterator end;
-    while (m_iter != end && *m_iter == nullptr)
+    while (m_iter != m_end
+	   && dynamic_cast<tui_source_window_base *> (*m_iter) == nullptr)
       ++m_iter;
   }
 
-  tui_window_iterator m_iter;
+  inner_iterator m_iter;
+  inner_iterator m_end;
 };
 
 /* A range adapter for source windows.  */
@@ -206,12 +211,13 @@ struct tui_source_windows
 {
   tui_source_window_iterator begin () const
   {
-    return tui_source_window_iterator (true);
+    return tui_source_window_iterator (tui_windows.begin (),
+				       tui_windows.end ());
   }
 
   tui_source_window_iterator end () const
   {
-    return tui_source_window_iterator ();
+    return tui_source_window_iterator (tui_windows.end ());
   }
 };
 
