]> git.decadent.org.uk Git - videolink.git/blob - style_sheets.cpp
Changed style-sheet application in Mozila/XULRunner 1.8 to override built-in preferen...
[videolink.git] / style_sheets.cpp
1 // Copyright 2005 Ben Hutchings <ben@decadent.org.uk>.
2 // See the file "COPYING" for licence details.
3
4 #include "style_sheets.hpp"
5
6 #include <nsContentCID.h>
7 #if MOZ_VERSION_MAJOR > 1 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
8 #   include <nsIStyleSheetService.h>
9 #   include <nsServiceManagerUtils.h>
10 #else
11 #   include <nsICSSLoader.h>
12 #   include <nsICSSStyleSheet.h>
13 #   include <nsIPresShell.h>
14 #   include <nsIServiceManagerUtils.h>
15 #endif
16 #include <nsIURI.h>
17 #include <nsNetUtil.h>
18
19 #include "xpcom_support.hpp"
20
21 using xpcom_support::check;
22
23 #if MOZ_VERSION_MAJOR > 1 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
24
25 // We just have to load and register a style-sheet as a user
26 // style-sheet.  There is no need to do anything for each page.
27
28 agent_style_sheet_holder init_agent_style_sheet(const char * uri)
29 {
30     nsCOMPtr<nsIURI> style_sheet_uri;
31     check(NS_NewURI(getter_AddRefs(style_sheet_uri), nsCString(uri)));
32
33     nsCOMPtr<nsIStyleSheetService> style_sheet_service;
34     static const nsCID style_sheet_service_cid = {
35         // NS_STYLESHEETSERVICE_CID copied from
36         // layout/base/nsStyleSheetService.cpp
37         0xfcca6f83, 0x9f7d, 0x44e4,
38         {0xa7, 0x4b, 0xb5, 0x94, 0x33, 0xe6, 0xc8, 0xc3}
39     };
40     check(CallGetService<nsIStyleSheetService>(
41               style_sheet_service_cid, getter_AddRefs(style_sheet_service)));
42     check(style_sheet_service->LoadAndRegisterSheet(
43               style_sheet_uri, nsIStyleSheetService::USER_SHEET));
44
45     return agent_style_sheet_holder();
46 }
47
48 #else // Mozilla version < 1.8
49
50 already_AddRefed<nsIStyleSheet> init_agent_style_sheet(const char * uri)
51 {
52     nsCOMPtr<nsICSSLoader> css_loader;
53     static const nsCID css_loader_cid = NS_CSS_LOADER_CID;
54     check(CallGetService<nsICSSLoader>(css_loader_cid,
55                                        getter_AddRefs(css_loader)));
56
57     nsCOMPtr<nsIURI> style_sheet_uri;
58     check(NS_NewURI(getter_AddRefs(style_sheet_uri), nsCString(uri)));
59
60     nsICSSStyleSheet * style_sheet;
61     check(css_loader->LoadAgentSheet(style_sheet_uri, &style_sheet));
62     return style_sheet;
63 }
64
65 // Apply a style-sheet to a given presentation shell as the top-priority
66 // agent style-sheet and disable the preferences-derived style rules.
67 void apply_agent_style_sheet(nsIStyleSheet * style_sheet,
68                              nsIPresShell * pres_shell)
69 {
70     nsCOMArray<nsIStyleSheet> style_sheets;
71     check(pres_shell->GetAgentStyleSheets(style_sheets));
72     check(style_sheets.InsertObjectAt(style_sheet, 0));
73     check(pres_shell->SetAgentStyleSheets(style_sheets));
74
75     check(pres_shell->EnablePrefStyleRules(false));
76
77     // Update the display
78     check(pres_shell->ReconstructStyleData());
79     check(pres_shell->FlushPendingNotifications(true));
80 }
81
82 #endif // Mozilla version >=/< 1.8