6
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
7
("Apple") in consideration of your agreement to the following terms, and your
8
use, installation, modification or redistribution of this Apple software
9
constitutes acceptance of these terms. If you do not agree with these terms,
10
please do not use, install, modify or redistribute this Apple software.
12
In consideration of your agreement to abide by the following terms, and subject
13
to these terms, Apple grants you a personal, non-exclusive license, under Apple�s
14
copyrights in this original Apple software (the "Apple Software"), to use,
15
reproduce, modify and redistribute the Apple Software, with or without
16
modifications, in source and/or binary forms; provided that if you redistribute
17
the Apple Software in its entirety and without modifications, you must retain
18
this notice and the following text and disclaimers in all such redistributions of
19
the Apple Software. Neither the name, trademarks, service marks or logos of
20
Apple Computer, Inc. may be used to endorse or promote products derived from the
21
Apple Software without specific prior written permission from Apple. Except as
22
expressly stated in this notice, no other rights or licenses, express or implied,
23
are granted by Apple herein, including but not limited to any patent rights that
24
may be infringed by your derivative works or by other works in which the Apple
25
Software may be incorporated.
27
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
28
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
29
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
31
COMBINATION WITH YOUR PRODUCTS.
33
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
34
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
35
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
37
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
38
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
39
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41
Copyright � 2000-2005 Apple Computer, Inc., All Rights Reserved
47
// -----------------------------------------------------------------------------
49
// -----------------------------------------------------------------------------
52
// Our base class for creating a Mac OS window which can be Carbon Event-savvy.
54
// -----------------------------------------------------------------------------
55
// TWindow private constructor
56
// -----------------------------------------------------------------------------
61
CFStringRef inNibName )
62
: _appSignature(inAppSignature)
66
Init( inName, inNibName );
69
// -----------------------------------------------------------------------------
70
// TWindow private constructor
71
// -----------------------------------------------------------------------------
76
: _appSignature(inAppSignature)
81
// -----------------------------------------------------------------------------
83
// -----------------------------------------------------------------------------
87
if ( fWindow != NULL )
88
DisposeWindow( fWindow );
91
// -----------------------------------------------------------------------------
92
// Init (with WindowRef)
93
// -----------------------------------------------------------------------------
101
EventTypeSpec kEvents[] =
103
{ kEventClassWindow, kEventWindowClose },
104
{ kEventClassWindow, kEventWindowActivated },
105
{ kEventClassWindow, kEventWindowDeactivated },
106
{ kEventClassWindow, kEventWindowDispose },
107
{ kEventClassWindow, kEventWindowDrawContent },
108
{ kEventClassWindow, kEventWindowBoundsChanged },
109
{ kEventClassWindow, kEventWindowGetIdealSize },
110
{ kEventClassWindow, kEventWindowGetMinimumSize },
111
{ kEventClassWindow, kEventWindowGetMaximumSize },
112
{ kEventClassCommand, kEventCommandProcess },
113
{ kEventClassCommand, kEventCommandUpdateStatus },
117
require_action( inWindow != NULL, InvalidWindow, err = paramErr );
121
err = RegisterForEvents( GetEventTypeCount( kEvents ), kEvents );
122
require_noerr( err, CantRegisterForEvents );
125
err = SetWindowProperty( inWindow, 'hifr', 'twin', sizeof( TWindow* ), &tWindow );
127
CantRegisterForEvents:
132
// -----------------------------------------------------------------------------
134
// -----------------------------------------------------------------------------
138
CFStringRef inWindowName,
139
CFStringRef inNibName )
145
err = nib.Init( inNibName );
146
require_noerr( err, CantInitNib );
148
err = nib.CreateWindow( inWindowName, &window );
149
require_noerr( err, CantCreateWindow );
151
err = Init( window );
158
// -----------------------------------------------------------------------------
160
// -----------------------------------------------------------------------------
165
WindowAttributes inAttributes,
166
const Rect& inBounds )
171
err = CreateNewWindow( inClass, inAttributes, &inBounds, &window );
172
require_noerr( err, CantCreateWindow );
174
err = Init( window );
180
// -----------------------------------------------------------------------------
182
// -----------------------------------------------------------------------------
185
TWindow::GetEventTarget()
187
return GetWindowEventTarget( GetWindowRef() );
190
// -----------------------------------------------------------------------------
191
// SourceWindowDispose
192
// -----------------------------------------------------------------------------
195
TWindow::SourceWindowDispose()
200
// -----------------------------------------------------------------------------
202
// -----------------------------------------------------------------------------
211
// -----------------------------------------------------------------------------
213
// -----------------------------------------------------------------------------
216
TWindow::GetPort() const
218
return GetWindowPort( GetWindowRef() );
221
// -----------------------------------------------------------------------------
223
// -----------------------------------------------------------------------------
227
CFStringRef inTitle )
229
SetWindowTitleWithCFString( GetWindowRef(), inTitle );
232
// -----------------------------------------------------------------------------
234
// -----------------------------------------------------------------------------
237
TWindow::CopyTitle() const
239
CFStringRef outTitle;
241
CopyWindowTitleAsCFString( GetWindowRef(), &outTitle );
246
// -----------------------------------------------------------------------------
248
// -----------------------------------------------------------------------------
251
TWindow::SetAlternateTitle(
252
CFStringRef inTitle )
254
SetWindowAlternateTitle( GetWindowRef(), inTitle );
257
// -----------------------------------------------------------------------------
258
// CopyAlternateTitle
259
// -----------------------------------------------------------------------------
262
TWindow::CopyAlternateTitle() const
264
CFStringRef outTitle;
266
CopyWindowAlternateTitle( GetWindowRef(), &outTitle );
271
// -----------------------------------------------------------------------------
273
// -----------------------------------------------------------------------------
278
ShowWindow( GetWindowRef() );
280
if ( GetWindowRef() == ActiveNonFloatingWindow() )
282
WindowActivationScope scope;
284
GetWindowActivationScope( GetWindowRef(), &scope );
285
if ( scope == kWindowActivationScopeAll )
286
AdvanceKeyboardFocus( GetWindowRef() );
290
// -----------------------------------------------------------------------------
292
// -----------------------------------------------------------------------------
297
HideWindow( GetWindowRef() );
300
// -----------------------------------------------------------------------------
302
// -----------------------------------------------------------------------------
305
TWindow::IsVisible() const
307
return IsWindowVisible( GetWindowRef() );
310
// -----------------------------------------------------------------------------
312
// -----------------------------------------------------------------------------
317
SelectWindow( GetWindowRef() );
320
// -----------------------------------------------------------------------------
322
// -----------------------------------------------------------------------------
329
// -----------------------------------------------------------------------------
331
// -----------------------------------------------------------------------------
338
// -----------------------------------------------------------------------------
340
// -----------------------------------------------------------------------------
343
TWindow::Deactivated()
347
// -----------------------------------------------------------------------------
349
// -----------------------------------------------------------------------------
356
// -----------------------------------------------------------------------------
358
// -----------------------------------------------------------------------------
365
// -----------------------------------------------------------------------------
367
// -----------------------------------------------------------------------------
370
TWindow::GetIdealSize()
372
Point idealSize = { 0, 0 };
377
// -----------------------------------------------------------------------------
379
// -----------------------------------------------------------------------------
382
TWindow::GetMinimumSize()
384
Point minSize = { 0, 0 };
389
// -----------------------------------------------------------------------------
391
// -----------------------------------------------------------------------------
394
TWindow::GetMaximumSize()
396
Point maxSize = { 0, 0 };
401
// -----------------------------------------------------------------------------
403
// -----------------------------------------------------------------------------
407
const Rect& inBounds )
409
SetWindowBounds( GetWindowRef(), kWindowStructureRgn, &inBounds );
412
// -----------------------------------------------------------------------------
414
// -----------------------------------------------------------------------------
417
TWindow::InvalidateArea(
420
InvalWindowRgn( GetWindowRef(), inRegion );
423
// -----------------------------------------------------------------------------
425
// -----------------------------------------------------------------------------
428
TWindow::InvalidateArea(
431
InvalWindowRect( GetWindowRef(), &inRect );
434
// -----------------------------------------------------------------------------
436
// -----------------------------------------------------------------------------
439
TWindow::ValidateArea(
442
ValidWindowRgn( GetWindowRef(), inRegion );
445
// -----------------------------------------------------------------------------
447
// -----------------------------------------------------------------------------
450
TWindow::ValidateArea(
453
ValidWindowRect( GetWindowRef(), &inRect );
456
// -----------------------------------------------------------------------------
457
// UpdateCommandStatus
458
// -----------------------------------------------------------------------------
461
TWindow::UpdateCommandStatus(
462
const HICommandExtended& inCommand )
464
if ( inCommand.commandID == kHICommandClose )
466
EnableMenuCommand( NULL, inCommand.commandID );
470
return false; // not handled
473
// -----------------------------------------------------------------------------
475
// -----------------------------------------------------------------------------
478
TWindow::HandleCommand(
479
const HICommandExtended& inCommand )
481
return false; // not handled
484
// -----------------------------------------------------------------------------
486
// -----------------------------------------------------------------------------
489
TWindow::SetDefaultButton(
492
SetWindowDefaultButton( GetWindowRef(), inView );
495
// -----------------------------------------------------------------------------
497
// -----------------------------------------------------------------------------
500
TWindow::SetCancelButton(
503
SetWindowCancelButton( GetWindowRef(), inView );
506
// -----------------------------------------------------------------------------
508
// -----------------------------------------------------------------------------
511
TWindow::HandleEvent(
512
EventHandlerCallRef inRef,
513
TCarbonEvent& inEvent )
515
OSStatus result = eventNotHandledErr;
517
HICommandExtended command;
519
switch ( inEvent.GetClass() )
521
case kEventClassCommand:
523
inEvent.GetParameter( kEventParamDirectObject, &command );
525
switch ( inEvent.GetKind() )
527
case kEventCommandProcess:
528
if ( this->HandleCommand( command ) )
532
case kEventCommandUpdateStatus:
533
if ( this->UpdateCommandStatus( command ) )
540
case kEventClassWindow:
541
switch ( inEvent.GetKind() )
543
case kEventWindowActivated:
544
CallNextEventHandler( inRef, inEvent.GetEventRef() );
549
case kEventWindowBoundsChanged:
550
if ( inEvent.GetParameter( kEventParamAttributes, &attributes ) )
552
if ( attributes & kWindowBoundsChangeSizeChanged )
557
else if ( attributes & kWindowBoundsChangeOriginChanged )
565
case kEventWindowClose:
570
case kEventWindowDeactivated:
571
CallNextEventHandler( inRef, inEvent.GetEventRef() );
576
case kEventWindowDispose:
577
CallNextEventHandler( inRef, inEvent.GetEventRef() );
578
this->SourceWindowDispose();
582
case kEventWindowDrawContent:
583
CallNextEventHandler( inRef, inEvent.GetEventRef() );
588
case kEventWindowGetIdealSize:
590
Point size = this->GetIdealSize();
592
if ( (size.h != 0) && (size.v != 0) )
594
inEvent.SetParameter( kEventParamDimensions, size );
600
case kEventWindowGetMinimumSize:
602
Point size = this->GetMinimumSize();
604
if ( (size.h != 0) && (size.v != 0) )
606
inEvent.SetParameter( kEventParamDimensions, size );
612
case kEventWindowGetMaximumSize:
614
Point size = this->GetIdealSize();
616
if ( (size.h != 0) && (size.v != 0) )
618
inEvent.SetParameter( kEventParamDimensions, size );
629
// -----------------------------------------------------------------------------
631
// -----------------------------------------------------------------------------
639
if ( GetControlByID( GetWindowRef(), &inID, &view ) != noErr )
645
// -----------------------------------------------------------------------------
647
// -----------------------------------------------------------------------------
655
ControlID viewID = { inSignature, inID };
657
view = GetView( viewID );
662
// -----------------------------------------------------------------------------
664
// -----------------------------------------------------------------------------
670
OSStatus err = noErr;
673
view = GetView( inID );
674
require_action( view != NULL, CantGetView, err = errUnknownControl );
676
EnableControl( view );
682
// -----------------------------------------------------------------------------
684
// -----------------------------------------------------------------------------
692
ControlID viewID = { inSignature, inID };
694
err = EnableView( viewID );
699
// -----------------------------------------------------------------------------
701
// -----------------------------------------------------------------------------
704
TWindow::DisableView(
707
OSStatus err = noErr;
710
view = GetView( inID );
711
require_action( view != NULL, CantGetView, err = errUnknownControl );
713
DisableControl( view );
719
// -----------------------------------------------------------------------------
721
// -----------------------------------------------------------------------------
724
TWindow::DisableView(
729
ControlID viewID = { inSignature, inID };
731
err = DisableView( viewID );
736
// -----------------------------------------------------------------------------
738
// -----------------------------------------------------------------------------
747
view = GetView( inID );
748
require_action( view != NULL, CantGetView, err = errUnknownControl );
756
// -----------------------------------------------------------------------------
758
// -----------------------------------------------------------------------------
766
ControlID viewID = { inSignature, inID };
768
err = ShowView( viewID );
773
// -----------------------------------------------------------------------------
775
// -----------------------------------------------------------------------------
784
view = GetView( inID );
785
require_action( view != NULL, CantGetView, err = errUnknownControl );
793
// -----------------------------------------------------------------------------
795
// -----------------------------------------------------------------------------
803
ControlID viewID = { inSignature, inID };
805
err = HideView( viewID );
810
// --------------------------------------------------------------------------------
811
// GetTWindowFromWindowRef
812
// --------------------------------------------------------------------------------
815
TWindow::GetTWindowFromWindowRef(
816
WindowRef inWindowRef )
818
TWindow* tWindow = NULL;
819
verify_noerr( GetWindowProperty( inWindowRef, 'hifr', 'twin', sizeof( TWindow* ), NULL, &tWindow ) );
824
TWindow::GetLabelControlString(const int inID, std::string& value)
826
return GetControlString(inID, kControlStaticTextTextTag, value);
830
TWindow::SetLabelControlString(const int inID, const std::string& value)
832
return SetControlString(inID, kControlStaticTextTextTag, value);
836
TWindow::GetTextControlString(const int inID, std::string& value)
838
return GetControlString(inID, kControlEditTextTextTag, value);
842
TWindow::SetTextControlString(const int inID, const std::string& value)
844
return SetControlString(inID, kControlEditTextTextTag, value);
848
TWindow::GetPasswordControlString(const int inID, std::string& value)
850
OSStatus err = noErr;
851
ControlID controlID = { _appSignature, inID };
852
ControlRef control = NULL;
853
CFStringRef result = NULL;
854
char* pszValue = NULL;
856
err = GetControlByID(GetWindowRef(), &controlID, &control);
859
err = GetControlData(control,
860
kControlEntireControl,
861
kControlEditTextPasswordCFStringTag,
867
CFIndex len = CFStringGetLength(result);
870
pszValue = new char[len+1];
871
if (CFStringGetCString(result, pszValue, len+1, kCFStringEncodingMacRoman))
874
delete [] pszValue; pszValue = NULL;
894
TWindow::SetPasswordControlString(const int inID, const std::string& value)
896
OSStatus err = noErr;
897
ControlID controlID = { _appSignature, inID };
898
ControlRef control = NULL;
899
CFStringRef cfVal = NULL;
901
err = GetControlByID(GetWindowRef(), &controlID, &control);
904
cfVal = CFStringCreateWithCString(NULL, value.c_str(), kCFStringEncodingMacRoman);
906
err = SetControlData(control,
907
kControlEntireControl,
908
kControlEditTextPasswordCFStringTag,
922
TWindow::GetControlString(const int inID, ResType resourceType, std::string& value)
924
OSStatus err = noErr;
925
ControlID controlID = { _appSignature, inID };
926
ControlRef control = NULL;
929
err = GetControlByID(GetWindowRef(), &controlID, &control);
934
err = GetControlDataSize(control,
935
kControlEntireControl,
938
if ((err == noErr) && actualSize > 0)
940
pszBuf = new char[actualSize+1];
943
err = GetControlData(control,
944
kControlEntireControl,
951
*(pszBuf+actualSize) = '\0';
966
TWindow::SetControlString(const int inID, ResType resourceType, const std::string& inValue)
968
OSStatus err = noErr;
969
ControlID controlID = { _appSignature, inID };
970
ControlRef control = NULL;
972
err = GetControlByID(GetWindowRef(), &controlID, &control);
975
err = SetControlData(control,
976
kControlEntireControl,
979
inValue.length() ? inValue.c_str() : "");
986
TWindow::SetControlValue(const int inID, const SInt32 inValue)
988
OSStatus err = noErr;
989
ControlID controlID = { _appSignature, inID };
990
ControlRef control = NULL;
992
err = GetControlByID(GetWindowRef(), &controlID, &control);
995
SetControl32BitValue(control, inValue);
1002
TWindow::GetControlValue(const int inID, SInt32& outValue)
1004
OSStatus err = noErr;
1005
ControlID controlID = { _appSignature, inID };
1006
ControlRef control = NULL;
1008
err = GetControlByID(GetWindowRef(), &controlID, &control);
1011
outValue = GetControl32BitValue(control);
1018
TWindow::IsRadioButtonSet(const int inID)
1021
return (noErr == GetControlValue(inID, value)) && (value != 0);
1025
TWindow::EnableLocalControl(const int inID)
1027
OSStatus err = noErr;
1028
ControlID controlID = { _appSignature, inID };
1029
ControlRef control = NULL;
1031
err = GetControlByID(GetWindowRef(), &controlID, &control);
1034
err = EnableControl(control);
1041
TWindow::DisableLocalControl(const int inID)
1043
OSStatus err = noErr;
1044
ControlID controlID = { _appSignature, inID };
1045
ControlRef control = NULL;
1047
err = GetControlByID(GetWindowRef(), &controlID, &control);
1050
err = DisableControl(control);
1057
TWindow::PostApplicationEvent(const int cmdID)
1059
OSStatus err = noErr;
1062
err = CreateEvent(NULL, kEventClassCommand, kEventCommandProcess, GetCurrentEventTime(), 0, &event);
1067
BlockZero(&command, sizeof(command));
1068
command.commandID = cmdID;
1070
SetEventParameter(event, kEventParamDirectObject, typeHICommand, sizeof(command), &command);
1072
SendEventToEventTarget(event, GetApplicationEventTarget());
1073
ReleaseEvent(event);
1080
TWindow::PostWindowEvent(const int cmdID, WindowRef window)
1082
OSStatus err = noErr;
1085
err = CreateEvent(NULL, kEventClassCommand, kEventCommandProcess, GetCurrentEventTime(), kEventAttributeUserEvent, &event);
1090
BlockZero(&command, sizeof(command));
1091
command.commandID = cmdID;
1093
SetEventParameter(event, kEventParamDirectObject, typeHICommand, sizeof(command), &command);
1095
SendEventToEventTarget(event, GetWindowEventTarget(window));
1096
ReleaseEvent(event);