Introduction to Toolbars

The Wolfram Language's unique symbolic architecture makes it easy to add toolbars with any possible appearance and action to a Wolfram System notebook.

Ruler

The ruler is a toolbar used to set the text margins of selected cells and the indentation of cell names and keywords. The notebook ruler can be added to a notebook by selecting (MenuGrayLink[Show Ruler, paclet:ref/menuitem/ShowRuler]) in the (MenuGrayLink[Window, paclet:guide/WindowMenu]) menu or programmatically by using the notebook option (RefLink[WindowToolbars, paclet:ref/WindowToolbars]).

Show ruler.

For example, the ruler can be shown in the current notebook by evaluating (RefLink[SetOptions, paclet:ref/SetOptions])[(RefLink[EvaluationNotebook, paclet:ref/EvaluationNotebook])[],(RefLink[WindowToolbars, paclet:ref/WindowToolbars])->"RulerBar"].

Docked Cells

Docked cells are cells that are displayed as a toolbar or banner at the top of the notebook and treated as deployed.

Docked cells.

While any type of cells can be included in cells, it is recommended that only those with the cell style "DockedCell" be used. The style "DockedCell" ensures that the cell behaves like a toolbar.

One way to implement a docked cell in the current notebook is to use (RefLink[SetOptions, paclet:ref/SetOptions])[(RefLink[EvaluationNotebook, paclet:ref/EvaluationNotebook])[],(RefLink[DockedCells, paclet:ref/DockedCells])->cells].

Here is a basic example of a docked cell containing a string.

CreateWindow[DockedCells->Cell["Lorem ipsum dolor sit amet, consectetur adipiscing elit.","DockedCell",Background->LightBlue]];

This next example contains the mathematical expression sin(x) in (RefLink[TraditionalForm, paclet:ref/TraditionalForm]).

CreateWindow[DockedCells->Cell[BoxData[FormBox[RowBox[{"sin","(","x",")"}],TraditionalForm]],"DockedCell",Background->LightGreen]];

Control objects such as action menus, popup menus, buttons, input fields, etc. can also be used in a docked cell. Create the toolbar contents and then wrap (RefLink[ToBoxes, paclet:ref/ToBoxes]) around it to generate the front end box structure automatically. Then place the (RefLink[ToBoxes, paclet:ref/ToBoxes]) expression into the (RefLink[BoxData, paclet:ref/BoxData]) of a (RefLink[Cell, paclet:ref/Cell]). For example, the following input creates an action menu toolbar that places a cell with the chosen style at the insertion point.

ex1=Grid[{{Item[Row[{Style["Wolfram",Bold,14,FontFamily->"Helvetica"],Style["Mathematica",Italic,Bold,14,FontFamily->"Helvetica"]},Spacer[1]],Alignment->Left],Item[ActionMenu["Choose a Style...",{"Title":>FrontEndTokenExecute[InputNotebook[],"Style","Title"],"Subtitle":>FrontEndTokenExecute[InputNotebook[],"Style","Subtitle"],"Subsubtitle":>FrontEndTokenExecute[InputNotebook[],"Style","Subsubtitle"]}],Alignment->Right]}},ItemSize->{{15,10}}];

CreateWindow[DockedCells->Cell[BoxData[ToBoxes[ex1]],"DockedCell"]];

If the spacing between the toolbar elements is static, some elements in a docked cell may become hidden at certain window widths. This can be solved by scaling the item sizes in (RefLink[Grid, paclet:ref/Grid]). The following example includes a label and (RefLink[ButtonBar, paclet:ref/ButtonBar]).

ex2=Grid[{{Item[Style["Evaluation Tools:",FontFamily->"Helvetica",12,Bold],Alignment->Left],Item[ButtonBar[{Style["Delete All Output",10]:>FrontEndTokenExecute["DeleteGeneratedCells"],Style["Abort Evaluation",10]:>FrontEndTokenExecute["EvaluatorAbort"],Style["Quit Kernel",10]:>FrontEndTokenExecute["EvaluatorQuit"]}],Alignment->Right]}},ItemSize->{{Scaled[0.3],Scaled[0.7]}}];

CreateWindow[DockedCells->Cell[BoxData[ToBoxes[ex2]],"DockedCell",Background->RGBColor[1, 0.991791, 0.866468]]];

Multiple docked cells can be included in a notebook.

CreateWindow[DockedCells->{Cell[BoxData[ToBoxes[ex1]],"DockedCell"],Cell[BoxData[ToBoxes[ex2]],"DockedCell",Background->RGBColor[1, 0.991791, 0.866468]]}];

Extended Examples

Word Processing Toolbar

This example includes buttons for setting text alignments and spacings, and opening the font dialog, print dialog, and the Documentation Center. In this example, each of the toolbar elements is generated separately and then converted into boxes using (RefLink[ToBoxes, paclet:ref/ToBoxes]) in the (RefLink[CreateWindow, paclet:ref/CreateWindow]) statement at the end.

Here is a row of buttons controlling the spacing and alignment of the notebook text:

alignmentsSpacings=Row[Flatten[{Button[Grid[{{"---"},{"--"},{"---"},{"-"}},BaseStyle->{9,"Label",Bold},Alignment->{"Columns"->{{#}}},Spacings->{"Columns"->{{0}},"Rows"->{{-.8}}}],{SelectionMove[InputNotebook[],All,Cell],SetOptions[NotebookSelection[InputNotebook[]],TextAlignment->#]},Appearance->"Palette",ImageSize->{27,20}]&/@{Left,Center,Right},Button[Grid[#1,BaseStyle->{9,"Label",Bold},Alignment->{"Columns"->{{Right}}},Spacings->{"Columns"->{{0}},"Rows"->{{#2}}}],{SelectionMove[InputNotebook[],All,Cell],SetOptions[NotebookSelection[InputNotebook[]],#3]},Appearance->"Palette",ImageSize->{27,20}]&@@@{{{{"---"},{"---"},{"---"},{"---"}},-0.8,TextJustification->1},{{{"---"},{"---"}},-0.8,LineSpacing->{1,3}},{{{"---"},{"---"}},-0.5,LineSpacing->{1.5,3}},{{{"---"},{"---"}},-0.1,LineSpacing->{2,3}}}}]]

This button opens the Fonts dialog to set formatting options for text selections:

font=Button[Style["A",Bold,Italic,20,FontFamily->"Times",FontColor->RGBColor[0.002166781109330892, 0., 0.5113145647364004]],FrontEndTokenExecute["FontPanel"],Appearance->None,ImageSize->{27,18}]

This button opens the system print dialog to print the notebook:

print=Button[ImageResize[,{26,19}],FrontEndTokenExecute["PrintDialog"],Appearance->None,ImageSize->{27,20}]

This button opens the Documentation Center:

help=Button[Framed[Style["?",9.1,Bold,FontFamily->"Helvetica"],Background->LightGreen,RoundingRadius->15,ImageSize->{Automatic,18}],FrontEndTokenExecute["SelectionHelpDialog"],Appearance->None,ImageSize->{27,20}]

Here are the labels for the buttons in the toolbar:

labels=Style[#,9,FontFamily->"Helvetica"]&/@{"Alignment","Fonts","Print","Help"}

Here is the completed toolbar in a new notebook:

CreateWindow[DockedCells->Cell[BoxData[ToBoxes[Grid[{{alignmentsSpacings,font,print,help},labels},Spacings->{{0,6,20,2},{Automatic}}]]],"DockedCell"]];

Decorative Toolbar

Images can also be used as docked cells. Below, a 1399×24 pixel image is being used as a decorative docked cell similar to those seen in many stylesheets.

image=Cell[BoxData[ToBoxes[]/.{(ImageSize->{___,___})->(ImageSize->{1399,24})}],"DockedCell",Background->Black,ImageMargins->0,CellMargins->{{0,0},{0,0}},CellFrameMargins->{{0,0},{0,0}}];

CreateWindow[DockedCells->image];

Defining Toolbars in a Stylesheet

A docked cell can also be defined in a stylesheet instead of in an individual notebook. For example, notebooks with the NaturalColor stylesheet all have the following docked cell at the top of each notebook.

The (RefLink[DockedCells, paclet:ref/DockedCells]) option value for stylesheets is stored in the notebook style definitions cell within a stylesheet notebook. To add a docked cell, start by opening a stylesheet notebook. If the cell does not already exist in the stylesheet, create a new cell with an expression of the form (RefLink[Cell, paclet:ref/Cell])[(RefLink[StyleData, paclet:ref/StyleData])["Notebook"]] and place it near the top of the notebook.

Add the docked cell(s) cells to the stylesheet by updating the notebook style definitions cell expression to resemble the following form: (RefLink[Cell, paclet:ref/Cell])[(RefLink[StyleData, paclet:ref/StyleData])["Notebook"],(RefLink[DockedCells, paclet:ref/DockedCells])->{cells}]. For example, print the cell from the previous example.

image//CellPrint

Unformat the printed cell and copy the cell expression into the notebook style definitions cell as the (RefLink[DockedCells, paclet:ref/DockedCells]) option value.

Finally, choose (MenuGrayLink[Cell, paclet:guide/CellMenu]) ▶ (MenuGrayLink[Show Expression, paclet:ref/menuitem/ShowExpression]) to reformat the notebook style definition cell.

Any new notebook with this stylesheet will contain the docked cell.