'Code module
'Subs in this code module will be accessible from all modules.
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    'Dim scalepy As Int : scalepy = 1
    'Dim IPBoxText As String : IPBoxText = ""
End Sub
Sub MB(title As String,msg As String,pos As String,can As String,neg As String) As Int
    Dim gBD As BetterDialogs
    Dim dlg_params As BD_CustomDlgParams
    Dim DR As Int
    Dim port As Boolean
    Dim lval As LayoutValues
    Dim sc As Float
    Dim Scrn As Double
    Dim psize, lsize As Int
    lval = GetDeviceLayoutValues
    sc = lval.scale
    Scrn = lval.ApproximateScreenSize
    If Scrn < 6 Then
'       psize = 18dip
'       lsize = 20dip
       psize = 18
       lsize = 20
      
    Else
'       psize = 35dip
'       lsize = 45dip
       psize = 35
       lsize = 45
    End If
   
    If Scrn > 7.5 Then
'       psize = 18dip
'       lsize = 20dip
       psize = 55
       lsize = 65
    End If      
   
   
    ' sc = 1
    If lval.Height > lval.Width Then
       port = True
    Else
       port = False
    End If
   
   
    Dim btnOK As Button
    btnOK.Initialize("")
    btnOK.Text = pos
    btnOK.TextColor = Colors.White
    'btnOK.Gravity = Gravity.CENTER ' + Gravity.CENTER_HORIZONTAL
   
    If port = True Then
      btnOK.TextSize = psize / sc '/sc
    Else
      btnOK.TextSize = lsize / sc '/sc
    End If   
    btnOK.Background = CreateStateListDrawable1
    'ToastMessageShow(" Port: "&port&" Height: "&lval.Height&" Width: "&lval.Width&"  SCALE: "&sc ,True)
   
    Dim btnCancel As Button
    btnCancel.Initialize("")
    btnCancel.Text = can
    If port = True Then
      btnCancel.TextSize = psize / sc '/ sc
    Else
      btnCancel.TextSize = lsize / sc '/sc
    End If   
    btnCancel.TextColor = Colors.White
    btnCancel.Background = CreateStateListDrawable1
    Dim btnneg As Button
    btnneg.Initialize("")
    btnneg.Text = neg
    If port = True Then
      btnneg.TextSize = psize / sc '/ sc
    Else
      btnneg.TextSize = lsize / sc '/sc
    End If   
    btnneg.TextColor = Colors.White
    btnneg.Background = CreateStateListDrawable1
    Dim pnlBody As Panel
    pnlBody.Initialize("")
    'pnlBody.LoadLayout("msgbody")
'    Dim pnlBody As Panel
'    pnlBody.Initialize("")
    Dim sld As StateListDrawable
    sld.Initialize
    Dim gd1 As GradientDrawable
    gd1.Initialize( "TOP_BOTTOM", Array As Int(Colors.DarkGray, Colors.Black))
    gd1.CornerRadius = 0dip
    sld.AddCatchAllState(gd1)
   
    Dim lab As Label
    lab.Initialize("")
    lab.Text = msg
'lab.Text = "Are You Sure You Want To End Your Shift Today. Maybe it will be tomorrow? It could even be the day after that - Who Knows?"   
    lab.TextColor = Colors.Yellow
    lab.Gravity = Gravity.CENTER_HORIZONTAL+Gravity.CENTER_VERTICAL
'    lab.Top = pnlBody.Top + 10dip
'    lab.left = pnlBody.left + 10dip
'    lab.width = 90%x
'    lab.Height = 90%y]
dlg_params.Initialize
dlg_params.Title = title
dlg_params.TitleWidth = -1
dlg_params.TitleHeight = 60dip
dlg_params.DialogBody = pnlBody
dlg_params.BodyWidth = 90%x
dlg_params.BodyHeight = 40%y
dlg_params.MarginHeight = 0dip
dlg_params.Background = sld
dlg_params.PositiveButton = btnOK
dlg_params.CancelButton = btnCancel
dlg_params.NegativeButton = btnneg
dlg_params.OpenKeyboard = False
' DR = gBD.CustomDialog( title,  -1,     60dip,    pnlBody,           90%x,     40%y, 0dip,   sld , btnOK, btnCancel, btnneg,  False,  "CD2"   )
'                       title, t_width, t_height,  Dialog -pnlbody, d_width, d_height, margin, background, btnok,btncancel,btnneg, openkeyboard, event)
    If port = True Then
       If Scrn < 6 Then
'         lab.TextSize = psize / sc
         pnlBody.AddView(lab, 10%x ,0%y , 70%x, 41%y)
'         pnlBody.AddView(lab, 10%x ,0%y , 70%x, 91%y)
        
         lab.TextSize = SetTextSize(lab,lab.Text,sc)
         DR = gBD.CustomDialog( dlg_params, "CD2"   )
       Else
'         lab.TextSize = psize / sc
         pnlBody.AddView(lab, 10%x ,0%y , 70%x, 41%y)
         lab.TextSize = SetTextSize(lab,lab.Text,sc)
         dlg_params.Title = "<H1>"&title&"</H1>"
         DR = gBD.CustomDialog(dlg_params, "CD2")
      
       End If
    Else
        pnlBody.AddView(lab , 5%x, 0 ,  80%x, 55%y)
        lab.TextSize = SetTextSize(lab ,lab.Text, sc)
         dlg_params.Title = "<H1>"&title&"</H1>"
         dlg_params.BodyWidth = 90%x
         dlg_params.BodyHeight = 60%y
         dlg_params.DialogBody = pnlBody
         DR = gBD.CustomDialog(dlg_params,  "CD2")
    '     DR = gBD.MsgBox(title,msg,btnOK.Text,btnCancel.Text,btnneg.Text,Null)
        
'        DR = gBD.CustomDialog( "<H1>"&title&"</H1>",  -1, 60dip,  pnlBody,  80%x, 60%y, 0dip,  sld , btnOK, btnCancel, btnneg, False, "CD2")
    End If
   
    'pnlBody.Color = Colors.Green
   
'    scalepy = scalepy + 1
'    If scalepy > 5 Then
'       scalepy = 1
'    End If
   
'    Log("DR=" & DR)
    Return DR
   
  
End Sub
Sub SetTextSize(lbl As Label, txt As String, Sca As Float) As Int
   Dim dt As Float
   Dim limit = 0.5 As Float
   Dim h As Int
   Dim stu As StringUtils
  
   lbl.Text = txt
   lbl.TextSize = 272
   dt = lbl.TextSize
   h = stu.MeasureMultilineTextHeight(lbl, txt)  
'   Log("Multi Line Height: "&h)
  
   Do While dt > limit Or h > lbl.Height
     dt = dt / 2
     h = stu.MeasureMultilineTextHeight(lbl, txt)  
     If h > lbl.Height Then
       lbl.TextSize = lbl.TextSize - dt
     Else
       lbl.TextSize = lbl.TextSize + dt
     End If
   Loop
   If Sca = 99.0 Then
      Return lbl.TextSize' / 1.30 '(scale / 2) ' 1.35
  
   Else
       Return lbl.TextSize / 1.20 '(scale / 2) ' 1.35
   End If
  
      
End Sub
Sub CD2_BackKeyPressed As Boolean
    Log("Back key detected")
    Return True 'Prevents the cancellation of the dialog
End Sub
Sub CreateStateListDrawable1 As StateListDrawable
    'Creates a SLD for the buttons of the custom dialog
    Dim sd As StateListDrawable
    sd.Initialize
    Dim gd1 As GradientDrawable
    gd1.Initialize("BOTTOM_TOP" , Array As Int(Colors.Gray, Colors.DarkGray))
    gd1.CornerRadius = 0dip
   
    Dim gd2 As GradientDrawable
    gd2.Initialize( "BOTTOM_TOP", Array As Int(Colors.DarkGray, Colors.Gray))
    gd2.CornerRadius = 0dip
    sd.AddState(sd.State_Pressed, gd2)
    sd.AddCatchAllState(gd1)
    Return sd
End Sub
Sub CreateStateListDrawable As StateListDrawable
    'Creates a SLD for the buttons of the custom dialog
    Dim sd As StateListDrawable
    sd.Initialize
    Dim gd1 As GradientDrawable
    gd1.Initialize("BOTTOM_TOP" , Array As Int(Colors.Gray, Colors.DarkGray))
    gd1.CornerRadius = 10dip
   
    Dim gd2 As GradientDrawable
    gd2.Initialize( "BOTTOM_TOP", Array As Int(Colors.DarkGray, Colors.Gray))
    gd2.CornerRadius = 10dip
    sd.AddState(sd.State_Pressed, gd2)
    sd.AddCatchAllState(gd1)
    Return sd
End Sub
Sub LoadDrawable(Name As String) As Object
    'Gets a drawable from the Android system resources
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
    r.Target = r.RunMethod("getSystem")
    Dim ID_Drawable As Int
    ID_Drawable = r.RunMethod4("getIdentifier", Array As Object(Name, "drawable", "android"), _
                                                Array As String("java.lang.String", "java.lang.String", "java.lang.String"))
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
    Return r.RunMethod2("getDrawable", ID_Drawable, "java.lang.int")
End Sub