#-----------------------------------------------------------------------
# File:         Horizontal
# Version:      1.2.3
# Licence:      GPL 2
#
# Description:  Layout of object positions on desktop
#
# Author:       Thomas Funk <t.funk@web.de>    
#               Bernhard Popp <kermit.popp@googlemail.com>
#
# Created:      08/16/2012
# Changed:      05/28/2016
#-----------------------------------------------------------------------
#               Top
#
# +--------+        +--------+        +--------+
# |        |        |        |        |        |
# +--------+        +--------+        +--------+
# IconManager           Pager           Panel
#
#-----------------------------------------------------------------------

########################################################################
#                        Positioning
########################################################################

#=======================================================================
# Pager
#=======================================================================

#-----------------------------------------------------------------------
# Set the desk top size in units of physical screen size
#
#   +-----+-----+-----+-----+
#   |     |     |     |     |
#   +-----+-----+-----+-----+
#
#-----------------------------------------------------------------------
DeskTopSize "$[infostore.x_desks]x$[infostore.y_desks]"

#-----------------------------------------------------------------------
# actual pager height. Needed for the "top bar" modules also
#-----------------------------------------------------------------------
PipeRead "echo InfoStoreAdd pager_height `perl -e 'printf \"%.0f\",(-1.57E-6*$[vp.height]**2+0.017*$[vp.height]+17.929)'`"

#-----------------------------------------------------------------------
# actual pager width
#-----------------------------------------------------------------------
PipeRead "echo InfoStoreAdd pager_width `perl -e 'printf \"%.0f\",($[infostore.pager_height]*($[vp.width]/$[vp.height])*$[infostore.x_desks])+4'`"

#-----------------------------------------------------------------------
# FvwmPager geometry
#-----------------------------------------------------------------------
PipeRead 'echo InfoStoreAdd pager_geometry $[infostore.pager_width]x$[infostore.pager_height]+$(($[vp.width]/2-$[infostore.pager_width]/2))+0'


#=======================================================================
# FvwmButtons Panel
#=======================================================================

#-----------------------------------------------------------------------
# Panel width
#-----------------------------------------------------------------------
# the width composed of:
#        +---+-----------------------+-------+-------+
#        |   |                       |       |       |
#        +---+-----------------------+-------+-------+
#        menu stalonetray             cpumon  clock

#-----------------------------------------------------------------------
# set bar and tray icon size depending on the screen height
#-----------------------------------------------------------------------
PipeRead    'if [ "$[vp.height]" -gt "2800" ]; then \
                echo InfoStoreAdd i_size 48; \
            elif [ "$[vp.height]" -gt "2500" ]; then \
                echo InfoStoreAdd i_size 36; \
            elif [ "$[vp.height]" -gt "2000" ]; then \
                echo InfoStoreAdd i_size 32; \
            elif [ "$[vp.height]" -gt "1800" ]; then \
                echo InfoStoreAdd i_size 30; \
            elif [ "$[vp.height]" -gt "1600" ]; then \
                echo InfoStoreAdd i_size 28; \
            elif [ "$[vp.height]" -gt "1500" ]; then \
                echo InfoStoreAdd i_size 26; \
            elif [ "$[vp.height]" -gt "1050" ]; then \
                echo InfoStoreAdd i_size 25; \
            elif [ "$[vp.height]" -gt "640" ]; then \
                echo InfoStoreAdd i_size 24; \
            elif [ "$[vp.height]" -gt "580" ]; then \
                echo InfoStoreAdd i_size 22; \
            else \
                echo InfoStoreAdd i_size 20; \
            fi'


PipeRead "echo InfoStoreAdd column_size `perl -e 'printf \"%.0f\",$[infostore.i_size]/1.7'`"
#InfoStoreAdd column_size 15

# first 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 7; \
            elif [ "$[vp.width]" -gt "1400" ]; then \
                echo InfoStoreAdd tray_icons 6; \
            elif [ "$[vp.width]" -gt "1200" ]; then \
                echo InfoStoreAdd tray_icons 5; \
            else \
                echo InfoStoreAdd tray_icons 4; \
            fi'

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

# needed columns
PipeRead 'echo InfoStoreAdd columns $((2+$[infostore.tray_columns]+1+4+1+5))'

# panel width
PipeRead 'echo InfoStoreAdd panel_width $(($[infostore.columns]*($[infostore.column_size])))'


#-----------------------------------------------------------------------
# Panel geometry
#-----------------------------------------------------------------------
PipeRead 'echo InfoStoreAdd panel_geometry $[infostore.panel_width]x$[infostore.pager_height]-0+0'


#=======================================================================
# Icon Manager (WinList per Page)
#=======================================================================

#-----------------------------------------------------------------------
# GlobalIconMan geometry
#-----------------------------------------------------------------------
InfoStoreAdd iconman_button_geometry $[infostore.panel_width]x0

#-----------------------------------------------------------------------
# GlobalIconMan Manager Geometry
#-----------------------------------------------------------------------
PipeRead "echo InfoStoreAdd yStartIman `perl -e 'printf \"%.0f\",$[vp.height]-($[infostore.fontsize]+7)'`"
InfoStoreAdd xStartIman 0
InfoStoreAdd iconman_manager_geometry 1x0+$[infostore.xStartIman]-$[infostore.yStartIman]

#-----------------------------------------------------------------------
# GlobalIconMan Manager Titlebar position
#-----------------------------------------------------------------------
Style GlobalIconMan TitleAtRight

#-----------------------------------------------------------------------
# GlobalIconMan Manager Tips
#-----------------------------------------------------------------------
InfoStoreAdd tips_iman false


#=======================================================================
# Others
#=======================================================================

#-----------------------------------------------------------------------
# Define bands at the edge of the screen. It is used to compute the
# Working Area: EwmhBaseStruts left right top bottom 
#-----------------------------------------------------------------------
Schedule 2000 PipeRead 'echo EwmhBaseStruts 0 0 $(($[infostore.pager_height]+4)) 0'

#-----------------------------------------------------------------------
# IconBox defines the area where FVWM will place any windows that are
# in the iconic state: 
#   IconBox left top right bottom
#
# IconGrid ist the distance between each icon. Default grid is 3x3 pixels:
#   IconGrid x y
#
# IconFill defines the direction icons will place. Default is from left
# to right. To fill an icon box in columns instead of rows, specify the
# vertical direction (top or bottom) first.
#-----------------------------------------------------------------------
Style * IconBox 20 80 -100 -20, \
        IconGrid 40 40, \
        IconFill left bottom, \
        StaysOnBottom


#=======================================================================
# Load layout elements
#=======================================================================
Load modules/GlobalIconMan
Load modules/ShowHide_GlobalIconMan
Load modules/Pager
Load modules/HorizontalPanel


#=======================================================================
# Functions
#=======================================================================

AddToFunc StartFunction
# start event handler for GlobalIconMan with ShowHide
+ I Module FvwmEvent FvwmEventGlobalIconMan
#-----------------------------------------------------------------------
# start window list module
+ I Module FvwmIconMan GlobalIconMan
#-----------------------------------------------------------------------
# start Pager
+ I Module FvwmPager
#-----------------------------------------------------------------------
# start horizontal panel
+ I Module FvwmButtons HorizontalPanel
#-----------------------------------------------------------------------
# start systray applets
+ I Wait stalonetray
+ I FuncStartSystrayApplets


DestroyFunc ReloadLayoutParts
AddToFunc   ReloadLayoutParts
+ I FuncPrintDbgMsg ReloadLayoutParts
+ I PipeRead 'fns-built-trayapplist $FVWM_USERDIR/.systray.log $FVWM_USERDIR/.trayapplist'
+ I FuncUnsetDeskKeyBindings
+ I Load modules/HorizontalPanel
+ I FuncRestartModuleByAlias FvwmButtons HorizontalPanel
+ I Wait stalonetray
+ I Exec exec fns-start-programs reload_applets $[FVWM_USERDIR]/.trayapplist


#=======================================================================
# Bindings
#=======================================================================

#   Keyname    Context  Modifi      Function

#-----------------------------------------------------------------------
# Help info with SHIFT-CTRL-ALT P to show all page related key bindings
#-----------------------------------------------------------------------
key P           A       SCM         FuncShowMessage  "Page Key Bindings" \
"SHIFT-CTRL:" \
"Cursor right/left: Switch to page right/left" \
"1-n: Switch to page n" \
"SHIFT-ALT:" \
"Cursor right/left: Switch active window to page right/left" \
"1-n: Switch active window to page n" \
"ALT-CTRL:" \
"Cursor right/left/up/down: Move mouse pointer by 1% of a page" \
"SHIFT-ALT-CTRL:" \
"Cursor right/left/up/down: Scroll right/left by 10% of a page"
