home *** CD-ROM | disk | FTP | other *** search
/ Executor 2.0 / executorv2.0.iso / pc / dos / extra / source / browser / browser.hqx / Browser / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-29  |  12.8 KB  |  523 lines

  1. #include "go.h"
  2. #include "display.h"
  3. #include "xfer.h"
  4. #include <unix.h>
  5. #include <assert.h>
  6. #include <string.h>
  7.  
  8. #include "misc.proto.h"
  9. #include "update.proto.h"
  10. #include "inithotband.proto.h"
  11. #include "initicons.proto.h"
  12. #include "mouse.proto.h"
  13.  
  14. #include "windlist.h"
  15.  
  16. /* todo: move to .h file */
  17. #define HELPSCROLLBAR    3
  18. #define HELPPICTITEM    2
  19. #define HELPDIALOGID    204
  20. #define HELPPICT        1000
  21.  
  22. pascal void
  23. helpscroll (ControlHandle c, short part)
  24. {
  25.   short v, v2, pagesize;
  26.   short type;
  27.   Rect r;
  28.   Handle h;
  29.   RgnHandle rgn, savergn;
  30.  
  31.   v = GetCtlValue (c);
  32.   pagesize = (*c)->contrlRect.bottom - (*c)->contrlRect.top - 3;
  33.   switch (part)
  34.     {
  35.     case inUpButton:
  36.       SetCtlValue (c, v - SCROLLSPEED);
  37.       break;
  38.     case inDownButton:
  39.       SetCtlValue (c, v + SCROLLSPEED);
  40.       break;
  41.     case inPageDown:
  42.       SetCtlValue (c, v + pagesize);
  43.       break;
  44.     case inPageUp:
  45.       SetCtlValue (c, v - pagesize);
  46.       break;
  47.     default:
  48.       break;
  49.     }
  50.   v2 = GetCtlValue (c);
  51.   GetDItem ((*c)->contrlOwner, HELPPICTITEM, &type, &h, &r);
  52.   rgn = NewRgn ();
  53.   ScrollRect (&r, 0, v - v2, rgn);
  54. #if 0
  55.   InvalRgn (rgn);
  56. #else
  57.   savergn = (*c)->contrlOwner->clipRgn;
  58.   (*c)->contrlOwner->clipRgn = rgn;
  59.   drawhelp ((*c)->contrlOwner, HELPPICTITEM);
  60.   (*c)->contrlOwner->clipRgn = savergn;
  61. #endif
  62.   DisposeRgn (rgn);
  63. }
  64.  
  65. pascal void
  66. drawhelp (DialogPtr dp, short item)
  67. {
  68.   PicHandle ph;
  69.   Rect r, r2;
  70.   ControlHandle c;
  71.   short offset, type;
  72.   Handle h;
  73.  
  74.   ph = (PicHandle) GetResource ('PICT', HELPPICT);
  75.   GetDItem (dp, HELPSCROLLBAR, &type, (Handle *) & c, &r);
  76.   offset = GetCtlValue (c);
  77.   GetDItem (dp, item, &type, &h, &r);
  78.   InsetRect (&r, -1, -1);
  79.   FrameRect (&r);
  80.   InsetRect (&r, 1, 1);
  81.   ClipRect (&r);
  82.   SetRect (&r2, r.left, r.top - offset,
  83.        r.left + (*ph)->picFrame.right - (*ph)->picFrame.left,
  84.        r.top - offset + (*ph)->picFrame.bottom - (*ph)->picFrame.top);
  85.   CopyBits (&((GrafPtr) (*c)->contrlRfCon)->portBits, &(*c)->contrlOwner->portBits,
  86.         &(*ph)->picFrame, &r2, patCopy, 0);
  87.   ClipRect (&dp->portRect);
  88. }
  89.  
  90. pascal char
  91. helpfilter (DialogPtr dp, EventRecord * ev, short *item)
  92. {
  93.   ControlHandle c;
  94.   short part;
  95.   Point pt;
  96.   GrafPtr saveport;
  97.  
  98.   if (ev->what == keyDown && (((ev->message & charCodeMask) == '\n') ||
  99.                   ((ev->message & charCodeMask) == 13)))
  100.     {
  101.       *item = 1;
  102.       return true;
  103.     }
  104.   else if (ev->what == mouseDown)
  105.     {
  106.       GetPort (&saveport);
  107.       SetPort (dp);
  108.       pt = ev->where;
  109.       GlobalToLocal (&pt);
  110.       part = FindControl (pt, dp, &c);
  111.       switch (part)
  112.     {
  113.     case inThumb:
  114.       TrackControl (c, pt, 0);
  115.       drawhelp ((*c)->contrlOwner, HELPPICTITEM);
  116.       break;
  117.     case inUpButton:
  118.     case inDownButton:
  119.     case inPageUp:
  120.     case inPageDown:
  121.       TrackControl (c, pt, helpscroll);
  122.       break;
  123.     default:
  124.       break;
  125.     }
  126.       SetPort (saveport);
  127.     }
  128.   return false;
  129. }
  130.  
  131. void
  132. help (void)
  133. {
  134.   short item;
  135.   PicHandle ph;
  136.   Rect r;
  137.   ControlHandle c;
  138.   short type;
  139.   Handle h;
  140.   DialogPtr dp;
  141.   GrafPtr gp;
  142.   GrafPtr saveport;
  143.  
  144.   dp = GetNewDialog (HELPDIALOGID, 0, 0);
  145. /* TODO: adjust the window size based upon the screen size, 
  146.    outline the done button 
  147.  */
  148.   GetDItem (dp, HELPSCROLLBAR, &type, (Handle *) & c, &r);
  149.   ph = (PicHandle) GetResource ('PICT', HELPPICT);
  150.   SetCtlMax (c, (*ph)->picFrame.bottom - (*ph)->picFrame.top - r.bottom + r.top);
  151.  
  152.   GetPort (&saveport);
  153.   gp = (GrafPtr) NewPtr (sizeof (GrafPort));
  154.   OpenPort (gp);
  155.   gp->portBits.rowBytes = ((*ph)->picFrame.right - (*ph)->picFrame.left + 15) / 16 * 2;
  156.   gp->portBits.baseAddr = NewPtr (((*ph)->picFrame.bottom - (*ph)->picFrame.top) *
  157.                   gp->portBits.rowBytes);
  158.   gp->portBits.bounds = (*ph)->picFrame;
  159.   (*c)->contrlRfCon = (long) gp;
  160.   SetPort (gp);
  161.   ClipRect (&gp->portBits.bounds);
  162.   RectRgn (gp->visRgn, &gp->portBits.bounds);
  163.   EraseRect (&gp->portBits.bounds);
  164.   DrawPicture (ph, &gp->portBits.bounds);
  165.   SetPort (saveport);
  166.  
  167.   GetDItem (dp, HELPPICTITEM, &type, &h, &r);
  168.   SetDItem (dp, HELPPICTITEM, type, (Handle) drawhelp, &r);
  169.   ShowWindow (dp);
  170.   BringToFront (dp);
  171.   ModalDialog ((ProcPtr) helpfilter, &item);
  172.   DisposePtr (gp->portBits.baseAddr);
  173.   ClosePort (gp);
  174.   DisposPtr ((Ptr) gp);
  175.   DisposDialog (dp);
  176. }
  177.  
  178. void
  179. screensaver (void)
  180. {
  181. }
  182.  
  183. void
  184. setband (short whichband)
  185. {
  186.   short i, j, type;
  187.   bandinfo *p;
  188.   ControlHandle c;
  189.   Rect r;
  190.  
  191.   p = &bands[g_currentband];
  192.   for (i = p->bandpos; i < p->numitems; i++)
  193.     HideControl ((**p->items)[i]);
  194.   GetDItem (g_hotband, g_currentband + 1, &type, (Handle *) & c, &r);
  195.   HiliteControl (c, 0);
  196.  
  197.   g_currentband = whichband;
  198.   p = &bands[g_currentband];
  199.   for (i = p->bandpos, j = 0; i < p->numitems && j < g_numdispinhotband; i++, j++)
  200.     {
  201.       MoveControl ((**p->items)[i], FIRSTICONX + ICONWIDTHUSED * j, 0);
  202.       ShowControl ((**p->items)[i]);
  203.     }
  204.   GetDItem (g_hotband, g_currentband + 1, &type, (Handle *) & c, &r);
  205.   HiliteControl (c, 255);
  206.   checkhotbandcontrol ();
  207. }
  208.  
  209. enum { ODD_COL_OFFSET = 20, TOP_PADDING = 5 };
  210.  
  211. void
  212. straightenwindow (WindowPtr wp)
  213. {
  214.   opendirinfo **infoh;
  215.   short i, h, v, row, col, itemsperrow, height, numrows, ctlheight, top;
  216.   short heightused, widthused;
  217.   ControlHandle c;
  218.   GrafPtr saveport;
  219.  
  220.   GetPort (&saveport);
  221.   SetPort (wp);
  222.   infoh = (opendirinfo **) ((WindowPeek) wp)->refCon;
  223.  
  224.   switch ((*infoh)->view)
  225.     {
  226.     case ICONVIEW:
  227.       widthused = ICONWIDTHUSED;
  228.       heightused = ICONHEIGHTUSED;
  229.       break;
  230.     case ICSVIEW:
  231.       widthused = ICSWIDTHUSED;
  232.       heightused = ICSHEIGHTUSED;
  233.       break;
  234.     case LISTVIEW:
  235.       widthused = LISTWIDTHUSED;
  236.       heightused = LISTHEIGHTUSED;
  237.       break;
  238.     }
  239.   itemsperrow = (wp->portRect.right - wp->portRect.left) / widthused;
  240.   
  241.   /* NOTE: there are currently problems with list view when more than one column
  242.      is used, so I have just made it below so that we never get multi-column list
  243.      view listings.  ctm 95-11-05 */
  244.   
  245.   if (itemsperrow < 1 || (*infoh)->view == LISTVIEW)
  246.     itemsperrow = 1;
  247.   numrows = ((*infoh)->numitems - 1) / itemsperrow + 1;
  248.  
  249.   c = (*infoh)->sbar;
  250.   height = wp->portRect.bottom - wp->portRect.top;
  251.   SizeControl (c, SCROLLBARWIDTH, height - SCROLLBARWIDTH + 2);
  252.   top = GetCtlValue (c);
  253.   ctlheight = numrows * heightused + ODD_COL_OFFSET - height + TOP_PADDING;
  254.   if (ctlheight < 0)
  255.     ctlheight = 0;
  256.   SetCtlMax (c, ctlheight);
  257.   if (top > ctlheight)
  258.     top = ctlheight;
  259.   SetCtlValue (c, top);
  260.   SetOrigin (0, top);
  261.   MoveControl (c, wp->portRect.right - SCROLLBARWIDTH + 1, wp->portRect.top);
  262.   drawpartialgrowicon (wp, false);
  263.   for (i = 0; i < (*infoh)->numitems; i++)
  264.     {
  265.       row = i / itemsperrow;
  266.       col = i % itemsperrow;
  267.       v = row * heightused + TOP_PADDING;
  268. #if 1
  269.       if ((col & 1) && (*infoh)->view == ICONVIEW)
  270.         v += ODD_COL_OFFSET;
  271.           
  272. #endif
  273.       h = col * widthused;
  274.       MoveControl ((**(*infoh)->items)[i], h, v);
  275.     }
  276.   if (GetCtlMax (c) <= 0)
  277.     HiliteControl (c, 255);
  278.   else
  279.     HiliteControl (c, 0);
  280.   if (FrontWindow () == wp)
  281.     ShowControl (c);
  282.   SetPort (saveport);
  283. }
  284.  
  285. /* TODO: move to .h file */
  286. #define GETINFOID    205
  287. #define GET_VOLUME_INFO_ID 206
  288. typedef enum
  289. {
  290.   GETINFO_OK_ITEM = 1,
  291.   GETINFO_CANCEL_ITEM,
  292.   GETINFO_FILELABEL_ITEM,
  293.   GETINFO_CREATORLABEL_ITEM,
  294.   GETINFO_TYPELABEL_ITEM,
  295.   GETINFO_FILE_ITEM,
  296.   GETINFO_CREATOR_ITEM,
  297.   GETINFO_TYPE_ITEM
  298. }
  299. getinfo_items_t;
  300.  
  301.  
  302. static void
  303. multiply_and_convert_to_string (Str255 str, long blk_size, unsigned short n_blocks)
  304. {
  305.   long k, bytes_remaining, m, k_remaining;
  306.   
  307.   blk_size /= 512; /* always guaranteed to be in 512 multiples, right? */
  308.   k = n_blocks * blk_size / 2;
  309.   bytes_remaining = (n_blocks * blk_size) % 2 * 512;
  310.   m = k / 1024;
  311.   k_remaining = k % 1024;
  312.   
  313. #if 1
  314.   if (m >= 2)
  315.     {
  316.       float m_float;
  317.       
  318.       m_float = ((float) k * 1024 + bytes_remaining) / (1024L * 1024);
  319.       str[0] = sprintf ((char *) str+1, "%.02f megabytes", m_float);
  320.     }
  321.   else
  322.     {
  323.       float k_float;
  324.       
  325.       k_float = k + (float) bytes_remaining / 1024;
  326.       str[0] = sprintf ((char *) str+1, "%.02f kilobytes", k_float);
  327.     }
  328. #else
  329.   str[0] = sprintf ((char *) str+1, "%ld MB, %ld KB, %ld bytes", m, k_remaining, bytes_remaining);
  330. #endif
  331. }
  332.  
  333. static void
  334. get_volume_info (ControlHandle c)
  335. {
  336.   DialogPtr dp;
  337.   short unused;
  338.   HParamBlockRec hpb;
  339.   Str255 name;
  340.   OSErr err;
  341.   
  342.   hpb.ioParam.ioVRefNum = ICON_VREF (c);
  343.   hpb.ioParam.ioNamePtr = name;
  344.   hpb.volumeParam.ioVolIndex = 0;
  345.   err = PBHGetVInfo (&hpb, FALSE);
  346.   
  347.   if (err != noErr)
  348.       ParamText ((StringPtr) "\pERROR", 0, 0, 0);
  349.   else
  350.     {
  351.       Str255 capacity, free;
  352.       
  353.       multiply_and_convert_to_string (capacity, hpb.volumeParam.ioVAlBlkSiz,
  354.                                         hpb.volumeParam.ioVNmAlBlks);
  355.       multiply_and_convert_to_string (free    , hpb.volumeParam.ioVAlBlkSiz,
  356.                                         hpb.volumeParam.ioVFrBlk);
  357.         ParamText (name, capacity, free, 0);
  358.       }
  359.  
  360.   dp = GetNewDialog (GET_VOLUME_INFO_ID, 0, (WindowPtr) - 1);
  361.   ShowWindow (dp);
  362.   SetPort (dp);
  363.   ModalDialog ((ProcPtr) 0, &unused);
  364.   DisposDialog (dp);
  365. }
  366.  
  367. void
  368. getinfo (void)
  369. {
  370.   item **ih;
  371.   ControlHandle c;
  372.   CInfoPBRec pb;
  373.   Str255 s;
  374.   DialogPtr dp;
  375.   short type, itemno, valid;
  376.   Rect r;
  377.   Handle h;
  378. /* 
  379.  * TODO: add file size, modification date, etc. 
  380.  *       outline ok button
  381.  *       center window
  382.  *       check to see if it's a folder info is gotten about.
  383.  */
  384.  
  385.   c = (**g_selection)[0];
  386.   
  387.   if (is_volume (c))
  388.     {
  389.       get_volume_info (c);
  390.       return;
  391.     }
  392.   ih = (item **) (*c)->contrlData;
  393.   pb.hFileInfo.ioNamePtr = (*c)->contrlTitle;
  394.   pb.hFileInfo.ioVRefNum = (*ih)->vrefnum;
  395.   pb.hFileInfo.ioFDirIndex = 0;
  396.   pb.hFileInfo.ioDirID = (*ih)->ioparid;
  397.   PBGetCatInfo (&pb, false);
  398.  
  399.   dp = GetNewDialog (GETINFOID, 0, (WindowPtr) - 1);
  400.   GetDItem (dp, GETINFO_FILE_ITEM, &type, &h, &r);
  401.   SetIText (h, (*c)->contrlTitle);
  402.   s[0] = 4;
  403.   memcpy ((char *) s + 1, (char *) &pb.hFileInfo.ioFlFndrInfo.fdCreator, 4);
  404.   GetDItem (dp, GETINFO_CREATOR_ITEM, &type, &h, &r);
  405.   SetIText (h, s);
  406.   s[0] = 4;
  407.   memcpy ((char *) s + 1, (char *) &pb.hFileInfo.ioFlFndrInfo.fdType, 4);
  408.   GetDItem (dp, GETINFO_TYPE_ITEM, &type, &h, &r);
  409.   SetIText (h, s);
  410.   SelIText (dp, GETINFO_CREATOR_ITEM, 0, 4);
  411.  
  412.   ShowWindow (dp);
  413.   SetPort (dp);
  414.   do
  415.     {
  416.       ModalDialog ((ProcPtr) 0, &itemno);
  417.       valid = true;
  418.       if (itemno == GETINFO_OK_ITEM)
  419.     {
  420.       GetDItem (dp, GETINFO_CREATOR_ITEM, &type, &h, &r);
  421.       GetIText (h, s);
  422.       if (s[0] != 4)
  423.         valid = false;
  424.       else
  425.         memcpy ((char *) &pb.hFileInfo.ioFlFndrInfo.fdCreator,
  426.                (char *) s + 1, 4);
  427.       GetDItem (dp, GETINFO_TYPE_ITEM, &type, &h, &r);
  428.       GetIText (h, s);
  429.       if (s[0] != 4)
  430.         valid = false;
  431.       else
  432.         memcpy ((char *) &pb.hFileInfo.ioFlFndrInfo.fdType,
  433.                (char *) s + 1, 4);
  434.       if (valid)
  435.         {
  436.           pb.hFileInfo.ioDirID = (*ih)->ioparid;
  437.           PBSetCatInfo (&pb, false);
  438.           setoneicon (c);
  439.         }
  440.       else
  441.         {
  442.           ParamText ((StringPtr) "\pCreators and types are 4 characters long.",
  443.              (StringPtr) "\p", (StringPtr) "\p", (StringPtr) "\p");
  444.           StopAlert (FOUR_PARAM_ALERT, (ProcPtr) 0);
  445.         }
  446.     }
  447.     }
  448.   while (!valid);
  449.   DisposDialog (dp);
  450. }
  451.  
  452. void
  453. savestate (void)
  454. {
  455.   FILE *f;
  456.   CWindowPeek wp;
  457.   short i, j, res;
  458.  
  459.   unlink (GOBACKUPFILE);
  460.   res = rename (GOSAVEFILE, GOBACKUPFILE);
  461. #if defined (I_FIGURED_OUT_A_GOOD_WAY_TO_MAKE_SURE_GOSAVEFILE_EXISTED_BEFORE_THE_RENAME)
  462.   if (res)
  463.     {
  464.       ParamText ((StringPtr) "\pThe old state file couldn't be saved",
  465.          (StringPtr) "\p", (StringPtr) "\p", (StringPtr) "\p");
  466.       StopAlert (FOUR_PARAM_ALERT, (ProcPtr) 0);
  467.       return;
  468.     }
  469. #endif
  470.   f = fopen (GOSAVEFILE, "w");
  471.   if (!f)
  472.     {
  473.       ParamText ((StringPtr) "\pThe browser's state could not be saved.",
  474.          (StringPtr) "\p", (StringPtr) "\p", (StringPtr) "\p");
  475.       StopAlert (FOUR_PARAM_ALERT, (ProcPtr) 0);
  476.       return;
  477.     }
  478.   for (i = 0; i < NUMBANDS; i++)
  479.     {
  480.       if (i != VOLBAND)
  481.     {
  482.       for (j = 0; j < bands[i].numitems; j++)
  483.         fprintf (f, "%s\n",
  484.           *(*(item **) (*(**bands[i].items)[j])->contrlData)->path);
  485.     }
  486.     }
  487.   fprintf (f, "\n");
  488. #ifdef THINK_C
  489.   for (wp = (CWindowPeek) WindowList; wp != 0; wp = wp->nextWindow)
  490.     {
  491. #else
  492.   for (wp = (CWindowPeek) LMGetWindowList (); wp != 0; wp = wp->nextWindow)
  493.     {
  494. #endif
  495.       if (browser_window_p ((WindowPtr) wp))
  496.     {
  497.       fprintf (f, "%s\n", *(*(opendirinfo **) wp->refCon)->path);
  498.       OffsetRect (&wp->port.portRect, -(*wp->port.portPixMap)->bounds.left,
  499.               -(*wp->port.portPixMap)->bounds.top);
  500.       fprintf (f, "%d %d %d %d\n", wp->port.portRect.left,
  501.            wp->port.portRect.top, wp->port.portRect.right,
  502.            wp->port.portRect.bottom);
  503.     }
  504.     }
  505.   res = fclose (f);
  506.   if (res)
  507.     {
  508.       ParamText ((StringPtr) "\pThe browser's state could not be saved.",
  509.          (StringPtr) "\p", (StringPtr) "\p", (StringPtr) "\p");
  510.       StopAlert (FOUR_PARAM_ALERT, (ProcPtr) 0);
  511.       return;
  512.     }
  513. /* TODO: make save file invisible */
  514.   res = unlink (GOBACKUPFILE);
  515.   if (res)
  516.     {
  517.       ParamText ((StringPtr) "\pThe browser's backup state file could not be removed.",
  518.          (StringPtr) "\p", (StringPtr) "\p", (StringPtr) "\p");
  519.       StopAlert (FOUR_PARAM_ALERT, (ProcPtr) 0);
  520.       return;
  521.     }
  522. }
  523.