Hello,

I am having a problem with toolbars. I have created a delegate class
for my toolbar, but when I added the
toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar: method it
crashes:

Exception:  EXC_BREAKPOINT (0x0006)
Code[0]:    0x00000002
Code[1]:    0x00000000


Thread 0 Crashed:
0   com.apple.Foundation        0x9282a45f _NSRaiseError + 227
1   com.apple.Foundation        0x92851247 +[NSException raise:format:] + 57
2   com.apple.Foundation        0x9285108c _NSArrayRaiseInsertNilException + 75
3   com.apple.Foundation        0x927f47fc -[NSCFArray insertObject:atIndex:] + 
125
4   com.apple.AppKit            0x933c5279 -[NSToolbar
_forceInsertItem:atIndex:] + 364
5   com.apple.AppKit            0x933c4df2 -[NSToolbar
_insertNewItemWithItemIdentifier:atIndex:notifyDelegate:notifyView:notifyFamilyAndUpdateDefaults:]
+ 152
6   com.apple.AppKit            0x933c4d52 -[NSToolbar
_appendNewItemWithItemIdentifier:notifyDelegate:notifyView:notifyFamilyAndUpdateDefaults:]
+ 97
7   com.apple.AppKit            0x933c4b94 -[NSToolbar
_setCurrentItemsToItemIdentifiers:notifyDelegate:notifyView:notifyFamilyAndUpdateDefaults:]
+ 335
8   com.apple.AppKit            0x933c43a4 -[NSToolbar
_loadFromUDIfNecessary] + 231
9   com.apple.AppKit            0x933c3b36 -[NSToolbar
_setToolbarViewWindow:] + 190
10  com.apple.AppKit            0x933c38ca -[NSWindow setToolbar:] + 395
11  org.magnifier.magnifier     0x00037c97
APPKIT_NSWINDOW_$__SETTOOLBAR$POBJC_OBJECT + 71 (NSWindow.inc:2205)
12  org.magnifier.magnifier     0x0003e315
VIEW_TMYVIEW_$__CREATEUSERINTERFACE + 453 (view.pas:74)
13  org.magnifier.magnifier     0x000020fd PASCALMAIN + 173 (texteditor.pas:46)
14  org.magnifier.magnifier     0x0001be85 FPC_SYSTEMMAIN + 69
15  org.magnifier.magnifier     0x00002032 _start + 216
16  org.magnifier.magnifier     0x00001f59 start + 41


I am using this string as the type encoding for my delegate method,
and this declaration:
@@:@@L

    class function toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar
      (_self: objc.id; _cmd: SEL; toolbar: objc.id;
      itemIdentifier: CFStringRef; flag: CBOOL): objc.id; cdecl;// static;

I suppose that the bool used in the routine will be mapped to a long,
is that it? What is the adequate type encoding for a bool in
Objective-C? The docs says nothing about this.

Here is my code:

constructor TMyToolbarController.Create;
begin
  { The class is registered on the Objective-C runtime before the
NSObject constructor is called }
  if not CreateClassDefinition(ClassName(), Str_NSObject) then
WriteLn('Failed to create objc class ' + ClassName());

  inherited Create;

  { Prepare CFStringRefs for the constants }

  OpenToolbarItemIdentifier := CFStringCreateWithPascalString(nil,
'OpenID', kCFStringEncodingUTF8);
  SaveToolbarItemIdentifier := CFStringCreateWithPascalString(nil,
'SaveID', kCFStringEncodingUTF8);
  CloseToolbarItemIdentifier := CFStringCreateWithPascalString(nil,
'CloseID', kCFStringEncodingUTF8);
end;

procedure TMyToolbarController.AddMethods;
begin
  AddMethod(Str_toolbarAllowedItemIdentifiers, '@@:@',
Pointer(toolbarAllowedItemIdentifiers));
  AddMethod(Str_toolbarDefaultItemIdentifiers, '@@:@',
Pointer(toolbarDefaultItemIdentifiers));
  AddMethod(Str_toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar,
   '@@:@@L', Pointer(toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar));
end;

class function TMyToolbarController.toolbarAllowedItemIdentifiers(_self:
objc.id;
  _cmd: SEL; toolbar: objc.id): CFArrayRef; cdecl;
var
  toolbarItems: array[0..3] of CFStringRef;
begin
  toolbarItems[0] := OpenToolbarItemIdentifier;
  toolbarItems[1] := SaveToolbarItemIdentifier;
  toolbarItems[2] := CloseToolbarItemIdentifier;
  toolbarItems[3] := nil;

  Result := CFArrayCreate(nil, @toolbarItems[0], 4, nil);
end;

class function TMyToolbarController.toolbarDefaultItemIdentifiers(_self:
objc.id;
  _cmd: SEL; toolbar: objc.id): CFArrayRef; cdecl;
begin
  Result := toolbarAllowedItemIdentifiers(_self, _cmd, toolbar);
end;

class function 
TMyToolbarController.toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar(_self:
objc.id;
  _cmd: SEL; toolbar: objc.id {NSToolbar}; itemIdentifier: CFStringRef;
  flag: CBOOL): objc.id {NSToolbarItem}; cdecl;
var
  toolbarItem: NSToolbarItem;
begin
  if CFStringCompare(itemIdentifier, OpenToolbarItemIdentifier,
kCFCompareCaseInsensitive) = kCFCompareEqualTo then
  begin
    toolbarItem := NSToolbarItem.initWithItemIdentifier(itemIdentifier);
//    [toolbarItem setLabel:@"Save"];
//   [toolbarItem setPaletteLabel:[toolbarItem label]];
//    [toolbarItem setToolTip:@"Save Your Passwords"];}
    toolbarItem.setImage(myModel.imgOpen.Handle);
    toolbarItem.setTarget(myController.Handle);
    toolbarItem.setAction(sel_registerName(PChar('doOpenFile:')));
  end
  else if CFStringCompare(itemIdentifier, SaveToolbarItemIdentifier,
kCFCompareCaseInsensitive) = kCFCompareEqualTo then
  begin
    toolbarItem := NSToolbarItem.initWithItemIdentifier(itemIdentifier);

    toolbarItem.setImage(myModel.imgSave.Handle);
    toolbarItem.setTarget(myController.Handle);
    toolbarItem.setAction(sel_registerName(PChar('doSaveFile:')));
  end
  else if CFStringCompare(itemIdentifier, CloseToolbarItemIdentifier,
kCFCompareCaseInsensitive) = kCFCompareEqualTo then
  begin
    toolbarItem := NSToolbarItem.initWithItemIdentifier(itemIdentifier);

    toolbarItem.setImage(myModel.imgClose.Handle);
    toolbarItem.setTarget(myController.Handle);
    toolbarItem.setAction(sel_registerName(PChar('doCloseFile:')));
  end
  else
  begin
    Result := nil;
    Exit
  end;

  Result := ToolbarItem.autorelease;
{  ToolbarItem.Handle := 0;
  ToolbarItem.Free;}
//    Result := Result. [toolbarItem autorelease];
end;


thanks,
-- 
Felipe Monteiro de Carvalho
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to