Online Book Reader

Home Category

Beautiful Code [319]

By Root 5351 0
the root node of the tree, reverse traversal, etc.

The importance of this extra mode of operation can hardly be overstated: not only did it substantially increase the speed of text entry and correction, it provided tremendous flexibility to the developers.

An elegant solution was needed to make the long-click menu context-sensitive, for it would have been too cumbersome to create a special long-click menu for each node of the binary tree. Like the tree, long-click menus are stored in the form of text files, which are editable in eLocutor. In selecting the appropriate long-click menu, eLocutor looks to see which node is highlighted. If a text file exists with the same name as the node in the long-click directory, it is picked up as the menu. If it doesn't, eLocutor looks for the name of the node one level above in the tree, and so on.

In this way, each subtree can have its own long-click menu, entirely under the control of the user. Another way to present this design is to say that unless a child menu item chooses to override the long-click menu defined for its parent, the child automatically inherits the parent's menu.

Partial code for implementing the long click is shown in Example 30-1. OpenLongClick-File looks for and opens a file with the same name as the parameter passed to it, and if that is not found, recursively looks for one with the name of its parent. Each time the long-click timer times out, a fresh line from this file is displayed in the text box tblongclick. When the button is released, the command in tblongclick is selected. Depending on how long the button is held pressed, the long-click timer runs out repeatedly. Each time the timer runs out, it causes the code in Example 30-1 to check and set the Boolean variable ThisIsALongClick, and then to execute some code that needs to run only once in each long click in order to select and open the appropriate long-click file for reading.

The portion that repeats upon each expiration of the long-click timer reads a line from the file and displays it in the tblongclick text box. When the file reaches the end, it is closed and reopened, and the first line is read in. When the button is released, ThisIsALongClick is reset.

Example 30-1. Implementing context-sensitive menu selection for the long click

Private Sub longclick_Timer()

Dim st As String

Dim filenum As Long

If Not ThisIsALongClick Then

ThisIsALongClick = True

If MenuTree.SelectedItem.Text = stStart Then

'we are at the root already

OpenLongClickFile MenuTree.SelectedItem

Else

OpenLongClickFile MenuTree.SelectedItem.Parent

'find the list of long-click menu choices suited for this context

End If

End If

If EOF(longclickfilenum) Then

'list of choices finished, cycle to the first one by reopening file

Close #longclickfilenum

Open stlongclickfilename For Input As #longclickfilenum

End If

Line Input #longclickfilenum, st

tblongclick = st

End Sub

Commands made available using the long click include:

>Start

Takes you to the root of the tree (the > indicates a "go to").

Upwards

Moves the cursor backward and upward in the tree until the right mouse button is clicked. Useful when you did not press the button when the desired menu choice was highlighted—i.e., you missed your turn.

Type This

Types whatever is highlighted in the tree into the middle box. Available only under the Type subtree.

Set Filter

Copies whatever is highlighted in the tree into the filter; useful for searching text. Also available as a long-click option only when the highlighted item is under the Type subtree.

Words Up, Words Down

For rapid scrolling during typing, described later.

Pause

Useful when a command has to be executed repeatedly. When the user holds the button down for a long click, the menu tree freezes, with one of its items highlighted. Selecting the long-click Pause option maintains this state of suspension. Now, each time the user clicks, the command highlighted in the menu tree is executed. To come out of pause, a long click must again be used.

Help

Opens up and plays a context-sensitive

Return Main Page Previous Page Next Page

®Online Book Reader