Project

General

Profile

Actions

Create a text object and insert an image in the text

Here, we create a small graphical interface without xml file.
The textbuffer is editable, so the user can write. Reset to ... reset the content ;-)
With Open, the user can choose a PNG file and put it into the buffer at the position 0 (but we could put to another position if we/he want(s).

typeof win = ObjGtkWidget;;
typeof vbox = ObjGtkWidget;;
typeof hbox = ObjGtkWidget;;
typeof text = ObjGtkWidget;;
typeof textbuffer = ObjGtkWidget;;
typeof buttonPix = ObjGtkWidget;;
typeof buttonReset = ObjGtkWidget;;

/* Reflex called when the main window is destroyed 
The GTK+ main loop is stopped */
fun CBwindowDestroyed (obj, userParam)=
    _gtkMainQuit;
    0;;

/* Reflex called when the user has choosen an image file */
fun CBopenFile (obj, userParam, filename) =
    _gtkTextBufferInsertPix 
        textbuffer // the text buffer object
        _checkpack filename // the selected filename
        0         // the position where pix will be inserted
        10         // width (can be nil for the original size)
        10         // height (can be nil for the original size)
        0;        // force to keep the ratio (1) or not (0)
    0;;

/* Reflex called when the user click on one of the two buttons
Here, the user parameter gives the button clicked */
fun CBbuttonClicked (obj, userParam)=
    if (userParam) then // Pix button
    (
        _gtkDialogFileChooserNew 
            _channel     // channel
            win         // mother
            "Choose a *.txt file"     // title
            nil         // default directory (nil for root)
            SCOL_GTK_ACTION_OPEN    // action : save, open, select, create folder)
            [SCOL_GTK_DIALOG_FILTER_PATTERN "*.png" "PNG"] :: nil    // filter
            @CBopenFile        // define the callback
            0;            // use rparameter
        0
    )
    else
    (
        _gtkTextBufferSet textbuffer nil; // the text buffer will be empty (nil, or "")
        0;
    );;

fun main ()=
    _showconsole;

    // Create the main window
    set win = _gtkWindowNew _channel nil "Scol 2dGTK+ : text" [320 50] [500 400];
    // Callback to the window destroyed
    _gtkWindowCBdestroyed win @CBwindowDestroyed 10;
    // Create a vbox, no homogenous and with 5 pixels between each element
    set vbox = _gtkBoxNew _channel nil 0 5 SCOL_GTK_BOX_VT;
    // Create a hbox to our two buttons
    set hbox = _gtkBoxNew _channel nil 0 5 SCOL_GTK_BOX_HZ;
    /* Create a text buffer with a default tags table. It's not imperative but it's better. 
    Otherwise, create the text directly and get the default text buffer after */
    set textbuffer = _gtkTextBufferNew _channel nil;
    // Create a text object
    set text = _gtkTextNew _channel textbuffer "Salut !";
    // Create two buttons : display an image and reset
    set buttonReset = _gtkButtonNew _channel "gtk-refresh" SCOL_GTK_BUTTON_STOCKITEM 0;
    _gtkWidgetCB buttonReset @CBbuttonClicked 0 SCOL_GTK_BUTTON_CLICKED SCOL_GTK_SIGNAL_MASK_STOP;
    set buttonPix = _gtkButtonNew _channel "gtk-open" SCOL_GTK_BUTTON_STOCKITEM 0;
    _gtkWidgetCB buttonPix @CBbuttonClicked 1 SCOL_GTK_BUTTON_CLICKED SCOL_GTK_SIGNAL_MASK_STOP;
    // Put text and buttons into their boxes
    // Put buttons into hbox : the order of the list gives the displayed order of the buttons
    _gtkBoxAddChilds hbox buttonReset :: buttonPix :: nil 0;    // 0 : we begins the insert by the begin !
    // Put the text and hbox object into vbox and set customs parameters
    _gtkBoxAddChilds vbox text :: hbox :: nil 0;
    _gtkBoxSetChilds vbox [text 1 1 0 0] :: [hbox 0 0 0 0] :: nil;   
    // Add the global box to the main window
    _gtkWindowAddChild win vbox;

    // Display all elements
    _gtkWidgetShowAll win;

    // Launch the GTK+ main loop
    _gtkMain;

    0;;

Return Examples

Updated by arkeon almost 3 years ago ยท 2 revisions