PDA

View Full Version : save text file...



Lionheart008
13-05-2009, 19:13
dear thinbasic users :)

theme UI/Textfile: how can I save a Text file ??? I am confused... :? have found this text input example and wanted to add a 'save button' %ID_Save... thought I have done all right.. but here's something missing.. perhaps anybody can give a little advice... thanks in advance... and you may beat me... when I am so clumsy ;)


USES "UI"
uses "console"

DIM Msg AS LONG '---Returned messages
DIM wParam AS LONG '---wParam in message pump
DIM lParam AS LONG '---lParam in message pump
DIM hDlg AS LONG '---Dialog handle
DIM sFile AS string
DIM sInput AS string '---Used to get input from field
DIM sBuffer AS string '---Used to set output buffer

dim wx as long '---Used to get client width
dim hy as long '---Used to get client height

DIM Counter AS LONG '---Counter to count output lines

'---Control IDs
%ID_TXTBUFFER = 10 '---Output field
%ID_LBLENTER = 20 '---Input field label
%ID_TXTENTER = 30 '---Input field
%ID_SAVE = 40
%ID_OPEN = 50
%ID_SAVE = 60
%ID_CANCEL = 70

'---Set starting width and height of the window
wx = 220
hy = 250

'---Create main dialog
DIALOG NEW 0, "Textsample using Enter key from input field", -1, -1, wx, hy, _
%WS_DLGFRAME OR _
%DS_CENTER OR _
%WS_CAPTION OR _
%WS_SYSMENU OR _
%WS_OVERLAPPEDWINDOW , _
0 TO hDlg

'---Show dialog
DIALOG SHOW MODELESS hDlg

'---Now start window message pump loop
WHILE IsWindow(hDlg)

'---Get the message and fill wParam and lParam
Msg = GetMessage(hDlg, wParam, lParam)

if Msg then printl msg, wparam, lparam

'---Test the returned message
SELECT CASE Msg

'---Message fired at the very beginning when dialog is initialized
'---We use it to add necessary controls
case %WM_INITDIALOG

CONTROL ADD TEXTBOX , hDlg, %ID_TXTBUFFER, "" , 2, 2, wx - 10, hy - 60, _
%WS_TABSTOP OR _
%ES_WANTRETURN OR _
%ES_MULTILINE OR _
%ES_AUTOHSCROLL OR _
%ES_AUTOVSCROLL OR _
%WS_VSCROLL OR _
%WS_HSCROLL

CONTROL ADD label , hDlg, %ID_LBLENTER, "Type some text and press <ENTER> key", 2, hy - 30, wx - 5, 12
CONTROL ADD TEXTBOX , hDlg, %ID_TXTENTER, "" , 2, hy - 20, wx - 5, 12, %WS_TABSTOP

CONTROL ADD BUTTON , hDlg, %ID_SAVE, "Save your text as ...", 55, 200, 80, 18, %WS_BORDER OR %WS_TABSTOP, %WS_EX_CLIENTEDGE

'---Configure the controls to automatically resize as needed
CONTROL SET RESIZE hDlg, %ID_TXTBUFFER, 1, 1, 1, 1 '---This control must resize every side
CONTROL SET RESIZE hDlg, %ID_LBLENTER , 1, 1, 0, 1 '---This control must have top size movable
CONTROL SET RESIZE hDlg, %ID_TXTENTER , 1, 1, 0, 1 '---This control must have top size movable
CONTROL SET RESIZE hDlg, %ID_SAVE , 1, 1, 1, 1 '---This control must have top size movable
'---In case of keyboard pressed ...
CASE %wm_keydown

'---Check which key using wParam
SELECT CASE wParam
'---In case of <ENTER> key ...
case 13
DoTheJob(%ID_TXTENTER, %ID_TXTBUFFER, "")
'end select
case 14
DoTheJob(%ID_TXTENTER, %ID_TXTBUFFER, %ID_SAVE "")
end select

'---Get the message and fill wParam and lParam
Msg = GetMessage(hDlg, wParam, lParam)

'---Now test the message
SELECT CASE Msg

CASE %WM_COMMAND
SELECT CASE wParam
CASE %ID_OPEN

sFile = OpenFile
if sFile <> "" then
CONTROL SET TEXT hDlg, 50, FILE_LOAD(sFile)
msgbox 0, "File " & sFile & " loaded into textbox"
else
msgbox 0, "No file selected"
end if

CASE %ID_SAVE
sFile = SaveFile
sFile = trim$(sFile)
if sFile <> "" then
CONTROL SET TEXT hDlg, 60, FILE_SAVE(sFile)
msgbox 0, "Save file name " & sFile & "." & $crlf & "Nothing done. Just an example."
else
msgbox 0, "No file selected, no save performed."
end if
END SELECT


CASE %WM_SYSCOMMAND
SELECT CASE wParam
CASE %SC_CLOSE
EXIT WHILE
END SELECT

'---Here we go in any other case
'---This is used like an idle time, when Msg = 0

case %WM_NOTIFY
printl "notify"

' case %WM_IDLE
'
' '---%WM_IDLE is in reality equal to zero
' '---It is like testing a CASE ELSE and in general can be used
' '---when you need to do something in idle time
'
' '---So, if idle, set back the focus to data entry field
' CONTROL set focus hDlg, %ID_TXTENTER
' doevents
'
END SELECT
end select
WEND

'---Close the dialog
DIALOG END hDlg


function DoTheJob(byval ID_In as long, byval ID_Out as long, AdditionalText as string) as long

control get text hDlg, ID_In to sInput
if trim$(sInput) = "" then
sInput = "<<<- Please type some text ->>>"
end if
Counter += 1
sInput = AdditionalText & format$(Counter, "0000") & " " & sInput

'control get text hDlg, ID_Out to sBuffer
control appendtotop text hDlg, ID_Out, sInput & $crlf '& sBuffer
control set text hDlg, ID_In, ""
CONTROL set focus hDlg, ID_In

end function

function OpenFile() as string
dim sFile as string
dim sFilter as string

sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"

sFile = Dialog_OpenFile(hDlg, _
"Open an file", _
DIR_GetCurrent, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY or %OFN_ENABLESIZING)
function = sFile
end function


function SaveFile() as string
dim sFile as string
dim sFilter as string

sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"

sFile = Dialog_SaveFile(hDlg, _
"Open an file", _
DIR_GetCurrent, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY or %OFN_ENABLESIZING)
function = sFile
end function



ciao, Lionheart

Petr Schreiber
13-05-2009, 19:32
Hi Frank,

how could you :D.

I would really recommend using CALLBACKs instead of ol' GetMessage, just check out how simple the code becomes:


' Basic Template for custom dialog
' Start Date 05-13-2009
' Created by by Petr Schreiber

USES "UI", "FILE"

' -- ID numbers of controls
Begin Const
%bClose = %WM_USER + 500
%tbText
%bLoad
%bSave

End Const

' -- Create dialog here
FUNCTION TBMAIN()
LOCAL hDlg AS DWORD

DIALOG New 0, "Load and save",-1,-1, 160, 145, _
%WS_POPUP Or %WS_VISIBLE Or _
%WS_CLIPCHILDREN Or %WS_CAPTION OR _
%WS_SYSMENU Or %WS_MINIMIZEBOX, 0 To hDlg

' -- Place controls here
CONTROL ADD textbox, hDlg, %tbText, "Text ... yes!", 5, 5, 150, 100, %ES_AUTOHSCROLL OR %ES_LEFT OR %WS_BORDER OR %WS_TABSTOP or %ES_MULTILINE or %ES_WANTRETURN or %WS_VSCROLL, %WS_EX_CLIENTEDGE OR %WS_EX_LEFT

CONTROL ADD BUTTON, hDlg, %bLoad, "Load", 5, 110, 60, 14
CONTROL ADD BUTTON, hDlg, %bSave, "Save", 95, 110, 60, 14
CONTROL ADD BUTTON, hDlg, %bClose, "Click to close", 95, 130, 60, 14

DIALOG SHOW MODAL hDlg, CALL dlgProc

END FUNCTION

' -- Callback for dialog
CALLBACK FUNCTION dlgProc()
local sFile as string

' -- Test for messages
SELECT CASE CBMSG

CASE %WM_INITDIALOG
' -- Put code to be executed after dialog creation here

CASE %WM_COMMAND
' -- You can handle controls here

SELECT CASE CBCTL

CASE %bLoad
IF CBCTLMSG = %BN_CLICKED THEN
sFile = OpenFile(cbhndl)
if len(sFile) then
control set text cbhndl, %tbText, file_load(sFile)
end if
END IF

CASE %bSave
IF CBCTLMSG = %BN_CLICKED THEN
sFile = SaveFile(cbhndl)
if len(sFile) then
file_save(sFile, Control_GetText(cbhndl, %tbText))
end if
END IF

CASE %bClose
IF CBCTLMSG = %BN_CLICKED THEN
DIALOG END CBHNDL
END IF

END SELECT


CASE %WM_CLOSE
' -- Put code to be executed before dialog end here

END SELECT

END FUNCTION

function OpenFile(hDlg as dword) as string
dim sFile as string
dim sFilter as string

sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"

sFile = Dialog_OpenFile(hDlg, _
"Open an file", _
APP_SOURCEPATH, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY or %OFN_ENABLESIZING)
function = sFile
end function


function SaveFile(hDlg as dword) as string
dim sFile as string
dim sFilter as string

sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"

sFile = Dialog_SaveFile(hDlg, _
"Open an file", _
APP_SOURCEPATH, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY or %OFN_ENABLESIZING)
function = sFile
end function



Petr

Lionheart008
13-05-2009, 21:47
hi petr ! :)

thank you... of course with callbacks... (must laugh!), I was so silly to use it with the old message stuff, but I have only (too) short checked the ui / text examples and so I haven't seen this way...

but one more wish... to get the text into the text field from the below, second textbox, how can I use the 'dothejob' call back to store the text in the first textbox ??? the user should do enter some words with the enter key and see it...

it's possible to do such things? :) callbacks are often my friends... but not always ! ;)



CASE %wm_keydown
IF CBCTLMSG = %ID_TXTENTER, "" THEN
sInput = dothejob(CBHNDL)
END IF
end select

show here my last version...


' Basic Template for custom dialog
' Start Date 05-13-2009
' Created by by Petr Schreiber

USES "UI", "FILE" ', "CONSOLE"

DIM hDlg AS LONG '---Dialog handle
DIM sFile AS string
DIM sInput AS string '---Used to get input from field

DIM Counter AS LONG

' -- ID numbers of controls
Begin Const
%ID_CLOSE = %WM_USER + 500
%ID_Text
%ID_Load
%ID_SAVE
%ID_TXTENTER
%ID_LBLENTER
End Const

' -- Create dialog here
FUNCTION TBMAIN()
LOCAL hDlg AS DWORD

DIALOG New 0, "Textbox Enter Key 1000",-1,-1, 170, 255, _
%WS_POPUP Or %WS_VISIBLE Or _
%WS_CLIPCHILDREN Or %WS_CAPTION OR _
%WS_SYSMENU Or %WS_MINIMIZEBOX, 0 To hDlg

' -- Place controls here
CONTROL ADD textbox, hDlg, %ID_Text, "Text ... yes!", 5, 5, 150, 150, %ES_AUTOHSCROLL OR %ES_LEFT OR %WS_BORDER OR %WS_TABSTOP or %ES_MULTILINE or %ES_WANTRETURN or %WS_VSCROLL, %WS_EX_CLIENTEDGE OR %WS_EX_LEFT

CONTROL ADD BUTTON, hDlg, %ID_Load, "Load", 5, 180, 60, 14
CONTROL ADD BUTTON, hDlg, %ID_Save, "Save", 95, 198, 60, 14
CONTROL ADD BUTTON, hDlg, %ID_Close, "Click to close", 95, 180, 60, 14
CONTROL ADD TEXTBOX , hDlg, %ID_TXTENTER, "type in" , 10, 235, 125, 16, %WS_TABSTOP, call dothejob '
CONTROL ADD label , hDlg, %ID_LBLENTER, "Type some text and press <ENTER> key", 12, 220,140, 12

DIALOG SHOW MODAL hDlg, CALL dlgProc

END FUNCTION

' -- Callback for dialog
CALLBACK FUNCTION dlgProc()
local sFile as string

SELECT CASE CBMSG

CASE %WM_INITDIALOG
' -- Put code to be executed after dialog creation here

CASE %WM_COMMAND
' -- You can handle controls here

SELECT CASE CBCTL

CASE %ID_Load
IF CBCTLMSG = %BN_CLICKED THEN
sFile = OpenFile(cbhndl)
if len(sFile) then
control set text cbhndl, %ID_Text, file_load(sFile)
end if
END IF

CASE %ID_Save
IF CBCTLMSG = %BN_CLICKED THEN
sFile = SaveFile(cbhndl)
if len(sFile) then
file_save(sFile, Control_GetText(cbhndl, %ID_Text))
end if
END IF

CASE %ID_Close
IF CBCTLMSG = %BN_CLICKED THEN
DIALOG END CBHNDL
END IF

'END SELECT

CASE %wm_keydown
IF CBCTLMSG = %ID_TXTENTER, "" THEN
sInput = dothejob(CBHNDL)
END IF
end select


CASE %WM_CLOSE
' -- Put code to be executed before dialog end here

END SELECT

'---In case of keyboard pressed ...
'CASE %wm_keydown
'end select

END FUNCTION

function OpenFile(hDlg as dword) as string
dim sFile as string
dim sFilter as string

sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"

sFile = Dialog_OpenFile(hDlg, _
"Open an file", _
APP_SOURCEPATH, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY or %OFN_ENABLESIZING)
function = sFile
end function


function SaveFile(hDlg as dword) as string
dim sFile as string
dim sFilter as string

sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"

sFile = Dialog_SaveFile(hDlg, _
"Open an file", _
APP_SOURCEPATH, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY or %OFN_ENABLESIZING)
function = sFile
end function


Function DoTheJob(byval ID_In as long, byval ID_Out as long, AdditionalText as string) as long

control get text cbhndl, ID_In to sInput
if trim$(sInput) = "" then
sInput = "<<<- Please type some text ->>>"
end if
Counter += 1
sInput = AdditionalText & format$(Counter, "0000") & " " & sInput

'control get text hDlg, ID_Out to sBuffer
control appendtotop text cbhndl, ID_Out, sInput & $crlf '& sBuffer
control set text cbhndl, ID_In, ""
CONTROL set focus cbhndl, ID_In

end function


good evening, ciao, Lionheart

Petr Schreiber
13-05-2009, 22:02
Hi Frank,

you were close! But there were some problems:

your DoTheJob used CBHNDL variable, which is allowed only in CALLBACK functions
DoTheJob had specified 4 parameters, while you supplied just one
WM_KeyDown was used like control ID


The easiest approach I know is to use the power of magical %IDOK - button specified using %IDOK is default, so when you hit enter, it is activated.

So I added button with this magic ID, and in the callback called your DoTheJob function.

See complete code listing below:


' Empty GUI script created on 05-13-2009 21:52:49 by Petr Schreiber (ThinAIR)
' Basic Template for custom dialog
' Start Date 05-13-2009
' Created by by Petr Schreiber

USES "UI", "FILE" ', "CONSOLE"

DIM hDlg AS LONG '---Dialog handle
DIM sFile AS string
DIM sInput AS string '---Used to get input from field

DIM Counter AS LONG

' -- ID numbers of controls
Begin Const
%ID_CLOSE = %WM_USER + 500
%ID_Text
%ID_Load
%ID_SAVE
%ID_TXTENTER
%ID_LBLENTER
End Const

' -- Create dialog here
FUNCTION TBMAIN()
LOCAL hDlg AS DWORD

DIALOG New 0, "Textbox Enter Key 1000",-1,-1, 170, 255, _
%WS_POPUP Or %WS_VISIBLE Or _
%WS_CLIPCHILDREN Or %WS_CAPTION OR _
%WS_SYSMENU Or %WS_MINIMIZEBOX, 0 To hDlg

' -- Place controls here
CONTROL ADD textbox, hDlg, %ID_Text, "Text ... yes!", 5, 5, 150, 150, %ES_AUTOHSCROLL OR %ES_LEFT OR %WS_BORDER OR %WS_TABSTOP or %ES_MULTILINE or %ES_WANTRETURN or %WS_VSCROLL, %WS_EX_CLIENTEDGE OR %WS_EX_LEFT

CONTROL ADD BUTTON, hDlg, %ID_Load, "Load", 5, 180, 60, 14
CONTROL ADD BUTTON, hDlg, %ID_Save, "Save", 95, 198, 60, 14
CONTROL ADD BUTTON, hDlg, %ID_Close, "Click to close", 95, 180, 60, 14
CONTROL ADD BUTTON, hDlg, %IDOK, "Click to close", 1000, 1000, 60, 14, call MagicButton
CONTROL ADD TEXTBOX , hDlg, %ID_TXTENTER, "type in" , 10, 235, 125, 16', %WS_TABSTOP;, call dothejob '
CONTROL ADD label , hDlg, %ID_LBLENTER, "Type some text and press <ENTER> key", 12, 220,140, 12

DIALOG SHOW MODAL hDlg, CALL dlgProc

END FUNCTION

' -- Callback for dialog
CALLBACK FUNCTION dlgProc()
local sFile as string

SELECT CASE CBMSG

CASE %WM_INITDIALOG
' -- Put code to be executed after dialog creation here

CASE %WM_COMMAND
' -- You can handle controls here

SELECT CASE CBCTL

CASE %ID_Load
IF CBCTLMSG = %BN_CLICKED THEN
sFile = OpenFile(cbhndl)
if len(sFile) then
control set text cbhndl, %ID_Text, file_load(sFile)
end if
END IF

CASE %ID_Save
IF CBCTLMSG = %BN_CLICKED THEN
sFile = SaveFile(cbhndl)
if len(sFile) then
file_save(sFile, Control_GetText(cbhndl, %ID_Text))
end if
END IF

CASE %ID_Close
IF CBCTLMSG = %BN_CLICKED THEN
DIALOG END CBHNDL
END IF

END SELECT


CASE %WM_CLOSE
' -- Put code to be executed before dialog end here

END SELECT

'---In case of keyboard pressed ...
'CASE %wm_keydown
'end select

END FUNCTION

function OpenFile(hDlg as dword) as string
dim sFile as string
dim sFilter as string

sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"

sFile = Dialog_OpenFile(hDlg, _
"Open an file", _
APP_SOURCEPATH, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY or %OFN_ENABLESIZING)
function = sFile
end function


function SaveFile(hDlg as dword) as string
dim sFile as string
dim sFilter as string

sFilter = "thinBasic Files (*.tBasic, *.tBasicc)|*.tBasic;*.tBasicc|"
sFilter += "Basic Files (*.BAS, *.INC)|*.BAS;*.INC|"
sFilter += "Resource Files (*.RC)|*.RC|"
sFilter += "Help files (*.HLP)|*.HLP|"
sFilter += "Text Files (*.TXT)|*.TXT|"
sFilter += "Word processing (*.RTF)|*.RTF|"
sFilter += "All Files (*.*)|*.*"

sFile = Dialog_SaveFile(hDlg, _
"Open an file", _
APP_SOURCEPATH, _
sFilter, _
"tBasic", _
%OFN_FILEMUSTEXIST OR %OFN_HIDEREADONLY or %OFN_ENABLESIZING)
function = sFile
end function

callback function MagicButton()
if cbctlmsg = %BN_CLICKED then
DoTheJob(CBHNDL, %ID_TXTENTER, %ID_Text, "Now this is additional text " )
end if
end function

Function DoTheJob(byval hDlg as dword, byval ID_In as long, byval ID_Out as long, AdditionalText as string) as long

control get text hDlg, ID_In to sInput
if trim$(sInput) = "" then
sInput = "<<<- Please type some text ->>>"
end if
Counter += 1
sInput = AdditionalText & format$(Counter, "0000") & " " & sInput

'control get text hDlg, ID_Out to sBuffer
control appendtotop text hDlg, ID_Out, sInput & $crlf '& sBuffer
control set text hDlg, ID_In, ""
CONTROL set focus hDlg, ID_In

end function

Lionheart008
13-05-2009, 22:50
:occasion:

one pils or weizenbier for you petr... ! :D

I am glad to see the fast progress between my first idea and this script! I like it :)


your DoTheJob used CBHNDL variable, which is allowed only in CALLBACK functions

yes, I know, but after I have tried it first with hDlg ;)

but nobody can beat the magic button!!!! great! 8)

good night, Lionheart