Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
HITextShowcase.cp
/* |
File: HITextShowcase.cp |
Contains: Demonstrates various, old and new, controls and HIViews associated with text. |
Version: 1.0.1 |
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. |
("Apple") in consideration of your agreement to the following terms, and your |
use, installation, modification or redistribution of this Apple software |
constitutes acceptance of these terms. If you do not agree with these terms, |
please do not use, install, modify or redistribute this Apple software. |
In consideration of your agreement to abide by the following terms, and subject |
to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs |
copyrights in this original Apple software (the "Apple Software"), to use, |
reproduce, modify and redistribute the Apple Software, with or without |
modifications, in source and/or binary forms; provided that if you redistribute |
the Apple Software in its entirety and without modifications, you must retain |
this notice and the following text and disclaimers in all such redistributions of |
the Apple Software. Neither the name, trademarks, service marks or logos of |
Apple Computer, Inc. may be used to endorse or promote products derived from the |
Apple Software without specific prior written permission from Apple. Except as |
expressly stated in this notice, no other rights or licenses, express or implied, |
are granted by Apple herein, including but not limited to any patent rights that |
may be infringed by your derivative works or by other works in which the Apple |
Software may be incorporated. |
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO |
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED |
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN |
COMBINATION WITH YOUR PRODUCTS. |
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION |
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT |
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN |
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Copyright © 2002 Apple Computer, Inc., All Rights Reserved |
*/ |
#include <Carbon/Carbon.h> |
#include "HITextShowcase.h" |
// |
// Drawing the common non-control text elements (TextEdit, ATSUI, and Appearance) |
// |
void DrawNonControlTextElements(Rect *itemRect) |
{ |
char theText[] = "Using TETextBox(). This is a Text Edit text box.\rUsing TETextBox(). This is a Text Edit text box.\rUsing TETextBox(). This is a Text Edit text box.\rUsing TETextBox(). This is a Text Edit text box.\rUsing TETextBox(). This is a Text Edit text box.\rUsing TETextBox(). This is a Text Edit text box."; |
OffsetRect(itemRect, 0, 120); |
TETextBox(theText, strlen(theText), itemRect, teJustLeft); |
FrameRect(itemRect); |
char atsuText[] = "Using atsuTextBox(). This is an ATSUI text box.\rUsing atsuTextBox(). This is an ATSUI text box.\rUsing atsuTextBox(). This is an ATSUI text box.\rUsing atsuTextBox(). This is an ATSUI text box.\rUsing atsuTextBox(). This is an ATSUI text box.\rUsing atsuTextBox(). This is an ATSUI text box."; |
CFStringRef theATSUITextRef = CFStringCreateWithCString(NULL, atsuText, kCFStringEncodingMacRoman); |
UniCharCount atsuTextLength = CFStringGetLength(theATSUITextRef); |
UniChar * UniCharBuffer = (UniChar *) malloc(atsuTextLength * sizeof(UniChar)); |
CFStringGetCharacters(theATSUITextRef, CFRangeMake(0, atsuTextLength), UniCharBuffer); |
OffsetRect(itemRect, 0, 120); |
atsuTextBox(UniCharBuffer, atsuTextLength, itemRect, teJustLeft, 0, katsuHorizontalText, true); |
FrameRect(itemRect); |
CFRelease(theATSUITextRef); |
OffsetRect(itemRect, 0, 120); |
DrawThemeTextBox(CFSTR("Using DrawThemeTextBox(). This is an Appearance Theme-compliant text box.\rUsing DrawThemeTextBox(). This is an Appearance Theme-compliant text box.\rUsing DrawThemeTextBox(). This is an Appearance Theme-compliant text box.\rUsing DrawThemeTextBox(). This is an Appearance Theme-compliant text box.\rUsing DrawThemeTextBox(). This is an Appearance Theme-compliant text box.\rUsing DrawThemeTextBox(). This is an Appearance Theme-compliant text box."), kThemeApplicationFont /* kThemeSystemFont */, kThemeStateActive, true, itemRect, teJustLeft, NULL); |
FrameRect(itemRect); |
} |
// |
// This is the handler for kEventWindowDrawContent for the non-compositing window. |
// |
// Since the window is non-compositing, we have to draw the background picture at this time |
// so that all other controls are drawn on top of it. |
// We can't use a picture control because it would have to be the first in the control list in |
// order to be drawn over by the other controls but then it would also grab and eat all the mouse clicks |
// preventing all the other controls from ever being clicked. |
// |
// Since we are already handling drawing here, we take the opportunity to draw the rest of the non-control |
// text elements (TextEdit, ATSUI, and Appearance) which could have been drawn in a kEventWindowUpdate handler. |
// |
pascal OSStatus WindowDrawingTextOverPictureHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData) |
{ |
PicHandle thePict = GetPicture(128); |
if (thePict != NULL) |
{ |
GrafPtr curPort; |
GetPort(&curPort); |
Rect theRect; |
GetPortBounds(curPort, &theRect); |
DrawPicture(thePict, &theRect); |
} |
Rect itemRect = {140, 20, 235, 580}; |
DrawNonControlTextElements(&itemRect); |
// drawing the standard window content last |
return CallNextEventHandler(nextHandler, theEvent); |
} |
// |
// This is the handler for kEventControlDraw for the background picture control in the compositing window. |
// |
// Since the window is compositing, we can just use a regular picture control for the background. This control |
// will be the first in the list so that it is drawn first and the other controls on top of it, |
// Since the window is compositing, the clicks (or mouseDown events) are sent to the controls in reverse order of |
// the drawing order so that the other controls will get them. |
// |
// We only have to draw the additional non-control text elements (TextEdit, ATSUI, Appearance, and HITheme) on top |
// of the picture (after we call CallNextEventHandler to get the standard handler for kEventControlDraw for the |
// background picture control). |
// |
pascal OSStatus ControlDrawingTextOverPictureHandler(EventHandlerCallRef nextHandler, EventRef theEvent, void* userData) |
{ |
// drawing the picture background first |
OSStatus status = CallNextEventHandler(nextHandler, theEvent); |
Rect itemRect = {140, 20, 235, 580}; |
DrawNonControlTextElements(&itemRect); |
CGContextRef context; |
GetEventParameter(theEvent, kEventParamCGContextRef, typeCGContextRef, NULL, sizeof(context), NULL, &context); |
HIRect hiBounds = { {itemRect.left, itemRect.top + 120}, {itemRect.right - itemRect.left, 95} }; |
HIThemeTextInfo textInfo = {0, kThemeStateActive, kThemeSystemFont, kHIThemeTextHorizontalFlushLeft, kHIThemeTextVerticalFlushTop, kHIThemeTextBoxOptionStronglyVertical, kHIThemeTextTruncationNone, 0, false}; |
HIThemeDrawTextBox(CFSTR("Using HIThemeDrawTextBox(). This is the new HITheme-compliant text box.\rUsing HIThemeDrawTextBox(). This is the new HITheme-compliant text box.\rUsing HIThemeDrawTextBox(). This is the new HITheme-compliant text box.\rUsing HIThemeDrawTextBox(). This is the new HITheme-compliant text box.\rUsing HIThemeDrawTextBox(). This is the new HITheme-compliant text box.\rUsing HIThemeDrawTextBox(). This is the new HITheme-compliant text box.\r"), &hiBounds, &textInfo, context, kHIThemeOrientationNormal); |
CGContextStrokeRect(context, hiBounds); |
return status; |
} |
void DoNewWindow(void) |
{ |
WindowRef theWind, theCWind; |
// creating the non-compositing window first |
Rect bounds = {50, 10, 710, 610}; |
OSStatus theStatus = CreateNewWindow( |
kDocumentWindowClass, |
kWindowStandardFloatingAttributes | kWindowStandardHandlerAttribute, |
&bounds, &theWind); |
if ((theStatus != noErr) || (theWind == NULL)) {DebugStr("\pCreateNewWindow failed!"); return;} |
SetWindowTitleWithCFString(theWind, CFSTR("HITextShowcase: Non-Compositing Window")); |
// let's create our picture background |
EventTypeSpec winEvent = {kEventClassWindow, kEventWindowDrawContent}; |
InstallEventHandler(GetWindowEventTarget(theWind), NewEventHandlerUPP(WindowDrawingTextOverPictureHandler), 1, &winEvent, NULL, NULL); |
ControlRef outControl; |
Rect itemRect = {20, 20, 35, 580}; |
theStatus = CreateStaticTextControl(theWind, &itemRect, CFSTR("Using CreateStaticTextControl(). This is a static text control (CDEF 18)"), NULL, &outControl); |
OffsetRect(&itemRect, 0, 40); |
theStatus = CreateEditTextControl(theWind, &itemRect, CFSTR("Using CreateEditTextControl(). This is an edit text control (CDEF 17)"), false, false, NULL, &outControl); |
CFStringRef theUnicodeString = CFStringCreateWithCString(NULL, "Using CreateEditUnicodeTextControl(). This is an Unicode edit text control: ", kCFStringEncodingMacRoman); |
CFMutableStringRef theMutableString = CFStringCreateMutableCopy(NULL, 0, theUnicodeString); |
CFRelease(theUnicodeString); |
UniChar japaneseChars[] = { 0x3053, 0x308c, 0x308f, 0x65e5, 0x672c, 0x8a9e, 0x3067, 0x3059, 0x3002 }; |
CFStringAppendCharacters(theMutableString, japaneseChars, 9); |
OffsetRect(&itemRect, 0, 40); |
theStatus = CreateEditUnicodeTextControl(theWind, &itemRect, theMutableString, false, NULL, &outControl); |
OffsetRect(&itemRect, 0, 40); itemRect.bottom += 80; |
theStatus = CreateScrollingTextBoxControl(theWind, &itemRect, 256, false, 0, 0, 0, &outControl); |
// creating the compositing window second |
OffsetRect(&bounds, 650, 0); bounds.bottom += 95; |
theStatus = CreateNewWindow( |
kDocumentWindowClass, |
kWindowStandardFloatingAttributes | kWindowStandardHandlerAttribute | kWindowCompositingAttribute, |
&bounds, &theCWind); |
if ((theStatus != noErr) || (theCWind == NULL)) {DebugStr("\pCreateNewWindow failed!"); return;} |
SetWindowTitleWithCFString(theCWind, CFSTR("HITextShowcase: Compositing Window")); |
// let's create our picture background as a control since we can in a compositing window |
ControlButtonContentInfo content; |
content.contentType = kControlContentPictRes; |
content.u.resID = 128; |
OffsetRect(&bounds, -bounds.left, -bounds.top); |
theStatus = CreatePictureControl(theCWind, &bounds, &content, true, &outControl); |
EventTypeSpec ctlEvent = {kEventClassControl, kEventControlDraw}; |
InstallEventHandler(GetControlEventTarget(outControl), NewEventHandlerUPP(ControlDrawingTextOverPictureHandler), 1, &ctlEvent, NULL, NULL); |
ShowControl(outControl); |
itemRect.top = 20; itemRect.left = 20; itemRect.bottom = 35; itemRect.right = 580; |
theStatus = CreateStaticTextControl(theCWind, &itemRect, CFSTR("Using CreateStaticTextControl(). This is a static text control (CDEF 18)"), NULL, &outControl); |
OffsetRect(&itemRect, 0, 40); |
theStatus = CreateEditTextControl(theCWind, &itemRect, CFSTR("Using CreateEditTextControl(). This is an edit text control (CDEF 17)"), false, false, NULL, &outControl); |
OffsetRect(&itemRect, 0, 40); |
theStatus = CreateEditUnicodeTextControl(theCWind, &itemRect, theMutableString, false, NULL, &outControl); |
CFRelease(theMutableString); |
HIViewRef contentView; |
HIViewFindByID(HIViewGetRoot(theCWind), kHIViewWindowContentID, &contentView); |
CFStringRef theStrings[] = { CFSTR("item 1"), CFSTR("item 2"), CFSTR("item 3"), CFSTR("item 4") }; |
CFArrayRef theListArray = CFArrayCreate(NULL, (const void **)theStrings, 4, &kCFTypeArrayCallBacks); |
HIRect hiBounds = { { itemRect.left, itemRect.top + 40 }, { itemRect.right - itemRect.left, itemRect.bottom - itemRect.top } }; |
HIComboBoxCreate(&hiBounds, CFSTR("Using HIComboBoxCreate(). This is a combo box HIView"), NULL, theListArray, kHIComboBoxAutoSizeListAttribute, &outControl); |
CFRelease(theListArray); |
HIViewAddSubview(contentView, outControl); |
HIViewSetVisible(outControl, true); |
hiBounds = CGRectOffset(hiBounds, 0.0, 40.0); |
// we set up a dummy menu so that we can see the search icon |
MenuRef dummyMenu = GetMenu(129); |
HISearchFieldCreate(&hiBounds, kHISearchFieldAttributesCancel, dummyMenu, CFSTR("Using HISearchFieldCreate(). This is a search field HIView"), &outControl); |
HIViewAddSubview(contentView, outControl); |
HIViewSetVisible(outControl, true); |
ShowWindow(theCWind); |
ShowWindow(theWind); |
} |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-10-27