GUI Overview and Introduction

See also: GUI-Classes, GUI, ViewRedirect.

SuperCollider provides for using different gui kits, and also provides syntax for transparently writing kit and platform independent code. Two main classes control this mechnanism: the gui factory abstraction class, GUI, and the subclasses of ViewRedirect, which transparently uses the GUI class to redirect to the kit-specific gui class.

For a table of all gui classes and their equivalents, see GUI-Classes.

For information on switching gui kits, see the documentation for GUI.

Basic usage: Kits and Syntax

w=Window("Test Window").front; // Window is a subclass of ViewRedirect

w.postln // w now contains the class appropriate to the gui kit

Kit-dependant gui classes have a prefix. For example, to make a window, the CocoaGUI kit uses SCWindow, or the swing kit uses JSCWindow. It is generally discouraged to use the kit-dependant classes directly. Instead, you simply use the subclasses of ViewRedirect. In most cases this involves simply using the class base name without any prefix, like in the example above. Behind the scenes, the above technique actually automatically redirects to the appropriate GUI class method. For a table of all gui classes and their kit-dependent equivalents, see GUI-Classes.

Depending on the curent GUI.scheme the example above is equivalent to writing, for example:

SCWindow("Test Window").front; // cocoa version


JSCWindow("Test Window").front; // swing version

Basic Usage: Windows, Containers and Decorators


// Window returns the window class for the current kit

w = Window( "my name is...", Rect( 128, 64, 340, 360 ));

w.view.decorator = FlowLayout( w.view.bounds ); // comment this out for no decorator

// w.addFlowLayout // you can use this instead of the above line for brevity.

w.view.background = Color( 0.6, 0.8, 0.8 );{ arg i;

// Here Button returns the button class for the current kit

b = Button( w, Rect( rrand( 20, 300 ), rrand( 20, 300 ), 75, 24 ));

b.states = [[ "Start " ++ i,, Color.rand ],

[ "Stop " ++ i, Color.white, ]];




w.postln //prints the window instance for the current gui kit

b.postln //prints the button instance for the current gui kit

In the above example, first a Window was created. A Window automatically has its own top level container, which is a subclass of CompositeView. A CompositeView can have a decorator, which automatically places the contents in a predetermined order. In this case it is a FlowLayout, which is the only one that currently exists. This is optional however. Try commenting out the decorator in the example above.

Window and CompositeView both have addFlowLayout utility methods which assign an instance of FlowLayout to their view decorators and return the decorator.

Most gui objects are subclasses of View. All subclasses of View are created with the same beginning arguments, parent and bounds, for example, Slider(parent, bounds).

The parent is a container view of some kind, and the bounds are an instance of Rect or a Point (for width and height only). Using a Point is convenient if you are using a decorator for the postioning.

(; // Use Rect for precise placement in a CompositeView

Slider(w, Rect (50,50,250,20)); // Slider is actually placed in w.view, which is a top view.

Slider(w.view, Rect (50,100,250,20)) // So this is equivalent when Window is used as a parent


You can easily nest containers, decorators and views to make complex layouts, and you can use colors to visually separate them:


w.view.decorator = FlowLayout(w.view.bounds); // notice that FlowView refers to w.view, not w

v=CompositeView(w, Rect(5,5,190,390));

v.background = Color.rand; // set the color

v.decorator = FlowLayout(v.bounds);

y=CompositeView(w, Rect(205,5,190,390));

y.background = Color.rand; // set the color

y.decorator = FlowLayout(y.bounds);{ Slider(v, 180@20).background_(v.background) };// Points used, since the layout is handled by a decorator.{ Slider2D(y,58@58).background_(Color.rand); };


Container Coordinates

Container and User views use relative bounds coordinates, which are measured from the parent view's top left corner.

(; // Use Rect for precise placement in a CompositeView

v=CompositeView(w, Rect(50,50,300,300));

v.background_(Color.grey); // give the subview a visible color

Slider(v, Rect (50,50,220,20)) // so this is equivalent when Window is used as a parent


Window Coordinates

For a Window, the bounds coordinates are measured from the bottom left of the screen.

The bounds.rect differs from that of views, and is the following: Rect(left, bottom, width,height). Typically, you will call Window.screenBounds to place a window precisely independently of the screen size you are working on.

("A Precisely Placed Window",

Rect(100, Window.screenBounds.height-300, 300, 200 )


) //since the window is 200 high and the bottom is at screenBounds.height-300, the top is 100 from the screen top


Views can resize or stretch according to nine differents states, according to the instance variable resize. For documentation and examples see resize.

Actions: Performing Things with a GUI Widget

Gui widgets typically have an action, which is a Function or FunctionList to be evaluated when the user interacts with the widget.

You can set the action, or use addAction, or removeAction to determine how a widget interacts with your code.


w = Window ("A Slider");

a = Slider (w, Rect(40, 10, 300, 30));

a.action={ |sl| sl.value.postln }; // set the action of the slider



// now incrementally add some more actions to the slider

a.addAction({ |sl| w.view.background = });

a.addAction({ |sl| sl.background = - sl.value) });

// adding and removing an action:

f = { |sl| "--------*******-------".postln; };



// or remove all, of course

a.action = nil;

GUI Timing: System Clock and App Clock

See: AppClock, TempoClock, SystemClock

Calls to the gui system from from the lang must be made from the AppClock, since the SystemClock is reserved for high priority sound related tasks. If you want to control a gui with a Routine or Task, then you must either use the AppClock to play them, or use the defer mechanism, which schedules a Function in the AppClock:



w.bounds=Rect(200.rand, 200+200.rand, 300,300);






The same thing using the defer mechanism and a SystemClock:



{w.bounds=Rect(200.rand, 200+200.rand, 300,300) }.defer; // you must defer this



{w.close}.defer; // you must defer this



In reality, defer simply schedules a Routine in an AppClock.

Asynchronous GUI Techniques: Communicating with the Sound Server

Sending values from a gui object to the sound server is basically the same as sending from the lang. Gui widgets typically have an action, which is a Function or FunctionList to be evaluated when the user interacts with the widget.

// use arrow keys to change frequency



n={arg freq=220;

var out;,0,0.2);{out =, 0.2,0.02+0.20.rand,8)};



w = Window("Use arrow keys to change the frequency by steps", Rect(100, 500, 500, 120));

b = NumberBox(w, Rect(200, 10, 100, 20));

b.value = 220;

b.action = {arg numb; n.set(\freq, numb.value); }; // set the action here to change the frequency.

b.addAction ( {w.view.background = Color.rand}); // add another action here.

b.step=55; //make the step a fraction of the freq






Recieving values, however is asynchronous, and requires either using an OSCresponderNode, or polling values.

In this example a Routine polls values:


w = Window("Frequency Monitor", Rect(200, Window.screenBounds.height-200,300,150)).front;


a = StaticText(w, Rect(45, 10, 200, 20)).background_(Color.rand);

a.string = "Current Frequency";, Rect(45, 70, 200, 20)).states_([["close",,Color.rand]]).action_({w.close});


q=SynthDef(\Docs_FreqMonitor, {var freq,snd;, 400, 650);,0,0.2);,snd);,freq); // output the frequency to a control bus


r= Routine{

{ // Set the value of the StaticText to the value in the control bus.

// Setting GUI values is asynchronous, so you must use .defer in the system clock.

// Also you must check if the window is still open, since Routine will continue for at least

// one step after you close the window.

b.get( {arg v; {w.isClosed.not.if{ a.string= " Current Frequency: "++v.round(0.01)}; }.defer} );

// b.get sends query to the server, and waits for a response before it sets the StaticText.






w.onClose={r.stop;; }; //clean up if the window closes


Here an OSCreponder changes a gui. See OSCresponderNode for more examples. It is important here that the action of the responder is defered.




x={ var d,arr,out;

d =,0.2); // generate triggers for SendTrig and for DynKlank, 0, 0.9); // send message to lang

arr=Array.fill(4, {,1200+1200.rand,d)});`[arr, nil, [1, 1, 1, 1]],*0.1);,0.5)


// register to receive this message

a = OSCresponderNode(s.addr, '/tr',

{ {w.refresh}.defer }).add; // you must use defer here

w.drawHook = {|me|



Pen.addArc(400.rand@400.rand, rrand(10, 100), 2pi.rand, pi);





CmdPeriod.doOnce({w.close}); //close window when sound is stopped

w.onClose=({a.remove;;});// clean up



Custom GUI: designing your own widgets using UserView

UserView is generally speaking a view in which you can draw, and for which you can define mouse, key, and drag and drop actions. For documentation on all of these, see the UserView, View, and Pen help files. The example below, however, will demonstrate a basic example of these techniques for designing a widget. The steps you need to take are the following:

(1) Create a User View (2) define a draw function (3) define an action (4) define mouse actions (5) define key actions (6) define drag and drop actions. You can omit steps which you don't want.

Caution, subclassing differs in many points from the example below. For a subclassing template and a quick tutorial on how to write a custom widget as a SCUserView subclass, see SCUserView-Subclassing.


var value = 0.5;

w =;

// (1) create a user view

v = UserView(w,Rect(50,50,200,20));

// (2) define a drawing function for Pen

v.drawFunc = {

// Draw the fill

Pen.fillColor = Color.grey;

Pen.addRect(Rect(0,0, v.bounds.width*value,v.bounds.height));


// Draw the triangle

Pen.fillColor =;

Pen.moveTo(((v.bounds.width*value)-5) @ v.bounds.height);

Pen.lineTo(((v.bounds.width*value)+5) @ v.bounds.height);

Pen.lineTo(((v.bounds.width*value)) @ (v.bounds.height/2));

Pen.lineTo(((v.bounds.width*value)-5) @ v.bounds.height);


// Draw the frame

Pen.strokeColor =;

Pen.addRect(Rect(0,0, v.bounds.width,v.bounds.height));



// (3) set an action to the user view

v.action = {value.postln; v.refresh};

// (4) define mouse actions

v.mouseDownAction = {arg view, x = 0.5,y, m;


([256, 0].includes(m)).if{ // restrict to no modifier

value = (x).linlin(0,v.bounds.width,0,1); v.doAction};


v.mouseMoveAction = v.mouseDownAction;

// (5) (optional) define key actions

v.keyDownAction = { arg view, char, modifiers, unicode,keycode;

if (unicode == 16rF700, { value = (value+0.1).clip(0,1) });

if (unicode == 16rF703, { value = (value+0.1).clip(0,1) });

if (unicode == 16rF701, { value = (value-0.1).clip(0,1) });

if (unicode == 16rF702, { value = (value-0.1).clip(0,1) });



// (6) (optional) define drag and drop

v.beginDragAction = {value}; // what to drag

v.canReceiveDragHandler = {View.currentDrag.isNumber}; // what to receive

v.receiveDragHandler = {value = View.currentDrag; v.doAction }; // what to do on receiving

// just for testing drag and drop


StaticText(w,Rect(50,150,350,50)).string_("To Test Drag and Drop,\nHold down Cmd (Ctl) Key");


Automatic GUI

You can get a quick simple automatic interface for a Synth with SynthDesc : makeWindow.



SynthDef("test", { arg out, freq=330, amp=0.6;,,0,amp))




Helper Methods in GUI

*stringBounds( string, font )

Returns a Rect object describing the bounds occupied by the given string if it was painted using the given font. Note that this method is asynchronous in SwingOSC, hence it is advised to use it inside a Routine.(not necessary on CocoaGUI). Example:



var text = "Test", bounds, font, fonts, rect =, total =;

fonts = Font.availableFonts;

w = "String Bounds", resizable: false );

w.view.background =;{

font = Font( fonts.choose, exprand( 6, 36 ));

bounds = GUI.stringBounds( text, font );

rect.set( rect.right, rect.bottom, bounds.width + 4, bounds.height + 2 ); w, rect )

.font_( font ).align_( \center ).string_( text ).background_( Color.white );

total = total.union( rect );


w.bounds = total.moveTo( 200, 200 );


}.fork( AppClock );


Browsers and Inspectors

These classes use the current GUI kit implementation as returned by GUI.current . You usually do not instantiate them directly, but use one of the "Plus-GUI" methods described in the next paragraph.









"Plus-GUI" methods are methods added to other classes such as String or Server that provide GUI functionality for those classes. These methods use the current GUI kit implementation as returned by GUI.current .


Examples for Inspectors:




Examples for Browsers:

UGen.browse; // ClassBrowser

SynthDescLib( \myLib ).read.browse;


Examples for Server:

if( s.window.notNil, {s.window.close});




Examples for Plotting:

see ArrayedCollection, Buffer, Env, Function, Signal, SoundFile, Wavetable


Examples for Scoping

see Bus, Function, Server, UGen, ScopeView, Stethoscope