# # ToolbarHelper.py # SoundFarmer # # Created by Donovan Preston on 8/26/04. # Copyright (c) 2004 Ulaluma. All rights reserved. # from objc import YES, NO import AppKit from Foundation import NSObject class ToolbarHelper(NSObject): def initWithToolbarDescriptionFile_toolbarIdentifier_window_target_( self, csv, toolbarIdentifier, window, target): """Initialize a toolbar given a text file containing a description of the buttons in it. The first line of the file is a comma-separated list of identifiers on the default toolbar. The rest of the lines are comma-separated lists of: identifier,label,pallete label,tooltip,action,imageName toolbarIdentifier: A string identifying this toolbar. window: The window to attach the toolbar to. target: The target of toolbar actions. """ self = self.init() lines = [x.split(',') for x in [x.strip() for x in open(csv).readlines()] if x and not x.startswith('#')] self._toolbarDefaultItemIdentifiers = lines[0] self._toolbarItems = {} toolbar = AppKit.NSToolbar.alloc().initWithIdentifier_(toolbarIdentifier) toolbar.setDelegate_(self) toolbar.setAllowsUserCustomization_(YES) toolbar.setAutosavesConfiguration_(YES) for (identifier, label, paletteLabel, toolTip, action, image) in lines[1:]: self._toolbarItems[identifier] = toolbarItem = AppKit.NSToolbarItem.alloc().initWithItemIdentifier_( identifier) toolbarItem.setLabel_(label) toolbarItem.setPaletteLabel_(paletteLabel) toolbarItem.setToolTip_(toolTip) toolbarItem.setTarget_(target) toolbarItem.setAction_(action) toolbarItem.setImage_(AppKit.NSImage.imageNamed_(image)) self.window = window window.setToolbar_(toolbar) return self def toolbarDefaultItemIdentifiers_(self, anIdentifier): """Return a list of identifiers which are present in the uncustomized toolbar. """ return self._toolbarDefaultItemIdentifiers + [ AppKit.NSToolbarFlexibleSpaceItemIdentifier, AppKit.NSToolbarCustomizeToolbarItemIdentifier] def toolbarAllowedItemIdentifiers_(self, anIdentifier): """Return a list of all allowed identifiers. """ return self._toolbarItems.keys() + [AppKit.NSToolbarSeparatorItemIdentifier, AppKit.NSToolbarSpaceItemIdentifier, AppKit.NSToolbarFlexibleSpaceItemIdentifier, AppKit.NSToolbarCustomizeToolbarItemIdentifier] def toolbarSelectableItemIdentifiers_(self, anIdentifier): """Return a list of "selectable" identifiers, which will press and remain highlighted instead of acting like a button. """ return [] def toolbar_itemForItemIdentifier_willBeInsertedIntoToolbar_( self, toolbar, itemIdentifier, flag): """Return an NSToolbarItem for the given toolbar and item identifier. """ return self._toolbarItems[itemIdentifier]