| 1 | /* manual_demoWindow.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2026 Paul Boersma |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "ManPagesM.h" |
| 20 | |
| 21 | void manual_demoWindow_init (ManPages me); |
| 22 | void manual_demoWindow_init (ManPages me) { |
| 23 | |
| 24 | MAN_PAGES_BEGIN |
| 25 | R"~~~( |
| 26 | ################################################################################ |
| 27 | "Demo window" |
| 28 | © Paul Boersma 2009–2023 |
| 29 | |
| 30 | The Demo window is a window in which you can draw and ask for user input. |
| 31 | You can use it for demonstrations, presentations, simulations, adaptive listening experiments, |
| 32 | and stand-alone programs (for the latter, see @@Scripting 9. Turning a script into a stand-alone program@). |
| 33 | |
| 34 | , @@Demo window 1. My first Demo window script@ (@`demo`) |
| 35 | , @@Demo window 2. Getting user input@ (@`demoWaitForInput`, @`demoClicked`, @`demoKeyPressed`, @`demoKey$`) |
| 36 | , @@Demo window 3. Getting click locations@ (@`demoX`, @`demoY`, @`demoClickedIn`) |
| 37 | , @@Demo window 4. Full-screen viewing@ |
| 38 | , @@Demo window 5. Asynchronous play@ (@`asynchronous`) |
| 39 | , @@Demo window 6. Animation@ (@`demoShow`, @`sleep`, @`demoPeekInput`) |
| 40 | , @@Demo window 7. Miscellaneous@ (@`demoShiftKeyPressed`, @`demoWindowTitle`) |
| 41 | , @@Demo window 8. Tips and Tricks@ |
| 42 | |
| 43 | ################################################################################ |
| 44 | "Demo window 1. My first Demo window script" |
| 45 | © Paul Boersma 2009–2023,2025 |
| 46 | |
| 47 | The Demo window is Praat’s least visible window: you can create it only through a script. |
| 48 | Try the following script after selecting a Sound object: |
| 49 | {; |
| 50 | \#`{demo} \@{Sound: ||Draw:} 0, 3, -1, 1, “yes”, “curve” |
| 51 | } |
| 52 | You see the Demo window turn up on the screen, with the Sound painted into it. |
| 53 | It works because the @@Sound: ||Draw...@ command is available in the Objects window when you select a Sound. Then try: |
| 54 | {; |
| 55 | \#`{demo} \@{Draw line:} 0, -1, 3, 1 |
| 56 | } |
| 57 | You see a line drawn from (0 seconds, -1 Pa) to (3 seconds, +1 Pascal) in the waveform. |
| 58 | It works because the @@Draw line...@ command is available in the Picture window. Then try: |
| 59 | {; |
| 60 | \#`{demo} \@{Erase all} |
| 61 | \#`{demo} \@{Red} |
| 62 | \#`{demo} \@{Axes:} 0, 100, 0, 100 |
| 63 | \#`{demo} \@{Text:} 50, “centre”, 50, “half”, “Hello” |
| 64 | } |
| 65 | You see a text appear in red, in the centre of the window. |
| 66 | This works because you are using commands from the Picture window, including the @@Axes...@ command, |
| 67 | which sets the world coordinates to something else than before (before, the world coordinates were determined by the Sound). |
| 68 | |
| 69 | Now suppose you want the Sound to appear in the top half of the window, |
| 70 | and some texts in the bottom left and bottom right corners of the window. |
| 71 | You can use @@Select outer viewport...@ and @@Select inner viewport...@, |
| 72 | if you know that the size of the Demo window is “100” horizontally and “100” vertically (rather than 60\xx60 inch, as the @@Picture window@), |
| 73 | and that the point (0, 0) lies in the bottom left (rather than the top left, as in the Picture window): |
| 74 | {; |
| 75 | \#`{demo} \@{Erase all} |
| 76 | \#`{demo} \@{Black} |
| 77 | \#`{demo} \@{Times} |
| 78 | \#`{demo} \@{24} |
| 79 | \#`{demo} \#@{Select outer viewport:} 0, 100, 50, 100 |
| 80 | \#`{demo} \@{Sound: ||Draw:} 0, 0, 0, 0, “yes”, “curve” |
| 81 | \#`{demo} \#@{Select inner viewport:} 0, 100, 0, 100 |
| 82 | \#`{demo} \@{Axes:} 0, 10, 0, 10 |
| 83 | \#`{demo} \@{Text:} 0, “left”, 0, “bottom”, “Left bottom corner” |
| 84 | \#`{demo} \@{Text:} 10, “right”, 0, “bottom”, “Right bottom corner” |
| 85 | } |
| 86 | As the title page of a presentation, you could do: |
| 87 | {; |
| 88 | \#`{demo} \@{Erase all} |
| 89 | \#`{demo} \@{Select inner viewport:} 0, 100, 0, 100 |
| 90 | \#`{demo} \@{Axes:} 0, 100, 0, 100 |
| 91 | \#`{demo} \@{Paint rectangle:} “purple”, 0, 100, 0, 100 |
| 92 | \#`{demo} \@{Pink} |
| 93 | \#`{demo} \@{Text:} 50, “centre”, 50, “half”, “This is my title” |
| 94 | } |
| 95 | ################################################################################ |
| 96 | "Demo window 2. Getting user input" |
| 97 | © Paul Boersma 2009–2023,2026 |
| 98 | |
| 99 | For almost all applications, you will want the user (or the participant in an experiment) to be able to click on things in the Demo window, |
| 100 | or to control the Demo window by pressing keys. Here is a presentation with two screens: |
| 101 | {; |
| 102 | \`{demo} \@{Erase all} |
| 103 | \`{demo} \@{Select inner viewport:} 0, 100, 0, 100 |
| 104 | \`{demo} \@{Axes:} 0, 100, 0, 100 |
| 105 | \`{demo} \@{Paint rectangle:} “purple”, 0, 100, 0, 100 |
| 106 | \`{demo} \@{Pink} |
| 107 | \`{demo} \@{Text:} 50, “centre”, 50, “half”, “This is the first page” |
| 108 | \`{demoWaitForInput} () |
| 109 | \`{demo} \@{Erase all} |
| 110 | \`{demo} \@{Paint rectangle:} “purple”, 0, 100, 0, 100 |
| 111 | \`{demo} \@{Text:} 50, “centre”, 50, “half”, “This is the second page” |
| 112 | } |
| 113 | In this example, you go from the first to the second screen either by clicking with the mouse or by pressing any key. |
| 114 | You will usually want to be more selective in your choice of user actions to respond to. |
| 115 | The function @`demoWaitForInput` always returns 1, so that you can use it nicely in a loop, in which you can react selectively: |
| 116 | {; |
| 117 | \`{label} FIRST_SCREEN |
| 118 | \`{demo} \@{Erase all} |
| 119 | \`{demo} \@{Black} |
| 120 | \`{demo} \@{Times} |
| 121 | \`{demo} \@{24} |
| 122 | \`{demo} \@{Select inner viewport:} 0, 100, 0, 100 |
| 123 | \`{demo} \@{Axes:} 0, 100, 0, 100 |
| 124 | \`{demo} \@{Paint rectangle:} “purple”, 0, 100, 0, 100 |
| 125 | \`{demo} \@{Pink} |
| 126 | \`{demo} \@{Text:} 50, “centre”, 50, “half”, “This is the first page” |
| 127 | while \#`{demoWaitForInput} () |
| 128 | if \`{demoClicked} () |
| 129 | goto SECOND_SCREEN |
| 130 | elsif \`{demoKeyPressed} () |
| 131 | if \`{demoKey$} () = “→” or \`{demoKey$} () = “ ” |
| 132 | goto SECOND_SCREEN |
| 133 | endif |
| 134 | endif |
| 135 | endwhile |
| 136 | \`{label} SECOND_SCREEN |
| 137 | \`{demo} \@{Erase all} |
| 138 | \`{demo} \@{Paint rectangle:} “purple”, 0, 100, 0, 100 |
| 139 | \`{demo} \@{Text:} 50, “centre”, 50, “half”, “This is the second page” |
| 140 | while \#`{demoWaitForInput} () |
| 141 | if \`{demoClicked} () |
| 142 | goto END |
| 143 | elsif \`{demoKeyPressed} () |
| 144 | if \`{demoKey$} () = “←” |
| 145 | goto FIRST_SCREEN |
| 146 | elsif \`{demoKey$} () = “→” or \`{demoKey$} () = “ ” |
| 147 | goto END |
| 148 | endif |
| 149 | endif |
| 150 | endwhile |
| 151 | \`{label} END |
| 152 | } |
| 153 | This script allows you to use the arrow keys and the space bar to navigate between the two screens. A shorter version is: |
| 154 | {; |
| 155 | \`{label} FIRST_SCREEN |
| 156 | \#`{demo} \@{Erase all} |
| 157 | \#`{demo} \@{Black} |
| 158 | \#`{demo} \@{Times} |
| 159 | \#`{demo} \@{24} |
| 160 | \#`{demo} \@{Select inner viewport:} 0, 100, 0, 100 |
| 161 | \#`{demo} \@{Axes:} 0, 100, 0, 100 |
| 162 | \#`{demo} \@{Paint rectangle:} "purple", 0, 100, 0, 100 |
| 163 | \#`{demo} \@{Pink} |
| 164 | \#`{demo} \@{Text:} 50, "centre", 50, "half", "This is the first page" |
| 165 | while \#`{demoWaitForInput} () |
| 166 | goto SECOND_SCREEN \#`{demoInput} (“•→ ”) |
| 167 | endwhile |
| 168 | \`{label} SECOND_SCREEN |
| 169 | \#`{demo} \@{Erase all} |
| 170 | \#`{demo} \@{Paint rectangle:} "purple", 0, 100, 0, 100 |
| 171 | \#`{demo} \@{Text:} 50, "centre", 50, "half", "This is the second page" |
| 172 | while \#`{demoWaitForInput} () |
| 173 | goto END \#`{demoInput} (“•→ ”) |
| 174 | goto FIRST_SCREEN \#`{demoInput} (“←”) |
| 175 | endwhile |
| 176 | \`{label} END |
| 177 | } |
| 178 | This uses two tricks, namely the possibility of following the #`goto` statement by a condition |
| 179 | and using @`demoInput` to quickly test for multiple possible inputs (the bullet represents a mouse click). |
| 180 | |
| 181 | ################################################################################ |
| 182 | "Demo window 3. Getting click locations" |
| 183 | © Paul Boersma 2009-2023 |
| 184 | |
| 185 | You can use the functions @`demoX` and @`demoY` to see where the user has clicked. |
| 186 | These functions respond in world coordinates. To see whether the user has clicked in the sound that occupies the |
| 187 | upper half of the screen in the above example, you do for instance |
| 188 | {; |
| 189 | while \`{demoWaitForInput} () |
| 190 | if \`{demoClicked} () |
| 191 | \`{demo} Select outer viewport: 0, 100, 50, 100 |
| 192 | \`{demo} Axes: 0, 3, -1, 1 |
| 193 | if \#`{demoX} () >= 0 and \#`{demoX} () < 3 and \#`{demoY} () >= -1 and \#`{demoY} () < 1 |
| 194 | } |
| 195 | The last line can be shortened to: |
| 196 | {; |
| 197 | if \#`{demoClickedIn} (0, 3, -1, 1) |
| 198 | } |
| 199 | Another example of when you want to know the click location is when you test for a click on a button |
| 200 | that you drew on the screen: |
| 201 | {; |
| 202 | \#`{demo} Paint rounded rectangle: “pink”, 30, 70, 16, 24 |
| 203 | \#`{demo} Text: 50, “centre”, 20, “half”, “Analyse” |
| 204 | while \#`{demoWaitForInput} () |
| 205 | goto ANALYSE \#`{demoClickedIn} (30, 70, 16, 24) |
| 206 | } |
| 207 | ################################################################################ |
| 208 | "Demo window 4. Full-screen viewing" |
| 209 | © Paul Boersma 2009–2023 |
| 210 | |
| 211 | When you click in the “zoom box” (the green button in the title bar of the Demo window on the Mac), |
| 212 | the Demo window will zoom out very strongly: it will fill up the whole screen. The menu bar becomes invisible, |
| 213 | although you can still make it temporarily visible and accessible by moving the mouse to the upper edge of the screen. |
| 214 | The Dock also becomes invisible, although you can make it temporarily visible and accessible by moving the mouse to the edge |
| 215 | of the screen (the left, bottom, or right edge, depending on where your Dock normally is). |
| 216 | When you click the zoom box again, the Demo window is restored to its original size. |
| 217 | See also @@Demo window 8. ||Tips and Tricks@. |
| 218 | |
| 219 | ################################################################################ |
| 220 | "Demo window 5. Asynchronous play" |
| 221 | © Paul Boersma 2009–2023 |
| 222 | |
| 223 | If you select a Sound and execute the command |
| 224 | {; |
| 225 | \@{Sound: ||Play} |
| 226 | } |
| 227 | Praat will play the whole sound before proceeding to the next line of your script. |
| 228 | You will often instead want Praat to continue running your script while the sound is playing. |
| 229 | To accomplish that, use the @`asynchronous` directive: |
| 230 | {; |
| 231 | \@{Create Sound as pure tone:} “tone”, 1, 0, 0.2, 44100, 440, 0.2, 0.01, 0.01 |
| 232 | \#`{asynchronous} \@{Sound: ||Play} |
| 233 | \@{Remove} |
| 234 | } |
| 235 | The sound will continue to play, even after the Sound object has been removed. |
| 236 | |
| 237 | Please note that a following Play command will interrupt the playing of the first: |
| 238 | {; |
| 239 | while \`{demoWaitForInput} () |
| 240 | if \`{demoClicked} () |
| 241 | \@{Create Sound as pure tone:} “tone”, 1, 0, 3.0, 44100, |
| 242 | ... \`{randomGauss} (440, 100), 0.2, 0.01, 0.01 |
| 243 | \#`{asynchronous} \@{Sound: ||Play} |
| 244 | \@{Remove} |
| 245 | endif |
| 246 | endwhile |
| 247 | } |
| 248 | The first sound will stop playing soon after the user clicks for the second time. |
| 249 | |
| 250 | ################################################################################ |
| 251 | "Demo window 6. Animation" |
| 252 | © Paul Boersma 2009–2023 |
| 253 | |
| 254 | In the above examples, things will often get drawn to the screen with some delay, |
| 255 | i.e., you may not see the erasures and paintings happen. This is because several operating systems |
| 256 | use %buffering of graphics. These systems will draw the graphics only just before getting user input. |
| 257 | This means that @`demoWaitForInput` is the place where your drawings will typically be painted on the screen. |
| 258 | If you want painting to happen earlier (e.g. in animations), you can use |
| 259 | {; |
| 260 | \#`{demoShow} () |
| 261 | } |
| 262 | Also in animations, you will often want to regulate the time span between two consecutive drawings. |
| 263 | If you want 0.05 seconds between drawings, you can put Praat to sleep temporarily with |
| 264 | {; |
| 265 | \#`{sleep} (0.05) |
| 266 | } |
| 267 | If you need user input during the animation, you can replace @`demoWaitForInput` or #`demoShow` with |
| 268 | {; |
| 269 | \#`{demoPeekInput}() |
| 270 | } |
| 271 | which returns immediately without waiting and will tell you (via e.g. @`demoClicked` or @`demoKey$`) |
| 272 | whether a mouse or key event happened during drawing or sleeping. |
| 273 | |
| 274 | ################################################################################ |
| 275 | "Demo window 7. Miscellaneous" |
| 276 | © Paul Boersma 2009–2023 |
| 277 | |
| 278 | To see whether any function keys are pressed (during a mouse click or key press), |
| 279 | you can use @`demoShiftKeyPressed` (), @`demoCommandKeyPressed` (), and @`demoOptionKeyPressed` (). |
| 280 | |
| 281 | To put some text in the title bar of the Demo window, try |
| 282 | {; |
| 283 | \`{demoWindowTitle}: “This is the title of my presentation” |
| 284 | } |
| 285 | ################################################################################ |
| 286 | "Demo window 8. Tips and Tricks" |
| 287 | © Paul Boersma 2009–2023 |
| 288 | |
| 289 | The initial size of the Demo window when you start it up is 1344\xx756 pixels, |
| 290 | which is 70 percent of a standard wide screen (1920\xx1080 pixels). |
| 291 | This means that if a font looks good at a size of 35 in the initial Demo window, |
| 292 | the font will look equally good at a size of 50 when you use a 1920\xx1080 video projector full-screen. |
| 293 | |
| 294 | If you resize the Demo window with the handle in the bottom left, or if you zoom the window out to the full screen, |
| 295 | you may see that the relative positions of the contents of the window will change. Also, clicking on buttons and in parts |
| 296 | of the window may yield unexpected %x and %y values. It is therefore advisable to resize the window only if you are on a page |
| 297 | that you can get out of by pressing a key, or by clicking anywhere in the window without using @`demoX`, @`demoY` or @`demoClickedIn`. |
| 298 | |
| 299 | If you click away the Demo window while it is waiting for input, you get a message saying “You interrupted the script...”. |
| 300 | If you do not want to see this message, you should make sure that the user can reach the end of the script, for instance by |
| 301 | pressing the \-> key on the last page. To make sure the user sees that the script has ended, you could end it with #`demo Erase all`. |
| 302 | |
| 303 | Your demo can save its preferences in a folder of your choice, |
| 304 | e.g. in #`preferencesDirectory$ + “/../GuineaPigAnalyzer”` if your demo is called GuineaPigAnalyzer. |
| 305 | If you want to be less conspicuous and like to use the Praat preferences folder instead, |
| 306 | please use the #`apps` subfolder, in this way: |
| 307 | {; |
| 308 | \`{createFolder}: preferencesDirectory$ + “/apps” |
| 309 | \`{createFolder}: preferencesDirectory$ + “/apps/GuineaPigAnalyzer” |
| 310 | } |
| 311 | ################################################################################ |
| 312 | )~~~" |
| 313 | MAN_PAGES_END |
| 314 | |
| 315 | } |
| 316 | |
| 317 | /* End of file manual_demoWindow.cpp */ |