"Unbreakable" Objects (Part III)

Version: 1.00.01 - last update: Saturday, September 26, 2009, 16:50:00

Previous ChapterNeat Solutions Home (TOC)Next Chapter


This is all about how to create a CLEAR ALL resistant class to speed-up things while developing.

Intro

In part two of this multi-part blog we started to implement our unbreakable Form. Let's resume...

The following Start() code shows some of the more important things you should get in there. BTW: it sounds to be a good idea to implement your Start() method using some Template Pattern Design!

This is our Form’s START method code:

WITH THIS
    *\\ move the form out of the visible desktop
    .Move(0,-1000)
    *//
    IF LEFT(.cStartMode,7) == "STARTUP"
        *\\ add some _SYSMENU items to show the form
        DEFINE PAD Resources OF _MSYSMENU ;
            PROMPT "Resources" ;
            COLOR SCHEME 3 ;
            KEY ALT+R, ""
        ON PAD Resources OF _MSYSMENU ;
            ACTIVATE POPUP resources
        DEFINE POPUP resources MARGIN ;
            RELATIVE SHADOW COLOR SCHEME 4
        DEFINE BAR 1 OF resources ;
            PROMPT "Show/Hide"
        DEFINE BAR 2 OF resources PROMPT "Exit"
        ON SELECTION BAR 1 OF ;
            resources _oResource.Show()
        ON SELECTION BAR 2 OF ;
            resources _oResource.Exit()
        *//
    ENDIF
ENDWITH

By now we got our Form up and running (and moved out of sight). VFP’s _SYSMENU shows two options under the new Resources-Pad. One to show/hide the Form, the other to shut it down! Let’s have a look on the methods that get called from the menu:

This is our Form’s SHOW method code. This is far away from being perfect!

LPARAMETERS nStyle
WITH THIS
    .lhidden = NOT .lhidden
    IF .lhidden
        .Move(0,-1000)
    ELSE
        .Move(0,0)
    ENDIF
ENDWITH

Our Form’s exit method is even simpler:

THIS.lAutoReStart = .F.
THIS.Release()

We set a Form property to flag that the user really wants to shut down our form. This flag property will be queried in our Form’s destroy (see below).

Finally here is our Form’s DESTROY event code:

IF THIS.lAutoRestart
    _SHELL = "DO Form (HOME()+'RESI.SCX') " + ;
       "NAME _oResource LINKED WITH 'RESTART' NOSHOW"
ELSE
    RELEASE PAD Resources OF _MSYSMENU
    RELEASE POPUPS resources EXTENDED
    RELEASE _oResource
ENDIF

As you can see, as long as our Form’s property “lAutoRestart” is TRUE, our Form will always reappear!

Good to know

  1. There are some other things to keep in mind when working with VFP forms that way: You should disable output (THISFORM.AllowOutput = .F.), otherwise it can happen to you that interactive output is echoed to your tool form, which may resides invisible outside the VFP’s desktop just at that moment!
  2. Keep in mind that issuing a THISFORM.RELEASE() completely bypasses your form’s QueryUnload() event! This is good to know if you intend to place some Exit-button on your form letting you shut down your tool instance. Putting a THISFORM.RELEASE() in the click event of such a button never will run any code that you put in your Form’s QueryUnload() event!
  3. Another thing to keep in mind is how VFP handles clicking a Form’s close box! As you know there is a Form property called ReleaseType. You do typically query the release type of a form in the QueryUnload() event method. 0 means “Variable released”, 1 means “Close Menu Command chosen or Close Box clicked” and 3 means “Exit VFP”. As you can see there is an ambiguity with value 1. Even more than that: issuing a CLEAR ALL will set the ReleaseType value of your Form to 1, too! The best thing to do is: just disable your tool Form’s close button (THISFORM.Closable = .F.) Now you can be sure that the value of 1 was set because of a CLEAR ALL.

Final thoughts

This short demo leaves enough room for improvements! There are pretty cool things you can do this way.

What’s about your own tools that behave just like all VFP’s system tools written in C++ like the project manager? I mean, if you’re hitting in CLEAR ALL, all of your class browser instances will be gone but not your project manager instances. Wouldn't it be nice to have some unbreakable tool forms behaving just the same:-)

This is pretty easy to do: You only have to capture (and save to file) your form position and state in your form’s QueryUnload event. Afterwards lock the screen and remember this using SYS(989,1). During your form’s INIT (or LOAD) open your temporary file and position your new form instance exactly over the old place again. Finally unlock the screen (if SYS(989) returns 1) and do not forger to reset this flag using SYS(989,0). Your new form instance looks like it never has been reloaded! – Cool gadgetry

IMHO the biggest return on investment you can have is when using this system to re-configure your IDE automatically. Imagine you have declared some pretty complex WIN API calls through VFP’s command window. While testing you are messing up your development environment. After a CLEAR ALL you have at least to scroll through your command history to re-issue all those declarations. Wouldn’t it be sweet some program could do that for you?

Keep going!

Okay, there still is something more :-)


Previous ChapterNeat Solutions Home (TOC)Next Chapter

No comments:

Post a Comment