Wait for mouse events.
If mouse button pressed in "Windows" box then;
         Scan Window List for Window titles, 
         Open Popup Menu with titles,
         Wait for Mouse up to select window, 
         Bring selected window to front and activate,
         Close Popup Menu.
If mouse button pressed in close box then
         Close window and exit.

         The small window contains three buttons, a close box, an about box, and a button for the popup menu. I did not use Intuition Gadgets for these because I could tell which button was hit simply by looking at the X-position of the mouse. Gadgets are worth using for more complex applications, but this technique often provides a simpler solution. The code that analyzes the X-position is in the word FW.PROCESS in Find_Window.f.

         Most of the program is fairly straightforward and can be deciphered from the comments. There are two areas, however, which need some explanation: scanning the windows for their titles, and the popup menu.


         The most important trick in FindWindow is getting the titles of all the open windows. On the Amiga, the first place to look for information is usually in the Library Base structures. When you open a library you get an address to a Library Base structure. Below this address in memory are the jumps to the various functions in the library. Above this address is a structure which contains lots of information used by the library.

         In the Intuition Library, there is a pointer to the Active Screen. Since we will be using the Workbench, that will be the Active Screen. Every screen has a pointer to its first window. Each window has a pointer to the next window in the screen. Here is a summary of what points to what:

IntuitionBase -> ActiveScreen -> FirstWindow -> NextWindow

         Let's use JForth to interactively explore these structures. This is often the way a JForth programmer will work. First figure out how the Amiga works by interactively experimenting. Once the technique is understood, then it is time to write the code. If you have JForth, fire it up. If not, follow along and we'll explain things as we go.

         First we should compile some tools to help us. DUMP_STRUCT will print the names and contents of the members of a structure. VALUE is a handy type of variable. Enter in JForth:

include ju:dump structinclude ju:value

After 2 or 3 seconds, JForth will print how many bytes were added to the dictionary. Now let's attach the precompiled include files that contain the Amiga structure definitions. Enter:

  getmodule includes

         To open the Intuition library in JForth, we can use INTUITION?. This will place the address of the IntuitionBase in a variable called INTUITION_LIB. We can fetch the address using @ then declare a value called IBASE that we can use to refer to the structure by name. Enter in JForth:

         intuition? intuition_lib @ >rel value IBASE

         IBASE contains the address of the IntuitionBase structure. To dump out the contents of the IntuitionBase, enter:

         ibase dst IntuitionBase

          Here is a partial listing of the result:

         652,088 S4 IB_ACTIVEWINDOW
         582,320 S4 IB_ACTIVESCREEN <-   ! ! !
         582,320 S4 IB_FIRSTSCREEN
           73,740 U4 IB_FLAGS
                329 S2 IB_MOUSEY

         Notice that the Mouse X,Y-position is stored in this structure. Just for fun, try moving the mouse and then reprinting the structure to see the numbers change. We could write to this structure using the S! operator. For more fun than most people can stand, push the mouse over to the left side of the screen and enter this:

         250 ibase s! ib_MouseX

         Then touch the mouse and watch the cursor jump! Please note that the IntuitionBase is considered PRIVATE and READONLY by Commodore. Do not write to this structure in any of the programs that you develop.
The IntuitionBase structure contains a Library structure which in turn contains a Node. To see these structures, enter:

         ibase dst library 
         ibase dst node

         Now let's get back to what we're supposed to be doing. The IntuitionBase also contains the address of the currently Active Screen. We can fetch from this structure member using the S@ operator.

ibase s@ ib_ ActiveScreen value ASCR ascr dst screen ( dump screen structure )

From the screen, we can get the address of the first window. We can then fetch the title of the window and print it. The word 0COUNT, pronounced "zero count," will take a C-style NUL terminated string and return the length of the string and the address of the first character which are needed by TYPE. Enter:

ascr s@ sc_FirstWindow value MYWIND mywind s@ wd_title 0count type