#
# This file is a part of the NsCDE - Not so Common Desktop Environment
# Author: Hegel3DReloaded
# Licence: GPLv3
#

UseGettext {$NSCDE_ROOT/share/locale;NsCDE-KeyboardMgr}
WindowLocaleTitle {Style Manager - Keyboard}
WindowSize 460 370
Colorset 22

Init
Begin
   Set $genfile= (GetOutput {sh -c "echo $FVWM_USERDIR/Xset.conf"} -1 1)
   Set $FILE = $genfile

   ChangeValue 9 (GetOutput  {xset -q | egrep 'auto repeat:' } 0 7)
   If (GetOutput  {xset -q | egrep 'auto repeat:' } 0 3)==on Then
   Begin
      ChangeValue 3 1
   End
   ChangeValue 5 (GetOutput  {xset -q | egrep 'auto repeat delay:' } 0 4)
   ChangeValue 7 (GetOutput  {xset -q | egrep 'auto repeat delay:' } 0 7)

   Set $DefFont = (GetOutput {$NSCDE_TOOLSDIR/getfont -v -t normal -s medium -Z 11.5} 1 -1)
   ChangeFont 2 $DefFont
   ChangeFont 3 $DefFont
   ChangeFont 4 $DefFont
   ChangeFont 6 $DefFont
   ChangeFont 8 $DefFont
   ChangeFont 10 $DefFont
   ChangeFont 11 $DefFont

   Set $BtnFont = (GetOutput {$NSCDE_TOOLSDIR/getfont -v -t normal -s medium -Z 16} 1 -1)
   ChangeFont 13 $BtnFont
   ChangeFont 14 $BtnFont
   ChangeFont 15 $BtnFont
   ChangeFont 16 $BtnFont

   Set $HScrollBarFont = (GetOutput {$NSCDE_TOOLSDIR/getfont -v -t normal -s small -Z 11.5} 1 -1)
   ChangeFont 5 $HScrollBarFont
   ChangeFont 7 $HScrollBarFont
   ChangeFont 9 $HScrollBarFont

   # Obtain Key Binding Set
   Do {SendToModule KeyboardMgr SendString 1 1 $[infostore.kbd_bind_set]}

   # Key bindings
   Key Escape A 16 1 {KeyClose}
   Key Return M 13 1 {KeyApply}
   Key Return C 14 1 {KeySave}
   Key Q C 16 1 {KeyClose}
   Key F1 A 15 1 {KeyHelp}
   Key Help A 15 1 {KeyHelp}

   # X compositor bug workaround
   Set $RedrawSelf = (GetOutput {echo $NSCDE_REDRAW_WORKAROUND} 1 1)

   # Locale stuff: Hack to keep leading or ending space(s)
   # which will be trimmed by LocaleTitle Widget directive.
   ChangeLocaleTitle 3 (GetTitle 3)
End

Widget 1
Property
   Position 10 0
   Flags NoReliefString NoFocus
   Type ItemDraw
   Title {}
   Icon NsCDE/DtKeybd.l.pm
   Main
      Case message of
      SingleClic :
      Begin
      End
      1 :
      Begin
         Set $KeyBindingSet = (LastString)
         Set $InitialKeyBindingSet = $KeyBindingSet
         If $KeyBindingSet == {cua} Then
         Begin
            ChangeValue 11 1
         End
         If $KeyBindingSet == {nscde1x} Then
         Begin
            ChangeValue 11 2
         End
      End
End

Widget 2
Property
   Size 100 0
   Position 352 8
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Default}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         Do {Exec xset r on r rate 512 16 c 50}
         ChangeValue 3 1
         ChangeValue 5 512
         ChangeValue 7 16
         ChangeValue 9 50
         ShowWidget 4
         ShowWidget 5
         ShowWidget 6
         ShowWidget 7
         WarpPointer 14
         ChangeValue 11 1
      End
End

Widget 3
   Property
   Position 10 58
   Flags NoReliefString
   Type CheckBox
   Title { Auto Repeat                                }
   Font "xft:::pixelsize=14"
   Main
      Case message of
      SingleClic :
      Begin
         If (GetValue 3) == 0 Then
         Begin
            HideWidget 4
            HideWidget 5
            HideWidget 6
            HideWidget 7
         End
         Else
         Begin
            ShowWidget 4
            ShowWidget 5
            ShowWidget 6
            ShowWidget 7
         End
      End
End

Widget 4
   Property
   Position 8 106
   Size 106 0
   Flags NoReliefString NoFocus Right
   Type ItemDraw
   LocaleTitle {Start Delay}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 5
   Property
   Size 328 1
   Position 120 86
   Flags NoReliefString
   Type HScrollBar
   Font "xft:::pixelsize=14"
   MinValue 0
   MaxValue 10000
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 6
   Property
   Position 8 166
   Size 106 0
   Flags NoReliefString NoFocus Right
   Type ItemDraw
   LocaleTitle {Repeat Delay}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 7
   Property
   Size 328 1
   Position 120 146
   Flags NoReliefString
   Type HScrollBar
   Font "xft:::pixelsize=14"
   MinValue 0
   MaxValue 255
   Value 0
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 8
   Property
   Position 8 228
   Size 106 0
   Flags NoReliefString NoFocus Right
   Type ItemDraw
   LocaleTitle {Click Volume}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 9
   Property
   Size 328 1
   Position 120 208
   Flags NoReliefString
   Type HScrollBar
   Font "xft:::pixelsize=14"
   MinValue 0
   MaxValue 100
   Value 0
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 10
   Property
   Size 106 0
   Position 2 282
   Type ItemDraw
   Value 0
   Flags NoReliefString NoFocus Right
   LocaleTitle {Key Binding Set    }
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 11
   Property
   Size 500 2
   Position 150 278
   Type PopupMenu
   Value 0
   Flags NoReliefString
   Title { cua | nscde1x }
   Font "xft:::pixelsize=14"
   Main
      Case message of
      SingleClic :
      Begin
         # X compositor bug workaround
         If $RedrawSelf == 1 Then
         Begin
            ChangeValue 11 (GetValue 11)
         End

         If (GetValue 11) == 1 Then
         Begin
            Set $KeyBindingSet = {cua}
         End
         If (GetValue 11) == 2 Then
         Begin
            Set $KeyBindingSet = {nscde1x}
         End
      End
End

Widget 12
   Property
   Size 452 0
   Position 2 320
   Type Rectangle
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 13
   Property
   Size 100 0
   Position 10 332
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Apply}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 13 1
      End
      1 :
      Begin
         Do {Exec xset c } (GetValue 9)
         If (GetValue 3) == 1 Then
            Do {Exec xset r on r rate } (GetValue 5) { } (GetValue 7)
         Else
            Do {Exec xset r off}
      End
End

Widget 14
   Property
   Size 100 0
   Position 123 332
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Save}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 14 1
      End
      1 :
      Begin
         # Workaround for FvwmScript WriteToFile which truncates existing file if it
         # doesn't find script's signature in it.
         Set $CheckEntryCmd = {egrep -q '^#KeyboardMgr,[1-9]*' "} $FILE {"; echo $?}
         Set $CheckEntry = (GetOutput $CheckEntryCmd 1 -1)

         If $CheckEntry <> 0 Then
         Begin
            Set $PrepareFileCmd = {(echo '#KeyboardMgr,00000'; echo '#end') >> "} $FILE {"}
            Set $PrepareFile = (GetOutput $PrepareFileCmd 1 -1)
         End

         Do {Exec xset c } (GetValue 9)
         If (GetValue 3) == 1 Then
         Begin
            Do {Exec xset r on r rate }  (GetValue 5) { } (GetValue 7)
            WriteToFile $FILE {xset r on r rate }  (GetValue 5) { } (GetValue 7) { c } (GetValue 9)
         End
         Else
         Begin
            Do {Exec xset r off}
            WriteToFile $FILE {xset r off c } (GetValue 9)
         End
         Do {Exec chmod +x } $FILE

         If $InitialKeyBindingSet <> $KeyBindingSet Then
         Begin
            Set $CheckKeyBindingSet = (GetOutput {egrep -q '^([[:space:]]+)?InfoStoreAdd kbd_bind_set .*' "$FVWM_USERDIR/NsCDE.conf"; echo $?} 1 -1)
            If $CheckKeyBindingSet == 0 Then
            Begin
               Set $WriteKbdBindingSetCmd = {$NSCDE_TOOLSDIR/ised -c 's/^\([[:space:]]\+\)\?InfoStoreAdd kbd_bind_set\([[:space:]]\+\)\([[:alnum:]]\+\)\([[:space:]]\+\)\?\(#.*\)\?/\1InfoStoreAdd kbd_bind_set } $KeyBindingSet {\4\5/g' -f "$FVWM_USERDIR/NsCDE.conf"}
               Set $WriteKbdBindingSet = (GetOutput $WriteKbdBindingSetCmd 1 -1)
            End
            If $CheckKeyBindingSet == 1 Then
            Begin
               Set $WriteKbdBindingSetCmd = {/usr/bin/ksh -c 'echo -ne "\n# Key Binding Set\nInfoStoreAdd kbd_bind_set } $KeyBindingSet {\n" >> "$FVWM_USERDIR/NsCDE.conf"'}
               Set $WriteKbdBindingSet = (GetOutput $WriteKbdBindingSetCmd 1 -1)
            End

            Do {Restart}
         End

         Quit
      End
End

Widget 15
   Property
   Size 100 0
   Position 235 332
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Help}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 15 1
      End
      1 :
      Begin
         Do {f_DisplayURL "$[gt.Keyboard Style Manager]" html/NsCDE-KeyboardMgr.html}
      End
End

Widget 16
   Property
   Size 100 0
   Position 348 332
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Dismiss}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 16 1
      End
      1 :
      Begin
         Quit
      End
End

