home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / XfeWidgets / Xfe / FontChooser.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  15.5 KB  |  483 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /*-----------------------------------------*/
  19. /*                                                                        */
  20. /* Name:        <Xfe/FontChooser.c>                                        */
  21. /* Description:    XfeFontChooser widget source.                            */
  22. /* Author:        Ramiro Estrugo <ramiro@netscape.com>                    */
  23. /*                                                                        */
  24. /*----------------------------------------------------------------------*/
  25.  
  26.  
  27. #include <Xfe/FontChooserP.h>
  28. #include <Xfe/ManagerP.h>
  29.  
  30. #include <Xm/ToggleB.h>
  31.  
  32. /*----------------------------------------------------------------------*/
  33. /*                                                                        */
  34. /* Warnings and messages                                                */
  35. /*                                                                        */
  36. /*----------------------------------------------------------------------*/
  37. #define MESSAGE1 "Widget is not a XfeFontChooser."
  38.  
  39. #define FONT_CHOOSER_ITEM_NAME "FontChooserItemName"
  40.  
  41. /*----------------------------------------------------------------------*/
  42. /*                                                                        */
  43. /* Core class methods                                                    */
  44. /*                                                                        */
  45. /*----------------------------------------------------------------------*/
  46. static void     Initialize        (Widget,Widget,ArgList,Cardinal *);
  47. static void     Destroy            (Widget);
  48. static Boolean    SetValues        (Widget,Widget,Widget,ArgList,Cardinal *);
  49.  
  50. /*----------------------------------------------------------------------*/
  51. /*                                                                        */
  52. /* Misc XfeFontChooser functions                                        */
  53. /*                                                                        */
  54. /*----------------------------------------------------------------------*/
  55. static XmFontList    FontListAccess        (Widget,Cardinal);
  56. static XmString        StringAccess        (Widget,Cardinal);
  57. static void            UpdateItems            (Widget);
  58. static void            UpdateFonts            (Widget);
  59. static void            UpdateLabels        (Widget);
  60.  
  61. /*----------------------------------------------------------------------*/
  62. /*                                                                        */
  63. /* Item callbacks                                                        */
  64. /*                                                                        */
  65. /*----------------------------------------------------------------------*/
  66. static void            ItemArmCB            (Widget,XtPointer,XtPointer);
  67. static void            ItemValueChangedCB    (Widget,XtPointer,XtPointer);
  68. static void            ItemDisarmCB        (Widget,XtPointer,XtPointer);
  69.  
  70. /*----------------------------------------------------------------------*/
  71. /*                                                                        */
  72. /* XfeFontChooser Resources                                                */
  73. /*                                                                        */
  74. /*----------------------------------------------------------------------*/
  75. static XtResource resources[] = 
  76. {
  77.     /* Callback resources */         
  78.     { 
  79.         XmNselectionChangedCallback,
  80.         XmCCallback,
  81.         XmRCallback,
  82.         sizeof(XtCallbackList),
  83.         XtOffsetOf(XfeFontChooserRec,xfe_font_chooser . selection_changed_callback),
  84.         XmRImmediate, 
  85.         (XtPointer) NULL
  86.     },
  87.  
  88.     /* Font item resources */
  89.     { 
  90.         XmNnumFontItems,
  91.         XmCNumFontItems,
  92.         XmRCardinal,
  93.         sizeof(Cardinal),
  94.         XtOffsetOf(XfeFontChooserRec , xfe_font_chooser . num_font_items),
  95.         XmRImmediate, 
  96.          (XtPointer) 0
  97.     },
  98.     { 
  99.         XmNfontItemLabels,
  100.         XmCFontItemLabels,
  101.         XmRXmStringTable,
  102.         sizeof(XmString *),
  103.         XtOffsetOf(XfeFontChooserRec , xfe_font_chooser . font_item_labels),
  104.         XmRImmediate, 
  105.          (XtPointer) NULL
  106.     },
  107.     { 
  108.         XmNfontItemFonts,
  109.         XmCFontItemFonts,
  110.         XmRFontList,
  111.         sizeof(XmFontList *),
  112.         XtOffsetOf(XfeFontChooserRec , xfe_font_chooser . font_item_fonts),
  113.         XmRImmediate, 
  114.          (XtPointer) NULL
  115.     },
  116.  
  117.     /* Force mapping_delay to 0 */
  118.     { 
  119.         XmNmappingDelay,
  120.         XmCMappingDelay,
  121.         XmRInt,
  122.         sizeof(int),
  123.         XtOffsetOf(XfeFontChooserRec , xfe_cascade . mapping_delay),
  124.         XmRImmediate, 
  125.          (XtPointer) 0
  126.     },
  127.  
  128.     /* Force sub_menu_alignment to XmALIGNMENT_END */
  129.     { 
  130.         XmNsubMenuAlignment,
  131.         XmCSubMenuAlignment,
  132.         XmRAlignment,
  133.         sizeof(unsigned char),
  134.         XtOffsetOf(XfeFontChooserRec , xfe_cascade . sub_menu_alignment),
  135.         XmRImmediate, 
  136.         (XtPointer) XmALIGNMENT_END
  137.     },
  138. };
  139.  
  140. /*----------------------------------------------------------------------*/
  141. /*                                                                        */
  142. /* XfeFontChooser widget class record initialization                    */
  143. /*                                                                        */
  144. /*----------------------------------------------------------------------*/
  145. _XFE_WIDGET_CLASS_RECORD(fontchooser,FontChooser) =
  146. {
  147.     {
  148.         /* Core Part */
  149.         (WidgetClass) &xfeCascadeClassRec,        /* superclass             */
  150.         "XfeFontChooser",                        /* class_name             */
  151.         sizeof(XfeFontChooserRec),                /* widget_size            */
  152.         NULL,                                    /* class_initialize       */
  153.         NULL,                                    /* class_part_initialize*/
  154.         FALSE,                                  /* class_inited           */
  155.         Initialize,                             /* initialize             */
  156.         NULL,                                   /* initialize_hook        */
  157.         XtInheritRealize,                       /* realize                */
  158.         NULL,                                    /* actions                */
  159.         0,                                        /* num_actions            */
  160.         resources,                              /* resources              */
  161.         XtNumber(resources),                    /* num_resources          */
  162.         NULLQUARK,                              /* xrm_class              */
  163.         TRUE,                                   /* compress_motion        */
  164.         XtExposeCompressMaximal,                /* compress_exposure      */
  165.         TRUE,                                   /* compress_enterleave    */
  166.         FALSE,                                  /* visible_interest       */
  167.         Destroy,                                /* destroy                */
  168.         XtInheritResize,                        /* resize                 */
  169.         XtInheritExpose,                        /* expose                 */
  170.         SetValues,                              /* set_values             */
  171.         NULL,                                   /* set_values_hook        */
  172.         XtInheritSetValuesAlmost,                /* set_values_almost      */
  173.         NULL,                                    /* get_values_hook        */
  174.         NULL,                                   /* accept_focus           */
  175.         XtVersion,                              /* version                */
  176.         NULL,                                   /* callback_private       */
  177.         XtInheritTranslations,                    /* tm_table               */
  178.         XtInheritQueryGeometry,                    /* query_geometry         */
  179.         XtInheritDisplayAccelerator,            /* display accel          */
  180.         NULL,                                   /* extension              */
  181.     },
  182.  
  183.     /* XmPrimitive Part */
  184.     {
  185.         XmInheritBorderHighlight,                /* border_highlight        */
  186.         XmInheritBorderUnhighlight,                /* border_unhighlight     */
  187.         XtInheritTranslations,                  /* translations           */
  188.         XmInheritArmAndActivate,                /* arm_and_activate       */
  189.         NULL,                                    /* syn resources          */
  190.         0,                                        /* num syn_resources      */
  191.         NULL,                                    /* extension              */
  192.     },
  193.     
  194.     /* XfePrimitive Part */
  195.     {
  196.         XfeInheritBitGravity,                    /* bit_gravity            */
  197.         XfeInheritPreferredGeometry,            /* preferred_geometry    */
  198.         XfeInheritMinimumGeometry,                /* minimum_geometry        */
  199.         XfeInheritUpdateRect,                    /* update_rect            */
  200.         NULL,                                    /* prepare_components    */
  201.         XfeInheritLayoutComponents,                /* layout_components    */
  202.         XfeInheritDrawBackground,                /* draw_background        */
  203.         XfeInheritDrawShadow,                    /* draw_shadow            */
  204.         XfeInheritDrawComponents,                /* draw_components        */
  205.         NULL,                                    /* extension            */
  206.     },
  207.  
  208.     /* XfeLabel Part */
  209.     {
  210.         XfeInheritLayoutString,                    /* layout_string        */
  211.         XfeInheritDrawString,                    /* draw_string            */
  212.         XfeInheritDrawSelection,                /* draw_selection        */
  213.         XfeInheritGetLabelGC,                    /* get_label_gc            */
  214.         XfeInheritGetSelectionGC,                /* get_selection_gc        */
  215.         NULL,                                    /* extension            */
  216.     },
  217.  
  218.     /* XfeButton Part */
  219.     {
  220.         XfeInheritLayoutPixmap,                    /* layout_pixmap        */
  221.         XfeInheritDrawPixmap,                    /* draw_pixmap            */
  222.         XfeInheritDrawRaiseBorder,                /* draw_raise_border    */
  223.         XfeInheritArmTimeout,                    /* arm_timeout            */
  224.         NULL,                                    /* extension            */
  225.     },
  226.  
  227.     /* XfeCascade Part */
  228.     {
  229.         NULL,                                    /* extension            */
  230.     },
  231.  
  232.     /* XfeFontChooser Part */
  233.     {
  234.         NULL,                                    /* extension            */
  235.     },
  236. };
  237.  
  238. /*----------------------------------------------------------------------*/
  239. /*                                                                        */
  240. /* xfeFontChooserWidgetClass declaration.                                */
  241. /*                                                                        */
  242. /*----------------------------------------------------------------------*/
  243. _XFE_WIDGET_CLASS(fontchooser,FontChooser);
  244.  
  245. /*----------------------------------------------------------------------*/
  246. /*                                                                        */
  247. /* Core class methods                                                    */
  248. /*                                                                        */
  249. /*----------------------------------------------------------------------*/
  250. static void
  251. Initialize(Widget rw,Widget nw,ArgList args,Cardinal *nargs)
  252. {
  253.     XfeFontChooserPart *    fp = _XfeFontChooserPart(nw);
  254.     XfeCascadePart *        cp = _XfeCascadePart(nw);
  255.  
  256.     XtVaSetValues(cp->sub_menu_id,XmNradioBehavior,True,NULL);
  257.  
  258.     UpdateItems(nw);
  259.     UpdateLabels(nw);
  260.     UpdateFonts(nw);
  261.  
  262.     /* Finish of initialization */
  263.     _XfePrimitiveChainInitialize(rw,nw,xfeFontChooserWidgetClass);
  264. }
  265. /*----------------------------------------------------------------------*/
  266. static void
  267. Destroy(Widget w)
  268. {
  269.     XfeFontChooserPart *    cp = _XfeFontChooserPart(w);
  270.  
  271.     /* Remove all CallBacks */
  272.     /* XtRemoveAllCallbacks(w,XmNselectionCallback); */
  273. }
  274. /*----------------------------------------------------------------------*/
  275. static Boolean
  276. SetValues(Widget ow,Widget rw,Widget nw,ArgList args,Cardinal *nargs)
  277. {
  278.     XfeFontChooserPart *    np = _XfeFontChooserPart(nw);
  279.     XfeFontChooserPart *    op = _XfeFontChooserPart(ow);
  280.  
  281.     /* num_font_items */
  282.     if (np->num_font_items != op->num_font_items)
  283.     {
  284.         UpdateItems(nw);
  285.  
  286.         _XfeConfigFlags(nw) |= XfeConfigGLE;
  287.     }
  288.  
  289.     /* font_item_labels */
  290.     if (np->font_item_labels != op->font_item_labels)
  291.     {
  292.         UpdateLabels(nw);
  293.  
  294.         _XfeConfigFlags(nw) |= XfeConfigGLE;
  295.     }
  296.  
  297.     /* font_item_fonts */
  298.     if (np->font_item_fonts != op->font_item_fonts)
  299.     {
  300.         UpdateFonts(nw);
  301.  
  302.         _XfeConfigFlags(nw) |= XfeConfigGLE;
  303.     }
  304.  
  305.     return _XfePrimitiveChainSetValues(ow,rw,nw,xfeFontChooserWidgetClass);
  306. }
  307. /*----------------------------------------------------------------------*/
  308.  
  309. /*----------------------------------------------------------------------*/
  310. /*                                                                        */
  311. /* Misc XfeFontChooser functions                                        */
  312. /*                                                                        */
  313. /*----------------------------------------------------------------------*/
  314. static XmFontList
  315. FontListAccess(Widget w,Cardinal i)
  316. {
  317.     XfeFontChooserPart *    cp = _XfeFontChooserPart(w);
  318.     XfeLabelPart *            lp = _XfeLabelPart(w);
  319.  
  320.     assert( i < cp->num_font_items );
  321.  
  322.     if (cp->font_item_fonts && cp->font_item_fonts[i])
  323.     {
  324.         return cp->font_item_fonts[i];
  325.     }
  326.  
  327.     return lp->font_list;
  328. }
  329. /*----------------------------------------------------------------------*/
  330. static XmString
  331. StringAccess(Widget w,Cardinal i)
  332. {
  333.     XfeFontChooserPart *    cp = _XfeFontChooserPart(w);
  334.  
  335.     assert( i < cp->num_font_items );
  336.  
  337.     if (cp->font_item_labels && cp->font_item_labels[i])
  338.     {
  339.         return cp->font_item_labels[i];
  340.     }
  341.  
  342.     return NULL;
  343. }
  344. /*----------------------------------------------------------------------*/
  345. static void
  346. UpdateItems(Widget w)
  347. {
  348.     XfeFontChooserPart *    cp = _XfeFontChooserPart(w);
  349.     Cardinal                i;
  350.     Widget                    sub_menu_id = XfeCascadeGetSubMenu(w);
  351.     Cardinal                num_available = _XfemNumChildren(sub_menu_id);
  352.  
  353.     /* No need to add or remove items */
  354.     if (num_available == cp->num_font_items)
  355.     {
  356.         return;
  357.     }
  358.  
  359.     /* Too many items available - kill the extra */
  360.     if (num_available > cp->num_font_items)
  361.     {
  362.         for(i = cp->num_font_items; i < num_available; i++)
  363.         {
  364.             XtDestroyWidget(_XfeChildrenIndex(sub_menu_id,i));
  365.         }
  366.     }
  367.     /* Not enough items available - add the needed extras */
  368.     else
  369.     {
  370.         for(i = num_available; i < cp->num_font_items; i++)
  371.         {
  372.             Arg                av[10];
  373.             Cardinal        ac = 0;
  374.             Widget            item;
  375.  
  376.             XtSetArg(av[ac],XmNindicatorType,    XmONE_OF_MANY);        ac++;
  377.             
  378.             item = XtCreateManagedWidget(FONT_CHOOSER_ITEM_NAME,
  379.                                          xmToggleButtonWidgetClass,
  380.                                          sub_menu_id,av,ac);
  381.             
  382.             XtAddCallback(item,XmNarmCallback,ItemArmCB,w);
  383.             XtAddCallback(item,XmNdisarmCallback,ItemDisarmCB,w);
  384.             XtAddCallback(item,XmNvalueChangedCallback,ItemValueChangedCB,w);
  385.         }
  386.     }
  387. }
  388. /*----------------------------------------------------------------------*/
  389. static void
  390. UpdateFonts(Widget w)
  391. {
  392.     XfeFontChooserPart *    cp = _XfeFontChooserPart(w);
  393.     Cardinal                i;
  394.     Widget                    sub_menu_id = XfeCascadeGetSubMenu(w);
  395.  
  396.     for(i = 0; i < cp->num_font_items; i++)
  397.     {
  398.         Widget            item = _XfeChildrenIndex(sub_menu_id,i);
  399.         XmFontList        font_list = FontListAccess(w,i);
  400.  
  401.         if (_XfeIsAlive(item) && font_list)
  402.         {
  403.             XtVaSetValues(item,XmNfontList,font_list,NULL);
  404.         }
  405.     }
  406. }
  407. /*----------------------------------------------------------------------*/
  408. static void
  409. UpdateLabels(Widget w)
  410. {
  411.     XfeFontChooserPart *    cp = _XfeFontChooserPart(w);
  412.     Cardinal                i;
  413.     Widget                    sub_menu_id = XfeCascadeGetSubMenu(w);
  414.     
  415.     for(i = 0; i < cp->num_font_items; i++)
  416.     {
  417.         Widget            item = _XfeChildrenIndex(sub_menu_id,i);
  418.         XmString        xm_string = StringAccess(w,i);
  419.  
  420.         if (_XfeIsAlive(item) && xm_string)
  421.         {
  422.             XtVaSetValues(item,XmNlabelString,xm_string,NULL);
  423.         }
  424.     }
  425. }
  426. /*----------------------------------------------------------------------*/
  427.  
  428. /*----------------------------------------------------------------------*/
  429. /*                                                                        */
  430. /* Item callbacks                                                        */
  431. /*                                                                        */
  432. /*----------------------------------------------------------------------*/
  433. static void
  434. ItemArmCB(Widget item,XtPointer client_data,XtPointer call_data)
  435. {
  436.     Widget                    w = (Widget) client_data;
  437.     XfeFontChooserPart *    cp = _XfeFontChooserPart(w);
  438.  
  439. }
  440. /*----------------------------------------------------------------------*/
  441. static void
  442. ItemValueChangedCB(Widget item,XtPointer client_data,XtPointer call_data)
  443. {
  444.     Widget                    w = (Widget) client_data;
  445.     XfeFontChooserPart *    cp = _XfeFontChooserPart(w);
  446.  
  447. }
  448. /*----------------------------------------------------------------------*/
  449. static void
  450. ItemDisarmCB(Widget item,XtPointer client_data,XtPointer call_data)
  451. {
  452.     Widget                    w = (Widget) client_data;
  453.     XfeFontChooserPart *    cp = _XfeFontChooserPart(w);
  454.  
  455. }
  456. /*----------------------------------------------------------------------*/
  457.  
  458. /*----------------------------------------------------------------------*/
  459. /*                                                                        */
  460. /* XfeFontChooser Public Methods                                        */
  461. /*                                                                        */
  462. /*----------------------------------------------------------------------*/
  463. Widget
  464. XfeCreateFontChooser(Widget pw,char * name,Arg * av,Cardinal ac)
  465. {
  466.     return XtCreateWidget(name,xfeFontChooserWidgetClass,pw,av,ac);
  467. }
  468. /*----------------------------------------------------------------------*/
  469. /* extern */ void
  470. XfeFontChooserDestroyChildren(Widget w)
  471. {
  472. #if 0
  473.     XfeFontChooserPart *    cp = _XfeFontChooserPart(w);
  474.  
  475.     assert( _XfeIsAlive(w) );
  476.     assert( XfeIsFontChooser(w) );
  477.     assert( _XfeIsAlive(cp->sub_menu_id) );
  478.  
  479.     XfeChildrenDestroy(cp->sub_menu_id);
  480. #endif
  481. }
  482. /*----------------------------------------------------------------------*/
  483.