1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
24
* Alternatively, the contents of this file may be used under the terms of
25
* either the GNU General Public License Version 2 or later (the "GPL"), or
26
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27
* in which case the provisions of the GPL or the LGPL are applicable instead
28
* of those above. If you wish to allow use of your version of this file only
29
* under the terms of either the GPL or the LGPL, and not to allow others to
30
* use your version of this file under the terms of the NPL, indicate your
31
* decision by deleting the provisions above and replace them with the notice
32
* and other provisions required by the GPL or the LGPL. If you do not delete
33
* the provisions above, a recipient may use your version of this file under
34
* the terms of any one of the NPL, the GPL or the LGPL.
36
* ***** END LICENSE BLOCK ***** */
38
#include "nsPresContext.h"
39
#include "nsIPresShell.h"
41
#include "nsILinkHandler.h"
42
#include "nsIDocShellTreeItem.h"
43
#include "nsStyleSet.h"
44
#include "nsImageLoader.h"
45
#include "nsIContent.h"
47
#include "nsIRenderingContext.h"
49
#include "nsIDocument.h"
50
#include "nsStyleContext.h"
51
#include "nsLayoutAtoms.h"
52
#include "nsILookAndFeel.h"
53
#include "nsWidgetsCID.h"
54
#include "nsIComponentManager.h"
55
#include "nsIURIContentListener.h"
56
#include "nsIInterfaceRequestor.h"
57
#include "nsIInterfaceRequestorUtils.h"
58
#include "nsIServiceManager.h"
59
#include "nsIDOMElement.h"
60
#include "nsContentPolicyUtils.h"
61
#include "nsIScriptGlobalObject.h"
62
#include "nsIDOMWindow.h"
63
#include "nsXPIDLString.h"
64
#include "nsIWeakReferenceUtils.h"
65
#include "nsCSSRendering.h"
67
#include "nsContentPolicyUtils.h"
68
#include "nsIScriptGlobalObject.h"
69
#include "nsIDOMDocument.h"
70
#include "nsAutoPtr.h"
71
#include "nsEventStateManager.h"
73
#include "nsBidiPresUtils.h"
76
// Needed for Start/Stop of Image Animation
77
#include "imgIContainer.h"
78
#include "nsIImageLoadingContent.h"
80
//needed for resetting of image service color
81
#include "nsLayoutCID.h"
82
#include "nsISelectionImageService.h"
85
MakeColorPref(const char *colstr)
87
PRUint32 red, green, blue;
90
// 4.x stored RGB color values as a string rather than as an int,
91
// thus we need to do this conversion
92
PR_sscanf(colstr, "#%02x%02x%02x", &red, &green, &blue);
93
colorref = NS_RGB(red, green, blue);
98
nsPresContext::PrefChangedCallback(const char* aPrefName, void* instance_data)
100
nsPresContext* presContext = (nsPresContext*)instance_data;
102
NS_ASSERTION(nsnull != presContext, "bad instance data");
103
if (nsnull != presContext) {
104
presContext->PreferenceChanged(aPrefName);
111
IsVisualCharset(const nsCAutoString& aCharset)
113
if (aCharset.EqualsIgnoreCase("ibm864") // Arabic//ahmed
114
|| aCharset.EqualsIgnoreCase("ibm862") // Hebrew
115
|| aCharset.EqualsIgnoreCase("iso-8859-8") ) { // Hebrew
116
return PR_TRUE; // visual text type
119
return PR_FALSE; // logical text type
125
PR_STATIC_CALLBACK(PRBool) destroy_loads(nsHashKey *aKey, void *aData, void* closure)
127
nsISupports *sup = NS_REINTERPRET_CAST(nsISupports*, aData);
128
nsImageLoader *loader = NS_REINTERPRET_CAST(nsImageLoader*, sup);
133
static NS_DEFINE_CID(kLookAndFeelCID, NS_LOOKANDFEEL_CID);
134
#include "nsContentCID.h"
135
static NS_DEFINE_CID(kSelectionImageService, NS_SELECTIONIMAGESERVICE_CID);
138
nsPresContext::nsPresContext()
139
: mDefaultVariableFont("serif", NS_FONT_STYLE_NORMAL, NS_FONT_VARIANT_NORMAL,
140
NS_FONT_WEIGHT_NORMAL, 0, NSIntPointsToTwips(12)),
141
mDefaultFixedFont("monospace", NS_FONT_STYLE_NORMAL, NS_FONT_VARIANT_NORMAL,
142
NS_FONT_WEIGHT_NORMAL, 0, NSIntPointsToTwips(10)),
143
mDefaultSerifFont("serif", NS_FONT_STYLE_NORMAL, NS_FONT_VARIANT_NORMAL,
144
NS_FONT_WEIGHT_NORMAL, 0, NSIntPointsToTwips(12)),
145
mDefaultSansSerifFont("sans-serif", NS_FONT_STYLE_NORMAL, NS_FONT_VARIANT_NORMAL,
146
NS_FONT_WEIGHT_NORMAL, 0, NSIntPointsToTwips(12)),
147
mDefaultMonospaceFont("monospace", NS_FONT_STYLE_NORMAL, NS_FONT_VARIANT_NORMAL,
148
NS_FONT_WEIGHT_NORMAL, 0, NSIntPointsToTwips(10)),
149
mDefaultCursiveFont("cursive", NS_FONT_STYLE_NORMAL, NS_FONT_VARIANT_NORMAL,
150
NS_FONT_WEIGHT_NORMAL, 0, NSIntPointsToTwips(12)),
151
mDefaultFantasyFont("fantasy", NS_FONT_STYLE_NORMAL, NS_FONT_VARIANT_NORMAL,
152
NS_FONT_WEIGHT_NORMAL, 0, NSIntPointsToTwips(12))
154
mCompatibilityMode = eCompatibility_FullStandards;
155
mImageAnimationMode = imgIContainer::kNormalAnimMode;
156
mImageAnimationModePref = imgIContainer::kNormalAnimMode;
158
SetBackgroundImageDraw(PR_TRUE); // always draw the background
159
SetBackgroundColorDraw(PR_TRUE);
162
mLinkHandler = nsnull;
165
mViewportStyleOverflow = NS_STYLE_OVERFLOW_AUTO;
167
mDefaultColor = NS_RGB(0x00, 0x00, 0x00);
168
mBackgroundColor = NS_RGB(0xFF, 0xFF, 0xFF);
170
mUseDocumentColors = PR_TRUE;
171
mUseDocumentFonts = PR_TRUE;
173
// the minimum font-size is unconstrained by default
174
mMinimumFontSize = 0;
176
mLinkColor = NS_RGB(0x00, 0x00, 0xEE);
177
mActiveLinkColor = NS_RGB(0xEE, 0x00, 0x00);
178
mVisitedLinkColor = NS_RGB(0x55, 0x1A, 0x8B);
179
mUnderlineLinks = PR_TRUE;
181
mUseFocusColors = PR_FALSE;
182
mFocusTextColor = mDefaultColor;
183
mFocusBackgroundColor = mBackgroundColor;
185
mFocusRingOnAnything = PR_FALSE;
187
mLanguageSpecificTransformType = eLanguageSpecificTransformType_Unknown;
188
mIsRenderingOnlySelection = PR_FALSE;
190
mIsVisual = PR_FALSE;
192
mIsBidiSystem = PR_FALSE;
197
nsPresContext::~nsPresContext()
199
mImageLoaders.Enumerate(destroy_loads);
201
NS_PRECONDITION(!mShell, "Presshell forgot to clear our mShell pointer");
205
mEventManager->SetPresContext(nsnull); // unclear if this is needed, but can't hurt
206
NS_RELEASE(mEventManager);
209
// Unregister preference callbacks
211
mPrefs->UnregisterCallback("font.", nsPresContext::PrefChangedCallback, (void*)this);
212
mPrefs->UnregisterCallback("browser.display.", nsPresContext::PrefChangedCallback, (void*)this);
213
mPrefs->UnregisterCallback("browser.underline_anchors", nsPresContext::PrefChangedCallback, (void*)this);
214
mPrefs->UnregisterCallback("browser.anchor_color", nsPresContext::PrefChangedCallback, (void*)this);
215
mPrefs->UnregisterCallback("browser.active_color", nsPresContext::PrefChangedCallback, (void*)this);
216
mPrefs->UnregisterCallback("browser.visited_color", nsPresContext::PrefChangedCallback, (void*)this);
217
mPrefs->UnregisterCallback("network.image.imageBehavior", nsPresContext::PrefChangedCallback, (void*)this);
218
mPrefs->UnregisterCallback("image.animation_mode", nsPresContext::PrefChangedCallback, (void*)this);
220
mPrefs->UnregisterCallback("bidi.", PrefChangedCallback, (void*)this);
229
NS_IF_RELEASE(mDeviceContext);
230
NS_IF_RELEASE(mLookAndFeel);
231
NS_IF_RELEASE(mLanguage);
234
NS_IMPL_ISUPPORTS2(nsPresContext, nsIPresContext, nsIObserver)
236
#define MAKE_FONT_PREF_KEY(_pref, _s0, _s1) \
240
static const char* const kGenericFont[] = {
251
nsPresContext::GetFontPreferences()
253
if (!mPrefs || !mLanguage)
256
/* Fetch the font prefs to be used -- see bug 61883 for details.
257
Not all prefs are needed upfront. Some are fallback prefs intended
258
for the GFX font sub-system...
260
1) unit : assumed to be the same for all language groups -------------
261
font.size.unit = px | pt XXX could be folded in the size... bug 90440
263
2) attributes for generic fonts --------------------------------------
264
font.default = serif | sans-serif - fallback generic font
265
font.name.[generic].[langGroup] = current user' selected font on the pref dialog
266
font.name-list.[generic].[langGroup] = fontname1, fontname2, ... [factory pre-built list]
267
font.size.[generic].[langGroup] = integer - settable by the user
268
font.size-adjust.[generic].[langGroup] = "float" - settable by the user
269
font.minimum-size.[langGroup] = integer - settable by the user
273
GetScaledPixelsToTwips(&p2t);
274
mDefaultVariableFont.size = NSFloatPixelsToTwips((float)16, p2t);
275
mDefaultFixedFont.size = NSFloatPixelsToTwips((float)13, p2t);
277
nsAutoString langGroup;
278
nsCOMPtr<nsIAtom> langGroupAtom;
279
mLanguage->GetLanguageGroup(getter_AddRefs(langGroupAtom));
280
langGroupAtom->ToString(langGroup);
284
nsXPIDLCString cvalue;
286
// get the current applicable font-size unit
287
enum {eUnit_unknown = -1, eUnit_px, eUnit_pt};
288
PRInt32 unit = eUnit_px;
289
nsresult rv = mPrefs->CopyCharPref("font.size.unit", getter_Copies(cvalue));
290
if (NS_SUCCEEDED(rv)) {
291
if (!PL_strcmp(cvalue.get(), "px")) {
294
else if (!PL_strcmp(cvalue.get(), "pt")) {
298
NS_WARNING("unexpected font-size unit -- expected: 'px' or 'pt'");
299
unit = eUnit_unknown;
303
// get font.minimum-size.[langGroup]
306
pref.Assign("font.minimum-size.");
307
AppendUTF16toUTF8(langGroup, pref);
309
rv = mPrefs->GetIntPref(pref.get(), &size);
310
if (NS_SUCCEEDED(rv)) {
311
if (unit == eUnit_px) {
312
mMinimumFontSize = NSFloatPixelsToTwips((float)size, p2t);
314
else if (unit == eUnit_pt) {
315
mMinimumFontSize = NSIntPointsToTwips(size);
319
// get attributes specific to each generic font
320
nsCAutoString generic_dot_langGroup;
321
for (PRInt32 eType = eDefaultFont_Variable; eType < eDefaultFont_COUNT; ++eType) {
322
generic_dot_langGroup.Assign(kGenericFont[eType]);
323
AppendUTF16toUTF8(langGroup, generic_dot_langGroup);
327
case eDefaultFont_Variable: font = &mDefaultVariableFont; break;
328
case eDefaultFont_Fixed: font = &mDefaultFixedFont; break;
329
case eDefaultFont_Serif: font = &mDefaultSerifFont; break;
330
case eDefaultFont_SansSerif: font = &mDefaultSansSerifFont; break;
331
case eDefaultFont_Monospace: font = &mDefaultMonospaceFont; break;
332
case eDefaultFont_Cursive: font = &mDefaultCursiveFont; break;
333
case eDefaultFont_Fantasy: font = &mDefaultFantasyFont; break;
334
default: NS_ERROR("not reached - bogus to silence some compilers"); break;
337
// set the default variable font (the other fonts are seen as 'generic' fonts
338
// in GFX and will be queried there when hunting for alternative fonts)
339
if (eType == eDefaultFont_Variable) {
340
MAKE_FONT_PREF_KEY(pref, "font.name", generic_dot_langGroup);
341
rv = mPrefs->CopyUnicharPref(pref.get(), getter_Copies(value));
342
if (NS_SUCCEEDED(rv)) {
343
font->name.Assign(value);
346
rv = mPrefs->CopyUnicharPref("font.default", getter_Copies(value));
347
if (NS_SUCCEEDED(rv)) {
348
mDefaultVariableFont.name.Assign(value);
353
if (eType == eDefaultFont_Monospace) {
354
// This takes care of the confusion whereby people often expect "monospace"
355
// to have the same default font-size as "-moz-fixed" (this tentative
356
// size may be overwritten with the specific value for "monospace" when
357
// "font.size.monospace.[langGroup]" is read -- see below)
358
font->size = mDefaultFixedFont.size;
360
else if (eType != eDefaultFont_Fixed) {
361
// all the other generic fonts are initialized with the size of the
362
// variable font, but their specific size can supersede later -- see below
363
font->size = mDefaultVariableFont.size;
367
// Bug 84398: for spec purists, a different font-size only applies to the
368
// .variable. and .fixed. fonts and the other fonts should get |font-size-adjust|.
369
// The problem is that only GfxWin has the support for |font-size-adjust|. So for
370
// parity, we enable the ability to set a different font-size on all platforms.
372
// get font.size.[generic].[langGroup]
373
// size=0 means 'Auto', i.e., generic fonts retain the size of the variable font
374
MAKE_FONT_PREF_KEY(pref, "font.size", generic_dot_langGroup);
375
rv = mPrefs->GetIntPref(pref.get(), &size);
376
if (NS_SUCCEEDED(rv) && size > 0) {
377
if (unit == eUnit_px) {
378
font->size = NSFloatPixelsToTwips((float)size, p2t);
380
else if (unit == eUnit_pt) {
381
font->size = NSIntPointsToTwips(size);
385
// get font.size-adjust.[generic].[langGroup]
386
// XXX only applicable on GFX ports that handle |font-size-adjust|
387
MAKE_FONT_PREF_KEY(pref, "font.size-adjust", generic_dot_langGroup);
388
rv = mPrefs->CopyCharPref(pref.get(), getter_Copies(cvalue));
389
if (NS_SUCCEEDED(rv)) {
390
font->sizeAdjust = (float)atof(cvalue.get());
394
printf("%s Family-list:%s size:%d sizeAdjust:%.2f\n",
395
generic_dot_langGroup.get(),
396
NS_ConvertUCS2toUTF8(font->name).get(), font->size,
403
nsPresContext::GetDocumentColorPreferences()
405
PRBool usePrefColors = PR_TRUE;
407
nsXPIDLCString colorStr;
408
nsCOMPtr<nsIDocShellTreeItem> docShell(do_QueryReferent(mContainer));
410
PRInt32 docShellType;
411
docShell->GetItemType(&docShellType);
412
if (nsIDocShellTreeItem::typeChrome == docShellType)
413
usePrefColors = PR_FALSE;
416
if (NS_SUCCEEDED(mPrefs->GetBoolPref("browser.display.use_system_colors", &boolPref))) {
417
usePrefColors = !boolPref;
421
if (NS_SUCCEEDED(mPrefs->CopyCharPref("browser.display.foreground_color", getter_Copies(colorStr)))) {
422
mDefaultColor = MakeColorPref(colorStr);
424
if (NS_SUCCEEDED(mPrefs->CopyCharPref("browser.display.background_color", getter_Copies(colorStr)))) {
425
mBackgroundColor = MakeColorPref(colorStr);
429
mDefaultColor = NS_RGB(0x00, 0x00, 0x00);
430
mBackgroundColor = NS_RGB(0xFF, 0xFF, 0xFF);
431
mLookAndFeel->GetColor(nsILookAndFeel::eColor_WindowForeground,
433
mLookAndFeel->GetColor(nsILookAndFeel::eColor_WindowBackground,
437
if (NS_SUCCEEDED(mPrefs->GetBoolPref("browser.display.use_document_colors", &boolPref))) {
438
mUseDocumentColors = boolPref;
443
nsPresContext::GetUserPreferences()
447
if (NS_SUCCEEDED(mPrefs->GetIntPref("browser.display.base_font_scaler", &prefInt))) {
448
mFontScaler = prefInt;
452
GetDocumentColorPreferences();
456
nsXPIDLCString colorStr;
457
if (NS_SUCCEEDED(mPrefs->GetBoolPref("browser.underline_anchors", &boolPref))) {
458
mUnderlineLinks = boolPref;
460
if (NS_SUCCEEDED(mPrefs->CopyCharPref("browser.anchor_color", getter_Copies(colorStr)))) {
461
mLinkColor = MakeColorPref(colorStr);
463
if (NS_SUCCEEDED(mPrefs->CopyCharPref("browser.active_color", getter_Copies(colorStr)))) {
464
mActiveLinkColor = MakeColorPref(colorStr);
466
if (NS_SUCCEEDED(mPrefs->CopyCharPref("browser.visited_color", getter_Copies(colorStr)))) {
467
mVisitedLinkColor = MakeColorPref(colorStr);
471
if (NS_SUCCEEDED(mPrefs->GetBoolPref("browser.display.use_focus_colors", &boolPref))) {
472
mUseFocusColors = boolPref;
473
mFocusTextColor = mDefaultColor;
474
mFocusBackgroundColor = mBackgroundColor;
475
if (NS_SUCCEEDED(mPrefs->CopyCharPref("browser.display.focus_text_color", getter_Copies(colorStr)))) {
476
mFocusTextColor = MakeColorPref(colorStr);
478
if (NS_SUCCEEDED(mPrefs->CopyCharPref("browser.display.focus_background_color", getter_Copies(colorStr)))) {
479
mFocusBackgroundColor = MakeColorPref(colorStr);
483
if (NS_SUCCEEDED(mPrefs->GetIntPref("browser.display.focus_ring_width", &prefInt))) {
484
mFocusRingWidth = prefInt;
487
if (NS_SUCCEEDED(mPrefs->GetBoolPref("browser.display.focus_ring_on_anything", &boolPref))) {
488
mFocusRingOnAnything = boolPref;
492
if (NS_SUCCEEDED(mPrefs->GetIntPref("browser.display.use_document_fonts", &prefInt))) {
493
mUseDocumentFonts = prefInt == 0 ? PR_FALSE : PR_TRUE;
496
// * replace backslashes with Yen signs? (bug 245770)
497
if (NS_SUCCEEDED(mPrefs->GetBoolPref("layout.enable_japanese_specific_transform", &boolPref))) {
498
mEnableJapaneseTransform = boolPref;
501
GetFontPreferences();
504
char* animatePref = 0;
505
nsresult rv = mPrefs->CopyCharPref("image.animation_mode", &animatePref);
506
if (NS_SUCCEEDED(rv) && animatePref) {
507
if (!nsCRT::strcmp(animatePref, "normal"))
508
mImageAnimationModePref = imgIContainer::kNormalAnimMode;
509
else if (!nsCRT::strcmp(animatePref, "none"))
510
mImageAnimationModePref = imgIContainer::kDontAnimMode;
511
else if (!nsCRT::strcmp(animatePref, "once"))
512
mImageAnimationModePref = imgIContainer::kLoopOnceAnimMode;
513
nsMemory::Free(animatePref);
517
if (NS_SUCCEEDED(mPrefs->GetIntPref("bidi.direction", &prefInt))) {
518
SET_BIDI_OPTION_DIRECTION(mBidi, prefInt);
520
if (NS_SUCCEEDED(mPrefs->GetIntPref("bidi.texttype", &prefInt))) {
521
SET_BIDI_OPTION_TEXTTYPE(mBidi, prefInt);
523
if (NS_SUCCEEDED(mPrefs->GetIntPref("bidi.controlstextmode", &prefInt))) {
524
SET_BIDI_OPTION_CONTROLSTEXTMODE(mBidi, prefInt);
526
if (NS_SUCCEEDED(mPrefs->GetIntPref("bidi.numeral", &prefInt))) {
527
SET_BIDI_OPTION_NUMERAL(mBidi, prefInt);
529
if (NS_SUCCEEDED(mPrefs->GetIntPref("bidi.support", &prefInt))) {
530
SET_BIDI_OPTION_SUPPORT(mBidi, prefInt);
532
if (NS_SUCCEEDED(mPrefs->GetIntPref("bidi.characterset", &prefInt))) {
533
SET_BIDI_OPTION_CHARACTERSET(mBidi, prefInt);
539
nsPresContext::GetCachedIntPref(PRUint32 aPrefType, PRInt32& aValue)
543
case kPresContext_MinimumFontSize:
544
aValue = mMinimumFontSize;
547
rv = NS_ERROR_INVALID_ARG;
548
NS_ERROR("invalid arg");
554
nsPresContext::ClearStyleDataAndReflow()
557
// Clear out all our style data.
558
mShell->StyleSet()->ClearStyleData(this);
560
// Force a reflow of the root frame
561
// XXX We really should only do a reflow if a preference that affects
562
// formatting changed, e.g., a font change. If it's just a color change
563
// then we only need to repaint...
564
mShell->StyleChangeReflow();
569
nsPresContext::PreferenceChanged(const char* aPrefName)
571
nsCOMPtr<nsIDocShellTreeItem> docShell(do_QueryReferent(mContainer));
573
PRInt32 docShellType;
574
docShell->GetItemType(&docShellType);
575
if (nsIDocShellTreeItem::typeChrome == docShellType)
579
// Initialize our state from the user preferences
580
GetUserPreferences();
582
// update the presShell: tell it to set the preference style rules up
584
mShell->SetPreferenceStyleRules(PR_TRUE);
587
mDeviceContext->FlushFontCache();
588
nsPresContext::ClearStyleDataAndReflow();
592
nsPresContext::Init(nsIDeviceContext* aDeviceContext)
594
NS_ASSERTION(!(mInitialized == PR_TRUE), "attempt to reinit pres context");
595
NS_ENSURE_ARG(aDeviceContext);
597
mDeviceContext = aDeviceContext;
598
NS_ADDREF(mDeviceContext);
600
// Get the look and feel service here; default colors will be initialized
601
// from calling GetUserPreferences() below.
602
nsresult rv = CallGetService(kLookAndFeelCID, &mLookAndFeel);
604
NS_ERROR("LookAndFeel service must be implemented for this toolkit");
608
mLangService = do_GetService(NS_LANGUAGEATOMSERVICE_CONTRACTID);
609
mPrefs = do_GetService(NS_PREF_CONTRACTID);
611
// Register callbacks so we're notified when the preferences change
612
mPrefs->RegisterCallback("font.", nsPresContext::PrefChangedCallback, (void*)this);
613
mPrefs->RegisterCallback("browser.display.", nsPresContext::PrefChangedCallback, (void*)this);
614
mPrefs->RegisterCallback("browser.underline_anchors", nsPresContext::PrefChangedCallback, (void*)this);
615
mPrefs->RegisterCallback("browser.anchor_color", nsPresContext::PrefChangedCallback, (void*)this);
616
mPrefs->RegisterCallback("browser.active_color", nsPresContext::PrefChangedCallback, (void*)this);
617
mPrefs->RegisterCallback("browser.visited_color", nsPresContext::PrefChangedCallback, (void*)this);
618
mPrefs->RegisterCallback("network.image.imageBehavior", nsPresContext::PrefChangedCallback, (void*)this);
619
mPrefs->RegisterCallback("image.animation_mode", nsPresContext::PrefChangedCallback, (void*)this);
621
mPrefs->RegisterCallback("bidi.", PrefChangedCallback, (void*)this);
624
// Initialize our state from the user preferences
625
GetUserPreferences();
628
mEventManager = new nsEventStateManager();
630
return NS_ERROR_OUT_OF_MEMORY;
632
NS_ADDREF(mEventManager);
634
rv = mEventManager->Init();
635
NS_ENSURE_SUCCESS(rv, rv);
637
mEventManager->SetPresContext(this);
640
mInitialized = PR_TRUE;
646
// Note: We don't hold a reference on the shell; it has a reference to
649
nsPresContext::SetShell(nsIPresShell* aShell)
652
// Remove ourselves as the charset observer from the shell's doc, because
653
// this shell may be going away for good.
654
nsCOMPtr<nsIDocument> doc;
655
mShell->GetDocument(getter_AddRefs(doc));
657
doc->RemoveCharSetObserver(this);
664
nsCOMPtr<nsIDocument> doc;
665
if (NS_SUCCEEDED(mShell->GetDocument(getter_AddRefs(doc)))) {
666
NS_ASSERTION(doc, "expect document here");
668
nsIURI *baseURI = doc->GetBaseURI();
670
if (!mNeverAnimate && baseURI) {
671
PRBool isChrome = PR_FALSE;
672
PRBool isRes = PR_FALSE;
673
baseURI->SchemeIs("chrome", &isChrome);
674
baseURI->SchemeIs("resource", &isRes);
676
if (!isChrome && !isRes)
677
mImageAnimationMode = mImageAnimationModePref;
679
mImageAnimationMode = imgIContainer::kNormalAnimMode;
683
doc->AddCharSetObserver(this);
684
UpdateCharSet(doc->GetDocumentCharacterSet().get());
693
nsPresContext::UpdateCharSet(const char* aCharSet)
696
NS_IF_RELEASE(mLanguage);
697
mLangService->LookupCharSet(aCharSet, &mLanguage); // addrefs
699
nsCOMPtr<nsIAtom> langGroupAtom;
700
mLanguage->GetLanguageGroup(getter_AddRefs(langGroupAtom));
701
NS_ASSERTION(langGroupAtom, "non-NULL language group atom expected");
702
// bug 245770 comment #23
703
if (langGroupAtom.get() == nsLayoutAtoms::Japanese &&
704
mEnableJapaneseTransform &&
705
nsCRT::strncasecmp(aCharSet, "UTF-", 4)) { // not very robust
706
mLanguageSpecificTransformType =
707
eLanguageSpecificTransformType_Japanese;
710
mLanguageSpecificTransformType =
711
eLanguageSpecificTransformType_None;
714
GetFontPreferences();
720
SetVisualMode(IsVisualCharset(mCharset) );
725
nsPresContext::Observe(nsISupports* aSubject,
727
const PRUnichar* aData)
729
if (!nsCRT::strcmp(aTopic, "charset")) {
730
UpdateCharSet(NS_LossyConvertUCS2toASCII(aData).get());
731
mDeviceContext->FlushFontCache();
732
nsPresContext::ClearStyleDataAndReflow();
737
NS_WARNING("unrecognized topic in nsPresContext::Observe");
738
return NS_ERROR_FAILURE;
742
nsPresContext::SetCompatibilityMode(nsCompatibility aMode)
744
mCompatibilityMode = aMode;
749
// enable/disable the QuirkSheet
751
EnableQuirkStyleSheet(mCompatibilityMode == eCompatibility_NavQuirks);
754
// Helper function for setting Anim Mode on image
755
static void SetImgAnimModeOnImgReq(imgIRequest* aImgReq, PRUint16 aMode)
758
nsCOMPtr<imgIContainer> imgCon;
759
aImgReq->GetImage(getter_AddRefs(imgCon));
761
imgCon->SetAnimationMode(aMode);
766
// Enumeration call back for HashTable
767
PR_STATIC_CALLBACK(PRBool) set_animation_mode(nsHashKey *aKey, void *aData, void* closure)
769
nsISupports *sup = NS_REINTERPRET_CAST(nsISupports*, aData);
770
nsImageLoader *loader = NS_REINTERPRET_CAST(nsImageLoader*, sup);
771
imgIRequest* imgReq = loader->GetRequest();
772
SetImgAnimModeOnImgReq(imgReq, (PRUint16)NS_PTR_TO_INT32(closure));
776
// IMPORTANT: Assumption is that all images for a Presentation
777
// have the same Animation Mode (pavlov said this was OK)
779
// Walks content and set the animation mode
780
// this is a way to turn on/off image animations
781
void nsPresContext::SetImgAnimations(nsIContent *aParent, PRUint16 aMode)
783
nsCOMPtr<nsIImageLoadingContent> imgContent(do_QueryInterface(aParent));
785
nsCOMPtr<imgIRequest> imgReq;
786
imgContent->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
787
getter_AddRefs(imgReq));
788
SetImgAnimModeOnImgReq(imgReq, aMode);
791
PRUint32 count = aParent->GetChildCount();
792
for (PRUint32 i = 0; i < count; ++i) {
793
SetImgAnimations(aParent->GetChildAt(i), aMode);
798
nsPresContext::SetImageAnimationMode(PRUint16 aMode)
800
NS_ASSERTION(aMode == imgIContainer::kNormalAnimMode ||
801
aMode == imgIContainer::kDontAnimMode ||
802
aMode == imgIContainer::kLoopOnceAnimMode, "Wrong Animation Mode is being set!");
804
// This hash table contains a list of background images
805
// so iterate over it and set the mode
806
mImageLoaders.Enumerate(set_animation_mode, (void*)aMode);
808
// Now walk the content tree and set the animation mode
810
nsCOMPtr<nsIDocument> doc;
811
if (mShell != nsnull) {
812
mShell->GetDocument(getter_AddRefs(doc));
814
nsIContent *rootContent = doc->GetRootContent();
816
SetImgAnimations(rootContent, aMode);
821
mImageAnimationMode = aMode;
825
nsPresContext::GetXBLBindingURL(nsIContent* aContent, nsIURI** aResult)
827
nsRefPtr<nsStyleContext> sc;
828
sc = StyleSet()->ResolveStyleFor(aContent, nsnull);
829
NS_ENSURE_TRUE(sc, NS_ERROR_FAILURE);
831
*aResult = sc->GetStyleDisplay()->mBinding;
832
NS_IF_ADDREF(*aResult);
837
nsPresContext::AllocateFromShell(size_t aSize, void** aResult)
840
return mShell->AllocateFrame(aSize, aResult);
845
nsPresContext::FreeToShell(size_t aSize, void* aFreeChunk)
848
return mShell->FreeFrame(aSize, aFreeChunk);
853
nsPresContext::GetMetricsFor(const nsFont& aFont, nsIFontMetrics** aResult)
855
NS_PRECONDITION(aResult, "null out param");
857
nsIFontMetrics* metrics = nsnull;
858
nsCOMPtr<nsIAtom> langGroup;
860
mLanguage->GetLanguageGroup(getter_AddRefs(langGroup));
862
mDeviceContext->GetMetricsFor(aFont, langGroup, metrics);
868
nsPresContext::GetDefaultFont(PRUint8 aFontID) const
872
// Special (our default variable width font and fixed width font)
873
case kPresContext_DefaultVariableFont_ID:
874
font = &mDefaultVariableFont;
876
case kPresContext_DefaultFixedFont_ID:
877
font = &mDefaultFixedFont;
880
case kGenericFont_serif:
881
font = &mDefaultSerifFont;
883
case kGenericFont_sans_serif:
884
font = &mDefaultSansSerifFont;
886
case kGenericFont_monospace:
887
font = &mDefaultMonospaceFont;
889
case kGenericFont_cursive:
890
font = &mDefaultCursiveFont;
892
case kGenericFont_fantasy:
893
font = &mDefaultFantasyFont;
897
NS_ERROR("invalid arg");
904
nsPresContext::GetTwipsToPixelsForFonts(float* aResult) const
906
NS_PRECONDITION(nsnull != aResult, "null ptr");
907
if (nsnull == aResult) {
908
return NS_ERROR_NULL_POINTER;
912
#ifdef NS_PRINT_PREVIEW
913
// If an alternative DC is available we want to use
914
// it to get the scaling factor for fonts. Usually, the AltDC
915
// is a printing DC so therefore we need to get the printers
916
// scaling values for calculating the font heights
917
nsCOMPtr<nsIDeviceContext> altDC;
918
mDeviceContext->GetAltDevice(getter_AddRefs(altDC));
920
app2dev = altDC->AppUnitsToDevUnits();
922
app2dev = mDeviceContext->AppUnitsToDevUnits();
925
app2dev = mDeviceContext->AppUnitsToDevUnits();
934
nsPresContext::GetScaledPixelsToTwips(float* aResult) const
936
NS_PRECONDITION(aResult, "null out param");
941
p2t = mDeviceContext->DevUnitsToAppUnits();
942
mDeviceContext->GetCanonicalPixelScale(scale);
950
nsPresContext::LoadImage(imgIRequest* aImage,
951
nsIFrame* aTargetFrame,//may be null (precached image)
952
imgIRequest **aRequest)
954
// look and see if we have a loader for the target frame.
956
nsVoidKey key(aTargetFrame);
957
nsImageLoader *loader = NS_REINTERPRET_CAST(nsImageLoader*, mImageLoaders.Get(&key)); // addrefs
960
loader = new nsImageLoader();
962
return NS_ERROR_OUT_OF_MEMORY;
964
NS_ADDREF(loader); // new
966
loader->Init(aTargetFrame, this);
967
mImageLoaders.Put(&key, loader);
970
loader->Load(aImage);
972
NS_IF_ADDREF(*aRequest = loader->GetRequest());
981
nsPresContext::StopImagesFor(nsIFrame* aTargetFrame)
983
nsVoidKey key(aTargetFrame);
984
nsImageLoader *loader = NS_REINTERPRET_CAST(nsImageLoader*, mImageLoaders.Get(&key)); // addrefs
990
mImageLoaders.Remove(&key);
996
nsPresContext::SetContainer(nsISupports* aHandler)
998
mContainer = do_GetWeakReference(aHandler);
1000
GetDocumentColorPreferences();
1004
already_AddRefed<nsISupports>
1005
nsPresContext::GetContainer()
1007
nsISupports *result;
1009
CallQueryReferent(mContainer.get(), &result);
1018
nsPresContext::GetBidiEnabled(PRBool* aBidiEnabled) const
1020
NS_ENSURE_ARG_POINTER(aBidiEnabled);
1021
*aBidiEnabled = PR_FALSE;
1022
NS_ASSERTION(mShell, "PresShell must be set on PresContext before calling nsPresContext::GetBidiEnabled");
1024
nsCOMPtr<nsIDocument> doc;
1025
mShell->GetDocument(getter_AddRefs(doc) );
1026
NS_ASSERTION(doc, "PresShell has no document in nsPresContext::GetBidiEnabled");
1028
*aBidiEnabled = doc->GetBidiEnabled();
1035
nsPresContext::SetBidiEnabled(PRBool aBidiEnabled) const
1038
nsCOMPtr<nsIDocument> doc;
1039
mShell->GetDocument(getter_AddRefs(doc) );
1041
doc->SetBidiEnabled(aBidiEnabled);
1048
nsPresContext::GetBidiUtils(nsBidiPresUtils** aBidiUtils)
1050
nsresult rv = NS_OK;
1053
mBidiUtils = new nsBidiPresUtils;
1055
rv = NS_ERROR_OUT_OF_MEMORY;
1058
*aBidiUtils = mBidiUtils;
1062
NS_IMETHODIMP nsPresContext::SetBidi(PRUint32 aSource, PRBool aForceReflow)
1065
if (IBMBIDI_TEXTDIRECTION_RTL == GET_BIDI_OPTION_DIRECTION(mBidi)
1066
|| IBMBIDI_NUMERAL_HINDI == GET_BIDI_OPTION_NUMERAL(mBidi)) {
1067
SetBidiEnabled(PR_TRUE);
1069
if (IBMBIDI_TEXTTYPE_VISUAL == GET_BIDI_OPTION_TEXTTYPE(mBidi)) {
1070
SetVisualMode(PR_TRUE);
1072
else if (IBMBIDI_TEXTTYPE_LOGICAL == GET_BIDI_OPTION_TEXTTYPE(mBidi)) {
1073
SetVisualMode(PR_FALSE);
1076
SetVisualMode(IsVisualCharset(mCharset) );
1078
if (mShell && aForceReflow) {
1079
nsPresContext::ClearStyleDataAndReflow();
1083
NS_IMETHODIMP nsPresContext::GetBidi(PRUint32* aDest) const
1092
nsPresContext::GetLanguageSpecificTransformType(
1093
nsLanguageSpecificTransformType* aType)
1095
NS_PRECONDITION(aType, "null out param");
1096
*aType = mLanguageSpecificTransformType;
1102
nsPresContext::GetTheme(nsITheme** aResult)
1104
if (!mNoTheme && !mTheme) {
1105
mTheme = do_GetService("@mozilla.org/chrome/chrome-native-theme;1");
1111
NS_IF_ADDREF(*aResult);
1112
return mTheme ? NS_OK : NS_ERROR_FAILURE;
1116
nsPresContext::ThemeChanged()
1118
// Tell the theme that it changed, so it can flush any handles to stale theme
1121
mTheme->ThemeChanged();
1123
// Clear all cached nsILookAndFeel colors.
1125
mLookAndFeel->LookAndFeelChanged();
1130
return mShell->ReconstructStyleData();
1134
nsPresContext::SysColorChanged()
1137
// Don't use the cached values for the system colors
1138
mLookAndFeel->LookAndFeelChanged();
1141
// Reset default background and foreground colors for the document since
1142
// they may be using system colors
1143
GetDocumentColorPreferences();
1145
// Clear out all of the style data since it may contain RGB values
1146
// which originated from system colors.
1147
nsCOMPtr<nsISelectionImageService> imageService;
1149
imageService = do_GetService(kSelectionImageService, &result);
1150
if (NS_SUCCEEDED(result) && imageService)
1152
imageService->Reset();
1155
// We need to do a full reflow (and view update) here. Clearing the style
1156
// data without reflowing/updating views will lead to incorrect change hints
1157
// later, because when generating change hints, any style structs which have
1158
// been cleared and not reread are assumed to not be used at all.
1159
nsPresContext::ClearStyleDataAndReflow();
1163
#ifdef MOZ_REFLOW_PERF
1165
nsPresContext::CountReflows(const char * aName, PRUint32 aType, nsIFrame * aFrame)
1168
mShell->CountReflows(aName, aType, aFrame);
1174
nsPresContext::PaintCount(const char * aName, nsIRenderingContext* aRenderingContext, nsIFrame * aFrame, PRUint32 aColor)
1177
mShell->PaintCount(aName, aRenderingContext, this, aFrame, aColor);