home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / c / fldrvw / fldrview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-27  |  14.4 KB  |  434 lines

  1. /********************************************************************/
  2. /*            Folder View - Drag and drop directory viewer          */
  3. /*               Public Domain (P) 1993  Benjamin Cooley            */
  4. /*                fldrview.cpp - FolderView Main Module             */
  5. /********************************************************************/
  6.  
  7. // FolderViews is an orphaned project, begun with the best intentions
  8. // before its poor overworked programmer was sucked up into other
  9. // more pressing projects.  Please give FolderViews a home, and a new
  10. // life.
  11.  
  12. // FolderViews is Donationware.. $15 for the program itself, and $10
  13. // dollars if your are going to use the source in another product.
  14. // Donationware means payment for use is purely VOLUNTARY, but
  15. // wouldn't you feel guilty if you finished FolderViews and were
  16. // making zogs of money via shareware and didn't give the poor
  17. // original programmer his $25 measly bucks?
  18.  
  19. // Please feel free to expand upon FolderViews and publish any
  20. // derivitives as shareware, public domain, or even as an actual
  21. // product.  All I ask is that you actually IMPROVE upon the product,
  22. // that the new product remains upwards compatible with the old,
  23. // and that any new product remains as compatible as possible with
  24. // Windows conventions, control styles, dialog styles, etc. Also,
  25. // feel free to change the name, but mention me (Ben Cooley), and
  26. // FolderViews in the about box if you could.
  27.  
  28. // As this product is not complete, please feel free to finish..
  29. //
  30. // o File copying, moving, deleting, etc.
  31. // o Add lots of PRE-SETUP directory views for commonly used applications.
  32. //   Folderviews tries to find the directory name of the application in
  33. //   its view database before creating a new entry.  Predefined views
  34. //   for many popular programs could be put in this database so that when
  35. //   the user installed FolderVies, he wouldn't have to set up all his
  36. //   directories himself. This would probably be the most usefull addition
  37. //   to the basic program.
  38. // o Add a GOOD default view setup that would display the folders and exe
  39. //   files in a directory, and have the various other groups one might use
  40. //   iconified underneath.
  41. // o Add an 'Association..' menu item to the File menu that would work
  42. //   like File Managers association menu.
  43. // o Implementing drag and drop server code.
  44. // o Adding the ability to drop group, file, or view icons into a Program
  45. //   Manager window
  46. // o Backup and restore
  47. // o Drive views available from program manager via a toolbar with drive
  48. //   icons for each disk in system. (Wish I could of added this).
  49. // o Completion of the 'Desktop' project.
  50. // o Search
  51. // o Directory creating, renaming, moving
  52. // o Help
  53. // o A ZIP view DLL that allows a view window to be displayed for
  54. //   a zip file.
  55. // o Interface to 3rd party DLL 'view' drivers so that other up and
  56. //   coming programs can build view drivers for different kinds of
  57. //   compression formats.
  58. // o Make FolderViews understand that floppy disks are not hard disks
  59. // o Teach FolderViews Desktop that CD-ROMS aren't Netword Drives
  60. // o Keep background windows from coming to the top immediately if your
  61. //   going to drag an icon.
  62. // o Add more OS/2 type Workplace Shell stuff with the right button click
  63. //   thing.
  64. // o Put the small view of the icon in the close box for the MDI group
  65. //   windows, and possibly even the main view windows.
  66. // o Make the view intelligently set up the group windows when a directory
  67. //   is first displayed.
  68.  
  69.  
  70.  
  71. #include "include.h"
  72. #pragma hdrstop
  73.  
  74. #include "fldrview.h"
  75.  
  76. /********************************************************************/
  77. /*                             Variables                            */
  78. /********************************************************************/
  79.  
  80. /* FolderView main window */
  81. PTMainView FolderView;
  82.  
  83. /* Program global values */
  84. HINSTANCE hInstance;
  85. char ProgramName[] = "Folder Views";
  86. char TextBuf[TEXTBUFLEN];
  87. char FolderViewFileName[MAXPATH];
  88. char FolderViewPath[MAXPATH];
  89. char DefaultFolderInfoDir[] = "FOLDERS\\";
  90. char FolderInfoPath[MAXPATH];
  91. char FolderInfoFileName[] = "FLDR0000.FIF";
  92. char FolderInfoWildcard[] = "FLDR*.FIF";
  93. int FolderInfoFileCountPos = 4;
  94. int FolderInfoFileCountLen = 4;
  95. char DefaultFolderInfoFileName[] = "DEFAULT.FIF";
  96. char DirFolderInfoFileName[] = "FOLDER.FIF";
  97. char ProfileSettings[] = "Settings";
  98. char ProfileDirectories[] = "Folders";
  99. char ProfileName[] = "FLDRVIEW.INI";
  100. char FileIconName[] = "[FileIcon]";
  101. char DirIconName[] = "[DirIcon]";
  102. char ProgIconName[] = "[ProgIcon]";
  103. char BatchIconName[] = "[BatchIcon]";
  104. char PifIconName[] = "[PifIcon]";
  105. int IconWidth, IconHeight, SmallIconWidth, SmallIconHeight;
  106. int IconSpaceWidth, IconSpaceHeight;
  107. int DescWidth, DescHeight, WrapDesc;
  108. int IconNameWidth, IconNameHeight;
  109. char FontName[MAXFONTNAME];
  110. int FontPointSize;
  111. HFONT Font;
  112. COLORREF TextColor, TextBkColor, SelectedTextColor, SelectedTextBkColor; 
  113. COLORREF WindowColor;
  114. HBRUSH WindowBrush, SelectedBrush;
  115. int IconLeft, IconTop;
  116. HDC MemDC;
  117. HBITMAP MemBitMap;
  118. HFONT MemFont;
  119. HBRUSH MemBrush;
  120. int AllNameWidth, AllDateWidth, AllTimeWidth, AllSizeWidth,
  121.     AllAttribWidth, AllDescWidth;
  122. BOOL ShuttingDown = FALSE;    // True when shutting down
  123. BOOL MouseDown = FALSE;
  124. PTIcon MouseDownIcon = NULL;
  125. int MouseDownIconX, MouseDownIconY;
  126. BOOL Dragging = FALSE;
  127. HCURSOR DragIconCursor, DragCursor;
  128.  
  129. /* DIB Bitmap structures */
  130. static long palette2[] = { 0x000000L, 0xFFFFFFL };
  131.  
  132. static long palette16[] =
  133. { 0x000000L, 0x800000L, 0x008000L, 0x808000L,
  134.   0x000080L, 0x800080L, 0x008080L, 0xC0C0C0L,
  135.   0x808080L, 0xFF0000L, 0x00FF00L, 0xFFFF00L,
  136.   0x0000FFL, 0xFF00FFL, 0x00FFFFL, 0xFFFFFFL };
  137.  
  138. static char bmid2[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 2];
  139. BITMAPINFO *bmi2 = (BITMAPINFO *)bmid2;
  140. BITMAPINFOHEADER *bmih2 = (BITMAPINFOHEADER *)bmid2;
  141. static char bmid16[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 16];
  142. BITMAPINFO *bmi16 = (BITMAPINFO *)bmid16;
  143. BITMAPINFOHEADER *bmih16 = (BITMAPINFOHEADER *)bmid16;
  144.  
  145. /* Icon List */
  146. TIconList IconList;
  147.  
  148. /********************************************************************/
  149. /*                    TMainApp Application Class                    */
  150. /********************************************************************/
  151.  
  152. _CLASSDEF(TMainApp)
  153. class TMainApp : public TApplication
  154. {
  155. public:
  156.     TMainApp(LPSTR AName, HANDLE hInstance, HANDLE hPrevInstance,
  157.         LPSTR lpCmdLine, int nCmdShow);
  158.     virtual ~TMainApp();
  159.   virtual void InitMainWindow();
  160.   virtual void InitInstance();
  161. };
  162.  
  163. TMainApp::TMainApp(LPSTR AName, HANDLE hInstance, HANDLE hPrevInstance,
  164.     LPSTR lpCmdLine, int nCmdShow):
  165.     TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  166. {
  167.     char buf[10];
  168.  
  169.   /* Set Global Instance */
  170.     ::hInstance = hInstance;
  171.  
  172.   /* Get Folderview Path */
  173.     GetModuleFileName(hInstance, FolderViewFileName, MAXPATH);
  174.     strcpy(FolderViewPath, FolderViewFileName);
  175.     Pchar ptr = FolderViewPath + strlen(FolderViewPath);
  176.     while (ptr > FolderViewPath && *ptr != '\\')
  177.         *(ptr--) = NULL;
  178.  
  179.   /* Get global mememory dc */
  180.     HDC dc = GetDC(NULL);
  181.     MemDC = CreateCompatibleDC(dc);
  182.     ReleaseDC(NULL, dc);
  183.  
  184.   /* Icon width and height */
  185.     IconWidth = GetSystemMetrics(SM_CXICON);
  186.     IconHeight = GetSystemMetrics(SM_CYICON);
  187.  
  188.   /* FolderInfoPathEntry entry */
  189.     char FolderInfoPathEntry[] = "FolderInfoPath";
  190.     GetPrivateProfileString(
  191.       ProfileSettings, FolderInfoPathEntry, "",
  192.       FolderInfoPath, MAXPATH, ProfileName);
  193.     if (!*FolderInfoPath)
  194.     {
  195.         strcpy(FolderInfoPath, FolderViewPath);
  196.         strcat(FolderInfoPath, DefaultFolderInfoDir);
  197.         WritePrivateProfileString(
  198.             ProfileSettings, FolderInfoPathEntry, "", ProfileName);
  199.         // Note: 'FolderInfoPath=' empty means uses program directory
  200.     }
  201.     else if (*(FolderInfoPath + strlen(FolderInfoPath) - 1) != '\\')
  202.     {
  203.         strcat(FolderInfoPath, "\\"); // Make sure path hase '\' at end
  204.         WritePrivateProfileString(
  205.             ProfileSettings, FolderInfoPathEntry, FolderInfoPath, ProfileName);
  206.     }
  207.  
  208.   /* IconSpacingWidth entry */
  209.     char IconSpaceWidthEntry[] = "IconSpacingWidth";
  210.     IconSpaceWidth = GetPrivateProfileInt(
  211.       ProfileSettings, IconSpaceWidthEntry, 0, ProfileName);
  212.     if (IconSpaceWidth == 0)
  213.     {
  214.         IconSpaceWidth = GetSystemMetrics(SM_CXICONSPACING);
  215.         WritePrivateProfileString(
  216.             ProfileSettings, IconSpaceWidthEntry,
  217.             itoa(IconSpaceWidth, buf, 10), ProfileName);
  218.     }
  219.  
  220.   /* IconSpacingHeight entry */
  221.     char IconSpaceHeightEntry[] = "IconSpacingHeight";
  222.     IconSpaceHeight = GetPrivateProfileInt(
  223.       ProfileSettings, IconSpaceHeightEntry, 0, ProfileName);
  224.     if (IconSpaceHeight == 0)
  225.     {
  226.         IconSpaceHeight = GetSystemMetrics(SM_CYICONSPACING);
  227.         WritePrivateProfileString(
  228.             ProfileSettings, IconSpaceHeightEntry,
  229.             itoa(IconSpaceHeight, buf, 10), ProfileName);
  230.     }
  231.  
  232.   /* FontName entry */
  233.     char FontNameEntry[] = "FontName";
  234.     GetPrivateProfileString(
  235.       ProfileSettings, FontNameEntry, "",
  236.       FontName, MAXFONTNAME, ProfileName);
  237.     if (!*FontName)
  238.     {
  239.         strcpy(FontName, "Helv");
  240.         WritePrivateProfileString(
  241.             ProfileSettings, FontNameEntry, FontName, ProfileName);
  242.     }
  243.  
  244.   /* FontPointSize entry */
  245.     char FontPointSizeEntry[] = "FontPointSize";
  246.     FontPointSize = GetPrivateProfileInt(
  247.       ProfileSettings, FontPointSizeEntry, 0, ProfileName);
  248.     if (FontPointSize == 0)
  249.     {
  250.         FontPointSize = 8;
  251.         WritePrivateProfileString(
  252.             ProfileSettings, FontPointSizeEntry, "8", ProfileName);
  253.     }
  254.  
  255.   /* Initialize global resources */
  256.     Font = CreateFont(FontPointSize,0,0,0,0,0,0,0,0,0,0,0,0,FontName);
  257.     TextColor = GetSysColor(COLOR_WINDOWTEXT);
  258.     TextBkColor = GetSysColor(COLOR_WINDOW);
  259.     SelectedTextColor = GetSysColor(COLOR_HIGHLIGHTTEXT);
  260.     SelectedTextBkColor = GetSysColor(COLOR_HIGHLIGHT);
  261.     WindowColor = TextBkColor;
  262.     WindowBrush = CreateSolidBrush(WindowColor);
  263.     SelectedBrush = CreateSolidBrush(SelectedTextBkColor);
  264.     IconLeft = (IconSpaceWidth - IconWidth) / 2;
  265.     if (IconLeft < 0) IconLeft = 0;
  266.     IconTop = (IconSpaceHeight - IconHeight - (FontPointSize + 4)) / 2;
  267.     if (IconTop < 0) IconTop = 0;
  268.     SmallIconWidth = 16;
  269.     SmallIconHeight = 16;
  270.     IconNameWidth = FontPointSize * 11 + SmallIconWidth;
  271.     IconNameHeight = FontPointSize + 4;
  272.     if (IconNameHeight < SmallIconHeight + 2)
  273.         IconNameHeight = SmallIconHeight + 2;
  274.     AllNameWidth = FontPointSize * 8;
  275.     AllDateWidth = FontPointSize * 7;
  276.     AllTimeWidth = FontPointSize * 6;
  277.     AllSizeWidth = FontPointSize * 8;
  278.     AllAttribWidth = FontPointSize * 5;
  279.     AllDescWidth = FontPointSize * 50;
  280.  
  281.   /* Initialize DIB stuff */
  282.     bmih2->biSize = sizeof(BITMAPINFOHEADER);
  283.     bmih2->biPlanes = 1;
  284.     bmih2->biBitCount = 1;
  285.     bmih2->biCompression = BI_RGB;
  286.     memcpy(bmi2->bmiColors, palette2, 4 * 2);
  287.     bmih16->biSize = sizeof(BITMAPINFOHEADER);
  288.     bmih16->biPlanes = 1;
  289.     bmih16->biBitCount = 4;
  290.     bmih16->biCompression = BI_RGB;
  291.     memcpy(bmi16->bmiColors, palette16, 4 * 16);
  292.  
  293.   /* Get Icons */
  294.     IconList.AddIconImage(new TIconImage(FileIconName, 0,
  295.         LoadIcon(hInstance, MAKEINTRESOURCE(ICO_File)) ));
  296.     IconList.AddIconImage(new TIconImage(DirIconName, 0, 
  297.         LoadIcon(hInstance, MAKEINTRESOURCE(ICO_Directory)) ));
  298.     IconList.AddIconImage(new TIconImage(BatchIconName, 0,
  299.         LoadIcon(hInstance, MAKEINTRESOURCE(ICO_Batch)) ));
  300.     IconList.AddIconImage(new TIconImage(PifIconName, 0, 
  301.         LoadIcon(hInstance, MAKEINTRESOURCE(ICO_Pif)) ));
  302.     IconList.AddIconImage(new TIconImage(ProgIconName, 0, 
  303.         LoadIcon(hInstance, MAKEINTRESOURCE(ICO_Program)) ));
  304.  
  305. }
  306.  
  307. TMainApp::~TMainApp()
  308. {
  309.  
  310.   /* Delete global dc */
  311.       DeleteDC(MemDC);
  312.  
  313.   /* Free global resources */
  314.     if (Font) DeleteObject(Font);
  315.     if (WindowBrush) DeleteObject(WindowBrush);
  316.     if (SelectedBrush) DeleteObject(SelectedBrush);
  317.  
  318. }
  319.  
  320. void TMainApp::InitMainWindow()
  321. {
  322.     char filepath[MAXPATH];
  323.     char filename[FILELEN];
  324.     BOOL fileexists = FALSE;
  325.     unsigned dummy;
  326.  
  327.     BOOL wildcard =
  328.         lpCmdLine[strlen(lpCmdLine) - 1] != '\\';
  329.  
  330.   /* Look for full path in [Directories] section */
  331.     GetPrivateProfileString(ProfileDirectories, lpCmdLine,
  332.         "", filename, FILELEN, ProfileName);
  333.     if (*filename)
  334.     {
  335.         strcpy(filepath, FolderInfoPath);
  336.         strcat(filepath, filename);
  337.     }
  338.  
  339.   /* If full path not there, try just the directory name */
  340.     if (!*filename)
  341.     {
  342.         GetPrivateProfileString(ProfileDirectories, lpCmdLine + 3,
  343.             "", filename, FILELEN, ProfileName);
  344.         if (*filename)
  345.         {
  346.             strcpy(filepath, FolderViewPath);
  347.             strcat(filepath, filename);
  348.         }
  349.     }
  350.  
  351.   /* See if setup in FOLDERS directory exists */
  352.     if (*filename)
  353.         if (!_dos_getfileattr(filepath, &dummy))
  354.             fileexists = TRUE;
  355.  
  356.   /* If no user setup in FOLDERS directory, try current directory */
  357.     if (!fileexists && !wildcard) // Don't bother if wildcard view
  358.     {
  359.         strcpy(filepath, lpCmdLine);
  360.         strcat(filepath, DirFolderInfoFileName);
  361.         if (!_dos_getfileattr(filepath, &dummy))
  362.             fileexists = TRUE;
  363.     }
  364.  
  365.   /* If neither user, or directory setup exists, use default setup file */
  366.     if (!fileexists && !wildcard)
  367.     {
  368.         strcpy(filepath, FolderViewPath);
  369.         strcat(filepath, DefaultFolderInfoFileName);
  370.         if (!_dos_getfileattr(filename, &dummy))
  371.             fileexists = TRUE;
  372.     } 
  373.  
  374.   /* If we have a file, try to load setup from it */
  375.     if (fileexists)
  376.     {
  377.         ifpstream is(filepath);
  378.         if (!is.fail())
  379.         {
  380.             Pchar buf = new char[FILEBUFSIZE];
  381.             is.setbuf(buf, FILEBUFSIZE);
  382.             is >> FolderView;
  383.             is.close();
  384.             delete buf;
  385.         }
  386.         else fileexists = FALSE; // Drop down to no setup file below
  387.     }
  388.  
  389.   /* If no setup file, start from scratch */
  390.     if (!fileexists)
  391.     {
  392.         FolderView = new TMainView(lpCmdLine);
  393.     }
  394.  
  395.     MainWindow = FolderView;
  396. }
  397.  
  398. void TMainApp::InitInstance()
  399. {
  400.     TApplication::InitInstance();
  401.  
  402.   /* Load accelerators */
  403.     HAccTable = LoadAccelerators(hInstance,
  404.         MAKEINTRESOURCE(ACC_Accelerator));
  405. }
  406.  
  407. /********************************************************************/
  408. /*                           Main Program                           */
  409. /********************************************************************/
  410.  
  411. char dirbuf[MAXDIR];
  412.  
  413. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  414.   LPSTR lpCmdLine, int nCmdShow)
  415. {
  416.  
  417.     Pchar dir;
  418.     if (!lpCmdLine || !*lpCmdLine)
  419.     {
  420.         strcpy(dirbuf, "?:\\");
  421.         dirbuf[0] = _getdrive() + 'A' - 1;
  422.         getcurdir(0, dirbuf + 3);
  423.         strcat(dirbuf, "\\");
  424.         dir = dirbuf;
  425.     }
  426.     else dir = lpCmdLine;
  427.     strupr(dir);
  428.             
  429.     TMainApp MainApp("FolderView", hInstance, hPrevInstance,
  430.       dir, nCmdShow);
  431.     MainApp.Run();
  432.     return MainApp.Status;
  433. }
  434.