objective c - Validating fonts and colors NSToolbarItem items -
using cocoa latest sdk on osx 10.6.6
i have nstoolbar custom toolbar items , built in fonts , colors nstoolbaritem items (nstoolbarshowfontsitem , nstoolbarshowcolorsitem identifiers).
i need able enable/disable in various situations. problem validatetoolbaritem:
never called these items (it being called other toolbar items).
the documentation not clear this:
the toolbar automatically takes care of darkening image item when clicked , fading when disabled. code has validate item. if image item has valid target/action pair, toolbar call nstoolbaritemvalidation’s validatetoolbaritem: on target if target implements it; otherwise item enabled default.
i don't explicitly set target/action these 2 toolbar items, want use default behavior. mean can't validate these items? or there other way can this?
thanks.
after trial , error, think able figure out , find reasonable workaround. post quick answer here future reference others facing same problem.
this 1 more of cocoa's design flaws. nstoolbar has hardcoded behavior set target/action nstoolbarshowfontsitem , nstoolbarshowcolorsitem nsapplication documentation hints never invoke validatetoolbaritem:
these nstoolbaritem items.
if need toolbar items validated, trivial thing not use default fonts/colors toolbar items roll own, calling same nsapplication actions (see below).
if using default ones, possible redirect target/action of them object , invoke original actions
- (void) toolbarwilladditem:(nsnotification *)notification { nstoolbaritem *addeditem = [[notification userinfo] objectforkey: @"item"]; if([[addeditem itemidentifier] isequal: nstoolbarshowfontsitemidentifier]) { [addeditem settarget:self]; [addeditem setaction:@selector(toolbaropenfontpanel:)]; } else if ([[addeditem itemidentifier] isequal: nstoolbarshowcolorsitemidentifier]) { [addeditem settarget:self]; [addeditem setaction:@selector(toolbaropencolorpanel:)]; } }
now validatetoolbaritem:
called:
- (bool)validatetoolbaritem:(nstoolbaritem *)theitem { //validate item here }
and here actions invoked:
-(ibaction)toolbaropenfontpanel:(id)sender { [nsapp orderfrontfontpanel:sender]; } -(ibaction)toolbaropencolorpanel:(id)sender { [nsapp orderfrontcolorpanel:sender]; }
i guess engineers designed never thought 1 want validate fonts/colors toolbar items. go figure.
Comments
Post a Comment