Close Window

Just starting out? Need help? Post your questions and find answers here.
IdeasVacuum
Posts: 143
Joined: Tue Feb 25, 2014 1:27 pm

Close Window

Post by IdeasVacuum »

From the Examples, #PB_Event_CloseWindow does not seem to be received? With or without the debugger on (whose window does close), I can't close the window.

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - CanvasGadget example file
;
;    (c) 2010 - Fantaisie Software
;
; ------------------------------------------------------------
;

Enumeration 10
  #IMAGE_Content  ; stores the previous CanvasGadget content while the mouse is down
  #IMAGE_Color
EndEnumeration

Enumeration
  #GADGET_Canvas
  #GADGET_Color 
  #GADGET_Brush
  #GADGET_Line
  #GADGET_Box
  #GADGET_Circle
  #GADGET_Clear 
EndEnumeration

Global CurrentColor, CurrentMode, StartX, StartY

; Draw the mouse action result depending on the currently selected mode and event type
;
Procedure DrawAction(x, y, EventType)

  If StartDrawing(CanvasOutput(#GADGET_Canvas))
    Select CurrentMode
    
      Case #GADGET_Brush
        If EventType = #PB_EventType_LeftButtonDown Or EventType = #PB_EventType_MouseMove
          Circle(x, y, 5, CurrentColor)
        EndIf
        
      Case #GADGET_Line
        DrawImage(ImageID(#IMAGE_Content), 0, 0)
        LineXY(StartX, StartY, x, y, CurrentColor)
      
      Case #GADGET_Box
        DrawImage(ImageID(#IMAGE_Content), 0, 0)
        Box(StartX, StartY, x-StartX, y-StartY, CurrentColor)
        
      Case #GADGET_Circle
        DrawImage(ImageID(#IMAGE_Content), 0, 0)
        
        If x > StartX
          rx = x - StartX
        Else
          rx = StartX - x
        EndIf
        
        If y > StartY
          ry = y - StartY
        Else
          ry = StartY - y
        EndIf
        
        If GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_Modifiers) & #PB_Canvas_Control
          ry = rx
        EndIf
        
        Ellipse(StartX, StartY, rx, ry, CurrentColor)
      
    EndSelect
    
    StopDrawing()
  EndIf

EndProcedure

CurrentColor = $0000FF
CurrentMode  = #GADGET_Brush
CreateImage(#IMAGE_Color, 35, 35, 24)
CreateImage(#IMAGE_Content, 380, 380, 24)

Procedure GadgetEvent()

  Select EventGadget()
    Case #GADGET_Canvas
      X = GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_MouseX)
      Y = GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_MouseY)
      Type = EventType()
    
      Select EventType()
      
        Case #PB_EventType_LeftButtonDown
          ;
          ; This stores the current content of the CanvasGadget in #IMAGE_Content,
          ; so it can be re-drawn while the mouse moves
          ;
          If StartDrawing(ImageOutput(#IMAGE_Content))
            DrawImage(GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_Image), 0, 0)
            StopDrawing()
          EndIf
          
          StartX = X
          StartY = Y
          DrawAction(X, Y, EventType())
        
        Case #PB_EventType_LeftButtonUp
          DrawAction(X, Y, EventType())                        
        
        Case #PB_EventType_MouseMove
          If GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton
            DrawAction(X, Y, EventType())            
          EndIf
                  
      EndSelect
    
    Case #GADGET_Color
      CurrentColor = RGB(Random(255), Random(255), Random(255))
      If StartDrawing(ImageOutput(#IMAGE_Color))
        Box(0, 0, 35, 35, CurrentColor)
        StopDrawing()
        SetGadgetAttribute(#GADGET_Color, #PB_Button_Image, ImageID(#IMAGE_Color))
      EndIf
      
    Case #GADGET_Brush, #GADGET_Line, #GADGET_Box, #GADGET_Circle
      EventGadget = EventGadget()
      For Gadget = #GADGET_Brush To #GADGET_Circle
        If Gadget = EventGadget
          SetGadgetState(Gadget, 1) 
        Else
          SetGadgetState(Gadget, 0) ; unset the state of all other gadgets
        EndIf
      Next Gadget          
      CurrentMode = EventGadget             
  
    Case #GADGET_Clear
      If StartDrawing(CanvasOutput(#GADGET_Canvas))
        Box(0, 0, 380, 380, $FFFFFF)
        StopDrawing()
      EndIf
            
  EndSelect
  
EndProcedure


If OpenWindow(0, 0, 0, 520, 400, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(#GADGET_Canvas, 10, 10, 380, 380, #PB_Canvas_ClipMouse | #PB_Canvas_Border)
  
  If StartDrawing(ImageOutput(#IMAGE_Color))
    Box(0, 0, 35, 35, CurrentColor)
    StopDrawing()
  EndIf

  ButtonImageGadget(#GADGET_Color, 400, 10, 50, 50, ImageID(#IMAGE_Color))
  
  ButtonGadget(#GADGET_Brush,  400, 100, 70, 25, "Brush",  #PB_Button_Toggle)
  ButtonGadget(#GADGET_Line,   400, 130, 70, 25, "Line",   #PB_Button_Toggle)
  ButtonGadget(#GADGET_Box,    400, 160, 70, 25, "Box",    #PB_Button_Toggle)
  ButtonGadget(#GADGET_Circle, 400, 190, 70, 25, "Circle", #PB_Button_Toggle)
    
  ButtonGadget(#GADGET_Clear,  400, 280, 70, 25, "Clear")
  
  SetGadgetState(#GADGET_Brush, 1)
  SetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_Cursor, #PB_Cursor_Cross)
  
  BindEvent(#PB_Event_Gadget, @GadgetEvent())
  
  CompilerIf #PB_Compiler_OS <> #PB_OS_Web
    Repeat 
           Event = WaitWindowEvent() 
         Until Event = #PB_Event_CloseWindow  ; If the user has pressed on the window close button
         CloseWindow(0) ; Added by me, still does not close
  CompilerEndIf

EndIf
IdeasVacuum
Posts: 143
Joined: Tue Feb 25, 2014 1:27 pm

Re: Close Window

Post by IdeasVacuum »

OK, if the WaitWindowEvent() is commented out and CloseWindow() bound to #PB_Event_CloseWindow, the Window can be closed:

Code: Select all

;
; ------------------------------------------------------------
;
;   PureBasic - CanvasGadget example file
;
;    (c) 2010 - Fantaisie Software
;
; ------------------------------------------------------------
;

Enumeration 10
  #IMAGE_Content  ; stores the previous CanvasGadget content while the mouse is down
  #IMAGE_Color
EndEnumeration

Enumeration
  #GADGET_Canvas
  #GADGET_Color 
  #GADGET_Brush
  #GADGET_Line
  #GADGET_Box
  #GADGET_Circle
  #GADGET_Clear 
EndEnumeration

Global CurrentColor, CurrentMode, StartX, StartY

Procedure CloseWindowEvent()
  CloseWindow(EventWindow())
EndProcedure

; Draw the mouse action result depending on the currently selected mode and event type
;
Procedure DrawAction(x, y, EventType)

  If StartDrawing(CanvasOutput(#GADGET_Canvas))
    Select CurrentMode
    
      Case #GADGET_Brush
        If EventType = #PB_EventType_LeftButtonDown Or EventType = #PB_EventType_MouseMove
          Circle(x, y, 5, CurrentColor)
        EndIf
        
      Case #GADGET_Line
        DrawImage(ImageID(#IMAGE_Content), 0, 0)
        LineXY(StartX, StartY, x, y, CurrentColor)
      
      Case #GADGET_Box
        DrawImage(ImageID(#IMAGE_Content), 0, 0)
        Box(StartX, StartY, x-StartX, y-StartY, CurrentColor)
        
      Case #GADGET_Circle
        DrawImage(ImageID(#IMAGE_Content), 0, 0)
        
        If x > StartX
          rx = x - StartX
        Else
          rx = StartX - x
        EndIf
        
        If y > StartY
          ry = y - StartY
        Else
          ry = StartY - y
        EndIf
        
        If GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_Modifiers) & #PB_Canvas_Control
          ry = rx
        EndIf
        
        Ellipse(StartX, StartY, rx, ry, CurrentColor)
      
    EndSelect
    
    StopDrawing()
  EndIf

EndProcedure

CurrentColor = $0000FF
CurrentMode  = #GADGET_Brush
CreateImage(#IMAGE_Color, 35, 35, 24)
CreateImage(#IMAGE_Content, 380, 380, 24)

Procedure GadgetEvent()

  Select EventGadget()
    Case #GADGET_Canvas
      X = GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_MouseX)
      Y = GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_MouseY)
      Type = EventType()
    
      Select EventType()
      
        Case #PB_EventType_LeftButtonDown
          ;
          ; This stores the current content of the CanvasGadget in #IMAGE_Content,
          ; so it can be re-drawn while the mouse moves
          ;
          If StartDrawing(ImageOutput(#IMAGE_Content))
            DrawImage(GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_Image), 0, 0)
            StopDrawing()
          EndIf
          
          StartX = X
          StartY = Y
          DrawAction(X, Y, EventType())
        
        Case #PB_EventType_LeftButtonUp
          DrawAction(X, Y, EventType())                        
        
        Case #PB_EventType_MouseMove
          If GetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton
            DrawAction(X, Y, EventType())            
          EndIf
                  
      EndSelect
    
    Case #GADGET_Color
      CurrentColor = RGB(Random(255), Random(255), Random(255))
      If StartDrawing(ImageOutput(#IMAGE_Color))
        Box(0, 0, 35, 35, CurrentColor)
        StopDrawing()
        SetGadgetAttribute(#GADGET_Color, #PB_Button_Image, ImageID(#IMAGE_Color))
      EndIf
      
    Case #GADGET_Brush, #GADGET_Line, #GADGET_Box, #GADGET_Circle
      EventGadget = EventGadget()
      For Gadget = #GADGET_Brush To #GADGET_Circle
        If Gadget = EventGadget
          SetGadgetState(Gadget, 1) 
        Else
          SetGadgetState(Gadget, 0) ; unset the state of all other gadgets
        EndIf
      Next Gadget          
      CurrentMode = EventGadget             
  
    Case #GADGET_Clear
      If StartDrawing(CanvasOutput(#GADGET_Canvas))
        Box(0, 0, 380, 380, $FFFFFF)
        StopDrawing()
      EndIf
            
  EndSelect
  
EndProcedure


If OpenWindow(0, 0, 0, 520, 400, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_Invisible)
  CanvasGadget(#GADGET_Canvas, 10, 10, 380, 380, #PB_Canvas_ClipMouse | #PB_Canvas_Border)
  
  If StartDrawing(ImageOutput(#IMAGE_Color))
    Box(0, 0, 35, 35, CurrentColor)
    StopDrawing()
  EndIf

  ButtonImageGadget(#GADGET_Color, 400, 10, 50, 50, ImageID(#IMAGE_Color))
  
  ButtonGadget(#GADGET_Brush,  400, 100, 70, 25, "Brush",  #PB_Button_Toggle)
  ButtonGadget(#GADGET_Line,   400, 130, 70, 25, "Line",   #PB_Button_Toggle)
  ButtonGadget(#GADGET_Box,    400, 160, 70, 25, "Box",    #PB_Button_Toggle)
  ButtonGadget(#GADGET_Circle, 400, 190, 70, 25, "Circle", #PB_Button_Toggle)
    
  ButtonGadget(#GADGET_Clear,  400, 280, 70, 25, "Clear")
  
  SetGadgetState(#GADGET_Brush, 1)
  SetGadgetAttribute(#GADGET_Canvas, #PB_Canvas_Cursor, #PB_Cursor_Cross)
  
  BindEvent(#PB_Event_CloseWindow, @CloseWindowEvent())
  BindEvent(#PB_Event_Gadget, @GadgetEvent())
  
  ;CompilerIf #PB_Compiler_OS <> #PB_OS_Web
    ;Repeat 
           ;Event = WaitWindowEvent() 
         ;Until Event = #PB_Event_CloseWindow  ; If the user has pressed on the window close button
         ;CloseWindow(0) ; Added by me, still does not close
  ;CompilerEndIf

EndIf
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: Close Window

Post by Peter »

IdeasVacuum wrote:From the Examples, #PB_Event_CloseWindow does not seem to be received? With or without the debugger on (whose window does close), I can't close the window.
take a closer look at the Compiler Directive around your CloseWindow() ;-)

Greetings ... Peter
IdeasVacuum
Posts: 143
Joined: Tue Feb 25, 2014 1:27 pm

Re: Close Window

Post by IdeasVacuum »

Well, the point with a Help example is that it should be working without requiring edit......... but the Spider Basic help is a bit if a mess at the moment, largely an edited version of the Pure Basic equivalent with new errors thrown in :D

I wanted to read about in-line JavaScript this morning - the help on that is actually about ASM :o
Post Reply