home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / EDO Hack / source code / sources / HackWindows.c < prev    next >
Encoding:
Text File  |  1997-06-28  |  8.1 KB  |  320 lines  |  [TEXT/CWIE]

  1. // --------------------------------------------------------------------------------------
  2. //  HackWindows.c
  3. //
  4. //  Written by Don Arbow and Marc A. Raiser, EveryDay Objects, Inc.
  5. //     in one day - June 26, 1997
  6. // --------------------------------------------------------------------------------------
  7.  
  8. #include "HackWindows.h"
  9.  
  10. #ifndef __LISTS__
  11. #include <Lists.h>
  12. #endif
  13.  
  14. #ifndef __TOOLUTILS__
  15. #include <ToolUtils.h>
  16. #endif
  17.  
  18. #include <Events.h>
  19.  
  20. #include "FetchParseURL.h"
  21. /* === Constants === */
  22.  
  23. #define        WIND_Hack        128
  24.  
  25. static WindowRef gWin;
  26.  
  27.  
  28. /* === Types === */
  29.  
  30. typedef struct    HackInfoRecord {
  31.     ListHandle        listBox;
  32. } HackInfoRecord, **HackInfoHandle;
  33.  
  34.  
  35. /* === Global Variables === */
  36.  
  37.     /* Bounds of the List box that displays the names of the dragged items */
  38. Rect        listView = {10, 20, 170, 470};
  39.  
  40.  
  41. /* === Macros === */
  42.  
  43.     /* Extracts the ListHandle from a HackInfoHandle that's stored */
  44.     /* in the refCon of a WindowRecord                               */
  45. #define        GetListBox(w)    (**((HackInfoHandle) GetWRefCon(w))).listBox
  46.  
  47.  
  48. /* ------------------------------------------------------------------------ */
  49. /*    AddListItem                                                            */
  50. /* ------------------------------------------------------------------------ */
  51. /*
  52.  *    Adds a url item to the List box in a Window
  53.  */
  54.  
  55. OSErr AddListItem(Str255 url)
  56. {
  57.     OSErr        err = nil;
  58.     ListHandle    list = GetListBox(gWin);
  59.     
  60.     Cell        newCell = { 0, 0 };
  61.                         
  62.         /* Add file name to the beginning of the List box */
  63.     SetPort(gWin);
  64.     
  65. //    if (!LSearch(url + 1, url[0], nil, &newCell, list)) {
  66.     if (url[0]) {
  67.         LSetDrawingMode(false, list);
  68.         LAddRow(1, 0, list);
  69.         LSetCell(url + 1, url[0], newCell, list);
  70.         LSetDrawingMode(true, list);
  71.         InvalRect(&listView);
  72.     }
  73.         
  74.     return err;
  75. }
  76.  
  77. /* ------------------------------------------------------------------------ */
  78. /*    CreateWindow                                                            */
  79. /* ------------------------------------------------------------------------ */
  80. /*
  81.  *    Make a new window that contains a List box of url items
  82.  */
  83.  
  84. void    CreateWindow(Str255 fileName)
  85. {
  86.     WindowPtr        theWindow;
  87.     
  88.     ListHandle        theListBox;
  89.     Rect            listBounds = {0, 0, 0, 1};
  90.     Point            cellSize = {0, 0};
  91.     
  92.     HackInfoHandle    hackInfo;
  93.     RgnHandle        receiveRgn;
  94.     Rect            receiveRect = listView;
  95.     
  96.         /* Make Toolbox window and store a HackInfoHandle */
  97.         /* in the window's refCon                          */
  98.     
  99.     theWindow = GetNewWindow(WIND_Hack, nil, (WindowPtr) -1);
  100.     hackInfo = (HackInfoHandle) NewHandle(sizeof(HackInfoRecord));
  101.     SetWRefCon(theWindow, (long) hackInfo);
  102.     
  103.     SetWTitle(theWindow, fileName);
  104.     
  105.         /* Create a List Manager list and store it in */
  106.         /* the HackInfoHandle                          */
  107.     
  108.     theListBox = LNew(&listView, &listBounds, cellSize, 0, theWindow,
  109.                         true, false, false, true);
  110.     (**hackInfo).listBox = theListBox;
  111.     
  112.         /* Create a region coinciding with the List box */
  113.     
  114.     receiveRgn = NewRgn();
  115.     RectRgn(receiveRgn, &receiveRect);
  116.     
  117.         /* Load information needed by our DragManagerModule. */
  118.         /* We specify that our window can accept items of     */
  119.         /* flavorTypeHFS (file or folder) within a region     */
  120.         /* that coincides with the List box. And that our     */
  121.         /* ReceiveHFSDrag function should be called for each */
  122.         /* item dragged and dropped into that region.         */
  123.     
  124.     ShowWindow(theWindow);
  125.     
  126.     gWin = theWindow;
  127. }
  128.  
  129. /* ------------------------------------------------------------------------ */
  130. /*    DestroyWindow                                                            */
  131. /* ------------------------------------------------------------------------ */
  132. /*
  133.  *    Dispose of a Window
  134.  *
  135.  *    We need to clean up our DragInfo, dispose of the List box, and
  136.  *    finally dispose of the Toolbox window.
  137.  */
  138.  
  139. void    DestroyWindow(WindowPtr inWindow)
  140. {
  141.     LDispose(GetListBox(inWindow));
  142.     DisposeHandle((Handle) GetWRefCon(inWindow));
  143.     DisposeWindow(inWindow);
  144. }
  145.  
  146. /* ------------------------------------------------------------------------ */
  147. /*    UpdateHackWindow                                                            */
  148. /* ------------------------------------------------------------------------ */
  149.  
  150. void    UpdateHackWindow(WindowPtr inWindow)
  151. {
  152.     Rect    boxFrame;
  153.  
  154.     GrafPtr    savePort;
  155.     GetPort(&savePort);
  156.     SetPort(inWindow);
  157.     BeginUpdate(inWindow);
  158.     
  159.     EraseRect(&inWindow->portRect);
  160.     
  161.         /* Draw List Box */
  162.     LUpdate(inWindow->visRgn, GetListBox(inWindow));
  163.     
  164.         /* Draw Frame around List Box */
  165.     boxFrame = listView;
  166.     InsetRect(&boxFrame, -1, -1);
  167.     FrameRect(&boxFrame);
  168.     
  169.         /* Draw caption under List Box */
  170.     MoveTo(boxFrame.left + 30, boxFrame.bottom + 16);
  171.     DrawString("\pMarc and Don's wonderful Subwoofer hack!!!!");
  172.  
  173.     EndUpdate(inWindow);
  174.     SetPort(savePort);
  175. }
  176.  
  177.  
  178. /* ------------------------------------------------------------------------ */
  179. /*    DoActivateWindow                                                        */
  180. /* ------------------------------------------------------------------------ */
  181.  
  182. void    ActivateHackWindow(WindowPtr inWindow)
  183. {
  184.     SetPort(inWindow);
  185.     LActivate(true, GetListBox(inWindow));
  186. }
  187.  
  188.  
  189. /* ------------------------------------------------------------------------ */
  190. /*    DoDeactivateWindow                                                        */
  191. /* ------------------------------------------------------------------------ */
  192.  
  193. void    DeactivateHackWindow(WindowPtr inWindow)
  194. {
  195.     SetPort(inWindow);
  196.     LActivate(false, GetListBox(inWindow));
  197. }
  198.  
  199.  
  200. /* ------------------------------------------------------------------------ */
  201. /*    DoDragWindow                                                            */
  202. /* ------------------------------------------------------------------------ */
  203.  
  204. void    DragHackWindow(EventRecord *inEvent, WindowPtr inWindow)
  205. {
  206.     Rect    screenRect = (**GetGrayRgn()).rgnBBox;
  207.     InsetRect(&screenRect, 4, 4);
  208.     DragWindow(inWindow, inEvent->where, &screenRect);
  209. }
  210.  
  211.  
  212. /* ------------------------------------------------------------------------ */
  213. /*    DoGrowWindow                                                            */
  214. /* ------------------------------------------------------------------------ */
  215.  
  216. void    GrowHackWindow(EventRecord *inEvent, WindowPtr inWindow)
  217. {
  218.     Rect    sizeRect;
  219.     long    newSize;
  220.     
  221.     sizeRect.top = 100;
  222.     sizeRect.left = 100;
  223.     sizeRect.bottom = ((**GetGrayRgn()).rgnBBox).bottom;
  224.     sizeRect.right = sizeRect.left + 300;
  225.     newSize = GrowWindow(inWindow, inEvent->where, &sizeRect);
  226.     
  227.     if (newSize != 0) {
  228.         SizeWindow(inWindow, LoWord(newSize), HiWord(newSize), true);
  229.         AdjustWindowSize(inWindow);
  230.     }
  231. }
  232.  
  233.  
  234. /* ------------------------------------------------------------------------ */
  235. /*    DoZoomWindow                                                            */
  236. /* ------------------------------------------------------------------------ */
  237.  
  238. void    ZoomHackWindow(WindowPtr inWindow, short inDirection)
  239. {
  240.     SetPort(inWindow);
  241.     EraseRect(&inWindow->portRect);
  242.     ZoomWindow(inWindow, inDirection, true);
  243.     AdjustWindowSize(inWindow);
  244. }
  245.  
  246.  
  247. /* ------------------------------------------------------------------------ */
  248. /*    DoClickWindow                                                            */
  249. /* ------------------------------------------------------------------------ */
  250.  
  251. void    ClickHackWindow(EventRecord *inEvent, WindowPtr inWindow)
  252. {
  253.     Point    mouseLocation;
  254.     Cell    cell = { 0, 0 };
  255.     ListHandle list;
  256.     Str255  url;
  257.     short   urlLen = 255;
  258.     StandardFileReply  sfFile;
  259.     OSErr   err = noErr;
  260.     
  261.     SetPort(inWindow);
  262.     
  263.     mouseLocation = inEvent->where;
  264.     GlobalToLocal(&mouseLocation);
  265.     // new url;
  266.     list = GetListBox(inWindow);
  267.     
  268.     if (LClick(mouseLocation, inEvent->modifiers, list)) {
  269.         // get the data;
  270.         
  271.         if (LGetSelect(true, &cell, list)) {
  272.             
  273.             LGetCell(url + 1, &urlLen, cell, list);
  274.             url[0] = urlLen;
  275.         }
  276.         
  277.         if (inEvent->modifiers & optionKey) {
  278.             StandardPutFile("\pSave this link as a file named:", "\pindex.html", &sfFile);
  279.             if (sfFile.sfGood) {
  280.                 p2cstr(url);
  281.                 err = URLDownload((char*)url, &sfFile.sfFile, kURLDisplayProgressFlag + kURLDisplayAuthFlag);
  282.                 if (err != noErr) {
  283.                     ShowError(err);
  284.                 }
  285.             }
  286.         } else {
  287.             QueryURL(url);
  288.             SetWTitle(inWindow, url);
  289.         }
  290.     }
  291. }
  292.  
  293. void ClearList(void) {
  294.  
  295.     ListHandle list;
  296.     
  297.     list = GetListBox(gWin);
  298.     LSetDrawingMode(false, list);
  299.     LDelRow(0, 0, list);
  300.     LSetDrawingMode(true, list);
  301. }
  302.  
  303. /* ------------------------------------------------------------------------ */
  304. /*    AdjustWindowSize                                                        */
  305. /* ------------------------------------------------------------------------ */
  306.  
  307. void    AdjustWindowSize(WindowPtr inWindow)
  308. {
  309.     SetPort(inWindow);
  310.     InvalRect(&inWindow->portRect);
  311. }
  312.  
  313.  
  314. /* ------------------------------------------------------------------------ */
  315. /*    DoKeyWindow                                                                */
  316. /* ------------------------------------------------------------------------ */
  317.  
  318. void    KeyHackWindow(EventRecord *inEvent, WindowPtr inWindow)
  319. {
  320. }