Women in Technology

Hear us Roar



Article:
  Creating Toolbars for Mac OS X
Subject:   Setting toolbars
Date:   2002-03-20 13:00:38
From:   joaquimlopes
I always read the Programming With Cocoa articles with interest and have learned a few things with them. However I find the method for setting the toolbars that was presented to be very convoluted (too many ifs). For my current project, a students management application, I came up with what I believe is a simpler way. Here it is:


- (NSToolbarItem *)toolbar: (NSToolbar *)toolbar itemForItemIdentifier: (NSString *)itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted
{
// Required delegate method Given an item identifier, self method returns an item
// The toolbar will use self method to obtain toolbar items that can be displayed in the customization sheet, or in the toolbar itself
NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc] initWithItemIdentifier: itemIdent] autorelease];
/* The MEDocumentManager is an object that I created that does many things, in one of them it reads a plist
called Ferramentas (the portuguese word for tools) that is a dictionary of dictionaries where the item
identifier for tool is the key, like this (this is an example for a tool called Actualizar that uses an image
called - what else - Actualizar, and has a Tag - 9 -):
<dict>
<key>Actualizar</key>
<dict>
<key>Image</key>
<string>Actualizar</string>
<key>Label</key>
<string>Actualizar</string>
<key>Tag</key>
<integer>9</integer>
<key>Tooltip</key>
<string>Actualiza os dados</string>
</dict>
</dict>
*/
NSDictionary *tools = [[MEDocumentManager sharedDocumentManager] toolsDictionary: itemIdent];


if(tools == nil) // Just checking, for sanity sake
{
toolbarItem = nil;
}
else
{
// Set the text label to be displayed in the toolbar and customization palette
[toolbarItem setLabel: [tools objectForKey:@"Label"]];
[toolbarItem setPaletteLabel: [tools objectForKey:@"Label"]];


// Set up a reasonable tooltip, and image
[toolbarItem setToolTip: [tools objectForKey:@"Tooltip"]];
[toolbarItem setImage: [NSImage imageNamed: [tools objectForKey:@"Image"]]];


// Tell the item what message to send when it is clicked
[toolbarItem setTarget: self];
[toolbarItem setAction: @selector(toolbarItemAction:)];
[toolbarItem setTag: [[tools objectForKey:@"Tag"] intValue]];
}
return toolbarItem;
}


The toolbarItemAction method then just looks like this:
- (void)toolbarItemAction: (id)item
{
switch([item tag])
{
case whateverTag:
[self doWhatever];
break;
// Other cases...
}
}


And the validateToolbarItem method looks like this


- (BOOL) validateToolbarItem: (NSToolbarItem *) toolbarItem
{
// Optional method self message is sent to us since we are the target of some toolbar item actions
// (for example: of the save items action)
BOOL enable = NO;

switch([toolbarItem tag])
{
case whateverTag:
enable = YES;
break;
// Other cases...
}
return enable;
}


I hope someone finds this helpful. At least for me this has saved me a lot of time and has enabled me to change a few things without changing a line of code.


Joaquim

Full Threads Oldest First

Showing messages 1 through 2 of 2.

  • Michael Beam photo Setting toolbars
    2002-03-25 17:36:12  Michael Beam | O'Reilly Author [View]

    Yeah, i agree that when you have a large number of items that some other management scheme would be appropriate than the if statements. I picked the method i did just to remove a layer of complexity, that is, i would have had to define the toolbar items somewhere. But as you pointed out, there's more than one way of doing it.

    Mike
    • Setting toolbars
      2006-07-27 04:54:49  sajid_hayat [View]

      Problem one
      Rotation

      I am trying to add some feature to the Sketch demo that will give it the ability to rotate object . I am having trouble in getting it to display the rotations correctly. When trying to draw the rotation I use an fineTransform and set just the rotateByDegrees: method. Doing this only seems to transform the coordinates within the bounds of the graphic, so it is rotation within the bounds, instead of rotating the bounds.


      Problem two
      Pencil
      we are not able to add pencil feature into sketch demo.