scide: LookupDialog - redo lookup on classes after partial lookup
[supercollider.git] / SCClassLibrary / Common / GUI / guicrucial / ObjectGui.sc
blobcdef07eb09d6b3da711d28ddfbeba6e0e24f3c12
3 ObjectGui : SCViewHolder {
4         /*
5                 this is a controller class:
6                         it creates the views and implements the relationship between them and the model
7                 model: the object for which this is a graphical user interface
8                 view: this.view is the flow view (aka the arg layout) that is passed to guiBody
9                         individual views/widgets are placed in it and their actions talk to either this object or the model
10         */
11         var <model,<dragSource;
13         guiBody { arg layout,bounds ... args;
14                 /* implement this method in your gui subclass */
16                 // if your model implement guiBody then call that
17                 // this is a lazy way to write simple guis for simple objects
18                 // where model/gui code separation is not especially important
19                 // and where the controller class doesn't need to maintain any state or vars
20                 if(model.respondsTo(\guiBody) and: {model.isKindOf(ObjectGui).not},{
21                         model.guiBody(layout,bounds,*args)
22                 })
23         }
25         *new { arg model;
26                 var new;
27                 new = super.new;
28                 new.model_(model);
29                 ^new
30         }
32         gui { arg parent, bounds ... args;
33                 var layout;
34                 layout=this.guify(parent,bounds);
35                 layout.flow({ arg layout;
36                         this.view = layout;
37                         this.writeName(layout);
38                         this.performList(\guiBody,[layout,bounds] ++ args);
39                 },bounds).background_(this.background);
40                 //if you created it, front it
41                 if(parent.isNil,{
42                         layout.resizeToFit(true,true);
43                         layout.front;
44                 });
45         }
46         guify { arg parent,bounds,title;
47                 // converts the parent to a FlowView or compatible object
48                 // thus creating a window from nil if needed
49                 // registers to remove self as dependent on model if window closes
50                 if(bounds.notNil,{
51                         bounds = bounds.asRect;
52                 });
53                 if(parent.isNil,{
54             parent = PageLayout(title ?? {model.asString.copyRange(0,50)},bounds,front:false);
55                 },{
56                         parent = parent.asPageLayout(bounds);
57                 });
58                 // i am not really a view in the hierarchy
59                 parent.removeOnClose(this);
60                 ^parent
61         }
62         model_ { |newModel|
63                 if(model.notNil,{ // a view has its model swapped with another
64                         model.removeDependant(this);
65                         model = newModel;
66                         model.addDependant(this);
67                         this.update;
68                 },{
69                         model = newModel;
70                         model.addDependant(this);
71                 })
72         }
73         viewDidClose {
74                 model.removeDependant(this);
75                 model = nil;
76                 super.viewDidClose;
77         }
79         background { ^Color.clear }
81         writeName { |layout|
82                 this.prWriteName(layout,model.asString)
83         }
84         prWriteName { arg layout,name;
85                 var string,font;
86                 font = GUI.font.new(*GUI.skin.fontSpecs);
87                 string = " " ++ (name);
88                 if(string.size > 40,{
89                         string = string.copyRange(0,40) ++ "...";
90                 });
91                 dragSource = GUI.dragSource.new(layout,Rect(0,0,(string.bounds(font).width + 10).max(70),GUI.skin.buttonHeight))
92                         .stringColor_(Color.new255(70, 130, 200))
93                         .font_(font)
94                         .background_(Color.white)
95                         .align_(\left)
96                         .beginDragAction_({ model })
97                         .object_(string);
98         }