FLTK-term

For issues with communication ports, protocols, etc.
dasyar
Posts: 302
Joined: Dec 04, 2008 15:31

FLTK-term

Postby dasyar » Jun 01, 2016 13:13

I am still very interested in creating an FBC FLTK GUI terminal program; I thought I would start this thread to see how far I can get with this.

I guess first I have to determine if FLTK would be able to get me where I want too go. Since I would need to get text to display in the GUI window as a response to a button press, for example, this is the first problem for me. I have the code below which is not working as expected, when I press the button, I do not see "Button was clicked!" anywhere on the GUI window. Not sure why this is occurring.

At the moment I thinking of a GUI window with a menu bar across the top with selections for 'Start/Quit', 'COM Port', 'BAUD', as a starter, with the area below the menu bar to be a terminal interaction area. I guess if I could get to that point, that would be a very good start. So, the main question, how do I get some text to display in the GUI window, in response to a button press?

Code: Select all

'FLTK_first.bas

#INCLUDE "fltk-c.bi"

'Screen 12

DIM SHARED AS Fl_Window PTR Window_Main
DIM SHARED AS Fl_Box PTR Box_text
DIM SHARED AS Fl_Button PTR Button_Click



SUB Create_Window_Main()
   Window_Main = Fl_WindowNew(300,200,"Button Test")
   Box_text = Fl_BoxNew(100,80,100,20,"Welcome to FLTK")
   Button_Click = Fl_ButtonNew(100,160,100,20,"Click me!")
END SUB

SUB  Button_Click_Event CDECL(widget AS FL_Widget PTR)
   'PRINT "Button was clicked!"
   Fl_OutputNew(100,110,100,20,"Button was clicked!")
END SUB

Create_Window_Main()
Fl_WidgetSetCallback0(Button_Click,@Button_Click_Event())
Fl_WindowShow(Window_Main)
Fl_Run

END
badidea
Posts: 544
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: FLTK-term

Postby badidea » Jun 01, 2016 21:01

Code: Select all

'FLTK_first.bas

#INCLUDE "fltk-c.bi"

'Screen 12

DIM SHARED AS Fl_Window PTR Window_Main
DIM SHARED AS Fl_Box PTR Box_text
DIM SHARED AS Fl_Button PTR Button_Click

SUB Create_Window_Main()
   Window_Main = Fl_WindowNew(300,200,"Button Test")
   Box_text = Fl_BoxNew(100,80,100,20,"Welcome to FLTK")
   Button_Click = Fl_ButtonNew(100,160,100,20,"Click me!")
END SUB

SUB  Button_Click_Event CDECL(widget AS FL_Widget PTR)
   'PRINT "Button was clicked!"
   'Fl_OutputNew(100,110,100,20,"Button was clicked!")
   Fl_WidgetSetLabel(Box_text, "Like this?")
END SUB

Create_Window_Main()
Fl_WidgetSetCallback0(Button_Click,@Button_Click_Event())
Fl_WindowShow(Window_Main)
Fl_Run

END

Edit: On my linux box, part the previous text remains visible. I would expect that a Fl_WidgetRedraw(Box_text) would fix that, but it does not. Could be a problem of my system, I have had other FLTK drawing issues. Else try Fl_Double_Window

Edit2: Fl_WidgetRedraw(Window_Main) after Fl_WidgetSetLabel(Box_text, "Like this?") does work.
dasyar
Posts: 302
Joined: Dec 04, 2008 15:31

Re: FLTK-term

Postby dasyar » Jun 02, 2016 11:00

I tried the suggestion that was made in the previous post, I guess it sort of works.

The problem is that the first char gets lost, and the last char of the string gets truncated, so you do not have the exact string showing. I thought that there would be a much easier way to display a string on the GUI window at a specified location.

I guess I need some more suggestions, or at least some indication as to what I am doing wrong.

I am doing this on my Windows 10 64 bit machine using Geany as my IDE. I have inserted '-s GUI' in the compile command, this way when you click on the FBC created .exe file, you get only the GUI window.

Code: Select all

'FLTK_first.bas

#INCLUDE "fltk-c.bi"

'Screen 12

DIM SHARED AS Fl_Window PTR Window_Main
DIM SHARED AS Fl_Box PTR Box_text
DIM SHARED AS Fl_Button PTR Button_Click
DIM SHARED AS Fl_Box PTR Box2_text


SUB Create_Window_Main()
   Window_Main = Fl_WindowNew(300,200,"Button Test")
   Box_text = Fl_BoxNew(100,80,100,20,"Welcome to FLTK")
   Box2_text = Fl_BoxNew(100,110,100,20,"?                    ")
   Button_Click = Fl_ButtonNew(100,160,100,20,"Click me!")
END SUB

SUB  Button_Click_Event CDECL(widget AS FL_Widget PTR)
   'PRINT "Button was clicked!"
   'Fl_OutputNew(100,110,100,20," Button was clicked!")
   Fl_WidgetSetLabel(Box2_text, "Button was clicked!")
END SUB

Create_Window_Main()
Fl_WidgetSetCallback0(Button_Click,@Button_Click_Event())
Fl_WindowShow(Window_Main)
Fl_Run

END
dasyar
Posts: 302
Joined: Dec 04, 2008 15:31

Re: FLTK-term

Postby dasyar » Jun 03, 2016 11:21

Below is a simple GUI window with a menu bar code, all of the components used work as expected.

I am starting to get the feeling that FLTK program just might be a little to light of a toolkit for my project. One key component is not available, or at least I have not stumbled across it, a multiline text widget with an IO component. I would need a widget that you could associate a port assignment that you could have the expected IO GUI session. I noticed that in all of the examples FLTK uses the CL window, but I am looking for something that will use a text area within the GUI window.

Since I already did a CL window term program, I would like to stick with a GUI window term program. Maybe those FB tools are still not available for creating a freeBasic GUI terminal program. Still looking for a program...


Code: Select all

'FLTKterm.bas
'This is the FB FLTK term file.
#INCLUDE "fltk-c.bi"

DECLARE SUB EndProgram CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
DECLARE SUB Info CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)


' Variables and etc
DIM SHARED AS Fl_Window PTR Window_Main

DIM SHARED AS Fl_Menu_Bar PTR Menu



' Create Main_window
SUB Create_Window_Main()
   Window_Main = Fl_WindowNew(900,700,"FLTKterm")
   Menu = Fl_Menu_BarNew(0,0,900,20)
   Fl_Menu_Add(Menu,"Quit",,@EndProgram())
   Fl_Menu_Add(Menu,"Help",,@Info())
END SUB

SUB EndProgram CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
   END
END SUB

SUB Info CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
   flMessageBox("Program Information","This is the FLTKterm terminal program.")
END SUB

' Main start
Create_Window_Main()
Fl_WindowShow(Window_Main)
Fl_Run

END
D.J.Peters
Posts: 7166
Joined: May 28, 2005 3:28
Location: Germany

Re: FLTK-term

Postby D.J.Peters » Jun 04, 2016 15:42

dasyar
Posts: 302
Joined: Dec 04, 2008 15:31

Re: FLTK-term

Postby dasyar » Jun 04, 2016 21:10

Thanks for the leads, but I am still more interested in using freeBasic tools, for creating a GUI terminal program.

Back when I was doing the console window terminal program, using freeBasic, I looked into finding some GUI tools, there were none. So, freeBasic has come a long way in terms of addressing GUI programs but it still has a way to go.

I am still not giving up, but I hate to fall back to a GUI/console window type setup. At this point that looks like the only option that is viable.
Lothar Schirm
Posts: 331
Joined: Sep 28, 2013 15:08
Location: Bavaria, Germany

Re: FLTK-term

Postby Lothar Schirm » Jun 05, 2016 15:09

Perhaps you could use two different widgets, e.g. an input widget (multiline?) for inputs and a text display for outputs?
dasyar
Posts: 302
Joined: Dec 04, 2008 15:31

Re: FLTK-term

Postby dasyar » Jun 06, 2016 11:51

I have used that type of terminal program before, input in one window, look for output in another window. I personally find those the types of programs cumbersome to use, but I guess some people like them.

On second thought maybe this setup would be somewhat better than maneuvering between a console window and your GUI window. Their has to be some kind of compromise that is available that could be used that makes a terminal session non cumbersome.
D.J.Peters
Posts: 7166
Joined: May 28, 2005 3:28
Location: Germany

Re: FLTK-term

Postby D.J.Peters » Jun 06, 2016 19:43

It's a Fl_Text_Editor widget for user input and the output of commands.
You have to use the FB commands shell and execute.

No magic at all

Joshy
dasyar
Posts: 302
Joined: Dec 04, 2008 15:31

Re: FLTK-term

Postby dasyar » Jun 07, 2016 19:18

So, moving along...

The program below kind of creates the window that I am after, a functional menu selection, and a Text_Edit area. Still not sure how the Text_Editor is working, the CR definitely moves to a new line. The question is, is the text or chars being typed in, are being saved to 'var buf' as a string or are they being saved as individual chars? If I wanted to capture a string to send out the PORT, will I have to deal with creating a string and then deal with a 'CR'?

Still not sure how I will be able to deal with the incoming string/char, and show it in the Text_Edit area. There is the 'Fl_Text_DisplaySetBuffer edt,buf' command, but I did not find any docs for that, so I am not sure how that should be used beyond the example that is shown.


Code: Select all

#INCLUDE "fltk-c.bi"

DECLARE SUB EndProgram CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
DECLARE SUB Info CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)


' Variables and etc
DIM SHARED AS Fl_Window PTR Window_Main
DIM SHARED AS Fl_Menu_Bar PTR Menu




' Create Main_window.
SUB Create_Window_Main()
   Window_Main = Fl_WindowNew(900,700,"FLTKterm")
   Menu = Fl_Menu_BarNew(0,0,900,20)
   Fl_Menu_Add(Menu,"Quit",,@EndProgram())
   Fl_Menu_Add(Menu,"Help",,@Info())
   var buf = Fl_Text_BufferNew()
   var edt = Fl_Text_EditorNew(5,30,Fl_WidgetGetW(Window_Main)-10,Fl_WidgetGetH(Window_Main))
   Fl_Text_DisplaySetBuffer edt,buf
END SUB

' Clean exit of program.
SUB EndProgram CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
   END
END SUB

' Display the message box.
SUB Info CDECL(widget AS Fl_Widget PTR, userData AS ANY PTR)
   flMessageBox("Program Information","This is the FLTKterm terminal program.")
END SUB

' Main start
Create_Window_Main()
Fl_WindowShow(Window_Main)
Fl_Run ' Runs constantly until 'Quit' button pressed.

END
D.J.Peters
Posts: 7166
Joined: May 28, 2005 3:28
Location: Germany

Re: FLTK-term

Postby D.J.Peters » Jun 07, 2016 21:03

dasyar wrote:Still not sure how the Text_Editor is working,
take a look at :
Fl_Text_Editor01.bas
Fl_Text_Editor02.bas
Fl_Text_Editor03.bas
dasyar wrote: the CR definitely moves to a new line. The question is, is the text or chars being typed in, are being saved to 'var buf' as a string or are they being saved as individual chars?
here are a starting point:

Joshy

Code: Select all

#INCLUDE "fltk-c.bi"
sub ModifyCb cdecl (byval p           as long, _
                    byval nInserted   as long, _
                    byval nDeleted    as long, _
                    byval nRestyled   as long, _
                    byval deletedText as const zstring ptr, _
                    byval Editor   as any ptr)
  if nInserted=0 andalso nDeleted=0 then exit sub
  dim as Fl_Text_Buffer ptr TextBuffer  = Fl_Text_DisplayGetBuffer(Editor)
  dim as zstring ptr Text
  dim as long e , s=p , char = Fl_Text_BufferCharAt(TextBuffer,p)
  if (nInserted > 0) then e = s + nInserted
  if (nDeleted  > 0) then e = s + nDeleted
  s = Fl_Text_BufferLineStart(TextBuffer,s)
  e = Fl_Text_BufferLength(TextBuffer)
  'e = Fl_Text_BufferLineEnd(TextBuffer,e)
  Text  = Fl_Text_BufferTextRange(TextBuffer ,s,e)
  if char=10 andalso len(*text)>1 then
    print "exeute command: " & *Text
  end if
end sub
'
' main
'
var win = Fl_Double_WindowNew(620,520,"Terminal Test")
var edt = Fl_Text_EditorNew(10,10,Fl_WidgetGetW(win)-20,Fl_WidgetGetH(win)-20)
var buf = Fl_Text_BufferNew()
Fl_Text_DisplaySetBuffer(edt,buf)
Fl_Text_BufferAddModifyCallback(buf,@ModifyCB,edt)
Fl_GroupSetResizable(win,edt)
Fl_WindowShow(win)
Fl_Run()
badidea
Posts: 544
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: FLTK-term

Postby badidea » Jun 07, 2016 21:15

Commands I used for my FLTK program some time ago:

=== text buffer stuff ===

dim shared as Fl_Text_Display ptr pTextDisplay
dim shared as Fl_Text_Buffer ptr pTextBuffer

pTextBuffer = Fl_Text_BufferNew()
Fl_Text_DisplaySetBuffer(pTextDisplay, pTextBuffer)
Fl_Text_DisplayShowCursor(pTextDisplay)

Fl_Text_BufferSetText(pTextBuffer, "")

dim as zstring ptr pText
Fl_Text_BufferAppend(pTextBuffer, *pText & LF)

=== cursor stuff ===

dim as long insPosCur
insPosCur = Fl_Text_DisplayGetInsertPosition(pTextDisplay)
Fl_Text_DisplayMoveDown(pTextDisplay)
Fl_Text_DisplayShowInsertPosition(pTextDisplay)

Some discussion here http://www.freebasic.net/forum/viewtopic.php?f=14&t=21548&hilit=fltk&start=645#p209552 about my confusion with Fl_Text_Display. (The 'obsolete' thread).

FTLK is nice, but the way it is wrapped for Freebasic (by D.J.Peters) is not always compatible with my brain.
D.J.Peters
Posts: 7166
Joined: May 28, 2005 3:28
Location: Germany

Re: FLTK-term

Postby D.J.Peters » Jun 07, 2016 22:02

as an example type:
dir (on windows)
ls -la (on linux)

Joshy

Code: Select all

#INCLUDE "fltk-c.bi"
sub ModifyCb cdecl (byval nCurPos     as long, _
                    byval nInserted   as long, _
                    byval nDeleted    as long, _
                    byval nRestyled   as long, _
                    byval deletedText as const zstring ptr, _
                    byval Editor      as any ptr)
  if nInserted=0 andalso nDeleted=0 then exit sub
  dim as Fl_Text_Buffer ptr TextBuffer  = Fl_Text_DisplayGetBuffer(Editor)
  dim as zstring ptr Text
  dim as long e , s=nCurPos , char = Fl_Text_BufferCharAt(TextBuffer,nCurPos)
  dim as string aLine
  if (nInserted > 0) then e = s + nInserted
  if (nDeleted  > 0) then e = s + nDeleted
  s = Fl_Text_BufferLineStart(TextBuffer,s)
  e = Fl_Text_BufferLength(TextBuffer)
  'e = Fl_Text_BufferLineEnd(TextBuffer,e)
  Text  = Fl_Text_BufferTextRange(TextBuffer ,s,e)
  if char=10 andalso len(*text)>3 then
    aLine = left(*text,len(*text)-1)
    aLine &= !" > tmp.txt\n"
    var result = shell(aLine)
    If result = -1 Then
      beep
    else
      aLine = "exeute command: " & *Text & !"\n"
      Fl_Text_BufferAppend(TextBuffer,strptr(aLine))
      var hFile =FreeFile()
      open "tmp.txt" for input as #hFile
        while not eof(hFile)
          line input #1,aLine
          aLine &=!"\n"
          Fl_Text_BufferAppend(TextBuffer,strptr(aLine))
        wend
      close #hFile
    end if
  end if
  if Text then Fl_Free Text
end sub
'
' main
'
chdir exepath()
var win = Fl_Double_WindowNew(620,520,"Terminal Test")
var edt = Fl_Text_EditorNew(10,10,Fl_WidgetGetW(win)-20,Fl_WidgetGetH(win)-20)
var buf = Fl_Text_BufferNew()
Fl_Text_DisplaySetBuffer(edt,buf)
Fl_Text_DisplaySetTextFont(edt,Fl_COURIER_BOLD)
Fl_Text_BufferAddModifyCallback(buf,@ModifyCB,edt)
Fl_GroupSetResizable(win,edt)
Fl_WindowShow(win)
Fl_Run()

dasyar
Posts: 302
Joined: Dec 04, 2008 15:31

Re: FLTK-term

Postby dasyar » Jun 07, 2016 22:27

FTLK is nice, but the way it is wrapped for Freebasic (by D.J.Peters) is not always compatible with my brain.

I looked at sGUI, I found that to be more difficult to figure out. I have yet to find an easy GUI program to be always compatible with my brain. If you want something that is somewhat mind boggling, give Visual C# a try.

May I suggest some sort of complete FLTK command documentation sheet. The examples are very useful, but maybe it should also include a list and explanation of all of the FTLK commands. As an example 'var buf = Fl_Text_BufferNew()', this does not tell me very much about what the command can really do.

I will keep hacking at this, and hopefully it will start to sink in, and the outcome could be a freeBasic GUI terminal program.
MrSwiss
Posts: 2022
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: FLTK-term

Postby MrSwiss » Jun 08, 2016 9:40

dasyar wrote:May I suggest some sort of complete FLTK command documentation sheet.

Why don't you get it from the Source: fltk.org/documentation?

Instead of using trial & error Method ???

Return to “Hardware Interfaces / Communication”

Who is online

Users browsing this forum: No registered users and 1 guest