home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BUG 4
/
BUGCD1997_05.BIN
/
aplic
/
clip4win
/
clip4win.exe
/
C4W30E.HUF
/
SOURCE
/
EDIT.PRG
< prev
next >
Wrap
Text File
|
1994-04-22
|
13KB
|
557 lines
////////////////////////////
//
// Clip-4-Win edit control demo
//
// Copyright (C) 1992,1993 Skelton Software, Kendal Cottage, Hillam, Leeds, UK.
// All Rights Reserved.
//
// Compile: edit /n /w
// Link: /se:600 edit,,,clip4win,edit.def
//
// Be sure to specify "heapsize n" (e.g. n = 2048) in the .def file,
// so there's some memory for the control to use.
//
// In a dialog, you can get a handle like this:
// hEdit = GetDlgItem(hWndDlg, ID_EDIT)
//
// (Actually, you can do that in an ordinary window, too. Maybe
// the function should really be called GetWndItem().)
//
////////////////////////////
#define WIN_WANT_EN // optional Edit Notification messages
#define WIN_WANT_CLIPBOARD
#define WIN_WANT_ALL
#include "windows.ch"
#define MAKELPARAM(nLow, nHigh) ((nLow) + (nHigh) * 65536)
#define CR chr(13)
#define CRLF chr(13) + chr(10)
#ifndef LIB_ONLY
#define ID_EDIT 1 // unique id (used for the edit control)
static hWnd, hInst, cText, hEdit, cAppName := "Clip-4-Win"
function main()
local hMenu, nEvent
hWnd = WinSetup(cAppName, "Clip-4-Win Edit Control Demo")
hInst = _GetInstance()
hMenu = MenuSetup()
HideCaret(hWnd)
do while .t.
do while (nEvent := ChkEvent()) == EVENT_NONE
// some "background" processing could go here
enddo
HandleEvent(nEvent) // give the event to the right handler
enddo
return 0
static procedure DoAbout()
MessageBox( , "Written by John Skelton.", "Info", MB_OK)
return
static procedure DoExit()
quit
return
// Single-line edit control
static procedure DoSEdit()
local cEdit := "Initial Text"
if hEdit != nil
? "Edit text (line 0) was:", EditGetLine(hEdit, 0)
DestroyWindow(hEdit) // remove previous edit control
endif
hEdit = CreateWindow("edit", ; // window class
cEdit, ; // initial buffer
WS_CHILD ; // child window
+ WS_VISIBLE ; // ... that can be seen
+ WS_BORDER ; // ... with a border
+ ES_LEFT ; // ... left justified
+ ES_AUTOHSCROLL, ; // ... auto horiz. scroll
50, 50, ; // x,y position
250, 30, ; // width, height
hWnd, ; // parent window
ID_EDIT, ; // id for child control to use
hInst) // our own app instance
// In a dialog, get the handle like this:
// hEdit = GetDlgItem(hWndDlg, ID_EDIT)
EditLimitText(hEdit, 15) // just for testing
SetFocus(hEdit)
AddHandler(hWnd, {|nEvent| EditEvent(nEvent)})
return
// Multi-line edit control
static procedure DoMEdit()
local cEdit := "Some strings" + CRLF + "for an Edit Control" + CRLF ;
+ "as an example" + CRLF + "for you"
if hEdit != nil
? "Edit text (line 0) was:", EditGetLine(hEdit, 0)
DestroyWindow(hEdit) // remove previous edit control
endif
hEdit = CreateWindow("edit", ; // window class
cEdit, ; // initial buffer
WS_CHILD ; // child window
+ WS_VISIBLE ; // ... that can be seen
+ WS_HSCROLL ; // ... horiz. scroll bar
+ WS_VSCROLL ; // ... vert. scroll bar
+ WS_BORDER ; // ... with a border
+ ES_LEFT ; // ... left justified
+ ES_MULTILINE ; // ... allow several lines
+ ES_AUTOHSCROLL ; // ... auto horiz. scroll
+ ES_AUTOVSCROLL, ; // ... auto vert. scroll
50, 50, ; // x,y position
250, 200, ; // width, height
hWnd, ; // parent window
ID_EDIT, ; // id for child control to use
hInst) // our own app instance
SetFocus(hEdit)
AddHandler(hWnd, {|nEvent| EditEvent(nEvent)})
return
static procedure EditEvent(nEvent)
local nEN, nLen
do case
case nEvent == EVENT_CONTROL
if _LastwParam() == ID_EDIT // child id
// we've been sent a message by the edit control
nEN = _LastHilParam() // get the reason for the msg
if nEN == EN_MAXTEXT .or. nEN == EN_ERRSPACE
// char(s) did not fit in the space available
nLen = SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0)
MessageBox( , "No more room" + CR ;
+ "Length = " + ltrim(str(nLen)), ;
"Edit control", MB_OK)
elseif nEN == EN_CHANGE
// contents have (probably) been changed
//elseif ...
endif
endif
case nEvent == EVENT_SETFOCUS
SetFocus(hEdit) // let edit control keep input focus
endcase
return
static function DoCut()
local aSel
if hEdit != nil
aSel = EditGetSel(hEdit)
if aSel[1] == aSel[2]
MessageBox( , "Nothing to Cut! (Try selecting some text.)", ;
"Warning")
endif
EditCut(hEdit)
endif
return nil
static function DoCopy()
local aSel
if hEdit != nil
aSel = EditGetSel(hEdit)
if aSel[1] == aSel[2]
MessageBox( , "Nothing to Copy! (Try selecting some text.)", ;
"Warning")
endif
EditCopy(hEdit)
endif
return nil
static function DoPaste()
if hEdit != nil
EditPaste(hEdit)
endif
return nil
#endif // !LIB_ONLY
/////////
//
// EditCanUndo( <hEdit> ) --> lCanUndo
//
// Returns .t. if the last edit control operation can be undone
//
/////////
function EditCanUndo(hEdit)
return SendMessage(hEdit, EM_CANUNDO, 0, 0) != 0
/////////
//
// EditCopy( <hEdit> ) --> nil
//
// Copy the edit control contents to the Windows clipboard.
//
// NOTE: Uses EM_SETSEL internally.
//
/////////
procedure EditCopy(hEdit)
SendMessage(hEdit, WM_COPY, 0, 0)
return
/////////
//
// EditCut( <hEdit> ) --> nil
//
// Cut the edit control contents to the Windows clipboard.
//
// NOTE: You can use the EM_UNDO message to restore the contents
// (still leaving a copy in the clipboard).
//
/////////
procedure EditCut(hEdit)
SendMessage(hEdit, WM_CUT, 0, 0)
return
/////////
//
// EditEmptyUndoBuffer( <hEdit> ) --> nil
//
// Clears the edit control undo buffer
//
/////////
procedure EditEmptyUndoBuffer(hEdit)
SendMessage(hEdit, EM_EMPTYUNDOBUFFER, 0, 0)
return
/////////
//
// EditGetFirstVisibleLine( <hEdit> ) --> nLine
//
// Returns the number of the top line that's visible
//
/////////
function EditGetFirstVisibleLine(hEdit)
return SendMessage(hEdit, EM_GETFIRSTVISIBLELINE, 0, 0)
/////////
//
// EditGetLine( <hEdit>, <nLine> ) --> cLine
//
// Returns the specified line from within an edit control
// (<nLine> starts at 0, and is ignored for a single-line edit control)
//
/////////
function EditGetLine(hEdit, nLine)
local nLen, cBuf := space(200) // seems generous
cBuf = c4w_i2bin(len(cBuf)) + cBuf
nLen = SendMessage(hEdit, EM_GETLINE, nLine, @cBuf)
return left(cBuf, nLen)
/////////
//
// EditGLineCount( <hEdit> ) --> nCount
//
// Returns the number of lines within a multi-line edit control
//
/////////
function EditGLineCount(hEdit)
return SendMessage(hEdit, EM_GETLINECOUNT, 0, 0)
/////////
//
// EditGetModify( <hEdit> ) --> lModify
//
// Returns .t. if the contents of the edit control have been changed
//
/////////
function EditGetModify(hEdit)
return SendMessage(hEdit, EM_GETMODIFY, 0, 0) != 0
/////////
//
// EditGetPasswordChar( <hEdit> ) --> nChar
//
// Returns the character displayed when the user enters text in an
// edit control (returns 0 if no password char exists)
//
/////////
function EditGetPasswordChar(hEdit)
return SendMessage(hEdit, EM_GETPASSWORDCHAR, 0, 0)
/////////
//
// EditGetSel( <hEdit> ) --> aSel
//
// Returns the start and end of the selected part of an edit control
//
// aSel is in the form { start, end }
//
/////////
function EditGetSel(hEdit)
return bin2a(c4w_l2bin(SendMessage(hEdit, EM_GETSEL, 0, 0)), "int[2]")
/////////
//
// EditGetText( <hEdit> ) --> cText
//
// Get all the text in an edit control.
//
/////////
function EditGetText(hEdit)
local nLen, cBuf
// get the length, and include room for the trailing null that Windows
// wants to add (even though we don't want it!)
nLen = SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0) + 1
cBuf = space(nLen)
nLen = SendMessage(hEdit, WM_GETTEXT, nLen, @cBuf) // note: not an EM_ msg
return left(cBuf, nLen)
/////////
//
// EditLimitText( <hEdit>, <nMax> ) --> nil
//
// Sets the maximum number of characters allowed in the edit control
// (in the range 0-nnnn, where nnnn is limited by the heapsize
// specified in the .def file for the linker)
//
/////////
procedure EditLimitText(hEdit, nMax)
SendMessage(hEdit, EM_LIMITTEXT, nMax, 0)
return
/////////
//
// EditLineFromChar( <hEdit>, <nPos> ) --> nLine
//
// Returns the line number indicated by the character position
//
// <nPos> counts from zero.
//
/////////
function EditLineFromChar(hEdit, nPos)
return SendMessage(hEdit, EM_LINEFROMCHAR, nPos, 0)
/////////
//
// EditLineIndex( <hEdit>, <nLine> ) --> nPos
//
// Returns the character position of a line within a multi-line
// edit control.
//
// <nLine> counts from zero. Use -1 for the current line.
//
/////////
function EditLineIndex(hEdit, nLine)
return SendMessage(hEdit, EM_LINEINDEX, nLine, 0)
/////////
//
// EditLineLength( <hEdit>, <nPos> ) --> nLen
//
// Returns the length of the line indicated by the character position
//
// <nPos> counts from zero.
//
/////////
function EditLineLength(hEdit, nPos)
return SendMessage(hEdit, EM_LINELENGTH, nPos, 0)
/////////
//
// EditPaste( <hEdit> ) --> nil
//
// Paste the contents of the Windows clipboard into an edit control.
//
/////////
procedure EditPaste(hEdit)
SendMessage(hEdit, WM_PASTE, 0, 0)
return
/////////
//
// EditReplaceSel( <hEdit>, <cNewStr> ) --> nil
//
// Replace the currently selected text in an edit control
// (the new string may be a different length, of course).
//
// To replace all the text, use EditSetText(), below.
//
/////////
procedure EditReplaceSel(hEdit, cNewStr)
SendMessage(hEdit, EM_REPLACESEL, 0, cNewStr)
return
/////////
//
// EditSetModify( <hEdit>, <lModify> ) --> nil
//
// Sets/clears the flag that indicates whether the contents of the
// edit control have been changed
//
/////////
procedure EditSetModify(hEdit, lModify)
SendMessage(hEdit, EM_SETMODIFY, iif(lModify, 1, 0), 0)
return
/////////
//
// EditSetPasswordChar( <hEdit>, <nNewChar> ) --> nil
//
// Sets the character displayed when the user enters text in a
// single-line edit control
//
/////////
procedure EditSetPasswordChar(hEdit, nChar)
SendMessage(hEdit, EM_SETPASSWORDCHAR, nChar, 0)
return
/////////
//
// EditSetReadOnly( <hEdit>, <lReadOnly> ) --> lSuccess
//
// Makes the contents of the edit control read-write or read-only
//
/////////
function EditSetReadOnly(hEdit, lReadOnly)
return SendMessage(hEdit, EM_SETREADONLY, iif(lReadOnly, 1, 0), 0) != 0
/////////
//
// EditSetSel( <hEdit>, <nStart>, <nEnd>, <lMakeCaretVisible> ) --> nil
//
// Sets the start and end of the selected part of an edit control.
// The caret is placed at the end, and optionally brought into
// sight (by scrolling as needed).
//
// Special values:
//
// nStart = 0, and nEnd = -1 all the text is selected
// nStart = -1 nothing selected
//
/////////
procedure EditSetSel(hEdit, nStart, nEnd, lMakeCaretVisible)
if lMakeCaretVisible == nil
lMakeCaretVisible = .t.
endif
SendMessage(hEdit, EM_SETSEL, ;
iif(lMakeCaretVisible, 0, 1), ; // note the order
MAKELPARAM(nStart, nEnd))
return
/////////
//
// EditSetText( <hEdit>, <cNewStr> ) --> nil
//
// Change all the text in an edit control.
//
// To replace part of the text, use EditReplaceSel(), above.
//
/////////
procedure EditSetText(hEdit, cNewStr)
SendMessage(hEdit, WM_SETTEXT, 0, cNewStr) // note: not an EM_ msg
return
/////////
//
// EditUndo( <hEdit> ) --> lUndone
//
// Attempts to undo the last edit control operation, and
// returns .t. if it was undone ok
//
/////////
function EditUndo(hEdit)
return SendMessage(hEdit, EM_UNDO, 0, 0) != 0
#ifndef LIB_ONLY
function nstr(n)
return alltrim(str(n)) + " "
static function MenuSetup()
local hWnd := SelectWindow(), hMenu, hPopupMenu
if (hMenu := GetMenu(hWnd)) != nil
DestroyMenu(hMenu)
endif
// do new one (forget old value)
hMenu = CreateMenu()
hPopupMenu = CreatePopupMenu()
AppendMenu(hMenu, "file", MF_ENABLED + MF_POPUP, "&File", hPopupMenu)
AppendMenu(hPopupMenu, "exit", MF_ENABLED + MF_STRING, "E&xit", {|| Alert("Thanks for running this demo"), DoExit()})
hPopupMenu = CreatePopupMenu()
AppendMenu(hMenu, "edit", MF_ENABLED + MF_POPUP, "&Edit", hPopupMenu)
AppendMenu(hPopupMenu, "cut", MF_ENABLED + MF_STRING, "Cu&t", {|c| DoCut()})
AppendMenu(hPopupMenu, "copy", MF_ENABLED + MF_STRING, "&Copy", {|c| DoCopy()})
AppendMenu(hPopupMenu, "paste", MF_ENABLED + MF_STRING, "&Paste", {|| DoPaste()})
hPopupMenu = CreatePopupMenu()
AppendMenu(hMenu, "demo", MF_ENABLED + MF_POPUP, "&Demo", hPopupMenu)
AppendMenu(hPopupMenu, "edit1", MF_ENABLED + MF_STRING, "&Single-line", {|c| DoSEdit()})
AppendMenu(hPopupMenu, "medit", MF_ENABLED + MF_STRING, "&Multi-line", {|c| DoMEdit()})
hPopupMenu = CreatePopupMenu()
AppendMenu(hMenu, "help", MF_ENABLED + MF_POPUP, "&Help", hPopupMenu)
AppendMenu(hPopupMenu, "about", MF_ENABLED + MF_STRING, "&About", {|| DoAbout()})
SetMenu(hWnd, hMenu)
return hMenu
#endif // !LIB_ONLY