home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlcmd.pak / OWLCMD.CPP < prev    next >
C/C++ Source or Header  |  1997-07-23  |  8KB  |  288 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1991,1993 by Borland International
  3. //
  4. // This program demonstrates the use of TFloatingFrame.  It uses
  5. // a class derived from TComboBox that catches the <RETURN> key in an
  6. // edit field and executes the command which was typed.  If the command
  7. // executed sucessfully it is stored in the combo box's list box for
  8. // future retrieval.
  9. // 
  10. // Commands supported:
  11. //  DOS     Executes command.com
  12. //  DIR     Launches WINFILE
  13. //  CD x$   Changes default directory (CD\  CD \)
  14. //  EXIT    Closes OWLCMD
  15. //  x$:     Changes default drive (D:)
  16. //  x$      Executes command x$
  17. // 
  18. //----------------------------------------------------------------------------
  19. #include <owl\owlpch.h>
  20. #include <owl\applicat.h>
  21. #include <owl\edit.h>
  22. #include <owl\combobox.h>
  23. #include <owl\floatfra.h>
  24. #include "string.h"
  25. #include "dir.h"
  26.  
  27. const int EDITWIDTH  = 300;  // Width of the combo box
  28. const int CMDBUFSIZE = 256;  // Max command size
  29. const int COMBO_ID   = 1;
  30.  
  31. // For diagnostics
  32. DIAG_DEFINE_GROUP(OwlCmd,1,0);
  33.  
  34. //
  35. // A Combo box is actually made up of three windows, an edit
  36. // window, a list box window, and a parent window.  If we need to trap
  37. // an event on the edit or list box window, normal OWL event handlers
  38. // won't work.  These event handlers would trap events to the parent
  39. // window.
  40. // 
  41. // TComboEdit is hard wired to the control ID that MS uses for the edit 
  42. // window. This allows us to trap events, like CR.
  43. //
  44.  
  45. const int COMBO_LIST_ID = 0x3e8;  // Window ID for list box component
  46. const int COMBO_EDIT_ID = 0x3e9;  // Window ID for edit field component
  47.  
  48. class TComboEdit : public TEdit {
  49.   public:
  50.     TComboEdit(TComboBox* parent, UINT textLen) : 
  51.       TEdit(parent, COMBO_EDIT_ID, textLen) 
  52.       {
  53.         Combo = parent;
  54.       }
  55.  
  56.   private:
  57.     // This is where we'll look for c/r and special keys
  58.     void EvChar(UINT key, UINT repeatCount, UINT flags);
  59.  
  60.     void ExecuteCmd(char *cmd);
  61.  
  62.     // We could also get Parent from TWindow and downcast it
  63.     TComboBox* Combo;
  64.  
  65.     char EditBuf[CMDBUFSIZE];
  66.     char CmdBuf[CMDBUFSIZE];
  67.  
  68.   DECLARE_RESPONSE_TABLE(TComboEdit);
  69. };
  70.  
  71. DEFINE_RESPONSE_TABLE1(TComboEdit,TEdit)
  72.   EV_WM_CHAR,
  73. END_RESPONSE_TABLE;
  74.  
  75. //
  76. // This is where we look for the <ENTER> key and execute commands
  77. //
  78. void
  79. TComboEdit::EvChar(UINT key, UINT /*repeatCount*/, UINT /*flags*/)
  80. {
  81.   char*    ptr;
  82.   int      result;
  83.   TWindow* fw;
  84.  
  85.   if (key == VK_RETURN) {
  86.     //
  87.     // Get Line into local buffer for parsing
  88.     //
  89.     GetText(EditBuf, sizeof(EditBuf));
  90.     if (!EditBuf[0]) // Ignore empty strings
  91.       return;
  92.  
  93.     strupr(EditBuf);
  94.     ptr = EditBuf;
  95.  
  96.     //
  97.     // Brain dead parser
  98.     //
  99.     if (strcmp(EditBuf, "DIR") == 0) {
  100.       wsprintf(CmdBuf,"winfile");
  101.       ExecuteCmd(CmdBuf);
  102.       return;
  103.  
  104.     } else if (strcmp(EditBuf,"DOS") == 0) {
  105.       wsprintf(CmdBuf,"COMMAND.COM");
  106.       ExecuteCmd(CmdBuf);
  107.       return;
  108.  
  109.      } else if (strcmp(EditBuf,"EXIT") == 0) {
  110.       //Quit
  111.       PostQuitMessage(0);
  112.       return;
  113.  
  114.     } else if (strncmp(EditBuf,"CD",2) == 0 && strlen(EditBuf) > 2 ) {
  115.       if (EditBuf[2] == '\\')     
  116.         result = chdir(EditBuf+2);
  117.       else if (EditBuf[2] == ' ') {
  118.         for (char* p = EditBuf+3; *p && *p == ' '; p++);
  119.         result = chdir(p); // Point past space
  120.       } else {
  121.         // Must be a program name starting with "cd"
  122.         ExecuteCmd(EditBuf);
  123.         return;
  124.       }
  125.       if (result) {
  126.         ptr = "Invalid directory";
  127.         MessageBox(ptr,"OWLCMD",MB_OK);
  128.         return;
  129.       } else {
  130.         ExecuteCmd(0); //Just store result and clear edit field
  131.         ptr = getcwd(EditBuf,sizeof(EditBuf));
  132.         if (ptr) {
  133.           fw = GetApplication()->GetMainWindow();
  134.           fw->SetCaption(ptr);
  135.           fw->SetWindowPos(0, TRect(), SWP_DRAWFRAME|SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER);
  136.           
  137.         }
  138.         return;
  139.       }
  140.  
  141.     } else if (isalpha(EditBuf[0]) && 
  142.         EditBuf[1] == ':' &&
  143.         EditBuf[2] == 0) {
  144.       //
  145.       // Set drive
  146.       //
  147.       setdisk(toupper(EditBuf[0])-'A');
  148.       ptr = getcwd(EditBuf,sizeof(EditBuf));
  149.       if (ptr){
  150.         ExecuteCmd(0); // Store and clear
  151.         fw = GetApplication()->GetMainWindow();
  152.         fw->SetCaption(ptr);
  153.         fw->SetWindowPos(0, TRect(), SWP_DRAWFRAME|SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER);
  154.       }
  155.  
  156.     } else {
  157.       ExecuteCmd(EditBuf);
  158.       return;
  159.     }
  160.   } else
  161.     DefaultProcessing();
  162.  
  163.   return;
  164. }
  165.  
  166. void
  167. TComboEdit::ExecuteCmd(char* cmd)
  168. {
  169.   if (cmd) {
  170.     int result = WinExec(cmd, SW_SHOWNORMAL);
  171.     switch (result) {
  172.       case 0:  cmd = "Out of memory"; break;
  173.       case 2:  cmd = "File not found"; break;
  174.       case 3:  cmd = "Path not found"; break;
  175.       default: cmd = "Error in WinExec"; break;
  176.     }
  177.     if (result <= 32 ) {
  178.       MessageBox(cmd, "OWLCMD", MB_OK);
  179.       return;
  180.     }
  181.   }
  182.  
  183.   //
  184.   // If we got here, then command was sucessful
  185.   // Store command in pick list buffer, & clear edit control
  186.   //
  187.   Combo->InsertString(EditBuf,0);
  188.   Clear();
  189. }
  190.  
  191. //------------------------------------------------------------------------
  192.  
  193. class TLineEdit : public TComboBox {
  194.   public:
  195.     TLineEdit(TWindow* p,int w, int h);
  196.  
  197.     TComboEdit* Ed;
  198. };                              
  199.  
  200. TLineEdit::TLineEdit(TWindow* p, int w, int h)
  201.   : TComboBox(p, COMBO_ID, -1, -1, w, h, CBS_DROPDOWN, CMDBUFSIZE-1)
  202. {
  203.   Ed = new TComboEdit(this, CMDBUFSIZE-1);
  204.  
  205.   // We don't want a sorted list!
  206.   Attr.Style &= ~CBS_SORT;
  207. }
  208.  
  209. //------------------------------------------------------------------------
  210.  
  211. class TMyFrame : public TFloatingFrame {
  212.   public:
  213.     TMyFrame(TWindow* client, char* title);
  214.  
  215.   private:
  216.     void    SetupWindow();
  217.  
  218.     TLineEdit* Cmd;
  219.  
  220.   DECLARE_RESPONSE_TABLE(TMyFrame);
  221. };
  222.  
  223. DEFINE_RESPONSE_TABLE1(TMyFrame, TFloatingFrame)
  224. END_RESPONSE_TABLE;
  225.  
  226. TMyFrame::TMyFrame(TWindow* client, char* title)
  227.   : TFloatingFrame(0, title, client)
  228. {
  229.   //
  230.   // We can set our own attributes, but must be before EnableTiny...
  231.   //
  232.   Attr.Style = WS_POPUP | WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX;
  233.  
  234.   //
  235.   // Or we can be specific, even make it BIGGER if we want
  236.   // If we set the option CloseBox flag to true, then clicking the box
  237.   // will close the window instead of displaying the system menu
  238.   //
  239.   EnableTinyCaption(58, FALSE);
  240.  
  241.   //
  242.   // Create our combo box and size it for an edit line and 5 text items
  243.   // What's the correct way to get the height of an edit control?
  244.   //
  245.   int lineH = GetSystemMetrics(SM_CYCAPTION)+2; // 1pixel top & bottom border
  246.   Cmd = new TLineEdit(this, EDITWIDTH, lineH*6);
  247.   Attr.X = (GetSystemMetrics(SM_CXSCREEN) - EDITWIDTH)/2;
  248.   Attr.Y = (GetSystemMetrics(SM_CYSCREEN) - lineH*6)/2;
  249.   Attr.W = EDITWIDTH;
  250.   Attr.H = 0;  // Resize ourselves later, when we know the actual size
  251. }
  252.  
  253. void
  254. TMyFrame::SetupWindow()
  255. {
  256.   TFloatingFrame::SetupWindow();
  257.  
  258.   //
  259.   // Resize window to actual combo box size
  260.   //
  261.   TRect cr = Cmd->GetWindowRect();
  262.   TRect newRect = GetWindowRect();
  263.   MoveWindow(newRect.left, newRect.top, 
  264.              newRect.Width(), CaptionHeight + cr.Height(), TRUE);
  265.  
  266.   char buf[MAXDIR+1];
  267.   if (getcwd(buf, sizeof(buf))) {
  268. //    strlwr(buf);
  269.     SetCaption(buf);
  270.   }
  271. }
  272.  
  273. //------------------------------------------------------------------------
  274.  
  275. class TOwlCmd : public TApplication {
  276.   public:
  277.     TOwlCmd() : TApplication("Owl Command") {}
  278.  
  279.   protected:
  280.     void InitMainWindow() {MainWindow = new TMyFrame(0,"OwlCmd");}
  281. };
  282.  
  283. int
  284. OwlMain(int /*argc*/, char* /*argv*/ [])
  285. {
  286.   return TOwlCmd().Run();
  287. }
  288.