3

What is the best way to right align a button in a dialog window, regardless of the window's WindowSize?

Here is a resizable window to test on:

CreateDialog[
 Column[{"Here's some text text text text text text text", 
         ChoiceButtons[]}], 
 WindowFrameElements -> {"ResizeArea"}, 
 WindowFrame -> "Normal"]

I would like the button to always be right aligned.

Some unsatisfactory solutions I thought of:

  • Put the button in an Item and right align. Then the right margin is determined by the Column size which can't easily be set in pixels.

  • Put the button in a Panel, right align it, and set a fixed size on the panel. Again, the size is determined by the panel size, not the window size.

Use case:

Suppose we need to create an alternative to MessageDialog. This new function, myMessageDialog, should take the WindowSize option, and the created notebook should respond to SetOptions[nb, WindowSize -> ...] The dialog buttons should always be aligned to the right edge of the window regardless of the window size.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • Please explain further the problem with Item. Could you use Spacer to set a margin after converting pixels to printer's points? – Mr.Wizard Jul 27 '12 at 11:54
  • @Mr.Wizard Try it in the sample dialog I included and resize the window. The buttons don't keep on the right if you increase the window size, which would be the ideal solution. Also, as I said, the right margin is determined by the column size, which is difficult to set properly. Item sizes can only be set in ems, not pixels. I use the Item approach in the image uploader's history dialog, and on a Chinese version of Windows, the sizes are all messed up because of the unpredictability of the em width (different system font). – Szabolcs Jul 27 '12 at 11:56
  • @Mr.Wizard I have tried to solve this problem properly many times during the past few months, and never managed to come up with a solution I'm satisfied with, and which is flexible enough to allow resizable windows. – Szabolcs Jul 27 '12 at 11:57
  • Spacer uses pixels. An em is the width of the letter M, or something like that ... – Szabolcs Jul 27 '12 at 12:01
  • According to the docs: "Spacer[w] displays as a spacer w printer's points wide." -- if Spacer works as desired you can use that to set the margin, can you not? That's one problem down if so. – Mr.Wizard Jul 27 '12 at 12:03
  • @Mr.Wizard Not really. A spacer will push the button to the right, but it will not right align them. I need them aligned to the right edge of the window itself. Pixels and points are pretty much the same for Mma (not sure about changing the dpi setting though). Also, the unit size coversion is not really the main issue here ... I just can't believe there's no proper easy way to do this. MessageDialog does it: it always has the button aligned to the right edge of the window. – Szabolcs Jul 27 '12 at 12:04
  • I meant something like Item[Row[{ChoiceButtons[], Spacer[7]}], Alignment -> Right] -- you said "right margin" so I assume you want something like this? Also, have you tried using Dynamic@CurrentValue["WindowSize"] to set the size of a Row/Grid/whatever? – Mr.Wizard Jul 27 '12 at 12:08
  • Dynamic@CurrentValue["WindowSize"] is an excellent suggestion! It is possible to use CreateDialog[..., WindowSize -> Dynamic[sz]], but that is very messy, especially because sz can't be properly localized with a DynamicModule. Your suggestion to use CurrentValue is much better. Unfortunately I'm not sure how to set the Grid size with this as it is specified in ItemSize units. But it's a step forward nevertheless. – Szabolcs Jul 27 '12 at 12:22

1 Answers1

3

This is unrefined, but I'll put it up as a proof of concept:

CreateDialog[
  Column[{
   Row[{"Here's some text text text text text text text"}, 
     ImageSize -> Dynamic[CurrentValue["WindowSize"] /. {x_, _} :> x - 15]],
   Item[ChoiceButtons[], Alignment -> Right]
   }],
  WindowFrameElements -> {"ResizeArea"}, WindowFrame -> "Normal"
]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • Why do you say "unrefined"? I see nothing clumsy about it. – DavidC Jul 27 '12 at 12:41
  • @David thanks :-) I didn't test it with anything but this one example so I am not presenting this as a complete solution, just an illustration of a method. – Mr.Wizard Jul 27 '12 at 12:43