Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: Re: Can nobody help me? Re: selecting a column in a grid

  • To: mathgroup at smc.vnet.net
  • Subject: [mg98333] Re: [mg98326] Re: [mg98312] Re: Can nobody help me? Re: selecting a column in a grid
  • From: John Fultz <jfultz at wolfram.com>
  • Date: Wed, 8 Apr 2009 02:44:34 -0400 (EDT)
  • Reply-to: jfultz at wolfram.com

A couple of ideas here.

First, would a button work?  It's not clear to me exactly how your UI works, so 
I don't know whether a button would interfere with other kinds of selectability. 
But a button can have the properties of expanding out automatically to the full 
width of the cell.  Unfortunately, it has to be visible to do that, but if this 
is acceptable for you, it should be a fairly simple solution.

Something like this...

randomLengthString := StringJoin[Table["a", {RandomInteger[{1, 20}]}]];
Grid[Table[
  With[{str = 
     "pushed {" <> ToString[i] <> ", " <> ToString[j] <> "}"}, 
   Button[randomLengthString, Print[str], Background -> White, 
    Appearance -> "Palette"
    ]], {i, 5}, {j, 5}], Spacings -> {0, 0}]

If that doesn't work for you, then you can calculate the dimensions you're 
looking for using Rasterize with the "RasterSize" element.  Here's some example 
code which does exactly this, assigning the list of widths and heights to the 
appropriate variables...

randomLengthString := StringJoin[Table["a", {RandomInteger[{1, 20}]}]];
gr = Grid[Table[randomLengthString, {i, 3}, {j, 5}]];
widths = First /@ Module[{width = Length[gr[[1, 1]]]},
    Table[
     Rasterize[
      gr /. Grid[mat_, opts___] :> 
        Grid[mat[[All, 1 ;; Min[i, width]]], opts], "RasterSize"], {i,
       width}]];
heights = Last /@ Module[{height = Length[gr[[1]]]},
   Table[
    Rasterize[
     gr /. Grid[mat_, opts___] :> 
       Grid[mat[[1 ;; Min[i, height]]], opts], "RasterSize"], {i, 
     height}]]

You could, of course, skip the 'heights' calculation if you don't need it. 
Here's a little visualization to show you how it's picking the numbers.


gridlines = {Line[{{#, 0}, {#, Last[heights]}}] & /@ (widths), 
   Line[{{0, #}, {Last[widths], #}}] & /@ (Last[heights] - heights)};
Graphics[{Inset[Rasterize[gr]], gridlines}, 
 ImageSize -> {Last[widths], Last[heights]}, AspectRatio -> Full, 
 PlotRange -> {{0, Last[widths]}, {0, Last[heights]}}] 

One downside to this method is that it doesn't center the dividing areas betweenthe fields, as you can see from the visualization.

Sincerely,
 
John Fultz
jfultz at wolfram.com
User Interface Group
Wolfram Research, Inc.


On Mon, 6 Apr 2009 05:01:04 -0400 (EDT), Paul Ellsmore wrote:
> Thanks Sjoerd,
>
> Unfortunately, this approach has a problem for me, and I have to admit I
> don't know why it works at all!
>
> The following example shows my problem (I think I have adapted your code
> to use a user-defined list of data properly):
> myData = {{aasdfpppppppppppp, bbbbbbbbb, c}, {dasdf, "", f},
> {gasdf, h, i}};
> Dynamic[clickpos]
> Grid[Table[DynamicModule[{col = Black, pos = {i, j}},
> EventHandler[Style[myData[[i, j]], FontColor -> Dynamic[col]],
> {"MouseDown" :> (clickpos = pos; col = Red),
> "MouseUp" :> (col = Black)}]], {i, 3}, {j, 3}],
> Frame -> All,
> BaseStyle -> {Editable -> False, Selectable -> False}]
>
> In the grid that results, the cell selection only works if the mouse is
> clicked inside the text in the cell, not in any whitespace, and the empty
> cell cannot be selected at all. This is a problem for me. But how does it
> work anyway? Eventhandler seems to be wrapped around the cell contents,
> rather than the cell itself, but why is there a dynamic value of {i,j}
> associated with the cell? I assume that it is something to do with the
> fact that pos is defined within Dynamic, but how does the code know what
> value of {i,j} is associated with each block of displayed text? Is this
> some normal behaviour of Table that I was not aware of, and where would I
> look to find more information about this behaviour? More to the point, is
> there a way of forcing each CELL to take the value of {i,j}, because this
> would solve my problem instantly?
>
> Incidentally, if you double-click in the empty cell, the whole table goes
> blue - why is that?
>
> Many thanks, but I still cannot believe that such a simple task is=
 proving
> to be so complicated.
>
> Cheers,
>
> Paul.
>
> -----Original Message-----
> From: Sjoerd C. de Vries [mailto:sjoerd.c.devries at gmail.com]
> Sent: 05 April 2009 11:40
> To: mathgroup at smc.vnet.net
> Subject: [mg98326] [mg98312] Re: Can nobody help me? Re: selecting a
> column in a grid
>
> Hi Paul,
>
> Cool down ;-) Try this for inspiration:
>
> Dynamic[clickpos]
> Grid[
> Table[
> DynamicModule[
> {col=Black, pos = {i, j}},
> EventHandler[
> Style[RandomInteger[{0, 1000000}], FontColor -> Dynamic[col]],
> {"MouseDown" :> (clickpos = pos; col = Red),
> "MouseUp" :> (col = Black)}
> ]
> ], {i, 5}, {j, 5}]
> ]
>
> Cheers -- Sjoerd
>
> On Apr 4, 3:12 am, "Paul Ellsmore" <paul.ellsm... at nanion.co.uk> wrote:
>> The key point in this problem is that the columns are not equal widths,
>> and in fact are variable width, depending on the particular data
>> displayed. So I set ItemSize to automatic, so that each column width is
>> defined by the data. The trouble is that MousePosition gives me
>> coordinates that are a linear function of the position in the grid, but
>> the columns are not linearly spaced in the Grid, so how do I know which
>> column the mouse is in?
>>
>> I need one of three things, all three of which are beyond my skills:
>>
>> 1) I could get the actual values that ItemSize is set to when Automatic
>> is used. CurrentValue[ItemSize] doesn't work. Anyone got an alternative?
>> 2) I could calculate what values ItemSize will be set to by looking at
>> the type and  number of characters in each grid cell, knowing the font
>> size, font family, cell margins, frame linewidth etc. But I don't have
>> access to the information needed - how many pixels does a Courier Size
>> 10 "n" take up?
>> 3) I could use EventHandler in a better way. Currently I wrap it around
>> the whole Grid, but if I could wrap it around a cell, that would work
>> for me. No syntax that I have tried works - should I try using GridBox,
>> or is there an obvious better idea?
>>
>> Nobody got any ideas at all? What kind of language is this, where you
>> can't even select a column in a table?
>>
>> Cheers,
>>
>> Paul.
>>
>> -----Original Message-----
>> From: Paul Ellsmore [mailto:paul.ellsm... at nanion.co.uk]
>> Sent: 01 April 2009 11:55
>> Subject:  selecting a column in a grid
>>
>> Hi,
>>
>> I have a tricky problem with a graphical user interface. I have a grid
>> of data, and I want to be able to MouseClick in the grid to either
>> select a row or a column. Using GUIKit, this is trivial, but I want to
>> be able to distribute my program using PlayerPro, which won't support
>> GUIKit.
>>
>> Selecting a row is straightforward using EventHandler and
>> EventHandlerScaled, since I know how many rows there are and they are
>> all one line in height. However, the column widths change according to
>> the data, since I have set ItemSize to Auto. So is there some way of
>> retrieving the actual values used in ItemSize, when Auto is used? I have
>> tried CurrentValue[ItemSize] in many variations, but it always fails.
>>
>> I could, of course, look at the data that will be in the grid, and
>> compute the widths of each column accordingly, and synthesize the values
>> that ItemSize will take. To do that, though, I would need to know what
>> the pixel width of a particular text string will be, which varies with
>> font family, size etc. Is there an easy way to do this? This is
>> essentially what Mathematica does when ItemSize is set to Auto, but I
>> have no idea how to do it myself.
>>
>> Currently I have EventHandler wrapped around the whole grid. Is it
>> possible to wrap it around each individual cell in a grid? I have tried
>> a few ideas, but they don't seem to work. The documentation suggests
>> that EventHandler can be wrapped around any expression, but that doesn't
>> seem to be the case, or at least the wrapped expression doesn't seem to
>> display properly inside other constructs, such as Grid, Column etc.
>>
>> Is there some better approach I could take, rather than using
>> EventHandler?
>
>> All ideas gratefully received.
>>
>> Cheers,
>>
>> Paul.
>>
>> Dr. Paul A. Ellsmore
>>
>> Nanion Limited
>>
>> Oxford Centre for Innovation
>>
>> Mill Street
>>
>> Oxford
>>
>> United Kingdom
>>
>> OX2 0JX
>>
>> Tel: +44 (0) 1865 811175
>>
>> Fax: +44 (0) 1865 248594





  • Prev by Date: Re: StringJoin with ToString
  • Next by Date: Re: UNDO and Mathematica - how useless is it?
  • Previous by thread: Re: Re: Can nobody help me? Re: selecting a column in a grid
  • Next by thread: Is there a BNF for Mathematica?