GetGadgetState, SetGadgetItemState problems

Just starting out? Need help? Post your questions and find answers here.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4792
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

GetGadgetState, SetGadgetItemState problems

Post by Fangbeast »

First of all, I am not going to paste the whole 3,000 line program in here so don't ask. Second, the following bits of code should illustrate my problem for those who use these commands extensively.

The following code just opens a window and checks if the ListIconGadget has been selected (an item in it that is)

Code: Select all

Open_View_Window:

  If GetGadgetState(#Gadget_main_listbox) <> -1
    If FileOpen = 1                                           ; Only open window if file has been opened
      If mutex = 0                                            ; No other windows allowed to be opened at the same time except main
        mutex = 1                                             ; Set the single window openable flag
        If Window_view()
          Gosub Get_Letter
        Else
          Stat(0, "Cannot open view window, problem with screen, memory or program")
        EndIf
      EndIf
    Else
      Stat(0, "A file must be open for this function to work!!")
    EndIf
  Else
    Stat(0, "There is no line currently selected to view")
  EndIf
  
Return

The next bit of code is re-usable, it selects the right record from the database by getting the record number of the item from the ListIconGadget via CurrentLine and CurrentRecord.s

Code: Select all

Get_Letter:

  CurrentLine     = GetGadgetState(#Gadget_main_listbox)                    ; What line are we on
  CurrentRecord.s = GetGadgetItemText(#Gadget_main_listbox, CurrentLine, 0)
  Debug Str(CurrentLine)
  Qry.s = "Select Dfile From Style Where Id = " + CurrentRecord.s + ";"
  ;----------------------------------------------------------------------------------------
  If DatabaseQuery(Qry.s)                                              ; Build the table if you can
    While NextDatabaseRow()
      Dfile.s    = GetDatabaseString(0)
    Wend
    If ReadFile(0, Dfile.s) And Lof() > 0
      CloseFile(0)
    Else
      Stat(0, "File seems to be damaged or missing, try another file?")
      Return
     EndIf
    CabName$   = Dfile.s   
    ExtractTo$ = "C:\iCat2\View\"
    WildCard$  = ""
    CallFunctionFast(SetupIterateCabinet, CabName$, 0, @CabinetCallback(), #FILEOP_DOIT)
    SetGadgetText(#Gadget_view_viewbox,  "C:\iCat2\View\Flavor.htm")
    SetGadgetText(#Gadget_view_diskfile, Dfile.s)
  Else
    Stat(0, "Cannot get record, table, database or query error or file isn't a letter")
    Return
  EndIf

Return

The last two bits of code allow me to re-use the Get_Letter subroutine again and again going backwards and forwards through the items in the ListIconGadget.

Code: Select all

Next_Letter:

  SetGadgetItemState(#Gadget_main_listbox, CurrentLine, 0)
  SetGadgetItemState(#Gadget_main_listbox, CurrentLine + 1, #PB_ListIcon_Selected)
  ActivateWindow()
  Gosub Get_Letter
  
; Get the previous letter in the window

Previous_Letter:

  SetGadgetItemState(#Gadget_main_listbox, CurrentLine, 0)
  SetGadgetItemState(#Gadget_main_listbox, CurrentLine - 1, #PB_ListIcon_Selected)
  ActivateWindow()
  Gosub Get_Letter

Return

Next_Letter doesn't work, Previous_Letter does and I have no idea why. I'm tired, cranky, hungry haven't had sex in months and won't till some kind person tells me what stupid blunder I have pulled here.

Some kind person save my soul please??

P.s This exact same routine worked under a previous version of PB, I just retested it.
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet