[futurebasic] Re: [FB] Check/Unchecking Menu Item

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : May 1999 : Group Archive : Group : All Groups

From: tedd <tedd@...>
Date: Mon, 17 May 1999 10:26:55 -0400
Ken wrote:

>p.s. I welcome any and all suggestions on optimizing my code.


Ken:

It's not so much optimizing your code as it is setting up a standard
sequence for doing stuff. Look at the difference between your code and what
I wrote below. Most of it is the same except for the sound logic. I like to
use flags for this type of logic.

For example, the FN playSound can be called by any part of your program and
you don't have to worry about it playing a sound when it isn't supposed to.
When the user indicates that he/she wants the program to use sounds, then
the flag (gSoundFlag) is set and all routines that use sound are written
without concern if the sound is going to be played or not. That decision is
made within the playSound FN and not within the routines that use sound.
Therein lies an important concept.

tedd


'---------------- Constants ----------------

_mAppleMenu = 255

_mFileMenu = 1
_mMapMenu = 1
_mSound = 2
_iQuit = 4

_testWindow = 1
_aboutWindow = 2
_mapWindow = 3


'---------------- Globals -------------------

DIM gSoundFlag
END GLOBALS

'---------------- Functions -----------------

LOCAL FN testWindow
  COORDINATE WINDOW

  TEXT _geneva, 12

  WINDOW _testWindow, "Menu Test Window", (0,0)-(280,100),_docNoGrow

  MENU 1,0, _enable, "File"                       'Menu items defined
  MENU 1,1, _enable, "Show map"
  MENU 1,2, _enable, "Sound"                      'Sound menu initially enabled
  MENU 1,3, _enable, "-"
  MENU 1,4, _enable, "/QQuit"

  APPLE MENU "About this program..."

  EDIT FIELD 1, "Text for Main window test
only.",(10,40)-(270,60),_statNoFramed,_centerJust

  EDIT FIELD #0

END FN

LOCAL FN aboutWindow
  COORDINATE WINDOW

  TEXT _geneva, 12

  WINDOW _aboutWindow, "About Menu Test Window", (5,40)-(280,130),_docNoGrow

  EDIT FIELD 2, "Text for About Window Menu
test.",(10,40)-(270,60),_statNoFramed, _centerJust
  EDIT FIELD #0

END FN

LOCAL FN mapWindow
  COORDINATE WINDOW

  TEXT _geneva, 12

  WINDOW _mapWindow, "Map Menu Test Window", (5,160)-(280,250),_docNoGrow

  EDIT FIELD 2, "Text for Map Window Menu
test.",(10,40)-(270,60),_statNoFramed, _centerJust
  EDIT FIELD #0

END FN


LOCAL FN playSound

  LONG IF gSoundFlag = _true
    BEEP : BEEP : BEEP
  XELSE
    BEEP
  END IF

END FN

LOCAL FN doDialog
  evnt = DIALOG(0)
  id   = DIALOG(evnt)

  SELECT evnt

    CASE _wndClick
      SELECT id
        CASE _testWindow
          WINDOW _testWindow
        CASE _mapWindow
          WINDOW _mapWindow
        CASE _aboutWindow
      END SELECT

    CASE _wndClose
      SELECT id
        CASE _testWindow
          WINDOW CLOSE _testWindow
        CASE _mapWindow
          WINDOW CLOSE _mapWindow
        CASE _aboutWindow
          WINDOW CLOSE _aboutWindow
      END SELECT

  END SELECT

END FN


LOCAL FN doMenu
  menuID = MENU(_menuID)
  itemID = MENU(_itemID)

  SELECT menuID
    CASE _mFileMenu
      SELECT itemID
        CASE _mMapMenu
          WINDOW CLOSE _mapWindow
          FN mapWindow
          FN playSound

        CASE _mSound

          LONG IF gSoundFlag = _true
            gSoundFlag = _false
          XELSE
            gSoundFlag = _true
          END IF

          MENU 1,2, gSoundFlag + 1,"Sound"
          FN playSound

        CASE _iQuit
          END
      END SELECT

    CASE _mAppleMenu
      WINDOW CLOSE _aboutWindow
      FN aboutWindow

  END SELECT

  MENU

END FN

'---------------- Event Loop -----------------
WINDOW OFF

ON DIALOG FN doDialog
ON MENU FN doMenu

FN testWindow

DO
  HANDLEEVENTS
UNTIL 0


___________________________________________________________________
<mailto:tedd@...>	               http://sperling.com/