home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / p2demo21.exe / PEL / DOSBOX.PEL < prev    next >
Text File  |  1995-04-04  |  10KB  |  319 lines

  1. # $Header:   P:\source\wmacros\dosbox.pev   1.16   04 Apr 1995 09:27:50   WALKER  $
  2. ## $Tabs:4 7$
  3.  
  4. ##############################################################################
  5. #
  6. #       Compuware Corporation
  7. #         31440 Northwestern Highway
  8. #           Farmington Hills, Michigan 48334-2564
  9. #
  10. #   This source code listing contains information that is
  11. #   proprietary to Compuware Corporation and may not be copied
  12. #   duplicated, translated, transmitted, stored, retrieved
  13. #   or in any manner or by any method conveyed or disclosed
  14. #   to a third party or parties without express written
  15. #   permission from Compuware Corporation.
  16. #
  17. #  
  18. ##############################################################################
  19.  
  20. #### $Workfile:   dosbox.pel  $: dos_box (DOS in a dialog).
  21.  
  22. ## System flags (for the dos_box() function)
  23. global SYS_MASK      = 0x007F
  24. global SYS_CLOSE     = 0x0080
  25. global SYS_PROMPT    = 0x0100
  26.  
  27. #
  28. # Resource ID's used by the DOS BOX.
  29. #
  30. local IDD_DOSBOX        = 253
  31. local IDE_STDOUT        =  51
  32. local IDE_STDIN         =  52
  33.  
  34. #
  35. # Values used to calculate the size of the STDOUT box when re-sizing.
  36. #
  37. local DOS_WIDTH_DIFF    =  50
  38. local DOS_HEIGHT_DIFF   =  70
  39.  
  40. local dos_boxes
  41. local dos_pipes
  42.  
  43. global function dos_box(cmd, flags, title)
  44. {
  45.    local dh_dos_box
  46.    local status     = 0
  47.    local pipeid     = create_pipes()
  48.  
  49.    if (pipeid)
  50.    {
  51.       dh_dos_box = create_mdialog(function_id( "dos_callback" ), mdi_mode == 2 ? -1 : 0,
  52.                                   IDD_DOSBOX, resource_dll )
  53.       if (dh_dos_box)
  54.       {
  55.          dos_boxes[pipeid]     = dh_dos_box
  56.          dos_pipes[dh_dos_box] = pipeid
  57.  
  58.          attach_event_handler(EVENT.PIPE_DATA, function_id("dos_data"))
  59.  
  60.          if (and(flags, SYS_PROMPT))
  61.             attach_event_handler(EVENT.PROCESS_COMPLETE, function_id("dos_done 2"))
  62.          else if (and(flags, SYS_CLOSE))
  63.             attach_event_handler(EVENT.PROCESS_COMPLETE, function_id("dos_done 1"))
  64.          else
  65.             attach_event_handler(EVENT.PROCESS_COMPLETE, function_id("dos_done 0"))
  66.  
  67.          if (title)
  68.             set_dialog_window( dh_dos_box, DWC_TITLE, title )
  69.          else if (cmd)
  70.             set_dialog_window( dh_dos_box, DWC_TITLE, cmd )
  71.  
  72.          begin_dialog(dh_dos_box, TRUE)
  73.  
  74.          status = system(cmd, pipeid, pipeid, pipeid, \
  75.                          and(or(flags, SYS_NOSESSION + SYS_ASYNC), SYS_MASK))
  76.  
  77.          if ( status > 0 )
  78.          {
  79.             if ( !and(flags, SYS_NOEXPOSE) )
  80.                process_pipes(pipeid, FALSE, FALSE)
  81.          }
  82.          else
  83.          {
  84.             cleanup_dos_box(dh_dos_box)
  85.             delete_dialog(dh_dos_box)
  86.             close_pipes(pipeid)
  87.          }
  88.       }
  89.    }
  90.    else
  91.       status = system(cmd, 0, 0, 0, \
  92.                       and(or(flags, SYS_SESSION + SYS_ASYNC), SYS_MASK))
  93.  
  94.    return status
  95. }
  96.  
  97. local IDM_DOS_COPY   = 2001
  98. local IDM_DOS_PASTE  = 2002
  99. local IDM_DOS_EDIT   = 2003
  100.  
  101. local dos_menu
  102. local dos_edit_menu
  103.  
  104. local function create_dos_menu(dlgid)
  105. {
  106.    dos_menu[dlgid]      = create_menu()
  107.    dos_edit_menu[dlgid] = create_menu()
  108.  
  109.    # Make append_menuitem work for duplicate menus, since we don't use
  110.    # menu functions.
  111.    menu_functions[IDM_DOS_COPY]  = ""; delete menu_functions[IDM_DOS_COPY] 
  112.    menu_functions[IDM_DOS_PASTE] = ""; delete menu_functions[IDM_DOS_PASTE]
  113.    menu_functions[IDM_DOS_EDIT]  = ""; delete menu_functions[IDM_DOS_EDIT] 
  114.  
  115.    append_menuitem(dos_edit_menu[dlgid], IDM_DOS_COPY,  0x0408,
  116.                    "~Copy\tCtrl-Ins")
  117.    append_menuitem(dos_edit_menu[dlgid], IDM_DOS_PASTE, 0x0408,
  118.                    "~Paste\tShift-Ins")
  119.    append_menuitem(dos_menu[dlgid],      IDM_DOS_EDIT,  0x0208,
  120.                    "~Edit", dos_edit_menu[dlgid])
  121.  
  122.    change_menu(dos_menu[dlgid], dlgid)
  123. }
  124.  
  125. local function cleanup_dos_menu(dlgid)
  126. {
  127. #  delete_menu(dos_edit_menu[dlgid], dlgid)
  128. #  delete_menu(dos_menu[dlgid],      dlgid)
  129.  
  130.    delete dos_edit_menu[dlgid]
  131.    delete dos_menu[dlgid]
  132.  
  133.    menu_functions[IDM_DOS_COPY]  = ""; delete menu_functions[IDM_DOS_COPY] 
  134.    menu_functions[IDM_DOS_PASTE] = ""; delete menu_functions[IDM_DOS_PASTE]
  135.    menu_functions[IDM_DOS_EDIT]  = ""; delete menu_functions[IDM_DOS_EDIT] 
  136. }
  137.  
  138. global function cleanup_dos_box(dlgid)
  139. {
  140.    cleanup_prompt_history()
  141.    cleanup_dos_menu(dlgid)
  142. }
  143.  
  144. global function dos_callback()
  145. {
  146.    local data
  147.    local dlgid
  148.    local buffer
  149.    local pipeid
  150.  
  151.    if ( callback_msg == DM_INIT )
  152.    {
  153.       create_dos_menu(callback_dialog_handle)
  154.       add_dialog_item(callback_dialog_handle, IDE_STDOUT, DCTRL_MLE)
  155.       add_dialog_item(callback_dialog_handle, IDE_STDIN,  DCTRL_EDIT_KEY)
  156.       set_dialog_item(callback_dialog_handle, IDE_STDIN,  DAC_SETFOCUS)
  157.       setup_prompt_history( "DOSBOX" )
  158.    }
  159.    else if ( callback_msg == DM_CLOSE || callback_msg == DM_CANCEL )
  160.    {
  161.       if (callback_dialog_handle in dos_pipes)
  162.          close_pipes(dos_pipes[callback_dialog_handle])
  163.  
  164.       cleanup_dos_box(callback_dialog_handle)
  165.       delete dos_pipes[callback_dialog_handle]
  166.       return DRC_EXIT
  167.    }
  168.    else if ( callback_msg == DM_OK )
  169.    {
  170.       if (callback_dialog_handle in dos_pipes)
  171.       {
  172.          dlgid  = callback_dialog_handle
  173.          pipeid = dos_pipes[dlgid]
  174.          data   = query_dialog_item(dlgid, IDE_STDIN, DAC_EDIT_TEXT) "\n"
  175.          buffer = dos_buffer(dlgid, length(data)) data
  176.          set_dialog_item(dlgid, IDE_STDOUT, DAC_EDIT_TEXT, buffer)
  177.          set_dialog_item(dlgid, IDE_STDIN,  DAC_EDIT_TEXT, "")
  178.          set_dialog_item(dlgid, IDE_STDIN,  DAC_SETFOCUS)
  179.          pputs(data, pipeid)
  180.       }
  181.       return DRC_MSG_PROCESSED
  182.    }
  183.    else if ( callback_msg == DM_MENUCOMMAND )
  184.    {
  185.       if (callback_index == IDM_DOS_COPY)
  186.          set_dialog_item(callback_dialog_handle, IDE_STDOUT, DAC_COPY)
  187.       else if (callback_index == IDM_DOS_PASTE)
  188.          set_dialog_item(callback_dialog_handle, IDE_STDIN, DAC_PASTE)
  189.    }
  190.    else if ( ( callback_msg == DM_CLICK && callback_index == DI_HELP ) ||
  191.              ( callback_msg == DM_HELPREQUESTED ) ) 
  192.    {
  193.       display_help("dosbox", 0 )
  194.       return DRC_MSG_PROCESSED
  195.    }
  196.    else if ( callback_msg == DM_INVALID_PCHAR && 
  197.              callback_index == IDE_STDIN )
  198.    {
  199.       dlgid           = callback_dialog_handle
  200.       prompt_response = query_dialog_item(dlgid, IDE_STDIN, DAC_EDIT_TEXT)
  201.       execute_event_handler(EVENT.INVALID_PCHAR)
  202.       set_dialog_item(dlgid, IDE_STDIN, DAC_EDIT_TEXT, prompt_response)
  203.    }
  204.    else if ( callback_msg == DM_RESIZE )
  205.       resize_dos_dialog( callback_dialog_handle )
  206.  
  207.    return DRC_CONTINUE
  208. }
  209.  
  210. global function dos_data()
  211. {
  212.    local len
  213.    local data
  214.    local dlgid
  215.    local buffer
  216.  
  217.    if ( process_pipe in dos_boxes )
  218.    {
  219.       data  = pipe_data
  220.       dlgid = dos_boxes[process_pipe]
  221.  
  222.       if (dlgid in dos_pipes)
  223.       {
  224.          buffer = dos_buffer(dlgid, length(data)) data
  225.          set_dialog_item(dlgid, IDE_STDOUT, DAC_EDIT_TEXT, buffer)
  226.  
  227.          len = length(buffer)
  228.          set_dialog_item(dlgid, IDE_STDOUT, DAC_SELECTION, len, len)
  229.       }
  230.    }
  231. }
  232.  
  233. global function dos_done(close)
  234. {
  235.    local save
  236.    local level
  237.    local title
  238.    local dlgid
  239.  
  240.    if ( process_pipe in dos_boxes )
  241.    {
  242.       dlgid = dos_boxes[process_pipe]
  243.  
  244.       if (dlgid in dos_pipes)
  245.       {
  246.          title = query_dialog_window( dlgid, DWC_TITLE )
  247.          set_dialog_item(dlgid,   DI_OK,     DAC_DISABLE)
  248.          set_dialog_item(dlgid,   IDE_STDIN, DAC_DISABLE)
  249.          set_dialog_window(dlgid, DWC_TITLE, "Completed: " title)
  250.  
  251.          if (close)
  252.          {
  253.             if (close > 1)
  254.             {
  255.                level            = message_level
  256.                message_level    = 0
  257.                save             = status_bar_flags
  258.                status_bar_flags = and( status_bar_flags, not( STB_MESSAGES ))
  259.                notify((length(title) ? title : "Command ") " Complete")
  260.                status_bar_flags = save
  261.                message_level    = level
  262.             }
  263.  
  264.             if (dlgid in dos_pipes)
  265.             {
  266.                cleanup_dos_box(dlgid)
  267.                delete_dialog(dlgid)
  268.             }
  269.          }
  270.       }
  271.  
  272.       delete dos_boxes[process_pipe]
  273.       delete dos_pipes[dlgid]
  274.       delete_event(EVENT.PIPE_DATA, function_id("dos_data"))
  275.       if (close)
  276.          delete_event(EVENT.PROCESS_COMPLETE, function_id("dos_done " close))
  277.       else
  278.          delete_event(EVENT.PROCESS_COMPLETE, function_id("dos_done 0"))
  279.    }
  280. }
  281.  
  282. local function dos_buffer(dlgid, len)
  283. {
  284.    local buffer = query_dialog_item(dlgid, IDE_STDOUT, DAC_EDIT_TEXT)
  285.    if (length(buffer) + len > 7800)
  286.       return suffix(buffer, 6000 - len)
  287.    else
  288.       return buffer
  289. }
  290.  
  291. local function resize_dos_dialog( dlgid )
  292. {
  293.    local new, pos, delta
  294.  
  295.    # new size is stored in a long as (short width)(short height)
  296.    new.width  = callback_data / 65536
  297.    new.height = callback_data % 65536
  298.  
  299.    # resize stdin & stdout boxes to match new dialog size
  300.    if ( new.width && new.height )
  301.    {
  302.       pos   = query_dialog_item( dlgid, IDE_STDOUT, DAC_POSITION)
  303.       delta = pos % 65536 + DOS_HEIGHT_DIFF
  304.  
  305.       set_dialog_item(dlgid, IDE_STDOUT, DAC_SIZE,
  306.                       new.width - DOS_WIDTH_DIFF, new.height - delta )
  307.  
  308.       set_dialog_item(dlgid, IDE_STDIN, DAC_SIZE, new.width - DOS_WIDTH_DIFF,
  309.                       query_dialog_item( dlgid, IDE_STDIN, DAC_SIZE) % 65536 )
  310.  
  311.       # show/hide the group of buttons if new dimensions are smaller
  312.       # than the group of buttons
  313.       #
  314.       show_button( dlgid, DI_OK,     new )
  315.       show_button( dlgid, DI_CANCEL, new )
  316.       show_button( dlgid, DI_HELP,   new )
  317.    }
  318. }
  319.