#-----------------------------------------------------------------------
# File:         G2likeTopBar
# Version:      1.2.4
# Licence:      GPL 2
#
# Description:  Gnome like top bar with root menu, personal menu, systray,
#               cpu graph, clock and logout menu
#
# Author:       Thomas Funk <t.funk@web.de>    
#               Bernhard Popp <kermit.popp@googlemail.com>
#
# Created:      10/09/2012
# Changed:      06/04/2016
#-----------------------------------------------------------------------
#
# Topbar
# +---+---+--------+-----------------------------------+---------+---+
# |   |   |        |                  |       |        |         |   |
# +---+---+--------+-----------------------------------+---------+---+
# Menu     (Apps)                     Systray  cpu      Date    Logout
#    Personal


Style G2likeTopBar !Title, Sticky, NeverFocus, WindowListSkip, HandleWidth 2
Style G2likeTopBar !Iconifiable, !Maximizable, FixedSize, FixedPosition

#=======================================================================
# Calculations
#=======================================================================

#-----------------------------------------------------------------------
# Systray icons
#-----------------------------------------------------------------------
# the count of icons visible in stalonetray must set related to the
# screen resolution
PipeRead    'if [ "$[vp.width]" -gt "1600" ]; then \
                echo InfoStoreAdd tray_icons 12; \
            elif [ "$[vp.width]" -gt "1400" ]; then \
                echo InfoStoreAdd tray_icons 10; \
            elif [ "$[vp.width]" -gt "1200" ]; then \
                echo InfoStoreAdd tray_icons 8; \
            else \
                echo InfoStoreAdd tray_icons 6; \
            fi'

#-----------------------------------------------------------------------
# needed tray columns
#-----------------------------------------------------------------------
PipeRead 'echo InfoStoreAdd tray_columns $(($[infostore.tray_icons]*2))'

PipeRead "echo InfoStoreAdd empty_field_columns `perl -e 'printf \"%.0f\",$[infostore.columns]-$[infostore.tray_columns]-22'`"

PipeRead 'echo InfostoreAdd tray_max_x $(($[infostore.tray_icons]+1))'


#=======================================================================
# FvwmButtons TopBarG2
#=======================================================================

#-----------------------------------------------------------------------
# For cpu applet
#-----------------------------------------------------------------------
PipeRead 'echo InfostoreAdd graph_width $(($[infostore.column_size]*4))'

#InfoStoreAdd graph_voffset 9
PipeRead `perl -e 'if ($[vp.height] > 1080) { \
		 				printf "InfoStoreAdd graph_voffset %.0f\\n", \
		 				(2.281E-8*$[vp.height]**3-1.326E-4*$[vp.height]**2+0.28*$[vp.height]-157.047)/$[infostore.hratio]; \
		 		  } else { \
		 				printf "InfoStoreAdd graph_voffset %.0f\\n", \
		 				(-4.891E-7*$[vp.height]**2+0.021*$[vp.height]-2.7)/$[infostore.hratio]; \
		 		  }'`

#InfoStoreAdd percentage_voffset 44
PipeRead `perl -e 'if ($[vp.height] > 900) { \
		 				printf "InfoStoreAdd percentage_voffset %.0f\\n", \
		 				(5.71E-13*$[vp.height]**4-7.57E-9*$[vp.height]**3+3.498E-5*$[vp.height]**2-0.0716*$[vp.height]+67.317)*$[infostore.hratio]; \
		 		  } else { \
		 				printf "InfoStoreAdd percentage_voffset %.0f\\n", \
		 				(9.568E-10*$[vp.height]**4-2.973E-6*$[vp.height]**3+3.476E-3*$[vp.height]**2-1.844*$[vp.height]+409.991)*$[infostore.hratio]; \
		 		  }'`

#-----------------------------------------------------------------------
# For clock applet
#-----------------------------------------------------------------------
#InfoStoreAdd time_fsize 10
PipeRead "echo InfoStoreAdd time_fsize `perl -e \
	'printf \"%.0f\",(-5.313E-10*$[vp.height]**3+1.194E-5*$[vp.height]**2+4.031E-3*$[vp.height]+3.9)/$[infostore.hratio]'`"

#InfoStoreAdd time_voffset 2
PipeRead `perl -e 'if ($[vp.height] > 1200) { \
		 				print "InfoStoreAdd time_voffset 0\\n"; \
		 		  } else { \
		 				printf "InfoStoreAdd time_voffset %.0f\\n", \
		 				(2.58E-13*$[vp.height]**5-1.258E-9*$[vp.height]**4+2.393E-6*$[vp.height]**3-2.215E-3*$[vp.height]**2+0.991*$[vp.height]-167.165); \
		 		  }'`

#InfoStoreAdd time_hsize 5
PipeRead "echo InfoStoreAdd time_hsize `perl -e \
	'printf \"%.0f\",(-1.829E-13*$[vp.height]**4+1.028E-9*$[vp.height]**3-2.018E-6*$[vp.height]**2+1.624E-3*$[vp.height]+4.55)'`"

#InfoStoreAdd date_fsize 8
PipeRead `perl -e 'if ($[vp.height] > 768) { \
		 				printf "InfoStoreAdd date_fsize %.0f\\n", \
		 				(-3.445E-12*$[vp.height]**4+2.2213E-8*$[vp.height]**3-4.378E-5*$[vp.height]**2+0.0511*$[vp.height]-10.27)/$[infostore.hratio]; \
		 		  } else { \
		 				print "InfoStoreAdd date_fsize 7\\n"; \
		 		  }'`

#InfoStoreAdd date_voffset 3
PipeRead `perl -e 'if ($[vp.height] > 1400) { \
		 				printf "InfoStoreAdd date_voffset %.0f\\n", \
		 				(-3.746E-11*$[vp.height]**4+3.261E-7*$[vp.height]**3-1.017E-3*$[vp.height]**2+1.372*$[vp.height]-664.219)/$[infostore.hratio]; \
		 		  } elsif ($[vp.height] > 1024) { \
		 				printf "InfoStoreAdd date_voffset %.0f\\n", \
		 				(-2.96E-7*$[vp.height]**3+1.079E-3*$[vp.height]**2-1.28*$[vp.height]+504.448)/$[infostore.hratio]; \
		 		  } else { \
		 				printf "InfoStoreAdd date_voffset %.0f\\n", \
		 				(-2.016E-10*$[vp.height]**4+7.574E-7*$[vp.height]**3-1.001E-3*$[vp.height]**2+0.561*$[vp.height]-109.315)/$[infostore.hratio]; \
		 		  }'`

#-----------------------------------------------------------------------
# Find Personal icon
#-----------------------------------------------------------------------
PipeRead "if [ `fns-find-icon -n 'bookmark-new' -s $[infostore.i_size] -c 'actions' --notdesk` ]; then \
            echo InfoStoreAdd personal_icon `fns-find-icon -n 'bookmark-new' -s $[infostore.i_size] -c 'actions' --notdesk`; \
          else \
            echo InfoStoreAdd personal_icon 'home.svg:$[infostore.i_size]x$[infostore.i_size]'; \
          fi"

#-----------------------------------------------------------------------
# find logout icon
#-----------------------------------------------------------------------
PipeRead "if [ `fns-find-icon -n 'system-log-out' -s $[infostore.i_size] -c 'actions' --notdesk` ]; then \
            echo InfoStoreAdd logout_icon `fns-find-icon -n 'system-log-out' -s $[infostore.i_size] -c 'actions' --notdesk`; \
          else \
            echo InfoStoreAdd logout_icon 'logout.svg:$[infostore.i_size]x$[infostore.i_size]'; \
          fi"


#=======================================================================
# Panel
#=======================================================================

#***********************************************************************
# General
#***********************************************************************
DestroyModuleConfig G2likeTopBar: *
*G2likeTopBar: Geometry         $[infostore.topbar_geometry]
*G2likeTopBar: Colorset         13
*G2likeTopBar: ActiveColorset   13
*G2likeTopBar: PressColorset    13
*G2likeTopBar: Font          	"xft:$[infostore.used_font]:pixelsize=$[infostore.fontsize]"

*G2likeTopBar: Rows             1
*G2likeTopBar: Columns          $[infostore.columns]
*G2likeTopBar: Frame            0

#***********************************************************************
# Buttons
#***********************************************************************

#-----------------------------------------------------------------------
# menu button
#-----------------------------------------------------------------------
*G2likeTopBar: (6x1, Colorset 13, ActiveColorset 11, \
            Left, Padding 5 0, Title (Side) "$[gt.Menu]", ActionOnPress, \
            Icon    icon_fns.svg:$[infostore.i_size]x$[infostore.i_size], \
            ActiveIcon    icon_fns.svg:$[infostore.i_size]x$[infostore.i_size], \
            Action(Mouse 1) `Menu MenuRoot`)

#-----------------------------------------------------------------------
# personal menu button
#-----------------------------------------------------------------------
*G2likeTopBar: (7x1, Colorset 13, ActiveColorset 11, \
            Left, Padding 5 0, Title (Side) "$[gt.Personal]", ActionOnPress, \
            Icon    $[infostore.personal_icon], \
            ActiveIcon    $[infostore.personal_icon], \
            Action(Mouse 1) `Menu MenuFvwmPersonal`)

#-----------------------------------------------------------------------
# empty field
#-----------------------------------------------------------------------
*G2likeTopBar: ($[infostore.empty_field_columns]x1, Colorset 13)

#-----------------------------------------------------------------------
# Systray
#-----------------------------------------------------------------------
*G2likeTopBar: ($[infostore.tray_columns]x1, Right, Colorset 13, \
            Swallow (UseOld, Close, Kill, Respawn) "stalonetray" \
            `Exec exec stalonetray --geometry 1x1 --max-geometry $[infostore.tray_max_x]x1 \
            --scrollbars horizontal --scrollbars-highlight $[fg.cs13] \
            --grow-gravity E --icon-gravity W --kludges force_icons_size -i $[infostore.i_size] \
            --parent-bg true -d none --log-level info 2> $[FVWM_USERDIR]/.systray.log`)

#-----------------------------------------------------------------------
# find conky and its version
#-----------------------------------------------------------------------
PipeRead "if [ `which conky` ]; then \
            if [ `conky -v |head -1|cut -d'.' -f2` -ge 10 ]; then \
                echo InfoStoreAdd conky_version conky1X; \
            else \
                echo InfoStoreAdd conky_version conky0X; \
            fi; \
          else \
            echo InfoStoreAdd conky_version conky0X; \
          fi"

#-----------------------------------------------------------------------
# find conky config for cpu applet
#-----------------------------------------------------------------------
PipeRead "if [ `fns-find-file -p $FVWM_USERDIR/layouts -r -s $[infostore.conky_version]rc_clock` ]; then \
            echo InfoStoreAdd conky_rc_path $FVWM_USERDIR; \
          else \
            echo InfoStoreAdd root_dir $FNS_SYSTEMDIR; \
          fi"

#-----------------------------------------------------------------------
# Cpuview
#-----------------------------------------------------------------------
*G2likeTopBar: (5x1, Colorset 13, Swallow (Close, Kill, Respawn) "conky_cpu" \
            `Exec exec conky -t '${color $[fg.cs13]}${voffset -$[infostore.graph_voffset]}${cpugraph \
            $[infostore.bar_height],$[infostore.graph_width] 71fb06 fb0634 -t}\\n${font \
            $[infostore.used_font]:size=$[infostore.date_fsize]}${voffset -$[infostore.percentage_voffset]}$alignc${cpu cpu0}%' \
            -c $[infostore.root_dir]/layouts/rc-files/$[infostore.conky_version]rc_cpu -p 2`, \
            Action (mouse 3) `Menu MenuCpuOps`,\
            Action (mouse 0) Nop)

#-----------------------------------------------------------------------
# find conky config for clock applet
#-----------------------------------------------------------------------
PipeRead "if [ `fns-find-file -p $FVWM_USERDIR/layouts -r -s $[infostore.conky_version]rc_cpu` ]; then \
            echo InfoStoreAdd root_dir $FVWM_USERDIR; \
          else \
            echo InfoStoreAdd root_dir $FNS_SYSTEMDIR; \
          fi"

#-----------------------------------------------------------------------
# Clock
#-----------------------------------------------------------------------
*G2likeTopBar: ($[infostore.time_hsize]x1, Colorset 13, Swallow (Close, Kill, Respawn) "conky_clock" \
            `Exec exec conky -t '${color $[fg.cs13]}${font $[infostore.used_font]:bold:size=$[infostore.time_fsize]}${voffset \
            -$[infostore.time_voffset]}$alignc${time %H:%M}\\n${font $[infostore.used_font]:size=$[infostore.date_fsize]}${voffset \
            -$[infostore.date_voffset]}$alignc${time %a %e %b}' \
            -c $[infostore.root_dir]/layouts/rc-files/$[infostore.conky_version]rc_clock -p 1`, \
            Action (mouse 3) `Menu MenuClockOps`,\
            Action (mouse 0) Nop)

#-----------------------------------------------------------------------
# logout button
#-----------------------------------------------------------------------
*G2likeTopBar: (2x1, Colorset 13, ActiveColorset 11, ActionOnPress, \
            Icon    $[infostore.logout_icon], \
            ActiveIcon    $[infostore.logout_icon], \
            Action(Mouse 1) `Menu MenuFvwmQuit`)

