home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / BC_502 / FILEBROW.PAK / FILEBROW.CPP next >
Encoding:
C/C++ Source or Header  |  1997-05-06  |  12.4 KB  |  483 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // Copyright (c) 1993, 1995 by Borland International, All Rights Reserved
  4. //----------------------------------------------------------------------------
  5. #include <owl/pch.h>
  6. #include <owl/applicat.h>
  7. #include <owl/framewin.h>
  8. #include <owl/listbox.h>
  9. #include <owl/editfile.rh>  // includes editsear.rh
  10. #include <owl/editfile.h>
  11. #include <owl/layoutwi.h>
  12. #include <owl/static.h>
  13. #include <owl/gdiobjec.h>
  14. #include <owl/combobox.h>
  15. #include <owl/button.h>
  16. #include <owl/menu.h>
  17.  
  18. #include <dir.h>
  19.  
  20. #include "filebrow.rh"
  21.  
  22. const ID_FilePane        = 1;
  23. const ID_EditPane        = 2;
  24. const ID_FindPane        = 3;
  25. const ID_FilePaneLabel   = 5;
  26. const ID_DirPane         = 6;
  27. const ID_DirPaneLabel    = 7;
  28. const ID_FilterPane      = 9;
  29. const ID_FilterPaneLabel = 10;
  30. const ID_DrivePane       = 11;
  31.  
  32. const CaptionHeight = 12;
  33. const EditHeight    = 30; // Height of an edit control, with borders, in pixels
  34. const ButtonWidth   = 40;
  35. const MaxEntry      = MAXFILE+MAXEXT+4;
  36.  
  37.  
  38. //----------------------------------------------------------------------------
  39.  
  40. //
  41. // File edit control class w/ a context popup menu
  42. //
  43. class TMyEdit : public TEditFile {
  44.   public:
  45.     TMyEdit(TWindow*        parent = 0,
  46.             int             id = 0,
  47.             const char far* text = 0,
  48.             int x = 0, int y = 0, int w = 0, int h = 0,
  49.             const char far* fileName = 0,
  50.             TModule*        module = 0) :
  51.             TEditFile(parent,id,text,x,y,w,h,fileName,module) {}
  52.  
  53.   void EvRButtonDown(uint modkeys, TPoint& point);
  54.  
  55.   DECLARE_RESPONSE_TABLE(TMyEdit);
  56. };
  57.  
  58. DEFINE_RESPONSE_TABLE1(TMyEdit,TEditFile)
  59.   EV_WM_RBUTTONDOWN,
  60. END_RESPONSE_TABLE;
  61.  
  62. void
  63. TMyEdit::EvRButtonDown(uint /*modkeys*/, TPoint& point)
  64. {
  65.   TPoint lp = point;
  66.   TPopupMenu *menu = new TPopupMenu();
  67.  
  68.   // Commands are handled by TEditFile
  69.   //
  70.   menu->AppendMenu(MF_STRING, CM_FILESAVE, "Save");
  71.   menu->AppendMenu(MF_STRING, CM_FILESAVEAS, "Save As");
  72.   menu->AppendMenu(MF_SEPARATOR, 0, 0);
  73.   menu->AppendMenu(MF_STRING, CM_EDITUNDO, "Undo");
  74.   menu->AppendMenu(MF_STRING, CM_EDITCUT, "Cut");
  75.   menu->AppendMenu(MF_STRING, CM_EDITCOPY, "Copy");
  76.   menu->AppendMenu(MF_STRING, CM_EDITPASTE, "Paste");
  77.   menu->AppendMenu(MF_STRING, CM_EDITDELETE, "Delete");
  78.   menu->AppendMenu(MF_STRING, CM_EDITCLEAR, "Clear");
  79.   menu->AppendMenu(MF_SEPARATOR, 0, 0);
  80.   menu->AppendMenu(MF_STRING, CM_EDITFIND, "Find");
  81.   menu->AppendMenu(MF_STRING, CM_EDITREPLACE, "Replace");
  82.   menu->AppendMenu(MF_STRING, CM_EDITFINDNEXT, "Next");
  83.   ClientToScreen(lp);
  84.  
  85.   menu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, lp, 0, HWindow);
  86.   delete menu;
  87. }
  88.  
  89. //----------------------------------------------------------------------------
  90.  
  91. class TBrowserWindow;
  92.  
  93. //
  94. // Layout window for find pane
  95. //
  96. class TFindPane : public TLayoutWindow {
  97.   public:
  98.     TFindPane(TBrowserWindow* parent);
  99.     void SetupWindow();
  100.     void Paint(TDC& dc, bool erase, TRect& rect);
  101.  
  102.     void Update();
  103.  
  104.     char Filter[256];
  105.  
  106.   protected:
  107.     // Notification Handlers
  108.     //
  109.     void SelectDir();
  110.     void SelectDrive();
  111.     void SelectFilter();
  112.  
  113.     void EvSize(uint sizeType, TSize& size);
  114.  
  115.   private:
  116.     // File Filter string
  117.     //
  118.     TButton* FilterPaneLabel;
  119.     TEdit*   FilterPane;
  120.  
  121.     // List of drives
  122.     //
  123.     TComboBox* DrivePane;
  124.  
  125.     // List of directories
  126.     //
  127.     TStatic*  DirPaneLabel;
  128.     TListBox* DirPane;
  129.  
  130.     TBrowserWindow* Browser;       // Our parent
  131.  
  132.   DECLARE_RESPONSE_TABLE(TFindPane);
  133. };
  134.  
  135. //
  136. // Main client layout window
  137. //
  138. class TBrowserWindow : public TLayoutWindow {
  139.   public:
  140.     TBrowserWindow();
  141.    ~TBrowserWindow()
  142.     {
  143.       delete CaptionFont;
  144.     }
  145.  
  146.     void SetupWindow();
  147.  
  148.     // Notification Handlers
  149.     //
  150.     void SelectFile();
  151.  
  152.     TFont*     CaptionFont;  // Used for pane labels
  153.     TFindPane* FindPane;
  154.  
  155.     // List of files
  156.     //
  157.     TStatic*   FilePaneLabel;
  158.     TListBox*  FilePane;
  159.     TMyEdit*   EditPane;
  160.  
  161.   DECLARE_RESPONSE_TABLE(TBrowserWindow);
  162. };
  163.  
  164. //----------------------------------------------------------------------------
  165.  
  166. DEFINE_RESPONSE_TABLE1(TFindPane, TLayoutWindow)
  167.   EV_LBN_SELCHANGE(ID_DirPane, SelectDir),
  168.   EV_CBN_CLOSEUP(ID_DrivePane, SelectDrive),
  169.   EV_BN_CLICKED(ID_FilterPaneLabel, SelectFilter),
  170.   EV_WM_SIZE,
  171. END_RESPONSE_TABLE;
  172.  
  173. TFindPane::TFindPane(TBrowserWindow* parent)
  174. :
  175.   TLayoutWindow(parent),
  176.   TWindow(parent, 0, 0)
  177. {
  178.   Browser = parent;
  179.   TLayoutMetrics lm;
  180.  
  181.   lm.X.Units = lm.Y.Units = lm.Width.Units = lm.Height.Units = lmPixels;
  182.  
  183.   // If layout window has a border, then it will automatically adjust
  184.   // children by 1 pixel
  185.   //
  186. //  Attr.Style |= WS_BORDER;
  187.  
  188.   DrivePane = new TComboBox(this, ID_DrivePane, 0, 0, 0, 0,
  189.     CBS_DROPDOWNLIST|CBS_SORT|CBS_NOINTEGRALHEIGHT, MAXDIR);
  190.  
  191.   DirPaneLabel = new TStatic(this, ID_DirPaneLabel, "Directory", 0, 0, 0, 0, 0);
  192.   DirPaneLabel->Attr.Style |= SS_CENTER;
  193.  
  194.   DirPane = new TListBox(this, ID_DirPane, 0, 0, 0, 0);
  195.   DirPane->Attr.Style |= LBS_NOINTEGRALHEIGHT;
  196.  
  197.   FilterPaneLabel = new TButton(this, ID_FilterPaneLabel, "Filter", 0, 0, 0, 0);
  198.   FilterPaneLabel->Attr.Style |= SS_CENTER;
  199.  
  200.   FilterPane = new TEdit(this, ID_FilterPane, 0, 0, 0, 0, 0, 0);
  201.  
  202.  
  203.   // (Drive &) DirPaneLabel
  204.   //
  205.   lm.Width.SameAs(lmParent, lmRight);
  206.   lm.Height.Absolute(CaptionHeight);
  207.   lm.X.SameAs(lmParent, lmLeft);
  208.   lm.Y.SameAs(lmParent, lmTop);
  209.   SetChildLayoutMetrics(*DirPaneLabel, lm);
  210.  
  211.   // Drive Pane
  212.   //
  213.   lm.Width.SameAs(lmParent, lmRight);
  214.   lm.Height.Absolute(5*EditHeight);
  215.   lm.X.SameAs(lmParent, lmLeft);
  216.   lm.Y.Below(DirPaneLabel);
  217.   SetChildLayoutMetrics(*DrivePane, lm);
  218.  
  219.   // Dir Pane
  220.   //
  221.   lm.X.SameAs(lmParent, lmLeft);
  222.   lm.Y.Below(DrivePane, -(4 * EditHeight));
  223.   lm.Width.SameAs(lmParent, lmRight);
  224.   lm.Height.Above(FilterPaneLabel);
  225.   SetChildLayoutMetrics(*DirPane, lm);
  226.  
  227.   // FilterPaneLabel
  228.   //
  229.   lm.Width.Absolute(ButtonWidth);
  230.   lm.Height.Absolute(EditHeight);
  231.   lm.X.SameAs(lmParent, lmLeft);
  232.   lm.Y.Set(lmBottom, lmSameAs, lmParent, lmBottom);
  233.   SetChildLayoutMetrics(*FilterPaneLabel, lm);
  234.  
  235.   // FilterPane
  236.   //
  237.   lm.Width.SameAs(lmParent, lmRight);
  238.   lm.Height.Absolute(EditHeight);     // Would be nicer to calculate size
  239.   lm.X.RightOf(FilterPaneLabel);
  240.   lm.Y.SameAs(FilterPaneLabel, lmTop);
  241.   SetChildLayoutMetrics(*FilterPane, lm);
  242.  
  243.   strcpy(Filter, "*.*");
  244.  
  245.   SetBkgndColor(TColor::Sys3dFace);
  246. }
  247.  
  248. void
  249. TFindPane::SetupWindow()
  250. {
  251.   char dirBuf[MAXDIR+1];
  252.   char *ptr;
  253.   TLayoutWindow::SetupWindow();
  254.  
  255.   // Fill list box with drives and directories
  256.   //
  257.   DirPane->DirectoryList(DDL_DIRECTORY | DDL_EXCLUSIVE, "*.*");
  258.   DrivePane->DirectoryList(DDL_DRIVES | DDL_EXCLUSIVE, "*.*");
  259.  
  260.   // Set edit field in drive list
  261.   //
  262.   dirBuf[0] = (char)(getdisk()+'A');
  263.   dirBuf[1] = 0;
  264.   DrivePane->SetSelString(dirBuf, 0);
  265.  
  266.   // Update label with current drive and directory
  267.   //
  268.   ptr = getcwd(dirBuf, sizeof(dirBuf));
  269.  
  270.   DirPaneLabel->SetWindowFont(*Browser->CaptionFont, false);
  271.   FilterPaneLabel->SetWindowFont(*Browser->CaptionFont, false);
  272.   DirPaneLabel->SetText(ptr);
  273.   FilterPane->SetText(Filter);
  274. }
  275.  
  276. //
  277. // Make sure comboboxes are not dropped when the window is re-sized or moved
  278. // if they are, they'll be left floating
  279. //
  280. void
  281. TFindPane::Paint(TDC& dc, bool erase, TRect& rect)
  282. {
  283.   if (DrivePane)
  284.     DrivePane->HideList();
  285.  
  286.   TLayoutWindow::Paint(dc, erase, rect);
  287. }
  288.  
  289. //
  290. // make sure comboboxes are not dropped when the window is re-sized or moved
  291. // if they are, they'll be left floating
  292. //
  293. void
  294. TFindPane::EvSize(uint sizeType, TSize& size)
  295. {
  296.   if (DrivePane)
  297.     DrivePane->HideList();
  298.  
  299.   TLayoutWindow::EvSize(sizeType, size);
  300. }
  301.  
  302. void
  303. TFindPane::SelectDrive()
  304. {
  305.   char buf[MaxEntry];    // Directory names look like [anydir]
  306.  
  307.   DrivePane->GetSelString(buf, sizeof buf);
  308.   setdisk(buf[2]-'a');   // Directories are shown in list box as [-a-]
  309.  
  310.   Update();
  311. }
  312.  
  313. void
  314. TFindPane::SelectDir()
  315. {
  316.   char buf[MaxEntry];     // Directory names look like [anydir]
  317.   DirPane->GetSelString(buf, sizeof buf);
  318.   char* ptr = buf+1;      // Strip leading [
  319.   buf[strlen(buf)-1] = 0; // Strip trailing ]
  320.   chdir(ptr);
  321.  
  322.   Update();
  323. }
  324.  
  325. void
  326. TFindPane::SelectFilter()
  327. {
  328.   char buf[256];
  329.   FilterPane->GetText(buf, sizeof(buf));  // Get text from edit control
  330.   strcpy(Filter, buf);
  331.   Update();
  332. }
  333.  
  334. void
  335. TFindPane::Update()
  336. {
  337.   // Update title bar with new directory
  338.   //
  339.   char dirBuf[MAXDIR];
  340.   char* ptr = getcwd(dirBuf, sizeof(dirBuf));
  341.   if (ptr)
  342.     DirPaneLabel->SetText(ptr);
  343.  
  344.   DirPane->ClearList();
  345.   DirPane->DirectoryList(DDL_DIRECTORY|DDL_EXCLUSIVE, "*.*");
  346.   Browser->FilePane->ClearList();
  347.   Browser->FilePane->DirectoryList(0, Filter);  // Update file list
  348. }
  349.  
  350. //----------------------------------------------------------------------------
  351.  
  352. DEFINE_RESPONSE_TABLE1(TBrowserWindow,TLayoutWindow)
  353.   EV_LBN_SELCHANGE(ID_FilePane,SelectFile),
  354. END_RESPONSE_TABLE;
  355.  
  356. TBrowserWindow::TBrowserWindow()
  357. :
  358.   TLayoutWindow(0),
  359.   TWindow(0, 0, 0)
  360. {
  361.   TLayoutMetrics lm;
  362.  
  363.   lm.X.Units = lm.Y.Units = lm.Width.Units = lm.Height.Units = lmPixels;
  364.  
  365.   // If layout window has a border, then it will automatically adjust
  366.   // children by 1 pixel
  367.   //
  368.   Attr.Style |= WS_BORDER;
  369.  
  370.   // Find a font that will fit into our small captions and buttons
  371.   //
  372.   CaptionFont = new TFont(
  373.     "Small Fonts",              // facename
  374.     -(CaptionHeight),           // height,
  375.     0, 0, 0, FW_NORMAL,         // width, esc, orientation, weight
  376.     VARIABLE_PITCH | FF_SWISS,  // Pitch and Family
  377.     false, false, false,        // Italic, Underline, Strikeout
  378.     ANSI_CHARSET,               // Charset
  379.     OUT_CHARACTER_PRECIS,       // Output precision
  380.     CLIP_DEFAULT_PRECIS,        // Clip precision
  381.     PROOF_QUALITY               // Quality
  382.   );
  383.  
  384.   FindPane = new TFindPane(this);
  385.  
  386.   lm.Width.PercentOf(lmParent, lmRight, 50);
  387.   lm.Height.PercentOf(lmParent, lmBottom, 50);
  388.   lm.X.SameAs(lmParent, lmLeft);
  389.   lm.Y.SameAs(lmParent, lmTop);
  390.   SetChildLayoutMetrics(*FindPane, lm);
  391.  
  392.   // File Pane ----------------------------------------------------------
  393.   //
  394.   FilePaneLabel = new TStatic(this, ID_FilePaneLabel, "Files", 0, 0, 0, 0, 0);
  395.   FilePaneLabel->Attr.Style |= SS_CENTER;
  396.  
  397.   lm.Width.SameAs(lmParent, lmRight);
  398.   lm.Height.Absolute(CaptionHeight);
  399.   lm.X.RightOf(FindPane, 1);
  400.   lm.Y.SameAs(FindPane, lmTop);
  401.   SetChildLayoutMetrics(*FilePaneLabel, lm);
  402.  
  403.   FilePane = new TListBox(this, ID_FilePane, 0, 0, 0, 0);
  404.  
  405.   // Prevent list box from adjusting it's size to avoid partial lines
  406.   //
  407.   FilePane->Attr.Style |= LBS_NOINTEGRALHEIGHT;
  408.  
  409.   lm.Width.SameAs(lmParent, lmRight);
  410.   lm.Height.SameAs(FindPane, lmBottom);
  411.   lm.X.RightOf(FindPane, 1);
  412.   lm.Y.Below(FilePaneLabel);
  413.   SetChildLayoutMetrics(*FilePane, lm);
  414.  
  415.   EditPane = new TMyEdit(this, ID_EditPane);
  416.   lm.Width.SameAs(lmParent, lmRight);
  417.   lm.Height.SameAs(lmParent, lmBottom);
  418.   lm.X.SameAs(lmParent, lmLeft);
  419.   lm.Y.Below(FindPane, 1);
  420.   SetChildLayoutMetrics(*EditPane, lm);
  421.  
  422.   SetBkgndColor(TColor::Sys3dFace);
  423. }
  424.  
  425. void
  426. TBrowserWindow::SetupWindow()
  427. {
  428.   TLayoutWindow::SetupWindow();
  429.  
  430.   // Fill list box with list of files in current directory
  431.   //
  432.   FilePane->DirectoryList(0, FindPane->Filter);
  433.  
  434.   // Switch to our small font
  435.   //
  436.   FilePaneLabel->SetWindowFont(*CaptionFont, false);
  437. }
  438.  
  439. void
  440. TBrowserWindow::SelectFile()
  441. {
  442.   char buf[MaxEntry];
  443.  
  444.   FilePane->GetSelString(buf, sizeof buf);
  445.  
  446.   // Here's where doc/view would allow us to switch to different views
  447.   // for different docs easily.
  448.   // For now, we'll get an error if we try to view a .exe file
  449.   // or a file that's too big for the edit buffer.
  450.   //
  451.   if (EditPane->Read(buf)) {
  452.     EditPane->Invalidate();
  453.     EditPane->SetFileName(buf);
  454.   }
  455.   else {
  456.     EditPane->SetText("File Error");
  457.   }
  458. }
  459.  
  460. //----------------------------------------------------------------------------
  461.  
  462. class TFileBrowserApp : public TApplication {
  463.   public:
  464.     TFileBrowserApp() : TApplication("") {}
  465.  
  466.     void InitMainWindow();
  467. };
  468.  
  469. void
  470. TFileBrowserApp::InitMainWindow()
  471. {
  472.   MainWindow = new TFrameWindow(0, "Owl File Browser", new TBrowserWindow());
  473.   MainWindow->SetIcon(this, IDC_1);
  474.   MainWindow->Attr.H = 300;
  475.   MainWindow->Attr.W = 300;
  476. }
  477.  
  478. int
  479. OwlMain(int /*argc*/, char* /*argv*/ [])
  480. {
  481.   return TFileBrowserApp().Run();
  482. }
  483.