FUNCTION General_Menu, text, title, OPAQUE = opaque, $
	NO_CLEAR = no_clear

;+
; NAME:
;	General_Menu
; PURPOSE:
;	Menu wich decides what kind of display to use
;	("AnMenu" or "WMenu") for a menu. In the case, of
;	the vt-type terminals, if there is not enough place for all
;	the entries on the screen, they will be  displayed only when
;	explicitely wanted.
; CALLING SEQUENCE:
;	choice = General_Menu( texts, title )
; INPUT ARGUMENTS:
;	texts: an array of strings
;	title: a scalar string
; OUTPUT ARGUMENT:
;	choice: The number of the entry selected + 1 (they are 
;		ranged from 1 to the number of elements).
; KEYWORD:
;	OPAQUE: an array of the same dimension as texts,
;		containing boolean values. If a value is one,
;		the corresponding array entry will NOT be 
;		displayed.
;	/NO_CLEAR: If set, on VT terminals, the graphic display
;		is not erased and the menu is superimposed.
; SIDE EFFECT:
;	On X-Windows, a window is opened.
;	On VT Terminals, the screen is cleared
; MODIFICATION HISTORY:
;	Created in June 1991 by A.Csillaghy 
;		Institute of Astronomy, ETH Zurich
;	Extended in September 1991 for Megamenus, A.Cs.
;	Extended in Feb 95 for x megamenus ... A.Cs.
;       The Part with ANMenu is not supported any more.
;          (ANMENU is not available any more!)
;          June 98, csillag@ssl.berkeley.edu
;-

IF N_Elements(text) EQ 0 THEN RETURN, 0
IF Keyword_Set(NO_CLEAR) THEN clear = 0 ELSE clear = 1

textLen = N_Elements(text)
IF !d.name EQ 'X' THEN BEGIN
    menuMax = 500
    IF textLen GT menuMax THEN BEGIN
        level = 0
        REPEAT BEGIN
            first = level*menuMax
            last = ((level+1)*menuMax)<(textLen) -1
            choice = WMenu( [title,  $
                             text(first:last), 'back ...', $
                             'forward ...'], TITLE = 0 ) 
            IF choice EQ last-first+2 THEN $
              level = (level-1)>0 $
            ELSE IF choice EQ last-first+3 THEN $
              level = (level+1)<(textLen/menuMax)
        END UNTIL choice LE (last-first+1)
        choice = first + choice 
    ENDIF ELSE BEGIN
        yPos = GetEnv( 'ENRAD_MENU_YPOS' ) ;obsolete keyword...
        IF yPos EQ '' THEN BEGIN
            yPos = GetEnv( 'RAGVIEW_MENU_YPOS' )
            IF yPos EQ '' THEN BEGIN
                yPos = 500 
            ENDIF ELSE yPos = Fix( yPos )
        ENDIF ELSE yPos = Fix( yPos )
        choice = WMenu( [title, text], TITLE = 0, $
                        XPOS = 100, YPOS = yPos ) 
    ENDELSE
    
    
;    choice = X_TVMenu(  text, title, 100, 500 ) +1 $
ENDIF ELSE BEGIN
    Print, 'Sorry, the menu interface is not supported for  your device'
    IF 0 THEN BEGIN    
        IF NOT Keyword_Set(OPAQUE) THEN opaque = IntArr(textLen)
        oldTextLength = N_Elements(text)
        offset = Where( opaque EQ 0 )
        newTextLength = N_Elements(opaque( offset ))
        newText = StrArr( newTextLength )
        j = 0 
        FOR i = 0, oldTextLength-1 DO $
          IF opaque(i) NE 1 THEN BEGIN
            newText(j) = text(i)
            j = j+1
        ENDIF
        IF newTextLength LT 19 THEN BEGIN
            choice = AnMenu(newText, newTextLength, title, 1,1,clear,1,0 )
        ENDIF ELSE BEGIN
            entriesNr = 18
            first = 0 & last = entriesNr-1 & i = 0 & nTexts = entriesNr+1
            nbPartMenu = newTextLength / entriesNr + 1
            REPEAT BEGIN
                nTexts = last-first+2
                choice = AnMenu( [newText(first:last), 'Continuing ... '], $
                                 nTexts, title, 1, 1, clear, 1, 0 )
                IF choice EQ nTexts-1 THEN BEGIN
                    i = (i+1) MOD nbPartMenu
                    first = entriesNr*i 
                    last = (newTextLength-1) * ( i EQ nbPartMenu-1) +  $
                      (entriesNr*(i+1)-1) * ( i NE nbPartMenu-1)
                ENDIF
            END UNTIL choice NE nTexts-1
            choice = choice + entriesNr*i
        ENDELSE
        choice = offset( choice ) + 1      
        Clear_Screen
    ENDIF
ENDELSE

RETURN, choice

END

