Dynamically loading .css files

Discussion of features and help with problems encountered while using libRocket

Dynamically loading .css files

Postby aquawicket on Mon Jul 10, 2017 6:06 pm

I'm attempting to load a .css file after the document is already created. Is this possible.

This can be done in most browsers by appending a link element to the head node. Rocket however provides no access to the head node that i'm aware of.

Code: Select all
// Method 1 - Not Working
Rocket::Core::StyleSheet* current_sheet = GetDocument()->GetStyleSheet();
Rocket::Core::StyleSheet* new_sheet = Rocket::Core::Factory::InstanceStyleSheetFile("MyStyleSheet.css");
current_sheet->CombineStyleSheet(new_sheet);
GetDocument()->SetStyleSheet(current_sheet);


Code: Select all
// Method 2 - Not Working
Rocket::Core::Element* link = GetDocument->CreateElement("link");
link->SetAttribute("rel", "stylesheet");
link->SetAttribute("type", "text/css");
link->SetAttribute("href", "MyStyleSheet.css");
Rocket::Core::Element* body = GetDocument();
body->AppendChild(link, true);
aquawicket
 
Posts: 10
Joined: Fri Jun 27, 2014 6:07 am

Re: Dynamically loading .css files

Postby aquawicket on Mon Jul 10, 2017 7:34 pm

I was able to get it working. My mistake was not using CombineStyleSheet()'s return value.

Working code:
Code: Select all
Rocket::Core::StyleSheet* current_sheet = GetDocument()->GetStyleSheet();
Rocket::Core::StyleSheet* new_sheet = Rocket::Core::Factory::InstanceStyleSheetFile("file.css");
current_sheet = current_sheet->CombineStyleSheet(new_sheet);
GetDocument()->SetStyleSheet(current_sheet);
aquawicket
 
Posts: 10
Joined: Fri Jun 27, 2014 6:07 am


Return to Using libRocket


cron