home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Applications / ircle 1.5.1 / source / ircle sources / IRCPreferences.p < prev    next >
Encoding:
Text File  |  1993-11-10  |  7.0 KB  |  309 lines  |  [TEXT/PJMM]

  1. {    ircle - Internet Relay Chat client    }
  2. {    File: IRCPreferences    }
  3. {    Copyright © 1992 Olaf Titz (s_titz@ira.uka.de)    }
  4.  
  5. {    This program is free software; you can redistribute it and/or modify    }
  6. {    it under the terms of the GNU General Public License as published by    }
  7. {    the Free Software Foundation; either version 2 of the License, or    }
  8. {    (at your option) any later version.    }
  9.  
  10. {    This program is distributed in the hope that it will be useful,    }
  11. {    but WITHOUT ANY WARRANTY; without even the implied warranty of    }
  12. {    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    }
  13. {    GNU General Public License for more details.    }
  14.  
  15. {    You should have received a copy of the GNU General Public License    }
  16. {    along with this program; if not, write to the Free Software    }
  17. {    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.    }
  18.  
  19. unit IRCPreferences;
  20. { deals with the Preferences settings and files }
  21.  
  22. interface
  23. uses
  24.     TCPTypes, TCPStuff, TCPConnections, ApplBase, MiscGlue, MsgWindows, {}
  25.     IRCGlobals, IRCaux, IRCChannels;
  26.  
  27. procedure InitIRCPreferences;
  28. { startup }
  29.  
  30. procedure GetPrefs;
  31. { run prefs inquire dialog }
  32.  
  33. procedure GetShortcuts;
  34. { inquire new shortcuts }
  35.  
  36. procedure SetShortcutsMenu;
  37. { set the shortcuts menu }
  38.  
  39. implementation
  40.  
  41. {$SETC DEBUG=false}
  42.  
  43. {$IFC DISTRIBUTION }
  44. {$SETC DEBUG=FALSE }
  45. {$ENDC}
  46.  
  47. { This module is ugly. I know that. -ot }
  48.  
  49. const
  50.     MAXMENU = 16;
  51.  
  52. var
  53.     shcd: DialogPtr;
  54.  
  55. function NewClearHandle (nbytes: longint): Handle;
  56. inline
  57.     $201F,        {    MOVE.L (A7)+, D0        }
  58.     $A322,        {    _NewHandle,Clear        }
  59.     $2E88,        {    MOVE.L A0,(A7)        }
  60.     $31C0, $0220;    {    MOVE.W D0,MemError    }
  61.  
  62. procedure defTODia (d: DialogPtr);
  63.     var
  64.         h: Handle;
  65.         i, j: integer;
  66.         l: longint;
  67.         s: Str255;
  68.         r: Rect;
  69.     begin
  70.         GetDItem(d, 3, i, h, r);
  71.         SetIText(h, default^^.Server);
  72.         GetDItem(d, 4, i, h, r);
  73.         NumToString(default^^.Port, s);
  74.         SetIText(h, s);
  75.         GetDItem(d, 5, i, h, r);
  76.         SetIText(h, default^^.Nick);
  77.         GetDItem(d, 6, i, h, r);
  78.         SetIText(h, default^^.userLoginName);
  79.         GetDItem(d, 7, i, h, r);
  80.         SetIText(h, default^^.userName);
  81.         GetDItem(d, 8, i, h, r);
  82. {    SetIText(h, default^^.autoExec); }
  83.         for i := 1 to 4 do begin
  84.             GetDItem(d, i + 8, j, h, r);
  85.             SetCtlValue(ControlHandle(h), ord(default^^.notify[i]))
  86.         end;
  87.     end;
  88.  
  89. procedure defFROMDia (d: DialogPtr);
  90.     var
  91.         h: Handle;
  92.         i, j: integer;
  93.         l: longint;
  94.         s: Str255;
  95.         r: Rect;
  96.     begin
  97.         GetDItem(d, 3, i, h, r);
  98.         GetIText(h, default^^.Server);
  99.         GetDItem(d, 4, i, h, r);
  100.         GetIText(h, s);
  101.         StringToNum(s, l);
  102.         default^^.Port := loword(l);
  103.         GetDItem(d, 5, i, h, r);
  104.         GetIText(h, default^^.Nick);
  105.         GetDItem(d, 6, i, h, r);
  106.         GetIText(h, default^^.userLoginName);
  107.         GetDItem(d, 7, i, h, r);
  108.         GetIText(h, default^^.userName);
  109.         GetDItem(d, 8, i, h, r);
  110. {     GetIText(h, default^^.autoExec); }
  111.         for i := 1 to 4 do begin
  112.             GetDItem(d, i + 8, j, h, r);
  113.             default^^.notify[i] := boolean(GetCtlValue(ControlHandle(h)))
  114.         end;
  115.     end;
  116.  
  117. procedure SetShortcutsMenu;
  118.     var
  119.         m: MenuHandle;
  120.         i: integer;
  121.         s: str255;
  122.     begin
  123.         m := GetMHandle(261);
  124.         for i := 1 to 10 do begin
  125.             if Shortcuts^^[i] = '' then
  126.                 s := ' '
  127.             else if length(Shortcuts^^[i]) > MAXMENU then
  128.                 s := concat(copy(Shortcuts^^[i], 1, MAXMENU), '...')
  129.             else
  130.                 s := Shortcuts^^[i];
  131.             SetItem(m, i + 2, s);
  132.         end;
  133.     end;
  134.  
  135. function CloseShortcuts (var e: eventRecord): boolean;
  136.     var
  137.         i, j: integer;
  138.         h: Handle;
  139.         r: Rect;
  140.         s: str255;
  141.     begin
  142.         if shcd <> nil then
  143.             if DialogPtr(e.message) = shcd then
  144.                 if TrackGoAway(shcd, e.where) then begin
  145.                     for i := 1 to 10 do begin
  146.                         GetDItem(shcd, i, j, h, r);
  147.                         GetIText(h, s);
  148.                         Shortcuts^^[i] := s;
  149.                     end;
  150.                     SetShortcutsMenu;
  151.                     DisposDialog(shcd);
  152.                     shcd := nil;
  153.                     dirtyPrefs := true;
  154.                     CloseShortcuts := true;
  155.                     exit(CloseShortcuts)
  156.                 end;
  157.         CloseShortcuts := false
  158.     end;
  159.  
  160.  
  161. procedure GetOldstylePrefs;
  162.     var
  163.         i: integer;
  164.         olds: boolean;
  165.     begin
  166.         olds := false;
  167.         default := defaultHndl(GetResource('Pref', 256));
  168.         if default = nil then begin
  169.             default := defaultHndl(NewClearHandle(sizeof(defaultType)));
  170.             default^^.port := 6667
  171.         end
  172.         else begin
  173.             DetachResource(Handle(default));
  174.             olds := true
  175.         end;
  176.         if gethandlesize(handle(default)) < sizeof(defaultType) then begin
  177.             sethandlesize(handle(default), sizeof(defaultType));
  178.             for i := 1 to 4 do
  179.                 default^^.notify[i] := false
  180.         end;
  181.         shortcuts := shortcutsHndl(GetResource('Pref', 257));
  182.         if shortcuts = nil then begin
  183.             shortcuts := shortcutsHndl(NewClearHandle(sizeof(ShortcutsType)));
  184.         end
  185.         else begin
  186.             DetachResource(Handle(shortcuts));
  187.             olds := true
  188.         end;
  189.         SetShortcutsMenu;
  190.         defFont := FSHndl(GetResource('Pref', 258));
  191.         if defFont <> nil then begin
  192.             with defFont^^ do begin
  193.                 MWdefaultFont := fnt;
  194.                 MWdefaultSize := siz
  195.             end;
  196.             ReleaseResource(Handle(defFont));
  197.             olds := true
  198.         end;
  199.         if olds then
  200.             i := Alert(A_CONVERT, nil);
  201.     end;
  202.  
  203.  
  204. procedure GetPrefs;
  205.     var
  206.         d: DialogPtr;
  207.         h: Handle;
  208.         i, j: integer;
  209.         r: Rect;
  210.         s: str255;
  211.     begin
  212.         d := GetNewDialog(D_PREFS, nil, WindowPtr(-1));
  213.         defTODia(d);
  214.         SelIText(d, 3, 0, 32767);
  215.         InitCursor;
  216.         repeat
  217.             ModalDialog(nil, i);
  218.             if i = 2 then begin
  219.                 DisposDialog(d);
  220.                 exit(GetPrefs)
  221.             end;
  222.             if (i >= 9) and (i <= 12) then begin
  223.                 GetDItem(d, i, j, h, r);
  224.                 default^^.notify[i - 8] := not default^^.notify[i - 8];
  225.                 SetCtlValue(ControlHandle(h), ord(default^^.notify[i - 8]));
  226.             end;
  227.         until i = 1;
  228.         defFROMDia(d);
  229.         DisposDialog(d);
  230.         default^^.nick := copy(default^^.nick, 1, 9);
  231.         dirtyPrefs := true;
  232.     end;
  233.  
  234. procedure GetShortcuts;
  235.     var
  236.         m: MenuHandle;
  237.         i, j: integer;
  238.         h: Handle;
  239.         r: Rect;
  240.     begin
  241.         if shcd = nil then begin
  242.             shcd := GetNewDialog(D_SHCUTS, nil, WindowPtr(-1));
  243.             m := GetMHandle(261);
  244.             for i := 1 to 10 do begin
  245.                 GetDItem(shcd, i, j, h, r);
  246.                 SetIText(h, Shortcuts^^[i]);
  247.             end;
  248.         end
  249.         else
  250.             SelectWindow(shcd);
  251.     end;
  252.  
  253.  
  254. procedure InitIRCPreferences;
  255.     var
  256.         i, j: integer;
  257.         a: AppFile;
  258.         s: str255;
  259.     procedure GetNewstylePrefs;
  260.         begin
  261.             default := defaultHndl(NewClearHandle(sizeof(defaultType)));
  262.             shortcuts := shortcutsHndl(NewClearHandle(sizeof(ShortcutsType)));
  263.             reset(initFile, a.fname);  { actual loading occurs later in IRCInit }
  264.             readPrefs := true;
  265.         end;
  266.     begin
  267.         shcd := nil;
  268.         default := nil;
  269.         shortcuts := nil;
  270.         readPrefs := false;
  271.         dirtyPrefs := false;
  272.         CountAppFiles(i, j);
  273.         if j = 0 then begin
  274.             default := defaultHndl(NewClearHandle(sizeof(defaultType)));
  275.             default^^.port := 6667;
  276.             shortcuts := shortcutsHndl(NewClearHandle(sizeof(ShortcutsType)));
  277.             GetPrefs
  278.         end
  279.         else begin
  280.             GetAppFiles(1, a);
  281.             j := SetVol(nil, a.vrefnum);
  282.             if j = 0 then begin
  283.                 i := OpenResFile(a.fname);
  284.                 j := ResError
  285.             end;
  286.             if (j = resFNotFound) or (j = EOFErr) then
  287.                 GetNewstylePrefs
  288.             else if j = 0 then begin
  289.                 if Count1Resources('Pref') > 0 then
  290.                     GetOldstylePrefs
  291.                 else
  292.                     GetNewstylePrefs;
  293.                 CloseResFile(i);
  294.             end
  295.             else begin
  296.                 NumtoString(j, s);
  297.                 ParamText(s, '', '', '');
  298.                 i := Alert(A_FERR, nil);
  299.                 ApplExit
  300.             end;
  301.         end;
  302.         if default = nil then
  303.             debugstr('default=nil');
  304.         if shortcuts = nil then
  305.             debugstr('shortcuts=nil');
  306.         i := ApplTask(@CloseShortcuts, mouseMsg + inGoAway);
  307.     end;
  308.  
  309. end.