Buchempfehlung
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Mikrocomputertechnik mit Controllern der Atmel AVR-RISC-Familie
Umfassend, aber leicht verständlich führt dieses Buch in die Programmierung von ATMEL AVR Mikrocontrollern ein. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

Tutorial

Widgetologie * GUI-Programierung mit FLTK

von MitgliedKnattertonSeite 1 von 1

Inhalt

8. Widgetologie
8.1 Box
8.2 Counter
8.3 Input
8.4 Choice

8. Widgetologie

ist ein weiteres Gebiet in dem Elemente von FLTK-C untersucht werden.

8.1 Box

Eine Box bietet eine Möglichkeit Text oder Grafik auf den Bildschirm zu bringen. Die Ausrichtung kann dabei für innerhalb und ausserhalb der Box gesondert angegeben werden. Einige Möglichkeiten:

' Fltk_Box_Text.bas

#include once "fltk-c.bi"

var Win = Fl_WindowNew(555, 555, "Fltk_Box_Text")
Dim As String text1 = "Text 1", text2 = "Text 2", text3 = "Text 3"

Var box1 = Fl_BoxNew(10,10,120, 50, text1)
Fl_WidgetSetAlign box1, Fl_ALIGN_TOP_LEFT or Fl_ALIGN_INSIDE
Fl_WidgetSetBox(box1, FL_ENGRAVED_BOX )
Fl_WidgetSetColor box1, FL_BLUE
Fl_WidgetSetLabelFont(box1, FL_COURIER)
Fl_WidgetSetLabelSize(box1,24)

Var box2 = Fl_BoxNew(10,210,120, 50, text2)
Fl_WidgetSetAlign box2, Fl_ALIGN_LEFT or Fl_ALIGN_INSIDE
Fl_WidgetSetBox(box2, FL_EMBOSSED_BOX)
Fl_WidgetSetColor box2, FL_RED
Fl_WidgetSetLabelFont(box2, FL_TIMES)
Fl_WidgetSetLabelSize(box2,24)

Var box3 = Fl_BoxNew(10,410,120, 50, text3)
Fl_WidgetSetAlign box3, Fl_ALIGN_BOTTOM_LEFT or Fl_ALIGN_INSIDE
Fl_WidgetSetBox(box3, FL_ENGRAVED_BOX)
Fl_WidgetSetColor(box3, FL_WHITE)
Fl_WidgetSetLabelFont(box3, FL_HELVETICA)
Fl_WidgetSetLabelSize(box3,24)

Var box4 = Fl_BoxNew(200, 10,120, 50, text1)
Fl_WidgetSetAlign box4, Fl_ALIGN_TOP or Fl_ALIGN_INSIDE
Fl_WidgetSetBox(box4, FL_ENGRAVED_BOX)
Fl_WidgetSetColor box4, FL_DARK_CYAN
Fl_WidgetSetLabelFont(box4, FL_TIMES)
Fl_WidgetSetLabelSize(box4,14)

Var box5 = Fl_BoxNew(200,210,120, 50, text2)
Fl_WidgetSetAlign box5, Fl_ALIGN_CENTER or Fl_ALIGN_INSIDE
Fl_WidgetSetBox(box5, FL_BORDER_BOX)
Fl_WidgetSetColor box5, FL_MAGENTA
Fl_WidgetSetLabelFont(box5, FL_TIMES)
Fl_WidgetSetLabelSize(box5,24)

Var box6 = Fl_BoxNew(200,410,120, 50, text3)
Fl_WidgetSetAlign box6, Fl_ALIGN_BOTTOM or Fl_ALIGN_INSIDE
Fl_WidgetSetBox(box6, FL_EMBOSSED_BOX)
Fl_WidgetSetColor box6, FL_YELLOW
Fl_WidgetSetLabelFont(box6, FL_HELVETICA)
Fl_WidgetSetLabelSize(box6,14)

Var box7 = Fl_BoxNew(400, 10,120, 50, text1)
Fl_WidgetSetAlign box7, Fl_ALIGN_TOP_RIGHT or Fl_ALIGN_INSIDE
Fl_WidgetSetBox(box7, FL_ENGRAVED_BOX)
Fl_WidgetSetColor box7, FL_GREEN
Fl_WidgetSetLabelFont(box7, FL_COURIER)
Fl_WidgetSetLabelSize(box7,14)

Var box8 = Fl_BoxNew(400,210,120, 50, text2)
Fl_WidgetSetAlign box8, Fl_ALIGN_RIGHT or Fl_ALIGN_INSIDE
Fl_WidgetSetBox(box8, FL_BORDER_BOX)
Fl_WidgetSetColor box8, FL_DARK_CYAN
Fl_WidgetSetLabelFont(box8, FL_TIMES)
Fl_WidgetSetLabelSize(box8,24)

Var box9 = Fl_BoxNew(400,410,120, 50, text3)
Fl_WidgetSetAlign box9, Fl_ALIGN_BOTTOM_RIGHT or Fl_ALIGN_INSIDE
Fl_WidgetSetBox(box9, FL_SHADOW_BOX )
Fl_WidgetSetColor box9, FL_DARK_YELLOW
Fl_WidgetSetLabelFont(box9, FL_HELVETICA)
Fl_WidgetSetLabelSize(box9,12)

' where FL_ALIGN can be one oft these

' Fl_ALIGN_CENTER   ' default
' Fl_ALIGN_TOP
' Fl_ALIGN_BOTTOM
' Fl_ALIGN_LEFT
' Fl_ALIGN_TOP_LEFT
' Fl_ALIGN_BOTTOM_LEFT
' Fl_ALIGN_LEFT_TOP
' Fl_ALIGN_RIGHT
' Fl_ALIGN_TOP_RIGHT
' Fl_ALIGN_BOTTOM_RIGHT
' Fl_ALIGN_RIGHT_TOP
' Fl_ALIGN_LEFT_BOTTOM
' Fl_ALIGN_RIGHT_BOTTOM
' Fl_ALIGN_POSITION_MASK

' can combined with "or" with these:

' Fl_ALIGN_INSIDE
' Fl_ALIGN_TEXT_OVER_IMAGE
' Fl_ALIGN_CLIP
' Fl_ALIGN_WRAP
' Fl_ALIGN_IMAGE_OVER_TEXT
' Fl_ALIGN_IMAGE_NEXT_TO_TEXT
' Fl_ALIGN_TEXT_NEXT_TO_IMAGE
' Fl_ALIGN_IMAGE_BACKDROP
' Fl_ALIGN_IMAGE_MASK

Fl_WindowShow Win
Fl_Run

box

8.2 Counter

Den Counter kann man auch für Integer oder Float nutzen.

' Fltk_Counter_01.bas

#include once "fltk-c.bi"

sub CounterCB cdecl (byval self as FL_WIDGET ptr,byval valuator as any ptr)
  print *Fl_WidgetGetLabel(self) & " value = " & Fl_ValuatorGetValue(valuator)
end sub

var win = Fl_WindowNew(333,333,"Fltk_Counter_01.bas")

var cnt1 = Fl_Simple_CounterNew(60,50,192,24,"int counter 1")
var cnt2 = Fl_Simple_CounterNew(60,100,192,24,"float counter 2")
var cnt3 = Fl_CounterNew(60,150,192,24,"int counter 3")
var cnt4 = Fl_CounterNew(60,200,192,24,"float counter 4")

 Fl_WidgetSetLabelType(cnt1,FL_LABEL_ENGRAVED )

' connect callbacks
Fl_WidgetSetCallbackArg cnt1,@CounterCB,cnt1
Fl_WidgetSetCallbackArg cnt2,@CounterCB,cnt2
Fl_WidgetSetCallbackArg cnt3,@CounterCB,cnt3
Fl_WidgetSetCallbackArg cnt4,@CounterCB,cnt4

' set to integer
Fl_ValuatorSetStep(cnt1, 1)
Fl_ValuatorSetStep(cnt3, 1)

' set bounds
Fl_ValuatorBounds(cnt1 , 0, 6)
Fl_ValuatorBounds(cnt2 , -7, 7)
Fl_ValuatorBounds(cnt3 , 0, 8)
Fl_ValuatorBounds(cnt4 , -1.7, 3.14)

' set initial values
Fl_ValuatorSetValue(cnt1 , 1)
Fl_ValuatorSetValue(cnt2 , -2.1)
Fl_ValuatorSetValue(cnt3 , 1)
Fl_ValuatorSetValue(cnt4 , 1.1)

Fl_GroupSetResizable win,win
Fl_WindowShow win
Fl_Run

counter


8.3 Input

Bei Input kann man Text oder Zahlen eingeben und es kann auch ein Wert vorgegeben werden.

' Fltk_Input_01.bas
' added initial values

#include once "fltk-c.bi"

'test of:
' Fl_InputNew
' Fl_Int_InputNew
' Fl_Float_InputNew
' Fl_Multiline_InputNew
' Fl_WidgetSetCallbackArg
' Fl_WidgetSetWhen

' nearly the same as Fl_Input03.bas
' but the userdata callback argument points to an Fl_Input_ ptr now.

sub InputCB cdecl (byval self as Fl_Widget ptr,byval input_ as any ptr)
 print "InputCB: " & *Fl_Input_GetValue(input_)
end sub

var win = Fl_WindowNew(320,200,"Fltk_Input_01.bas")
var inp1 = Fl_InputNew          (120, 5,190, 20,"Fl_Input:")
var inp2 = Fl_Int_InputNew      (120,30,190, 20,"Fl_Int_Input:")
var inp3 = Fl_Float_InputNew    (120,55,190, 20,"Fl_Float_Input:")
var inp4 = Fl_Multiline_InputNew(120,80,190,100,"Fl_Multiline_Input:")

Fl_WidgetSetWhen inp4,FL_WHEN_CHANGED

' set callbacks
Fl_WidgetSetCallbackArg inp1,@InputCB,inp1
Fl_WidgetSetCallbackArg inp2,@InputCB,inp2
Fl_WidgetSetCallbackArg inp3,@InputCB,inp3
Fl_WidgetSetCallbackArg inp4,@InputCB,inp4

' set initial values (for all strings)
Fl_Input_SetValue(inp1, "Test 1")
Fl_Input_SetValue(inp2, "1234")
Fl_Input_SetValue(inp3, "0.1234")
Fl_Input_SetValue(inp4, "Test 4")

Fl_WindowShow win
Fl_Run

input

8.4 Choice

Mit Choice kann man aus einer Dropdownbox auswählen.

' Fltk_Choice_01.bas

#include once "fltk-c.bi"

' test of:
' Fl_Choice    http://www.fltk.org/doc-1.3/classFl__Choice.html

sub ChoiceCB cdecl (byval self as Fl_Widget ptr,byval cho as any ptr)
  ' get parent of the widget
  var win = Fl_WidgetWindow(self)
  ' get index of selected item
  var ind = Fl_ChoiceGetValue(cho)
  ' copy label from item to window caption
  Fl_WindowCopyLabel win,Fl_Menu_GetMenu(cho)[ind].text
end sub

var win = Fl_WindowNew(256,240,"Fltk_Choice_01.bas")
var cho = Fl_ChoiceNew(64,30,128,24,"Label:")
' add item
Fl_Menu_Add3(cho,"item1")
Fl_Menu_Add3(cho,"item2|item4")
' insert item
Fl_Menu_Insert(cho,2,"item3")
' select item
Fl_ChoiceSetValue(cho,2)

'Fl_ChoiceSetValueByItem(cho, @"test") 'value))

' set label(choice is treated as widget here)
Fl_WidgetSetLabel(cho, "Test:")

' add standard callback
Fl_WidgetSetCallbackArg cho,@ChoiceCB,cho

Fl_WindowShow win
Fl_Run

choice

 

Zusätzliche Informationen und Funktionen
  • Das Tutorial wurde am 01.10.2019 von MitgliedKnatterton angelegt.
  • Die aktuellste Version wurde am 02.10.2019 von MitgliedKnatterton gespeichert.
  Bearbeiten Bearbeiten  

  Versionen Versionen