home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / XfeWidgets / Xfe / Util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  14.5 KB  |  636 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/Util.h>                                            */
  21. /* Description:    Xfe widgets misc utilities source.                        */
  22. /* Author:        Ramiro Estrugo <ramiro@netscape.com>                    */
  23. /*                                                                        */
  24. /*----------------------------------------------------------------------*/
  25.  
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29.  
  30. #include <Xfe/XfeP.h>
  31.  
  32. #include <Xfe/PrimitiveP.h>
  33. #include <Xfe/ManagerP.h>
  34.  
  35. #include <Xm/LabelP.h>
  36. #include <Xm/LabelGP.h>
  37. #include <Xfe/LabelP.h>
  38.  
  39. #if XmVersion >= 2000
  40. #include <Xm/Gadget.h>
  41. #include <Xm/Primitive.h>
  42. #include <Xm/Manager.h>
  43. #endif
  44.  
  45. #define MESSAGE0 "XfeInstancePointer() called with non XfePrimitive or XfeManager widget."
  46.  
  47. /*----------------------------------------------------------------------*/
  48. /*                                                                        */
  49. /* Simple Widget access functions                                        */
  50. /*                                                                        */
  51. /*----------------------------------------------------------------------*/
  52. /* extern */ Widget
  53. XfeWindowedWidget(Widget w)
  54. {
  55.     assert( _XfeIsAlive(w) );
  56.  
  57.     if (!_XfeIsAlive(w))
  58.     {
  59.         return NULL;
  60.     }
  61.  
  62.     return (XmIsGadget(w) ? XtParent(w) : w);
  63. }
  64. /*----------------------------------------------------------------------*/
  65. /* extern */ Boolean
  66. XfeIsViewable(Widget w)
  67. {
  68.     XWindowAttributes xwa;
  69.  
  70.     if (!_XfeIsAlive(w) || !XtIsWidget(w) || !_XfeIsRealized(w))
  71.     {
  72.         return False;
  73.     }
  74.  
  75.     if (XGetWindowAttributes(XtDisplay(w),_XfeWindow(w),&xwa))
  76.     {
  77.         return (xwa.map_state == IsViewable);
  78.     }
  79.     
  80.     return False;
  81. }
  82. /*----------------------------------------------------------------------*/
  83. /* extern */ Boolean
  84. XfeIsAlive(Widget w)
  85. {
  86.     return _XfeIsAlive(w);
  87. }
  88. /*----------------------------------------------------------------------*/
  89. /* extern */ XtPointer
  90. XfeUserData(Widget w)
  91. {
  92.     XtPointer user_data = NULL;
  93.     
  94.     assert( w != NULL );
  95.     assert(XmIsGadget(w) || XmIsPrimitive(w) || XmIsManager(w));
  96.     
  97.     if (XmIsPrimitive(w))
  98.     {
  99.         user_data = _XfeUserData(w);
  100.     }
  101.     else if (XmIsManager(w))
  102.     {
  103.         user_data = _XfemUserData(w);
  104.     }
  105.     else if (XmIsGadget(w))
  106.     {
  107.         XtVaGetValues(w,XmNuserData,&user_data,NULL);
  108.     }
  109.     
  110.     return user_data;
  111. }
  112. /*----------------------------------------------------------------------*/
  113. /* extern */ XtPointer
  114. XfeInstancePointer(Widget w)
  115. {
  116.     assert( w != NULL );
  117.     assert( XfeIsPrimitive(w) || XfeIsManager(w) );
  118.     
  119.     if (XfeIsPrimitive(w))
  120.     {
  121.         return _XfeInstancePointer(w);
  122.     }
  123.     else
  124.     {
  125.         return _XfemInstancePointer(w);
  126.     }
  127.  
  128.     _XfeWarning(w,MESSAGE0);
  129.  
  130.     return NULL;
  131. }
  132. /*----------------------------------------------------------------------*/
  133. /* extern */ Colormap
  134. XfeColormap(Widget w)
  135. {
  136.     assert( _XfeIsAlive(w) );
  137.     
  138.     return _XfeColormap(w);
  139. }
  140. /*----------------------------------------------------------------------*/
  141. /* extern */ Cardinal
  142. XfeDepth(Widget w)
  143. {
  144.     assert( _XfeIsAlive(w) );
  145.     
  146.     return _XfeDepth(w);
  147. }
  148. /*----------------------------------------------------------------------*/
  149. /* extern */ Pixel
  150. XfeBackground(Widget w)
  151. {
  152.     assert( _XfeIsAlive(w) );
  153.  
  154.     if (!_XfeIsAlive(w))
  155.     {
  156.         return 0;
  157.     }
  158.     
  159.     return _XfeBackgroundPixel(w);
  160. }
  161. /*----------------------------------------------------------------------*/
  162. /* extern */ Pixel
  163. XfeForeground(Widget w)
  164. {
  165.     assert( _XfeIsAlive(w) );
  166.  
  167.     if (!_XfeIsAlive(w))
  168.     {
  169.         return 0;
  170.     }
  171.     
  172.     if (XmIsPrimitive(w))
  173.     {
  174.         return _XfeForeground(w);
  175.     }
  176.  
  177.     return _XfemForeground(w);
  178. }
  179. /*----------------------------------------------------------------------*/
  180. /* extern */ String
  181. XfeClassNameForWidget(Widget w)
  182. {
  183.     assert( w != NULL );
  184.  
  185.     return _XfeClassName(_XfeClass(w));
  186. }
  187. /*----------------------------------------------------------------------*/
  188.  
  189.  
  190. /*----------------------------------------------------------------------*/
  191. /*                                                                        */
  192. /* Simple WidgetClass access functions                                    */
  193. /*                                                                        */
  194. /*----------------------------------------------------------------------*/
  195. /* extern */ String
  196. XfeClassName(WidgetClass wc)
  197. {
  198.     assert( wc != NULL );
  199.  
  200.     return _XfeClassName(wc);
  201. }
  202. /*----------------------------------------------------------------------*/
  203. /* extern */ WidgetClass
  204. XfeSuperClass(WidgetClass wc)
  205. {
  206.     assert( wc != NULL );
  207.  
  208.     return _XfeSuperClass(wc);
  209. }
  210. /*----------------------------------------------------------------------*/
  211.  
  212.  
  213. /*----------------------------------------------------------------------*/
  214. /*                                                                        */
  215. /* XmFontList                                                            */
  216. /*                                                                        */
  217. /*----------------------------------------------------------------------*/
  218. /* extern */ XmFontList
  219. XfeXmFontListCopy(Widget w,XmFontList font_list,unsigned char font_type)
  220. {
  221.     XmFontList new_font_list;
  222.     
  223.     /* If the font is null, create a default one */
  224.     if (!font_list)
  225.     {
  226.         new_font_list = XmFontListCopy(_XmGetDefaultFontList(w,font_type));
  227.     }
  228.     /* Otherwise make a carbon copy */
  229.     else
  230.     {
  231.         new_font_list = XmFontListCopy(font_list);
  232.     }
  233.     
  234.     return new_font_list;
  235. }
  236. /*----------------------------------------------------------------------*/
  237. /* extern */ XmFontList
  238. XfeXmFontListAccess(Widget w)
  239. {
  240.     XmFontList    font_list = NULL;
  241.  
  242.     assert( _XfeIsAlive(w) );
  243.  
  244.     if (!_XfeIsAlive(w))
  245.     {
  246.         return NULL;
  247.     }
  248.  
  249.     if (XmIsLabel(w))
  250.     {
  251.         font_list = ((XmLabelWidget) w) -> label . font;
  252.     }
  253.     else if (XmIsLabelGadget(w))
  254.     {
  255.         font_list = ((XmLabelGadget) w) -> label . font;
  256.     }
  257.     else if (XfeIsLabel(w))
  258.     {
  259.         font_list = ((XfeLabelWidget) w) -> xfe_label . font_list;
  260.     }
  261.  
  262.     return font_list;
  263. }
  264. /*----------------------------------------------------------------------*/
  265.  
  266.  
  267. /*----------------------------------------------------------------------*/
  268. /*                                                                        */
  269. /* XmString                                                                */
  270. /*                                                                        */
  271. /*----------------------------------------------------------------------*/
  272. /* extern */ XmString
  273. XfeXmStringCopy(Widget w,XmString xm_string,String fallback)
  274. {
  275.     XmString new_xm_string;
  276.     
  277.     /* Make sure the string is setup properly */
  278.     if (!xm_string)
  279.     {
  280.         /* If no xmstring is given, create using the fallback cstring */
  281.         new_xm_string = XmStringCreateLocalized(fallback);
  282.     }
  283.     else
  284.     {
  285.         /* Otherwise make a carbon copy - no check done to verify xmstring */
  286.         new_xm_string = XmStringCopy(xm_string);
  287.     }
  288.     
  289.     return new_xm_string;
  290. }
  291. /*----------------------------------------------------------------------*/
  292. /* extern */ String
  293. XfeXmStringGetPSZ(XmString xm_string,char * tag)
  294. {
  295.     String        psz_string = NULL;
  296.  
  297.     if (xm_string)
  298.     {
  299.         XmStringGetLtoR(xm_string,tag,&psz_string);
  300.     }    
  301.     
  302.     return psz_string;
  303. }
  304. /*----------------------------------------------------------------------*/
  305. /*extern*/ void
  306. XfeSetXmStringPSZ(Widget w,String name,char * tag,char * value)
  307. {
  308.     XmString xm_string;
  309.  
  310.     assert( w != NULL );
  311.     assert( name != NULL );
  312.  
  313.     xm_string = XmStringCreateLtoR(value,tag);
  314.  
  315.     XtVaSetValues(w,name,xm_string,NULL);
  316.  
  317.     if (xm_string)
  318.     {
  319.         XmStringFree(xm_string);
  320.     }
  321. }
  322. /*----------------------------------------------------------------------*/
  323.  
  324. /*----------------------------------------------------------------------*/
  325. /*                                                                        */
  326. /* Colors                                                                */
  327. /*                                                                        */
  328. /*----------------------------------------------------------------------*/
  329. /* extern */ Pixel
  330. XfeSelectPixel(Widget w,Pixel base)
  331. {
  332.     Pixel    select_pixel;
  333.     Widget    core_widget = XfeWindowedWidget(w);
  334.     
  335.     XmGetColors(_XfeScreen(core_widget),_XfeColormap(core_widget),
  336.                 base,NULL,NULL,NULL,&select_pixel);
  337.     
  338.     return select_pixel;
  339. }
  340. /*----------------------------------------------------------------------*/
  341.  
  342. /*----------------------------------------------------------------------*/
  343. /*                                                                        */
  344. /* Explicit invocation of core methods.                                  */
  345. /*                                                                        */
  346. /*----------------------------------------------------------------------*/
  347. /* extern */ void
  348. XfeExpose(Widget w,XEvent * event,Region region)
  349. {
  350.     WidgetClass wc;
  351.  
  352.     assert( _XfeIsAlive(w) );
  353.     assert( XtClass(w) != NULL );
  354.  
  355.     if (!_XfeIsAlive(w))
  356.     {
  357.         return;
  358.     }
  359.  
  360.     wc = XtClass(w);
  361.     
  362.     assert( wc->core_class.expose != NULL );
  363.  
  364.     (*wc->core_class.expose)(w,event,region);
  365. }
  366. /*----------------------------------------------------------------------*/
  367. /* extern */ void
  368. XfeResize(Widget w)
  369. {
  370.     WidgetClass wc;
  371.  
  372.     assert( _XfeIsAlive(w) );
  373.     assert( XtClass(w) != NULL );
  374.  
  375.     if (!_XfeIsAlive(w))
  376.     {
  377.         return;
  378.     }
  379.  
  380.     wc = XtClass(w);
  381.     
  382.     assert( wc->core_class.resize != NULL );
  383.  
  384.     (*wc->core_class.resize)(w);
  385. }
  386. /*----------------------------------------------------------------------*/
  387.  
  388. /*----------------------------------------------------------------------*/
  389. /*                                                                        */
  390. /* Sleep routine.                                                        */
  391. /*                                                                        */
  392. /*----------------------------------------------------------------------*/
  393. static void
  394. SleepTimeout(XtPointer client_data,XtIntervalId * id)
  395. {
  396.      Boolean * sleeping = (Boolean *) client_data;
  397.  
  398.      *sleeping = False;
  399. }
  400. /*----------------------------------------------------------------------*/
  401. /* extern */ void
  402. XfeSleep(Widget w,XfeEventLoopProc proc,int ms)
  403. {
  404.      Boolean        sleeping = True;
  405.  
  406.     assert( _XfeIsAlive(w) );
  407.     assert( proc != NULL );
  408.  
  409.     XtAppAddTimeOut(XtWidgetToApplicationContext(w),
  410.                     ms,
  411.                     SleepTimeout,
  412.                     (XtPointer) &sleeping);    
  413.  
  414.     /* Invoke the user's event loop while we are sleeping */
  415.     while(sleeping)
  416.     {
  417.         (*proc)();
  418.     }
  419. }
  420. /*----------------------------------------------------------------------*/
  421.  
  422. /*----------------------------------------------------------------------*/
  423. /* extern */ void 
  424. XfeRectSet(XRectangle *    rect,
  425.            Position        x,
  426.            Position        y,
  427.            Dimension    width,
  428.            Dimension    height)
  429. {
  430.     assert( rect != NULL );
  431.  
  432.     rect->x            = x;
  433.     rect->y            = y;
  434.     rect->width        = width;
  435.     rect->height    = height;
  436. }
  437. /*----------------------------------------------------------------------*/
  438. /* extern */ void 
  439. XfeRectCopy(XRectangle * dst,XRectangle * src)
  440. {
  441.     assert( dst != NULL );
  442.     assert( src != NULL );
  443.  
  444.     dst->x            = src->x;
  445.     dst->y            = src->y;
  446.     dst->width        = src->width;
  447.     dst->height        = src->height;
  448. }
  449. /*----------------------------------------------------------------------*/
  450. /* extern */ void 
  451. XfePointSet(XPoint *    point,
  452.             Position    x,
  453.             Position    y)
  454. {
  455.     assert( point != NULL );
  456.  
  457.     point->x    = x;
  458.     point->y    = y;
  459. }
  460. /*----------------------------------------------------------------------*/
  461. /* extern */ Boolean
  462. XfePointInRect(XRectangle *    rect,int x,int y)
  463. {
  464.     assert( rect != NULL );
  465.  
  466.     return ( (x >= rect->x) &&
  467.              (x <= (rect->x + rect->width)) &&
  468.              (y >= rect->y) &&
  469.              (y <= (rect->y + rect->height)) );
  470. }
  471. /*----------------------------------------------------------------------*/
  472.  
  473. /*----------------------------------------------------------------------*/
  474. /*                                                                        */
  475. /* Management                                                            */
  476. /*                                                                        */
  477. /*----------------------------------------------------------------------*/
  478. /* extern */ void
  479. XfeSetManagedState(Widget w,Boolean state)
  480. {
  481.     if (!_XfeIsAlive(w))
  482.     {
  483.         return;
  484.     }
  485.  
  486.     if (state)
  487.     {
  488.         XtManageChild(w);
  489.     }
  490.     else
  491.     {
  492.         XtUnmanageChild(w);
  493.     }
  494. }
  495. /*----------------------------------------------------------------------*/
  496. /* extern */ void
  497. XfeToggleManagedState(Widget w)
  498. {
  499.     if (!_XfeIsAlive(w))
  500.     {
  501.         return;
  502.     }
  503.  
  504.     if (_XfeIsManaged(w))
  505.     {
  506.         XtUnmanageChild(w);
  507.     }
  508.     else
  509.     {
  510.         XtManageChild(w);
  511.     }
  512. }
  513. /*----------------------------------------------------------------------*/
  514.  
  515. /*----------------------------------------------------------------------*/
  516. /*                                                                        */
  517. /* XEvent functions                                                        */
  518. /*                                                                        */
  519. /*----------------------------------------------------------------------*/
  520. /* extern */ Boolean
  521. XfeEventGetXY(XEvent * event,int * x_out,int * y_out)
  522. {
  523.     Boolean    result = False;
  524.     int        x = 0;
  525.     int        y = 0;
  526.  
  527.     assert( x_out || y_out );
  528.     
  529.     if (event)
  530.     {
  531.         if (event->type == EnterNotify || event->type == LeaveNotify)
  532.         {
  533.             x = event->xcrossing.x;
  534.             y = event->xcrossing.y;
  535.  
  536.             result = True;
  537.         }
  538.         else if (event->type == MotionNotify)
  539.         {
  540.             x = event->xmotion.x;
  541.             y = event->xmotion.y;
  542.  
  543.             result = True;
  544.         }
  545.         else if (event->type == ButtonPress || event->type == ButtonRelease)
  546.         {
  547.             x = event->xbutton.x;
  548.             y = event->xbutton.y;
  549.  
  550.             result = True;
  551.         }
  552.     }
  553.  
  554.     if (x_out)
  555.     {
  556.         *x_out = x;
  557.     }
  558.  
  559.     if (y_out)
  560.     {
  561.         *y_out = y;
  562.     }
  563.  
  564.     return result;
  565. }
  566. /*----------------------------------------------------------------------*/
  567. /* extern */ Boolean
  568. XfeEventGetRootXY(XEvent * event,int * x_out,int * y_out)
  569. {
  570.     Boolean    result = False;
  571.     int        x = 0;
  572.     int        y = 0;
  573.  
  574.     assert( x_out || y_out );
  575.     
  576.     if (event)
  577.     {
  578.         if (event->type == EnterNotify || event->type == LeaveNotify)
  579.         {
  580.             x = event->xcrossing.x_root;
  581.             y = event->xcrossing.y_root;
  582.  
  583.             result = True;
  584.         }
  585.         else if (event->type == MotionNotify)
  586.         {
  587.             x = event->xmotion.x_root;
  588.             y = event->xmotion.y_root;
  589.  
  590.             result = True;
  591.         }
  592.         else if (event->type == ButtonPress || event->type == ButtonRelease)
  593.         {
  594.             x = event->xbutton.x_root;
  595.             y = event->xbutton.y_root;
  596.  
  597.             result = True;
  598.         }
  599.     }
  600.  
  601.     if (x_out)
  602.     {
  603.         *x_out = x;
  604.     }
  605.  
  606.     if (y_out)
  607.     {
  608.         *y_out = y;
  609.     }
  610.  
  611.     return result;
  612. }
  613. /*----------------------------------------------------------------------*/
  614. /* extern */ Modifiers
  615. XfeEventGetModifiers(XEvent * event)
  616. {
  617.     if (event)
  618.     {
  619.         if (event->type == ButtonPress || event->type == ButtonRelease)
  620.         {
  621.             return event->xbutton.state;
  622.         }
  623.         else if (event->type == MotionNotify)
  624.         {
  625.             return event->xmotion.state;
  626.         }
  627.         else if (event->type == KeyPress || event->type == KeyRelease)
  628.         {
  629.             return event->xkey.state;
  630.         }
  631.     }
  632.  
  633.     return 0;
  634. }
  635. /*----------------------------------------------------------------------*/
  636.