Re: Question on Dynamic with "DockedCell"
- To: mathgroup at smc.vnet.net
- Subject: [mg104173] Re: Question on Dynamic with "DockedCell"
- From: David Reiss <dbreiss at gmail.com>
- Date: Wed, 21 Oct 2009 06:33:55 -0400 (EDT)
- References: <hbjtn3$606$1@smc.vnet.net>
Hi Jason, I have run into this as well when I wanted to create a menu in a DockedCell that displayed the current set of files in a particular directory. One issue is that one has to trigger any Dynamic that is in the cell, and for some cases one doesn't want something constantly testing the cou=mputer's filesystem for changes. I had played with Refresh and things like that. A solution that I came up with, that served my particular application needs was to create a reason for a menu to update. So, my menu was generated through a function likethe following. Note that it has an argument, but the parameter in the arrument is not used in the funtion: myMenu[z_] := ActionMenu["Open...", FileNameTake[#, -1] :> SystemOpen[#] & /@ FileNames[{"*"}, {$UserDocumentsDirectory}]] Note that if you create a cell with this menu in it it will not update if you add or remove a file in $UserDocumentsDirector: TextCell[Dynamic[myMenu[1]], "Text"] // CellPrint The trick now is to introduce a new function which has as its only purpose to change the value of a parameter: $MysteryParameter. Make sure that that parameter is Unprotected if it is in a Package. $MysteryParameter will be used in the argument to myMenu to force it to trigger the Dynamic. And this will be acomplished by having the Cell that contains the menu have a CellEventAction. $MysteryParameter = 1; Toggle$MysteryParameter[] := Switch[$MysteryParameter, 1, $MysteryParameter = 2, 2, $MysteryParameter = 1, _, $MysteryParameter = 1]; And finally one intruduces a CellEventAction to the cell that causes the value of $MysteryParameter to change and thereby causes the myMenu in the Dynamic to update because its argument has changed. TextCell[Dynamic[myMenu[$MysteryParameter]], "Text", CellEventActions -> {"MouseMoved" :> (Toggle$MysteryParameter [])}] // \ CellPrint Or, in a DockedCell SetOptions[EvaluationNotebook[], DockedCells -> { MakeBoxes@ TextCell[Dynamic[myMenu[$MysteryParameter]], "DockedCell", CellEventActions -> {"MouseMoved" :> (Toggle$MysteryParameter [])}, StripOnInput -> True]} ] I hope this helps.... --David http://scientificarts.com/worklife On Oct 20, 4:51 am, Jason Ledbetter <jasonbr... at gmail.com> wrote: > Folk, > I'm attempting to create a dynamically-appearing set of choices/menus usi= ng > ButtonBar in a notebook window but I've been unable to get the desired > affect to date. I've cobbled together the following code which almost doe= s > what I'm looking for but I could use some pointers in getting the rest of > the way. > > The goal here is for the "topMenu" items to appear when the notebook is > first instantiated and for the "secondMenu" option to appear when a > "topMenu" option is selected. As it stands now, I'm having to start with = the > "secondMenu" option pre-defined to a bogus value. > > If I were to abstract my goal some more, I'm trying to figure out how to > dynamically add/remove RowBox items that include ButtonBars from the > notebook based on options that are selected. > > I'm guessing that I somehow need to programmatically generate the content= s > of a Dynamically wrapped RowBox (e.g., Dynamic[RowBox[...]]) but I'm not > quite sure how to go about that just yet. > > Any help is greatly appreciated... > > Here's what I have thus far: > > --snip-- > Module[ > > {topMenu, secondMenu}, > > topMenu = {"option1", "option2"}; > secondMenu = {"No option selected"}; > > menuChoice[choice_] := Module[{}, > Switch[choice, > "option1", secondMenu = {"one", "selected"}, > "option2", secondMenu = {"two", "selected"}, _, Print[choice= ] > ]]; > > CreateWindow[DockedCells -> Cell[ > RowBox[ > { > ToBoxes[ > ButtonBar[ToString[#] :> menuChoice[#] & /@ topMenu] > ], > ToBoxes[ > Dynamic[ > ButtonBar[ToString[#] :> menuChoice[#] & /@ secondMenu= ] > ] > ] > } > ], > "DockedCell"], > TextAlignment -> Center > ] > ]; > > --snip-- > > thanks, > > -jbl