#include once "crt/stdio.bi"

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

sub _init_ constructor
  IupOpen(0,0)
  'iupKeyInit()
end sub

sub _exit_ destructor
  IupClose()
end sub


sub text2multiline cdecl (ih as IHandle ptr, attribute as zstring ptr)
  printf(!"text2multiline\n")
  dim as IHandle ptr mltline = IupGetDialogChild(ih, "mltline")
  dim as IHandle ptr text = IupGetDialogChild(ih, "text")
  IupSetAttribute (mltline, attribute, IupGetAttribute (text, "VALUE"))
end sub

sub multiline2text cdecl (ih as IHandle ptr, attribute as zstring ptr)
  printf(!"multiline2text\n")
  dim as IHandle ptr mltline = IupGetDialogChild(ih, "mltline")
  dim as IHandle ptr text = IupGetDialogChild(ih, "text")
  IupSetAttribute (text, "VALUE", IupGetAttribute(mltline, attribute))
end sub

function btn_append_cb cdecl (ih as IHandle ptr) as integer
  printf(!"btn_append_cb\n")
  text2multiline(ih, "APPEND") 
  return IUP_DEFAULT
end function

function btn_insert_cb cdecl (ih as IHandle ptr) as integer
  printf(!"btn_insert_cb\n")
  text2multiline(ih, "INSERT") 
  return IUP_DEFAULT
end function

function btn_clip_cb cdecl (ih as IHandle ptr) as integer
  printf(!"btn_clip_cb\n")
  text2multiline(ih, "CLIPBOARD") 
  return IUP_DEFAULT
end function

function btn_key_cb cdecl (ih as IHandle ptr) as integer
  printf(!"btn_key_cb\n")
  dim as IHandle ptr mltline = IupGetDialogChild(ih, "mltline")
  dim as IHandle ptr text = IupGetDialogChild(ih, "text")
  IupSetFocus(mltline)
  IupFlush()
  IupSetfAttribute(NULL, "KEY", "%d", iupKeyNameToCode(IupGetAttribute(text, "VALUE")))
  return IUP_DEFAULT
end function

function btn_caret_cb cdecl (ih as IHandle ptr) as integer
  printf(!"btn_caret_cb\n")
  dim as IHandle ptr opt = IupGetHandle("text2multi")
  if (IupGetInt(opt, "VALUE")) then
    text2multiline(ih, "CARET") 
  else 
    multiline2text(ih, "CARET")
  end if
  return IUP_DEFAULT
end function

function btn_readonly_cb cdecl (ih as IHandle ptr) as integer
  printf(!"btn_readonly_cb\n")
  dim as IHandle ptr opt = IupGetHandle("text2multi")
  if (IupGetInt(opt, "VALUE")) then
    text2multiline(ih, "READONLY") 
  else 
    multiline2text(ih, "READONLY")
  end if
  return IUP_DEFAULT
end function

function btn_selection_cb cdecl (ih as IHandle ptr) as integer
  printf(!"btn_selection_cb\n")
  dim as IHandle ptr opt = IupGetHandle ("text2multi")
  if (IupGetInt (opt, "VALUE")) then
    text2multiline (ih, "SELECTION") 
  else 
    multiline2text (ih, "SELECTION")
  end if
  return IUP_DEFAULT
end function

function btn_selectedtext_cb cdecl (ih as IHandle ptr) as integer
  printf(!"btn_selectedtext_cb\n")
  dim as IHandle ptr opt = IupGetHandle ("text2multi")
  if (IupGetInt (opt, "VALUE")) then
    text2multiline (ih, "SELECTEDTEXT") 
  else 
    multiline2text (ih, "SELECTEDTEXT")
  end if
  return IUP_DEFAULT
end function

function btn_overwrite_cb cdecl (ih as IHandle ptr) as integer
  printf(!"btn_overwrite_cb\n")
  dim as IHandle ptr opt = IupGetHandle ("text2multi")
  if (IupGetInt (opt, "VALUE")) then
    text2multiline (ih, "OVERWRITE") 
  else 
    multiline2text (ih, "OVERWRITE")
  end if
  return IUP_DEFAULT
end function

function btn_active_cb cdecl (ih as IHandle ptr) as integer
  printf(!"btn_active_cb\n")
  dim as IHandle ptr opt = IupGetHandle ("text2multi")
  if (IupGetInt (opt, "VALUE")) then
    text2multiline (ih, "ACTIVE") 
  else 
    multiline2text (ih, "ACTIVE")
  end if
  return IUP_DEFAULT
end function

function btn_remformat_cb cdecl (ih as IHandle ptr) as integer
  printf(!"btn_selectedtext_cb\n")
  text2multiline (ih, "REMOVEFORMATTING") 
  return IUP_DEFAULT
end function

function btn_nc_cb cdecl (ih as IHandle ptr) as integer
  printf(!"btn_nc_cb\n")
  dim as IHandle ptr opt = IupGetHandle ("text2multi")
  if (IupGetInt (opt, "VALUE")) then
    text2multiline (ih, "NC") 
  else 
    multiline2text (ih, "NC")
  end if
  return IUP_DEFAULT
end function

function btn_value_cb cdecl (ih as IHandle ptr) as integer
  printf(!"btn_value_cb\n")
  dim as IHandle ptr opt = IupGetHandle ("text2multi")
  if (IupGetInt (opt, "VALUE")) then
    text2multiline (ih, "VALUE") 
  else 
    multiline2text (ih, "VALUE")
  end if
  return IUP_DEFAULT
end function

function btn_tabsize_cb cdecl (ih as IHandle ptr) as integer
  printf(!"btn_tabsize_cb\n")
  dim as IHandle ptr opt = IupGetHandle ("text2multi")
  if (IupGetInt (opt, "VALUE")) then
    text2multiline (ih, "TABSIZE") 
  else 
    multiline2text (ih, "TABSIZE")
  end if
  return IUP_DEFAULT
end function

function kf2 cdecl () as integer
  printf("kf2\n")
  return IUP_DEFAULT
end function

function file_open cdecl as integer
  printf(!"file_open")
  dim as string filename = space(100)
  IupGetFile(strptr(filename))  ' test key after dlg in multiline
  printf(filename)
  printf(!"\n")
  return IUP_DEFAULT
end function



function kany cdecl (ih as IHandle ptr, c as integer) as integer
  if (iup_isprint(c)) then
    printf(!"K_ANY(%d = %s \'%c\')\n", c, iupKeyCodeToName(c), c)
  else
    printf(!"K_ANY(%d = %s)\n", c, iupKeyCodeToName(c))
  end if
  printf(!"  CARET(%s)\n", IupGetAttribute(ih, "CARET"))
  if (c = K_cA) then return IUP_IGNORE   ' Sound a beep in Windows
  if (c = K_cP) then
    file_open()
    return IUP_IGNORE   ' Sound a beep in Windows
  end if
  return IUP_CONTINUE
end function

function action cdecl (ih as IHandle ptr, c as integer, after as zstring ptr) as integer
  if (iup_isprint(c)) then
    printf(!"ACTION(%d = %s \'%c\', %s)\n", c, iupKeyCodeToName(c), c, after)
  else
    printf(!"ACTION(%d = %s, %s)\n", c, iupKeyCodeToName(c), after)
  end if
  if (c = K_i) then return IUP_IGNORE   ' OK
  if (c = K_cD) then return IUP_IGNORE   ' Sound a beep in Windows
  if (c = K_h) then return K_j
  return IUP_DEFAULT
end function

function caret_cb cdecl (ih as IHandle ptr, lin as integer, col as integer, pos_ as integer) as integer
  printf(!"CARET_CB(%d, %d - %d)\n", lin, col, pos_)
  printf(!"  CARET(%s - %s)\n", IupGetAttribute(ih, "CARET"), IupGetAttribute(ih, "CARETPOS"))
  return IUP_DEFAULT
end function

function getfocus_cb cdecl (ih as IHandle ptr) as integer
  printf(!"GETFOCUS_CB()\n")
  return IUP_DEFAULT
end function

function help_cb cdecl (ih as IHandle ptr) as integer
  printf(!"HELP_CB()\n")
  return IUP_DEFAULT
end function
     
function killfocus_cb cdecl (ih as IHandle ptr) as integer
  printf(!"KILLFOCUS_CB()\n")
  return IUP_DEFAULT
end function

function leavewindow_cb cdecl (ih as IHandle ptr) as integer
  printf(!"LEAVEWINDOW_CB()\n")
  return IUP_DEFAULT
end function

function enterwindow_cb cdecl (ih as IHandle ptr) as integer
  printf(!"ENTERWINDOW_CB()\n")
  return IUP_DEFAULT
end function

function btn_def_esc_cb cdecl as integer
  printf(!"DEFAULTESC\n")
  return IUP_DEFAULT
end function

function btn_def_enter_cb cdecl as integer
  printf(!"DEFAULTENTER\n")
  return IUP_DEFAULT
end function

function dropfiles_cb cdecl (ih as IHandle ptr, filename as const zstring ptr, num as integer, x as integer, y as integer) as integer
  printf(!"DROPFILES_CB(file %s num %d, x=%d y=%d)\n", filename, num, x, y)
  return IUP_DEFAULT
end function

function button_cb cdecl (ih as IHandle ptr,but as integer, pressed as integer, x as integer, y as integer, status as zstring ptr) as integer
  dim  as integer lin, col, pos_
  printf(!"BUTTON_CB(but=%c (%d), x=%d, y=%d [%s])\n", but,pressed,x,y, status)
  pos_ = IupConvertXYToPos(ih, x, y)
  IupTextConvertPosToLinCol(ih, pos_, @lin, @col)
  printf(!"         (lin=%d, col=%d, pos=%d)\n", lin, col, pos_)
  return IUP_DEFAULT
end function

function motion_cb cdecl (ih as IHandle ptr, x as integer,y as integer, status as zstring ptr) as integer
  dim as integer lin, col, pos_
  printf("!MOTION_CB(x=%d, y=%d [%s])\n",x,y, status)
  pos_ = IupConvertXYToPos(ih, x, y)
  IupTextConvertPosToLinCol(ih, pos_, @lin, @col)
  printf(!"         (lin=%d, col=%d, pos=%d)\n", lin, col, pos_)
  return IUP_DEFAULT
end function

sub TextTest
  dim as integer formatting = 0

'  IupSetGlobal("UTF8AUTOCONVERT", "NO")

  dim as IHandle ptr text = IupText (NULL)
  IupSetAttribute(text, "EXPAND", "HORIZONTAL")
  IupSetAttribute(text, "CUEBANNER", "Enter Attribute Value Here")
  IupSetAttribute(text, "NAME", "text")
  IupSetAttribute(text, "TIP", "Attribute Value")

  dim as IHandle ptr opt = IupToggle("Set/Get", NULL)
  IupSetAttribute (opt, "VALUE", "ON")
  IupSetHandle ("text2multi", opt)

  dim as IHandle ptr mltline = IupText(NULL) 'IupMultiLine(NULL)  
  IupSetAttribute(mltline, "MULTILINE", "YES")
  IupSetAttribute(mltline, "NAME", "mltline")

  IupSetCallback(mltline, "DROPFILES_CB", cast(Icallback,@dropfiles_cb))
  IupSetCallback(mltline, "BUTTON_CB",    cast(Icallback,@button_cb))
'  IupSetCallback(mltline, "MOTION_CB",    cast(Icallback,@motion_cb))
  IupSetCallback(mltline, "HELP_CB",      cast(Icallback,@help_cb))
  IupSetCallback(mltline, "GETFOCUS_CB",  cast(Icallback,@getfocus_cb))
  IupSetCallback(mltline, "KILLFOCUS_CB", cast(Icallback,@killfocus_cb))
  IupSetCallback(mltline, "ENTERWINDOW_CB", cast(Icallback,@enterwindow_cb))
  IupSetCallback(mltline, "LEAVEWINDOW_CB", cast(Icallback,@leavewindow_cb))
'  IupSetCallback(mltline, "ACTION", cast(Icallback,@action))
  IupSetCallback(mltline, "K_ANY", cast(Icallback,@kany))
  IupSetCallback(mltline, "K_F2", cast(Icallback,@kf2))
  IupSetCallback(mltline, "CARET_CB", cast(Icallback,@caret_cb))
'  IupSetAttribute(mltline, "WORDWRAP", "YES")
'  IupSetAttribute(mltline, "BORDER", "NO")
  IupSetAttribute(mltline, "AUTOHIDE", "YES")
'  IupSetAttribute(mltline, "BGCOLOR", "255 0 128")
'  IupSetAttribute(mltline, "FGCOLOR", "0 128 192")
'  IupSetAttribute(mltline, "PADDING", "15x15")

  IupSetAttribute(mltline, "VALUE", !"First Line\nSecond Line Big Big Big\nThird Line\nmore\nmore\n") ' Windows-1252
  IupSetAttribute(mltline, "TIP", !"First Line\nSecond Line\nThird Line")

  /' Turns on multiline expand and text horizontal expand '/
  IupSetAttribute(mltline, "SIZE", "80x80")
  IupSetAttribute(mltline, "EXPAND", "YES")

' IupSetAttribute(mltline, "FONT", "Courier, 16")
' IupSetAttribute(mltline, "FONT", "Arial, 12")
' IupSetAttribute(mltline, "FONT", "Helvetica, 14")

  formatting = 1
  if (formatting) then         /' just to make easier to comment this section '/
     /' formatting before Map '/
    IupSetAttribute(mltline, "FORMATTING", "YES")

    dim as IHandle ptr formattag = IupUser()
    IupSetAttribute(formattag, "ALIGNMENT", "CENTER")
    IupSetAttribute(formattag, "SPACEAFTER", "10")
    IupSetAttribute(formattag, "FONTSIZE", "24")
    IupSetAttribute(formattag, "SELECTION", "3,1:3,50")
    IupSetAttribute(mltline, "ADDFORMATTAG_HANDLE", cast(zstring ptr,formattag))

    formattag = IupUser()
    IupSetAttribute(formattag, "BGCOLOR", "255 128 64")
    IupSetAttribute(formattag, "UNDERLINE", "SINGLE")
    IupSetAttribute(formattag, "WEIGHT", "BOLD")
    IupSetAttribute(formattag, "SELECTION", "3,7:3,11")
    IupSetAttribute(mltline, "ADDFORMATTAG_HANDLE", cast(zstring ptr,formattag))
  end if

  /' Creates buttons '/
  dim as IHandle ptr btn_append       = IupButton("&APPEND", NULL)
  dim as IHandle ptr btn_insert       = IupButton("INSERT", NULL)
  dim as IHandle ptr btn_caret        = IupButton("CARET", NULL)
  dim as IHandle ptr btn_readonly     = IupButton("READONLY", NULL)
  dim as IHandle ptr btn_selection    = IupButton("SELECTION", NULL)
  dim as IHandle ptr btn_selectedtext = IupButton("SELECTEDTEXT", NULL)
  dim as IHandle ptr btn_nc           = IupButton("NC", NULL)
  dim as IHandle ptr btn_value        = IupButton("VALUE", NULL)
  dim as IHandle ptr btn_tabsize      = IupButton("TABSIZE", NULL)
  dim as IHandle ptr btn_clip         = IupButton("CLIPBOARD", NULL)
  dim as IHandle ptr btn_key          = IupButton("KEY", NULL)
  dim as IHandle ptr btn_def_enter    = IupButton("Default Enter", NULL)
  dim as IHandle ptr btn_def_esc      = IupButton("Default Esc", NULL)
  dim as IHandle ptr btn_active       = IupButton("ACTIVE", NULL)
  dim as IHandle ptr btn_remformat    = IupButton("REMOVEFORMATTING", NULL)
  dim as IHandle ptr btn_overwrite    = IupButton("OVERWRITE", NULL)

  IupSetAttribute(btn_append, "TIP", "First Line\nSecond Line\nThird Line")

  /' Registers callbacks '/
  IupSetCallback(btn_append,       "ACTION", cast(Icallback,@btn_append_cb))
  IupSetCallback(btn_insert,       "ACTION", cast(Icallback,@btn_insert_cb))
  IupSetCallback(btn_caret,        "ACTION", cast(Icallback,@btn_caret_cb))
  IupSetCallback(btn_readonly,     "ACTION", cast(Icallback,@btn_readonly_cb))
  IupSetCallback(btn_selection,    "ACTION", cast(Icallback,@btn_selection_cb))
  IupSetCallback(btn_selectedtext, "ACTION", cast(Icallback,@btn_selectedtext_cb))
  IupSetCallback(btn_nc,           "ACTION", cast(Icallback,@btn_nc_cb))
  IupSetCallback(btn_value,        "ACTION", cast(Icallback,@btn_value_cb))
  IupSetCallback(btn_tabsize,      "ACTION", cast(Icallback,@btn_tabsize_cb))
  IupSetCallback(btn_clip,         "ACTION", cast(Icallback,@btn_clip_cb))
  IupSetCallback(btn_key,          "ACTION", cast(Icallback,@btn_key_cb))
  IupSetCallback(btn_def_enter,    "ACTION", cast(Icallback,@btn_def_enter_cb))
  IupSetCallback(btn_def_esc,      "ACTION", cast(Icallback,@btn_def_esc_cb))
  IupSetCallback(btn_active,       "ACTION", cast(Icallback,@btn_active_cb))
  IupSetCallback(btn_remformat,    "ACTION", cast(Icallback,@btn_remformat_cb))
  IupSetCallback(btn_overwrite,    "ACTION", cast(Icallback,@btn_overwrite_cb))

  dim as IHandle ptr lbl = IupLabel("&Multiline:")
  IupSetAttribute(lbl, "PADDING", "2x2")

  /' Creates dlg '/
  dim as IHandle ptr dlg = IupDialog(IupVbox(lbl, _
                          mltline, _
                          IupHbox (text, opt, NULL), _
                          IupHbox (btn_append, btn_insert, btn_caret, btn_readonly, btn_selection, NULL), _
                          IupHbox (btn_selectedtext, btn_nc, btn_value, btn_tabsize, btn_clip, btn_key, NULL), _
                          IupHbox (btn_def_enter, btn_def_esc, btn_active, btn_remformat, btn_overwrite, NULL), _
                          NULL))
  IupSetCallback(dlg, "K_cO", cast(Icallback,@file_open))
  IupSetAttribute(dlg, "TITLE", "IupText Test")
  IupSetAttribute(dlg, "MARGIN", "10x10")
  IupSetAttribute(dlg, "GAP", "5")
  IupSetAttributeHandle(dlg, "DEFAULTENTER", btn_def_enter)
  IupSetAttributeHandle(dlg, "DEFAULTESC", btn_def_esc)
  IupSetAttribute(dlg, "SHRINK", "YES")
  
  formatting=1
  if (formatting) then         /' just to make easier to comment this section '/
    IupSetAttribute(mltline, "FORMATTING", "YES")
    'IupMap(dlg)
    /' formatting after Map '/
    dim as IHandle ptr formattag = IupUser()
    IupSetAttribute(formattag, "ITALIC", "YES")
    IupSetAttribute(formattag, "STRIKEOUT", "YES")
    IupSetAttribute(formattag, "SELECTION", "2,1:2,12")
    IupSetAttribute(mltline, "ADDFORMATTAG_HANDLE", cast(zstring ptr,formattag))
  end if

  /' Shows dlg in the center of the screen '/
  IupShowXY(dlg, IUP_CENTER, IUP_CENTER)
  IupSetFocus(mltline)
end sub

'
' main
'
TextTest
IupMainLoop






