| 1 |
#charset "us-ascii" |
| 2 |
|
| 3 |
/* |
| 4 |
* TADS 3 Library - Menu System, console edition |
| 5 |
* |
| 6 |
* This implements the menusys user interface for the traditional |
| 7 |
* console-mode interpreters. |
| 8 |
*/ |
| 9 |
#include "adv3.h" |
| 10 |
|
| 11 |
|
| 12 |
/* ------------------------------------------------------------------------ */ |
| 13 |
/* |
| 14 |
* Menu Item - user interface implementation for the console |
| 15 |
*/ |
| 16 |
modify MenuItem |
| 17 |
/* |
| 18 |
* Call menu.display when you're ready to show the menu. This |
| 19 |
* should be called on the top-level menu; we run the entire menu |
| 20 |
* display process, and return when the user exits from the menu |
| 21 |
* tree. |
| 22 |
*/ |
| 23 |
display() |
| 24 |
{ |
| 25 |
/* save the top-level key list */ |
| 26 |
MenuItem.curKeyList = keyList; |
| 27 |
|
| 28 |
/* set myself as the current menu and the top-level menu */ |
| 29 |
MenuItem.topMenu = self; |
| 30 |
MenuItem.curMenu = self; |
| 31 |
|
| 32 |
/* display the menu in the javascript client */ |
| 33 |
showMenu(nil); |
| 34 |
|
| 35 |
/* process network events until the user closes the menu */ |
| 36 |
MenuItem.isOpen = true; |
| 37 |
processNetRequests({: !MenuItem.isOpen }); |
| 38 |
} |
| 39 |
|
| 40 |
/* current menu, and current top-level menu */ |
| 41 |
curMenu = nil |
| 42 |
topMenu = nil |
| 43 |
|
| 44 |
/* is the menu open? */ |
| 45 |
isOpen = nil |
| 46 |
|
| 47 |
/* show this menu as a submenu */ |
| 48 |
showMenu(from) |
| 49 |
{ |
| 50 |
/* get the XML representation of the menu item list */ |
| 51 |
local xml = '<menusys><<getXML(from)>></menusys>'; |
| 52 |
|
| 53 |
/* tell the javascript client to display the menu */ |
| 54 |
webMainWin.sendWinEvent(xml); |
| 55 |
|
| 56 |
/* save this in the main window as the current menu state */ |
| 57 |
webMainWin.menuSysState = xml; |
| 58 |
} |
| 59 |
|
| 60 |
/* navigate into a submenu */ |
| 61 |
enterSubMenu(idx) |
| 62 |
{ |
| 63 |
/* validate the index and select the new menu */ |
| 64 |
if (idx >= 1 && idx <= contents.length()) |
| 65 |
{ |
| 66 |
/* get the new menu */ |
| 67 |
local m = contents[idx]; |
| 68 |
|
| 69 |
/* note the new menu location */ |
| 70 |
MenuItem.curMenu = m; |
| 71 |
|
| 72 |
/* show the new menu */ |
| 73 |
m.showMenu(self); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/* |
| 78 |
* Package my menu items as XML, to send to the javascript API. |
| 79 |
* 'from' is the menu we just navigated from, if any. This is nil |
| 80 |
* when we enter the top level menu, since we're not navigating from |
| 81 |
* another menu; when we navigate from a parent to a child, this is |
| 82 |
* the parent; when we return from a child to a parent, this is the |
| 83 |
* child; and when we move directly from sibling to sibling (via a |
| 84 |
* next/previous chapter command), this is the sibling. When we |
| 85 |
* display a new topic in a topic list menu, this is simply 'self'. |
| 86 |
*/ |
| 87 |
getXML(from) |
| 88 |
{ |
| 89 |
/* set up a string buffer for the xml */ |
| 90 |
local s = new StringBuffer(); |
| 91 |
|
| 92 |
/* update the menu contents */ |
| 93 |
updateContents(); |
| 94 |
|
| 95 |
/* start with the menu title */ |
| 96 |
s.append('<title><<title.htmlify()>></title>'); |
| 97 |
|
| 98 |
/* note if we're the top-level menu */ |
| 99 |
if (location == nil || MenuItem.topMenu == self) |
| 100 |
s.append('<isTop/>'); |
| 101 |
|
| 102 |
/* run through the contents */ |
| 103 |
for (local item in contents) |
| 104 |
s.append('<menuItem><<item.title.htmlify()>></menuItem>'); |
| 105 |
|
| 106 |
/* if the 'from' menu is a child, initially select it */ |
| 107 |
local idx = contents.indexOf(from); |
| 108 |
if (idx != nil) |
| 109 |
s.append('<fromChild><<idx>></fromChild>'); |
| 110 |
|
| 111 |
/* add the keys */ |
| 112 |
getKeysXML(s); |
| 113 |
|
| 114 |
/* return the string */ |
| 115 |
return toString(s); |
| 116 |
} |
| 117 |
|
| 118 |
/* get the XML description of the top-level key list */ |
| 119 |
getKeysXML(buf) |
| 120 |
{ |
| 121 |
buf.append('<keylists>'); |
| 122 |
for (local kl in curKeyList) |
| 123 |
{ |
| 124 |
buf.append('<keylist>'); |
| 125 |
for (local k in kl) |
| 126 |
{ |
| 127 |
if (k == ' ') |
| 128 |
k = 'U+0020'; |
| 129 |
buf.append('<key><<k>></key>'); |
| 130 |
} |
| 131 |
buf.append('</keylist>'); |
| 132 |
} |
| 133 |
buf.append('</keylists>'); |
| 134 |
} |
| 135 |
|
| 136 |
/* |
| 137 |
* Prepare a title or content string for our XML output. If 'val' is |
| 138 |
* a string, we'll run it through the output formatter to expand any |
| 139 |
* special <.xxx> sequences. If 'val' is a property, we'll evaluate |
| 140 |
* the property of self, capturing the output if it generates any or |
| 141 |
* capturing the string if it returns one. In all cases, we take the |
| 142 |
* result string and convert TADS special characters to HTML, and |
| 143 |
* finally html-escape the result for inclusion in XML output, and |
| 144 |
* return the resulting string. |
| 145 |
*/ |
| 146 |
formatXML(func) |
| 147 |
{ |
| 148 |
/* call the function and process it through the menu stream filters */ |
| 149 |
local txt = menuOutputStream.captureOutput(func); |
| 150 |
|
| 151 |
/* convert special characters and html-escape the result */ |
| 152 |
return txt.specialsToHtml().htmlify(); |
| 153 |
} |
| 154 |
; |
| 155 |
|
| 156 |
/* |
| 157 |
* Menu system UI request processor. This receives requests from the |
| 158 |
* javascript client in response to user actions: selecting a menu item, |
| 159 |
* navigating to the parent menu, closing the menu. |
| 160 |
*/ |
| 161 |
menuSysEventPage: WebResource |
| 162 |
vpath = '/webui/menusys' |
| 163 |
processRequest(req, query) |
| 164 |
{ |
| 165 |
/* check the action type */ |
| 166 |
if (query.isKeyPresent('close')) |
| 167 |
{ |
| 168 |
/* close the menu */ |
| 169 |
MenuItem.isOpen = nil; |
| 170 |
} |
| 171 |
else if (query.isKeyPresent('prev')) |
| 172 |
{ |
| 173 |
/* go to the parent menu, or close the menu if at the top */ |
| 174 |
local cur = MenuItem.curMenu; |
| 175 |
if (cur != nil && cur != MenuItem.topMenu && cur.location != nil) |
| 176 |
{ |
| 177 |
/* go to the parent */ |
| 178 |
local par = cur.location; |
| 179 |
MenuItem.curMenu = par; |
| 180 |
par.showMenu(cur); |
| 181 |
} |
| 182 |
else |
| 183 |
{ |
| 184 |
/* there's no parent - close the menu */ |
| 185 |
MenuItem.isOpen = nil; |
| 186 |
|
| 187 |
/* tell the UI to close its menu dialog */ |
| 188 |
webMainWin.sendWinEvent('<menusys><close/></menusys>'); |
| 189 |
webMainWin.menuSysState = ''; |
| 190 |
} |
| 191 |
} |
| 192 |
else if (query.isKeyPresent('select')) |
| 193 |
{ |
| 194 |
/* |
| 195 |
* Select a child menu. The 'select=n' parameter is the |
| 196 |
* index in the current menu's child list of the new item to |
| 197 |
* select. Retrieve the index. |
| 198 |
*/ |
| 199 |
MenuItem.curMenu.enterSubMenu(toInteger(query['select'])); |
| 200 |
} |
| 201 |
else if (query.isKeyPresent('nextTopic')) |
| 202 |
{ |
| 203 |
/* get the next topic in the current topic menu */ |
| 204 |
sendAck(req, MenuItem.curMenu.getNextTopicXML()); |
| 205 |
|
| 206 |
/* rebuild the menu state for the change */ |
| 207 |
webMainWin.menuSysState = |
| 208 |
'<menusys><<MenuItem.curMenu.getXML(MenuItem.curMenu) |
| 209 |
>></menusys>'; |
| 210 |
|
| 211 |
/* we've sent our reply, so we're done */ |
| 212 |
return; |
| 213 |
} |
| 214 |
else if (query.isKeyPresent('chapter')) |
| 215 |
{ |
| 216 |
/* get the next or previous chapter, as applicable */ |
| 217 |
local dir = query['chapter']; |
| 218 |
local m = MenuItem.curMenu; |
| 219 |
local par = m.location; |
| 220 |
local nxt = (dir == 'next' |
| 221 |
? par.getNextMenu(m) : par.getPrevMenu(m)); |
| 222 |
|
| 223 |
/* enter this chapter */ |
| 224 |
if (nxt != nil) |
| 225 |
{ |
| 226 |
MenuItem.curMenu = nxt; |
| 227 |
nxt.showMenu(m); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
/* acknowledge the request */ |
| 232 |
sendAck(req); |
| 233 |
} |
| 234 |
; |
| 235 |
|
| 236 |
/* ------------------------------------------------------------------------ */ |
| 237 |
/* |
| 238 |
* Menu topic item - console UI implementation |
| 239 |
*/ |
| 240 |
modify MenuTopicItem |
| 241 |
/* get the XML description of my menu list */ |
| 242 |
getXML(from) |
| 243 |
{ |
| 244 |
/* start with an empty result buffer */ |
| 245 |
local s = new StringBuffer(); |
| 246 |
|
| 247 |
/* update our contents, as needed */ |
| 248 |
updateContents(); |
| 249 |
|
| 250 |
/* add the title and total number of items in the menu */ |
| 251 |
s.append('<title><<title.htmlify()>></title>' |
| 252 |
+ '<numItems><<menuContents.length()>></numItems>'); |
| 253 |
|
| 254 |
/* note if we're the top-level menu */ |
| 255 |
if (location == nil || MenuItem.topMenu == self) |
| 256 |
s.append('<isTop/>'); |
| 257 |
|
| 258 |
/* add each item in our list */ |
| 259 |
for (local i in 1..lastDisplayed) |
| 260 |
s.append(getTopicXML(i)); |
| 261 |
|
| 262 |
/* add the keys */ |
| 263 |
getKeysXML(s); |
| 264 |
|
| 265 |
/* return the XML string */ |
| 266 |
return toString(s); |
| 267 |
} |
| 268 |
|
| 269 |
/* get the next topic, in XML format */ |
| 270 |
getNextTopicXML() |
| 271 |
{ |
| 272 |
/* if we're not already at the last item, advance the counter */ |
| 273 |
if (lastDisplayed < menuContents.length()) |
| 274 |
++lastDisplayed; |
| 275 |
|
| 276 |
/* format the last item */ |
| 277 |
return getTopicXML(lastDisplayed); |
| 278 |
} |
| 279 |
|
| 280 |
/* get the XML formatted description of the item at the given index */ |
| 281 |
getTopicXML(i) |
| 282 |
{ |
| 283 |
/* get the item */ |
| 284 |
local item = menuContents[i]; |
| 285 |
|
| 286 |
/* get the item's text, and format as XML */ |
| 287 |
item = formatXML(dataType(item) == TypeObject |
| 288 |
? {: item.getItemText() } : item); |
| 289 |
|
| 290 |
/* format the item text */ |
| 291 |
return '<topicItem><<item>></topicItem>'; |
| 292 |
} |
| 293 |
; |
| 294 |
|
| 295 |
/* ------------------------------------------------------------------------ */ |
| 296 |
/* |
| 297 |
* Long topic item |
| 298 |
*/ |
| 299 |
modify MenuLongTopicItem |
| 300 |
/* get my XML description */ |
| 301 |
getXML(from) |
| 302 |
{ |
| 303 |
/* start with an empty result buffer */ |
| 304 |
local s = new StringBuffer(); |
| 305 |
|
| 306 |
/* update our contents, as needed */ |
| 307 |
updateContents(); |
| 308 |
|
| 309 |
/* add my title (heading) */ |
| 310 |
local t = heading != nil && heading != '' ? heading : title; |
| 311 |
s.append('<title><<t.htmlify()>></title>'); |
| 312 |
|
| 313 |
/* add our contents */ |
| 314 |
s.append('<longTopic><<formatXML({: menuContents })>></longTopic>'); |
| 315 |
|
| 316 |
/* |
| 317 |
* if this is a chapter menu, note if we have links for the next |
| 318 |
* and previous chapters |
| 319 |
*/ |
| 320 |
if (isChapterMenu) |
| 321 |
{ |
| 322 |
local m; |
| 323 |
|
| 324 |
if ((m = location.getNextMenu(self)) != nil) |
| 325 |
s.append('<nextChapter><<m.title.htmlify()>></nextChapter>'); |
| 326 |
|
| 327 |
if ((m = location.getPrevMenu(self)) != nil) |
| 328 |
s.append('<prevChapter><<m.title.htmlify()>></prevChapter>'); |
| 329 |
} |
| 330 |
|
| 331 |
/* add the keys */ |
| 332 |
getKeysXML(s); |
| 333 |
|
| 334 |
/* return the XML string */ |
| 335 |
return toString(s); |
| 336 |
} |
| 337 |
; |