Browse Source

Fix the data grid and inline-block layout with specified width

Michael Ragazzon 6 years ago
parent
commit
a0f066d8d7

+ 1 - 4
Include/Rocket/Controls/ElementDataGridCell.h

@@ -49,10 +49,7 @@ public:
 
 	void Initialise(int column, Core::Element* header);
 	int GetColumn();
-
-protected:
-	virtual void OnResize() override;
-
+	
 private:
 	int column;
 	Core::Element* header;

+ 1 - 2
Include/Rocket/Controls/ElementDataGridRow.h

@@ -66,8 +66,7 @@ public:
 	/// Returns the number of children that aren't dirty (have been loaded)
 	int GetNumLoadedChildren();
 
-	// Removes all the child cells and fetches them again from the data
-	// source.
+	/// Removes all the child cells and fetches them again from the data source.
 	void RefreshRows();
 
 	/// Returns whether this row is expanded or not.

+ 1 - 1
Samples/assets/invader.rcss

@@ -199,7 +199,7 @@ datagrid input.text
 	width: 100%;
 	height: auto;
 	margin: 0px;
-	padding: 0px 2px 0px;
+	padding: 0 5px;
 
 	border-width: 1px;
 	border-color: black;

+ 12 - 10
Samples/invaders/data/high_score.rml

@@ -5,8 +5,8 @@
 		<style>
 			body
 			{
-				width: 350px;
-				height: 350px;
+				width: 420px;
+				height: 420px;
 				
 				margin: auto;
 			}
@@ -20,17 +20,19 @@
 			datagrid
 			{
 				margin-bottom: 20px;
-				min-rows: 10;
 			}
 			
 			datagrid data_grid_body
 			{
 				min-height: 200px;
 			}
-			
+			datagrid datagridrow
+			{
+				padding: 7px 0;
+			}
 			defender
 			{
-				display: block;
+				display: inline-block;
 				width: 64px;
 				height: 16px;
 				
@@ -40,11 +42,11 @@
 		</style>
 	</head>
 	<body template="window" onload="add_score">
-		<datagrid source="high_scores.scores" onrowupdate="check_input">
-			<col fields="name,name_required" formatter="name" width="40%">Pilot:</col>
-			<col fields="colour" formatter="ship" width="20%">Ship:</col>
-			<col fields="wave" width="20%">Wave:</col>
-			<col fields="score" width="20%">Score:</col>
+		<datagrid source="high_scores.scores" onrowupdate="check_input" min-rows="10">
+			<col fields="name,name_required" formatter="name" width="40%">Pilot</col>
+			<col fields="colour" formatter="ship" width="20%">Ship</col>
+			<col fields="wave" width="20%">Wave</col>
+			<col fields="score" width="20%">Score</col>
 		</datagrid>
 		<button onclick="check_name; goto main_menu; close game_window">Main Menu</button>
 	</body>

+ 6 - 5
Samples/invaders/src/EventHandlerOptions.cpp

@@ -123,19 +123,20 @@ void EventHandlerOptions::ProcessEvent(Rocket::Core::Event& event, const Rocket:
 	// warning message.
 	else if (value == "bad_graphics")
 	{
-		Rocket::Core::ElementDocument* options_body = event.GetTargetElement()->GetOwnerDocument();
+		using namespace Rocket::Core;
+		ElementDocument* options_body = event.GetTargetElement()->GetOwnerDocument();
 		if (options_body == NULL)
 			return;
 
-		Rocket::Core::Element* bad_warning = options_body->GetElementById("bad_warning");
+		Element* bad_warning = options_body->GetElementById("bad_warning");
 		if (bad_warning)
 		{
 			// The 'value' parameter of an onchange event is set to the value the control would send if it was
 			// submitted; so, the empty string if it is clear or to the 'value' attribute of the control if it is set.
-			if (event.GetParameter< Rocket::Core::String >("value", "").empty())
-				bad_warning->SetProperty("display", "none");
+			if (event.GetParameter< String >("value", "").empty())
+				bad_warning->SetProperty("display", Property(Style::Display::None));
 			else
-				bad_warning->SetProperty("display", "block");
+				bad_warning->SetProperty("display", Property(Style::Display::Block));
 		}
 	}
 }

+ 2 - 2
Source/Controls/ElementDataGrid.cpp

@@ -44,7 +44,7 @@ ElementDataGrid::ElementDataGrid(const Rocket::Core::String& tag) : Core::Elemen
 	Rocket::Core::XMLAttributes attributes;
 
 	// Create the row for the column headers:
-	header = dynamic_cast< ElementDataGridRow* >(Core::Factory::InstanceElement(this, "#rktctl_datagridrow", "datagridheader", attributes));
+	header = static_cast< ElementDataGridRow* >(Core::Factory::InstanceElement(this, "#rktctl_datagridrow", "datagridheader", attributes));
 	header->SetProperty("display", Core::Property(Core::Style::Display::Block));
 	header->Initialise(this);
 	AppendChild(header);
@@ -58,7 +58,7 @@ ElementDataGrid::ElementDataGrid(const Rocket::Core::String& tag) : Core::Elemen
 
 	body_visible = false;
 
-	root = dynamic_cast< ElementDataGridRow* >(Core::Factory::InstanceElement(this, "#rktctl_datagridrow", "datagridroot", attributes));
+	root = static_cast< ElementDataGridRow* >(Core::Factory::InstanceElement(this, "#rktctl_datagridrow", "datagridroot", attributes));
 	root->SetProperty("display", Core::Property(Core::Style::Display::None));
 	root->Initialise(this);
 

+ 2 - 6
Source/Controls/ElementDataGridCell.cpp

@@ -51,7 +51,8 @@ void ElementDataGridCell::Initialise(int _column, Core::Element* _header)
 	if (header)
 	{
 		header->AddReference();
-		SetProperty("width", Core::Property(header->GetBox().GetSize(Core::Box::MARGIN).x, Core::Property::PX));
+		if(auto p = header->GetLocalProperty("width"))
+			SetProperty("width", *p);
 	}
 }
 
@@ -60,10 +61,5 @@ int ElementDataGridCell::GetColumn()
 	return column;
 }
 
-void ElementDataGridCell::OnResize()
-{
-	SetProperty("width", Core::Property(header->GetBox().GetSize(Core::Box::MARGIN).x, Core::Property::PX));
-}
-
 }
 }

+ 3 - 3
Source/Core/LayoutEngine.cpp

@@ -83,8 +83,7 @@ bool LayoutEngine::FormatElement(Element* element, const Vector2f& containing_bl
 
 	if (shrink_to_fit)
 	{
-		// For inline blocks, we want to shrink the box back to its inner content width, recreating the LayoutBlockBox.
-		// There is an issue where resize events are not propagated correctly, which affects e.g. DataGridCells.
+		// For inline blocks with 'auto' width, we want to shrink the box back to its inner content width, recreating the LayoutBlockBox.
 		float content_width = block_box->InternalContentWidth();
 
 		if (content_width < containing_block.x)
@@ -393,7 +392,8 @@ bool LayoutEngine::FormatElementReplaced(Element* element)
 	Vector2f containing_block_size = GetContainingBlock(block_context_box);
 
 	LayoutEngine layout_engine;
-	layout_engine.FormatElement(element, containing_block_size, true);
+	bool shrink_to_width = element->GetComputedValues().width.type == Style::Width::Auto;
+	layout_engine.FormatElement(element, containing_block_size, shrink_to_width);
 
 	block_context_box->AddInlineElement(element, element->GetBox())->Close();
 

+ 1 - 1
Source/Core/WidgetSlider.cpp

@@ -105,7 +105,7 @@ bool WidgetSlider::Initialise(Orientation _orientation)
 	track = Factory::InstanceElement(parent, "*", "slidertrack", XMLAttributes());
 
 	bar = Factory::InstanceElement(parent, "*", "sliderbar", XMLAttributes());
-	bar->SetProperty(DRAG, DRAG);
+	bar->SetProperty(DRAG, Core::Property(Core::Style::Drag::Drag));
 
 	arrows[0] = Factory::InstanceElement(parent, "*", "sliderarrowdec", XMLAttributes());
 	arrows[1] = Factory::InstanceElement(parent, "*", "sliderarrowinc", XMLAttributes());