// This example shows a text editor that runs in an
// XbpCrt window. The window has a menu bar that
// includes a submenu for processing text files. This
// menu includes the items "New", "Open", "Save" and
// "Save As". A file dialog is used for these menu
// options. The actual text editor is provided by an
// XbpMLE object whose :cargo instance variable contains
// the file name of the current file.
#include "Xbp.ch"
#include "Appevent.ch"
#pragma library("XppUi2")
PROCEDURE Main
LOCAL nEvent, mp1, mp2, oXbp
LOCAL oMLE, oFileDlg
SetColor("N/W")
CLS
// Create file dialog with desktop as parent
oFileDlg := XbpFileDialog():new()
oFileDlg:create( AppDesktop() )
// Create MLE
oMLE := XbpMLE():new():create( ,, {50,50}, {550,300} )
oMLE:cargo := ""
// Default menu for managing text files
TextFileMenu( SetAppWindow():menuBar(), oFileDlg, oMLE )
// Event loop
nEvent := 0
DO WHILE nEvent <> xbeP_Close
nEvent := AppEvent( @mp1, @mp2, @oXbp )
oXbp:HandleEvent( nEvent, mp1, mp2 )
ENDDO
RETURN
// *************************************************
// This function creates a default menu for opening
// and saving text files
//
FUNCTION TextFileMenu( oMenuBar, oFileDlg, oMLE )
LOCAL oMenu := XbpMenu():new(oMenuBar):create()
oMenu:title := "~File"
oMenu:addItem( { "~New", ;
{|| oMLE:setData(""), oMLE:cargo := "", ;
SetAppWindow():setTitle( "No name" ), ;
oMenu:disableItem(4) } } )
oMenu:addItem( { "~Open...", ;
{|| ReadFile( oFileDlg, oMLE ), ;
IIf( Empty(oMLE:cargo), NIL, oMenu:enableItem(4)) } } )
oMenu:addItem( { ,, XBPMENUBAR_MIS_SEPARATOR,0 } )
oMenu:addItem( { "~Save", ;
{|| WriteFile( oMLE:cargo, oMLE ) }, ;
0, XBPMENUBAR_MIA_DISABLED } )
oMenu:addItem( { "Save ~as...", ;
{|| WriteFile( oFileDlg:saveAs( oMLE:cargo ), oMLE ) } } )
oMenuBar:addItem( {oMenu, NIL} )
RETURN oMenu
// *******************************************
// The procedure activates the file dialog for
// selecting a file. Reads selected files and
// copies into MLE.
//
PROCEDURE ReadFile( oFileDlg, oMLE )
LOCAL cFile := oFileDlg:open( oMLE:cargo )
IF ! Empty( cFile )
oMLE:cargo := cFile
oMLE:setData( MemoRead( cFile ) )
SetAppWindow():setTitle( cFile )
ENDIF
RETURN
// **************************************
// Write text buffer of the MLE to file
//
PROCEDURE WriteFile( cFile, oMLE )
IF ! Empty( cFile )
oMLE:cargo := cFile
MemoWrit( cFile, oMLE:getData() )
SetAppWindow():setTitle( cFile )
ENDIF
RETURN