This repository has been archived by the owner on Apr 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 305
/
nsChildView.mm
5217 lines (4286 loc) · 161 KB
/
nsChildView.mm
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: objc; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/Util.h"
#ifdef MOZ_LOGGING
#define FORCE_PR_LOG
#endif
#include "prlog.h"
#include <unistd.h>
#include <math.h>
#include "nsChildView.h"
#include "nsCocoaWindow.h"
#include "nsObjCExceptions.h"
#include "nsCOMPtr.h"
#include "nsToolkit.h"
#include "nsCRT.h"
#include "nsFontMetrics.h"
#include "nsIRollupListener.h"
#include "nsIViewManager.h"
#include "nsIInterfaceRequestor.h"
#include "nsIFile.h"
#include "nsILocalFileMac.h"
#include "nsGfxCIID.h"
#include "nsIDOMSimpleGestureEvent.h"
#include "nsNPAPIPluginInstance.h"
#include "nsThemeConstants.h"
#include "nsDragService.h"
#include "nsClipboard.h"
#include "nsCursorManager.h"
#include "nsWindowMap.h"
#include "nsCocoaFeatures.h"
#include "nsCocoaUtils.h"
#include "nsMenuUtilsX.h"
#include "nsMenuBarX.h"
#ifdef __LP64__
#include "ComplexTextInputPanel.h"
#endif
#include "gfxContext.h"
#include "gfxQuartzSurface.h"
#include "nsRegion.h"
#include "Layers.h"
#include "LayerManagerOGL.h"
#include "GLContext.h"
#include "mozilla/layers/CompositorCocoaWidgetHelper.h"
#ifdef ACCESSIBILITY
#include "nsAccessibilityService.h"
#endif
#include "mozilla/Preferences.h"
#include <dlfcn.h>
#include <ApplicationServices/ApplicationServices.h>
#include "sampler.h"
using namespace mozilla;
using namespace mozilla::layers;
using namespace mozilla::gl;
using namespace mozilla::widget;
using namespace mozilla;
#undef DEBUG_UPDATE
#undef INVALIDATE_DEBUGGING // flash areas as they are invalidated
// Don't put more than this many rects in the dirty region, just fluff
// out to the bounding-box if there are more
#define MAX_RECTS_IN_REGION 100
#ifdef PR_LOGGING
PRLogModuleInfo* sCocoaLog = nsnull;
#endif
extern "C" {
CG_EXTERN void CGContextResetCTM(CGContextRef);
CG_EXTERN void CGContextSetCTM(CGContextRef, CGAffineTransform);
CG_EXTERN void CGContextResetClip(CGContextRef);
}
// defined in nsMenuBarX.mm
extern NSMenu* sApplicationMenu; // Application menu shared by all menubars
// these are defined in nsCocoaWindow.mm
extern bool gConsumeRollupEvent;
bool gChildViewMethodsSwizzled = false;
extern nsISupportsArray *gDraggedTransferables;
ChildView* ChildViewMouseTracker::sLastMouseEventView = nil;
NSEvent* ChildViewMouseTracker::sLastMouseMoveEvent = nil;
NSWindow* ChildViewMouseTracker::sWindowUnderMouse = nil;
NSPoint ChildViewMouseTracker::sLastScrollEventScreenLocation = NSZeroPoint;
#ifdef INVALIDATE_DEBUGGING
static void blinkRect(Rect* r);
static void blinkRgn(RgnHandle rgn);
#endif
nsIRollupListener * gRollupListener = nsnull;
nsIWidget * gRollupWidget = nsnull;
bool gUserCancelledDrag = false;
PRUint32 nsChildView::sLastInputEventCount = 0;
@interface ChildView(Private)
// sets up our view, attaching it to its owning gecko view
- (id)initWithFrame:(NSRect)inFrame geckoChild:(nsChildView*)inChild;
- (void)forceRefreshOpenGL;
// set up a gecko mouse event based on a cocoa mouse event
- (void) convertCocoaMouseEvent:(NSEvent*)aMouseEvent toGeckoEvent:(nsInputEvent*)outGeckoEvent;
- (NSMenu*)contextMenu;
- (void)setIsPluginView:(BOOL)aIsPlugin;
- (void)setPluginEventModel:(NPEventModel)eventModel;
- (void)setPluginDrawingModel:(NPDrawingModel)drawingModel;
- (NPDrawingModel)pluginDrawingModel;
- (BOOL)isRectObscuredBySubview:(NSRect)inRect;
- (void)processPendingRedraws;
- (void)drawRect:(NSRect)aRect inContext:(CGContextRef)aContext;
// Called using performSelector:withObject:afterDelay:0 to release
// aWidgetArray (and its contents) the next time through the run loop.
- (void)releaseWidgets:(NSArray*)aWidgetArray;
#if USE_CLICK_HOLD_CONTEXTMENU
// called on a timer two seconds after a mouse down to see if we should display
// a context menu (click-hold)
- (void)clickHoldCallback:(id)inEvent;
#endif
#ifdef ACCESSIBILITY
- (id<mozAccessible>)accessible;
#endif
- (BOOL)inactiveWindowAcceptsMouseEvent:(NSEvent*)aEvent;
@end
#pragma mark -
/* Convenience routines to go from a gecko rect to cocoa NSRects and back
*
* Gecko rects (nsRect) contain an origin (x,y) in a coordinate
* system with (0,0) in the top-left of the screen. Cocoa rects
* (NSRect) contain an origin (x,y) in a coordinate system with
* (0,0) in the bottom-left of the screen. Both nsRect and NSRect
* contain width/height info, with no difference in their use.
* If a Cocoa rect is from a flipped view, there is no need to
* convert coordinate systems.
*/
static inline void
NSRectToGeckoRect(const NSRect & inCocoaRect, nsIntRect & outGeckoRect)
{
outGeckoRect.x = NSToIntRound(inCocoaRect.origin.x);
outGeckoRect.y = NSToIntRound(inCocoaRect.origin.y);
outGeckoRect.width = NSToIntRound(inCocoaRect.origin.x + inCocoaRect.size.width) - outGeckoRect.x;
outGeckoRect.height = NSToIntRound(inCocoaRect.origin.y + inCocoaRect.size.height) - outGeckoRect.y;
}
static inline void
ConvertGeckoRectToMacRect(const nsIntRect& aRect, Rect& outMacRect)
{
outMacRect.left = aRect.x;
outMacRect.top = aRect.y;
outMacRect.right = aRect.x + aRect.width;
outMacRect.bottom = aRect.y + aRect.height;
}
// Flips a screen coordinate from a point in the cocoa coordinate system (bottom-left rect) to a point
// that is a "flipped" cocoa coordinate system (starts in the top-left).
static inline void
FlipCocoaScreenCoordinate(NSPoint &inPoint)
{
inPoint.y = nsCocoaUtils::FlippedScreenY(inPoint.y);
}
void EnsureLogInitialized()
{
#ifdef PR_LOGGING
if (!sCocoaLog) {
sCocoaLog = PR_NewLogModule("nsCocoaWidgets");
}
#endif // PR_LOGGING
}
#pragma mark -
nsChildView::nsChildView() : nsBaseWidget()
, mView(nsnull)
, mParentView(nsnull)
, mParentWidget(nsnull)
, mVisible(false)
, mDrawing(false)
, mPluginDrawing(false)
, mIsDispatchPaint(false)
, mPluginInstanceOwner(nsnull)
{
EnsureLogInitialized();
memset(&mPluginCGContext, 0, sizeof(mPluginCGContext));
#ifndef NP_NO_QUICKDRAW
memset(&mPluginQDPort, 0, sizeof(mPluginQDPort));
#endif
SetBackgroundColor(NS_RGB(255, 255, 255));
SetForegroundColor(NS_RGB(0, 0, 0));
}
nsChildView::~nsChildView()
{
// Notify the children that we're gone. childView->ResetParent() can change
// our list of children while it's being iterated, so the way we iterate the
// list must allow for this.
for (nsIWidget* kid = mLastChild; kid;) {
nsChildView* childView = static_cast<nsChildView*>(kid);
kid = kid->GetPrevSibling();
childView->ResetParent();
}
NS_WARN_IF_FALSE(mOnDestroyCalled, "nsChildView object destroyed without calling Destroy()");
mResizerImage = nsnull;
// An nsChildView object that was in use can be destroyed without Destroy()
// ever being called on it. So we also need to do a quick, safe cleanup
// here (it's too late to just call Destroy(), which can cause crashes).
// It's particularly important to make sure widgetDestroyed is called on our
// mView -- this method NULLs mView's mGeckoChild, and NULL checks on
// mGeckoChild are used throughout the ChildView class to tell if it's safe
// to use a ChildView object.
[mView widgetDestroyed]; // Safe if mView is nil.
mParentWidget = nil;
TearDownView(); // Safe if called twice.
}
NS_IMPL_ISUPPORTS_INHERITED1(nsChildView, nsBaseWidget, nsIPluginWidget)
nsresult nsChildView::Create(nsIWidget *aParent,
nsNativeWidget aNativeParent,
const nsIntRect &aRect,
EVENT_CALLBACK aHandleEventFunction,
nsDeviceContext *aContext,
nsWidgetInitData *aInitData)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
// Because the hidden window is created outside of an event loop,
// we need to provide an autorelease pool to avoid leaking cocoa objects
// (see bug 559075).
nsAutoreleasePool localPool;
// See NSView (MethodSwizzling) below.
if (!gChildViewMethodsSwizzled) {
nsToolkit::SwizzleMethods([NSView class], @selector(mouseDownCanMoveWindow),
@selector(nsChildView_NSView_mouseDownCanMoveWindow));
#ifdef __LP64__
if (nsCocoaFeatures::OnLionOrLater()) {
nsToolkit::SwizzleMethods([NSEvent class], @selector(addLocalMonitorForEventsMatchingMask:handler:),
@selector(nsChildView_NSEvent_addLocalMonitorForEventsMatchingMask:handler:),
true);
nsToolkit::SwizzleMethods([NSEvent class], @selector(removeMonitor:),
@selector(nsChildView_NSEvent_removeMonitor:), true);
}
#endif
#ifndef NP_NO_CARBON
TextInputHandler::SwizzleMethods();
#endif
gChildViewMethodsSwizzled = true;
}
mBounds = aRect;
// Ensure that the toolkit is created.
nsToolkit::GetToolkit();
BaseCreate(aParent, aRect, aHandleEventFunction, aContext, aInitData);
// inherit things from the parent view and create our parallel
// NSView in the Cocoa display system
mParentView = nil;
if (aParent) {
// This is the case when we're the popup content view of a popup window.
SetBackgroundColor(aParent->GetBackgroundColor());
SetForegroundColor(aParent->GetForegroundColor());
// inherit the top-level window. NS_NATIVE_WIDGET is always a NSView
// regardless of if we're asking a window or a view (for compatibility
// with windows).
mParentView = (NSView<mozView>*)aParent->GetNativeData(NS_NATIVE_WIDGET);
mParentWidget = aParent;
} else {
// This is the normal case. When we're the root widget of the view hiararchy,
// aNativeParent will be the contentView of our window, since that's what
// nsCocoaWindow returns when asked for an NS_NATIVE_VIEW.
mParentView = reinterpret_cast<NSView<mozView>*>(aNativeParent);
}
// create our parallel NSView and hook it up to our parent. Recall
// that NS_NATIVE_WIDGET is the NSView.
NSRect r;
nsCocoaUtils::GeckoRectToNSRect(mBounds, r);
mView = [(NSView<mozView>*)CreateCocoaView(r) retain];
if (!mView) return NS_ERROR_FAILURE;
[(ChildView*)mView setIsPluginView:(mWindowType == eWindowType_plugin)];
// If this view was created in a Gecko view hierarchy, the initial state
// is hidden. If the view is attached only to a native NSView but has
// no Gecko parent (as in embedding), the initial state is visible.
if (mParentWidget)
[mView setHidden:YES];
else
mVisible = true;
// Hook it up in the NSView hierarchy.
if (mParentView) {
[mParentView addSubview:mView];
}
// if this is a ChildView, make sure that our per-window data
// is set up
if ([mView isKindOfClass:[ChildView class]])
[[WindowDataMap sharedWindowDataMap] ensureDataForWindow:[mView window]];
NS_ASSERTION(!mTextInputHandler, "mTextInputHandler has already existed");
mTextInputHandler = new TextInputHandler(this, mView);
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
// Creates the appropriate child view. Override to create something other than
// our |ChildView| object. Autoreleases, so caller must retain.
NSView*
nsChildView::CreateCocoaView(NSRect inFrame)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NIL;
return [[[ChildView alloc] initWithFrame:inFrame geckoChild:this] autorelease];
NS_OBJC_END_TRY_ABORT_BLOCK_NIL;
}
void nsChildView::TearDownView()
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
if (!mView)
return;
NSWindow* win = [mView window];
NSResponder* responder = [win firstResponder];
// We're being unhooked from the view hierarchy, don't leave our view
// or a child view as the window first responder.
if (responder && [responder isKindOfClass:[NSView class]] &&
[(NSView*)responder isDescendantOf:mView]) {
[win makeFirstResponder:[mView superview]];
}
// If mView is win's contentView, win (mView's NSWindow) "owns" mView --
// win has retained mView, and will detach it from the view hierarchy and
// release it when necessary (when win is itself destroyed (in a call to
// [win dealloc])). So all we need to do here is call [mView release] (to
// match the call to [mView retain] in nsChildView::StandardCreate()).
// Also calling [mView removeFromSuperviewWithoutNeedingDisplay] causes
// mView to be released again and dealloced, while remaining win's
// contentView. So if we do that here, win will (for a short while) have
// an invalid contentView (for the consequences see bmo bugs 381087 and
// 374260).
if ([mView isEqual:[win contentView]]) {
[mView release];
} else {
// Stop NSView hierarchy being changed during [ChildView drawRect:]
[mView performSelectorOnMainThread:@selector(delayedTearDown) withObject:nil waitUntilDone:false];
}
mView = nil;
NS_OBJC_END_TRY_ABORT_BLOCK;
}
nsCocoaWindow*
nsChildView::GetXULWindowWidget()
{
id windowDelegate = [[mView window] delegate];
if (windowDelegate && [windowDelegate isKindOfClass:[WindowDelegate class]]) {
return [(WindowDelegate *)windowDelegate geckoWidget];
}
return nsnull;
}
NS_IMETHODIMP nsChildView::Destroy()
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
if (mOnDestroyCalled)
return NS_OK;
mOnDestroyCalled = true;
[mView widgetDestroyed];
nsBaseWidget::Destroy();
ReportDestroyEvent();
mParentWidget = nil;
TearDownView();
nsBaseWidget::OnDestroy();
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
#pragma mark -
#if 0
static void PrintViewHierarchy(NSView *view)
{
while (view) {
NSLog(@" view is %x, frame %@", view, NSStringFromRect([view frame]));
view = [view superview];
}
}
#endif
// Return native data according to aDataType
void* nsChildView::GetNativeData(PRUint32 aDataType)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSNULL;
void* retVal = nsnull;
switch (aDataType)
{
case NS_NATIVE_WIDGET:
case NS_NATIVE_DISPLAY:
retVal = (void*)mView;
break;
case NS_NATIVE_WINDOW:
retVal = [mView window];
break;
case NS_NATIVE_GRAPHIC:
NS_ERROR("Requesting NS_NATIVE_GRAPHIC on a Mac OS X child view!");
retVal = nsnull;
break;
case NS_NATIVE_OFFSETX:
retVal = 0;
break;
case NS_NATIVE_OFFSETY:
retVal = 0;
break;
case NS_NATIVE_PLUGIN_PORT:
case NS_NATIVE_PLUGIN_PORT_QD:
case NS_NATIVE_PLUGIN_PORT_CG:
{
// The NP_CGContext pointer should always be NULL in the Cocoa event model.
if ([(ChildView*)mView pluginEventModel] == NPEventModelCocoa)
return nsnull;
UpdatePluginPort();
#ifndef NP_NO_QUICKDRAW
if (aDataType != NS_NATIVE_PLUGIN_PORT_CG) {
retVal = (void*)&mPluginQDPort;
break;
}
#endif
retVal = (void*)&mPluginCGContext;
break;
}
}
return retVal;
NS_OBJC_END_TRY_ABORT_BLOCK_NSNULL;
}
#pragma mark -
nsTransparencyMode nsChildView::GetTransparencyMode()
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_RETURN;
nsCocoaWindow* windowWidget = GetXULWindowWidget();
return windowWidget ? windowWidget->GetTransparencyMode() : eTransparencyOpaque;
NS_OBJC_END_TRY_ABORT_BLOCK_RETURN(eTransparencyOpaque);
}
// This is called by nsContainerFrame on the root widget for all window types
// except popup windows (when nsCocoaWindow::SetTransparencyMode is used instead).
void nsChildView::SetTransparencyMode(nsTransparencyMode aMode)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK;
nsCocoaWindow* windowWidget = GetXULWindowWidget();
if (windowWidget) {
windowWidget->SetTransparencyMode(aMode);
}
NS_OBJC_END_TRY_ABORT_BLOCK;
}
NS_IMETHODIMP nsChildView::IsVisible(bool& outState)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
if (!mVisible) {
outState = mVisible;
}
else {
// mVisible does not accurately reflect the state of a hidden tabbed view
// so verify that the view has a window as well
outState = ([mView window] != nil);
// now check native widget hierarchy visibility
if (outState && NSIsEmptyRect([mView visibleRect])) {
outState = false;
}
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
void nsChildView::HidePlugin()
{
NS_ASSERTION(mWindowType == eWindowType_plugin,
"HidePlugin called on non-plugin view");
#ifndef NP_NO_QUICKDRAW
if (mPluginInstanceOwner && mView &&
[(ChildView*)mView pluginDrawingModel] == NPDrawingModelQuickDraw) {
NPWindow* window;
mPluginInstanceOwner->GetWindow(window);
nsRefPtr<nsNPAPIPluginInstance> instance;
mPluginInstanceOwner->GetInstance(getter_AddRefs(instance));
if (window && instance) {
window->clipRect.top = 0;
window->clipRect.left = 0;
window->clipRect.bottom = 0;
window->clipRect.right = 0;
instance->SetWindow(window);
}
}
#endif
}
void nsChildView::UpdatePluginPort()
{
NS_ASSERTION(mWindowType == eWindowType_plugin,
"UpdatePluginPort called on non-plugin view");
#if !defined(NP_NO_CARBON) || !defined(NP_NO_QUICKDRAW)
NSWindow* cocoaWindow = [mView window];
WindowRef carbonWindow = cocoaWindow ? (WindowRef)[cocoaWindow windowRef] : NULL;
#endif
if (!mView
#ifndef NP_NO_QUICKDRAW
|| [(ChildView*)mView pluginDrawingModel] != NPDrawingModelQuickDraw
#endif
) {
// [NSGraphicsContext currentContext] is supposed to "return the
// current graphics context of the current thread." But sometimes
// (when called while mView isn't focused for drawing) it returns a
// graphics context for the wrong window. [window graphicsContext]
// (which "provides the graphics context associated with the window
// for the current thread") seems always to return the "right"
// graphics context. See bug 500130.
mPluginCGContext.context = NULL;
mPluginCGContext.window = NULL;
#ifndef NP_NO_CARBON
if (carbonWindow) {
mPluginCGContext.context = (CGContextRef)[[cocoaWindow graphicsContext] graphicsPort];
mPluginCGContext.window = carbonWindow;
}
#endif
}
#ifndef NP_NO_QUICKDRAW
else {
if (carbonWindow) {
mPluginQDPort.port = ::GetWindowPort(carbonWindow);
NSPoint viewOrigin = [mView convertPoint:NSZeroPoint toView:nil];
NSRect frame = [[cocoaWindow contentView] frame];
viewOrigin.y = frame.size.height - viewOrigin.y;
// need to convert view's origin to window coordinates.
// then, encode as "SetOrigin" ready values.
mPluginQDPort.portx = (PRInt32)-viewOrigin.x;
mPluginQDPort.porty = (PRInt32)-viewOrigin.y;
} else {
mPluginQDPort.port = NULL;
}
}
#endif
}
static void HideChildPluginViews(NSView* aView)
{
NSArray* subviews = [aView subviews];
for (unsigned int i = 0; i < [subviews count]; ++i) {
NSView* view = [subviews objectAtIndex: i];
if (![view isKindOfClass:[ChildView class]])
continue;
ChildView* childview = static_cast<ChildView*>(view);
if ([childview isPluginView]) {
nsChildView* widget = static_cast<nsChildView*>([childview widget]);
if (widget) {
widget->HidePlugin();
}
} else {
HideChildPluginViews(view);
}
}
}
// Hide or show this component
NS_IMETHODIMP nsChildView::Show(bool aState)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
if (aState != mVisible) {
// Provide an autorelease pool because this gets called during startup
// on the "hidden window", resulting in cocoa object leakage if there's
// no pool in place.
nsAutoreleasePool localPool;
[mView setHidden:!aState];
mVisible = aState;
if (!mVisible && IsPluginView())
HidePlugin();
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
// Change the parent of this widget
NS_IMETHODIMP
nsChildView::SetParent(nsIWidget* aNewParent)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
if (mOnDestroyCalled)
return NS_OK;
nsCOMPtr<nsIWidget> kungFuDeathGrip(this);
if (mParentWidget) {
mParentWidget->RemoveChild(this);
}
if (aNewParent) {
ReparentNativeWidget(aNewParent);
} else {
[mView removeFromSuperview];
mParentView = nil;
}
mParentWidget = aNewParent;
if (mParentWidget) {
mParentWidget->AddChild(this);
}
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP
nsChildView::ReparentNativeWidget(nsIWidget* aNewParent)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
NS_PRECONDITION(aNewParent, "");
if (mOnDestroyCalled)
return NS_OK;
NSView<mozView>* newParentView =
(NSView<mozView>*)aNewParent->GetNativeData(NS_NATIVE_WIDGET);
NS_ENSURE_TRUE(newParentView, NS_ERROR_FAILURE);
// we hold a ref to mView, so this is safe
[mView removeFromSuperview];
mParentView = newParentView;
[mParentView addSubview:mView];
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
void nsChildView::ResetParent()
{
if (!mOnDestroyCalled) {
if (mParentWidget)
mParentWidget->RemoveChild(this);
if (mView)
[mView removeFromSuperview];
}
mParentWidget = nsnull;
}
nsIWidget*
nsChildView::GetParent()
{
return mParentWidget;
}
float
nsChildView::GetDPI()
{
NSWindow* window = [mView window];
if (window && [window isKindOfClass:[BaseWindow class]]) {
return [(BaseWindow*)window getDPI];
}
return 96.0;
}
NS_IMETHODIMP nsChildView::Enable(bool aState)
{
return NS_OK;
}
NS_IMETHODIMP nsChildView::IsEnabled(bool *aState)
{
// unimplemented
if (aState)
*aState = true;
return NS_OK;
}
NS_IMETHODIMP nsChildView::SetFocus(bool aRaise)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
NSWindow* window = [mView window];
if (window)
[window makeFirstResponder:mView];
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
// Override to set the cursor on the mac
NS_IMETHODIMP nsChildView::SetCursor(nsCursor aCursor)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
if ([mView isDragInProgress])
return NS_OK; // Don't change the cursor during dragging.
nsBaseWidget::SetCursor(aCursor);
return [[nsCursorManager sharedInstance] setCursor:aCursor];
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
// implement to fix "hidden virtual function" warning
NS_IMETHODIMP nsChildView::SetCursor(imgIContainer* aCursor,
PRUint32 aHotspotX, PRUint32 aHotspotY)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
nsBaseWidget::SetCursor(aCursor, aHotspotX, aHotspotY);
return [[nsCursorManager sharedInstance] setCursorWithImage:aCursor hotSpotX:aHotspotX hotSpotY:aHotspotY];
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
#pragma mark -
// Get this component dimension
NS_IMETHODIMP nsChildView::GetBounds(nsIntRect &aRect)
{
if (!mView) {
aRect = mBounds;
} else {
NSRect frame = [mView frame];
NSRectToGeckoRect(frame, aRect);
}
return NS_OK;
}
NS_IMETHODIMP nsChildView::ConstrainPosition(bool aAllowSlop,
PRInt32 *aX, PRInt32 *aY)
{
return NS_OK;
}
// Move this component, aX and aY are in the parent widget coordinate system
NS_IMETHODIMP nsChildView::Move(PRInt32 aX, PRInt32 aY)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
if (!mView || (mBounds.x == aX && mBounds.y == aY))
return NS_OK;
mBounds.x = aX;
mBounds.y = aY;
NSRect r;
nsCocoaUtils::GeckoRectToNSRect(mBounds, r);
[mView setFrame:r];
if (mVisible)
[mView setNeedsDisplay:YES];
NotifyRollupGeometryChange(gRollupListener);
ReportMoveEvent();
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP nsChildView::Resize(PRInt32 aWidth, PRInt32 aHeight, bool aRepaint)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
if (!mView || (mBounds.width == aWidth && mBounds.height == aHeight))
return NS_OK;
mBounds.width = aWidth;
mBounds.height = aHeight;
NSRect r;
nsCocoaUtils::GeckoRectToNSRect(mBounds, r);
[mView setFrame:r];
if (mVisible && aRepaint)
[mView setNeedsDisplay:YES];
NotifyRollupGeometryChange(gRollupListener);
ReportSizeEvent();
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
NS_IMETHODIMP nsChildView::Resize(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight, bool aRepaint)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
BOOL isMoving = (mBounds.x != aX || mBounds.y != aY);
BOOL isResizing = (mBounds.width != aWidth || mBounds.height != aHeight);
if (!mView || (!isMoving && !isResizing))
return NS_OK;
if (isMoving) {
mBounds.x = aX;
mBounds.y = aY;
}
if (isResizing) {
mBounds.width = aWidth;
mBounds.height = aHeight;
}
NSRect r;
nsCocoaUtils::GeckoRectToNSRect(mBounds, r);
[mView setFrame:r];
if (mVisible && aRepaint)
[mView setNeedsDisplay:YES];
NotifyRollupGeometryChange(gRollupListener);
if (isMoving) {
ReportMoveEvent();
if (mOnDestroyCalled)
return NS_OK;
}
if (isResizing)
ReportSizeEvent();
return NS_OK;
NS_OBJC_END_TRY_ABORT_BLOCK_NSRESULT;
}
static const PRInt32 resizeIndicatorWidth = 15;
static const PRInt32 resizeIndicatorHeight = 15;
bool nsChildView::ShowsResizeIndicator(nsIntRect* aResizerRect)
{
NSView *topLevelView = mView, *superView = nil;
while ((superView = [topLevelView superview]))
topLevelView = superView;
if (![[topLevelView window] showsResizeIndicator] ||
!([[topLevelView window] styleMask] & NSResizableWindowMask))
return false;
if (aResizerRect) {
NSSize bounds = [topLevelView bounds].size;
NSPoint corner = NSMakePoint(bounds.width, [topLevelView isFlipped] ? bounds.height : 0);
corner = [topLevelView convertPoint:corner toView:mView];
aResizerRect->SetRect(NSToIntRound(corner.x) - resizeIndicatorWidth,
NSToIntRound(corner.y) - resizeIndicatorHeight,
resizeIndicatorWidth, resizeIndicatorHeight);
}
return true;
}
// In QuickDraw mode the coordinate system used here should be that of the
// browser window's content region (defined as everything but the 22-pixel
// high titlebar). But in CoreGraphics mode the coordinate system should be
// that of the browser window as a whole (including its titlebar). Both
// coordinate systems have a top-left origin. See bmo bug 474491.
//
// There's a bug in this method's code -- it currently uses the QuickDraw
// coordinate system for both the QuickDraw and CoreGraphics drawing modes.
// This bug is fixed by the patch for bug 474491. But the Flash plugin (both
// version 10.0.12.36 from Adobe and version 9.0 r151 from Apple) has Mozilla-
// specific code to work around this bug, which breaks when we fix it (see bmo
// bug 477077). So we'll need to coordinate releasing a fix for this bug with
// Adobe and other major plugin vendors that support the CoreGraphics mode.
NS_IMETHODIMP nsChildView::GetPluginClipRect(nsIntRect& outClipRect, nsIntPoint& outOrigin, bool& outWidgetVisible)
{
NS_OBJC_BEGIN_TRY_ABORT_BLOCK_NSRESULT;
NS_ASSERTION(mWindowType == eWindowType_plugin,
"GetPluginClipRect must only be called on a plugin widget");
if (mWindowType != eWindowType_plugin) return NS_ERROR_FAILURE;
NSWindow* window = [mView window];
if (!window) return NS_ERROR_FAILURE;
NSPoint viewOrigin = [mView convertPoint:NSZeroPoint toView:nil];
NSRect frame = [[window contentView] frame];
viewOrigin.y = frame.size.height - viewOrigin.y;
// set up the clipping region for plugins.
NSRect visibleBounds = [mView visibleRect];
NSPoint clipOrigin = [mView convertPoint:visibleBounds.origin toView:nil];
// Convert from cocoa to QuickDraw coordinates
clipOrigin.y = frame.size.height - clipOrigin.y;
outClipRect.x = NSToIntRound(clipOrigin.x);
outClipRect.y = NSToIntRound(clipOrigin.y);
// need to convert view's origin to window coordinates.
// then, encode as "SetOrigin" ready values.
outOrigin.x = -NSToIntRound(viewOrigin.x);
outOrigin.y = -NSToIntRound(viewOrigin.y);
bool isVisible;
IsVisible(isVisible);
if (isVisible && [mView window] != nil) {
outClipRect.width = NSToIntRound(visibleBounds.origin.x + visibleBounds.size.width) - NSToIntRound(visibleBounds.origin.x);
outClipRect.height = NSToIntRound(visibleBounds.origin.y + visibleBounds.size.height) - NSToIntRound(visibleBounds.origin.y);
if (mClipRects) {
nsIntRect clipBounds;
for (PRUint32 i = 0; i < mClipRectCount; ++i) {
clipBounds.UnionRect(clipBounds, mClipRects[i]);
}
outClipRect.IntersectRect(outClipRect, clipBounds - outOrigin);
}
// XXXroc should this be !outClipRect.IsEmpty()?
outWidgetVisible = true;
}
else {
outClipRect.width = 0;
outClipRect.height = 0;
outWidgetVisible = false;
}