/' Iup Alarm: Example in C 
   Shows a dialog similar to the one shown when you exit a program without saving.
'/

#include once "crt/stdlib.bi"
#include once "crt/stdio.bi"

#include once "inc/iup.bi"
#include once "inc/iupcd.bi"

sub _init_ constructor
  IupOpen(0,0)
end sub

sub _exit_ destructor
  IupClose()
end sub


dim shared as Ihandle ptr dlg,bt,gauge,tabs,cv 
dim shared as cdCanvas ptr cdcanvas
dim shared as integer need_redraw, redraw_count 

function toggle_redraw cdecl() As Integer
  cdCanvasActivate(cdcanvas)
  need_redraw = Not need_redraw
  return IUP_DEFAULT
End Function

function redraw cdecl() As Integer
  if (need_redraw) Then
    cdCanvasBox(cdcanvas, 0, 300, 0, redraw_count/100)
    IupSetfAttribute(gauge, "VALUE", "%g", cast(single,redraw_count)/30000.0)
    redraw_count+=1
    if (redraw_count = 30000)Then
      cdCanvasClear(cdcanvas)
      redraw_count = 0
      need_redraw = 0
    End If
  End If
  return IUP_DEFAULT
End Function

'
' main
'
gauge = IupProgressBar()
cv    = IupCanvas(NULL)
bt    = IupButton("Start/Stop", NULL)
IupSetAttribute(bt,    "SIZE", "50x50")
IupSetAttribute(gauge, "SIZE", "200x15")
IupSetAttribute(cv,    "SIZE", "200x200")
dlg   = IupDialog(IupVbox(cv, IupHbox(gauge, bt, NULL), NULL))
IupSetAttribute(dlg, "TITLE", "Redraw test")
IupSetFunction("IDLE_ACTION", Cast(Icallback,@redraw))

IupMap(dlg)
  
cdcanvas = cdCreateCanvas(cdContextIup(), cv)
cdCanvasForeground(cdcanvas, CD_BLUE)
cdCanvasClear(cdcanvas)
  
IupSetCallback(bt, "ACTION", Cast(Icallback,@toggle_redraw))
IupShowXY(dlg, IUP_CENTER, IUP_CENTER)
IupMainLoop()
