1 // Copyright 2005 Ben Hutchings <ben@decadent.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 #if MOZ_VERSION_MAJOR > 1 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
11 # include <nsServiceManagerUtils.h>
13 # include <nsIServiceManagerUtils.h>
16 #include <nsNetUtil.h>
18 #include "xpcom_support.hpp"
20 using xpcom_support::check;
22 // Load a CSS from an (absolute) URI.
23 // TODO: Support loading from an absolute, or better, relative filename.
24 already_AddRefed<nsIStyleSheet> load_css(const char * uri)
26 nsCOMPtr<nsICSSLoader> css_loader;
27 static const nsCID css_loader_cid = NS_CSS_LOADER_CID;
28 check(CallGetService<nsICSSLoader>(css_loader_cid,
29 getter_AddRefs(css_loader)));
31 nsCOMPtr<nsIURI> style_sheet_uri;
32 check(NS_NewURI(getter_AddRefs(style_sheet_uri), nsCString(uri)));
34 nsICSSStyleSheet * style_sheet;
35 check(css_loader->LoadAgentSheet(style_sheet_uri, &style_sheet));
39 // Apply a style-sheet to a given presentation shell as the top-priority
40 // agent style-sheet and disable the preferences-derived style rules.
41 void apply_style_sheet(nsIStyleSheet * style_sheet, nsIPresShell * pres_shell)
43 nsCOMArray<nsIStyleSheet> style_sheets;
44 check(pres_shell->GetAgentStyleSheets(style_sheets));
45 check(style_sheets.InsertObjectAt(style_sheet, 0));
46 check(pres_shell->SetAgentStyleSheets(style_sheets));
48 // FIXME: We need to find an alternative that works in Mozilla 1.8.
49 # if MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR < 8
50 check(pres_shell->EnablePrefStyleRules(false));
54 # if MOZ_VERSION_MAJOR > 1 \
55 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
56 pres_shell->ReconstructStyleData();
57 check(pres_shell->FlushPendingNotifications(Flush_Display));
59 check(pres_shell->ReconstructStyleData());
60 check(pres_shell->FlushPendingNotifications(true));