Page 1 of 1

Own Message requester without API

Posted: Fri May 05, 2006 8:40 am
by Klonk
Code updated for 5.20+

Hi,
maybe someone find this useful.

I created a special Messagerequester which could be either displayed centered to the parent window or could be positioned absolutely on the screen.

Of course positioning could have been done through WINAPI calls, but this code is completely plattform independent (or should be). Also the positioning of MessageRequesters I found on thsi forum gave Stack errors on Win98SE. This verison works without problem on Win98SE.

Currently it features the same functions as MessageRequester, but could be easily extended for "Retry - Cancel" or anything else Messagerequester.


Enjoy it and use it at will.

Code: Select all

;{ ==========================================================================
;|  MessageRequesterEx(title$,text$,flags,xPos=-1,yPos=-1)
;|    This is a special procedure to display a window in the same way as a
;|    MessageRequester. When yPos is not provided then xPos will be treated
;|    as WinID. winID is the value provided by an OpenWindow. If winID is
;|    valid the Requester will be displayed
;|    centered to that window. If the winID is not valid then it is possible
;|    to set the position of the top left edge of the requester window manually
;|    xPos and yPos are only used, when no winID was given. When no windowID
;|    or xPos or yPos is given, then the message is displayed cenered to the
;|    screen. The same constants are used than with MessageRequester.
;|    No OS specific flags (e.g. from WinAPI) are supported!
;} ==========================================================================
;{ ==========================================================================
;|  MessageRequesterEx(title$,text$,flags,xPos=-1,yPos=-1)
;|    This is a special procedure to display a window in the same way as a
;|    MessageRequester. When yPos is not provided then xPos will be treated
;|    as WinID. winID is the value provided by an OpenWindow. If winID is
;|    valid the Requester will be displayed
;|    centered to that window. If the winID is not valid then it is possible
;|    to set the position of the top left edge of the requester window manually
;|    xPos and yPos are only used, when no winID was given. When no windowID
;|    or xPos or yPos is given, then the message is displayed cenered to the
;|    screen. The same constants are used than with MessageRequester.
;|    No OS specific flags (e.g. from WinAPI) are supported!
;} ==========================================================================
Procedure MessageRequesterEx(title$,text$,flags=-1,xPos=-1,yPos=-1)
;***** display message requester at special position or centered to parent window
  Structure Button
    text.s  ;contains Buttontext
    id.l    ;contains ButtonID
    val.l   ;contains Button Returnvalue
  EndStructure
  Dim Buttons.Button(3)  ;maximum of 3 buttons possible
  #buttonwidth = 70 ;set width per button (pixels)
  #buttonspace = 5 ;set space between buttons (pixels)
  #lineheight = 13 ;set height per line (pixels)
  #minheight = 70 ;set minimum height of the requester window for one line text
  #maxwidth = 790 ;set maximum width of requester
  #widthcorrection = 0.78 ; correction factor for textwidth calculation
   ExamineDesktops() ; allow checking for desktopsize
  If flags = -1 ;set default if no flags provided
    flags = #PB_MessageRequester_Ok
  EndIf ;set default for WinID
  If yPos = -1
    winID = xPos
  Else
    winID = -1
  EndIf
  ;{ calculate necessary number of buttons and set texts for buttons
  If flags = #PB_MessageRequester_Ok
    ButtonCount = 1
    Buttons(1)\text = "Ok"
    Buttons(1)\val = #PB_MessageRequester_Ok
  ElseIf flags = #PB_MessageRequester_YesNo
    ButtonCount = 2
    Buttons(1)\text = "Yes"
    Buttons(1)\val = #PB_MessageRequester_Yes
    Buttons(2)\text = "No"
    Buttons(2)\val = #PB_MessageRequester_No
  ElseIf flags = #PB_MessageRequester_YesNoCancel
    ButtonCount = 3
    Buttons(1)\text = "Yes"
    Buttons(1)\val = #PB_MessageRequester_Yes
    Buttons(2)\text = "No"
    Buttons(2)\val = #PB_MessageRequester_No
    Buttons(3)\text = "Cancel"
    Buttons(3)\val = #PB_MessageRequester_Cancel
  EndIf
  ;}
  ;{ calculate maximum allowed width of requester
  maximumwidth=#maxwidth
  If IsWindow(winID) ; when window exists limit maximum width to window width
    If WindowWidth(winID)<#maxwidth
      maximumwidth = WindowWidth(winID)
    EndIf
  EndIf
  If (xPos<>-1) And (yPos<>-1) And ((DesktopWidth(0)-xPos)<#maxwidth) ;make requester not bigger than available space
    maximumwidth = DesktopWidth(0)-xPos
  EndIf
  ;}
  ;{ calculate necessary width And height of the requester
  image = CreateImage(#PB_Any,8,8) ;create temporary image for checking of textsize
  StartDrawing(ImageOutput(Image))
  textlines = 1 ; minimum one line
  If Round(TextWidth(text$)*#widthcorrection,1)+20 >= maximumwidth
    RequesterWidth = maximumwidth
    temp$ = text$
    For i = 1 To Len(temp$)
      If TextWidth(Left(temp$,i))+20 >= maximumwidth ;cut string into pieces and check how many lines this would be
        textlines=textlines+1
        temp$= Right(temp$,Len(temp$)-i)
        i=1
      EndIf
    Next
    If textlines > 1 ;vary heigth when necessary
      RequesterHeight = #minheight+textlines*#lineheight
    Else
      RequesterHeight = #minheight
    EndIf
  ElseIf Round(TextWidth(text$)*#widthcorrection,1) >= (ButtonCount*#buttonwidth+(ButtonCount-1)*#buttonspace)
    RequesterWidth = Round(TextWidth(text$)*#widthcorrection,1)+20
    RequesterHeight = #minheight
  Else
    RequesterWidth = (ButtonCount*#buttonwidth+(ButtonCount-1)*#buttonspace)+20
    RequesterHeight = #minheight
  EndIf
  StopDrawing()
  FreeImage(image) ;free memory for temporary image
  ;}
  ;{ open window according to provided data
  If IsWindow(winID)
    Requester = OpenWindow(#PB_Any, 0, 0, RequesterWidth, RequesterHeight, title$, #PB_Window_WindowCentered, WindowID(winID))
  ElseIf (xPos<>-1) And (yPos<>-1)
    Requester = OpenWindow(#PB_Any, xPos, yPos, RequesterWidth, RequesterHeight, title$, 0)
  Else
    Requester = OpenWindow(#PB_Any, 0, 0, RequesterWidth, RequesterHeight, title$, #PB_Window_ScreenCentered)
  EndIf
  ;}
  If Requester
    TextGadget(#PB_Any, 5,10, RequesterWidth-10, (textlines+1)*#lineheight+5, text$, #PB_Text_Center)
    For i = 1 To ButtonCount
      Buttons(i)\id = ButtonGadget(#PB_Any, Round((Requesterwidth-(ButtonCount*#buttonwidth)-((ButtonCount-1)*#buttonspace))/2,0)+((i-1)*75),Requesterheight-30 ,#buttonwidth,20, Buttons(i)\text)
    Next
  EndIf
  Repeat ;handle event for message requester window
    Event = WaitWindowEvent()
    If Event = #PB_Event_Gadget ;button was pressed
      For i=1 To ButtonCount
        If EventGadget() = Buttons(i)\id ;determine which button was pressed
          CloseWindow(Requester) ;close Requesterwindow
          ProcedureReturn Buttons(i)\val ;give back result
        EndIf
      Next
    EndIf
  ForEver
EndProcedure

MessageRequesterEx("title", "text")