Search code examples
rgwidgetsrgtk2

How to retrieve checkbox state in gWidgets2tcltk (works in gWidgets2RGtk2)


The basic problem seem to be that I can't retrieve the state of a widget from an internal function.

I am trying to make my package developed using gWidgets2RGtk2 compatible with gWidgets2tcltk. The package includes a graphical user interface implementing the possibility to save the state of the gui. The code works as intended using the RGtk2 but there are problems using tcltk. I have tried various variants of the if constructs used. I find it strange that I can read the state from within .loadState, but not from within .saveState. They are called from different places, so can the problem be related to environments? I have been stuck for a while so maybe I am blind to an obvious solution, or perhaps there is a much better way to accomplish the behaviour I am after. The below code exemplifies the problem.

I am using R version 3.5.1 on a Windows 10 system, gWidgets2tcltk_1.0-6, and gWidgets2_1.0-7.

# The code works as intended using RGtk2.
require(gWidgets2RGtk2)
options("guiToolkit"="RGtk2")

# The code does not work when using tcltk.
# require(gWidgets2tcltk)
# options("guiToolkit" = "tcltk")

saveStateExample <- function(env = parent.frame(), savegui = NULL) {

  # savegui = NULL, Default when started manually as GUI wrapped function.
  # savegui = TRUE, Passed from main GUI when started as part of full program.
  # savegui = FALSE, Passed from main GUI when started as part of full program.


  # Create windows.
  w <- gwindow(title = "Checkbox behaviour", visible = FALSE)

  # Runs when window is closed.
  addHandlerDestroy(w, handler = function(h, ...) {

    # Save GUI state.
    .saveState()
  })

  # Create container.
  g <- ggroup(container = w, expand = TRUE, horizontal = FALSE)

  # Add checkbox to control saving gui statesa.
  save_state_chk <- gcheckbox(text = "Save state", checked = FALSE, container = g)

  # Add a text widget.
  text_edt <- gedit(container = g)

  # Add buttons to manually trigger the functions.
  load_btn <- gbutton(text = "Run .loadState", container = g)
  save_btn <- gbutton(text = "Run .saveState", container = g)

  addHandlerChanged(load_btn, handler = function(h, ...) {

    .loadState()

  })

  addHandlerChanged(save_btn, handler = function(h, ...) {

    .saveState()

  })

  # Internal function ---------------------------------------------------------

  .loadState <- function() {
    message(".loadState")
    message("save_state_chk was ", svalue(save_state_chk))
    message("savegui was ", savegui)

    # First check if save argument was passed.
    if (!is.null(savegui)) {
      # Update widget with passed value.
      svalue(save_state_chk) <- savegui
      message("save_state_chk set to ", savegui)
      message("save_state_chk is ", svalue(save_state_chk))
    } else {
      # Look for previously saved flag.
      if (exists(".package_savegui", envir = env, inherits = FALSE)) {
        svalue(save_state_chk) <- get(".package_savegui", envir = env)
        message(".package_savegui loaded")
      }
    }

    message("LOAD SAVED STATE")

    # Then load settings if true.
    if (svalue(save_state_chk)) {
      if (exists(".package_text", envir = env, inherits = FALSE)) {
        svalue(text_edt) <- get(".package_text", envir = env)
      }

      message("GUI saved state loaded")
    } else {
      message("GUI default state loaded")
    }
  }

  # Internal function ---------------------------------------------------------

  .saveState <- function() {
    message(".saveState")
    message("save_state_chk was ", svalue(save_state_chk))
    message("savegui was ", savegui)

    # First check status of save flag.
    if (is.null(svalue(save_state_chk))) {
      message("save_state_chk=NULL")
    } else {
      message("SAVE STATE")
      # Then save settings if true.
      if (svalue(save_state_chk)) {
        assign(x = ".package_savegui", value = svalue(save_state_chk), envir = env)
        assign(x = ".package_text", value = svalue(text_edt), envir = env)

        message("GUI state saved")
      } else { # or remove all saved values if false.

        if (exists(".package_savegui", envir = env, inherits = FALSE)) {
          remove(".package_savegui", envir = env)
        }
        if (exists(".package_text", envir = env, inherits = FALSE)) {
          remove(".package_text", envir = env)
        }

        message("GUI state cleared")
      }
    }
  }

  # Run internal function to load state before showing window.
  .loadState()
  visible(w) <- TRUE
}

# Open gui.
saveStateExample()

EDIT1: I have made a slightly more minimal example below that works with both toolkits as you describe. The purpose was to make sure that the internal functions worked as intended, which they did.

To further track down the problem I edited the first code example above to include buttons to manually trigger the internal functions. The buttons work, the state can be read and is printed by the message function. However, the state is still not read when the destroy handler is triggered (using the tcltk toolkit). It seems like something is destroyed too early using the tcltk toolkit, but not using the RGtk2 toolkit. Any ideas?

# The code works as intended using RGtk2 and tcltk.
# require(gWidgets2RGtk2)
# options("guiToolkit"="RGtk2")

 require(gWidgets2tcltk)
 options("guiToolkit" = "tcltk")

saveStateExample <- function() {

  # Create windows.
  w <- gwindow(title = "Checkbox behaviour", visible = FALSE)

  # Runs when window is closed.
  addHandlerDestroy(w, handler = function(h, ...) {

    message("Window destroyed")
    message("save_state_chk is ", svalue(save_state_chk))
    message("text_edt is ", svalue(text_edt))

  })

  # Create container.
  g <- ggroup(container = w, expand = TRUE, horizontal = FALSE)

  # Add checkbox to control saving gui statesa.
  save_state_chk <- gcheckbox(text = "Save state", checked = FALSE, container = g)

  # Add a text widget.
  text_edt <- gedit(container = g)

  # Add buttons.
  check_btn <- gbutton(text = "Check", container = g)
  uncheck_btn <- gbutton(text = "UnCheck", container = g)

  addHandlerChanged(check_btn, handler = function(h, ...) {

    .setCheckTrue()

  })

  addHandlerChanged(uncheck_btn, handler = function(h, ...) {

    .setCheckFalse()

  })

  # Internal function ---------------------------------------------------------

  .setCheckTrue <- function() {

    message(".setCheckTrue")
    message("save_state_chk was ", svalue(save_state_chk))

    svalue(save_state_chk) <- TRUE
    message("save_state_chk is ", svalue(save_state_chk))
    message("text_edt is ", svalue(text_edt))

  }

  # Internal function ---------------------------------------------------------

  .setCheckFalse <- function() {

    message(".setCheckFalse")
    message("save_state_chk was ", svalue(save_state_chk))

    svalue(save_state_chk) <- FALSE
    message("save_state_chk is ", svalue(save_state_chk))
    message("text_edt is ", svalue(text_edt))

  }

  # Show window.
  visible(w) <- TRUE

}

# Open gui.
saveStateExample()

EDIT2: Thanks for the tip @jverzani, I have tried addHandlerUnrealize as you suggested. That solves the problem of not reading any value for tcltk. However, it took me a while to sort things out and get it to work with both toolkits - the window could not be closed. I did not find much in the documentation, but by trial and error it seem like RGtk2 and tcltk have implemented different logistics. To continue to the destroy event RGtk2 requires FALSE, while tcltk requires TRUE. Below is a fixed version of the first code example:

# The code now works as intended using both RGtk2 and tcltk!
# require(gWidgets2RGtk2)
# options("guiToolkit"="RGtk2")
require(gWidgets2tcltk)
options("guiToolkit" = "tcltk")

saveStateExample <- function(env = parent.frame(), savegui = NULL) {

  # savegui = NULL, Default when started manually as GUI wrapped function.
  # savegui = TRUE, Passed from main GUI when started as part of full program.
  # savegui = FALSE, Passed from main GUI when started as part of full program.


  # Create windows.
  w <- gwindow(title = "Checkbox behaviour", visible = FALSE)

  # Runs when window is closed.
  addHandlerUnrealize(w, handler = function(h, ...) {

    # Save GUI state.
    .saveState()

    message("UNREALIZE!")

    # Check which toolkit we are using.
    if (gtoolkit() == "tcltk") {
      message("tcltk, returned TRUE!")
      return(TRUE) # Destroys window under tcltk, but not RGtk2.
    } else {
      message("RGtk2, returned FALSE!")
      return(FALSE) # Destroys window under RGtk2, but not with tcltk.
    }
  })

  # Runs when window is closed.
  addHandlerDestroy(w, handler = function(h, ...) {
    message("DESTROY!")
    # addHandlerDestroy does not care of return type for either RGtk2 or tcltk?
  })

  # Create container.
  g <- ggroup(container = w, expand = TRUE, horizontal = FALSE)

  # Add checkbox to control saving gui statesa.
  save_state_chk <- gcheckbox(text = "Save state", checked = FALSE, container = g)

  # Add a text widget.
  text_edt <- gedit(container = g)

  # Add buttons to manually trigger the functions.
  load_btn <- gbutton(text = "Run .loadState", container = g)
  save_btn <- gbutton(text = "Run .saveState", container = g)

  addHandlerChanged(load_btn, handler = function(h, ...) {
    .loadState()
  })

  addHandlerChanged(save_btn, handler = function(h, ...) {
    .saveState()
  })

  # Internal function ---------------------------------------------------------

  .loadState <- function() {
    message(".loadState")
    message("save_state_chk was ", svalue(save_state_chk))
    message("savegui was ", savegui)

    # First check if save argument was passed.
    if (!is.null(savegui)) {
      # Update widget with passed value.
      svalue(save_state_chk) <- savegui
      message("save_state_chk set to ", savegui)
      message("save_state_chk is ", svalue(save_state_chk))
    } else {
      # Look for previously saved flag.
      if (exists(".package_savegui", envir = env, inherits = FALSE)) {
        svalue(save_state_chk) <- get(".package_savegui", envir = env)
        message(".package_savegui loaded")
      }
    }

    message("LOAD SAVED STATE")

    # Then load settings if true.
    if (svalue(save_state_chk)) {
      if (exists(".package_text", envir = env, inherits = FALSE)) {
        svalue(text_edt) <- get(".package_text", envir = env)
      }

      message("GUI saved state loaded")
    } else {
      message("GUI default state loaded")
    }
  }

  # Internal function ---------------------------------------------------------

  .saveState <- function() {
    message(".saveState")
    message("save_state_chk was ", svalue(save_state_chk))
    message("savegui was ", savegui)

    # First check status of save flag.
    if (is.null(svalue(save_state_chk))) {
      message("save_state_chk=NULL")
    } else {
      message("SAVE STATE")
      # Then save settings if true.
      if (svalue(save_state_chk)) {
        assign(x = ".package_savegui", value = svalue(save_state_chk), envir = env)
        assign(x = ".package_text", value = svalue(text_edt), envir = env)

        message("GUI state saved")
      } else { # or remove all saved values if false.

        if (exists(".package_savegui", envir = env, inherits = FALSE)) {
          remove(".package_savegui", envir = env)
        }
        if (exists(".package_text", envir = env, inherits = FALSE)) {
          remove(".package_text", envir = env)
        }

        message("GUI state cleared")
      }
    }
  }

  # Run internal function to load state before showing window.
  .loadState()
  visible(w) <- TRUE
}

# Open gui.
saveStateExample()

Solution

  • The cause of the problem was that addHandlerDestroy is not suitable for saving widget state. It may work, as it did for RGtk2, but there is no guarantee that widgets will be reachable at this point. The solution is to use addHandlerUnrealize instead, as pointed out by @jverzani in a comment:

    You should try addHandlerUnrealize here (though I don't know if this works for RGtk2 and can't test). That will call destroy as long as your handler does not return FALSE but will execute before the destroy event so your widgets will still be available to read from. – jverzani Jan 24 at 16:38

    Interestingly, the example code revealed different implementations of signalling the destruction of a window (see the original post for a workaround). Note that @jverzani consider pushing a fix, that will change this.