7
$\begingroup$

Consider the following. When clicking in "Browse" and then choosing a file, the path will remain the original one and won't change to the one of the file I chose:

nb = EvaluationNotebook[];

DynamicModule[
 {path1 = FileNameJoin[{$HomeDirectory, "Desktop", "file.nb"}],
  path2 = FileNameJoin[{$HomeDirectory, "Desktop", "file.xls"}]}, 
 CreateDialog[
  Column[{Row[{InputField[Dynamic[path1], String, Enabled -> False], 
      FileNameSetter[Dynamic[path1], 
       "Open", {"Mathematica Notebook" -> {"*.nb"}}]}], 
    Row[{InputField[Dynamic[path2], String, Enabled -> False], 
      FileNameSetter[Dynamic[path2], 
       "Open", {"Excel files" -> {"*.xls", "*.xlsx"}}]}]}], 
  NotebookEventActions :> {"WindowClose" :> (SetOptions[nb, 
       Visible -> True])}]
]
$\endgroup$
1
  • 2
    $\begingroup$ John, before you post a question you should try to narrow down the problem to the shortest section of code that displays it as I have done for you. You will get a much better response to your questions if readers do not have as much code to wade through. This is a good question, by the way, and has my vote! $\endgroup$
    – Mr.Wizard
    Commented Jun 30, 2012 at 8:23

1 Answer 1

8
$\begingroup$

The behaviour is indeed inconsistent: CreateDialog uses the option WindowSize -> All, where All (god knows why) causes the unexpected behaviour with dynamic content. See resolution of the problem at the end of the post.

The following even more simple example clearly shows that something fishy is going on: only path2 can be set via the second button but not path1:

DynamicModule[{path1 = "path #1", path2 = "path #2"},
  CreateDialog@Grid@{
     {FileNameSetter[Dynamic@path1, 
       "Open", {"Mathematica Notebook" -> {"*.nb"}}], Dynamic@path1},
     {FileNameSetter[Dynamic@path2, 
       "Open", {"Excel files" -> {"*.xls", "*.xlsx"}}], Dynamic@path2}
     }];

Removing the CreateDialog wrapper causes both FileNameSetters to work as expected, which indicates that it is the special context of the dialog window that causes the erroneous behaviour. Since CreateDialog[expr] is just a specialized CreateWindow[DocumentNotebook[expr]], we have to look at the options CreateDialog uses as defaults for its internal CreateWindow. As it turns out, the blame lies with WindowSize. Compare the behavior of the following two examples where only the value of WindowSize differs.

This is faulty:

DynamicModule[{path1 = "path #1", path2 = "path #2"},
 CreateWindow[DocumentNotebook@Grid@{
     {FileNameSetter[Dynamic@path1, 
       "Open", {"Mathematica Notebook" -> {"*.nb"}}], Dynamic@path1},
     {FileNameSetter[Dynamic@path2, 
       "Open", {"Excel files" -> {"*.xls", "*.xlsx"}}], Dynamic@path2}
     }, WindowSize -> All]]

This works as expected:

DynamicModule[{path1 = "path #1", path2 = "path #2"},
 CreateWindow[DocumentNotebook@Grid@{
     {FileNameSetter[Dynamic@path1, 
       "Open", {"Mathematica Notebook" -> {"*.nb"}}], Dynamic@path1},
     {FileNameSetter[Dynamic@path2, 
       "Open", {"Excel files" -> {"*.xls", "*.xlsx"}}], Dynamic@path2}
     }, WindowSize -> {200, 200}]]

And then I remembered: Strange behavior of CreateDialog in (Dynamic)Module

The options for CreateWindow (to make it look like it is a dialog) are:

{WindowSize -> {200, 200}, Deployed -> True, ShowCellBracket -> False,
 WindowFloating -> False, StyleDefinitions -> "Dialog.nb",
 WindowFrame -> "ModelessDialog"}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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