# WholeCellGroupOpener and Hyperlinks

My problem is: I would like to activate WholeCellGroupOpener on a cell containing hyperlinks. But when I do that, the hyperlinks are no longer active. Is there a way to change the order of the two, so that 'Hyperlink' is activated before 'WholeCellGroupOpener'.

More precisely: I want the cell group to collapse when I click anything but a hyperlink.

Does anyone know a way to achieve that?

-
Could you provide the simple code that you tried? –  halmir Jul 31 '13 at 22:58

I could only get the kind of behavior you seek with an EventHandler and PassDownEvents -> True, in this case ClickPane.

## Manually

Below is the ClickPane. We're going to replace "HL" manually by a Hyperlink later. First execute this and we'll paster the output later into the desired cell. I will use a "Section" cell for illustration; hence the style "Section". Alter to suit.

ClickPane[
Style["This is a link to HL but you can click anywhere else to open/close the cell group",
"Section"],
(SelectionMove[InputNotebook[], All, CellGroup];
FrontEndExecute[FrontEndToken["OpenCloseGroup"]]) &,
PassEventsDown -> True]


Second, create a hyperlink. We'll paste this later, too. We wrap it in an EventHandler so that click on the hyperlink does not close the cell group (because of PassEventsUp -> False). Styling the text as "Hyperlink" makes be colored like a hyperlink; without this, it gets the standard cell styling ("Section" in our example).

EventHandler[
{"MouseClicked" :> (Null)},
PassEventsDown -> True, PassEventsUp -> False]


Third, create the cell you want, say, a new section cell.

Fourth, select the cell bracket and copy the whole cell containing the ClickPane output.

Fifth, create an inline cell in the section (Control-9).

Sixth, paste the ClickPane output. You won't be able to edit it with the mouse. A mouse click will open or close the cell group. Edit by click next to the cell and use your arrow keys.

Seventh, using your arrow keys and backspace, get rid of the extra quotation marks.

Eighth, copy the hyperlink.

Ninth, using your arrow and shift keys, select the "HL". Paste the hyperlink. It should automatically style to "Section".

One drawback is that single letters are italicized, as though they are mathematical variables. You can manually select the "a" and unitalicize.

## Programatically

Below is a function to create a cell. The str follows a similar syntax as in StringForm: for each "" in str Replace substitutes each link in turn from the list links. (Note links here is to be a list, which is different from StringForm.) ToBoxes is pretty aggressive about putting extra quotes in, and the replace code at the end strips the extra quotes from the appropriate place.

openerHLForm[str_, links_, style_] :=
CellPrint@(Cell[BoxData@ToBoxes[
ClickPane[
Row[
Module[{n = 0},
Replace[
StringCases[str, "" | Whitespace | Except[WhitespaceCharacter] ..],
"" :> EventHandler[links[[++n]], {"MouseClicked" :> (Null)},
PassEventsDown -> True, PassEventsUp -> False],
1]],
BaseStyle -> style],
(SelectionMove[InputNotebook[], All, CellGroup];
FrontEndExecute[FrontEndToken["OpenCloseGroup"]]) &,
PassEventsDown -> True],
StandardForm], style] /.
tb_TemplateBox :>
MapAt[Replace[#, s_String :> StringTrim[s, "\""]] & /@ # &, tb, 1])


The patterns are not thoroughly tested, so you may have to adjust things for overly complicated settings. Note that single letters are not italicized.

openerHLForm["This is a link to  but you can click anywhere else to open/close the cell group",
"Section"]


openerHLForm["This is a link to  but you can click anywhere else, \
except , to open/close the cell group",
"Subsection"]


-
Thank you very much for your time and your useful and thorough explanation, but I am hoping for a simpler answer as I need to do this to a lot of cells. –  Sofic Aug 1 '13 at 12:21
@Sofic There are others with much more experience dealing with the front end. Let's hope there is a simpler way! :) –  Michael E2 Aug 1 '13 at 12:30