1 // Copyright 2005 Ben Hutchings <ben@decadentplace.org.uk>.
2 // See the file "COPYING" for licence details.
4 #include "style_sheets.hpp"
6 #include <nsContentCID.h>
7 #include <nsICSSLoader.h>
8 #include <nsICSSStyleSheet.h>
9 #include <nsIPresShell.h>
10 #include <nsIServiceManagerUtils.h>
12 #include <nsNetUtil.h>
14 #include "xpcom_support.hpp"
16 using xpcom_support::check;
18 // Load a CSS from an (absolute) URI.
19 // TODO: Support loading from an absolute, or better, relative filename.
20 already_AddRefed<nsIStyleSheet> load_css(const char * uri)
22 nsCOMPtr<nsICSSLoader> css_loader;
23 static const nsCID css_loader_cid = NS_CSS_LOADER_CID;
24 check(CallGetService<nsICSSLoader>(css_loader_cid,
25 getter_AddRefs(css_loader)));
27 nsCOMPtr<nsIURI> style_sheet_uri;
28 check(NS_NewURI(getter_AddRefs(style_sheet_uri), nsCString(uri)));
30 nsICSSStyleSheet * style_sheet;
31 check(css_loader->LoadAgentSheet(style_sheet_uri, &style_sheet));
35 // Apply a style-sheet to a given presentation shell as the top-priority
36 // agent style-sheet and disable the preferences-derived style rules.
37 void apply_style_sheet(nsIStyleSheet * style_sheet, nsIPresShell * pres_shell)
39 nsCOMArray<nsIStyleSheet> style_sheets;
40 check(pres_shell->GetAgentStyleSheets(style_sheets));
41 check(style_sheets.InsertObjectAt(style_sheet, 0));
42 check(pres_shell->SetAgentStyleSheets(style_sheets));
44 check(pres_shell->EnablePrefStyleRules(false));
47 check(pres_shell->ReconstructStyleData());
48 check(pres_shell->FlushPendingNotifications(true));