home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / epmmac2.zip / STYLEBUT.E < prev    next >
Text File  |  1995-04-07  |  8KB  |  196 lines

  1. ; This is a Toolbar Actions file.  You add a line to your ACTIONS.LST:
  2. ;    stylebut
  3. ; to indicate that this should be invoked to build the list of defined
  4. ; actions when the user asks for a list.
  5.  
  6. include 'stdconst.e'
  7. include 'english.e'
  8.  
  9. ; Next, define some additional text constants (defined as separate constants
  10. ; instead of using the strings where needed in order to allow for easier NLS
  11. ; translation).
  12.  
  13. const
  14.    STYLEBUT__MSG        = 'Apply_style'
  15.    STYLEBUT_PROMPT      = 'Apply a style to marked text, or entire file.'
  16.    STYLEBUT2_PROMPT     = 'A style may be passed as a parameter, or will be prompted for otherwise.'
  17.    STYLEBUT_PROMPT__MSG = 'Select a style to apply.'
  18.    UNKNOWN_STYLE__MSG   = 'Unknown style'
  19.    NO_STYLES__MSG       = 'No styles saved.'
  20.    APPLY__MSG           = '~Apply'
  21.    UNSTYLE__MSG    = 'Remove style'  -- Messagebox Title
  22.    UNSTYLE_PROMPT  = 'Remove a style from text pointed at by cursor'
  23.    NO_STYLE__MSG   = 'No font set around cursor position'
  24.  
  25. ; Here is the <file_name>_ACTIONLIST command that adds the action command
  26. ; to the list.
  27.  
  28. defc stylebut_actionlist
  29.    universal ActionsList_FileID    -- This is the fileid that gets the line(s)
  30.    insertline 'apply_style'STYLEBUT_PROMPT'  'STYLEBUT2_PROMPT'stylebut', ActionsList_FileID.last+1, ActionsList_FileID
  31.    insertline 'remove_style'UNSTYLE_PROMPT'stylebut', ActionsList_FileID.last+1, ActionsList_FileID
  32.  
  33. ; These are the command that will be called for the above actions.
  34.  
  35. defc apply_style
  36.    universal appname, app_hini
  37.    parse arg arg1 stylename
  38.    if arg1 = 'S' then       -- button Selected
  39.       sayerror 0
  40.       getfileid fid
  41.       mt = marktype()
  42.       if mt then
  43.          getmark l1, l2, c1, c2, markfid
  44.          if fid<>markfid then
  45.             sayerror OTHER_FILE_MARKED__MSG
  46.             return
  47.          endif
  48.       endif
  49.       if stylename = '' then          -- Provide list of styles
  50.          App = 'Style'\0
  51.          inidata = copies(' ', MAXCOL)
  52.       compile if EPM32
  53.          retlen = \0\0\0\0
  54.          l = dynalink32('PMSHAPI',
  55.                         '#115', -- 'PRF32QUERYPROFILESTRING',
  56.                         atol(app_hini)    ||  -- HINI_PROFILE
  57.                         address(App)      ||  -- pointer to application name
  58.                         atol(0)           ||  -- Key name is NULL; returns all keys
  59.                         atol(0)           ||  -- Default return string is NULL
  60.                         address(inidata)  ||  -- pointer to returned string buffer
  61.                         atol(MAXCOL)      ||       -- max length of returned string
  62.                         address(retlen), 2)         -- length of returned string
  63.       compile else
  64.          l =  dynalink( 'PMSHAPI',
  65.                         'PRFQUERYPROFILESTRING',
  66.                         atol(app_hini)   ||  -- HINI_PROFILE
  67.                         address(App)     ||  -- pointer to application name
  68.                         atol(0)          ||  -- Key name is NULL; returns all keys
  69.                         atol(0)          ||  -- Default return string is NULL
  70.                         address(inidata) ||  -- pointer to returned string buffer
  71.                         atol_swap(MAXCOL), 2)        -- max length of returned string
  72.       compile endif
  73.  
  74.          if not l then
  75.             sayerror NO_STYLES__MSG
  76.             'fontlist'
  77.             return
  78.          endif
  79.          inidata = strip(inidata, 'T')
  80.          if rightstr(inidata, 2) = \0\0 then
  81.             inidata = leftstr(inidata, length(inidata)-1)
  82.          else  -- Was too long; just show whole strings
  83.             inidata = leftstr(inidata, lastpos(\0, inidata))
  84.          endif
  85.          inidata = \1 || translate(strip(inidata, 'T'), \1, \0)  -- Change nulls to ASCII 1's for listbox delimiter.
  86.          parse value listbox(STYLEBUT__MSG,
  87.                              inidata,
  88.                              '/'APPLY__MSG'/'Cancel__MSG,1,5,min(count(\1, inidata)-1,12),0,
  89.        compile if EVERSION >= 5.60
  90.                              gethwndc(APP_HANDLE) || atoi(1) || atoi(1) || atoi(0) ||
  91.        compile else
  92.                              atoi(1) || atoi(1) || atoi(0) || gethwndc(APP_HANDLE) ||
  93.        compile endif
  94.                              STYLEBUT_PROMPT__MSG) with button 2 stylename \0
  95.          if button <> \1 then
  96.             return
  97.          endif
  98.       else
  99.          stylestuff = queryprofile(app_hini, 'Style', stylename)
  100.          if stylestuff='' then
  101.             sayerror UNKNOWN_STYLE__MSG '"'stylename'"'
  102.             return
  103.          endif
  104.       endif  /* stylename passed as arg */
  105.       if not mt then
  106.          call pset_mark(1, .last, 1, length(textline(.last)), 'CHAR' , fid)
  107.       endif
  108.       'process_style' stylename
  109.       if not mt then
  110.          unmark
  111.       endif
  112.    elseif arg1 = 'I' then   -- button Initialized
  113.       display -8
  114.       sayerror STYLEBUT_PROMPT
  115.       display 8
  116.    elseif arg1 = 'H' then   -- button Help
  117. ;     'compiler_help_add stylebut.hlp' -- Sample code; no .hlp file is
  118. ;     'helpmenu 32100'                 -- provided for STYLEBUT.
  119.                                        -- Instead, we'll just pop a messagebox containing the prompt.
  120.       call winmessagebox(STYLEBUT__MSG, STYLEBUT_PROMPT, MB_OK + MB_INFORMATION + MB_MOVEABLE)
  121.    elseif arg1 = 'E' then   -- button End
  122. ;;    sayerror 0
  123.    endif
  124.  
  125. const
  126.    COLOR_CLASS = 1
  127.    BOOKMARK_CLASS = 13
  128.    STYLE_CLASS =  14
  129.    FONT_CLASS =  16
  130.  
  131. defc remove_style  -- Based on code from Toby Thurston
  132.                    -- Enhanced to remove Color & Style attributes as well as Font.
  133.    parse arg arg1 .
  134.    if arg1 = 'S' then       -- button Selected
  135.      class = FONT_CLASS
  136.      offst1 = 0
  137.      col1 = .col
  138.      line1 = .line
  139.      attribute_action FIND_PREV_ATTR_SUBOP, class, offst1, col1, line1
  140.      if not class then      -- No font, maybe color?
  141.         class = COLOR_CLASS
  142.         attribute_action FIND_PREV_ATTR_SUBOP, class, offst1, col1, line1
  143.      endif
  144.      if class then          -- Found one...
  145.         offst2 = offst1
  146.         col2 = col1
  147.         line2 = line1
  148.         saveclass = class
  149.         attribute_action FIND_MATCH_ATTR_SUBOP, class, offst2, col2, line2
  150.         ll = 256            -- arbitary large line length factor
  151.         if class &          -- if found a font and...
  152.           (line1 * ll + col1) <= (.line * ll + .col) & -- 1st attr before cursor
  153.           (line2 * ll + col2) >= (.line * ll + .col)   -- 2nd attr after cursor
  154.         then                -- Found a match around cursor so delete them
  155.           attribute_action DELETE_ATTR_SUBOP, class, offst1, col1, line1
  156.           attribute_action DELETE_ATTR_SUBOP, class, offst2, col2, line2
  157.           if offst1 = -2 & offst2 = -2 then
  158.              offst1 = -1; offst2 = -1
  159.           endif
  160.           if saveclass=FONT_CLASS then  -- See if there's a color around it.
  161.              removestyle_delete(COLOR_CLASS, offst1, col1, line1, offst2, col2, line2)
  162.           endif
  163.           removestyle_delete(STYLE_CLASS, offst1, col1, line1, offst2, col2, line2)
  164.         else
  165.            sayerror NO_STYLE__MSG
  166.         endif
  167.      else
  168.         sayerror NO_STYLE__MSG
  169.      endif
  170.    elseif arg1 = 'I' then   -- button Initialized
  171.       display -8
  172.       sayerror UNSTYLE_PROMPT
  173.       display 8
  174.    elseif arg1 = 'H' then   -- button Help
  175.       call winmessagebox(UNSTYLE__MSG, UNSTYLE_PROMPT, MB_OK + MB_INFORMATION + MB_MOVEABLE)
  176. ;  elseif arg1 = 'E' then   -- button End
  177. ;;    sayerror 0
  178.    endif
  179.  
  180. defproc removestyle_delete(x_CLASS, offst1, col1, line1, offst2, col2, line2)
  181.    query_attribute class, val, IsPush, offst1, col1, line1
  182.    if class=x_CLASS & IsPush then
  183.       offst3 = offst1
  184.       col3 = col1
  185.       line3 = line1
  186.       attribute_action FIND_MATCH_ATTR_SUBOP, class, offst3, col3, line3
  187.       if class & offst3=offst2 & col3=col2 & line3=line2 then
  188.          attribute_action DELETE_ATTR_SUBOP, class, offst1, col1, line1
  189.          attribute_action DELETE_ATTR_SUBOP, class, offst2, col2, line2
  190.       endif
  191.    endif
  192.  
  193.  compile if EVERSION >= 6
  194. EA_comment 'This is a toolbar "actions" file which defines a command for a Style button.'
  195.  compile endif
  196.