Simple non-modal custom dialog box closing automatically

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Simple non-modal custom dialog box closing automatically

Post by infratec »

I added Veras idea to my version above.
No need for main event loop modifications.

@RASHAD
since I lost already most of my hair (maybe I'm scratching to often) I have no need for a scalp doctor.
:mrgreen:
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Simple non-modal custom dialog box closing automatically

Post by Vera »

Thanks Bernd for Binding them :wink:

It'll soon dawn over the ocean wide ~ and I hope it'll be a happy awakening when Blue drops in :)
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4954
Joined: Sun Apr 12, 2009 6:27 am

Re: Simple non-modal custom dialog box closing automatically

Post by RASHAD »

Hi Blue
It is a weird approach some how (Using your code as it is)
If you like it complete it

Code: Select all

;- ***** ***** ***** ***** ***** ***** *****
;- Blue - September 2013
;- --- --- --- --- --- --- --- --- --- ---
;- Child Window that closes 
;- ... when click detected outside of it
;- ***** ***** ***** ***** ***** ***** *****
; the child (technically correct : secondary) window has its own event loop

EnableExplicit
;{ constants
;- constants
;- .... for convenience
#enAttente = " Just waiting..."

#text = #PB_Gadget_FrontColor
#back = #PB_Gadget_BackColor

#colourF = $F4F7F9  ; child window background
#colourB = $FFFFFF  ; text box background 
#colourT = $FF0000  ; text
#colourL = $0779F8  ; labels

Enumeration   ; fenêtres
  #main_WINDOW
  #child_WINDOW
EndEnumeration

;- .... for gadgets
Enumeration
  #cmdOK
  #cmdANNULER
  #infoLABEL
  #infoBOX
  
  #F2_cmdOK
  #F2_cmd2
  #F2_cmd3

  #F2_cadre1
  #F2_cadre2
  #F2_cadreLABEL
  
  #Cont_1
EndEnumeration
;}

;- --- --- --- --- --- --- --- --- --- --- --- ---
Procedure Child_Window()
  Define winH = 120
  Define winW = WindowWidth(#main_WINDOW) * 0.66
  DisableGadget(#Cont_1,1)
  If 0 = OpenWindow(#child_WINDOW, 0, 0, winW, winH, "Busy Body", 
                    #PB_Window_BorderLess | #PB_Window_WindowCentered, 
                    WindowID(#main_WINDOW))
    
    SetGadgetText(#infoBox, " Child window could not be created")
    ProcedureReturn 
  EndIf
  SetWindowColor(#child_WINDOW,#colourF)
  SetGadgetText(#infoBox, " Click anywhere outside the child window to close it. ")

  ;{ gadgets hiding here
  ;- .... gadgets
  Define gadget, gX,gY, gW,gH
  
  gX = 1
  gY = 1
  gW = winW - (gX * 2)
  gH = winH - (gY * 2)
  FrameGadget(#F2_cadre1, gX, gY, gW,gH, "", #PB_Frame_Flat)
  
  gX + 8
  gY + 8
  gW = winW - (gX * 2)
  gH = winH - (gY * 2)
  FrameGadget(#F2_cadre2, gX,gY, gW,gH, "")

  gadget = #F2_cadreLABEL
    gX + 8
    gW = 80
    gH = 18
    TextGadget(gadget, gX,gY, gW,gH, "Child window",#PB_Text_Center)
    SetGadgetColor(gadget, #back,#colourF)
    SetGadgetColor(gadget, #text, #colourL)

  #dX = 12
  gW = 100
  gX + #dX
  gY + 24
  gH = 25
  ButtonGadget(#F2_cmd2, gX,gY, gW,gH, "Something")

  gX + gW + #dX
  ButtonGadget(#F2_cmd3, gX,gY, gW,gH, "Something else")

  gW = 70
  gH = 32
  gX = GadgetWidth(#F2_cadre2) - gW
  gY = GadgetHeight(#F2_cadre2) - gH
  ButtonGadget(#F2_cmdOK, gX,gY, gW,gH, "OK", #PB_Button_Default)
  ;}

  ;- .... event loop
  Define event, gadget, window
  Repeat
    event = WaitWindowEvent()
    window = EventWindow()

    If window <> #child_WINDOW           ;- ...  [109] >> catching outside events
    ; events occuring OUTSIDE our dialog box are intercepted here, 
    ; therefore preempting actions tied to gadgets in the main window
      SetActiveWindow(#child_WINDOW)
      Select event
        ;Case #PB_Event_  : Break                      ; uncomment to have the main close button close the child window
        Case #PB_Event_LeftClick,#PB_Event_RightClick
              If WindowMouseX(#main_WINDOW) >= GadgetX(#Cont_1) And WindowMouseX(#main_WINDOW) <= GadgetX(#Cont_1) + gW And WindowMouseY(#main_WINDOW) >= GadgetY(#Cont_1) And WindowMouseY(#main_WINDOW) <= GadgetY(#Cont_1) + gH
                  DisableGadget(#Cont_1,0)
                  Debug "Hi Blue"
                  Break
              Else
                DisableGadget(#Cont_1,0)
                Break
              EndIf                ; mouse clicked : the loop's work is done. (Windows OS only)
       ;Case #PB_Event_LeftClick, #PB_Event_RightClick : Break   ; cross-platform : comment above line (won't catch clicks on buttons)
      EndSelect
     ;Debug "event = "+ Str(event) + "  gadget = "+ Str(gadget) 
      Continue     ; disregard everything
    EndIf

    Select event
      Case #PB_Event_Gadget
        gadget = EventGadget()
        Select gadget
          Case #F2_cmd2 : SetGadgetText(#infoBox,"The child window is busy doing something...")
          Case #F2_cmd3 : SetGadgetText(#infoBox,"The child window is doing something else...")
          Case #F2_cmdOK : Break
          Default        : SetGadgetText(#infoBox,"gadget #" + Str(gadget))
        EndSelect
    EndSelect
  ForEver

  CloseWindow(#child_WINDOW)
  SetGadgetText(#infoBox, #enAttente)
EndProcedure  ; _Child_Window_

Procedure Main_Window()
  Define winH = 280
  Define winW = 400
  If 0 = OpenWindow(#main_WINDOW, 0, 0, winW, winH, "Main Window", 
                    #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    End
  EndIf

  ;- .... gadgets
  Define gadget, gX,gY, gW,gH
  #marge = 10

  gX = #marge
  gY = #marge
  gW = 130
  gH = 25
  ButtonGadget(#cmdOK, gX,gY, gW,gH, "Open child window", #PB_Button_Default)

  gH = 36
  gW = 90
  gX = winW - gW - #marge
  gY = winH - gH - #marge
  ContainerGadget(#Cont_1,gX,gY, gW,gH)
    ButtonGadget(#cmdANNULER, 0,0, gW,gH, "Quit")
  CloseGadgetList()  

  gadget = #infoLABEL
    gX = #marge
    gW = 90
    gH = 22
    gY - gH 
    TextGadget(gadget, gX,gY, gW,gH, "INFORMATION :")
    SetGadgetColor(gadget, #text,#colourL)

  gadget = #infoBOX
    gX + gW
    gW = winW - gX - #marge
    gH = 24
    gY - 5
    TextGadget(gadget, gX,gY, gW,gH, "",#PB_Text_Border)
    SetGadgetColor(gadget, #back, #colourB)
    SetGadgetColor(gadget, #text, #colourT)
EndProcedure  ; _Main_Window_

;- --- --- --- --- --- --- --- --- --- --- --- ---

;- main event loop
Define event, evType, gadget
Define fenetre

Main_Window()
SetGadgetText(#infoBox,#enAttente)

Repeat
  event = WaitWindowEvent()
  
  Select event
    Case #PB_Event_CloseWindow : Break
    Case #PB_Event_Gadget :
      gadget = EventGadget()
      Select gadget
        Case #cmdANNULER  : Break
        Case #cmdOK      : Child_Window()
      EndSelect
  EndSelect
  
ForEver

End

Egypt my love
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Simple non-modal custom dialog box closing automatically

Post by Blue »

Hello Vera.
Vera wrote:Maybe let time decide ? [...]
What a very ingenious idea and piece of code. :shock:
Nice demonstration of the power of thinking outside the box : :idea: Approaching the problem from that angle certainly had nothing obvious... :!:

Plus, it does offer a cross-platform solution since, as much as i can tell, it uses only PureBasic native constructs and commands.

Thank you, Vera.

I'm borrowing your idea + code to plug a piece of software of mine that's been giving me a hard time for much too long now.
If it does the job as i expect it will, i'm taking you out for a beer. Or a glass of water. (Your choice.)
Lucky you ! :D


PS: I'm not sure why, but i no longer receive eMail notification from the forum when a topic i follow gets updated. Anyone else having this kind of trouble ?
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Simple non-modal custom dialog box closing automatically

Post by Blue »

Hello Rashad.
You wrote:@Bernd
@Vera
You are dead meat :mrgreen:
Just wait for Blue
I don't know what colour glasses you wear when reading me, but whatever they are, they require an adjustment : the shade of Blue they reflect back to you is definitely off. :cry:
Please correct. 8)


About your weird approach (your words, not mine!)
Very nice touch, the "Hi Blue" debug output. :shock: (Talk about personalized software...) :oops:
Interesting twist, Rashad !
Great minds always converging, as you know, i want you to know that i also now check with WindowMouseX(#mainWIndow) if an event occurs outside the boundaries of the child window.
Simply this : an X-coordinate of -1 ==> the event is OUTSIDE the child window.

Anyhow, the point is moot.
I'm exploring the approach suggested by Vera.
It looks simpler and more promising than anything i've tried so far.
Last edited by Blue on Mon Sep 28, 2015 9:23 pm, edited 1 time in total.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Simple non-modal custom dialog box closing automatically

Post by Blue »

Vera wrote:It'll soon dawn over the ocean wide ~ and I hope it'll be a happy awakening when Blue drops in :)
Cute !
I assume you meant to write "It'll soon be dawn across the ocean"
Not to correct you. Only to let you know how i understood it.
Correct me if i'm wrong.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Simple non-modal custom dialog box closing automatically

Post by Vera »

Hi Blue,

I'm really happy I could be of help, but I can't take the credit for the neat trick, only for remembering that it's there and adjusting it to your aim :D

... however, I do would like to go out for a dark beer Image and many laughs

... and let it dawn on us :mrgreen:

Image ps: your assumption is brilliant
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Simple non-modal custom dialog box closing automatically

Post by Vera »

Blue wrote:PS: I'm not sure why, but i no longer receive eMail notification from the forum when a topic i follow gets updated. Anyone else having this kind of trouble ?
I've heard about issues alike, by Danilo (no pm-notification) and oldefoxx (no thread-notification) but no known reason for it so far.
I think it can have to do with the different URLs one can use to login (assuming it might occur in connection with the old ones), but I wouldn't know how to backtrace it.
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Simple non-modal custom dialog box closing automatically

Post by Blue »

Well, Vera,
You wrote:... however, I do would like to go out for a dark beer Image and many laughs
I'll just keep dutifully collecting my Air Miles points and... who knows ?
But dark beer ? :shock: Really ? Is that as good for my complexion as my regular Grolsch breakfast beer ? :?:
You also wrote:[...] ps: your assumption is brilliant
Thank you for the confirmation.
Because knowing that you are brilliant is OK, of course, but being told that you are brilliant is so much better. :lol:

RE: your code (or someone else's that you remembered)
It's clever, but, in its current incarnation, it fails on one point :
it does not, as it closes the CHILD window, block actions associated with gadgets in the MAIN window.

Both points matter equally in this topic: closing a child window...
(1) by simply clicking ANYWHERE outside of it (including, possibly, on gadgets within the MAIN window) (your code : OK)
(2) NOT launching any action by a click on a main window gadget.(your code : not OK)
Last edited by Blue on Mon Sep 28, 2015 10:51 pm, edited 1 time in total.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Simple non-modal custom dialog box closing automatically

Post by Blue »

RE : not receiving notifications

Definitely a server problem, and NOT a settings problem : i just received, in a 30-seconds burst, about 20 PB notifications for the period beginning Sunday a week ago !!!

Yes, computers definitely improve communications and save time !
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Simple non-modal custom dialog box closing automatically

Post by Blue »

Hello Bernd
Gee, for the first time I notice that Bernd and Infratec are one and the same person.
I had in mind that Bernd was from Germany, and Infratec from Texas. :oops:
infratec wrote:I added Veras idea to my version above.
No need for main event loop modifications.
I must have been asleep at the switch, 'cause I don't recall what your approach was.
Now what I see is that your contribution is a streamlined version of Vera's idea, (quite a nice streamlining operation too, BTW! :idea: ), but it appears before Vera's...

So, call me confused. :?
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
infratec
Always Here
Always Here
Posts: 7620
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Simple non-modal custom dialog box closing automatically

Post by infratec »

Hi,

infratec was the company name were I worked when I joined the forum.
Bernd is my 'real' name. :wink:

My version was before Veras, but without implementig a timeout.
When I saw Veras version with timeout, I added this to my version.
To give you both possibilities.

The general point is:
in my version you don't need to handle the event stuff in your main loop.
You simply have nothing todo with it.

Bernd
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Simple non-modal custom dialog box closing automatically

Post by Blue »

infratec wrote:[...]
The general point is:
in my version you don't need to handle the event stuff in your main loop.
You simply have nothing todo with it.
Yes, of course, i noticed that immediately... after studying the code for a long time ! :oops:
It makes for a very clean coding style.
And i'm glad you shared it, because i know for sure that i would never (not in a hundred years !) have come up with such a clean process. :!: :!: :!:
It's a very "Ah!... wow!" learning opportunity. Thanks for that.

Edit 1: Using the deActivate event to cancel actions tied to the main window's gadgets is a brilliant solution : it makes things so much simpler.
However, I still have a hard time with how you bind the About() Procedure to 2 separate events, one right after the other. I see that it works, but i don't understand why. If you can find the time and patience to explain a bit, i'd appreciate.
Edit 2: Never mind the last line above. I fully get it now. And this little gem of unobtrusive and flexible code works perfectly, timed or untimed.
Three Hoorays for the Bernd Closing Method (patent pending 2015), which appears to be 100% multi-platform.
If I were Vera, I'd append a rowdy bunch of jumping Rhinemaidens here, but I don't possess her magic powers.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: Simple non-modal custom dialog box closing automatically

Post by Vera »

oH² Confused,
this is how I do it ... Image...

Image
Image

ps: still too tired to code
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Re: Simple non-modal custom dialog box closing automatically

Post by Blue »

:lol: :lol: :lol:
You crack me up.
Laughter is definitely the best medicine.

I was going to talk code, but i just zoomed in 3000 times and read your message about being too tired to code (hard morning in Essen ? :cry: ).
So i'll give you a

Code: Select all

Break
for now... 8)
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
Post Reply