G4C ; <= Don't delete this, enter a space, or insert a empty line before it!!!


;  O====================================O
;
;     $VER: MemView v1.18 (29.11.2001)
;
;  O====================================O


WinBig 0 0 132 39 ""             ; Window-Size (for the BIG one's)
WinType 00001000                 ; Window-Attributes
WinFont XHelvetica.font 9 000    ; A Extra-Small Window-Font
VarPath "*"                      ; Use Global-Variables
SCREEN WORKBENCH                 ; Open Window on Workbench Screen
;WINBACKGROUND IMAGE mv_main 0

; Routines
xroutine check_colors
   ifexists screen ~Workbench
      flash
      ezreq "Could not open window..." "Argh" ""
      guiquit #this
      stop
   endif

   .test = ""
   append env:.test "G4C\n\nSCREEN WORKBENCH\n\n"
   append env:.test "xonload\n\tguiopen #this\n\n"
   append env:.test "xonquit\n\tdelete env:.test"

   guiload env:.test

   info gui .test
   col = $$SCREEN.COLORS
   guiquit .test
   if $col < 8
      flash
      ezreq "Your Workbench has only $col colours\n\nPlease open a Workbench Screen\nwith more than 8 colors!" "Ok, i will make it!" ""
      guiquit MemView
   else
      gosub #this load
   endif


; Loading Prefs-File
xroutine load              
   ifexists file env:MemView.prefs
      guiload env:MemView.prefs
      gosub #this run
   else
      flash
      ezreq "Please use MemViewPrefs\nto config MemView!" "Open MemViewPrefs" ""
      gosub #this readtt
      gosub #this open_prefs
   endif


xroutine readtt
   ; Default Settings
   SHOWMEM = USED
   PREFS = "MemView:MemViewPrefs"

   ;Read Tooltypes
   ttget :MemView

   ; Setting the switch
   docase $SHOWMEM
     case = USED
            *switch = 0
            break
     case = FREE
            *switch = 1
            break
   endcase


xroutine run
   set margins 5/0/0       ; Setting Title-Margins

   ; Try to selflocate
   ifexists assign ~MemView
      extract #this guipath mvpath
      assign MemView: $mvpath
   endif

   ; Get the Tooltypes
   gosub #this readtt

   ; Loading the Backdrop-Pattern
   loadimage MemView:MemView.back_1 mv_main WORKBENCH
   loadimage MemView:MemView.back_2 mv_inactive_main WORKBENCH

   changeimage #this 99 -1 -1 mv_main

   ; Version-Strings
   prog = MemView
   vers = v1.18
   date = 26.11.2001
   name = Nightshade
   mail = thenightshade@freenet.de

   *openprefs = 0

   ; Reading-Out your maximum available Chip/Fast/Total Memory
   cli "c:avail >env:.mem"
   ch = $.mem
   cnt = 0
   while $cnt <= 2
   cutvar ch cut line -1 cline
   ++cnt
   endwhile
   cutvar cline cut word -2 maxc
   cutvar maxc cut word 1 tc

   fa = $.mem
   cnt = 0
   while $cnt <= 1
   cutvar fa cut line -1 fline
   ++cnt
   endwhile
   cutvar fline cut word -2 maxf
   if $maxf != 0
      cutvar maxf cut word 1 tf
   else
      flash
      Ezreq "You don't have any FastRAM,\nor you have running NoFastMem.\n\nPlease get some FastRAM,\nor deactivate NoFastMem!" "Ok, i will do it!" ""
      guiquit #this
      stop
   endif

   to = $.mem
   cutvar to cut line -1 tline
   cutvar tline cut word -2 maxt
   cutvar maxt cut word 1 tt

   ; Always by tidy, says my mother
   delvar maxc
   delvar cline
   delvar ch
   delvar cnt
   delvar maxf
   delvar cnt
   delvar fline
   delvar fa
   delvar to
   delvar tline
   delvar maxt

   delete env:.mem

   stopped = 0             ; xTimer is running? 0 = no, 1 = yes
   setgad #this 100 on     ; Start the xTimer

   ; Read Memory
   gosub #this read_mem

   ; Open Window
   guiopen #this

   ; Re-Setting Window-Position
   gosub #this set_winpos $prog


xroutine set_winpos win
   ; Read the Screen-Proportions
   info gui $win

   sh = $$SCREEN.H
   sw = $$SCREEN.W

   ; Read-Out Window-Dimention
   wh = $$WIN.H
   ww = $$WIN.W

   ; Calc the new Window-Position
   l == $sw - $ww
   t == $sh - $wh

   ; Setting-Up the new Window-Position
   nl == $l - $xspace
   nt == $t - $yspace
  
   
   ; Move the Window on the new Position
   guiclose $win
   changearg $win 0 0 $nl
   changearg $win 0 1 $nt
   guiopen $win


xroutine showmem mem type txt
   if $mem >= 75
      gad = Excellent
   elseif $mem >= 50
      gad = Good
   elseif $mem >= 25
      gad = Poor
   elseif $mem <= 24
      gad = Bad
   endif

   guiwindow #this wait
   gosub #this mv_inactive
   ezreq "$type Memory Usage:\n\n$txt" $gad ""
   guiwindow #this resume


; Read the memory and change the color-indicators
xroutine read_mem          
   ; Re-Calc the memory (very important)
   chip  = $(($$MEM.CHIP * 100) / $tc)
   fast  = $(($$MEM.FAST * 100) / $tf)
   total = $(($$MEM.ALL * 100) / $tt)

   usedchip = $($tc - $$MEM.CHIP)
   usedfast = $($tf - $$MEM.FAST)
   usedtotal = $($tt - $$MEM.ALL)

   lchip = $(($$MEM.CLARGE * 100) / $tc)
   lfast = $(($$MEM.FLARGE * 100) / $tf)

   larall = $($$MEM.CLARGE + $$MEM.FLARGE)
   ltotal = $(($larall * 100) / $tt)


   ; Cut chipmem
   if $chip < 100
   and $chip >= 10
      cut = 2
   endif
   if $chip = 100
      cut = 3
   endif
   if $chip < 10
      cut = 1
   endif

   cutvar chip cut char $cut chip


   ; cut fastmem
   if $fast < 100
   and $fast >= 10
      cut = 2
   endif
   if $fast = 100
      cut = 3
   endif
   if $fast < 10
      cut = 1
   endif

   cutvar fast cut char $cut fast


   ; Cut totalmem
   if $total < 100
   and $total >= 10
      cut = 2
   endif
   if $total = 100
      cut = 3
   endif
   if $total < 10
      cut = 1
   endif

   cutvar total cut char $cut total


   ; Cut largest chipmem
   if $lchip < 100
   and $lchip >= 10
      cut = 2
   endif
   if $lchip = 100
      cut = 3
   endif
   if $chip < 10
      cut = 1
   endif

   cutvar lchip cut char $cut lchip


   ; Cut largest fastmem
   if $lfast < 100
   and $lfast >= 10
      cut = 2
   endif
   if $lfast = 100
      cut = 3
   endif
   if $lfast < 10
      cut = 1
   endif

   cutvar lfast cut char $cut lfast


   ; Cut largest totalmem
   if $ltotal < 100
   and $ltotal >= 10
      cut = 2
   endif
   if $ltotal = 100
      cut = 3
   endif
   if $ltotal < 10
      cut = 1
   endif

   cutvar ltotal cut char $cut ltotal


   ; Cut used chipmem
   if $uchip < 100
   and $uchip >= 10
      cut = 2
   elseif $uchip = 100
      cut = 3
   elseif $uchip < 10
      cut = 1
   endif

   cutvar uchip cut char $cut uchip


   ; cut used fastmem
   if $ufast = 100
   and $ufast >= 10
      cut = 2
   endif
   if $ufast = 100
      cut = 3
   endif
   if $ufast < 10
      cut = 1
   endif

   cutvar ufast cut char $cut ufast


   ; Cut used totalmem
   if $utotal < 100
   and $utotal >= 10
      cut = 2
   endif
   if $utotal = 100
      cut = 3
   endif
   if $utotal < 10
      cut = 1
   endif

   cutvar utotal cut char $cut utotal


   ; Set colors for chipmem
   if $chip > 50
      gcol1 = $green
   endif
   if $chip <= 50
      gcol1 = $yellow
   endif
   if $chip <= 25
      gcol1 = $red
   endif

   ; Set color for fastmem
   if $fast > 50
      gcol2 = $green
   endif
   if $fast <= 50
      gcol2 = $yellow
   endif
   if $fast <= 25
      gcol2 = $red
   endif

   ; Set color for totalmem
   if $total > 50
      bkcol = $green
   endif
   if $total <= 50
      bkcol = $yellow
   endif
   if $total <= 25
      bkcol = $red
   endif

   uchip = $(100 - $chip)
   ufast = $(100 - $fast)
   utotal = $(100 - $total)

   ;changearg #this 200 4 $bkcol
   setattr #this/200 tbox 2/1/$bkcol\/button/in

   ; Update the Gauges & change the Colors
   if $switch = 0
      update #this 1 $chip
      update #this 2 $fast
   else
      update #this 1 $uchip
      update #this 2 $ufast
   endif

   changearg #this 1 6 $gcol1
   changearg #this 2 6 $gcol2
   changearg #this 1 7 $back
   changearg #this 2 7 $back
   partredraw #this 0 0 0 0


   ; Change the text of Requester-Body
   cmem = "Free: $$MEM.CHIP ($chip\%%)\nUsed: $usedchip ($uchip\%%)\n\nLargest: $$MEM.CLARGE ($lchip\%%)"
   fmem = "Free: $$MEM.FAST ($fast\%%)\nUsed: $usedfast ($ufast\%%)\n\nLargest: $$MEM.FLARGE ($lfast\%%)"
   tmem = "Free: $$MEM.ALL ($total\%%)\nUsed: $usedtotal ($utotal\%%)\n\nLargest: $larall ($ltotal\%%)"


xroutine quit              ; Quitting GUI :,-((
   guiwindow #this wait
      gosub #this mv_inactive
      ezreq "Do you want to quit $prog\?" "Yes, please!|No, thanks!" quit
         if $quit = 1
            guiquit #this
         endif
   guiwindow #this resume


xroutine stop_timer
   guiwindow #this wait
   if $stopped = 0
      setgad #this 100 off
      gosub #this mv_inactive
      ezreq "Timer has been stopped!\n\nTo restart, press the S-Key again!" "Close" ""
      stopped = 1
   elseif $stopped = 1
      setgad #this 100 on
      gosub #this mv_inactive
      ezreq "Timer restarted!" "Thanks!" ""
      stopped = 0
   endif
   guiwindow #this resume


xroutine open_prefs
   *openprefs = 1
   ifexists file $PREFS
      guiload $PREFS
   else
      flash
      guiwindow #this wait

      ezreq "Could not open\n$PREFS\n\nMaybe the wrong path\nentered in Tooltypes?" "I will check it!" ""
      guiwindow #this resume
   endif


xroutine flush_mem
   cli "c:avail flush >nil:"
   gosub #this read_mem


xroutine switch
   if $switch = 0
      *switch = 1
   else
      *switch = 0
   endif
   gosub #this read_mem


xroutine show_mem
   cli "c:avail >env:.availmem"
   guiwindow #this wait
   gosub #this mv_inactive
      ezreq "$.availmem" "OK" ""
      delete env:.availmem
   guiwindow #this resume


xroutine about
   guiwindow #this wait
   gosub #this mv_inactive
   ezreq "$prog $vers ($date)\nEmailware © 2001 by $name\n($mail)" "Nice" ""
   guiwindow #this resume

xroutine mv_inactive
   changeimage #this 99 -1 -1 mv_inactive_main
   partredraw #this 0 0 0 0
   win = 0

xroutine mv_active
   changeimage #this 99 -1 -1 mv_main
   partredraw #this 0 0 0 0
   win = 1

; GUI-Events
xTimer REPEAT 0:0:05 off   ; Do it every 2 seconds
   gosub #this read_mem    ; Re-Read Memory
      gadid 100            ; Timers ID-Number


xOnLoad
   gosub #this check_colors

xonopen
   win = 0

xonQuit  ; quitting
   ; Delete global variables
   delvar *xspace
   delvar *yspace
   delvar *back
   delvar *green
   delvar *yellow
   delvar *red
   delvar *switch
   delvar *openprefs

   ; Turn-off timer
   setgad #this 100 off

   ; Quitting all GUIs :,-((
   guiquit .test
   GuiQuit MV_MenuShadow
   GuiQuit MV_PopupMenu
   GuiQuit #This

   ; Free Image
   freeimage mv_main
   freeimage mv_inactive_main


xonRMB ; Click the Right Mouse Button to open the Popup-Menu
   guiopen MV_MenuShadow
   guiopen MV_PopupMenu



; Key Events
xonkey a    ; Hit the A-Key for About-Requester
   gosub #this about

xonkey f    ; Hit the F-Key to flush Memory
   gosub #this flush_mem

xonkey m    ; Hit the M-Key to show the available Memory
   gosub #this show_mem

xonkey p    ; Hit the P-Key to open MemViewPrefs
   gosub #this open_prefs

xonkey q    ; Hit the Q-Key for quitting
   gosub #this quit

xonkey s    ; Hit the S-Key to stop/restart the Timer
   gosub #this stop_timer

xonkey t    ; Hit the T-Key to toggle between Free/Used Memory
   gosub #this switch


xoninactive
   ifexists window ~MV_PopupMenu
   and $win = 0
      changeimage #this 99 -1 -1 mv_inactive_main
      partredraw #this 0 0 0 0
      win = 1
   endif

xonactive
   ifexists window ~MV_PopupMenu
   and $win = 1
      changeimage #this 99 -1 -1 mv_main
      partredraw #this 0 0 0 0
      win = 0
   endif

; The GUI themself
image 0 0 ""
gadid 99


BOX -1 -1 0 0 out button
box -1 -1 126 33 in ridge


; The LED (Shows the Total Memory)
xtextbox 110 14 12 12 "" ""
   gadid 200


; The first Gauge shows the ChipRAM, the otherone shows the FastRAM
GAUGE 32 7 72 11 OUT BUTTON 0 1 100
   attr title "2/1/outline/lside/middle/Chip:"
      gadid 1

GAUGE 32 21 72 11 OUT BUTTON 0 1 100
   attr title "2/1/outline/lside/middle/Fast:"
      gadid 2


; Clicking on the Gauges or the LED, brings up the Memory-Usage
; Click in the upperleft corner to quitting MemView
xarea 0 0 5 5 none
   gosub #this quit

xarea 32 7 72 11 none
   gosub #this showmem $chip Chip $cmem

xarea 33 21 72 11 none
   gosub #this showmem $fast Fast $fmem

xarea 110 14 12 12 none
   gosub #this showmem $total Total $tmem



newfile MV_PopupMenu
winbig 0 0 48 103 ""
wintype 00001000
winfont xhelvetica.font 9 000
winonwin MV_MenuShadow -2 -2

xonkey #R
   gosub #this close

xonkey #V
   gosub #this close


xonrmb
   gosub #this close


xbefore
   if $menu != $bar
   and $menu != $dpr
      gosub #this close
   endif


xoninactive
   gosub #this close


xroutine close
   guiclose MV_MenuShadow
   guiclose #this


xonopen
   bar = "[31;8m~~~~~~~[0m"
   dpr = "[31;8m[P]refs[0m"

   lvuse #this 1
   ifexists window ~MemViewPrefs
      lvadd "[P]refs"
   else
      lvadd "$dpr"
   endif
   lvadd "$bar"
   lvadd "[F]lush"
   lvadd "[M]emory"
   lvadd "$bar"
   if $MemView/stopped = 0
      lvadd "[S]top"
   else
      lvadd "[S]tart"
   endif
   lvadd "[T]oggle"
   lvadd "$bar"
   lvadd "[A]bout"
   lvadd "$bar"
   lvadd "[Q]uit"


xonclose
   lvuse #this 1
   lvclear


xlistview 0 0 64 0 "" menu "" 0 txt
   docase $menu
     case = "[P]refs"
            gosub MemView open_prefs
            break
     case = "[F]lush"
            gosub MemView flush_mem
            break
     case = "[M]emory"
            gosub MemView show_mem
            break
     case = "[A]bout"
            gosub MemView about
            break
     case = "[T]oggle"
            gosub MemView switch
            break
     case = "[S]top"
     case = "[S]tart"
            gosub MemView stop_timer
            break
     case = "[Q]uit"
            gosub MemView quit
            break
   endcase
   attr lvstyle 2031
   gadid 1


newfile MV_MenuShadow
winbig 0 0 48 103 ""
wintype 00001000
winbackground pattern 0 1
winfont xhelvetica.font 9 000
winonmouse 21 14












; Am i a good programmer, or what?!
;      (since July 2000)
;           + + +
; Amiga make dreams comes true!!!
;           + + +
; Okay, but i'm finished, really!
;           + + +
; ... ... ... ... ... ... ... ...
; ... ... ... ... ... ... ... ...
; ... ... ... ... ... ... ... ...
;           + + +
; Argh, you're still reading!?
;           + + +
; Please do everything else,
; but stop to reading this
; stupid text!!!!!!!!!!!!!!!
;           + + +
; ----- T H E   E N D ! -----
