File: style_sheets.cpp

package info (click to toggle)
videolink 1.0-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 368 kB
  • ctags: 502
  • sloc: cpp: 3,210; ansic: 880; makefile: 121
file content (82 lines) | stat: -rw-r--r-- 2,713 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2005 Ben Hutchings <ben@decadent.org.uk>.
// See the file "COPYING" for licence details.

#include "style_sheets.hpp"

#include <nsContentCID.h>
#if MOZ_VERSION_MAJOR > 1 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)
#   include <nsIStyleSheetService.h>
#   include <nsServiceManagerUtils.h>
#else
#   include <nsICSSLoader.h>
#   include <nsICSSStyleSheet.h>
#   include <nsIPresShell.h>
#   include <nsIServiceManagerUtils.h>
#endif
#include <nsIURI.h>
#include <nsNetUtil.h>

#include "xpcom_support.hpp"

using xpcom_support::check;

#if MOZ_VERSION_MAJOR > 1 || (MOZ_VERSION_MAJOR == 1 && MOZ_VERSION_MINOR >= 8)

// We just have to load and register a style-sheet as a user
// style-sheet.  There is no need to do anything for each page.

agent_style_sheet_holder init_agent_style_sheet(const char * uri)
{
    nsCOMPtr<nsIURI> style_sheet_uri;
    check(NS_NewURI(getter_AddRefs(style_sheet_uri), nsCString(uri)));

    nsCOMPtr<nsIStyleSheetService> style_sheet_service;
    static const nsCID style_sheet_service_cid = {
	// NS_STYLESHEETSERVICE_CID copied from
	// layout/base/nsStyleSheetService.cpp
	0xfcca6f83, 0x9f7d, 0x44e4,
	{0xa7, 0x4b, 0xb5, 0x94, 0x33, 0xe6, 0xc8, 0xc3}
    };
    check(CallGetService<nsIStyleSheetService>(
	      style_sheet_service_cid, getter_AddRefs(style_sheet_service)));
    check(style_sheet_service->LoadAndRegisterSheet(
	      style_sheet_uri, nsIStyleSheetService::USER_SHEET));

    return agent_style_sheet_holder();
}

#else // Mozilla version < 1.8

already_AddRefed<nsIStyleSheet> init_agent_style_sheet(const char * uri)
{
    nsCOMPtr<nsICSSLoader> css_loader;
    static const nsCID css_loader_cid = NS_CSS_LOADER_CID;
    check(CallGetService<nsICSSLoader>(css_loader_cid,
				       getter_AddRefs(css_loader)));

    nsCOMPtr<nsIURI> style_sheet_uri;
    check(NS_NewURI(getter_AddRefs(style_sheet_uri), nsCString(uri)));

    nsICSSStyleSheet * style_sheet;
    check(css_loader->LoadAgentSheet(style_sheet_uri, &style_sheet));
    return style_sheet;
}

// Apply a style-sheet to a given presentation shell as the top-priority
// agent style-sheet and disable the preferences-derived style rules.
void apply_agent_style_sheet(nsIStyleSheet * style_sheet,
			     nsIPresShell * pres_shell)
{
    nsCOMArray<nsIStyleSheet> style_sheets;
    check(pres_shell->GetAgentStyleSheets(style_sheets));
    check(style_sheets.InsertObjectAt(style_sheet, 0));
    check(pres_shell->SetAgentStyleSheets(style_sheets));

    check(pres_shell->EnablePrefStyleRules(false));

    // Update the display
    check(pres_shell->ReconstructStyleData());
    check(pres_shell->FlushPendingNotifications(true));
}

#endif // Mozilla version >=/< 1.8