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

UseGettext {$NSCDE_ROOT/share/locale;NsCDE-Subpanel:$NSCDE_ROOT/share/locale;NsCDE-Subpanels}
WindowTitle {Subpanel Settings}
WindowSize 370 270
Colorset 22

Init
Begin
   # Extract subpanel number from the end of subpanel's window resource name
   Set $SubpanelResName = (GetScriptArgument 1)
   Set $SubpanelNoCmd = {echo "} $SubpanelResName {" | sed 's/NsCDE-Subpanel//g'}
   Set $SubpanelNo = (GetOutput $SubpanelNoCmd 1 -1)

   If $SubpanelNo == {} Then
   Begin
      Quit
   End

   # Find out if we are going to read subpanel data from the system or user Subpanels.actions file
   Set $ChkActionsCmd = {egrep '^S} $SubpanelNo {,.*' "$FVWM_USERDIR/Subpanels.actions" 2>/dev/null}
   Set $ChkActions = (GetOutput $ChkActionsCmd 1 -1)
   If $ChkActions <> {} Then
   Begin
      Set $SubpanelActionsRead = {$FVWM_USERDIR/Subpanels.actions}
   End
   Else
   Begin
      Set $SubpanelActionsRead = {$NSCDE_DATADIR/defaults/Subpanels.actions}
   End
   Set $UserSubpanelActionsFile = {$FVWM_USERDIR/Subpanels.actions}
   Set $SystemSubpanelActionsRead = {$NSCDE_DATADIR/defaults/Subpanels.actions}

   # Fill subpanel parameters in variables
   Set $SubpanelDataCmd = {egrep "^S} $SubpanelNo {,(NAME|WIDTH|ENABLED)" "} $SubpanelActionsRead {" | sort | cut -d"," -f3}
   Set $SubpanelEnabled = (GetOutput $SubpanelDataCmd 1 -1)
   Set $SubpanelDisplayName = (GetOutput $SubpanelDataCmd 2 -1)
   Set $SubpanelDisplayLocalizedNameCmd = {echo '} $SubpanelDisplayName {' | $NSCDE_TOOLSDIR/ised -c 's/^\(\$\[gt\.\)\(.*\)\(\]\)/\2/g' -o -f -}
   Set $SubpanelDisplayLocalizedName = (GetOutput $SubpanelDisplayLocalizedNameCmd 1 -1)
   Set $SubpanelWidth = (GetOutput $SubpanelDataCmd 3 -1)

   # Fill discovered values into widgets and titles
   Set $WindowTitlePrint = (Gettext {Subpanel}) { } (Gettext $SubpanelDisplayLocalizedName) {: } (Gettext {Settings})
   ChangeWindowTitle $WindowTitlePrint
   ChangeTitle 1 $WindowTitlePrint
   ChangeTitle 5 (Gettext $SubpanelDisplayLocalizedName)
   ChangeTitle 8 $SubpanelWidth
   ChangeValue 11 $SubpanelEnabled

   If (GetValue 11) == 0 Then
   Begin
      Set $SubpanelInitiallyDisabled = 1
   End
   Else
   Begin
      Set $SubpanelInitiallyDisabled = 0
   End

   # Fill system defaults in variables - for resetting
   Set $SubpanelSysDataCmd = {egrep "^S} $SubpanelNo {,(NAME|WIDTH|ENABLED)" "} $SystemSubpanelActionsRead {" | sort | cut -d"," -f3}
   Set $SubpanelSysEnabled = (GetOutput $SubpanelSysDataCmd 1 -1)
   Set $SubpanelSysDisplayName = (GetOutput $SubpanelSysDataCmd 2 -1)
   Set $SubpanelSysWidth = (GetOutput $SubpanelSysDataCmd 3 -1)

   # Cursor on the end of input form please
   ChangeMinValue 5 9999
   ChangeMinValue 8 9999

   # Prepare regenerate command
   Set $GenerateFvwmButtonsCfg2Cmd = {"$NSCDE_TOOLSDIR/generate_subpanels" -w > "$FVWM_USERDIR/Subpanels.fvwm2.fvwmgen"}
   Set $GenerateFvwmButtonsCfg3Cmd = {"$NSCDE_TOOLSDIR/generate_subpanels" -f > "$FVWM_USERDIR/Subpanels.fvwm3.fvwmgen"}

   Set $ResetDone = 0

   Set $DefaultFont = (GetOutput {$NSCDE_TOOLSDIR/getfont -v -t normal -s medium -Z 11} 1 -1)
   ChangeFont 1 $DefaultFont
   ChangeFont 2 $DefaultFont
   ChangeFont 4 $DefaultFont
   ChangeFont 5 $DefaultFont
   ChangeFont 10 $DefaultFont
   ChangeFont 7 $DefaultFont
   ChangeFont 6 $DefaultFont
   ChangeFont 9 $DefaultFont
   ChangeFont 12 $DefaultFont
   ChangeFont 8 $DefaultFont

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

   # Key bindings
   Key Q C 15 1 {KeyClose}
   Key Return C 14 1 {KeyOk}
   Key Escape A 15 1 {KeyClose}
   Key F1 A 16 1 {KeyHelp}
   Key Help A 16 1 {KeyHelp}

   # Nasty workaround because ForeColor of the TextField
   # widget is not applied and global value is used anyway.
   # How it works: if wanted Colorset is applied with
   # ChangeColorset to (any?) other widget after initialization
   # of the script, colorset foreground color will be properly
   # applied to TextField widget too. Even if fake widget is
   # hidden, this workaround still works as it should. 
   # We define hidden PushButton widhet on the end of the script
   # and send string "workaround" to routine 1 of the TextField
   # widget ("1 : " can be attached to any widget in fact ...).
   # This routine than changes the colorset of the hidden
   # PushButton widget, and this then triggers change in buggy
   # TextField to be applied.
   # This bug can be seen when default colorset for the script has
   # different foreground color than colorset applied to the
   # TextField widget. Another possible workaround is to define
   # globally back/fore/sh/hi colors for the script instead of
   # (or after definition) of a colorset, but this is unflexible,
   # since script background cannot be changed dinamically or
   # from outside of the script as colorset functionality allows.
   Do {Schedule 50 SendToModule SubpanelSettings SendString 17 1 workaround}
   Do {Schedule 250 SendToModule SubpanelSettings SendString 17 1 workaround}
   Do {Schedule 350 SendToModule SubpanelSettings SendString 17 1 workaround}
End

Widget 1
   Property
   Position 6 10
   Size 320 20
   Type ItemDraw
   Flags NoReliefString Left NoFocus
   Title {Subpanel Settings: }
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 2
   Property
   Size 20 0
   Position 290 6
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Reset}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         # Make sure Subpanels.actions exists in $FVWM_USERDIR
         Set $ExistanceEnsureCmd = {[ ! -r ] "} $UserSubpanelActionsFile {" && touch "} $UserSubpanelActionsFile {"}
         Set $ExistanceEnsure = (GetOutput $ExistanceEnsureCmd 1 -1)

         Set $ResetCfCmd = {$NSCDE_TOOLSDIR/ised -c '/^S} $SubpanelNo {,.*/d' -f "} $UserSubpanelActionsFile {"}
         Set $ResetCf = (GetOutput $ResetCfCmd 1 -1)

         Set $WindowTitlePrint = {Subpanel } $SubpanelSysDisplayName { Settings}
         ChangeWindowTitle $WindowTitlePrint
         ChangeTitle 1 $WindowTitlePrint
         ChangeTitle 5 $SubpanelSysDisplayName
         ChangeTitle 8 $SubpanelSysWidth
         ChangeValue 11 $SubpanelSysEnabled

         # Stupid TextField colorset bug workaround
         Do {SendToModule SubpanelSettings SendString 17 1 workaround}
         Do {SendToModule SubpanelSettings SendString 17 1 workaround}

         Set $ChangeDetectString = $SubpanelDisplayName $SubpanelWidth $SubpanelEnabled

         Set $GenerateFvwmButtonsCfg2 = (GetOutput $GenerateFvwmButtonsCfg2Cmd 1 -1)
         Set $GenerateFvwmButtonsCfg3 = (GetOutput $GenerateFvwmButtonsCfg3Cmd 1 -1)
         Do {f_ReadCfg Subpanels}
         Do {Schedule 200 KillModule FvwmButtons } $SubpanelResName

         Set $ResetDone = 1
      End
End

Widget 3
   Property
   Size 358 168
   Position 6 42
   Type Rectangle
End

Widget 4
   Property
   Position 14 54
   Size 30 20
   Type ItemDraw
   Flags NoReliefString Left NoFocus
   LocaleTitle {Subpanel Name:}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 5
   Property
   Position 14 80
   Size 256 20
   Type TextField
   Flags NoReliefString Left
   Font "xft:::pixelsize=15"
   Title {}
   Colorset 20
   Main
      Case message of
      SingleClic :
      Begin
         Set $ResetDone = 0
      End
End

Widget 6
   Property
   Size 20 0
   Position 284 78
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Default}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         ChangeTitle 5 $SubpanelSysDisplayName

         # Stupid TextField colorset bug workaround
         Do {SendToModule SubpanelSettings SendString 17 1 workaround}
         Do {SendToModule SubpanelSettings SendString 17 1 workaround}
      End
End

Widget 7
   Property
   Position 16 124
   Size 30 20
   Type ItemDraw
   Flags NoReliefString Left NoFocus
   LocaleTitle {Subpanel Title List Width: }
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 8
   Property
   Position 222 120
   Size 48 20
   Type TextField
   Flags NoReliefString Left
   Title {}
   Font "xft:::pixelsize=15"
   Colorset 20
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 9
   Property
   Size 20 0
   Position 284 118
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Default}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         ChangeTitle 8 $SubpanelSysWidth

         # Stupid TextField colorset bug workaround
         Do {SendToModule SubpanelSettings SendString 17 1 workaround}
         Do {SendToModule SubpanelSettings SendString 17 1 workaround}
      End
End

Widget 10
   Property
   Position 16 164
   Size 30 20
   Type ItemDraw
   Flags NoReliefString Left NoFocus
   LocaleTitle {This Subpanel is Enabled: }
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 11
   Property
   Position 222 160
   Size 48 20
   Type CheckBox
   Flags NoReliefString Left
   Font "xft:::pixelsize=16"
   Title {}
   Main
      Case message of
      SingleClic :
      Begin
      End
End

Widget 12
   Property
   Size 20 0
   Position 284 158
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Default}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         ChangeValue 11 $SubpanelSysEnabled
      End
End

Widget 13
   Property
   Size 360 0
   Position 4 222
   Type Rectangle
End

Widget 14
   Property
   Size 90 0
   Position 10 232
   Flags NoReliefString
   Type PushButton
   LocaleTitle {OK}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 14 1
      End
      1 :
      Begin
         Set $ChangeFields = (GetTitle 5) (GetTitle 8) (GetValue 11)
         If $ChangeFields == $ChangeDetectString Then
         Begin
            Quit
         End

         If $ResetDone == 1 Then
         Begin
            Quit
         End

         Set $NewSubpanelEnabled = (GetValue 11)
         Set $NewSubpanelName = (GetTitle 5)
         Set $NewSubpanelWidth = (GetTitle 8)

         # Make sure Subpanels.actions exists in $FVWM_USERDIR
         Set $ExistanceOK = 0

         Set $ExistanceEnsureCmd = {egrep "^S} $SubpanelNo {,(NAME|WIDTH|ENABLED),.*" "} $UserSubpanelActionsFile{" 2>/dev/null | wc -l}
         Set $ExistanceEnsure = (GetOutput $ExistanceEnsureCmd 1 1)

         If $ExistanceEnsure == 3 Then
         Begin
            Set $WriteNewSubpanelConfCmd = {$NSCDE_TOOLSDIR/ised -c 's/^S} $SubpanelNo {,NAME,\(.*\)/S} $SubpanelNo {,NAME,} $NewSubpanelName
             {/g; s/^S} $SubpanelNo {,ENABLED,\(.*\)/S} $SubpanelNo {,ENABLED,} $NewSubpanelEnabled
             {/g; s/^S} $SubpanelNo {,WIDTH,\(.*\)/S} $SubpanelNo {,WIDTH,} $NewSubpanelWidth {/g' -f "} $UserSubpanelActionsFile {"}
            Set $WriteNewSubpanelConf = (GetOutput $WriteNewSubpanelConfCmd 1 -1)
            Set $ExistanceOK = 1
         End

         If $ExistanceEnsure == 0 Then
         Begin
            Set $WriteNewSubpanelConfCmd = {/usr/bin/ksh93 -c 'echo -ne "\nS} $SubpanelNo {,NAME,} $NewSubpanelName
             {\nS} $SubpanelNo {,ENABLED,} $NewSubpanelEnabled
             {\nS} $SubpanelNo {,WIDTH,} $NewSubpanelWidth {\n" >> "} $UserSubpanelActionsFile {"'}
            Set $WriteNewSubpanelConf = (GetOutput $WriteNewSubpanelConfCmd 1 -1)
            Set $AddTheRestFromSysCmd = {egrep ^S} $SubpanelNo {,ENTRY,.* "} $SystemSubpanelActionsRead {" >> "} $UserSubpanelActionsFile {"}
            Set $AddTheRestFromSys = (GetOutput $AddTheRestFromSysCmd 1 -1)
            Set $ExistanceOK = 1
         End

         If $ExistanceOK == 0 Then
         Begin
            Do {f_Notifier "Subpanel Settings" "Dismiss" "NsCDE/Error.xpm" "$[gt.Subpanel] } $SubpanelNo
             { $[gt.has errors in declaration. Edit] } $UserSubpanelActionsFile { $[gt.to fix it and then rerun this tool.]"}
         End

         Set $GenerateFvwmButtonsCfg2 = (GetOutput $GenerateFvwmButtonsCfg2Cmd 1 -1)
         Set $GenerateFvwmButtonsCfg3 = (GetOutput $GenerateFvwmButtonsCfg3Cmd 1 -1)

         If $GenerateFvwmButtonsCfg2 <> {} Then
         Begin
            Do {f_Notifier "Subpanel Settings" "Dismiss" "NsCDE/Error.xpm" "Unexpected output from generate_subpanels command: \\\"} $GenerateFvwmButtonsCfg2 {\\\""}
         End

         If $GenerateFvwmButtonsCfg3 <> {} Then
         Begin
            Do {f_Notifier "Subpanel Settings" "Dismiss" "NsCDE/Error.xpm" "Unexpected output from generate_subpanels command: \\\"} $GenerateFvwmButtonsCfg3 {\\\""}
         End

         # Case where disabled subpanel has been enabled
         If (GetValue 11) == 1 Then
         Begin
            If $SubpanelInitiallyDisabled == 1 Then
            Begin
               Do {f_ReadCfg Subpanels}
               Do {f_RestartFrontPanel}
               Quit
            End
         End

         # Case where enabled subpanel has been disabled
         If (GetValue 11) == 0 Then
         Begin
            If $SubpanelInitiallyDisabled == 0 Then
            Begin
               Do {f_ReadCfg Subpanels}
               Do {f_RestartFrontPanel}
               Quit
            End
         End

         # Rest functions
         Do {f_ReadCfg Subpanels}
         Do {Schedule 200 KillModule FvwmButtons } $SubpanelResName
         Quit
      End
End

Widget 15
   Property
   Size 90 0
   Position 138 232
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Close}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 15 1
      End
      1 :
      Begin
         Quit
      End
End

Widget 16
   Property
   Size 90 0
   Position 272 232
   Flags NoReliefString
   Type PushButton
   LocaleTitle {Help}
   Font "xft:::pixelsize=15"
   Main
      Case message of
      SingleClic :
      Begin
         SendSignal 16 1
      End
      1 :
      Begin
         Do {f_DisplayURL "$[gt.Subpanel Settings]" html/NsCDE-SubpanelSettings.html}
      End
End

# Nasty workaround because ForeColor of the TextField
# widget is not applied and global value is used anyway.
# See end of the Init procedure for explanation.
Widget 17
   Property
   Size 40 10
   Position 10 10
   Type PushButton
   Flags NoReliefString NoFocus Hidden
   Title {XXXXXXXXXXXXXXXXXXXXXXXXXXX}
   Colorset 20
   Main
      Case message of
      SingleClic :
      Begin
      End
      1 :
      Begin
         If (LastString) == {workaround} Then
         Begin
            ChangeColorset 5 20
            ChangeColorset 8 20
            ChangeColorset 17 20
         End
      End
End

