home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / FILEBROW.PAK / FILEBROW.CPP next >
C/C++ Source or Header  |  1995-08-29  |  12KB  |  462 lines

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