home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 6 / 06.iso / b / b094 / 4.img / COLORCEL.L!S / COLORCEL.L!S
Encoding:
Text File  |  1993-02-22  |  14.5 KB  |  433 lines

  1. '<<Name>> Globals : System Use Only
  2.  
  3. '*    Copyright (c) Lotus Development Corporation 1990, 1991, 1992, 1993
  4.  
  5. '************Globals : System Use Only************************************
  6. '* This script is the main Module for Color Cells 
  7. '* It should only be run once, and it managed by main Color Cells script
  8. '* When run, this script declares the functions that:
  9. '*     (1) manage the Dialog Box - put it up, and retrieve user settings
  10. '*     (2) Determine what cells the "current selection" refers to, i.e.,
  11. '*               if formula selection, use cells calculated by the formula
  12. '*               if item selection, use cells in the item(s)
  13. '*     (3) Take action based on the user's dialog box selections
  14. '*        (4) Actually color each cell based on its value
  15. '******************************************************************************
  16.  
  17. MODULE ColorCells
  18.  
  19. LIBRARY MYTRIG("colortrg.dll")
  20. DECLARE GLOBAL Function RegisterColorRecalc LIB MYTRIG () as INTEGER
  21. DECLARE GLOBAL Function UnregisterColorRecalc LIB MYTRIG () as INTEGER
  22.  
  23. BOOLEAN alreadyregistered?
  24. alreadyregistered?=FALSE
  25. BOOLEAN ranonce?
  26.  
  27. string ltcond
  28. string gtcond
  29. string eqcond
  30.  
  31. integer redv%,bluev%,yellowv%,magentav%,cyanv%,blackv%, bold%
  32.  
  33. integer logand%    ' global variable to hold value of "logical AND" (if 0, "OR" is used)
  34. integer trig%    
  35. integer clr          ' to hold color user chooses from dialog
  36.  
  37. const BLACK=255
  38. const RED=128
  39. const YELLOW=50
  40. const BLUE=168
  41. const MAGENTA = 124
  42. const GREEN=68
  43. const CYAN=70
  44.  
  45.  
  46. const LTCTRL=8000    ' all the edit controls and buttons as they are assigned in the color.dlg file.
  47. const GTCTRL=8001
  48. const EQCTRL=8002
  49. const REDBTN=23
  50. const BLUEBTN=24
  51. const YELLOWBTN=25
  52. const MAGENTABTN=26
  53. const CYANBTN=27
  54. const BLACKBTN=28
  55. const BOLDBTN=31
  56.  
  57. const ANDBTN=29
  58. const ORBTN=30
  59.  
  60. const TRIGRECALC=32
  61.  
  62. const OK=1
  63. const CANCEL = 2
  64.  
  65. textval$ = string$(100," ") ' this is the variable to contain text values of cells, in case user wants to color the cell if EQUAL to a string
  66.                     ' Handle cell values with strings up to 100 characters
  67. OPTION GLOBAL    ' all function definitions below will be globally accessible
  68.  
  69. Function CreateCellSelection(cellsel%) as BOOLEAN
  70.  
  71. ' this is called only when original selection is in the datapane
  72.     r%=ModifySelHandle(cellsel%,"",sel_CELLSONLY)     ' try to create a cell selection out of it,if at all possible
  73.     SetSelection(cellsel%)
  74.     GetIntProperty(cellsel%,IT_SELTYPE, seltype&)
  75. ' each of the following types are all valid cell selections
  76.     if seltype&=sel_CELLSONLY  or seltype&=sel_REFINED or seltype&=sel_CELL  then
  77.         CreateCellSelection = TRUE
  78.         else
  79.         CreateCellSelection=FALSE
  80.     end if
  81. end Function
  82.  
  83.  
  84.  
  85. Function PutUpDialog as BOOLEAN
  86.  
  87. 'this function fills the dialog and gets resulting values into global variables
  88. if NOT ranonce? then
  89. FillButton(REDBTN,1) ' dialog defaults to "Red if Negative" the first time
  90.  
  91. ' Just to be sure, make sure all the others are "off"
  92. for button=REDBTN+1 to BLACKBTN    ' this is dependent on the button control numbers being consecutive
  93. FillButton(button,0)
  94. next button
  95.  
  96. FillButton(BOLDBTN,0)
  97. FillText(LTCTRL,"0")
  98. ' Just to be sure, fill the other text fields with nothing
  99. FillText(GTCTRL,"")
  100. FillText(EQCTRL,"")
  101.  
  102. FillButton(TRIGRECALC,0) ' don't check this box
  103.  
  104. FillButton(ANDBTN,1)    ' to default to "OR" case, set this global var to 0 during initialization
  105. FillButton(ORBTN,0)
  106.  
  107. end if
  108.  
  109. DialBox("colorcel.dlg","colorcond",res%) ' since no path is given, scripts path will be searched.
  110.  
  111. if res%=OK then 
  112. PutUpDialog=TRUE 
  113. ranonce?=TRUE
  114.  
  115. ltcond=string$(100," ") ' to contain the value of the "less than" number typed into the dialog
  116. gtcond=string$(100," ") ' to contain the value of the "greater than" number
  117. eqcond=string$(100," ") ' to contain the value of the "equals" number or string
  118.  
  119.  
  120. 'get color         
  121.     GetDialogState(REDBTN,redv%)
  122.     FillButton(REDBTN,redv%)        'Fill it for next time
  123.     GetDialogState(BLUEBTN,bluev%)
  124.     FillButton(BLUEBTN,bluev%)
  125.     GetDialogState(YELLOWBTN,yellowv%)
  126.     FillButton(YELLOWBTN,yellowv%)
  127.     GetDialogState(MAGENTABTN,magentav%)
  128.     FillButton(MAGENTABTN,magentav%)
  129.     GetDialogState(CYANBTN,cyanv%)
  130.     FillButton(CYANBTN,cyanv%)
  131.     GetDialogState(BLACKBTN,blackv%)
  132.     FillButton(BLACKBTN,blackv%)
  133.     select case 1
  134.     case redv%:
  135.          clr=RED 
  136.     case bluev%:
  137.         clr=BLUE
  138.     case yellowv%:
  139.         clr=YELLOW
  140.     case magentav%:
  141.         clr=MAGENTA
  142.     case cyanv%:
  143.         clr=CYAN
  144.     case blackv%:
  145.         clr=BLACK
  146.     case else:
  147.         print "No color chosen" ' should never happen
  148.     end select
  149.  
  150. GetDialogState(BOLDBTN,bold%)     'Bold box checked?
  151. FillButton(BOLDBTN,bold%)        ' set state for next time
  152.  
  153. GetDialogState(TRIGRECALC,trig%) ' Get check box state of "Trigger on Recalc" 
  154. if trig%=1 and not alreadyregistered? then
  155.     RegisterColorRecalc()
  156.     alreadyregistered?=TRUE
  157. end if
  158. if trig%=0 and alreadyregistered? then
  159.     UnRegisterColorRecalc()
  160.     alreadyregistered?=FALSE
  161. end if
  162.  
  163. FillButton(TRIGRECALC,trig%) ' Set it again for next time
  164. GetDialogState(ANDBTN,logand%) ' Gets Logical AND (if logand? is 0, then logical OR)
  165. FillButton(ANDBTN,logand%)
  166. if logand%=0 then FillButton(ORBTN,1)
  167. 'get value conditions
  168.  
  169. GetDialogField(LTCTRL,ltcond,len(ltcond)-1,actlen%)
  170. FillText(LTCTRL,ltcond)
  171. GetDialogField(GTCTRL,gtcond,len(gtcond)-1,actlen%)
  172. FillText(GTCTRL,gtcond)
  173. GetDialogField(EQCTRL,eqcond,len(eqcond)-1,actlen%)
  174. FillText(EQCTRL,eqcond)
  175.  
  176.  
  177. end if
  178.  
  179. if res%=CANCEL then
  180. PutUpDialog=FALSE
  181. 'Even on Cancel, still have to fill dialog with previous values for next time
  182. FillText(LTCTRL,ltcond)
  183. FillText(GTCTRL,gtcond)
  184. FillText(EQCTRL,eqcond)
  185. FillButton(REDBTN,redv%)        
  186. FillButton(BLUEBTN,bluev%)
  187. FillButton(YELLOWBTN,yellowv%)
  188. FillButton(MAGENTABTN,magentav%)
  189. FillButton(CYANBTN,cyanv%)
  190. FillButton(BLACKBTN,blackv%)
  191. FillButton(BOLDBTN,bold%)
  192. FillButton(ANDBTN,logand%)
  193. if logand%=0 then FillButton(ORBTN,1)
  194. FillButton(TRIGRECALC,trig%)
  195.  
  196. print "Canceled"
  197. end if
  198.  
  199. end Function
  200.  
  201. Function NumberP (string cellvalue) as BOOLEAN
  202. ' This function determines whether a given string can be converted to a number
  203. on error ERR_COERCE
  204.     NumberP=FALSE
  205. end on
  206. NumberP=TRUE
  207. s#=Number(cellvalue)    ' if this operation triggers an ERR_COERCE, then the value is not a number
  208. end Function
  209.  
  210. Function CheckAndColor(cellselection%)
  211. golt?=FALSE
  212. gogt?=FALSE
  213. goeq?=FALSE
  214.         if len(ltcond) > 0 then    ' if there's a string in the dialog field, convert to number
  215.         ltnum=Val(ltcond)     ' this is where ltnum gets a value (otherwise its datatype remains NULL)
  216.         golt?=TRUE        
  217.         end if
  218.  
  219.         if len(gtcond) > 0 then
  220.         gtnum=Val(gtcond)    ' this is where gtnum gets a value (otherwise its datatype remains NULL)    
  221.         gogt?=TRUE
  222.         end if
  223.  
  224.         if len(eqcond) > 0 then
  225.             if NumberP(eqcond) then
  226.                 eqnum=Val(eqcond)    ' this is where eqnum gets a value (otherwise its datatype remains NULL
  227.                 'otherwise eqcond (string value) will be used
  228.             end if
  229.         goeq?=TRUE
  230.         end if
  231.  
  232.  
  233. 'Get the conditions that make sense based on the above
  234. ' Use the "GO" variables to later determine whether the cell value should be checked for the condition
  235.  
  236.     SetIntProperty(cellselection%,TextFGColor,BLACK)     ' All cells first set to BLACK
  237.     SetIntProperty(cellselection%,TextBold,0)        ' Set them to UnBold
  238.     
  239.     CreateIterator(cellselection%, SM_CELL, iHandle%)     ' Create an iterator to go through each cell in the selection
  240.     
  241.     r%= GetNextElement(iHandle%, cHandle%)         ' get the first cell in the selection
  242.     Do While r% = err_OK                          ' GetNextElement will return an error when there are no more elements (cells)
  243.         textval$ = string$(100," ")    ' make sure to reinitialize the size of the textval$ buffer for each cell value
  244.                             ' because it gets truncated to the length of the text value
  245.                             ' on the next command.
  246.         GetCellValue(cHandle%, ct%,n#,textval$,len(textval$) -1,a%)
  247.         'print n#
  248.     if logand% = 1 then
  249.         ltok? = TRUE    ' These variables say whether the cell value actually meets the condition
  250.         gtok? = TRUE    ' Used later for logical AND case
  251.         eqok? = TRUE    ' Initialize them all to true for AND case
  252.     else     
  253.         ltok? = FALSE    ' this is for the logical OR case
  254.         gtok? = FALSE
  255.         eqok? = FALSE
  256.     end if
  257.         if golt?  then
  258.             if ct%=cval_NUMBER then
  259.                 if n#<ltnum then
  260.                 ltok? = TRUE ' redundant, but logical
  261.                 else ltok? = FALSE
  262.                 end if
  263.             else ltok? = FALSE
  264.             end if
  265.         end if
  266.         if gogt? then
  267.             if ct%=cval_NUMBER then
  268.                 if n# > gtnum then
  269.                 gtok? = TRUE ' redundant, but logical
  270.                 else gtok? = FALSE
  271.                 end if
  272.             else gtok? = FALSE
  273.             end if
  274.         end if
  275.         if goeq? then
  276.             if numberp(eqcond)   then
  277.                 if n# = eqnum then
  278.                 eqok? = TRUE ' redundant, but logical
  279.                 else eqok? = FALSE
  280.                 end if
  281.             else
  282.                 if ucase$(textval$) =ucase$(ltrim$(rtrim$(eqcond))) then    'Note:  ucase$ here  makes the matching case insensitive
  283.                     eqok? = TRUE ' redundant, but logical
  284.                     else eqok? = FALSE
  285.                 end if
  286.             end if
  287.         end if
  288. 'Finally, check the conditions, and do the work
  289.  
  290.     if logand% = 1 then        
  291.         if ltok? AND gtok? AND eqok? then
  292.              SetIntProperty(cHandle%,TextFGColor, clr) 
  293.             if bold%=1 then SetIntProperty(cHandle%,TextBold,1)
  294.         end if
  295.     else
  296.         if ltok? OR gtok? OR eqok? then
  297.              SetIntProperty(cHandle%,TextFGColor, clr) 
  298.             if bold%=1 then SetIntProperty(cHandle%,TextBold,1)
  299.         end if
  300.     end if
  301.  
  302.         r%=GetNextElement(iHandle%, cHandle%)     ' get the next cell in the selection
  303.     Loop
  304.     
  305.     FreeIterator(iHandle%)
  306.  
  307. end Function
  308.  
  309. end MODULE
  310.  
  311. '<<Name>> Selection : System Use Only
  312. '***********Selection : System Use Only **********************************************
  313. '* This script is called by Color Cells and  takes the current selection 
  314. '* to determine the most appropriate cell selection to CheckAndColor
  315. '* This script is not called by the recalc trigger, which operates on all the cells in the
  316. '* current worksheet
  317. '****************************************************************************************
  318. EnableRefresh(0)
  319. GetIntGlobalProperty(GP_StatusBar,origsb&) ' capture original setting
  320. SetIntGlobalProperty(GP_StatusBar,0) ' scripts run faster with it not displaying
  321.  
  322.  
  323. origname$=string$(100," ") 
  324. long origseltype&
  325.  
  326. GetSelection(origsel%)
  327. r%=GetStringProperty(origsel%,IT_SELNAME,origname$,len(origname$)-1,nameact%) 'get the name of the original selection
  328. GetIntProperty(origsel%,IT_SELTYPE,origseltype&)     ' get the original selection type to change back later
  329. select case origseltype&
  330.     case sel_FORMULA :    ' if selection is formula(e), color cells it(they) calculate
  331.         d%=PutUpDialog() 
  332.         if d% then
  333.         CreateIterator(0,SM_FORMULA,formiter%)
  334.         f%=GetNextElement(formiter%,nextform%)
  335.             while f% = err_OK
  336.             GetCellsForFormula(nextform%,cellsel%)
  337.             CheckAndColor(cellsel%)
  338.             f%=GetNextElement(formiter%,nextform%)
  339.             wend
  340.         else
  341.         r%=ModifySelHandle(0,origname$,origseltype&) ' Set selection back to user's original selection
  342.         end if
  343.     case else :            ' if selection other than formula, make sure you can create cell selection from it.
  344.         if CreateCellSelection(origsel%) then
  345.             d%=PutUpDialog() 
  346.             if d% then
  347.             SetSelectionByName(formula1%,"1",sel_FORMULA,"","","")     ' change the current selection to the formula pane (looks prettier)
  348.             CheckAndColor(origsel%) 
  349.             r%=ModifySelHandle(0,origname$,origseltype&) ' Set selection back to user's original selection
  350.             FreeSelHandle(formula1%)
  351.         else
  352.         r%=ModifySelHandle(0,origname$,origseltype&) ' Set selection back to user's original selection
  353.             end if
  354.         end if
  355. end select
  356.  
  357. EnableRefresh(1)
  358. SetIntGlobalProperty(GP_StatusBar,origsb&) ' reset status bar 
  359.     
  360. '<<Name>> Recalc : System Use Only
  361. '*****************Recalc : System Use Only **************************************************
  362. '*  This is the script called by the Trigger Function after it's been registered;
  363. '*  Registration happens  in CheckAndColor Function in Globals
  364. '* and Color By Cell Value Script has already been run at least once, 
  365. '* so Global Functions are available
  366. '* This script is different from the Selection script, in that it uses 
  367. '* All Cells in the Current Worksheet, instead of just those selected
  368. '************************************************************************************************
  369. EnableRefresh(0)
  370. GetIntGlobalProperty(GP_StatusBar,origsb&) ' capture original setting
  371. SetIntGlobalProperty(GP_StatusBar,0) ' scripts run faster with it not displaying
  372. GetIntGlobalProperty(GP_UNDO,origundo&)
  373. SetIntGlobalProperty(GP_UNDO,0)
  374. viewname$=string$(80," ")
  375. sheetname$=string$(80," ")
  376. modelname$=string$(80," ")
  377. catname$=string$(80," ")
  378.  
  379. GetSelection(current%)
  380. GetStringProperty(current%,IT_CONTEXT1,viewname$, len(viewname$) -1,actlen%)
  381. GetStringProperty(current%,IT_CONTEXT2,sheetname$, len(sheetname$) -1,actlen%)
  382. GetStringProperty(current%,IT_MODELNAME,modelname$,len(modelname$)-1,actlen%)
  383. CreateSelHandle(sheetsel%,sheetname$,sel_SHEET,viewname$,sheetname$,modelname$)
  384.  
  385. r%=CreateIterator(sheetsel%,SM_CATEGORY,sheetiter%)
  386. GetNextElement(sheetiter%,onecat%)
  387. 'one category gives me all the cells in the worksheet
  388. GetStringProperty(onecat%,IT_SELNAME,catname$,len(catname$)-1,actlen%)
  389. CreateSelHandle(allcells%,catname$,sel_CELLSONLY,"","","")
  390.  
  391. CheckAndColor(allcells%) 
  392. SetIntGlobalProperty(GP_StatusBar,origsb&)
  393. SetIntGlobalProperty(GP_UNDO,origundo&)
  394. '<<Name>> Color Cells
  395. '******************* Color Cells **************************************************************
  396. '*    This script is the main procedure and when invoked will:            
  397. '*    (1) Load Global definitions, if not already loaded                    
  398. '*    (2) Bring up a dialog box for user to choose conditions for coloring cells 
  399. '*          in the current selection.
  400. '*    (3) If the user selects "All Cells on Recalc", a trigger will be registered,
  401. '*          and all cells in the current worksheet will be colored according to the
  402. '*          most recent conditions set in the dialog box.
  403. '*    (4) This script can be invoked repeatedly to change the conditions for coloring
  404. '*             and should also be used to remove the Recalc trigger (by unchecking the box)
  405. '***********************************************************************************************
  406.  
  407. thisfile$="colorcel.lss"
  408.  
  409. GetIntGlobalProperty(GP_StatusBar,origsb&) ' capture original setting
  410. SetIntGlobalProperty(GP_StatusBar,0) ' scripts run faster with it not displaying
  411. GetIntGlobalProperty(GP_UNDO,origundo&)
  412. SetIntGlobalProperty(GP_UNDO, 0)
  413.  
  414. cmodexists=FALSE
  415. for i in session 
  416. if nameof(i) = "COLORCELLS" then
  417. cmodexists=TRUE
  418. end if
  419. next
  420.  
  421. if cmodexists= FALSE then
  422. RunScript(thisfile$,"Globals : System Use Only")
  423. end if
  424.  
  425. RunScript(thisfile$,"Selection : System Use Only")
  426.  
  427.  
  428. EnableRefresh(1)
  429. SetIntGlobalProperty(GP_StatusBar,origsb&) ' scripts run faster with it not displaying
  430. SetIntGlobalProperty(GP_UNDO,origundo&)
  431.  
  432.  
  433.