~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/layout/base/src/nsPresContext.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
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.
 
35
 *
 
36
 * ***** END LICENSE BLOCK ***** */
 
37
#include "nsCOMPtr.h"
 
38
#include "nsPresContext.h"
 
39
#include "nsIPresShell.h"
 
40
#include "nsIPref.h"
 
41
#include "nsILinkHandler.h"
 
42
#include "nsIDocShellTreeItem.h"
 
43
#include "nsStyleSet.h"
 
44
#include "nsImageLoader.h"
 
45
#include "nsIContent.h"
 
46
#include "nsIFrame.h"
 
47
#include "nsIRenderingContext.h"
 
48
#include "nsIURL.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"
 
66
#include "prprf.h"
 
67
#include "nsContentPolicyUtils.h"
 
68
#include "nsIScriptGlobalObject.h"
 
69
#include "nsIDOMDocument.h"
 
70
#include "nsAutoPtr.h"
 
71
#include "nsEventStateManager.h"
 
72
#ifdef IBMBIDI
 
73
#include "nsBidiPresUtils.h"
 
74
#endif // IBMBIDI
 
75
 
 
76
// Needed for Start/Stop of Image Animation
 
77
#include "imgIContainer.h"
 
78
#include "nsIImageLoadingContent.h"
 
79
 
 
80
//needed for resetting of image service color
 
81
#include "nsLayoutCID.h"
 
82
#include "nsISelectionImageService.h"
 
83
 
 
84
static nscolor
 
85
MakeColorPref(const char *colstr)
 
86
{
 
87
  PRUint32 red, green, blue;
 
88
  nscolor colorref;
 
89
 
 
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);
 
94
  return colorref;
 
95
}
 
96
 
 
97
int PR_CALLBACK
 
98
nsPresContext::PrefChangedCallback(const char* aPrefName, void* instance_data)
 
99
{
 
100
  nsPresContext*  presContext = (nsPresContext*)instance_data;
 
101
 
 
102
  NS_ASSERTION(nsnull != presContext, "bad instance data");
 
103
  if (nsnull != presContext) {
 
104
    presContext->PreferenceChanged(aPrefName);
 
105
  }
 
106
  return 0;  // PREF_OK
 
107
}
 
108
 
 
109
#ifdef IBMBIDI
 
110
PRBool
 
111
IsVisualCharset(const nsCAutoString& aCharset)
 
112
{
 
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
 
117
  }
 
118
  else {
 
119
    return PR_FALSE; // logical text type
 
120
  }
 
121
}
 
122
#endif // IBMBIDI
 
123
 
 
124
 
 
125
PR_STATIC_CALLBACK(PRBool) destroy_loads(nsHashKey *aKey, void *aData, void* closure)
 
126
{
 
127
  nsISupports *sup = NS_REINTERPRET_CAST(nsISupports*, aData);
 
128
  nsImageLoader *loader = NS_REINTERPRET_CAST(nsImageLoader*, sup);
 
129
  loader->Destroy();
 
130
  return PR_TRUE;
 
131
}
 
132
 
 
133
static NS_DEFINE_CID(kLookAndFeelCID,  NS_LOOKANDFEEL_CID);
 
134
#include "nsContentCID.h"
 
135
static NS_DEFINE_CID(kSelectionImageService, NS_SELECTIONIMAGESERVICE_CID);
 
136
 
 
137
 
 
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))
 
153
{
 
154
  mCompatibilityMode = eCompatibility_FullStandards;
 
155
  mImageAnimationMode = imgIContainer::kNormalAnimMode;
 
156
  mImageAnimationModePref = imgIContainer::kNormalAnimMode;
 
157
 
 
158
  SetBackgroundImageDraw(PR_TRUE);              // always draw the background
 
159
  SetBackgroundColorDraw(PR_TRUE);
 
160
 
 
161
  mShell = nsnull;
 
162
  mLinkHandler = nsnull;
 
163
  mContainer = nsnull;
 
164
 
 
165
  mViewportStyleOverflow = NS_STYLE_OVERFLOW_AUTO;
 
166
 
 
167
  mDefaultColor = NS_RGB(0x00, 0x00, 0x00);
 
168
  mBackgroundColor = NS_RGB(0xFF, 0xFF, 0xFF);
 
169
  
 
170
  mUseDocumentColors = PR_TRUE;
 
171
  mUseDocumentFonts = PR_TRUE;
 
172
 
 
173
  // the minimum font-size is unconstrained by default
 
174
  mMinimumFontSize = 0;
 
175
 
 
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;
 
180
 
 
181
  mUseFocusColors = PR_FALSE;
 
182
  mFocusTextColor = mDefaultColor;
 
183
  mFocusBackgroundColor = mBackgroundColor;
 
184
  mFocusRingWidth = 1;
 
185
  mFocusRingOnAnything = PR_FALSE;
 
186
 
 
187
  mLanguageSpecificTransformType = eLanguageSpecificTransformType_Unknown;
 
188
  mIsRenderingOnlySelection = PR_FALSE;
 
189
#ifdef IBMBIDI
 
190
  mIsVisual = PR_FALSE;
 
191
  mBidiUtils = nsnull;
 
192
  mIsBidiSystem = PR_FALSE;
 
193
  mBidi = 0;
 
194
#endif // IBMBIDI
 
195
}
 
196
 
 
197
nsPresContext::~nsPresContext()
 
198
{
 
199
  mImageLoaders.Enumerate(destroy_loads);
 
200
 
 
201
  NS_PRECONDITION(!mShell, "Presshell forgot to clear our mShell pointer");
 
202
  SetShell(nsnull);
 
203
 
 
204
  if (mEventManager) {
 
205
    mEventManager->SetPresContext(nsnull);   // unclear if this is needed, but can't hurt
 
206
    NS_RELEASE(mEventManager);
 
207
  }
 
208
 
 
209
  // Unregister preference callbacks
 
210
  if (mPrefs) {
 
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);
 
219
#ifdef IBMBIDI
 
220
    mPrefs->UnregisterCallback("bidi.", PrefChangedCallback, (void*)this);
 
221
#endif
 
222
  }
 
223
#ifdef IBMBIDI
 
224
  if (mBidiUtils) {
 
225
    delete mBidiUtils;
 
226
  }
 
227
#endif // IBMBIDI
 
228
 
 
229
  NS_IF_RELEASE(mDeviceContext);
 
230
  NS_IF_RELEASE(mLookAndFeel);
 
231
  NS_IF_RELEASE(mLanguage);
 
232
}
 
233
 
 
234
NS_IMPL_ISUPPORTS2(nsPresContext, nsIPresContext, nsIObserver)
 
235
 
 
236
#define MAKE_FONT_PREF_KEY(_pref, _s0, _s1) \
 
237
 _pref.Assign(_s0); \
 
238
 _pref.Append(_s1);
 
239
 
 
240
static const char* const kGenericFont[] = {
 
241
  ".variable.",
 
242
  ".fixed.",
 
243
  ".serif.", 
 
244
  ".sans-serif.", 
 
245
  ".monospace.",
 
246
  ".cursive.",
 
247
  ".fantasy."
 
248
};
 
249
 
 
250
void
 
251
nsPresContext::GetFontPreferences()
 
252
{
 
253
  if (!mPrefs || !mLanguage)
 
254
    return;
 
255
 
 
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...
 
259
 
 
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
 
262
 
 
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
 
270
  */
 
271
 
 
272
  float p2t;
 
273
  GetScaledPixelsToTwips(&p2t);
 
274
  mDefaultVariableFont.size = NSFloatPixelsToTwips((float)16, p2t);
 
275
  mDefaultFixedFont.size = NSFloatPixelsToTwips((float)13, p2t);
 
276
 
 
277
  nsAutoString langGroup;
 
278
  nsCOMPtr<nsIAtom> langGroupAtom;
 
279
  mLanguage->GetLanguageGroup(getter_AddRefs(langGroupAtom));
 
280
  langGroupAtom->ToString(langGroup);
 
281
 
 
282
  nsCAutoString pref;
 
283
  nsXPIDLString value;
 
284
  nsXPIDLCString cvalue;
 
285
 
 
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")) {
 
292
      unit = eUnit_px;
 
293
    }
 
294
    else if (!PL_strcmp(cvalue.get(), "pt")) {
 
295
      unit = eUnit_pt;
 
296
    }
 
297
    else {
 
298
      NS_WARNING("unexpected font-size unit -- expected: 'px' or 'pt'");
 
299
      unit = eUnit_unknown;
 
300
    }
 
301
  }
 
302
 
 
303
  // get font.minimum-size.[langGroup]
 
304
  PRInt32 size;
 
305
 
 
306
  pref.Assign("font.minimum-size.");
 
307
  AppendUTF16toUTF8(langGroup, pref);
 
308
 
 
309
  rv = mPrefs->GetIntPref(pref.get(), &size);
 
310
  if (NS_SUCCEEDED(rv)) {
 
311
    if (unit == eUnit_px) {
 
312
      mMinimumFontSize = NSFloatPixelsToTwips((float)size, p2t);
 
313
    }
 
314
    else if (unit == eUnit_pt) {
 
315
      mMinimumFontSize = NSIntPointsToTwips(size);
 
316
    }
 
317
  }
 
318
 
 
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);
 
324
 
 
325
    nsFont* font;
 
326
    switch (eType) {
 
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;
 
335
    }
 
336
 
 
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);
 
344
      }
 
345
      else {
 
346
        rv = mPrefs->CopyUnicharPref("font.default", getter_Copies(value));
 
347
        if (NS_SUCCEEDED(rv)) {
 
348
          mDefaultVariableFont.name.Assign(value);
 
349
        }
 
350
      } 
 
351
    }
 
352
    else {
 
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;
 
359
      }
 
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;
 
364
      }
 
365
    }
 
366
 
 
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.
 
371
 
 
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);
 
379
      }
 
380
      else if (unit == eUnit_pt) {
 
381
        font->size = NSIntPointsToTwips(size);
 
382
      }
 
383
    }
 
384
 
 
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());
 
391
    }
 
392
 
 
393
#ifdef DEBUG_rbs
 
394
    printf("%s Family-list:%s size:%d sizeAdjust:%.2f\n",
 
395
            generic_dot_langGroup.get(),
 
396
           NS_ConvertUCS2toUTF8(font->name).get(), font->size,
 
397
           font->sizeAdjust);
 
398
#endif
 
399
  }
 
400
}
 
401
 
 
402
void
 
403
nsPresContext::GetDocumentColorPreferences()
 
404
{
 
405
  PRBool usePrefColors = PR_TRUE;
 
406
  PRBool boolPref;
 
407
  nsXPIDLCString colorStr;
 
408
  nsCOMPtr<nsIDocShellTreeItem> docShell(do_QueryReferent(mContainer));
 
409
  if (docShell) {
 
410
    PRInt32 docShellType;
 
411
    docShell->GetItemType(&docShellType);
 
412
    if (nsIDocShellTreeItem::typeChrome == docShellType)
 
413
      usePrefColors = PR_FALSE;
 
414
  }
 
415
  if (usePrefColors) {
 
416
    if (NS_SUCCEEDED(mPrefs->GetBoolPref("browser.display.use_system_colors", &boolPref))) {
 
417
      usePrefColors = !boolPref;
 
418
    }
 
419
  }
 
420
  if (usePrefColors) {
 
421
    if (NS_SUCCEEDED(mPrefs->CopyCharPref("browser.display.foreground_color", getter_Copies(colorStr)))) {
 
422
      mDefaultColor = MakeColorPref(colorStr);
 
423
    }
 
424
    if (NS_SUCCEEDED(mPrefs->CopyCharPref("browser.display.background_color", getter_Copies(colorStr)))) {
 
425
      mBackgroundColor = MakeColorPref(colorStr);
 
426
    }
 
427
  }
 
428
  else {
 
429
    mDefaultColor = NS_RGB(0x00, 0x00, 0x00);
 
430
    mBackgroundColor = NS_RGB(0xFF, 0xFF, 0xFF);
 
431
    mLookAndFeel->GetColor(nsILookAndFeel::eColor_WindowForeground,
 
432
                           mDefaultColor);
 
433
    mLookAndFeel->GetColor(nsILookAndFeel::eColor_WindowBackground,
 
434
                           mBackgroundColor);
 
435
  }
 
436
 
 
437
  if (NS_SUCCEEDED(mPrefs->GetBoolPref("browser.display.use_document_colors", &boolPref))) {
 
438
    mUseDocumentColors = boolPref;
 
439
  }
 
440
}
 
441
 
 
442
void
 
443
nsPresContext::GetUserPreferences()
 
444
{
 
445
  PRInt32 prefInt;
 
446
 
 
447
  if (NS_SUCCEEDED(mPrefs->GetIntPref("browser.display.base_font_scaler", &prefInt))) {
 
448
    mFontScaler = prefInt;
 
449
  }
 
450
 
 
451
  // * document colors
 
452
  GetDocumentColorPreferences();
 
453
 
 
454
  // * link colors
 
455
  PRBool boolPref;
 
456
  nsXPIDLCString colorStr;
 
457
  if (NS_SUCCEEDED(mPrefs->GetBoolPref("browser.underline_anchors", &boolPref))) {
 
458
    mUnderlineLinks = boolPref;
 
459
  }
 
460
  if (NS_SUCCEEDED(mPrefs->CopyCharPref("browser.anchor_color", getter_Copies(colorStr)))) {
 
461
    mLinkColor = MakeColorPref(colorStr);
 
462
  }
 
463
  if (NS_SUCCEEDED(mPrefs->CopyCharPref("browser.active_color", getter_Copies(colorStr)))) {
 
464
    mActiveLinkColor = MakeColorPref(colorStr);
 
465
  }
 
466
  if (NS_SUCCEEDED(mPrefs->CopyCharPref("browser.visited_color", getter_Copies(colorStr)))) {
 
467
    mVisitedLinkColor = MakeColorPref(colorStr);
 
468
  }
 
469
 
 
470
 
 
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);
 
477
    }
 
478
    if (NS_SUCCEEDED(mPrefs->CopyCharPref("browser.display.focus_background_color", getter_Copies(colorStr)))) {
 
479
      mFocusBackgroundColor = MakeColorPref(colorStr);
 
480
    }
 
481
  }
 
482
 
 
483
  if (NS_SUCCEEDED(mPrefs->GetIntPref("browser.display.focus_ring_width", &prefInt))) {
 
484
    mFocusRingWidth = prefInt;
 
485
  }
 
486
 
 
487
  if (NS_SUCCEEDED(mPrefs->GetBoolPref("browser.display.focus_ring_on_anything", &boolPref))) {
 
488
    mFocusRingOnAnything = boolPref;
 
489
  }
 
490
 
 
491
  // * use fonts?
 
492
  if (NS_SUCCEEDED(mPrefs->GetIntPref("browser.display.use_document_fonts", &prefInt))) {
 
493
    mUseDocumentFonts = prefInt == 0 ? PR_FALSE : PR_TRUE;
 
494
  }
 
495
 
 
496
  // * replace backslashes with Yen signs? (bug 245770)
 
497
  if (NS_SUCCEEDED(mPrefs->GetBoolPref("layout.enable_japanese_specific_transform", &boolPref))) {
 
498
    mEnableJapaneseTransform = boolPref;
 
499
  }
 
500
  
 
501
  GetFontPreferences();
 
502
 
 
503
  // * image animation
 
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);
 
514
  }
 
515
 
 
516
#ifdef IBMBIDI
 
517
  if (NS_SUCCEEDED(mPrefs->GetIntPref("bidi.direction", &prefInt))) {
 
518
     SET_BIDI_OPTION_DIRECTION(mBidi, prefInt);
 
519
  }
 
520
  if (NS_SUCCEEDED(mPrefs->GetIntPref("bidi.texttype", &prefInt))) {
 
521
     SET_BIDI_OPTION_TEXTTYPE(mBidi, prefInt);
 
522
  }
 
523
  if (NS_SUCCEEDED(mPrefs->GetIntPref("bidi.controlstextmode", &prefInt))) {
 
524
     SET_BIDI_OPTION_CONTROLSTEXTMODE(mBidi, prefInt);
 
525
  }
 
526
  if (NS_SUCCEEDED(mPrefs->GetIntPref("bidi.numeral", &prefInt))) {
 
527
     SET_BIDI_OPTION_NUMERAL(mBidi, prefInt);
 
528
  }
 
529
  if (NS_SUCCEEDED(mPrefs->GetIntPref("bidi.support", &prefInt))) {
 
530
     SET_BIDI_OPTION_SUPPORT(mBidi, prefInt);
 
531
  }
 
532
  if (NS_SUCCEEDED(mPrefs->GetIntPref("bidi.characterset", &prefInt))) {
 
533
     SET_BIDI_OPTION_CHARACTERSET(mBidi, prefInt);
 
534
  }
 
535
#endif
 
536
}
 
537
 
 
538
NS_IMETHODIMP
 
539
nsPresContext::GetCachedIntPref(PRUint32 aPrefType, PRInt32& aValue) 
 
540
{
 
541
  nsresult rv = NS_OK;
 
542
  switch (aPrefType) {
 
543
    case kPresContext_MinimumFontSize:
 
544
      aValue = mMinimumFontSize;
 
545
      break;
 
546
    default:
 
547
      rv = NS_ERROR_INVALID_ARG;
 
548
      NS_ERROR("invalid arg");
 
549
  }
 
550
  return rv;
 
551
}
 
552
 
 
553
void
 
554
nsPresContext::ClearStyleDataAndReflow()
 
555
{
 
556
  if (mShell) {
 
557
    // Clear out all our style data.
 
558
    mShell->StyleSet()->ClearStyleData(this);
 
559
 
 
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();
 
565
  }
 
566
}
 
567
 
 
568
void
 
569
nsPresContext::PreferenceChanged(const char* aPrefName)
 
570
{
 
571
  nsCOMPtr<nsIDocShellTreeItem> docShell(do_QueryReferent(mContainer));
 
572
  if (docShell) {
 
573
    PRInt32 docShellType;
 
574
    docShell->GetItemType(&docShellType);
 
575
    if (nsIDocShellTreeItem::typeChrome == docShellType)
 
576
      return;
 
577
  }
 
578
 
 
579
  // Initialize our state from the user preferences
 
580
  GetUserPreferences();
 
581
 
 
582
  // update the presShell: tell it to set the preference style rules up
 
583
  if (mShell) {
 
584
    mShell->SetPreferenceStyleRules(PR_TRUE);
 
585
  }
 
586
 
 
587
  mDeviceContext->FlushFontCache();
 
588
  nsPresContext::ClearStyleDataAndReflow();
 
589
}
 
590
 
 
591
NS_IMETHODIMP
 
592
nsPresContext::Init(nsIDeviceContext* aDeviceContext)
 
593
{
 
594
  NS_ASSERTION(!(mInitialized == PR_TRUE), "attempt to reinit pres context");
 
595
  NS_ENSURE_ARG(aDeviceContext);
 
596
 
 
597
  mDeviceContext = aDeviceContext;
 
598
  NS_ADDREF(mDeviceContext);
 
599
 
 
600
  // Get the look and feel service here; default colors will be initialized
 
601
  // from calling GetUserPreferences() below.
 
602
  nsresult rv = CallGetService(kLookAndFeelCID, &mLookAndFeel);
 
603
  if (NS_FAILED(rv)) {
 
604
    NS_ERROR("LookAndFeel service must be implemented for this toolkit");
 
605
    return rv;
 
606
  }
 
607
 
 
608
  mLangService = do_GetService(NS_LANGUAGEATOMSERVICE_CONTRACTID);
 
609
  mPrefs = do_GetService(NS_PREF_CONTRACTID);
 
610
  if (mPrefs) {
 
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);
 
620
#ifdef IBMBIDI
 
621
    mPrefs->RegisterCallback("bidi.", PrefChangedCallback, (void*)this);
 
622
#endif
 
623
 
 
624
    // Initialize our state from the user preferences
 
625
    GetUserPreferences();
 
626
  }
 
627
 
 
628
  mEventManager = new nsEventStateManager();
 
629
  if (!mEventManager)
 
630
    return NS_ERROR_OUT_OF_MEMORY;
 
631
 
 
632
  NS_ADDREF(mEventManager);
 
633
 
 
634
  rv = mEventManager->Init();
 
635
  NS_ENSURE_SUCCESS(rv, rv);
 
636
 
 
637
  mEventManager->SetPresContext(this);
 
638
 
 
639
#ifdef DEBUG
 
640
  mInitialized = PR_TRUE;
 
641
#endif
 
642
 
 
643
  return NS_OK;
 
644
}
 
645
 
 
646
// Note: We don't hold a reference on the shell; it has a reference to
 
647
// us
 
648
NS_IMETHODIMP
 
649
nsPresContext::SetShell(nsIPresShell* aShell)
 
650
{
 
651
  if (mShell) {
 
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));
 
656
    if (doc) {
 
657
      doc->RemoveCharSetObserver(this);
 
658
    }
 
659
  }    
 
660
 
 
661
  mShell = aShell;
 
662
 
 
663
  if (mShell) {
 
664
    nsCOMPtr<nsIDocument> doc;
 
665
    if (NS_SUCCEEDED(mShell->GetDocument(getter_AddRefs(doc)))) {
 
666
      NS_ASSERTION(doc, "expect document here");
 
667
      if (doc) {
 
668
        nsIURI *baseURI = doc->GetBaseURI();
 
669
 
 
670
        if (!mNeverAnimate && baseURI) {
 
671
            PRBool isChrome = PR_FALSE;
 
672
            PRBool isRes = PR_FALSE;
 
673
            baseURI->SchemeIs("chrome", &isChrome);
 
674
            baseURI->SchemeIs("resource", &isRes);
 
675
 
 
676
          if (!isChrome && !isRes)
 
677
            mImageAnimationMode = mImageAnimationModePref;
 
678
          else
 
679
            mImageAnimationMode = imgIContainer::kNormalAnimMode;
 
680
        }
 
681
 
 
682
        if (mLangService) {
 
683
          doc->AddCharSetObserver(this);
 
684
          UpdateCharSet(doc->GetDocumentCharacterSet().get());
 
685
        }
 
686
      }
 
687
    }
 
688
  }
 
689
  return NS_OK;
 
690
}
 
691
 
 
692
void
 
693
nsPresContext::UpdateCharSet(const char* aCharSet)
 
694
{
 
695
  if (mLangService) {
 
696
    NS_IF_RELEASE(mLanguage);
 
697
    mLangService->LookupCharSet(aCharSet, &mLanguage);  // addrefs
 
698
    if (mLanguage) {
 
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;
 
708
      }
 
709
      else {
 
710
        mLanguageSpecificTransformType =
 
711
        eLanguageSpecificTransformType_None;
 
712
      }
 
713
    }
 
714
    GetFontPreferences();
 
715
  }
 
716
#ifdef IBMBIDI
 
717
  //ahmed
 
718
  mCharset=aCharSet;
 
719
 
 
720
  SetVisualMode(IsVisualCharset(mCharset) );
 
721
#endif // IBMBIDI
 
722
}
 
723
 
 
724
NS_IMETHODIMP
 
725
nsPresContext::Observe(nsISupports* aSubject, 
 
726
                       const char* aTopic,
 
727
                       const PRUnichar* aData)
 
728
{
 
729
  if (!nsCRT::strcmp(aTopic, "charset")) {
 
730
    UpdateCharSet(NS_LossyConvertUCS2toASCII(aData).get());
 
731
    mDeviceContext->FlushFontCache();
 
732
    nsPresContext::ClearStyleDataAndReflow();
 
733
 
 
734
    return NS_OK;
 
735
  }
 
736
 
 
737
  NS_WARNING("unrecognized topic in nsPresContext::Observe");
 
738
  return NS_ERROR_FAILURE;
 
739
}
 
740
 
 
741
void
 
742
nsPresContext::SetCompatibilityMode(nsCompatibility aMode)
 
743
{
 
744
  mCompatibilityMode = aMode;
 
745
 
 
746
  if (!mShell)
 
747
    return;
 
748
 
 
749
  // enable/disable the QuirkSheet
 
750
  mShell->StyleSet()->
 
751
    EnableQuirkStyleSheet(mCompatibilityMode == eCompatibility_NavQuirks);
 
752
}
 
753
 
 
754
// Helper function for setting Anim Mode on image
 
755
static void SetImgAnimModeOnImgReq(imgIRequest* aImgReq, PRUint16 aMode)
 
756
{
 
757
  if (aImgReq) {
 
758
    nsCOMPtr<imgIContainer> imgCon;
 
759
    aImgReq->GetImage(getter_AddRefs(imgCon));
 
760
    if (imgCon) {
 
761
      imgCon->SetAnimationMode(aMode);
 
762
    }
 
763
  }
 
764
}
 
765
 
 
766
 // Enumeration call back for HashTable
 
767
PR_STATIC_CALLBACK(PRBool) set_animation_mode(nsHashKey *aKey, void *aData, void* closure)
 
768
{
 
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));
 
773
  return PR_TRUE;
 
774
}
 
775
 
 
776
// IMPORTANT: Assumption is that all images for a Presentation 
 
777
// have the same Animation Mode (pavlov said this was OK)
 
778
//
 
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)
 
782
{
 
783
  nsCOMPtr<nsIImageLoadingContent> imgContent(do_QueryInterface(aParent));
 
784
  if (imgContent) {
 
785
    nsCOMPtr<imgIRequest> imgReq;
 
786
    imgContent->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST,
 
787
                           getter_AddRefs(imgReq));
 
788
    SetImgAnimModeOnImgReq(imgReq, aMode);
 
789
  }
 
790
  
 
791
  PRUint32 count = aParent->GetChildCount();
 
792
  for (PRUint32 i = 0; i < count; ++i) {
 
793
    SetImgAnimations(aParent->GetChildAt(i), aMode);
 
794
  }
 
795
}
 
796
 
 
797
void
 
798
nsPresContext::SetImageAnimationMode(PRUint16 aMode)
 
799
{
 
800
  NS_ASSERTION(aMode == imgIContainer::kNormalAnimMode ||
 
801
               aMode == imgIContainer::kDontAnimMode ||
 
802
               aMode == imgIContainer::kLoopOnceAnimMode, "Wrong Animation Mode is being set!");
 
803
 
 
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);
 
807
 
 
808
  // Now walk the content tree and set the animation mode 
 
809
  // on all the images
 
810
  nsCOMPtr<nsIDocument> doc;
 
811
  if (mShell != nsnull) {
 
812
    mShell->GetDocument(getter_AddRefs(doc));
 
813
    if (doc) {
 
814
      nsIContent *rootContent = doc->GetRootContent();
 
815
      if (rootContent) {
 
816
        SetImgAnimations(rootContent, aMode);
 
817
      }
 
818
    }
 
819
  }
 
820
 
 
821
  mImageAnimationMode = aMode;
 
822
}
 
823
 
 
824
nsresult
 
825
nsPresContext::GetXBLBindingURL(nsIContent* aContent, nsIURI** aResult)
 
826
{
 
827
  nsRefPtr<nsStyleContext> sc;
 
828
  sc = StyleSet()->ResolveStyleFor(aContent, nsnull);
 
829
  NS_ENSURE_TRUE(sc, NS_ERROR_FAILURE);
 
830
 
 
831
  *aResult = sc->GetStyleDisplay()->mBinding;
 
832
  NS_IF_ADDREF(*aResult);
 
833
  return NS_OK;
 
834
}
 
835
 
 
836
NS_IMETHODIMP
 
837
nsPresContext::AllocateFromShell(size_t aSize, void** aResult)
 
838
{
 
839
  if (mShell)
 
840
    return mShell->AllocateFrame(aSize, aResult);
 
841
  return NS_OK;
 
842
}
 
843
 
 
844
NS_IMETHODIMP
 
845
nsPresContext::FreeToShell(size_t aSize, void* aFreeChunk)
 
846
{
 
847
  if (mShell)
 
848
    return mShell->FreeFrame(aSize, aFreeChunk);
 
849
  return NS_OK;
 
850
}
 
851
 
 
852
NS_IMETHODIMP
 
853
nsPresContext::GetMetricsFor(const nsFont& aFont, nsIFontMetrics** aResult)
 
854
{
 
855
  NS_PRECONDITION(aResult, "null out param");
 
856
 
 
857
  nsIFontMetrics* metrics = nsnull;
 
858
  nsCOMPtr<nsIAtom> langGroup;
 
859
  if (mLanguage) {
 
860
    mLanguage->GetLanguageGroup(getter_AddRefs(langGroup));
 
861
  }
 
862
  mDeviceContext->GetMetricsFor(aFont, langGroup, metrics);
 
863
  *aResult = metrics;
 
864
  return NS_OK;
 
865
}
 
866
 
 
867
const nsFont*
 
868
nsPresContext::GetDefaultFont(PRUint8 aFontID) const
 
869
{
 
870
  const nsFont *font;
 
871
  switch (aFontID) {
 
872
    // Special (our default variable width font and fixed width font)
 
873
    case kPresContext_DefaultVariableFont_ID:
 
874
      font = &mDefaultVariableFont;
 
875
      break;
 
876
    case kPresContext_DefaultFixedFont_ID:
 
877
      font = &mDefaultFixedFont;
 
878
      break;
 
879
    // CSS
 
880
    case kGenericFont_serif:
 
881
      font = &mDefaultSerifFont;
 
882
      break;
 
883
    case kGenericFont_sans_serif:
 
884
      font = &mDefaultSansSerifFont;
 
885
      break;
 
886
    case kGenericFont_monospace:
 
887
      font = &mDefaultMonospaceFont;
 
888
      break;
 
889
    case kGenericFont_cursive:
 
890
      font = &mDefaultCursiveFont;
 
891
      break;
 
892
    case kGenericFont_fantasy: 
 
893
      font = &mDefaultFantasyFont;
 
894
      break;
 
895
    default:
 
896
      font = nsnull;
 
897
      NS_ERROR("invalid arg");
 
898
      break;
 
899
  }
 
900
  return font;
 
901
}
 
902
 
 
903
NS_IMETHODIMP
 
904
nsPresContext::GetTwipsToPixelsForFonts(float* aResult) const
 
905
{
 
906
  NS_PRECONDITION(nsnull != aResult, "null ptr");
 
907
  if (nsnull == aResult) {
 
908
    return NS_ERROR_NULL_POINTER;
 
909
  }
 
910
 
 
911
  float app2dev;
 
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));
 
919
  if (altDC) {
 
920
    app2dev = altDC->AppUnitsToDevUnits();
 
921
  } else {
 
922
    app2dev = mDeviceContext->AppUnitsToDevUnits();
 
923
  }
 
924
#else
 
925
  app2dev = mDeviceContext->AppUnitsToDevUnits();
 
926
#endif
 
927
  *aResult = app2dev;
 
928
  return NS_OK;
 
929
}
 
930
 
 
931
 
 
932
 
 
933
NS_IMETHODIMP
 
934
nsPresContext::GetScaledPixelsToTwips(float* aResult) const
 
935
{
 
936
  NS_PRECONDITION(aResult, "null out param");
 
937
 
 
938
  float scale;
 
939
  float p2t;
 
940
 
 
941
  p2t = mDeviceContext->DevUnitsToAppUnits();
 
942
  mDeviceContext->GetCanonicalPixelScale(scale);
 
943
  scale = p2t * scale;
 
944
 
 
945
  *aResult = scale;
 
946
  return NS_OK;
 
947
}
 
948
 
 
949
nsresult
 
950
nsPresContext::LoadImage(imgIRequest* aImage,
 
951
                         nsIFrame* aTargetFrame,//may be null (precached image)
 
952
                         imgIRequest **aRequest)
 
953
{
 
954
  // look and see if we have a loader for the target frame.
 
955
 
 
956
  nsVoidKey key(aTargetFrame);
 
957
  nsImageLoader *loader = NS_REINTERPRET_CAST(nsImageLoader*, mImageLoaders.Get(&key)); // addrefs
 
958
 
 
959
  if (!loader) {
 
960
    loader = new nsImageLoader();
 
961
    if (!loader)
 
962
      return NS_ERROR_OUT_OF_MEMORY;
 
963
 
 
964
    NS_ADDREF(loader); // new
 
965
 
 
966
    loader->Init(aTargetFrame, this);
 
967
    mImageLoaders.Put(&key, loader);
 
968
  }
 
969
 
 
970
  loader->Load(aImage);
 
971
 
 
972
  NS_IF_ADDREF(*aRequest = loader->GetRequest());
 
973
 
 
974
  NS_RELEASE(loader);
 
975
 
 
976
  return NS_OK;
 
977
}
 
978
 
 
979
 
 
980
void
 
981
nsPresContext::StopImagesFor(nsIFrame* aTargetFrame)
 
982
{
 
983
  nsVoidKey key(aTargetFrame);
 
984
  nsImageLoader *loader = NS_REINTERPRET_CAST(nsImageLoader*, mImageLoaders.Get(&key)); // addrefs
 
985
 
 
986
  if (loader) {
 
987
    loader->Destroy();
 
988
    NS_RELEASE(loader);
 
989
 
 
990
    mImageLoaders.Remove(&key);
 
991
  }
 
992
}
 
993
 
 
994
 
 
995
void
 
996
nsPresContext::SetContainer(nsISupports* aHandler)
 
997
{
 
998
  mContainer = do_GetWeakReference(aHandler);
 
999
  if (mContainer) {
 
1000
    GetDocumentColorPreferences();
 
1001
  }
 
1002
}
 
1003
 
 
1004
already_AddRefed<nsISupports>
 
1005
nsPresContext::GetContainer()
 
1006
{
 
1007
  nsISupports *result;
 
1008
  if (mContainer)
 
1009
    CallQueryReferent(mContainer.get(), &result);
 
1010
  else
 
1011
    result = nsnull;
 
1012
 
 
1013
  return result;
 
1014
}
 
1015
 
 
1016
#ifdef IBMBIDI
 
1017
NS_IMETHODIMP
 
1018
nsPresContext::GetBidiEnabled(PRBool* aBidiEnabled) const
 
1019
{
 
1020
  NS_ENSURE_ARG_POINTER(aBidiEnabled);
 
1021
  *aBidiEnabled = PR_FALSE;
 
1022
  NS_ASSERTION(mShell, "PresShell must be set on PresContext before calling nsPresContext::GetBidiEnabled");
 
1023
  if (mShell) {
 
1024
    nsCOMPtr<nsIDocument> doc;
 
1025
    mShell->GetDocument(getter_AddRefs(doc) );
 
1026
    NS_ASSERTION(doc, "PresShell has no document in nsPresContext::GetBidiEnabled");
 
1027
    if (doc) {
 
1028
      *aBidiEnabled = doc->GetBidiEnabled();
 
1029
    }
 
1030
  }
 
1031
  return NS_OK;
 
1032
}
 
1033
 
 
1034
NS_IMETHODIMP
 
1035
nsPresContext::SetBidiEnabled(PRBool aBidiEnabled) const
 
1036
{
 
1037
  if (mShell) {
 
1038
    nsCOMPtr<nsIDocument> doc;
 
1039
    mShell->GetDocument(getter_AddRefs(doc) );
 
1040
    if (doc) {
 
1041
      doc->SetBidiEnabled(aBidiEnabled);
 
1042
    }
 
1043
  }
 
1044
  return NS_OK;
 
1045
}
 
1046
 
 
1047
NS_IMETHODIMP
 
1048
nsPresContext::GetBidiUtils(nsBidiPresUtils** aBidiUtils)
 
1049
{
 
1050
  nsresult rv = NS_OK;
 
1051
 
 
1052
  if (!mBidiUtils) {
 
1053
    mBidiUtils = new nsBidiPresUtils;
 
1054
    if (!mBidiUtils) {
 
1055
      rv = NS_ERROR_OUT_OF_MEMORY;
 
1056
    }
 
1057
  }
 
1058
  *aBidiUtils = mBidiUtils;
 
1059
  return rv;
 
1060
}
 
1061
 
 
1062
NS_IMETHODIMP   nsPresContext::SetBidi(PRUint32 aSource, PRBool aForceReflow)
 
1063
{
 
1064
  mBidi = aSource;
 
1065
  if (IBMBIDI_TEXTDIRECTION_RTL == GET_BIDI_OPTION_DIRECTION(mBidi)
 
1066
      || IBMBIDI_NUMERAL_HINDI == GET_BIDI_OPTION_NUMERAL(mBidi)) {
 
1067
    SetBidiEnabled(PR_TRUE);
 
1068
  }
 
1069
  if (IBMBIDI_TEXTTYPE_VISUAL == GET_BIDI_OPTION_TEXTTYPE(mBidi)) {
 
1070
    SetVisualMode(PR_TRUE);
 
1071
  }
 
1072
  else if (IBMBIDI_TEXTTYPE_LOGICAL == GET_BIDI_OPTION_TEXTTYPE(mBidi)) {
 
1073
    SetVisualMode(PR_FALSE);
 
1074
  }
 
1075
  else {
 
1076
    SetVisualMode(IsVisualCharset(mCharset) );
 
1077
  }
 
1078
  if (mShell && aForceReflow) {
 
1079
    nsPresContext::ClearStyleDataAndReflow();
 
1080
  }
 
1081
  return NS_OK;
 
1082
}
 
1083
NS_IMETHODIMP   nsPresContext::GetBidi(PRUint32* aDest) const
 
1084
{
 
1085
  if (aDest)
 
1086
    *aDest = mBidi;
 
1087
  return NS_OK;
 
1088
}
 
1089
#endif //IBMBIDI
 
1090
 
 
1091
NS_IMETHODIMP
 
1092
nsPresContext::GetLanguageSpecificTransformType(
 
1093
                nsLanguageSpecificTransformType* aType)
 
1094
{
 
1095
  NS_PRECONDITION(aType, "null out param");
 
1096
  *aType = mLanguageSpecificTransformType;
 
1097
 
 
1098
  return NS_OK;
 
1099
}
 
1100
 
 
1101
NS_IMETHODIMP
 
1102
nsPresContext::GetTheme(nsITheme** aResult)
 
1103
{
 
1104
  if (!mNoTheme && !mTheme) {
 
1105
    mTheme = do_GetService("@mozilla.org/chrome/chrome-native-theme;1");
 
1106
    if (!mTheme)
 
1107
      mNoTheme = PR_TRUE;
 
1108
  }
 
1109
 
 
1110
  *aResult = mTheme;
 
1111
  NS_IF_ADDREF(*aResult);
 
1112
  return mTheme ? NS_OK : NS_ERROR_FAILURE;
 
1113
}
 
1114
 
 
1115
NS_IMETHODIMP
 
1116
nsPresContext::ThemeChanged()
 
1117
{
 
1118
  // Tell the theme that it changed, so it can flush any handles to stale theme
 
1119
  // data.
 
1120
  if (mTheme)
 
1121
    mTheme->ThemeChanged();
 
1122
 
 
1123
  // Clear all cached nsILookAndFeel colors.
 
1124
  if (mLookAndFeel)
 
1125
    mLookAndFeel->LookAndFeelChanged();
 
1126
  
 
1127
  if (!mShell)
 
1128
    return NS_OK;
 
1129
 
 
1130
  return mShell->ReconstructStyleData();
 
1131
}
 
1132
 
 
1133
NS_IMETHODIMP
 
1134
nsPresContext::SysColorChanged()
 
1135
{
 
1136
  if (mLookAndFeel) {
 
1137
     // Don't use the cached values for the system colors
 
1138
    mLookAndFeel->LookAndFeelChanged();
 
1139
  }
 
1140
   
 
1141
  // Reset default background and foreground colors for the document since
 
1142
  // they may be using system colors
 
1143
  GetDocumentColorPreferences();
 
1144
 
 
1145
  // Clear out all of the style data since it may contain RGB values
 
1146
  // which originated from system colors.
 
1147
  nsCOMPtr<nsISelectionImageService> imageService;
 
1148
  nsresult result;
 
1149
  imageService = do_GetService(kSelectionImageService, &result);
 
1150
  if (NS_SUCCEEDED(result) && imageService)
 
1151
  {
 
1152
    imageService->Reset();
 
1153
  }
 
1154
 
 
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();
 
1160
  return NS_OK;
 
1161
}
 
1162
 
 
1163
#ifdef MOZ_REFLOW_PERF
 
1164
NS_IMETHODIMP
 
1165
nsPresContext::CountReflows(const char * aName, PRUint32 aType, nsIFrame * aFrame)
 
1166
{
 
1167
  if (mShell) {
 
1168
    mShell->CountReflows(aName, aType, aFrame);
 
1169
  }
 
1170
  return NS_OK;
 
1171
}
 
1172
 
 
1173
NS_IMETHODIMP
 
1174
nsPresContext::PaintCount(const char * aName, nsIRenderingContext* aRenderingContext, nsIFrame * aFrame, PRUint32 aColor)
 
1175
{
 
1176
  if (mShell) {
 
1177
    mShell->PaintCount(aName, aRenderingContext, this, aFrame, aColor);
 
1178
  }
 
1179
  return NS_OK;
 
1180
}
 
1181
#endif