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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
|
/*
* Copyright (C) 2005 Apple Computer, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// I don't think this class belongs in WebKit. Lets move it out.
// Things that I've never bothered working out:
// For non-sheet windows, handle Carbon WindowMove events so as to do the same things as -[NSWindow _windowMoved].
// Check to see how this stuff deals with various screen size change scenarious.
// M.P. Warning - 9/17/01
// There are some invariants I'm maintaining for objects of this class which have been successfully initialized but not deallocated. These all make it easier to not override every single method of NSWindow.
// _auxiliaryStorage->auxWFlags.hasShadow will always be false if the Carbon window has a kWindowNoShadowAttribute, and vice versa.
// _auxiliaryStorage->_auxWFlags.minimized will always reflect the window's Carbon collapsed state.
// _borderView will always point to an NSCarbonWindowFrame.
// _contentView will always point to an NSCarbonWindowContentView;
// _frame will always reflect the window's Carbon kWindowStructureRgn bounds.
// _styleMask will always have _NSCarbonWindowMask set, and will have NSClosableWindowMask, NSMiniaturizableWindowMask, NSResizableWindowMask, and/or NSTitledWindowMask set as appropriate.
// _wflags.oneShot and _wflags.delayedOneShot will always be false.
// _wFlags.visible will always reflect the window's Carbon visibility.
// _windowNum will always be greater than zero, and valid.
// The instance variables involved are ones that came to my attention during the initial writing of this class; I haven't methodically gone through NSWindow's ivar list or anything like that. M.P. Notice - 10/10/00
// Things that have to be worked on if NSCarbonWindows are ever used for something other than dialogs and sheets:
// Clicking on an NSCarbonWindow while a Cocoa app-modal dialog is shown does not beep, as it should [old bug, maybe fixed now].
// Handling of mouse clicks or key presses for any window control (close, miniaturize, zoom) might not be all there.
// Handling of miniaturization of Carbon windows via title bar double-click might not be all there.
// The background on NSCarbonWindowTester's sample window (not sample dialog or sample sheet) might be wrong.
// The controls on NSCarbonWindowTester's sample window look inactive when the window is inactive, but have first-click behavior.
// M.P. Warning - 12/14/00
// Some things would have to be made public if someone wanted to subclass this so as to support more menu item commands. M.P. Warning - 9/19/00
#ifndef __LP64__
#import "CarbonWindowAdapter.h"
#import "CarbonWindowFrame.h"
#import "CarbonWindowContentView.h"
#import "HIViewAdapter.h"
#import <WebKitSystemInterface.h>
#import <AppKit/AppKit.h>
//#import <CoreGraphics/CGSWindow.h>
#import <HIToolbox/CarbonEvents.h>
#import <HIToolbox/Controls.h>
#import <HIToolbox/HIView.h>
#import <assert.h>
#import <WebCore/WebCoreObjCExtras.h>
#import <WebCore/RunLoop.h>
#import <runtime/InitializeThreading.h>
#import <wtf/MainThread.h>
#import "WebKitLogging.h"
#import "WebNSObjectExtras.h"
#import "WebTypesInternal.h"
@interface NSWindow(HIWebFrameView)
- (id)_initContent:(const NSRect *)contentRect styleMask:(unsigned int)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag contentView:aView;
- (void)_oldPlaceWindow:(NSRect)frameRect;
- (void)_windowMovedToRect:(NSRect)actualFrame;
- (void)_setWindowNumber:(NSInteger)nativeWindow;
- (NSGraphicsContext *)_threadContext;
- (void)_setFrame:(NSRect)newWindowFrameRect;
- (void)_setVisible:(BOOL)flag;
@end
@interface NSApplication(HIWebFrameView)
- (void)setIsActive:(BOOL)aFlag;
- (id)_setMouseActivationInProgress:(BOOL)flag;
- (BOOL)_handleKeyEquivalent:(NSEvent*)theEvent;
@end
@interface NSInputContext
- (BOOL)processInputKeyBindings:(NSEvent *)event;
@end
// Forward declarations.
static OSStatus NSCarbonWindowHandleEvent(EventHandlerCallRef inEventHandlerCallRef, EventRef inEventRef, void *inUserData);
@implementation CarbonWindowAdapter
// Return an appropriate window frame class.
+ (Class)frameViewClassForStyleMask:(unsigned int)style {
// There's only one appropriate window style, and only one appropriate window frame class.
assert(style & WKCarbonWindowMask());
return [CarbonWindowFrame class];
}
// Overriding of the parent class' designated initializer, just for safety's sake.
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)style backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag {
// Do the standard Cocoa thing.
self = [super initWithContentRect:contentRect styleMask:style backing:bufferingType defer:flag];
if (self==nil) return nil;
// Simple.
_windowRef = NULL;
_windowRefIsOwned = NO;
_eventHandler = NULL;
// Done.
return self;
}
// Given a reference to a Carbon window that is to be encapsulated, an indicator of whether or not this object should take responsibility for disposing of the Carbon window, and an indicator of whether to disable Carbon window ordering, initialize. This is the class' designated initializer.
- (id)initWithCarbonWindowRef:(WindowRef)inWindowRef takingOwnership:(BOOL)inWindowRefIsOwned disableOrdering:(BOOL)inDisableOrdering carbon:(BOOL)inCarbon {
NSBackingStoreType backingStoreType;
CarbonWindowContentView *carbonWindowContentView;
NSWindow *windowAsProperty;
OSStatus osStatus;
UInt32 windowFeatures;
WindowAttributes windowAttributes;
unsigned int styleMask;
void *nativeWindow;
WindowModality windowModality;
ControlRef contentView;
// Simple.
// It's very weak to have to put this before the invocation of [super initWithContentRect:...], but -setContentView: is invoked from within that initializer. It turns out that the common admonition about not calling virtual functions from within C++ constructors makes sense in Objective-C too. M.P. Notice - 10/10/00
_windowRef = inWindowRef;
//_auxiliaryStorage->_windowRef = inWindowRef;
_windowRefIsOwned = inWindowRefIsOwned;
_carbon = inCarbon;
// Find out the window's CoreGraphics window reference.
nativeWindow = WKGetNativeWindowFromWindowRef(inWindowRef);
// Find out the window's Carbon window attributes.
GetWindowAttributes(inWindowRef, &windowAttributes);
// Find out the window's Carbon window features.
GetWindowFeatures(inWindowRef, &windowFeatures);
// Figure out the window's backing store type.
// At one time, this had code stolen from CreatePlatformWindow in HIToolbox/Windows/Platform/CGSPlatform.c
// But now the non-retained window class is a Carbon secret that's not even in
// WindowsPriv.h; maybe we'll have to revisit this if someone needs to use WebKit
// in a non-retained window.
backingStoreType = NSBackingStoreRetained;
// Figure out the window's style mask.
styleMask = WKCarbonWindowMask();
if (windowAttributes & kWindowCloseBoxAttribute) styleMask |= NSClosableWindowMask;
if (windowAttributes & kWindowResizableAttribute) styleMask |= NSResizableWindowMask;
if (windowFeatures & kWindowCanCollapse) styleMask |= NSMiniaturizableWindowMask;
if (windowFeatures & kWindowHasTitleBar) styleMask |= NSTitledWindowMask;
osStatus = GetWindowModality(_windowRef, &windowModality, NULL);
if (osStatus != noErr) {
NSLog(@"Couldn't get window modality: error=%ld", osStatus);
return nil;
}
// Create one of our special content views.
carbonWindowContentView = [[[CarbonWindowContentView alloc] init] autorelease];
// Do some standard Cocoa initialization. The defer argument's value is YES because we don't want -[NSWindow _commonAwake] to get called. It doesn't appear that any relevant NSWindow code checks _wFlags.deferred, so we should be able to get away with the lie.
self = (CarbonWindowAdapter*)[super _initContent:NULL styleMask:styleMask backing:backingStoreType defer:YES contentView:carbonWindowContentView];
if (!self) return nil;
assert(_contentView);
// Record accurately whether or not this window has a shadow, in case someone asks.
// _auxiliaryStorage->_auxWFlags.hasShadow = (windowAttributes & kWindowNoShadowAttribute) ? NO : YES;
// Record the window number.
[self _setWindowNumber:(NSInteger)nativeWindow];
// Set up from the frame rectangle.
// We didn't even really try to get it right at _initContent:... time, because it's more trouble that it's worth to write a real +[NSCarbonWindow frameRectForContentRect:styleMask:]. M.P. Notice - 10/10/00
[self reconcileToCarbonWindowBounds];
// Install an event handler for the Carbon window events in which we're interested.
const EventTypeSpec kEvents[] = {
{ kEventClassWindow, kEventWindowActivated },
{ kEventClassWindow, kEventWindowDeactivated },
{ kEventClassWindow, kEventWindowBoundsChanged },
{ kEventClassWindow, kEventWindowShown },
{ kEventClassWindow, kEventWindowHidden }
};
const EventTypeSpec kControlBoundsChangedEvent = { kEventClassControl, kEventControlBoundsChanged };
osStatus = InstallEventHandler( GetWindowEventTarget(_windowRef), NSCarbonWindowHandleEvent, GetEventTypeCount( kEvents ), kEvents, (void*)self, &_eventHandler);
if (osStatus!=noErr) {
[self release];
return nil;
}
osStatus = InstallEventHandler( GetControlEventTarget( HIViewGetRoot( _windowRef ) ), NSCarbonWindowHandleEvent, 1, &kControlBoundsChangedEvent, (void*)self, &_eventHandler);
if (osStatus!=noErr) {
[self release];
return nil;
}
HIViewFindByID( HIViewGetRoot( _windowRef ), kHIViewWindowContentID, &contentView );
osStatus = InstallEventHandler( GetControlEventTarget( contentView ), NSCarbonWindowHandleEvent, 1, &kControlBoundsChangedEvent, (void*)self, &_eventHandler);
if (osStatus!=noErr) {
[self release];
return nil;
}
// Put a pointer to this Cocoa NSWindow in a Carbon window property tag.
// Right now, this is just used by NSViewCarbonControl. M.P. Notice - 10/9/00
windowAsProperty = self;
osStatus = SetWindowProperty(_windowRef, WKCarbonWindowPropertyCreator(), WKCarbonWindowPropertyTag(), sizeof(NSWindow *), &windowAsProperty);
if (osStatus!=noErr) {
[self release];
return nil;
}
// Ignore the Carbon window activation/deactivation events that Carbon sends to its windows at app activation/deactivation. We'll send such events when we think it's appropriate.
_passingCarbonWindowActivationEvents = NO;
// Be sure to sync up visibility
[self _setVisible:(BOOL)IsWindowVisible( _windowRef )];
// Done.
return self;
}
- (void)setViewsNeedDisplay:(BOOL)wellDoThey {
// Make sure we can flush anything that needs it.
// We need to sync the context here. I was hoping I didn't need to do this,
// but apparently when scrolling, the AppKit view system draws directly.
// When this occurs, I cannot intercept it to make it draw in my HIView
// context. What ends up happening is that it draws, but nothing ever
// flushes it.
if ( [self windowNumber] != -1 )
{
CGContextRef cgContext = (CGContextRef)[[self _threadContext] graphicsPort];
CGContextSynchronize( cgContext );
}
}
+ (void)initialize
{
JSC::initializeThreading();
WTF::initializeMainThreadToProcessMainThread();
WebCore::RunLoop::initializeMainRunLoop();
WebCoreObjCFinalizeOnMainThread(self);
}
// Given a reference to a Carbon window that is to be encapsulated, and an indicator of whether or not this object should take responsibility for disposing of the Carbon window, initialize.
- (id)initWithCarbonWindowRef:(WindowRef)inWindowRef takingOwnership:(BOOL)inWindowRefIsOwned {
// for now, set disableOrdering to YES because that is what we've been doing and is therefore lower risk. However, I think it would be correct to set it to NO.
return [self initWithCarbonWindowRef:inWindowRef takingOwnership:inWindowRefIsOwned disableOrdering:YES carbon:NO];
}
// Clean up.
- (void)dealloc {
if (WebCoreObjCScheduleDeallocateOnMainThread([CarbonWindowAdapter class], self))
return;
// Clean up, if necessary.
// if we didn't remove the event handler at dealloc time, we would risk getting sent events after the window has been deallocated. See 2702179. M.P. Notice - 6/1/01
if (_eventHandler) RemoveEventHandler(_eventHandler);
// Do the standard Cocoa thing.
[super dealloc];
}
- (void)finalize {
ASSERT_MAIN_THREAD();
if (_eventHandler) RemoveEventHandler(_eventHandler);
[super finalize];
}
- (WindowRef)windowRef {
// Simple.
return _windowRef;
}
// should always be YES, but check in order to avoid initialization or deallocation surprises
- (BOOL)_hasWindowRef {
return (_windowRef != NULL);
}
// an NSCarbonWindow does not manage the windowRef. The windowRef manages the NSCarbonWindow
- (BOOL)_managesWindowRef {
return NO;
}
- (void)_removeWindowRef {
_windowRef = NULL;
if (_eventHandler) RemoveEventHandler(_eventHandler);
_eventHandler = NULL;
}
- (WindowClass)_carbonWindowClass {
WindowClass windowClass = kDocumentWindowClass;
OSStatus osStatus;
if ([self _hasWindowRef]) {
osStatus = GetWindowClass([self windowRef], &windowClass);
if (osStatus != noErr) {
NSLog(@"Couldn't get window class: error=%ld", osStatus);
}
}
return windowClass;
}
// Update this window's frame and content frame rectangles to match the Carbon window's structure bounds and content bounds rectangles. Return yes if the update was really necessary, no otherwise.
- (BOOL)reconcileToCarbonWindowBounds {
OSStatus osStatus;
NSRect newContentFrameRect;
NSRect newWindowFrameRect;
NSRect oldContentFrameRect;
Rect windowContentBoundsRect;
Rect windowStructureBoundsRect;
// Initialize for safe returning.
BOOL reconciliationWasNecessary = NO;
// Precondition check.
assert(_contentView);
// Get the Carbon window's bounds, which are expressed in global screen coordinates, with (0,0) at the top-left of the main screen.
osStatus = GetWindowBounds(_windowRef, kWindowStructureRgn, &windowStructureBoundsRect);
if (osStatus!=noErr) NSLog(@"A Carbon window's structure bounds couldn't be gotten.");
osStatus = GetWindowBounds(_windowRef, kWindowContentRgn, &windowContentBoundsRect);
if (osStatus!=noErr) NSLog(@"A Carbon window's content bounds couldn't be gotten.");
// Set the frame rectangle of the border view and this window from the Carbon window's structure region bounds.
newWindowFrameRect.origin.x = windowStructureBoundsRect.left;
newWindowFrameRect.origin.y = NSMaxY([(NSScreen *)[[NSScreen screens] objectAtIndex:0] frame]) - windowStructureBoundsRect.bottom;
newWindowFrameRect.size.width = windowStructureBoundsRect.right - windowStructureBoundsRect.left;
newWindowFrameRect.size.height = windowStructureBoundsRect.bottom - windowStructureBoundsRect.top;
if (!NSEqualRects(newWindowFrameRect, _frame)) {
[self _setFrame:newWindowFrameRect];
[_borderView setFrameSize:newWindowFrameRect.size];
reconciliationWasNecessary = YES;
}
// Set the content view's frame rect from the Carbon window's content region bounds.
newContentFrameRect.origin.x = windowContentBoundsRect.left - windowStructureBoundsRect.left;
newContentFrameRect.origin.y = windowStructureBoundsRect.bottom - windowContentBoundsRect.bottom;
newContentFrameRect.size.width = windowContentBoundsRect.right - windowContentBoundsRect.left;
newContentFrameRect.size.height = windowContentBoundsRect.bottom - windowContentBoundsRect.top;
oldContentFrameRect = [(NSView *)_contentView frame];
if (!NSEqualRects(newContentFrameRect, oldContentFrameRect)) {
[(NSView *)_contentView setFrame:newContentFrameRect];
reconciliationWasNecessary = YES;
}
// Done.
return reconciliationWasNecessary;
}
// Handle an event just like an NSWindow would.
- (void)sendSuperEvent:(NSEvent *)inEvent {
// Filter out a few events that just result in complaints in the log.
// Ignore some unknown event that gets sent when NSTextViews in printing accessory views are focused. M.P. Notice - 12/7/00
BOOL ignoreEvent = NO;
NSEventType eventType = [inEvent type];
if (eventType==NSSystemDefined) {
short eventSubtype = [inEvent subtype];
if (eventSubtype==7) {
ignoreEvent = YES;
}
} else if (eventType == NSKeyDown) {
// Handle command-space as [NSApp sendEvent:] does.
if ([NSInputContext processInputKeyBindings:inEvent]) {
return;
}
}
// Simple.
if (!ignoreEvent) [super sendEvent:inEvent];
}
- (void)relinquishFocus
{
NSResponder* firstResponder;
// Carbon thinks that a control has the keyboard focus,
// or we wouldn't be being asked to relinquish focus.
firstResponder = [self firstResponder];
if ([firstResponder isKindOfClass:[NSView class]] ){
// Make the window the first responder, so that no view is the key view.
[self makeFirstResponder:self];
}
}
- (BOOL)makeFirstResponder:(NSResponder *)aResponder
{
// Let NSWindow focus the appropriate NSView.
if (![super makeFirstResponder:aResponder])
return NO;
// Now, if the view we're focusing is in a HIWebView, find the
// corresponding HIWebView for the NSView, and tell carbon to
// clear any focused control.
HIViewRef viewRef = 0;
NSResponder *firstResponder = [self firstResponder];
if ([firstResponder isKindOfClass:[NSView class]]) {
NSView *view = (NSView *)firstResponder;
while (view) {
viewRef = [HIViewAdapter getHIViewForNSView:view];
if (viewRef)
break;
view = [view superview];
}
}
HIViewRef focus;
GetKeyboardFocus (_windowRef, &focus);
if (focus != viewRef) {
SetKeyboardFocus (_windowRef, viewRef, kControlIndicatorPart );
}
return YES;
}
// There's no override of _addCursorRect:cursor:forView:, despite the fact that NSWindow's invokes [self windowNumber], because Carbon windows won't have subviews, and therefore won't have cursor rects.
// There's no override of _autoResizeState, despite the fact that NSWindow's operates on _windowNum, because it looks like it might work on Carbon windows as is.
// Disappointingly, -_blockHeartBeat: is not immediately invoked to turn off heartbeating. Heartbeating is turned off by setting the gDefaultButtonPaused global variable, and then this method is invoked later, if that global is set (at heartbeating time I guess). Something has to change if we want to hook this up in Carbon windows. M.P. Warning - 9/17/01
/*
// Do the right thing for a Carbon window.
- (void)_blockHeartBeat:(BOOL)flag {
ControlRef defaultButton;
OSStatus osStatus;
// Do the standard Cocoa thing.
[super _blockHeartBeat:flag];
// If there's a default Carbon button in this Carbon window, make it stop pulsing, the Carbon way.
// This is inspired by HIToolbox/Controls/Definitions/ButtonCDEF.c's ButtonEventHandler(). M.P. Notice - 12/5/00
osStatus = GetWindowDefaultButton(_windowRef, &defaultButton);
if (osStatus==noErr && defaultButton) {
Boolean anotherButtonIsTracking = flag ? TRUE : FALSE;
osStatus = SetControlData(defaultButton, kControlNoPart, kControlPushButtonAnotherButtonTrackingTag, sizeof(Boolean), &anotherButtonIsTracking);
if (osStatus==noErr) DrawOneControl(defaultButton);
else NSLog(@"Some data couldn't be set in a Carbon control.");
}
}
*/
// Do the right thing for a Carbon window.
- (void)_cancelKey:(id)sender {
// Most of the time the handling of the cancel key will be done by Carbon, but this method will be invoked if an NSCarbonWindow is wrapping a Carbon window that contains an NSViewCarbonControl, and the escape key or whatever is pressed with an NSTextView focused. Just do what Carbon would do.
ControlRef cancelButton;
GetWindowCancelButton(_windowRef, &cancelButton);
if (cancelButton) {
if (IsControlActive(cancelButton)) {
HIViewSimulateClick(cancelButton, kControlButtonPart, 0, NULL);
}
}
}
// Do the right thing for a Carbon window.
- (void)_commonAwake {
// Complain, because this should never be called. We insist that -[NSCarbonWindow initWithCarbonWindowRef] is the only valid initializer for instances of this class, and that there's no such thing as a one-shot NSCarbonWindow.
NSLog(@"-[NSCarbonWindow _commonAwake] is not implemented.");
}
// There's no override of _commonInitFrame:styleMask:backing:defer:, despite the fact that NSWindow's modifies quite a few instance variables, because it gets called in a harmless way if the class instance is properly initialized with -[NSCarbonWindow initWithCarbonWindowRef:takingOwnership:].
// Do the right thing for a Carbon window.
- (id)_destroyRealWindow:(BOOL)orderingOut {
// Complain, because this should never be called. We don't support one-shot NSCarbonWindows.
NSLog(@"-[NSCarbonWindow _destroyRealWindow:] is not implemented.");
return self;
}
// There's no override of _discardCursorRectsForView, despite the fact that NSWindow's invokes [self windowNumber], because Carbon windows won't have subviews, and therefore won't have cursor rects.
// There's no override of _forceFlushWindowToScreen, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
// There's no override of _getPositionFromServer, despite the fact that NSWindow's operates on _windowNum, because it's only called from -[NSApplication _activateWindows], which is hopefully about to become obsolete any second now.
// There's no override of _globalWindowNum, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
// There's no override of _initContent:styleMask:backing:defer:contentView:, despite the fact that NSWindow's modifies _auxiliaryStorage->_auxWFlags.hasShadow, because it will never get called if the class instance is properly initialized with -[NSCarbonWindow initWithCarbonWindowRef:takingOwnership:].
// There's no override of _initContent:styleMask:backing:defer:counterpart:, despite the fact that NSWindow's modifies _auxiliaryStorage->_auxWFlags.hasShadow, because it will never get called if the class instance is properly initialized with -[NSCarbonWindow initWithCarbonWindowRef:takingOwnership:].
// Do what NSWindow would do, but then sychronize the Carbon window structures.
- (void)_oldPlaceWindow:(NSRect)frameRect {
OSStatus osStatus;
// Do the standard Cocoa thing.
[super _oldPlaceWindow:frameRect];
// Tell Carbon to update its various regions.
// Despite its name, this function should be called early and often, even if the window isn't visible yet. 2702648. M.P. Notice - 7/24/01
osStatus = WKSyncWindowWithCGAfterMove(_windowRef);
if (osStatus!=noErr) NSLog(@"A Carbon window's bounds couldn't be synchronized (%i).", (int)osStatus);
}
// There's no override of _orderOutAndCalcKeyWithCounter:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
// There's no override of _realHeartBeatThreadContext, despite the fact that NSWindows's invokes [self windowNumber], because it looks like it might not do anything that will effect a Carbon window.
// There's no override of _registerWithDockIfNeeded, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
// There's no override of _removeCursorRect:cursor:forView:, despite the fact that NSWindow's invokes [self windowNumber], because Carbon windows won't have subviews, and therefore won't have cursor rects.
// There's no override of _setAvoidsActivation:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
// There's no override of _setFrame:, despite the fact that NSWindow's modifies _frame, because it looks like it might work on Carbon windows as is. The synchronization of the Carbon window bounds rect to the Cocoa frame rect is done in the overrides of _oldPlaceWindow: and _windowMovedToRect:.
// There's no override of _setFrameCommon:display:stashSize:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
// There's no override of _setWindowNumber:, despite the fact that NSWindow's modifies _windowNum and invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
// Do what NSWindow would do, but for a Carbon window.
// This function is mostly cut-and-pasted from -[NSWindow _termWindowIfOwner]. M.P. Notice - 8/7/00
- (void)_termWindowIfOwner {
[self _setWindowNumber:-1];
_wFlags.isTerminating = YES;
if (_windowRef && _windowRefIsOwned) DisposeWindow(_windowRef);
// KW - need to clear window shadow state so it gets reset correctly when new window created
// if ([_borderView respondsToSelector:@selector(setShadowState:)]) {
// [_borderView setShadowState:kFrameShadowNone];
// }
_wFlags.isTerminating = NO;
}
// There's no override of _threadContext, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might not do anything that will effect a Carbon window.
// There's no override of _windowMoved:, despite the fact that NSWindow's operates on _windowNum, because it looks like it might work on Carbon windows as is.
// Do what NSWindow would do, but then sychronize the Carbon window structures.
- (void)_windowMovedToRect:(NSRect)actualFrame {
OSStatus osStatus;
// Do the standard Cocoa thing.
[super _windowMovedToRect:actualFrame];
// Let Carbon know that the window has been moved, unless this method is being called "early."
if (_wFlags.visible) {
osStatus = WKSyncWindowWithCGAfterMove(_windowRef);
if (osStatus!=noErr) NSLog(@"A Carbon window's bounds couldn't be synchronized (%i).", (int)osStatus);
}
}
- (NSRect)constrainFrameRect:(NSRect)actualFrame toScreen:(NSScreen *)screen {
// let Carbon decide window size and position
return actualFrame;
}
- (void)selectKeyViewFollowingView:(NSView *)aView {
HIViewRef view = NULL;
view = [HIViewAdapter getHIViewForNSView:aView];
if ( view )
{
HIViewRef contentView;
GetRootControl( GetControlOwner( view ), &contentView );
HIViewAdvanceFocus( contentView, 0 );
}
else
{
[super selectKeyViewFollowingView:aView];
}
}
- (void)selectKeyViewPrecedingView:(NSView *)aView {
HIViewRef view = NULL;
view = [HIViewAdapter getHIViewForNSView:aView];
if ( view )
{
HIViewRef contentView;
GetRootControl( GetControlOwner( view ), &contentView );
HIViewAdvanceFocus( contentView, shiftKey );
}
else
{
[super selectKeyViewPrecedingView:aView];
}
}
- (void)makeKeyWindow {
[NSApp _setMouseActivationInProgress:NO];
[NSApp setIsActive:YES];
[super makeKeyWindow];
WKShowKeyAndMain();
}
// Do the right thing for a Carbon window.
- (BOOL)canBecomeKeyWindow {
return YES;
}
// Do the right thing for a Carbon window.
- (BOOL)canBecomeMainWindow {
OSStatus osStatus;
WindowClass windowClass;
// By default, Carbon windows cannot become the main window.
// What about when the default isn't right? Requiring subclassing seems harsh. M.P. Warning - 9/17/01
// KW - modify this to allow document windows to become main
// This is primarily to get the right look, so that you don't have two windows that both look active - one Cocoa document and one Carbon document
osStatus = GetWindowClass(_windowRef, &windowClass);
return (osStatus == noErr && windowClass == kDocumentWindowClass);
}
// There's no override of deminiaturize:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
// There's no override of disableCursorRects, despite the fact that NSWindow's invokes [self windowNumber], because Carbon windows won't have subviews, and therefore won't have cursor rects.
// There's no override of enableCursorRects, despite the fact that NSWindow's invokes [self windowNumber], because Carbon windows won't have subviews, and therefore won't have cursor rects.
// Do the right thing for a Carbon window.
- (void)encodeWithCoder:(NSCoder *)coder {
// Actually, this will probably never be implemented. M.P. Notice - 8/2/00
NSLog(@"-[NSCarbonWindow encodeWithCoder:] is not implemented.");
}
// There's no override of frame, despite the fact that NSWindow's returns _frame, because _frame is one of the instance variables whose value we're keeping synchronized with the Carbon window.
// Do the right thing for a Carbon window.
- (id)initWithCoder:(NSCoder *)coder {
// Actually, this will probably never be implemented. M.P. Notice - 8/2/00
NSLog(@"-[NSCarbonWindow initWithCoder:] is not implemented.");
[self release];
return nil;
}
// There's no override of level, despite the fact that NSWindow's returns _level, because _level is one of the instance variables whose value we're keeping synchronized with the Carbon window.
// There's no override of miniaturize:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
// There's no override of resizeToScreenWithEvent:, despite the fact that NSWindow's operates on _windowNum.
// It looks like it's only called when an _NSForceResizeEventType event is passed into -[NSWindow sendEvent:], and I can't find any instances of that happening.
/*
// Do the right thing for a Carbon Window.
- (void)sendEvent:(NSEvent *)theEvent {
// Not all events are handled in the same manner.
NSEventType eventType = [theEvent type];
if (eventType==NSAppKitDefined) {
// Handle the event the Cocoa way. Carbon won't understand it anyway.
[super sendEvent:theEvent];
}
}
*/
// There's no override of setAcceptsMouseMovedEvents:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
// There's no override of setBackingType:, despite the fact that NSWindow's invokes [self windowNumber], because it's apparently not expected to do anything anyway, judging from the current implementation of PSsetwindowtype().
// Do what NSWindow would do, but for a Carbon window.
- (void)setContentView:(NSView *)aView {
NSRect contentFrameRect;
OSStatus osStatus;
Rect windowContentBoundsRect;
// Precondition check.
assert(_borderView);
assert([_borderView isKindOfClass:[CarbonWindowFrame class]]);
assert(_windowRef);
// Parameter check.
assert(aView);
assert([aView isKindOfClass:[CarbonWindowContentView class]]);
// Find out the window's Carbon window structure region (content) bounds.
osStatus = GetWindowBounds(_windowRef, kWindowContentRgn, &windowContentBoundsRect);
if (osStatus!=noErr) NSLog(@"A Carbon window's content bounds couldn't be gotten.");
contentFrameRect.origin = NSZeroPoint;
contentFrameRect.size.width = windowContentBoundsRect.right - windowContentBoundsRect.left;
contentFrameRect.size.height = windowContentBoundsRect.bottom - windowContentBoundsRect.top;
// If the content view is still in some other view hierarchy, pry it free.
[_contentView removeFromSuperview];
assert(![_contentView superview]);
// Record the content view, and size it to this window's content frame.
_contentView = aView;
[(NSView *)_contentView setFrame:contentFrameRect];
// Make the content view a subview of the border view.
[_borderView addSubview:_contentView];
// Tell the content view it's new place in the responder chain.
[_contentView setNextResponder:self];
}
// There's no override of setDepthLimit:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
- (BOOL)worksWhenModal {
WindowClass windowClass = [self _carbonWindowClass];
return (windowClass == kFloatingWindowClass || windowClass == kUtilityWindowClass);
}
- (void)_setModalWindowLevel {
return;
}
- (id)_clearModalWindowLevel {
return nil;
}
// There's no override of setLevel:, despite the fact that NSWindow's invokes [self windowNumber], because it looks like it might work on Carbon windows as is.
// I thought at first that there should be a mapping between Cocoa level and Carbon window class, but experiments convince me that such is not the case. M.P. Notice - 9/18/00
// There's no override of windowNumber, despite the fact that NSWindow's returns _windowNum, because _windowNum is one of the instance variables whose value we're keeping synchronized with the Carbon window.
- (UInt32)carbonHICommandIDFromActionSelector:(SEL)inActionSelector {
// Initialize with the default return value.
UInt32 hiCommandID = 0;
// Pretty simple, if tedious.
if (inActionSelector==@selector(clear:)) hiCommandID = kHICommandClear;
else if (inActionSelector==@selector(copy:)) hiCommandID = kHICommandCopy;
else if (inActionSelector==@selector(cut:)) hiCommandID = kHICommandCut;
else if (inActionSelector==@selector(paste:)) hiCommandID = kHICommandPaste;
else if (inActionSelector==@selector(redo:)) hiCommandID = kHICommandRedo;
else if (inActionSelector==@selector(selectAll:)) hiCommandID = kHICommandSelectAll;
else if (inActionSelector==@selector(undo:)) hiCommandID = kHICommandUndo;
// Done.
return hiCommandID;
}
- (void)sendCarbonProcessHICommandEvent:(UInt32)inHICommandID {
EventTargetRef eventTargetRef;
HICommand hiCommand;
OSStatus osStatus;
// Initialize for safe error handling.
EventRef eventRef = NULL;
// Create a Process Command event. Don't mention anything about the menu item, because we don't want the Carbon Event handler fiddling with it.
hiCommand.attributes = 0;
hiCommand.commandID = inHICommandID;
hiCommand.menu.menuRef = 0;
hiCommand.menu.menuItemIndex = 0;
osStatus = CreateEvent(NULL, kEventClassCommand, kEventCommandProcess, GetCurrentEventTime(), kEventAttributeNone, &eventRef);
if (osStatus!=noErr) {
NSLog(@"CreateEvent() returned %i.", (int)osStatus);
goto CleanUp;
}
osStatus = SetEventParameter(eventRef, kEventParamDirectObject, typeHICommand, sizeof(HICommand), &hiCommand);
if (osStatus!=noErr) {
NSLog(@"SetEventParameter() returned %i.", (int)osStatus);
goto CleanUp;
}
// Send a Carbon event to whatever has the Carbon user focus.
eventTargetRef = GetUserFocusEventTarget();
osStatus = SendEventToEventTarget(eventRef, eventTargetRef);
if (osStatus!=noErr) {
NSLog(@"SendEventToEventTarget() returned %i.", (int)osStatus);
goto CleanUp;
}
CleanUp:
// Clean up.
if (eventRef) ReleaseEvent(eventRef);
}
- (Boolean)sendCarbonUpdateHICommandStatusEvent:(UInt32)inHICommandID withMenuRef:(MenuRef)inMenuRef andMenuItemIndex:(UInt16)inMenuItemIndex {
EventTargetRef eventTargetRef;
HICommand hiCommand;
OSStatus osStatus;
// Initialize for safe error handling and flag returning.
Boolean eventWasHandled = FALSE;
EventRef eventRef = NULL;
// Create a Process Command event. Don't mention anything about the menu item, because we don't want the Carbon Event handler fiddling with it.
hiCommand.attributes = kHICommandFromMenu;
hiCommand.commandID = inHICommandID;
hiCommand.menu.menuRef = inMenuRef;
hiCommand.menu.menuItemIndex = inMenuItemIndex;
osStatus = CreateEvent(NULL, kEventClassCommand, kEventCommandUpdateStatus, GetCurrentEventTime(), kEventAttributeNone, &eventRef);
if (osStatus!=noErr) {
NSLog(@"CreateEvent() returned %i.", (int)osStatus);
goto CleanUp;
}
osStatus = SetEventParameter(eventRef, kEventParamDirectObject, typeHICommand, sizeof(HICommand), &hiCommand);
if (osStatus!=noErr) {
NSLog(@"SetEventParameter() returned %i.", (int)osStatus);
goto CleanUp;
}
// Send a Carbon event to whatever has the Carbon user focus.
eventTargetRef = GetUserFocusEventTarget();
osStatus = SendEventToEventTarget(eventRef, eventTargetRef);
if (osStatus==noErr) {
eventWasHandled = TRUE;
} else if (osStatus!=eventNotHandledErr) {
NSLog(@"SendEventToEventTarget() returned %i.", (int)osStatus);
goto CleanUp;
}
CleanUp:
// Clean up.
if (eventRef) ReleaseEvent(eventRef);
// Done.
return eventWasHandled;
}
- (void)_handleRootBoundsChanged
{
HIViewRef root = HIViewGetRoot( _windowRef );
HIRect frame;
HIViewGetFrame( root, &frame );
[_borderView setFrameSize:*(NSSize*)&frame.size];
}
- (void)_handleContentBoundsChanged
{
HIViewRef root, contentView;
HIRect rootBounds, contentFrame;
NSRect oldContentFrameRect;
root = HIViewGetRoot( _windowRef );
HIViewFindByID( root, kHIViewWindowContentID, &contentView );
HIViewGetFrame( contentView, &contentFrame );
HIViewGetBounds( root, &rootBounds );
// Set the content view's frame rect from the Carbon window's content region bounds.
contentFrame.origin.y = rootBounds.size.height - CGRectGetMaxY( contentFrame );
oldContentFrameRect = [(NSView *)_contentView frame];
if ( !NSEqualRects( *(NSRect*)&contentFrame, oldContentFrameRect ) ) {
[(NSView *)_contentView setFrame:*(NSRect*)&contentFrame];
}
}
- (OSStatus)_handleCarbonEvent:(EventRef)inEvent callRef:(EventHandlerCallRef)inCallRef {
OSStatus result = eventNotHandledErr;
switch ( GetEventClass( inEvent ) )
{
case kEventClassControl:
{
ControlRef control;
check( GetEventKind( inEvent ) == kEventControlBoundsChanged );
GetEventParameter( inEvent, kEventParamDirectObject, typeControlRef, NULL,
sizeof( ControlRef ), NULL, &control );
if ( control == HIViewGetRoot( _windowRef ) )
[self _handleRootBoundsChanged];
else
[self _handleContentBoundsChanged];
}
break;
case kEventClassWindow:
switch ( GetEventKind( inEvent ) )
{
case kEventWindowShown:
[self _setVisible:YES];
break;
case kEventWindowHidden:
[self _setVisible:NO];
break;
case kEventWindowActivated:
[self makeKeyWindow];
break;
case kEventWindowDeactivated:
[self resignKeyWindow];
break;
case kEventWindowBoundsChanged:
[self reconcileToCarbonWindowBounds];
break;
}
break;
}
return result;
}
// Handle various events that Carbon is sending to our window.
static OSStatus NSCarbonWindowHandleEvent(EventHandlerCallRef inEventHandlerCallRef, EventRef inEventRef, void *inUserData) {
// default action is to send event to next handler. We modify osStatus as necessary where we don't want this behavior
OSStatus osStatus = eventNotHandledErr;
// We do different things for different event types.
CarbonWindowAdapter *carbonWindow = (CarbonWindowAdapter *)inUserData;
osStatus = [carbonWindow _handleCarbonEvent: inEventRef callRef: inEventHandlerCallRef];
// Done. If we want to propagate the event, we return eventNotHandledErr to send it to the next handler
return osStatus;
}
// [3364117] We need to make sure this does not fall through to the AppKit implementation! bad things happen.
- (void)_reallyDoOrderWindow:(NSWindowOrderingMode)place relativeTo:(int)otherWin findKey:(BOOL)doKeyCalc forCounter:(BOOL)isACounter force:(BOOL)doForce isModal:(BOOL)isModal {
}
- (NSRect) _growBoxRect
{
WindowAttributes attrs;
NSRect retRect = NSZeroRect;
GetWindowAttributes( _windowRef, &attrs );
if ( attrs & kWindowResizableAttribute )
{
HIRect bounds, rect;
HIViewRef view;
HIViewGetBounds( HIViewGetRoot( _windowRef ), &bounds );
HIViewFindByID( HIViewGetRoot( _windowRef ), kHIViewWindowGrowBoxID, &view );
HIViewGetFrame( view, &rect );
rect.origin.y = bounds.size.height - CGRectGetMaxY( rect ) - 1;
rect.origin.x++;
retRect = *(NSRect*)▭
}
return retRect;
}
@end // implementation CarbonWindowAdapter
#endif
|