Search code examples
rfontsgwidgets

How to set font for a gcheckbox object


The font method in gWidgets does not seem to work for gcheckbox (with the RGtk2 toolkit). In the following code styling is applied correctly to a glabel object but fails for gcheckbox.

library(gWidgets)
w <- gwindow("test", height=50)
g <- ggroup(container=w)
cb1 <- gcheckbox(text="one", container=g)
cb2 <- gcheckbox(text="two", container=g)
label <- glabel("text", container=g)
font(cb2) <- list(size=20)
font(label) <- list(size=20)

Is there a way to make this work?


Solution

  • There isn't anything in the API for setting this font, but you can hack it in:

    ## setfont properties
    setfont_hack <- function(cb, spec) {
      require(RGtk2)
      widget <- getToolkitWidget(cb)$getChildren()[[1]]
      font_descr <- pangoFontDescriptionNew()
      if(!is.null(spec$weight))
        font_desc$setWeight(PangoWeight[spec$weight])
      if(!is.null(spec$style))
        font_desc$setStyle(PangoStyle[spec$style])
      if(!is.null(spec$scale))
        font_desc$setSize(spec$scale * PANGO_SCALE)
      if(!is.null(spec$family))
        font_desc$setFamily(spec$family)
      widget$modifyFont(font_desc)
    
      if(!is.null(spec$color))
        widget$modifyFg(GtkStateType[1], spec$color)
    }
    
    cb <- gcheckbox("label", cont=gwindow())
    setfont_hack(cb, list(weight="bold", color="blue"))
    

    It should actually be enough to call font<- on the label part of the checkbox (just getToolkitWidget(cb)$getChildren()[[1]]), but the inheritance isn't set up right. (S3 classes aren't properly promoted to S4.)

    In gWidgets2RGtk2 this isn't much better, but there you can do this:

    require(RGtk2)
    label <- getToolkitWidget(cb)$getChildren()[[1]]
    cb$set_rgtk2_font(label, list(weight="bold"))