4. What's MiniGUI
Previous Home Next
4.12. Cat + Rat = Catrat (Button + CheckBox = CheckButton)

DESCRIPTION:
  CheckButton control - it's a really "catrat": acting as a checkbox, it looks like a button (or a normal button with text, or described in previous topic button with a picture). We will write a commands that defines these two controls: one - with the text, another - "with a picture.
  @ 30, 10 CHECKBUTTON CheckButton_1 ;
            CAPTION "CheckButton with text" ;
            WIDTH 150 ;
            VALUE .F.

  @ 70, 10 CHECKBUTTON CheckButton_2 ;
            PICTURE "Open.BMP" ;
            WIDTH 27 ;
            HEIGHT 27 ;
            VALUE .F. ;
            TOOLTIP "CheckButton with picture"
On the screen, these controls will look like this:

prog_12

Note that in the first checkbutton I pointed out the width (Width), otherwise the button with the width of the "default" (as - and if - remember, it is equal to 100 pixels) is not big enough to contain this "too long" label. Where I found "Open.BMP"?.. Somewhere in the examples (in the SAMPLES folder), I don't remember - look, there are many graphics files in folders with MiniGUI samples...

Let's write an example of the use of the "catrat" (more precisely - the two: with text and graphics). And yet something in the process learn a little...
PROCESS ITSELF:
  1. Create a file (or copy the "basis" of the previously created PROG_11) PROG_12.PRG and change the text to make it like this:

    --------------------- start of PROG_12.PRG -------------
    #include "minigui.ch"
    
    REQUEST HB_CODEPAGE_RU1251, HB_CODEPAGE_RU866
    
    //===============
    
    PROCEDURE Main
    
      private w1r, w1c
    
      SET LANGUAGE TO RUSSIAN
      hb_SetCodepage( "RU1251" )
    
      DEFINE WINDOW Win_1 ;
        AT 0,0 ;
        WIDTH 400 ;
        HEIGHT 200 ;
        TITLE "Cat + Rat = CatRat (Button + CheckBox = CheckButton)" ;
        MAIN ;
        NOMINIMIZE ;
        NOMAXIMIZE ;
        NOSIZE ;
        ON MOVE Fun_1() ;
        ICON "ICON_CAS.ICO" ;
        FONT "Times New Roman" SIZE 11
    
        @ 30, 10 CHECKBUTTON CB_1 ;
                  CAPTION "Dissaaaaable!" ;
                  WIDTH 200 ;
                  VALUE .F. ;
                  ON CHANGE Fun_2() ;
                  TOOLTIP "Click to ENABLE"
    
        @ 70, 10 CHECKBUTTON CB_2 ;
                  PICTURE "res\edit_ok.BMP" ;
                  WIDTH 27 ;
                  HEIGHT 27 ;
                  VALUE .F. ;
                  ON CHANGE Fun_3() ;
                  TOOLTIP "Click to ENABLE"
    
        @ 30, 250 LABEL L_1 value "Label 1" INVISIBLE
    
        @ 70, 250 LABEL L_2 value "Label 2" INVISIBLE
    
      END WINDOW
    
      CENTER WINDOW Win_1
    
      w1r := Win_1.Row
      w1c := Win_1.Col
    
      ACTIVATE WINDOW Win_1
    
    RETURN
    
    *--------------------------------------------------------*
    Function Fun_1()
    *--------------------------------------------------------*
    
    /*
     restoration the window coordinates
     specified when the program starts
     when you try to move the window
    */
    
      Win_1.Row := w1r
      Win_1.Col := w1c
    
    Return Nil
    
    *--------------------------------------------------------*
    Function Fun_2()
    *--------------------------------------------------------*
    
    // it change the text on the button CB_1, it's tooltip and visibility 
    
      if Win_1.CB_1.Value=.T.
        Win_1.CB_1.Caption := "All right already - you can!"
        Win_1.CB_1.Tooltip := strtran ( Win_1.CB_1.Tooltip, "ENABLE", "DISSAAAAABLE" )
      else
        Win_1.CB_1.Caption := "Dissaaaaable!"
        Win_1.CB_1.Tooltip := strtran ( Win_1.CB_1.Tooltip, "DISSAAAAABLE", "ENABLE" )
      endif
    
      Win_1.L_1.Visible := Win_1.CB_1.Value
    
    Return Nil
    
    *--------------------------------------------------------*
    Function Fun_3()
    *--------------------------------------------------------*
    
    // it change tooltip on the button CB_2 and visibility of label "L_2"
    
      if Win_1.CB_2.Value=.T.
        Win_1.CB_2.Tooltip := strtran ( Win_1.CB_2.Tooltip, "ENABLE", "DISSAAAAABLE" )
      else
        Win_1.CB_2.Tooltip := strtran ( Win_1.CB_2.Tooltip, "DISSAAAAABLE", "ENABLE" )
      endif
    
      Win_1.L_2.Visible := Win_1.CB_2.Value
    
    Return Nil
    
    --------------------- finish of PROG_12.PRG --------------

  2. Start the compilation (using our "universal" BAT-file - look about it in previous article):
    C:\where_it_is_into_the_comp>Compi.bat prog_12
    
    Compiled correctly? Run? "And why the button with the picture is empty?.." But I (and the program, I think, too) is interesting - where it will get file with the figure? You have to write a link to this file - "res\edit_ok.bmp" - but not a subfolder, or even more so the file in it, no! "And where can I get it?.." Personally me took it from folder MiniGUI\RESOURCES - so let's create a subfolder called "res", copy this file to it from RESOURCES... Now recompile program... Got it? That's just it. Now you can play in "I see - I do not see." :)

    prog_12a

    But... the button under the "picture" turned out too small - the picture edges rests... Well, let's see the size of the picture and correct in chekbutton determining (2-4 pixels to be larger than the figure on each side).

    prog_12b

    I.e. value of checkbutton obvious immediately: "pressed" button - it equals .T. (True); "depressed" - meaning .F. (False). Simple and clear...

    Under the program, I hope, no questions?.. And so it is clear what is being done with the visibility of labels L_1 and L_2 (do you remember? we played with them in article about CheckBox) so as changing tips (tooltip) for checkbuttons depending on what value they assume after the change (for ON CHANGE)... "No, it is unclear!.. For sure - not all... And what the function STRTRAN there? I don't know this..." Ah, I understand - you are not programmed in Clipper or Foxpro (or in any of these languages - the so-called "xBase-languages family" - "from where is gone" Harbour compiler and MiniGUI library)... Well, in general, you give to the function STRTRAN three options: 1st - the original string, 2nd - which part to change in this line (if you meet a few - to change everything), 3rd - what to change. The result of it's work - modified the original string... That's the changing of text and tooltips on the 1st checkbutton... Also, of course, it can be writes "more elegance" - but right now, for clarity, we write it "in the forehead."

    So, that's all... Oh, no, not all! Imagine that you have someone to whom you give (of course - proudly, but humbly smiling :) ) your program (ie, EXE-file only)... Imitate this process - create in a folder with your examples a "temporary" subfolder (call it, for example, "tmp") and copy there the file PROG_12.EXE only... Run it there (how to do it - somewhere at his home in New Zealand... why New?.. well, let in Old Zealand... :) in general, on some other computer - the one whom you have given your "masterpiece")... "Huuum... And why this button is clear again?.." "Oh-oh! And icon from the top left corner runs away, too!.."

    Aha! Here's the hitch... We must give a "client" not only EXE-file, but everything else: the icon, and a subfolder (calling it just "res" - or where you are "point by finger" for program, where to take the graphics), and copying into it all you want graphic files... In general, we must create for a program at run-time the same "workspace" that was when it compiles... Either... Either "to bury" them somehow in himself EXE-file - and then (although its size will increase) all needs will be "in one bottle"... This method is called "to compile with resources". Let's see how it's done.

    Create a file named the same as that of PRG-file (ie in this case - PROG_12) and the expansion RC (ie, "resource") - namely, "PROG_12.RC". If the compiler finds such a file, it will know - from where and what to take... And what a name to each "resource" (drawing, icon, sound) given by us - and used into the program to refer to this resource... Let's write the resource file (I took a sample of such a file from the folder "MiniGUI\SAMPLES\Advanced\BinaryWatch"; lines starting with "//" - comments, as well as in programs, writing them is not necessary - but read them in this sample to represent "what is what here").

    --------------------- start of PROG_12.RC -------------
    // Using a resource file like this, you can include bitmaps, icons and sounds
    // inside EXE.
    //
    //     Resource name    Resource Type    Resource FileName
    //
    // (next 2 lines - for example)
    //
    //      WATCH               ICON            watch.ico
    //      SOUNDTICK           WAVE            tick.wav
    //
    // But this is our resources (for PROG_12.PRG)
    
     MY_ICON        ICON          icon_cas.ico
     RIS_OK         BITMAP        res\edit_ok.bmp
    
    --------------------- finish of PROG_12.RC --------------

    So - we wrote it (by the way, the number of spaces between the columns doesn't matter - just one! We trims columns simply for "readability"), saved file...

    Now correct a little our PRG-file in a couple of places (watch for changes!). But we will not overwrite already written lines - the "old" pieces will "comment out", and edit - their copies that are inserted below the "old" (it will be easier to compare changes, and - if anything - to rollback to a previous version, commenting out the "new" and removing the comments from the "old").

    Let's copy the definition of the window (lines starting with DEFINE WINDOW and ending FONT "Times New Roman" SIZE 11) and insert this piece after the "old" (in an empty string). Then put on the line above the "old" piece of the combination of symbols "/*" (without the quotes! top of multi-line comment) and on the line below the "old" piece - "*/" (again without the quotes! bottom of a multiline comment). Теперь все строки между "/*" и "*/" компилятором замечаться не будут (он будет считать их одним большим комментарием - см., к примеру, на такой длинный комментарий в описании назначения функции "Fun_1"). Должно это - и старый, и новый кусок - выглядеть так: Now compiler will not be noticed all lines between "/*" and "*/" (it would take their one big commentary - see, for example, in such a long comment to describe the purpose of the function "Fun_1"). All of it - the old and a new piece - should looks like this:
     /*
      DEFINE WINDOW Win_1 ;
        AT 0,0 ;
        WIDTH 400 ;
        HEIGHT 200 ;
        TITLE "Cat + Rat = CatRat (Button + CheckBox = CheckButton)" ;
        MAIN ;
        NOMINIMIZE ;
        NOMAXIMIZE ;
        NOSIZE ;
        ON MOVE Fun_1() ;
        ICON "ICON_CAS.ICO" ;
        FONT "Times New Roman" SIZE 11
     */
    
      DEFINE WINDOW Win_1 ;
        AT 0,0 ;
        WIDTH 400 ;
        HEIGHT 200 ;
        TITLE "Cat + Rat = CatRat (Button + CheckBox = CheckButton)" ;
        MAIN ;
        NOMINIMIZE ;
        NOMAXIMIZE ;
        NOSIZE ;
        ON MOVE Fun_1() ;
        ICON "ICON_CAS.ICO" ;
        FONT "Times New Roman" SIZE 11
    
    Now let's write in "new" piece (change in bold):
      DEFINE WINDOW Win_1 ;
        AT 0,0 ;
        WIDTH 400 ;
        HEIGHT 200 ;
        TITLE "Cat + Rat = CatRat (Button + CheckBox = CheckButton)" ;
        MAIN ;
        NOMINIMIZE ;
        NOMAXIMIZE ;
        NOSIZE ;
        ON MOVE Fun_1() ;
        ICON "MY_ICON" ;
        FONT "Times New Roman" SIZE 11
    
    I.e. instead to refer to a file with an icon we pointed to the resource (to it's name [and what it wanted - and is writed!] resource file [in the 1st column] and then given [in the 3rd column] - where the file located at compile time, where compiler can get it and "bury" into EXE-file)... And it's very easy!

    Now let's change the definition of checkbutton CB_2. It should look like this:
     /*
        @ 70, 10 CHECKBUTTON CB_2 ;
                  PICTURE "res\edit_ok.BMP" ;
                  WIDTH 27 ;
                  HEIGHT 27 ;
                  VALUE .F. ;
                  ON CHANGE Fun_3() ;
                  TOOLTIP "Click to ENABLE"
     */
    
        @ 70, 10 CHECKBUTTON CB_2 ;
                  PICTURE "RIS_OK" ;
                  WIDTH 27 ;
                  HEIGHT 27 ;
                  VALUE .F. ;
                  ON CHANGE Fun_3() ;
                  TOOLTIP "Click to ENABLE"
    
    Compiling... Now leave from the program that runs ("automatic") after compiling (it certainly will work as it should into it's "home" folder! but how it will be in "alien" folder?)... Copy file PROG_12.EXE (only!) to "temporary" folder ("give or send to a friend") and try to run... Всё работает, всё на месте! Размер EXE-файла увеличился всего-то на сумму длин файлов ресурсов (в данном случае - на какие-то жалкие три килобайта ! при общем его размере "мегабайт с копейками"), а польза - большая (не опрофунились перед другом из Старой Зеландии). :) Everything works, everything is in place! Size of EXE-file has increased to a total sum of the lengths of resource files (in this case - in some miserable three kilobytes! for a total it's amount - "little greater than one megabyte"), and the benefits - a large (not confused with your "great gift to friend from Old Zealand"). :)

    Well, that's another useful things along the way we learned. Next we will discuss... in general, will next calendar.

MiniGUI - © 2003-2008 Roberto Lopez
Extended version - MiniGUI Team (Grigory Filatov, Jacek Kubica, Janusz Pora)
Edited by Janusz Pora
© Alex L. Gustow. Translation and reformatting. 2009