home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / monitors / rsys / rsyssrc.lha / RSysShowFont.c < prev    next >
C/C++ Source or Header  |  1993-09-19  |  10KB  |  439 lines

  1. /*
  2. ***************************************************************************
  3. *
  4. * Datei:
  5. *      RSysShowFont.c
  6. *
  7. * Inhalt:
  8. *
  9. *      --- Globale Routinen ---
  10. *
  11. *    void ShowFont ( struct TextFont *tf );
  12. *
  13. *      --- Lokale  Routinen ---
  14. *
  15. *    static int BoldCGadClicked ( void );
  16. *    static int HandleShowFontIDCMP ( void );
  17. *    static int ItalicCGadClicked ( void );
  18. *    static int NormalCGadClicked ( void );
  19. *    static int OpenShowFontWindow ( void );
  20. *    static int UnderlineCGadClicked ( void );
  21. *    static void clipWindow ( struct Window *win , LONG minX , LONG minY , LONG maxX , LONG maxY );
  22. *    static void IsNoAttrib ( void );
  23. *    static void Set ( int num , int checked );
  24. *    static void SetAttr ( int attrib );
  25. *    static void ShowFontRender ( struct TextAttr *tfont );
  26. *    static void unclipWindow ( struct Window *win );
  27. *
  28. * Bemerkungen:
  29. *      Enthält die Routinen zur Darstellung eines beliebig
  30. *      großen Fonts. Benutzt Clip-Routinen der Layers-Library.
  31. *
  32. * Erstellungsdatum:
  33. *      12-Jul-93     Rolf Böhme
  34. *
  35. * Änderungen:
  36. *      12-Jul-93     Rolf Böhme      Erstellung
  37. *
  38. ***************************************************************************
  39. */
  40.  
  41. #include "RSys.h"
  42.  
  43. static int NormalCGadClicked(void);
  44. static int ItalicCGadClicked(void);
  45. static int BoldCGadClicked(void);
  46. static int UnderlineCGadClicked(void);
  47. static int HandleShowFontIDCMP(void);
  48.  
  49. static struct Window *ShowFontWnd = NULL;
  50. static struct Gadget *ShowFontGList = NULL;
  51. static struct IntuiMessage ShowFontMsg;
  52. static struct Gadget *ShowFontGadgets[4];
  53. static UWORD ShowFontLeft = 0;
  54. static UWORD ShowFontTop = 0;
  55. static UWORD ShowFontWidth = 500;
  56. static UWORD ShowFontHeight = 120;
  57. static UBYTE *ShowFontWdt = (UBYTE *) NAME " - Show Font";
  58. static char *testtext;
  59. static struct IntuiText ShowFontIText[]=
  60. {
  61.     1, 2, JAM2, 76, 11, NULL, NULL, NULL
  62. };
  63.  
  64. static struct TextAttr testfont;
  65.  
  66. static UWORD ShowFontGTypes[]=
  67. {
  68.     CHECKBOX_KIND,
  69.     CHECKBOX_KIND,
  70.     CHECKBOX_KIND,
  71.     CHECKBOX_KIND,
  72.     BUTTON_KIND
  73. };
  74.  
  75. static struct NewGadget ShowFontNGad[]=
  76. {
  77.     12, 104, 26, 11, (UBYTE *) "Normal", NULL, GD_NormalCGad, PLACETEXT_RIGHT, NULL, (APTR) NormalCGadClicked,
  78.     141, 104, 26, 11, (UBYTE *) "Italic", NULL, GD_ItalicCGad, PLACETEXT_RIGHT, NULL, (APTR) ItalicCGadClicked,
  79.     270, 104, 26, 11, (UBYTE *) "Bold", NULL, GD_BoldCGad, PLACETEXT_RIGHT, NULL, (APTR) BoldCGadClicked,
  80.     400, 104, 26, 11, (UBYTE *) "Underl", NULL, GD_UnderlineCGad, PLACETEXT_RIGHT, NULL, (APTR) UnderlineCGadClicked,
  81. };
  82.  
  83. static ULONG *ShowFontGTags[]=
  84. {
  85.     (ULONG *) (GTCB_Checked), (ULONG *) TRUE, (ULONG *) (TAG_DONE),
  86.     (ULONG *) (TAG_DONE),
  87.     (ULONG *) (TAG_DONE),
  88.     (ULONG *) (TAG_DONE),
  89. };
  90.  
  91. static long bevelbox[4];
  92.  
  93. struct Library *LayersBase = NULL;
  94.  
  95. static void
  96. unclipWindow(struct Window *win)
  97. {
  98.     struct Region *old_region;
  99.  
  100.     if (NULL != (old_region = InstallClipRegion(win->WLayer, NULL))) DisposeRegion(old_region);
  101. }
  102.  
  103. static void
  104. clipWindow(struct Window *win, LONG minX, LONG minY, LONG maxX, LONG maxY)
  105. {
  106.     struct Region *new_region;
  107.     struct Rectangle my_rectangle;
  108.  
  109.     my_rectangle.MinX = minX;
  110.     my_rectangle.MinY = minY;
  111.     my_rectangle.MaxX = maxX;
  112.     my_rectangle.MaxY = maxY;
  113.  
  114.     /* get a new region and OR in the limits. */
  115.     if ((new_region = NewRegion()) != NULL)
  116.     {
  117.         if (OrRectRegion(new_region, &my_rectangle) == FALSE)
  118.         {
  119.             DisposeRegion(new_region);
  120.             new_region = NULL;
  121.         }
  122.     }
  123.  
  124.     InstallClipRegion(win->WLayer, new_region);
  125.  
  126.     return;
  127. }
  128.  
  129. static void
  130. ShowFontRender(struct TextAttr *tfont)
  131. {
  132.     struct IntuiText it;
  133.     int    ilen;
  134.  
  135.     EraseRect(ShowFontWnd->RPort,
  136.                  bevelbox[0] + 2, bevelbox[1] + 1,
  137.                  bevelbox[0] + bevelbox[2] - 3, bevelbox[1] + bevelbox[3] - 2);
  138.  
  139.     CopyMem((char *)&ShowFontIText[0], (char *)&it, (long)sizeof(struct IntuiText));
  140.  
  141.     it.IText = (STRPTR) testtext;
  142.     it.ITextFont = tfont;
  143.  
  144.     ilen = IntuiTextLength(&it);
  145.  
  146.     it.LeftEdge = bevelbox[0] + ((bevelbox[2] - ilen) / 2);
  147.     it.LeftEdge = (it.LeftEdge < bevelbox[0]) ? bevelbox[0] + 2 : it.LeftEdge;
  148.  
  149.     it.TopEdge = bevelbox[1] + ((bevelbox[3] - tfont->ta_YSize) / 2);
  150.     it.TopEdge = (it.TopEdge < bevelbox[1]) ? bevelbox[1] + 1 : it.TopEdge;
  151.  
  152.     PrintIText(ShowFontWnd->RPort, &it, 0, 0);
  153.  
  154.     return;
  155. }
  156.  
  157. static int
  158. OpenShowFontWindow(void)
  159. {
  160.     struct NewGadget ng;
  161.     struct Gadget *g;
  162.     UWORD lc,
  163.             tc;
  164.     UWORD wleft = ShowFontLeft,
  165.             wtop = ShowFontTop,
  166.             ww,
  167.             wh;
  168.     int    gl[] = {0};
  169.  
  170.    AdjustWindowDimensions(Scr, ShowFontLeft, ShowFontTop, ShowFontWidth, ShowFontHeight,
  171.                                &wleft, &wtop, &ww, &wh);
  172.  
  173.     if (!(g = CreateContext(&ShowFontGList))) return 1L;
  174.  
  175.     for (lc = 0, tc = 0; lc < 4; lc++)
  176.     {
  177.         CopyMem((char *)&ShowFontNGad[lc], (char *)&ng, (long)sizeof(struct NewGadget));
  178.  
  179.         ng.ng_VisualInfo = VisualInfo;
  180.         ng.ng_TextAttr = Font;
  181.         ng.ng_LeftEdge = OffX + ComputeX(ng.ng_LeftEdge);
  182.         ng.ng_TopEdge = OffY + ComputeY(ng.ng_TopEdge);
  183.         ng.ng_Width = ComputeX(ng.ng_Width);
  184.         ng.ng_Height = ComputeY(ng.ng_Height);
  185.  
  186.         ShowFontGadgets[lc] = g = CreateGadgetA((ULONG) ShowFontGTypes[lc], g, &ng, (struct TagItem *) & ShowFontGTags[tc]);
  187.         makelabelvisible(ShowFontGadgets[lc]);
  188.  
  189.         while (ShowFontGTags[tc]) tc += 2;
  190.  
  191.         tc++;
  192.  
  193.         if (NOT g) return 2L;
  194.     }
  195.  
  196.     bevelbox[0] = OffX + ComputeX((UWORD) 12);
  197.     bevelbox[1] = OffY + ComputeY((UWORD) 5);
  198.     bevelbox[2] = (long)ComputeX((UWORD) 477);
  199.     bevelbox[3] = (long)ComputeY((UWORD) 93);
  200.  
  201.     sprintf(testtext, "(%ld) %s", testfont.ta_YSize, testfont.ta_Name);
  202.  
  203.     if (!(ShowFontWnd = OpenWindowTags(NULL,
  204.                                                   WA_Left, wleft,
  205.                                                   WA_Top, wtop,
  206.                                                   WA_Width, ww,
  207.                                                   WA_Height, wh,
  208.                                                   WA_IDCMP,
  209.                                                   TEXTIDCMP | CHECKBOXIDCMP | BUTTONIDCMP | IDCMP_CLOSEWINDOW |
  210.                                                   IDCMP_REFRESHWINDOW | VANILLAKEY,
  211.                                                   WA_Flags, WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET |
  212.                                                   WFLG_SMART_REFRESH | WFLG_ACTIVATE | WFLG_RMBTRAP,
  213.                                                   WA_Title, ShowFontWdt,
  214.                                                   WA_PubScreenFallBack, TRUE,
  215.                                                   WA_PubScreen, Scr,
  216.                                                   TAG_DONE)))
  217.         return 4L;
  218.  
  219.     RefreshRastPort(ShowFontWnd, ShowFontGadgets, gl, 0, FALSE, ShowFontGList);
  220.  
  221.     EraseRect(ShowFontWnd->RPort,
  222.                  bevelbox[0] + 2, bevelbox[1] + 1,
  223.                  bevelbox[0] + bevelbox[2] - 3, bevelbox[1] + bevelbox[3] - 2);
  224.  
  225.     DrawBevelBox(ShowFontWnd->RPort,
  226.                      bevelbox[0], bevelbox[1],
  227.                      bevelbox[2], bevelbox[3],
  228.                      GT_VisualInfo, VisualInfo,
  229.                      GTBB_Recessed, TRUE,
  230.                      TAG_DONE);
  231.  
  232.     clipWindow(ShowFontWnd, bevelbox[0] + 4, bevelbox[1] + 2,
  233.                   bevelbox[0] + bevelbox[2] - 6, bevelbox[1] + bevelbox[3] - 4);
  234.  
  235.     ShowFontRender(&testfont);
  236.  
  237.     GT_RefreshWindow(ShowFontWnd, NULL);
  238.  
  239.     return 0L;
  240. }
  241.  
  242. static int flags[4] =
  243. {TRUE, FALSE, FALSE, FALSE},
  244.         defflags[4] =
  245. {TRUE, FALSE, FALSE, FALSE};
  246. static int attributes[4] =
  247. {FS_NORMAL, FSF_ITALIC, FSF_BOLD, FSF_UNDERLINED};
  248.  
  249. static void
  250. SetAttr(int attrib)
  251. {
  252.     int    i;
  253.     int    newattr;
  254.  
  255.     if (attrib != FS_NORMAL)
  256.     {
  257.         newattr = 0;
  258.  
  259.         for (i = 0; i < 4; i++) newattr |= (flags[i] == TRUE) ? attributes[i] : 0;
  260.     }
  261.     else newattr = FS_NORMAL;
  262.  
  263.     testfont.ta_Style = newattr;
  264.  
  265.     ShowFontRender(&testfont);
  266.  
  267.     return;
  268. }
  269.  
  270. static void
  271. Set(int num, int checked)
  272. {
  273.     flags[num - GD_NormalCGad] = checked;
  274.  
  275.     GT_SetGadgetAttrs(ShowFontGadgets[num - GD_NormalCGad], ShowFontWnd,
  276.                             NULL,
  277.                             GTCB_Checked, flags[num - GD_NormalCGad],
  278.                             TAG_DONE);
  279.  
  280.     return;
  281. }
  282.  
  283. static void
  284. IsNoAttrib(void)
  285. {
  286.     if (!flags[1] && !flags[2] && !flags[3]) Set((int)GD_NormalCGad, TRUE);
  287.  
  288.     return;
  289. }
  290.  
  291. static int
  292. NormalCGadClicked(void)
  293. {
  294.     int    i;
  295.  
  296.     if (!flags[GD_NormalCGad - GD_NormalCGad]) SetAttr(attributes[0]);
  297.  
  298.     Set((int)GD_NormalCGad, TRUE);
  299.  
  300.     for (i = GD_ItalicCGad; i <= GD_UnderlineCGad; i++) Set(i, FALSE);
  301.  
  302.     return TRUE;
  303. }
  304.  
  305. static int
  306. ItalicCGadClicked(void)
  307. {
  308.     /* routine when gadget "Italic" is clicked. */
  309.     Set((int)GD_NormalCGad, FALSE);
  310.  
  311.     flags[GD_ItalicCGad - GD_NormalCGad] = (flags[GD_ItalicCGad - GD_NormalCGad] == TRUE) ? FALSE : TRUE;
  312.  
  313.     SetAttr(attributes[1]);
  314.  
  315.     IsNoAttrib();
  316.  
  317.     return TRUE;
  318. }
  319.  
  320. static int
  321. BoldCGadClicked(void)
  322. {
  323.     /* routine when gadget "Bold" is clicked. */
  324.     Set((int)GD_NormalCGad, FALSE);
  325.  
  326.     flags[GD_BoldCGad - GD_NormalCGad] = (flags[GD_BoldCGad - GD_NormalCGad] == TRUE) ? FALSE : TRUE;
  327.  
  328.     SetAttr(attributes[2]);
  329.  
  330.     IsNoAttrib();
  331.  
  332.     return TRUE;
  333. }
  334.  
  335. static int
  336. UnderlineCGadClicked(void)
  337. {
  338.     /* routine when gadget "Underline" is clicked. */
  339.     Set((int)GD_NormalCGad, FALSE);
  340.  
  341.     flags[GD_UnderlineCGad - GD_NormalCGad] = (flags[GD_UnderlineCGad - GD_NormalCGad] == TRUE) ? FALSE : TRUE;
  342.  
  343.     SetAttr(attributes[3]);
  344.  
  345.     IsNoAttrib();
  346.  
  347.     return TRUE;
  348. }
  349.  
  350. static int
  351. HandleShowFontIDCMP(void)
  352. {
  353.     struct IntuiMessage *m;
  354.     int    (*func) (void);
  355.     BOOL    running = TRUE;
  356.     struct Gadget *gad;
  357.  
  358.     while (m = GT_GetIMsg(ShowFontWnd->UserPort))
  359.     {
  360.         CopyMem((char *)m, (char *)&ShowFontMsg, (long)sizeof(struct IntuiMessage));
  361.  
  362.         GT_ReplyIMsg(m);
  363.  
  364.         switch (ShowFontMsg.Class)
  365.         {
  366.             case IDCMP_REFRESHWINDOW:
  367.                 GT_BeginRefresh(ShowFontWnd);
  368.                 GT_EndRefresh(ShowFontWnd, TRUE);
  369.                 break;
  370.  
  371.             case IDCMP_VANILLAKEY:
  372.                 if ((char)ShowFontMsg.Code == ESC) running = FALSE;
  373.                 break;
  374.  
  375.             case IDCMP_CLOSEWINDOW:
  376.                 running = FALSE;
  377.                 break;
  378.  
  379.             case IDCMP_GADGETUP:
  380.             gad = (struct Gadget *) ShowFontMsg.IAddress;
  381.             HandleHelp((enum RSysNumbers)gad->GadgetID);
  382.  
  383.                 func = (void *)(gad->UserData);
  384.                 running = func();
  385.                 break;
  386.         }
  387.     }
  388.  
  389.     return running;
  390. }
  391.  
  392. void
  393. ShowFont(struct TextFont *tf)
  394. {
  395.     int    i;
  396.  
  397.     DPOS;
  398.  
  399.     PrintInfo("Show font", SPEAK, 0);
  400.  
  401.     if (LayersBase = OpenLibrary((STRPTR) "layers.library", 36))
  402.     {
  403.       LockMainWindow(LOCK);
  404.  
  405.         for (i = 0; i < 4; i++)    flags[i] = defflags[i];
  406.  
  407.         testtext = MyAllocVec(strlen(tf->tf_Message.mn_Node.ln_Name) + 10, MEMF_CLEAR, NO_KILL);
  408.  
  409.         if (testtext)
  410.         {
  411.             testfont.ta_Name = (STRPTR) tf->tf_Message.mn_Node.ln_Name;
  412.             testfont.ta_YSize = tf->tf_YSize;
  413.             testfont.ta_Style = 0;
  414.             testfont.ta_Flags = 0;
  415.  
  416.             if (OpenASysWindow(OpenShowFontWindow, NO_KILL))
  417.             {
  418.                 while (HandleShowFontIDCMP()) ;
  419.  
  420.                 unclipWindow(ShowFontWnd);
  421.  
  422.                 CloseASysWindow(&ShowFontWnd, &ShowFontGList, NULL);
  423.             }
  424.  
  425.             MyFreeVec(testtext);
  426.         }
  427.  
  428.         CloseLibrary(LayersBase);
  429.         LayersBase = NULL;
  430.  
  431.       LockMainWindow(UNLOCK);
  432.     }
  433.     else ErrorHandle("layers.library", LIBRARY_ERR, OPEN_FAIL, KILL);
  434.  
  435.     PrintStatistics();
  436.  
  437.     return;
  438. }
  439.