Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to use Unicode with Carbon



Hi,

I tryed to use this to change the title of a window when a button is
pressed, but it doesn't work.

I am using this string:

const
 MyUTF8String = 'Texto ł ñ ø ß á';

Encoded with UTF-8. I opened the file with TextWrangler to ensure it's
really utf-8

And tryed to set the window title with this code:

function ButtonMessagePressed: OSStatus;
var
 CFString: CFStringRef;
begin
 CFString := CFStringCreateWithCString(nil,
Pointer(PChar(MyUTF8String)), kCFStringEncodingUTF8);

 SetWindowTitleWithCFString(MainWindow, CFString);

 CFRelease(CFString);

 result := 0;
end;

But it doesn't work. I only see 'Texto' and not my entire string. i.e.
the special characters disappeared.

I looked at the apple docs for SetWindowTitleWithCFString, but they
don't say anything special.
http://developer.apple.com/documentation/Carbon/Reference/Window_Manager/Reference/reference.html#//apple_ref/c/func/SetWindowTitleWithCFString

Does anyone have an idea of what is wrong?

Here is a full compilable test program (there are some other things on
it, but they shouldn't interfere):

{
carbontest.pas

*****************************************************************************
*                                                                           *
*  This demonstration program is public domain, which means no copyright,   *
* but also no warranty!                                                     *
*                                                                           *
*  This program is distributed in the hope that it will be useful,          *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
*                                                                           *
*****************************************************************************
}
program carbontest;

{$mode delphi}

uses
SysUtils, FPCMacOSAll, MacPas;

var
 mainWindow: WindowRef;
 contentView: HIViewRef;
 button1, button2: ControlRef;
 staticText: ControlRef;
 showTextFlag: Boolean = false;

const
 kButtonHello = 'HELO';
 kButtonMessage = 'MSGE';
 MyUTF8String = 'Texto ł ñ ø ß á';

{ implementation of the functions }

{ Functions to easely generate carbon structures }

function GetQDRect(Left, Top, Width, Height: Integer): FPCMacOSAll.Rect;
begin
 result.Left := Left;
 result.Top := Top;
 result.Right := Left + Width;
 result.Bottom := Top + Height;
end;

{ Shows a message box }

procedure DoShowMessage(ATitle, AMsg: string);
var
 outItemHit: SInt16;
 err: OSErr;
begin
 err := StandardAlert(kAlertNoteAlert, ATitle, AMsg, nil, outItemHit);
end;

{ Event handling routines }

function ButtonHelloPressed: OSStatus;
begin
 result := 0;
end;

function ButtonMessagePressed: OSStatus;
var
 CFString: CFStringRef;
begin
 CFString := CFStringCreateWithCString(nil,
Pointer(PChar(MyUTF8String)), kCFStringEncodingUTF8);

 SetWindowTitleWithCFString(MainWindow, CFString);

 CFRelease(CFString);

 result := 0;
end;

{ Message handling function }

function WindowCommandHandler(nextHandler: EventHandlerCallRef;
theEvent: EventRef; userDataPtr: UnivPtr): OSStatus;
var
 status: OSStatus;
 ignoreresult: OSStatus;
 aCommand: HICommand;
begin
 status := eventNotHandledErr;

 ignoreresult := GetEventParameter(theEvent, kEventParamDirectObject,
  typeHICommand, nil, sizeof(aCommand), nil, @aCommand);

 if aCommand.commandID = FOUR_CHAR_CODE(kButtonHello) then status :=
ButtonHelloPressed()
 else if aCommand.commandID = FOUR_CHAR_CODE(kButtonMessage) then
status := ButtonMessagePressed();

 result := status;
end;

{ Initialization and finalization routines }

procedure Initialize;
var
 status, ignoreResult: OSStatus;
 cmdEvent: EventTypeSpec;
 eventHandler: EventHandlerUPP;
 fontStyle: ControlFontStyleRec;
begin
 status := CreateNewWindow(kDocumentWindowClass,
  (kWindowStandardDocumentAttributes or kWindowStandardHandlerAttribute
   or kWindowCompositingAttribute),
  GetQDRect(100, 100, 350, 350), mainWindow);

 if (status <> noErr) or (mainWindow = nil) then
 begin
   DoShowMessage('Error', 'CreateNewWindow failed');
   Exit;
 end;

 ignoreResult := SetWindowTitleWithCFString(mainWindow,
CFSTRP('Carbon FPC Controls Demo'));

 ignoreResult := HIViewFindByID(HIViewGetRoot(mainWindow),
kHIViewWindowContentID, contentView);

 { Add events }

 cmdEvent.eventClass := kEventClassCommand;
 cmdEvent.eventKind := kEventCommandProcess;
 eventHandler := NewEventHandlerUPP(@WindowCommandHandler);
 ignoreResult := InstallEventHandler(GetWindowEventTarget(mainWindow),
  eventHandler, 1, @cmdEvent, nil, nil);

 { Creates the hello button }

 ignoreResult := CreatePushButtonControl(nil, GetQDRect(50, 200, 100, 50),
  CFSTRP('Hello Button'), button1);

 ignoreResult := HIViewAddSubview(contentView, button1);
 ignoreResult := SetControlCommandID(button1, FOUR_CHAR_CODE(kButtonHello));
 ignoreResult := HIViewSetVisible(button1, TRUE);

 { Creates the set text button }

 ignoreResult := CreatePushButtonControl(nil, GetQDRect(200, 200, 125, 50),
  CFSTRP('Set Form Title'), button2);

 ignoreResult := HIViewAddSubview(contentView, button2);
 ignoreResult := SetControlCommandID(button2, FOUR_CHAR_CODE(kButtonMessage));
 ignoreResult := HIViewSetVisible(button2, TRUE);

 { Creates the text control }

 fontStyle.flags := kControlUseJustMask or kControlUseSizeMask;
 fontStyle.just := teCenter;
 fontStyle.size := 30;

 ignoreResult := CreateStaticTextControl(mainWindow,
  GetQDRect(0, 50, 350, 50), nil, @fontStyle, staticText);

 ignoreResult := HIViewAddSubview(contentView, staticText);
 ignoreResult := HIViewSetVisible(staticText, FALSE);

 HIViewSetText(staticText, CFSTRP('Hello Controls!'));

 { Shows the window }

 ShowWindow(mainWindow);
end;

procedure DoCloseWindow(theWind: WindowRef);
var
 theEvent: EventRef;
begin
 CreateEvent(nil, kEventClassWindow, kEventWindowClose,
GetCurrentEventTime, kEventAttributeNone, theEvent);
 SetEventParameter(theEvent, kEventParamDirectObject, typeWindowRef,
sizeof(WindowRef), theWind);
 SendEventToEventTarget(theEvent, GetWindowEventTarget(theWind));
end;

{ Closes all windows, so they have time to save any user data (none in
this case) }

procedure Finalize;
begin
 DoCloseWindow(mainWindow);
end;

{ Main program section }

begin
 Initialize();

 RunApplicationEventLoop();

 Finalize();
end.
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden

References: 
 >How to use Unicode with Carbon (From: "Felipe Monteiro de Carvalho" <email@hidden>)
 >Re: How to use Unicode with Carbon (From: David Duncan <email@hidden>)
 >Re: How to use Unicode with Carbon (From: "Felipe Monteiro de Carvalho" <email@hidden>)
 >Re: How to use Unicode with Carbon (From: Jon Guyer <email@hidden>)
 >Re: How to use Unicode with Carbon (From: "Felipe Monteiro de Carvalho" <email@hidden>)
 >Re: How to use Unicode with Carbon (From: Eric Schlegel <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.