home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / XfeWidgets / XfeTest / TestUtil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.5 KB  |  372 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:        <XfeTest/TestUtil.c>                                    */
  21. /* Description:    Xfe widget misc utils.                                    */
  22. /* Author:        Ramiro Estrugo <ramiro@netscape.com>                    */
  23. /*                                                                        */
  24. /*----------------------------------------------------------------------*/
  25.  
  26.  
  27. #include <Xfe/XfeTest.h>
  28. #include <ctype.h>
  29.  
  30. static XmString fe_StringChopCreate(char *,char *,XmFontList,int);
  31.  
  32. /*----------------------------------------------------------------------*/
  33. Pixel
  34. XfeButtonRaiseBackground(Widget w)
  35. {
  36.     Pixel raise_background;
  37.  
  38.     assert( XfeIsButton(w) );
  39.  
  40.     XtVaGetValues(w,XmNraiseBackground,&raise_background,NULL);
  41.  
  42.     return raise_background;
  43. }
  44. /*----------------------------------------------------------------------*/
  45. Pixel
  46. XfeButtonArmBackground(Widget w)
  47. {
  48.     Pixel arm_background;
  49.  
  50.     assert( XfeIsButton(w) );
  51.  
  52.     XtVaGetValues(w,XmNarmBackground,&arm_background,NULL);
  53.  
  54.     return arm_background;
  55. }
  56. /*----------------------------------------------------------------------*/
  57. XfeTruncateXmStringProc
  58. XfeGetTruncateXmStringProc(void)
  59. {
  60.     return fe_StringChopCreate;
  61. }
  62. /*----------------------------------------------------------------------*/
  63. /* extern */ void
  64. XfeWidgetToggleManaged(Widget w)
  65. {
  66.     assert( XfeIsAlive(w) );
  67.  
  68.     if (XtIsManaged(w))
  69.     {
  70.         XtUnmanageChild(w);
  71.     }
  72.     else
  73.     {
  74.         XtManageChild(w);
  75.     }
  76. }
  77. /*----------------------------------------------------------------------*/
  78. /* extern */ void
  79. XfeResourceToggleBoolean(Widget w,String name)
  80. {
  81.     Boolean value;
  82.  
  83.     assert( XfeIsAlive(w) );
  84.  
  85.     XtVaGetValues(w,name,&value,NULL);
  86.  
  87.     XtVaSetValues(w,name,!value,NULL);
  88. }
  89. /*----------------------------------------------------------------------*/
  90.  
  91. static Cardinal    _flash_num = 0;
  92. static Boolean    _flash_on = False;
  93. static Cardinal    _flash_ms = 0;
  94. static GC        _flash_gc = NULL;
  95. static Pixel    _flash_bg = 0;
  96. static Pixel    _flash_fg = 0;
  97.  
  98. /*----------------------------------------------------------------------*/
  99. static void
  100. FlashTimeout(XtPointer client_data,XtIntervalId * id)
  101. {
  102.     Widget        w = (Widget) client_data;
  103.  
  104.     if (!_flash_on || !_flash_num || !XfeIsAlive(w))
  105.     {
  106.         _flash_on = False;
  107.         _flash_num = 0;
  108.  
  109.         return;
  110.     }
  111.  
  112.     XFillRectangle(XtDisplay(w),XtWindow(w),_flash_gc,
  113.                    0,0,XfeWidth(w),XfeHeight(w));
  114.  
  115.     _flash_num--;
  116.  
  117.     XtAppAddTimeOut(XtWidgetToApplicationContext(w),
  118.                     _flash_ms,FlashTimeout,(XtPointer) w);    
  119. }
  120. /*----------------------------------------------------------------------*/
  121. /* extern */ void
  122. XfeWidgetFlash(Widget w,Cardinal ms,Cardinal n)
  123. {
  124.     assert( XfeIsAlive(w) );
  125.     assert( n > 0 );
  126.     assert( ms > 0 );
  127.  
  128.     if (_flash_on)
  129.     {
  130.         return;
  131.     }
  132.  
  133.     if (!XfeIsAlive(w) || !XtIsManaged(w))
  134.     {
  135.         return;
  136.     }
  137.  
  138.     _flash_num = n * 2;
  139.     _flash_ms = ms;
  140.     _flash_on = True;
  141.     _flash_bg = XfeBackground(w);
  142.     _flash_fg = XfeForeground(XtParent(w));
  143.  
  144.     if (_flash_gc)
  145.     {
  146.         XtReleaseGC(w,_flash_gc);
  147.     }
  148.  
  149.      _flash_gc = XfeAllocateSelectionGc(w,1,_flash_bg,_flash_fg);
  150.     
  151.     XtAppAddTimeOut(XtWidgetToApplicationContext(w),
  152.                     ms,FlashTimeout,(XtPointer) w);    
  153. }
  154. /*----------------------------------------------------------------------*/
  155. /* extern */ XmFontList
  156. XfeGetFontList(String name)
  157. {
  158.     XmFontList        font_list = NULL;
  159.     XmFontListEntry    entry;
  160.  
  161.     assert( name != NULL );
  162.     assert( XfeIsAlive(XfeAppShell()) );
  163.  
  164.     /* Load (ie, create) a font list entry */
  165.     entry = XmFontListEntryLoad(XtDisplay(XfeAppShell()),
  166.                                 name,
  167.                                 XmFONT_IS_FONT,
  168.                                 XmFONTLIST_DEFAULT_TAG);
  169.     
  170.     if (entry)
  171.     {
  172.         font_list = XmFontListAppendEntry(NULL,entry);
  173.  
  174.         XmFontListEntryFree(&entry);
  175.     }
  176.  
  177.     return font_list;
  178. }
  179. /*----------------------------------------------------------------------*/
  180. /* extern */ XmString
  181. XfeGetXmString(String name)
  182. {
  183.     assert( name != NULL );
  184.  
  185.     return XmStringCreateLtoR(name,XmFONTLIST_DEFAULT_TAG);
  186. }
  187. /*----------------------------------------------------------------------*/
  188. /* extern */ XmFontList *
  189. XfeGetFontListTable(String * names,Cardinal n)
  190. {
  191.     XmFontList *    xm_font_list_table = NULL;
  192.     Cardinal        i;
  193.  
  194.     assert( names != NULL );
  195.     assert( n > 0 );
  196.  
  197.     xm_font_list_table = (XmFontList *) XtMalloc(sizeof(XmFontList) * n);    
  198.  
  199.     for(i = 0; i < n; i++)
  200.     {
  201.         assert( names[i] != NULL );
  202.  
  203.         xm_font_list_table[i] = XfeGetFontList(names[i]);
  204.     }
  205.  
  206.     return xm_font_list_table;
  207. }
  208. /*----------------------------------------------------------------------*/
  209. /* extern */ XmString *
  210. XfeGetXmStringTable(String * names,Cardinal n)
  211. {
  212.     XmString *        xm_string_table = NULL;
  213.     Cardinal        i;
  214.  
  215.     assert( names != NULL );
  216.     assert( n > 0 );
  217.  
  218.     xm_string_table = (XmString *) XtMalloc(sizeof(XmString) * n);    
  219.  
  220.     for(i = 0; i < n; i++)
  221.     {
  222.         assert( names[i] != NULL );
  223.  
  224.         xm_string_table[i] = XfeGetXmString(names[i]);
  225.     }
  226.  
  227.     return xm_string_table;
  228. }
  229. /*----------------------------------------------------------------------*/
  230. /* extern */ void
  231. XfeFreeXmStringTable(XmString * table,Cardinal n)
  232. {
  233.     Cardinal i;
  234.  
  235.     assert( table != NULL );
  236.     assert( n > 0 );
  237.  
  238.     for(i = 0; i < n; i++)
  239.     {
  240.         assert( table[i] != NULL );
  241.         
  242.         XmStringFree(table[i]);
  243.     }
  244.  
  245.     XtFree((char *) table);
  246. }
  247. /*----------------------------------------------------------------------*/
  248. /* extern */ Pixel
  249. XfeGetPixel(String name)
  250. {
  251.     Pixel            pixel;
  252.     XrmValue        from;
  253.     XrmValue        to;
  254.  
  255.     assert( name != NULL );
  256.     assert( XfeIsAlive(XfeAppShell()) );
  257.  
  258.     from.addr    = name;
  259.     from.size    = strlen(name);
  260.  
  261.     to.addr        = (XPointer) &pixel;
  262.     to.size        = sizeof(pixel);
  263.  
  264.     if (!XtConvertAndStore(XfeAppShell(),XmRString,&from,XmRPixel,&to))
  265.     {
  266.         pixel = WhitePixelOfScreen(XtScreen(XfeAppShell()));
  267.     }
  268.  
  269.     return pixel;
  270. }
  271. /*----------------------------------------------------------------------*/
  272. /* extern */ Cursor
  273. XfeGetCursor(String name)
  274. {
  275.     Cursor            cursor;
  276.     XrmValue        from;
  277.     XrmValue        to;
  278.  
  279.     assert( name != NULL );
  280.     assert( XfeIsAlive(XfeAppShell()) );
  281.  
  282.     from.addr    = name;
  283.     from.size    = strlen(name);
  284.  
  285.     to.addr        = (XPointer) &cursor;
  286.     to.size        = sizeof(cursor);
  287.  
  288.     if (!XtConvertAndStore(XfeAppShell(),XmRString,&from,XmRCursor,&to))
  289.     {
  290.         cursor = None;
  291.     }
  292.  
  293.     return cursor;
  294. }
  295. /*----------------------------------------------------------------------*/
  296. /* extern */ void
  297. XfeStringToUpper(String s)
  298. {
  299.     char *pc = s;
  300.  
  301.     while(*pc != '\0')
  302.     {
  303.         *pc = toupper(*pc);
  304.  
  305.         pc++;
  306.     }
  307. }
  308. /*----------------------------------------------------------------------*/
  309. /* extern */ void
  310. XfeStringToLower(String s)
  311. {
  312.     char *pc = s;
  313.  
  314.     while(*pc != '\0')
  315.     {
  316.         *pc = tolower(*pc);
  317.  
  318.         pc++;
  319.     }
  320. }
  321. /*----------------------------------------------------------------------*/
  322.  
  323.  
  324. static XmString
  325. fe_StringChopCreate(char* message, char* tag, XmFontList font_list,
  326.             int maxwidth)
  327. {
  328.   XmString label = XmStringCreate ((char *) message, tag);
  329.   int string_width;
  330.  
  331.   if (!font_list) return label;
  332.  
  333.   string_width = XmStringWidth(font_list, label);
  334.   if (string_width >= maxwidth) {
  335.     /* The string is bigger than the label.  Mid-truncate it. */
  336.  
  337.     XmString try;
  338.     int length = 0;
  339.     int maxlength = strlen(message);
  340.     int tlen;
  341.     int hlen;
  342.     char* text = XtMalloc(maxlength + 10);
  343.     char* end = message + maxlength;
  344.     if (!text) return label;
  345.       
  346.     string_width = 0;
  347.     while (string_width < maxwidth && length < maxlength) {
  348.       length++;
  349.       tlen = length / 2;
  350.       hlen = length - tlen;
  351.       strncpy(text, message, hlen);
  352.       strncpy(text + hlen, "...", 3);
  353.       strncpy(text + hlen + 3, end - tlen, tlen);
  354.       text[length + 3] = '\0';
  355.       try = XmStringCreate(text, tag);
  356.       if (!try) break;
  357.       string_width = XmStringWidth(font_list, try);
  358.       if (string_width >= maxwidth) {
  359.     XmStringFree(try);
  360.       } else {
  361.     XmStringFree(label);
  362.     label = try;
  363.       }
  364.       try = 0;
  365.     }
  366.  
  367.     XtFree (text);
  368.   }
  369.  
  370.   return label;
  371. }
  372.