Saturday, June 26, 2021

Toggling Text Visibility

 A simple way of toggling text is to assign a tag and manipulate its -invisible option, again using the event handling capability of a tag to control the setting. Notice that the snippet below, for each array two tags are created _${a} and _${a}_ where the former is the display header with the control, and the latter the tag to be elided. 
















# !/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

package require Gnocl

set txt [gnocl::text -tabs 150 -editable 0]
gnocl::window -child $txt -onDelete { exit } -setSize 0.25

array set app [gnocl::application]
array set fruit [list apple 1 bannana 2 cherry 3]
array set cars [list morris a humber b austin c]

proc addLabel {wid str tag} {
     $wid addChild [gnocl::label -text $str]
     $wid tag apply lineStart lineEnd -tags $tag
     $wid insert end \n
}

proc tagClick {w n event} {
     if { $event == "buttonPress-1" } {
          $w tag configure ${n}_ -invisible [ gnocl::toggle [$w tag cget ${n}_ -invisible]]
     }
}


foreach a [list fruit cars app] {

     $txt tag create _$a -paragraph blue -onEvent { %w clearSelection ; tagClick %w %n %t-%b }
     $txt insert end $a\n -tags _$a
     
     foreach n [array names $a] {
          $txt tag create _${a}_ -indent 10 -data 1
          $txt insert end "${n}\t[set ${a}($n)]\n" -tags _${a}_
     }
}

Tuesday, June 22, 2021

Getting Widget Style Properties

Until the move over to Gtk4, Gnocl is still built against the Gtk 2.21 libraries. One of the inconveniences of Gtk is getting and setting widget style settings which are considered to be set globally by the desktop style settings and not for the programmer to tinker around with. Needless to say, there are times when different defaults are preferred, largely to draw the users attention to 'something a bit different'.

The function gtk_widget_modify_font  is a convenience function to set the widget basefont as shown in this snippet from the button.c module, 

if ( options[baseFontIdx].status == GNOCL_STATUS_CHANGED ) {
GtkWidget *label;
label = gnoclFindChild ( GTK_WIDGET ( para->button ), GTK_TYPE_LABEL );
PangoFontDescription *font_desc = pango_font_description_from_string ( Tcl_GetString ( options[baseFontIdx].val.obj ) );
gtk_widget_modify_font ( GTK_WIDGET ( label ), font_desc );
pango_font_description_free ( font_desc );
}

Unfortunately, there's no direct retrieval convenience function in the Gtk API and the documentation on the matter is scant but, again from the button.c module, retrieving style properties can be done directly:

if ( idx == baseFontIdx ) {
GNOCL_DEBUG_LINE

GtkWidget *widget;
GtkStyle *style;

widget = gnoclFindChild ( GTK_WIDGET ( para->button ), GTK_TYPE_LABEL );
obj = Tcl_NewStringObj ( pango_font_description_to_string ( gtk_rc_get_style ( GTK_WIDGET ( widget ) )->font_desc), -1 );
}


There is an alternative api function gtk_style_get, as illustrated in this clip from treeList.c

if ( idx == treeLineWidthIdx ) {
gint size;
GtkStyle *style = gtk_rc_get_style ( para->view );
gtk_style_get ( style, G_OBJECT_TYPE ( para->view ), "tree-line-width", &size );
obj = Tcl_NewIntObj ( size );
goto end;
}


This won't work with base font as there's no object property!

The gtkstyle.h include file from the Gtk source code gives the structure containing all the details.


struct _GtkStyle
{
  GObject parent_instance;
  /*< public >*/
  GdkColor fg[5];
  GdkColor bg[5];
  GdkColor light[5];
  GdkColor dark[5];
  GdkColor mid[5];
  GdkColor text[5];
  GdkColor base[5];
  GdkColor text_aa[5]; /* Halfway between text/base */
  GdkColor black;
  GdkColor white;
  PangoFontDescription *font_desc;
  gint xthickness;
  gint ythickness;
  GdkGC *fg_gc[5];
  GdkGC *bg_gc[5];
  GdkGC *light_gc[5];
  GdkGC *dark_gc[5];
  GdkGC *mid_gc[5];
  GdkGC *text_gc[5];
  GdkGC *base_gc[5];
  GdkGC *text_aa_gc[5];
  GdkGC *black_gc;
  GdkGC *white_gc;
  GdkPixmap *bg_pixmap[5];
  /*< private >*/
  gint attach_count;
  gint depth;
  GdkColormap *colormap;
  GdkFont *private_font;
  PangoFontDescription *private_font_desc; /* Font description for style->private_font or %NULL */
  /* the RcStyle from which this style was created */
  GtkRcStyle *rc_style;
  GSList *styles;   /* of type GtkStyle* */
  GArray *property_cache;
  GSList         *icon_factories; /* of type GtkIconFactory* */
};



Thursday, June 17, 2021

Creating temporary files

The C stdio package has two function calls related to the creation of temporary files: tmpFile and tmpnam. The Glib too has similar functions in its api such as g_mkstemp, g_file_open_tmp and so on. For someone programming in C, both of these approaches are excellent resources but someones scripting in Tcl more suitable approaches can be taken these C procedures will require a significant amount code to produce an effective binding which, may not offer worthwhile advantages.

The inclusion of a new command gnocl::tmpFile into to the core offers a convenient way of producing unique filenames. The command takes a single argument, an optional prefix, and returns a unique name with a randomized suffix.

If a prefix is specified:


puts [gnocl::tmpFile aaa]

/tmp/aaa.SYzqiJQA8Eeg4GZC1eDb

puts [gnocl::tmpFile bbb]

/tmp/bbb.VIT88L2zjeynnHn5dVfH

puts [gnocl::tmpFile ccc]

/tmp/ccc.3IkQqXjYtwDowshdjjCc


Without a prefix, the defaul 'TclScript' will be used.


puts [gnocl::tmpFile]

/tmp/TclScript.RcrXWEALx2ZDuS3DITli


If a greater level of control in need in the event of opening multiple temporary files then something along the line of the following procs may be of use.


proc tmp:open {} {

    set fname [gnocl::tmpFile]

    set fp [open $fname w]

    lappend ::tmp:files $fp $fname

    return $fp

}


proc tmp:files {} {

return [set ::tmp:files]

}


proc tmp:close {fp} {

close $fp

set ::tmp:files [dict remove [set ::tmp:files] $fp]

}


proc tmp:closeAll {} {

dict for {k v} [set ::tmp:files] {  close $k }

set ::tmp:files ""

}


proc tmp:unclosed {} {

return [dict keys [set ::tmp:files]]

}


set FP1 [tmp:open]

puts $FP1

set FP2 [tmp:open]


puts [tmp:files]

puts [tmp:unclosed]


tmp:close $FP2

puts [tmp:unclosed]


tmp:closeAll

puts [tmp:unclosed]

Sunday, June 06, 2021

May 2021 News!

 Just a handful on modifications this month.

 

2021-05:
    gnocl::winfo parent $wid
        o no longer crashed when parent == null.
    gnocl::menu
        o -data and cget -children now work correctly.
    gnocl::text
        o fixed minor internal issues relating to getPos, lineStart/lineEnd keyword usage.
    gnocl::notebook
        o intermittant crash using the remove/removePage subcommand fixed.
        o began implementation of embedded tab widgets
    gnocl::eventBox
        o fixed problems with -child command.
            (For some strange reason, the gnoclOptChild call would not work
            for eventBoxes after it had been modified to remove previous
            the child. The same functionality is not handled by the local
            configure/cget functions and it works fine.)
    gnocl::list/tree
        o new command: autosize.
    gnocl::window
        o new option  -hideOnDelete, hides window when deleted,
          useful for preventing toolpalette window from being destroyed.