Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Background: Consider the following ( for the purpose of illustrating this question ) simplified, but working snippet of code.

 TabView[
  {
   {patt, "Pattern" -> 1},
   {motif, "Motif" -> Column[{
      Button["Type", Print[" NOT IMPLEMENTED YET"], ImageSize -> 100],
      Button["New shape", Print[" NOT IMPLEMENTED YET"], ImageSize -> 100],
      Button["Pixel", Print[" NOT IMPLEMENTED YET"], ImageSize -> 100]}]}
  },
  Dynamic[tab]]

I have a GUI in a Module / Manipulate construction which contains a tabview that displays several views of the same data on its tabs. Each tab contains a series of buttons to further specify the particular view on that tab. Each tab consists several buttons and other controls, i.e. setterbars, 2D sliders. After each click or other action on a control the data is modified and all views are recalculated and displayed on the tabs.

The issue is that after each tab the tabview always returns to page 1. I have already made accomodations by using the following format of TabView:

  TabView[{ 
   {value1, "Tabname"->CodeOnTab1}, 
   {valueN, "Tabname"->CodeOnTabn} }, 
  Dynamic[VAR]

As I understand the Tabview opens tabN if VAR is set to valueN. This leads to the following question: what is the best =coding strategy= to make a TabView aware of the tab it should display ( open ) when it is re-displayed ( considering the last user action ) ? The goal / criteria is: minimal code use.

share|improve this question
    
From a quick test, it already does that with your code. Did you possibly use for VAR a variable which goes out of scope? –  celtschk May 15 '12 at 10:44
    
The code above is merely an illustration. The GUI part of the program ( currently ) consists of four tabs with about 50 controls. I want to make sure that I implement the best strategy I can think of. All that GUI code is a sort of overhead in relation to the real problem I am trying to solve. - The best thing I can think of is to set the value of VAR after each action, that adds another 50 ( probably more ) lines of code. –  ndroock1 May 15 '12 at 10:56
    
Ideally I would need a TabView that can trigger the execution of a function on entering a tab. See my dilemma. There are many alternatives to investigate. –  ndroock1 May 15 '12 at 11:03
2  
Your example does not seem to really illustrate the problem you are having. One general advice: given that your goal is to build presumably complex GUI, and that you are (rightly) concerned about code duplication / bloating, I would use code generation (something I already suggested many times). It is particularly well suited for UI programming, given that the final result is always an (often rather complex) Mathematica expression, and also that the nature of Mathematica UI programming makes it hard to modularize the controls by other means. –  Leonid Shifrin May 15 '12 at 11:22
1  
For one example of a (rather non-trivial) custom control coded using similar techniques (although still not as powerful as code generation - I used higher-order functions / closures), you may look at my implementation of nested menus –  Leonid Shifrin May 15 '12 at 11:26
show 4 more comments

2 Answers 2

up vote 6 down vote accepted

I may have misunderstood your problem, but it looks like you just need to create a persistent local variable keeping the value of the tab which was last open. One way to do this:

DynamicModule[{tab},
   myCustomTab[] :=
      TabView[
       {
          {patt, "Pattern" -> 1},
          {motif, "Motif" -> 
             Column[{
               Button["Type", Print[" NOT IMPLEMENTED YET"], ImageSize -> 100],
               Button["New shape", Print[" NOT IMPLEMENTED YET"], ImageSize -> 100],
               Button["Pixel", Print[" NOT IMPLEMENTED YET"], ImageSize -> 100]}]}
       },
       Dynamic[tab]]
]

What matters is that you create a closure, so the variable tab is not local to the function myCustomTab (in the sense that it is not re-initialized on every function's invocation).

EDIT

Ok, it is probably a good time to explain what I mean by code generation, since I mentioned this technique many times already. Basically, I mean that you will be better off by creating your own DSL for UI. I will illustrate it here simplistically with rule application, but generally I would rather use recursion, which is more powerful. You can notice that your code is full of repeated elements, and this is true even for such a small code snippet. Here is one way to reduce the boilerplate:

This is a function written by @Szabolcs, which will be handy here

ClearAll[withRules]
SetAttributes[withRules, HoldAll]
withRules[rules_, expr_] :=
  First@PreemptProtect@Internal`InheritedBlock[
    {Rule, RuleDelayed},
    SetAttributes[{Rule, RuleDelayed}, HoldFirst];
    Hold[expr] /. rules
]

This is the starting point:

myTab = 
  myTabView[
   {"Pattern" -> 1,
     "Motif" -> Column[{
         myButton["Type", noimpl[]],
         myButton["New shape", noimpl[]],
         myButton["Pixel", noimpl[]]}]
    }];

This is an auxiliary function:

Clear[dressTabView];
dressTabView[lrules_] :=
   t : myTabView[{__Rule}] :>
     DynamicModule[{tab},
        Append[
           Replace[t, e : (label_ -> w_) :> {withRules[lrules, label], e}, 2],
           Dynamic[tab]]];

This is a chain of transformations needed to generate your widget with tab memory:

myTab /. dressTabView[{"Motif" :> motif, "Pattern" :> patt}] /.
    myButton[args__] :> Button[args, ImageSize -> 100] /.
       noimpl[] :> Print[" NOT IMPLEMENTED YET"] /.
          myTabView -> TabView

The main point is, as usual, to separate the specific from the general.

share|improve this answer
    
As I wrote in the comments. The snippet is merely an illustration. Currently I have four tabs with about 50 controls. So if I do something in tab3 everything is re-calculated and I must return to tab 3. Next step I might selected something from a dynamic popupviewmenu in tab 2 and then return there. I don't want to change the tab variable in each control related code but if possible by entering the tab. So my question was what is the best way to do this. Develop a custom Tabcontrol, something else maybe. Am i overlooking something? –  ndroock1 May 15 '12 at 12:35
    
@ndroock1 Please see my edit - I tried to explain my point of view, although this is really a simplistic example. You can do much more powerful things this way. –  Leonid Shifrin May 15 '12 at 13:12
    
Leonid I'll accept the answer, It's not really solved but it's an interesting idea you proposed. I may reformulate this to a better question sometime. My GUI time up. - Btw. Would you know a good place to discuss code in a more general way, i.e. code style, design patterns, etc. May be Mathematica or some other functional or lisp-y language ?? –  ndroock1 May 15 '12 at 13:46
    
@ndroock1 Thanks for accepting, but you don't have to do this if your problem is not solved. OTOH, you could have formualated it better. As for a good place - do you mean discussion forum? A place to discuss things? I am probably the wrong person to ask. And, b.t.w., design patterns are evil (they really were created to work around the limitations of a given language). Code style is also mostly evil I think, as long as it has any social aspect to it. What matters is only power, all the rest is about solving social, rather than technical, problems. –  Leonid Shifrin May 15 '12 at 13:54
add comment

I might be missing the point completely, but you could do something like this

Manipulate[
 TabView[
  {{"patt", "Pattern" -> type},
   {"motif", "Motif" ->
     Grid[{{Column[{
          Button["Line", type = Line, ImageSize -> 100], 
          Button["Bezier", type = BezierCurve, ImageSize -> 100], 
          Button["Point", type = Point, ImageSize -> 100]}],
        Graphics[type[{{0, 0}, {1, 0}, {0, 1}}]]}}]}}, 
  Dynamic[choice]],
 {{type, Line}, None},
 {{choice, "patt"}, None}]

Using Dynamic[choice] in TabView will update choice to the name of the current tab automatically whenever you select a tab so there is no need to update it manually.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.