message

Just starting out? Need help? Post your questions and find answers here.
mestnyi
Addict
Addict
Posts: 1103
Joined: Mon Nov 25, 2013 6:41 am

message

Post by mestnyi »

help with this code so that it works on all three OS

Code: Select all

;\\                                       ; win ; mac ; lin
; Debug #PB_MessageRequester_YesNoCancel  ; 3   ; 2
; Debug #PB_MessageRequester_YesNo        ; 4   ; 1
; Debug #PB_MessageRequester_Yes          ; 6   ; 6
; Debug #PB_MessageRequester_Error        ; 16  ; 8
; Debug #PB_MessageRequester_Warning      ; 48  ; 16
; Debug #PB_MessageRequester_Info         ; 64  ; 4
; 
; Debug ""
; Debug #PB_MessageRequester_Ok           ; 0   ; 0
; Debug #PB_MessageRequester_Cancel       ; 2   ; 2
; Debug #PB_MessageRequester_No           ; 7   ; 7
; 
; ;MessageRequester(


EnableExplicit
Declare CanvasEvents( )

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
   Procedure winCloseHandler(WindowID,message,wParam,lParam)
      Protected Text$
      Protected Result = #PB_ProcessPureBasicEvents
      
      Select message
         Case #WM_CLOSE : Text$ = "Close"
         Case #WM_DESTROY : Text$ = "Destroy"
         Case #WM_QUIT : Text$ = "Quit"
      EndSelect
      Debug "winCloseHandler "+message +" "+ Text$+" "+Str(wParam)+" "+Str(lParam)
      
      ProcedureReturn Result
   EndProcedure
   
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
   ProcedureC winCloseHandler(*Widget.GtkWidget, *Event.GdkEventAny, UserData.I)
      Debug "winCloseHandler"
      gtk_main_quit_( )
   EndProcedure
   
CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
   ProcedureC winCloseHandler(obj.i, sel.i, win.i) 
      Debug "winShouldClose"
      CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "stop:", win)
      ProcedureReturn #YES
   EndProcedure
   
CompilerEndIf

;-
Procedure WaitEvents( window )
   Protected win = WindowID( window )
   
   ;\\ start main loop
   CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      ;       ;\\
      ;       Protected NSObjectClass = objc_getClass_("NSObject")
      ;       Protected myWindowDelegateClass = objc_allocateClassPair_(NSObjectClass, "myWindowDelegate", 0)
      ;       class_addProtocol_(myWindowDelegateClass, objc_getProtocol_("NSWindowDelegate"))
      ;       class_addMethod_(myWindowDelegateClass, sel_registerName_("windowShouldClose:"), @winCloseHandler(), "c@:@")
      ;       Protected myWindowDelegate = class_createInstance_(myWindowDelegateClass, 0)
      ;       CocoaMessage(0, win, "setDelegate:", myWindowDelegate)
      
      ;\\
      CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "run")
      
   CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
      ;       ;\\ https://www.purebasic.fr/english/viewtopic.php?p=570200&hilit=move+items#p570200
      ;       g_signal_connect_(win, "delete-event", @winCloseHandler( ), 0)
      ;       g_signal_connect_(win, "destroy", @winCloseHandler( ), 0)
      
      ;\\
      gtk_main_( )
      
   CompilerElseIf #PB_Compiler_OS = #PB_OS_Windows
      Protected msg.MSG
      
      ; SetWindowCallback( @winCloseHandler( ), window )
      
      ; While PeekMessage_(@msg,0,0,0,1)
      While GetMessage_(@msg, #Null, 0, 0 )
         TranslateMessage_(msg) 
         DispatchMessage_(msg)
         
         ;          If msg\message = #WM_NCMOUSEMOVE
         ;             Debug "" + #PB_Compiler_Procedure + " " + msg\message + " " + msg\hwnd + " " + msg\lParam + " " + msg\wParam
         ;          EndIf
         ;          
         ;          ;   If msg\wParam = #WM_QUIT
         ;          ;     Debug "#WM_QUIT "
         ;          ;   EndIf
      Wend
      
   CompilerEndIf
EndProcedure

;-
Procedure MessageEvents( )
   Protected *ew = EventGadget( )
   Protected *message = EventWindow( )
   
   Select EventType( )
      Case #PB_EventType_LeftClick
         Select GetGadgetText( *ew )
            Case "No"     : SetWindowData( *message, #PB_MessageRequester_No )     ; no
            Case "Yes"    : SetWindowData( *message, #PB_MessageRequester_Yes )    ; yes
            Case "Cancel" : SetWindowData( *message, #PB_MessageRequester_Cancel ) ; cancel
         EndSelect
         
         ;\\
         UnbindGadgetEvent( *ew, @MessageEvents( ), #PB_EventType_LeftClick )
         
         ;\\ stop main loop
         CompilerSelect #PB_Compiler_OS 
            CompilerCase #PB_OS_Windows
               PostQuitMessage_( 0 )
            CompilerCase #PB_OS_Linux
               gtk_main_quit_( )
            CompilerCase #PB_OS_MacOS
               CocoaMessage( 0, CocoaMessage( 0, 0, "NSApplication sharedApplication" ), "stop:", 0 )
         CompilerEndSelect
         
   EndSelect
   
   ProcedureReturn #PB_Ignore
EndProcedure


; UseJPEGImageDecoder() 
; UseJPEG2000ImageDecoder() 
UsePNGImageDecoder() 
; UseTIFFImageDecoder() 
; UseTGAImageDecoder() 
; UseGIFImageDecoder()

Procedure Message( Title.s, Text.s, flag.q, *parent )
   Protected result
   Protected img = 0, f1 = - 1, f2 = 8, width = 400, height = 120
   Protected bw = 85, bh = 25, iw = height - bh - f1 - f2 * 4 - 2 - 1
   
   Protected x = 100
   Protected y = 100
   
   Protected *message = OpenWindow(#PB_Any, x, y, width, height, Title, #PB_Window_TitleBar|#PB_Window_WindowCentered, *parent)
   
   If Flag & #PB_MessageRequester_Info
   EndIf
   
   If Flag & #PB_MessageRequester_Error
   EndIf
   
   If Flag & #PB_MessageRequester_Warning
   EndIf
   
   ;\\
   ContainerGadget(#PB_Any, f1, f1, width - f1 * 2, height - bh - f1 - f2 * 2 - 1 )
   ImageGadget(#PB_Any, f2, f2, iw, iw, img, #PB_Image_Border ); | #__flag_center )
   TextGadget(#PB_Any, f2 + iw + f2, f2, width - iw, iw, Text) ;, #__flag_textcenter | #__flag_textleft )
   CloseGadgetList( )
   
   Protected *ok, *no, *cancel
   
   *ok = ButtonGadget(#PB_Any, width - bw - f2, height - bh - f2, bw, bh, "Ok");, #__button_Default )
   BindGadgetEvent( *ok, @MessageEvents( ), #PB_EventType_LeftClick )
   If Flag & #PB_MessageRequester_YesNo Or
      Flag & #PB_MessageRequester_YesNoCancel
      SetGadgetText( *ok, "Yes" )
      *no = ButtonGadget(#PB_Any, width - ( bw + f2 ) * 2 - f2, height - bh - f2, bw, bh, "No" )
      BindGadgetEvent( *no, @MessageEvents( ), #PB_EventType_LeftClick )
   EndIf
   
   If Flag & #PB_MessageRequester_YesNoCancel
      *cancel = ButtonGadget(#PB_Any, width - ( bw + f2 ) * 3 - f2 * 2, height - bh - f2, bw, bh, "Cancel" )
      BindGadgetEvent( *cancel, @MessageEvents( ), #PB_EventType_LeftClick )
   EndIf
   
   ;\\
   StickyWindow( *message, #True )
   
    ; macos bug no post event
   PostEvent( #PB_Event_Gadget, 0,0, #PB_EventType_FirstCustomValue )
   
   ;\\
   WaitEvents( *message )
   
   StickyWindow( *message, #False )
   result = GetWindowData( *message )
   
   ;\\ close
   CloseWindow( *message )
   
   ProcedureReturn result
EndProcedure


;-
Procedure ShowMessage( UseGadgetList )
   Debug "open - Title"
   Define Result = Message("Title", "Please make your input:", #PB_MessageRequester_YesNoCancel|#PB_MessageRequester_Info, UseGadgetList) 
   Debug " close - Title " + Result
   
   Define flag, a$ = "Result of the previously requester was: "
   
   If Result = #PB_MessageRequester_Yes       ; pressed Yes button
      flag = #PB_MessageRequester_Ok|#PB_MessageRequester_Info
      a$ +#LF$+ "Yes"
   ElseIf Result = #PB_MessageRequester_No    ; pressed No button
      flag = #PB_MessageRequester_YesNo|#PB_MessageRequester_Error
      a$ +#LF$+ "No"
   Else                                       ; pressed Cancel button or Esc
      flag = #PB_MessageRequester_YesNoCancel|#PB_MessageRequester_Warning
      a$ +#LF$+ "Cancel"
   EndIf
   
   Debug "open - Information"
   Result = Message("Information", a$, flag, UseGadgetList)
   Debug "close - Information "+Result
EndProcedure

Procedure CanvasEvents( )
   Protected x,y
   
   If EventType() = #PB_EventType_FirstCustomValue
      Debug "#PB_EventType_FirstCustomValue "
   EndIf
   
   
   If EventType() = #PB_EventType_LeftButtonDown Or 
      (EventType() = #PB_EventType_MouseMove And GetGadgetAttribute(0, #PB_Canvas_Buttons) & #PB_Canvas_LeftButton)
      
      If StartDrawing(CanvasOutput(0))
         x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
         y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
         Circle(x, y, 10, RGB(Random(255), Random(255), Random(255)))
         StopDrawing()
      EndIf
      
   EndIf
   
   If EventType() = #PB_EventType_RightButtonDown
      If StartDrawing(CanvasOutput(0))
         Box(0, 0, 200, 200, #White)
         StopDrawing()
      EndIf
   EndIf
EndProcedure

Procedure EventClick( )
   If EventType( ) = #PB_EventType_LeftClick
      ShowMessage( WindowID(0) )
   EndIf
EndProcedure

If OpenWindow( 0, 150, 150, 600, 300, "demo message", #PB_Window_SizeGadget | #PB_Window_SystemMenu )
   CanvasGadget(0,10,10,490, 250 )
   BindGadgetEvent( 0, @canvasevents() )
   
   Define *showButton = ButtonGadget(#PB_Any, 600-100, 300-40, 90,30,"show")
   BindGadgetEvent( *showButton, @EventClick() )
   
   ShowMessage( WindowID(0) )
   
   Define Event
   Repeat
      Event = WaitWindowEvent( )
      If Event = #PB_Event_Gadget
         If EventType( ) = #PB_EventType_LeftClick
            If EventGadget( ) = 0
               Debug "main loop canvas click event"
            EndIf
         EndIf
      EndIf
   Until Event = #PB_Event_CloseWindow
   
EndIf
User avatar
Kiffi
Addict
Addict
Posts: 1510
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: message

Post by Kiffi »

Please be so kind and choose meaningful subjects for your requests. I already had to adapt your last thread.
Hygge
mestnyi
Addict
Addict
Posts: 1103
Joined: Mon Nov 25, 2013 6:41 am

Re: message

Post by mestnyi »

Kiffi wrote: Tue Dec 05, 2023 11:28 pm Please be so kind and choose meaningful subjects for your requests. I already had to adapt your last thread.
How can I understand this?
BarryG
Addict
Addict
Posts: 4225
Joined: Thu Apr 18, 2019 8:17 am

Re: message

Post by BarryG »

It's because your topic title is just one word: "message". Means nothing to people browsing.
mestnyi
Addict
Addict
Posts: 1103
Joined: Mon Nov 25, 2013 6:41 am

Re: message

Post by mestnyi »

ok, what should I call it? Tell me, I’ll call it that
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: message

Post by spikey »

We've no idea what you are trying to accomplish. "I want to make this code work" doesn't help, that's true for everyone in some way or another. Give us some explanation of what you are doing and why.

I can see this code is heading in several different directions simultaneously but I can't see any sensible reasons why. On the surface PureBasic seems to be able to do everything you want via a normal WaitWindowEvent() loop as shown in the documentation with no need for OS specific or callback code:

Code: Select all

EnableExplicit

#WindowX = 150
#WindowY = 150
#WindowWidth = 600
#WindowHeight = 300

Declare CanvasEvents()
Declare DialogEvents()
Define.I MainWindow, DialogResult, TheEvent, TheExit, Canvas, Button

Procedure ResizeEvent( )
  
  Shared MainWindow, Canvas, Button
  
  ResizeGadget(Canvas, #PB_Ignore, #PB_Ignore, WindowWidth(MainWindow) - 130, WindowHeight(MainWindow) - 20)
  ResizeGadget(Button, WindowWidth(MainWindow) - 100, WindowHeight(MainWindow) - 40, #PB_Ignore, #PB_Ignore)
  
EndProcedure

Procedure ButtonEvents()
  
  Shared DialogResult, TheExit
  
  Protected Message$
  
  DialogResult = MessageRequester("Exit", "Do you want to exit?", #PB_MessageRequester_YesNoCancel | #PB_MessageRequester_Info)
  
  Select DialogResult
    Case #PB_MessageRequester_Yes
      Message$ = "Result of the previous requester was: Yes."
      
    Case #PB_MessageRequester_No
      Message$ = "Result of the previous requester was: No."
      
    Case #PB_MessageRequester_Cancel
      Message$ = "Result of the previous requester was: Cancel."
  EndSelect
      
  MessageRequester("Information", Message$, #PB_MessageRequester_Info)
  
  If DialogResult = #PB_MessageRequester_Yes
    TheExit = #True
  EndIf
  
EndProcedure
  
Procedure CanvasEvents( )
  
  Shared Canvas
  
  Static Drawing
  
  Protected TheEventType = EventType( )
  Protected x,y
  
  Select TheEventType
      
    Case #PB_EventType_FirstCustomValue
      Debug "#PB_EventType_FirstCustomValue "
      
    Case #PB_EventType_LeftButtonDown
      Drawing = #True  
      
    Case #PB_EventType_LeftButtonUp
      Drawing = #False
      
    Case #PB_EventType_MouseMove
      If Drawing And StartDrawing(CanvasOutput(Canvas))
        x = GetGadgetAttribute(Canvas, #PB_Canvas_MouseX)
        y = GetGadgetAttribute(Canvas, #PB_Canvas_MouseY)
        Circle(x, y, 10, RGB(Random(255), Random(255), Random(255)))
        StopDrawing()
      EndIf
      
    Case #PB_EventType_RightButtonDown
      If StartDrawing(CanvasOutput(Canvas))
        Box(0, 0, GadgetWidth(Canvas), GadgetHeight(Canvas), #White)
        StopDrawing()
      EndIf
      
  EndSelect
  
EndProcedure

MainWindow = OpenWindow( #PB_Any, #WindowX, #WindowY, #WindowWidth, #WindowHeight, "demo message", #PB_Window_SizeGadget | #PB_Window_SystemMenu )
If MainWindow
  
  Canvas = CanvasGadget(#PB_Any, 10, 10, #WindowWidth - 130, #WindowHeight - 20)
  Button = ButtonGadget(#PB_Any, #WindowWidth - 100, #WindowHeight - 40, 90, 30, "show")
  
  BindGadgetEvent( Canvas, @CanvasEvents() )
  BindGadgetEvent( Button, @ButtonEvents() )
  BindEvent(#PB_Event_SizeWindow, @ResizeEvent(), MainWindow)
  
  Repeat
    TheEvent = WaitWindowEvent( )
    
    If TheEvent = #PB_Event_CloseWindow
      TheExit = #True
    EndIf
    
  Until TheExit = #True
  
EndIf
What made you think you need the ... hybrid mixture ... you've posted? If we understand a bit more about the 'bigger picture', we might be able to give some helpful answers.
mestnyi
Addict
Addict
Posts: 1103
Joined: Mon Nov 25, 2013 6:41 am

Re: message

Post by mestnyi »

because WaitWindowEvent() cannot be used inside a BindEvent and I need this "Opens a blocking requester to display some information. The program execution is totally stopped until the user close the requester."

and in general I don’t like the built-in MessageRequester because it always opens in the middle of the screen and not in the middle of the window of the child who called it.
in Linux, MessageRequester buttons extend beyond the window border.
I want everything to have the same look.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: message

Post by spikey »

mestnyi wrote: Wed Dec 06, 2023 5:57 pm because WaitWindowEvent() cannot be used inside a BindEvent
That's not an issue, that's not what it's for anyway. Use Event() inside the bound procedure to find out what WaitWindowEvent() returned for the current trigger. See below.
mestnyi wrote: Wed Dec 06, 2023 5:57 pm I need this "Opens a blocking requester to display some information".
There are a couple of ways to achieve this, see DisableWindow().
Or you could have a "restrict events" variable which you check inside your bound procedures:

Code: Select all

EnableExplicit

Define.i RestrictEvents, TheExit

RestrictEvents = -1

Procedure WinEvents()
  
  Shared RestrictEvents, TheExit
  Protected TheEvent, TheWindow, TheGadget
  
  TheEvent = Event()
  TheWindow = EventWindow()
  TheGadget = EventGadget()
  
  If RestrictEvents <> -1 And RestrictEvents <> TheWindow
    Debug "Window " + Str(TheWindow) + " is currently inhibited."
    ProcedureReturn
  EndIf
  
  Select TheEvent
      
    Case #PB_Event_CloseWindow
      TheExit = #True
      
    Case #PB_Event_Gadget
      
      Select TheGadget
          
        Case 0, 3, 6
          RestrictEvents = TheWindow
          Debug "Events are now restricted to window " + StrU(TheWindow) + "."
          
        Case 1, 4, 7
          RestrictEvents = -1
          Debug "Events are now unrestricted."
          
        Case 2, 5, 8
          Debug "Button click in window " + StrU(TheWindow) + "."
          
      EndSelect
      
  EndSelect
  
EndProcedure

If OpenWindow(0, 50, 50, 300, 100, "Test 0", #PB_Window_SystemMenu | #PB_Window_SizeGadget)
  ButtonGadget(0, 10, 10, 90, 25, "Restrict events")
  ButtonGadget(1, 100, 10, 90, 25, "Enable events")
  ButtonGadget(2, 200, 10, 90, 25, "Test")
  BindEvent(#PB_Event_Gadget, @WinEvents(), 0)
  BindEvent(#PB_Event_CloseWindow, @WinEvents(), 0)
EndIf

If OpenWindow(1, 50, 200, 300, 100, "Test 1", #PB_Window_SystemMenu | #PB_Window_SizeGadget)
  ButtonGadget(3, 10, 10, 90, 25, "Restrict events")
  ButtonGadget(4, 100, 10, 90, 25, "Enable events")
  ButtonGadget(5, 200, 10, 90, 25, "Test")
  BindEvent(#PB_Event_Gadget, @WinEvents(), 1)
  BindEvent(#PB_Event_CloseWindow, @WinEvents(), 1)
EndIf

If OpenWindow(2, 400, 50, 300, 100, "Test 2", #PB_Window_SystemMenu | #PB_Window_SizeGadget)
  ButtonGadget(6, 10, 10, 90, 25, "Restrict events")
  ButtonGadget(7, 100, 10, 90, 25, "Enable events")
  ButtonGadget(8, 200, 10, 90, 25, "Test") 
  BindEvent(#PB_Event_Gadget, @WinEvents(), 2)
  BindEvent(#PB_Event_CloseWindow, @WinEvents(), 2)
EndIf

Repeat 
  WaitWindowEvent()
Until TheExit = #True
Or if you want multiple active and multiple inactive windows concurrently each window could have its state information in SetWindowData...
mestnyi
Addict
Addict
Posts: 1103
Joined: Mon Nov 25, 2013 6:41 am

Re: message

Post by mestnyi »

anything but what I need, that's your intention? :)
if you can help make my example work, you are welcome. I don't need anything else.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 778
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: message

Post by spikey »

mestnyi wrote: Wed Dec 06, 2023 8:01 pm anything but what I need, that's your intention? :)
if you can help make my example work, you are welcome. I don't need anything else.
That's the point. What you've presented isn't going to work.
mestnyi
Addict
Addict
Posts: 1103
Joined: Mon Nov 25, 2013 6:41 am

Re: message

Post by mestnyi »

spikey wrote: Wed Dec 06, 2023 8:08 pm
mestnyi wrote: Wed Dec 06, 2023 8:01 pm anything but what I need, that's your intention? :)
if you can help make my example work, you are welcome. I don't need anything else.
That's the point. What you've presented isn't going to work.
Why? this works in Windows, now I need the same in Mac OS and Linux, Linux I think I’ll figure it out myself, but that’s later.
AZJIO
Addict
Addict
Posts: 2228
Joined: Sun May 14, 2017 1:48 am

Re: message

Post by AZJIO »

Post Reply