How to Trap Close (X) Button Event in a Console Application Using GTWVT?

774 views
Skip to first unread message

SD

unread,
May 31, 2013, 4:47:37 AM5/31/13
to harbou...@googlegroups.com
From: Swapan Das

ISSUE: When a user clicks the top-right CLOSE (red X) button, the application terminates/closes instantly.(These windows Form buttons Max/Restore/Close appears under GT)

CONSEQUENCE: The application fails to trap this close event, and goes for an abnormal exit from the application. This Close event needed to be trapped so  that a msg box could be invoked with "Exit Y/n?", and do the necessary housekeeping (close all files etc.) before quitting from the application. I mean user can do accidentally click this X button while there's some "unfinished" entry. The application should receive a "trigger" always to quit safely and gracefully............

ATTACHMENT: Please find the attached image files depicting this scenario.
 
EXPERIMENTED WITH:
hb_gtInfo( HB_GTI_CLOSABLE, .T. ) // activate close button - default
hb_gtInfo( HB_GTI_CLOSABLE, .F. ) // deactivate close button
hb_gtInfo( HB_GTI_ALTENTER, .T. )  // activate alt-enter for full screen
HB_K_CLOSE

But things were not coming to my need.....

Finally, come up with the following solution. May not be the best way but its trapping the CLOSE EVENT everytime and from any level in the application when the user is clicking the X button. Was unable to trap the close event in the main do while loop like shown in WVTEXT.PRG, so needed to switch to set key hb_k_close to CloseApp (closeapp=udf).

This is working, but instead of using SET KEY, this can be accomplished in other and better way..........

SOLUTION:

compiled using myapp.hbp 

myapp.hbp:
...
...
...
-
-m -n
-gui
-gtwvt

*---- myapp.prg
Function Main()
....
......
#include "hbgtinfo.ch" && this is what you need only for this issue
#include "Inkey.ch"
#include "Box.ch"
#include "Commands.ch"
#include "DIRECTRY.CH"
#include "Combo.ch"
#include "setcurs.ch"
#include "common.ch"
.....
.......
SET KEY K_ALTC TO AltC && took idea from my this old function to trap Alt-C
SET KEY HB_K_CLOSE TO CloseApp && CloseApp=UDF
SETCANCEL(.F.)
nEvent := INKEY_ALL + HB_INKEY_GTEVENT
Set( _SET_EVENTMASK, nEvent )
 * --- main menu:
Do While .t.
      *----- pull-down menu system routine with arrays and calling certain routines..
      * like:
      aVert1 := { ;
                  {" Main Setup       ", {|| zempty()  } } ;
                }

      * ----------- 2) FGS MENU:  
      aVert5a:= { ;
                  {"..                 ", {|| zempty()  } } ;
                }


      * ---------- MAIN HEADS : Horizontal Menu 
      //          {" Miscellaneous " ,aVert32}, ; 
      aHoriz := { ;
                  {" Setup "    ,aVert36}, ;
                  {" RMS "        ,aVert4}, ;
                  {" FGS "        ,aVert5}, ;
                  {" Entry  "        ,aVert2}, ;
                  {" Reports  "      ,aVert8}, ;
                  {" Calculations " ,aVert38}, ;
                  {" Quit ",aVert37} ; 
                }
      SET WRAP ON
      * ------- WAS UNABLE TO TRAP CLOSE EVENT IN THIS LOOP HERE like shown in wvtext.prg
      * ------ SO NEEDED TO SWITCH TO SET KEY HB_K_CLOSE TO CloseApp
      If LastKey()=27
         Private nMANS:=1
         Band2()
         nMANS=ASKYN2([ Exit To OS ? ],18*nRowDiff,30*nColDiff)
         if nMANS=1
            setmode(25,80)  
            set colo to
            setcolor("gr+/b")
            set curs on
            //clear
            set colo to
            //quit
            Out2sys()
         Else
            loop
         Endi
      Endi && If LastKey()=27
Enddo
* ----- main menu do while end:

Function CloseApp
    IF Alert( "Close Application", { "Yes", "No" } ) == 1
       CLOSE ALL
       CLEA ALL
       OUT2SYS() && my prv. program to clean things..and exit to OS (operating sys)
    ENDIF
Return NIL
App_Close1.jpg
App_Close2.jpg

SD

unread,
May 31, 2013, 4:57:33 AM5/31/13
to harbou...@googlegroups.com
Forgot to mention I'm still using version HARBOUR 3.0................, and my test is with respect to this version only.

elch

unread,
Jun 1, 2013, 7:49:46 AM6/1/13
to harbou...@googlegroups.com

Hi Swapan,


GTWVT is no console application - that is a real (graphic) window.

What you described works also fine in GTXWC (Linux),

! but NOT in GTWVG or brand new GTQTC !


--

i do not really understand - a question ?


a) you must disable the window close button with:

hb_GTInfo( HB_GTI_CLOSABLE, .F. )

else the application window will close by click on this


b) after disable close button, you will get a HB_K_CLOSE inkey, when user clicks on that button.

Therefore you correctly set the EventMask:

Set( _SET_EVENTMASK, INKEY_ALL + HB_INKEY_GTEVENT )


[ just a TIP: subtract ( - ) INKEY_MOVE, to get rid of all these mouse move inkeys ( HB_K_MOVE ) ]


c) SETKEY() works in READ, MENU TO, ACHOICE etc ..

in other cases you need a loop (i.e.: do while) to check for that special inkey.



Conclusion:

your menu ' eat ' the HB_K_CLOSE, will not exit as it seem to do after a K_ESC.

My interest: what functions are you calling for your menu ?



Regards

Rolf

SD

unread,
Jun 1, 2013, 4:08:04 PM6/1/13
to harbou...@googlegroups.com
Dear Rolf:

On Saturday, 1 June 2013 17:19:46 UTC+5:30, elch wrote:

Hi Swapan,


Thanks a lot for attending this thread....

 


GTWVT is no console application - that is a real (graphic) window.

I considered it as a console only, and HMG (the only gui which I've tested a bit) as a real gui...
BTW, Does GTWVT carries  "command button" and other windows things?! If yes, I'm not aware of it and that would be just great. Moreover for my this 1st harbour app. wanted to keep the console commands only and not move to "complete gui" for easy maintenance...

What you described works also fine in GTXWC (Linux),

! but NOT in GTWVG or brand new GTQTC !

I assume trapping of "close" button event would be required in any platform, and GTWVG or GTQTC definitely be having it. May my crude method won't work there.
 --

i do not really understand - a question ?

 Simply put the Q. would be:How to Trap Close (X) Button Event under GTWVT? 


a) you must disable the window close button with:

hb_GTInfo( HB_GTI_CLOSABLE, .F. )

else the application window will close by click on this


Sorry! My fault!! I missed this line from my snippet of code. Actually I discarded all my actual house keeping commands,as wanted to show the "concerned" code only, but in doing so the line which is very much required got removed. Thanks for mentioning this vital point. Yes the following code is very much required:
hb_GTInfo( HB_GTI_CLOSABLE, .F. )



b) after disable close button, you will get a HB_K_CLOSE inkey, when user clicks on that button.

Therefore you correctly set the EventMask:

Set( _SET_EVENTMASK, INKEY_ALL + HB_INKEY_GTEVENT )


c) SETKEY() works in READ, MENU TO, ACHOICE etc ..

in other cases you need a loop (i.e.: do while) to check for that special inkey.

Conclusion:

your menu ' eat ' the HB_K_CLOSE, will not exit as it seem to do after a K_ESC.


Perhaps later on was "able" to work it under the loop also without using SET KEY...but definitely had some issues/glitches, so removed it and kept finally SET KEY for now.

My interest: what functions are you calling for your menu ?

The functions/procedures are calling simple regular prg routines/modules as per the menu selection. This is a very small fragment of the actual menu system (as it was not needed here). Were you asking on the same line or wanted to know something else...am not clear about it..

Regards,
Swapan
India

elch

unread,
Jun 2, 2013, 1:14:51 PM6/2/13
to harbou...@googlegroups.com
Hi again, Swapan


GTWVT is no console application - that is a real (graphic) window.

I considered it as a console only, and HMG (the only gui which I've tested a bit) as a real gui...
BTW, Does GTWVT carries  "command button" and other windows things?! If yes, I'm not aware of it and that would be just great. Moreover for my this 1st harbour app. wanted to keep the console commands only and not move to "complete gui" for easy maintenance...

you're right!, only a graphic window makes no GUI.

A big difference of GUI versus CUI/ TUI is the concept of input management: complex event & slot driven versus simple inkey queue.


( GTWVT is designed for use with text coordinates, but the char positions are internally calculated on pixels.

Further it have, explainable, problems with persistence of graphics - a picture to describe:

https://groups.google.com/d/msg/harbour-users/agqNvpLAwbg/jYEe3INwCngJ )


If i refer to 'console', i mean a pure only text capable window - which does not need a GUI desktop - often found in conjunction with a server only.

GTWVT can not, what you above asked - but it is a bit more than 'only console' and was once time the origin for GTWVG.

 

What you described works also fine in GTXWC (Linux),

! but NOT in GTWVG or brand new GTQTC !

I assume trapping of "close" button event would be required in any platform, and GTWVG or GTQTC definitely be having it. May my crude method won't work there.

both GTs theoretically can ... but

GTWVG generates a K_ESC ( if the close button is enabled ! ;-)

in GTQTC three lines must be outcommented ( i already posted, but it was refused ).

So these are NOT able in official release to trap HB_K_CLOSE ( only my personal versions does so .. )


 
Perhaps later on was "able" to work it under the loop also without using SET KEY...but definitely had some issues/glitches, so removed it and kept finally SET KEY for now.

for a fully control of HB_K_CLOSE you need BOTH types.

SETKEY() is needed for the case when application i.e. just stand in a getmask ( READ ).

The way with checking result of INKEY() [ or LASTKEY() ] is needed when i.e. working with a browse class and there subsequent checking user input.

 
Regards
Rolf
Reply all
Reply to author
Forward
0 new messages