Dialog manipulation - user-interface

I want to create a dialog window, where the user can perform various tasks, and would like him to return from the dialog by clicking on the Cancel button with the mouse (i.e. not by hitting Enter). Therefore I do not want to use CreateDialog. However, by creating a less-specific dialog window via CreateWindow, all strings appear unformatted.
expr = Column[{
Row#{"set variable to: ", InputField["value", String]},
"Try to hit Enter in any of the dialogs: it closes #2 but not #1.",
CancelButton[]
}];
CreateWindow[DialogNotebook[expr], WindowSize -> All, WindowMargins -> {{100, Automatic}, {Automatic, Automatic}}, WindowTitle -> "1. CreateWindow & DialogNotebook"];
CreateDialog[expr, WindowTitle -> "2. CreateDialog"];
Is there any clever way to have the looks of the second dialog window, but the button-behaviour of the first one? Of course, expr here is a simple example, but it can be quite complex in reality, thus it is no option to wrap every string into Cell[string, "Text"], and every other expression into some obscure boxform.

This will stop your dialog window closing when Enter is pressed:
CreateDialog[expr, WindowTitle -> "2. CreateDialog", NotebookEventActions -> {}];
It overwrites the default dialog NotebookEventActions.

Another option:
expr = Style[
Column[{Row#{"set variable to: ", InputField["value", String]},
"Try to hit Enter in any of the dialogs: it closes #2 but not \
#1.", CancelButton[]}], ShowStringCharacters -> False];

Perhaps using TextCell:
expr = Column[{Row#{TextCell#"set variable to: ",
InputField["value", String]},
TextCell#"Try to hit Enter in any of the dialogs: \
it closes #2 but not #1.",
CancelButton[]}];
CreateWindow[
DialogNotebook[expr], WindowSize -> All,
WindowMargins -> {{100, Automatic}, {Automatic, Automatic}},
WindowTitle -> "1. CreateWindow & DialogNotebook"]
Edit
Use
TextCell#Style[" ... blah blah ...", style_opt ]
for formatting.

There are a number of ways to do this, and other folks have posted two good ones, but in my opinion the easiest approach is to set the BaseStyle of the Column expression to match the base style of the dialog, and then use CreateWindow. The style in question is "Panel", so this gets you the result you want:
expr = Column[{Row#{"set variable to: ", InputField["value", String]},
"Try to hit Enter in any of the dialogs: it closes #2 but not #1.",
CancelButton[]}, BaseStyle -> "Panel"];
CreateWindow[DialogNotebook[expr], WindowSize -> All,
WindowMargins -> {{100, Automatic}, {Automatic, Automatic}},
WindowTitle -> "1. CreateWindow & DialogNotebook"];

Related

Set bounds property for "choose from list" in AppleScript?

I'm making an applet that prompts users to choose from a list, and my list currently has 169 items. When the list is generated, the window extends from the top of the display to the bottom, a bit overwhelming for a user.
I'm wondering if anyone knows of a way to edit the bounds property of the window generated by "choose from list?"
Here's the code I'm using (not including separate code to compile the list and store it in "pkgList"):
set userChoice to (choose from list pkgList with title "Title" with prompt "Choose file:" OK button name "OK" cancel button name "Cancel" without empty selection allowed) as string
I'm very new to AppleScript, so detailed explanations and analogies are much appreciated. :)
You might use CocoaDialog instead:
set l to {"a a", "b"}
set l2 to ""
repeat with i in l
set l2 to l2 & quoted form of i & " "
end repeat
do shell script "/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog \\
standard-dropdown --title Title --text Text --items " & l2
set {button, answer} to paragraphs of result
if button is 1 then return
item {answer + 1} of l

How to modify OUTPUT font type?

Is it possible to change the OUTPUT font type instead of the default one? How?
This is my default stylesheet: http://filefactory.com/file/cfc2cb0/n/blueOutput.nb
Thanks!
The problem lies in StandardForm not respecting the FontFamily option, although it does seem to respect most other font options. Sjoerd's answer used TraditionalForm output and thus worked. You can see this problem if you run
SetOptions[EvaluationNotebook[], StyleDefinitions -> Notebook[{
Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Output"],
FontColor -> RGBColor[0, 0, .5], FontSize -> 14,
FontFamily -> "Symbol", FontWeight -> "Bold"]}]]
Then compare
{1 + 1, "abc", Sin[x]} (* This is by default in StandardForm *)
{1 + 1, "abc", Sin[x]} // StandardForm
{1 + 1, "abc", Sin[x]} // OutputForm
{1 + 1, "abc", Sin[x]} // TraditionalForm
You can also look at
Dynamic[CurrentValue/#{FontFamily, FontWeight, FontSize}]
Dynamic[CurrentValue/#{FontFamily, FontWeight, FontSize}] // TraditionalForm
which shows that the CurrentValue of FontFamily "seen" in the output depends on the output format.
Unfortunately, I don't see how to get around this issue...
Just go to the Format > Edit Stylesheet... menu. Then in the private style definitions sheet that pops-up choose 'Output' from the pull-down menu and change the looks of the resulting Output cell. This stylesheet will be stored with your open notebook.
In light of Simon's answer, you could force output printing in a certain style using $PrePrint.
$PrePrint = Style[#, FontFamily -> "Symbol"] &;
{1 + 1, "abc", Sin[x]}
You can do this by redefining the StandardForm style which is used for Output style by default (see the DefaultFormatType option in the Output style):
SetOptions[EvaluationNotebook[],
StyleDefinitions ->
Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["StandardForm"],
FontFamily -> "Palatino Linotype"]},
StyleDefinitions -> "PrivateStylesheetFormatting.nb"]]
But Input style in this case is also affected because it is based on the StandardForm style too...
You could try wrapping your inputs using the Style[] command. For example:
test="This is a test string.";
Style[test,{Red,"Title"}]
This generates the string in my style sheet's 'title' settings in the colour red. The solution of changing your Stylesheets is obviously preferable to this, but this might be a quick and dirty temporary workaround.

Getting CellDingbat to remember its state between Mathematica sessions

I have modified my notebook's stylesheet to include a StyleData["Todo"] that inherits from StyleData["Item"]. It changes the cell dingbat to a checkbox. In the stylesheet editor:
Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Item"]],
CellDingbat->DynamicModuleBox[{$CellContext`color$$},
CheckboxBox[
Dynamic[$CellContext`color$$], {RGBColor[1, 0.5, 0],RGBColor[0,Rational[2, 3], 0]},
Background -> Dynamic[$CellContext`color$$]],
DynamicModuleValues :> {}
],
]
The problem is that the state of the checkbox, when used in a notebook, is not saved between Mathematica sessions. I thought the DynamicModule[] would do the trick. How do I get the checkbox to remember its state?
EDIT
Simon's solution does save the state of the checkbox, but the checkbox is clipped when used as a CellDingbat (MacOS X). Putting Simon's code in a CellFrameLabels options does the trick, and also keeps the default "Item" CellDingbat. Here is what I've gone with:
Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Item"]],
CellFrameLabels->{{
ButtonBox[
CheckboxBox[False], ButtonFunction :> (SelectionMove[
ButtonNotebook[], All, ButtonCell];
With[{$CellContext`new = ReplaceAll[
Options[
NotebookSelection[
ButtonNotebook[]], CellFrameLabels], CheckboxBox[
Pattern[$CellContext`x,
Alternatives[True, False]]] :> CheckboxBox[
Not[$CellContext`x]]]},
SetOptions[
NotebookSelection[
ButtonNotebook[]], $CellContext`new]]; SelectionMove[
ButtonNotebook[], After, CellContents]), Appearance -> None,
Method -> "Preemptive", Evaluator -> Automatic], None}, {
None, None}},
MenuSortingValue->1621]
The problem with your code (I think) is that a new DynamicModule does not get created each time you create a new "ToDo" cell. So there is nowhere that the state of each Checkbox can get saved.
The simplest solution I could think of for storing the state of the Checkbox for each "ToDo" cell is to overwrite the CellDingbat the first time that the Checkbox is activated.
(Other options I played with were using TaggingRules,
toggling between "ToDo" and "ToDone" styles, etc...)
However, even a plain Checkbox in a CellDingbat does not store its state - try running the following then cycle the output through a Show Expression cycle.
CellPrint[Cell["test", "Text", CellDingbat -> ToBoxes[Checkbox[]]]]
To get around this, I used Checkbox with the definite argument True or False wrapped up in a button that changes the state. This is stupid and inefficient, but it works!
So, my code for the cell style
Cell[StyleData["ToDo", StyleDefinitions -> StyleData["Item"]],
CellDingbat -> ButtonBox[CheckboxBox[False],
ButtonFunction :> (SelectionMove[ButtonNotebook[], All, ButtonCell];
With[{$CellContext`new = ReplaceAll[
Options[NotebookSelection[ButtonNotebook[]], CellDingbat],
CheckboxBox[Pattern[$CellContext`x, Alternatives[True, False]]] :> CheckboxBox[Not[$CellContext`x]]]},
SetOptions[NotebookSelection[ButtonNotebook[]], $CellContext`new]];
SelectionMove[ButtonNotebook[], After, CellContents]),
Appearance -> None, Method -> "Preemptive", Evaluator -> Automatic]]
I'm not happy with this solution, but it's the best I've come up with. An improvement would be to move the button function code out of the cell so that it is not repeated for every checked ToDo cell. Also to make it run without a ReplaceAll so that the kernel is not needed and the function can be run using just the frontend.

How to control Trigger state (Pause, Play) using code (not just buttons)

A trigger is useful to use for animation, but I am not able to find a way to change the state of the trigger in the code (i.e. without having to hit the Pause or Play button myself).
For example, suppose I want to do a simulation where when some event happen, I want to make the currently active trigger go to a PAUSE state, and when another event happen, I want the trigger to go to PLAY state.
The buttons to do that will still be there, but I want to also be able to change these from the code without having to physically do it.
The reason is, I am doing some action, and having the trigger being in PLAY mode while I am doing this other action is making things not working.
So I need to make it go to PAUSE state, and when I am done, I can set it back to PLAY state.
Here is a small example of what I mean:
Manipulate[
EventHandler[
Dynamic#Graphics[
{Circle[{0,0},1], Text[n,pt] },
PlotRange->All,ImageSize->200,ImagePadding->10],
{
"MouseDown":>
(
(* What to do here to cause the trigger to become Paused?"*)
pt=MousePosition["Graphics"]
),
"MouseDragged":>
(
(* while dragging, the trigger remains in PAUSED state "*)
Print["mouse dragged"];
pt=MousePosition["Graphics"]
),
"MouseUp":>
(
Print["MouseUp"]
(* What to do here to cause the trigger to Play again?"*)
)
}
],
Control[{{n,0,"Run"},0,100,0.01,
ControlType->Trigger, DisplayAllSteps->True, AnimationRate->1,
AppearanceElements->{"PlayPauseButton","ResetButton"}}
],
{{pt,{0,0}},ControlType->None}
]
In above, when I drag the mouse on the display, I want the trigger to become PAUSED so that the number shown is not changing while being dragged. When done with dragging, I can then make the trigger PLAY again if needed.
So, my question: Is there a way to change trigger state like the above in the code?
I can ofcourse not use trigger at all, and code everything myself in other ways, but thought to ask before I give up, as trigger is convenient to use.
Here is a link to more documentation of trigger and the buttons.
The closest thing I found is the Enabled-> option to trigger, but this just makes the trigger itself enabled to not, and does not affect the trigger state. i.e. if trigger is firing, it will remain firing even if I make disabled.
http://reference.wolfram.com/mathematica/ref/Manipulator.html
http://reference.wolfram.com/mathematica/ref/Trigger.html
thanks
There is probably an easier way to do this, but this seems to work. It's basically mimicking a Trigger by creating a scheduled task and stopping and starting it when a mouse button is pressed or released or when the Play/Pause button is clicked.
DynamicModule[{start = 0, finish = 100, dt = 0.01, running = False, task, n},
n = start;
Manipulate[
EventHandler[
Dynamic#
Graphics[{Circle[{0, 0}, 1], Text[n, pt]}, PlotRange -> All,
ImageSize -> 200, ImagePadding -> 10],
{
"MouseDown" :>
(StopScheduledTask[task]; pt = MousePosition["Graphics"]),
"MouseDragged" :>
(Print["mouse dragged"]; pt = MousePosition["Graphics"]),
"MouseUp" :>
(If[running, StartScheduledTask[task]]; Print["MouseUp"])
}],
Control[Labeled[
Row[{
Button[
Dynamic#If[running, Magnify["\[DoubleVerticalBar]", 1.5],
Magnify["\[RightPointer]", 1.5]],
(If[running, running = False; StopScheduledTask[task],
running = True; StartScheduledTask[task]]),
Appearance -> "Palette", ImageSize -> 15, ContentPadding -> False],
Button[
Magnify["\[FirstPage]", 1.5],
(n = start; ResetScheduledTask[task]),
Appearance -> "Palette", ImageSize -> 15, ContentPadding -> False]
}], "Run", Left]
],
{{pt, {0, 0}}, ControlType -> None}
],
Initialization :> (task =
CreateScheduledTask[n += dt, {dt, Floor[(finish - start)/dt]}]),
Deinitialization :> RemoveScheduledTask[task]
]
Edit: Changed the appearance of the controls to make them look more like traditional play/pause/reset buttons.

How to delete unnecessary options from Notebook[]?

By default Notebook[] has a small set of Options:
In[4]:= Options[EvaluationNotebook[]]
Out[4]= {FrontEndVersion ->
"7.0 for Microsoft Windows (32-bit) (February 18, 2009)",
StyleDefinitions -> "Default.nb",
WindowMargins -> {{0, Automatic}, {Automatic, 0}},
WindowSize -> {616, 537}}
Sometimes I wish to modify Notebook appearance and set additional Options. For example I like to have comments to be Plain rather than Bold:
SetOptions[EvaluationNotebook[],
AutoStyleOptions -> {"CommentStyle" -> {FontWeight -> Plain,
FontColor -> GrayLevel[0.6`], ShowAutoStyles -> False,
ShowSyntaxStyles -> False, AutoNumberFormatting -> False}}]
Now Options[EvaluationNotebook[]] will return also new option I have set.
But sometimes I wish to restore default behavior and delete additional Options. How can I do that?
Igor's answer is almost right. To remove the options set by
SetOptions[EvaluationNotebook[],
AutoStyleOptions -> {"CommentStyle" -> {FontWeight -> Plain,
FontColor -> GrayLevel[0.6`], ShowAutoStyles -> False,
ShowSyntaxStyles -> False, AutoNumberFormatting -> False}}]
You need to run
SetOptions[EvaluationNotebook[],
AutoStyleOptions -> {"CommentStyle" -> Inherited}]
But this only works for options that are standard and have a default to inherit (if it's a cell then from the enclosing section or notebook, if it's a notebook then from the stylesheet). What if you make up your own option, e.g.
Protect[HiddenData];
SetOptions[EvaluationNotebook[], HiddenData -> {"here's a string"}]
I don't know how to programmatically remove this option.
Edit:
Actually, to remove the HiddenData option created above, I can use something like
NotebookPut[DeleteCases[NotebookGet[EvaluationNotebook[]],
$CellContext`HiddenData -> _],
EvaluationNotebook[]]
Edit 2:
Mr Wizard asked how to remove all user-set notebook options. Assuming that this means all options that can't be inherited, then I believe that the following should work:
NotebookPut[
With[{nb = NotebookGet[EvaluationNotebook[]], opts = Options[Notebook][[All, 1]]},
Prepend[Select[Rest#nb, MemberQ[opts, First[#]] &], First#nb]],
EvaluationNotebook[]]
But maybe there are options associated with the StyleSheet that I've ignored...
If he meant how do you get back to your system's default notebook options - then you can just delete all notebook options:
NotebookPut[Notebook[First#NotebookGet[EvaluationNotebook[]]],
EvaluationNotebook[]]
(1) Select Format -> Options Inspector (or Shift+Ctrl+O on Windows)
(2) For the two fields next to "Show option values" select Notebook and as text
(3) Select and delete all text in the box below
(4) Click Apply
After understanding NotebookGet, I believe this works for full options reset.
NotebookPut[
Notebook#First#NotebookGet[EvaluationNotebook[]],
EvaluationNotebook[]]
Use:
SetOptions[EvaluationNotebook[], Background -> Inherited]
Igor

Resources