Repository: myremylar/pygame_gui
Issue: -1 Dimensions Broken on Text Boxes

**Describe the bug**
The documentation says that setting the dimensions of the rect to -1 will cause the text box to match the final dimension to whatever the text rendering produces.  While it does match the final dimensions correctly, it doesn't seem to draw the textbox onto the screen and so the box appears to be invisible.

I got around this by using get_relative_rect() to find the new dimensions, then creating the textbox again using the new dimensions.

**To Reproduce**
Steps to reproduce the behaviour:
1. Create a new UITextBox with -1 as one or both of the dimensions.

**Expected behaviour**
I would expect the text box to adjust it's dimensions and still display on the screen without any extra input from me.

**Screenshots**
If applicable, add screenshots to help explain your problem.

**Platform and software (please complete the following information):**
 - OS: Windows
 - Pygame GUI version 0.6.8
 - Pygame version 2.2.0



Changed files:
pygame_gui/core/drawable_shapes/drawable_shape.py, pygame_gui/core/text/text_box_layout.py, pygame_gui/core/text/text_line_chunk.py

diff --git a/pygame_gui/core/drawable_shapes/drawable_shape.py b/pygame_gui/core/drawable_shapes/drawable_shape.py
index 2a9b147..4bdc17f 100644
--- a/pygame_gui/core/drawable_shapes/drawable_shape.py
+++ b/pygame_gui/core/drawable_shapes/drawable_shape.py
@@ -148,6 +148,8 @@ class DrawableShape:
 
         self.theming = theming_parameters
         self.containing_rect = containing_rect.copy()
+        self.dynamic_width = True if self.containing_rect.width == -1 else False
+        self.dynamic_height = True if self.containing_rect.height == -1 else False
         self.text_view_rect: Optional[pygame.Rect] = None
 
         self.shadow_width = 0
@@ -207,7 +209,7 @@ class DrawableShape:
         self.only_text_changed = False
 
     def _evaluate_contents_for_containing_rect(self):
-        if self.containing_rect.width == -1:
+        if self.dynamic_width:
             # check to see if we have text and a font, this won't work with HTML
             # text - throw a warning?
             # What we really need to to is process the html text layout by this
@@ -230,7 +232,7 @@ class DrawableShape:
                 self.text_view_rect.width = text_width
                 self.text_box_layout.view_rect.width = self.text_view_rect.width
                 self.containing_rect.width = final_width
-        if self.containing_rect.height == -1:
+        if self.dynamic_height:
             if self.text_box_layout is not None:
                 text_height = self.text_box_layout.layout_rect.height
 
@@ -490,10 +492,17 @@ class DrawableShape:
             self.text_view_rect = self.containing_rect.copy()
             self.text_view_rect.x = 0
             self.text_view_rect.y = 0
-            if self.text_view_rect.width != -1:
-                self.text_view_rect.width -= ((total_text_buffer * 2) + (2 * horiz_padding))
-            if self.text_view_rect.height != -1:
-                self.text_view_rect.height -= ((total_text_buffer * 2) + (2 * vert_padding))
+            if self.dynamic_width:
+                self.text_view_rect.width = -1
+            else:
+                self.text_view_rect.width = max(0, self.text_view_rect.width -
+                                                ((total_text_buffer * 2) + (2 * horiz_padding)))
+
+            if self.dynamic_height:
+                self.text_view_rect.height = -1
+            else:
+                self.text_view_rect.height = max(0, self.text_view_rect.height -
+                                                 ((total_text_buffer * 2) + (2 * vert_padding)))
 
             text_actual_area_rect = self.text_view_rect.copy()
             text_actual_area_rect.x = total_text_buffer + horiz_padding
diff --git a/pygame_gui/core/text/text_box_layout.py b/pygame_gui/core/text/text_box_layout.py
index 01958af..877bfa5 100644
--- a/pygame_gui/core/text/text_box_layout.py
+++ b/pygame_gui/core/text/text_box_layout.py
@@ -55,6 +55,15 @@ class TextBoxLayout:
             for rect in self.input_data_rect_queue:
                 self.layout_rect.width += rect.width
 
+        if self.view_rect.width == -1:
+            self.dynamic_width = True
+        else:
+            self.dynamic_width = False
+        if self.view_rect.height == -1:
+            self.dynamic_height = True
+        else:
+            self.dynamic_height = False
+
         self.layout_rect_queue = None
         self.finalised_surface = None
         self.floating_rects: List[TextLayoutRect] = []
@@ -142,6 +151,10 @@ class TextBoxLayout:
                 current_row = self._handle_regular_rect(current_row, text_layout_rect, input_queue)
         # make sure we add the last row to the layout
         self._add_row_to_layout(current_row, last_row=True)
+        if self.dynamic_width:
+            self.view_rect.width = self.layout_rect.width
+        if self.dynamic_height:
+            self.view_rect.height = self.layout_rect.height
 
     def _add_row_to_layout(self, current_row: TextBoxLayoutRow, last_row=False):
         # handle an empty row being added to layout
diff --git a/pygame_gui/core/text/text_line_chunk.py b/pygame_gui/core/text/text_line_chunk.py
index 108ba44..3de8f0c 100644
--- a/pygame_gui/core/text/text_line_chunk.py
+++ b/pygame_gui/core/text/text_line_chunk.py
@@ -109,9 +109,9 @@ class TextLineChunkFTFont(TextLayoutRect):
         text_width = self._text_render_width(text, font) + (2 * text_shadow_width)
         text_height = text_rect.height
         if max_dimensions is not None:
-            if max_dimensions[0] != -1:
+            if not (max_dimensions[0] == -1 or max_dimensions[1] == -1):
+                # we don't have dynamic maximum dimensions so use maximum dimensions for both
                 text_width = min(max_dimensions[0], text_width)
-            if max_dimensions[1] != -1:
                 text_height = min(max_dimensions[1], text_height)
         return text_height, text_rect, text_width
 
@@ -567,9 +567,9 @@ class TextLineChunkFTFont(TextLayoutRect):
         text_width = self._text_render_width(self.text, self.font)
         text_height = text_rect.height
         if self.max_dimensions is not None:
-            if self.max_dimensions[0] != -1:
+            if not (self.max_dimensions[0] == -1 or self.max_dimensions[1] == -1):
+                # we don't have dynamic maximum dimensions so use maximum dimensions for both
                 text_width = min(self.max_dimensions[0], text_width)
-            if self.max_dimensions[1] != -1:
                 text_height = min(self.max_dimensions[1], text_height)
 
         self.size = (text_width, text_height) # noqa pylint: disable=attribute-defined-outside-init; pylint getting confused
@@ -592,9 +592,9 @@ class TextLineChunkFTFont(TextLayoutRect):
         text_width = self._text_render_width(self.text, self.font)
         text_height = text_rect.height
         if self.max_dimensions is not None:
-            if self.max_dimensions[0] != -1:
+            if not (self.max_dimensions[0] == -1 or self.max_dimensions[1] == -1):
+                # we don't have dynamic maximum dimensions so use maximum dimensions for both
                 text_width = min(self.max_dimensions[0], text_width)
-            if self.max_dimensions[1] != -1:
                 text_height = min(self.max_dimensions[1], text_height)
 
         self.size = (text_width, text_height)  # noqa pylint: disable=attribute-defined-outside-init; pylint getting confused