Controlling Insertion Point and Selected Text in a Text Object
Description
This example shows how you can control the insertion point and selected text in a text control on an Xdialog box. Normally, when you place a text control on an Xdialog box, you bind the control to a character variable. For example:
'text = "some text" 'ui_dlg_box("title","[.20text]")
However, if you want to be able to control the Insertion Point and Selected text, then you must bind the control to a 'pointer' variable that has a .text and a .object property, as shown in the example below. The .text property contains the text in the variable. The .object property gives you access to the properties and methods of this text object.
dim ptext as P dim ptext.text as C dim ptext.object as P ptext.text = "This is an example showing how Xdialog can control the insertion point and selected text in an character variable on an Xdialog dialog box." + crlf(2)+\ "This is some more text following a carriage return." dim dlg_title as C dlg_title = "Controlling Insertion Point and Selection in a Text Object on an Xdialog Box" dim flagCutCopy as L flagCutCopy = .f.
The {watch} command sets up a watch event. the result of the expression ptext.object.get_select()is monitored. Every time the value in this expression changes, the 'change' event fires. the 'change' event sets the flag flagCutCopy to .T. if there is some text selected, and to .F. if there is no text selected.
ui_dlg_box(dlg_title,<<%dlg% {watch=ptext.object.get_select()!change} {startup=init} {font=courier new,10}
M flag is for multi-line text box
W flag is for word wrap
A flag is to allow the text control to accept Tabs (i.e. do not tab out of the control when the user types a tab)
K flag means keep selection visible even when the control does not have focus.
[%MAW;K%.150,10ptext]; {font=} {sp=5}
if flag_ok = .t. then ip = ptext.object.get_cursor() new_ip = ip + len(string) ptext.object.insert_text(string)
After clicking the 'insert text' button focus is on the button. we want to return focus to the edit control.
ui_dlg_ctl_goto(dlg_title,"ptext") ptext.object.set_cursor(new_ip) 'this causes the text box to scroll if the insertion point is not currently visible ptext.object.show_caret() end if end if if a_dlg_button = "show" then a_dlg_button = "" current_selection = ptext.object.get_select() ip = ptext.object.get_cursor() ui_msg_box("Current Selection","Selected text: " + current_selection + crlf(2) + "Insertion point: " + ip +crlf()+"Length of selection: " + len(current_selection) ) end if if a_dlg_button = "cut" then ip = ptext.object.get_cursor() ptext.object.cut()
After clicking the 'cut' button focus is on the button. we want to return focus to the edit control.
ui_dlg_ctl_goto(dlg_title,"ptext") 'set the insertion point. make sure that the insertion point is not past the end of the text if ip <=len(ptext.text) then ptext.object.set_cursor(ip) else ptext.object.set_cursor( len(ptext.text) ) end if
This causes the text box to scroll if the insertion point is not currently visible.
ptext.object.show_caret() a_dlg_button = "" end if if a_dlg_button = "copy" then a_dlg_button = "" 'get the current insertion point and selected text ip = ptext.object.get_cursor() selected_text = ptext.object.get_select() ptext.object.copy()
After clicking the 'copy' button focus is on the button. we want to return focus to the edit control.
ui_dlg_ctl_goto(dlg_title,"ptext") 'restore the current selected text and insertion point ptext.object.select(ip, len(selected_text) ) 'this causes the text box to scroll if the insertion point is not currently visible ptext.object.show_caret() end if if a_dlg_button = "paste" then a_dlg_button = "" 'get the current insertion point ip = ptext.object.get_cursor()
Compute the new insertion point, which is the current insertion point plus the length of the inserted text.
new_ip = ip + len( ClipBoard.Get_Data() ) ptext.object.paste() 'after clicking the 'paste' button focus is on the button. we want to return focus to the edit control ui_dlg_ctl_goto(dlg_title,"ptext") ptext.object.set_cursor(new_ip) 'this causes the text box to scroll if the insertion point is not currently visible ptext.object.show_caret() end if if a_dlg_button = "delete" then a_dlg_button = "" ip = ptext.object.get_cursor() ptext.object.delete() 'after clicking the 'cut' button focus is on the button. we want to return focus to the edit control ui_dlg_ctl_goto(dlg_title,"ptext")
Set the insertion point. Make sure that the insertion point is not past the end of the text.
if ip <=len(ptext.text) then ptext.object.set_cursor(ip) else ptext.object.set_cursor( len(ptext.text) ) end if a_dlg_button = "" 'this causes the text box to scroll if the insertion point is not currently visible ptext.object.show_caret() end if if a_dlg_button = "select_text" then a_dlg_button = "" start_pos = 1 len = 10 flag_ok = .f. ui_dlg_box("Select Text","Start Position |[.5start_pos];Length |[.5len];{lf};| ",<<%txt% if a_dlg_button = "ok" then flag_ok = .t. end if %txt%) if flag_ok = .t. then ptext.object.select(start_pos,len) end if end if if a_dlg_button = "highlight" then a_dlg_button = "" string = "carriage" flag_ok = .f. ui_dlg_box("Highlight String","String |[.15string];{lf};| ",<<%txt% if a_dlg_button = "ok" then flag_ok = .t. end if %txt%)
Convert the text in the control from CR-LF ( crlf()) delimited to LF ( chr(10) ) delimited. This is because this is how Alpha Anywhere stores the text internally, and so the position of the highlighted text needs to be computed based on an LF delimited string.
textlf = stritran( ptext.text,crlf(),chr(10) ) start_pos = at(string,textlf) if start_pos = 0 then ui_msg_box("Error","Not found.") else ptext.object.select(start_pos, len(string) ) end if 'this causes the text box to scroll if the insertion point is not currently visible ptext.object.show_caret() end if if a_dlg_button = "set_ip" then a_dlg_button = "" pos = 1 flag_ok = .f. ui_dlg_box("Set Insertion Point","Insert point position |[.5pos];{lf};| ",<<%txt% if a_dlg_button = "ok" then flag_ok = .t. end if %txt%) if flag_ok = .t. then ui_dlg_ctl_goto(dlg_title,"ptext") ptext.object.set_cursor(pos) ptext.object.show_caret() end if end if if a_dlg_button = "init" then a_dlg_button = "" 'give focus to the text control and set the insertion point to the beginning of the text ui_dlg_ctl_goto(dlg_title,"ptext") ptext.object.set_cursor(1)
ptext.object.set_cursor( len(ptext.text) ) 'this causes the text box to scroll if the insertion point is not currently visible ptext.object.show_caret()
end if %code%)
Limitations
Desktop applications only
See Also