home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / XfeWidgets / Xfe / Find.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  10.4 KB  |  393 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/Find.c>                                            */
  21. /* Description:    Xfe widgets utilities to find children.                    */
  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. #include <Xfe/PrimitiveP.h>
  32. #include <Xfe/ManagerP.h>
  33.  
  34. /*----------------------------------------------------------------------*/
  35. /*                                                                        */
  36. /* Private find functions                                                */
  37. /*                                                                        */
  38. /*----------------------------------------------------------------------*/
  39. static Widget    FindThisLevel    (WidgetList            children,
  40.                                  Cardinal            num_children,
  41.                                  XfeWidgetTestFunc    func,
  42.                                  int                mask,
  43.                                  XtPointer            data);
  44. /*----------------------------------------------------------------------*/
  45. static Widget    FindNextLevel    (WidgetList            children,
  46.                                  Cardinal            num_children,
  47.                                  XfeWidgetTestFunc    func,
  48.                                  int                mask,
  49.                                  Boolean            popups,
  50.                                  XtPointer            data);
  51. /*----------------------------------------------------------------------*/
  52. static Boolean    FindTestName    (Widget w,XtPointer data);
  53. static Boolean    FindTestClass    (Widget w,XtPointer data);
  54. static Boolean    FindTestWindow    (Widget w,XtPointer data);
  55. static Boolean    FindTestCoord    (Widget w,XtPointer data);
  56. /*----------------------------------------------------------------------*/
  57. #define FLAG_ALIVE(w,m)        (((m) & XfeFIND_ALIVE) ? _XfeIsAlive(w) : True)
  58. #define FLAG_REALIZED(w,m)    (((m) & XfeFIND_REALIZED) ? XtIsRealized(w) : True)
  59. #define FLAG_MANAGED(w,m)    (((m) & XfeFIND_MANAGED) ? XtIsManaged(w) : True)
  60. /*----------------------------------------------------------------------*/
  61.  
  62.  
  63.  
  64. /*----------------------------------------------------------------------*/
  65. /*                                                                        */
  66. /* Descendant find functions                                            */
  67. /*                                                                        */
  68. /*----------------------------------------------------------------------*/
  69. /* extern */ Widget
  70. XfeDescendantFindByFunction(Widget                w,
  71.                             XfeWidgetTestFunc    func,
  72.                             int                    mask,
  73.                             Boolean                popups,
  74.                             XtPointer            data)
  75. {
  76.     assert( w != NULL );
  77.     assert( func != NULL );
  78.  
  79.     if (!w || !func)
  80.     {
  81.         return NULL;
  82.     }
  83.  
  84.     /* Look at the normal children */
  85.     if (XtIsComposite(w))
  86.     {
  87.         WidgetList    children = _XfemChildren(w);
  88.         Cardinal    num_children = _XfemNumChildren(w);
  89.         Widget        test;
  90.  
  91.         test = FindThisLevel(children,num_children,func,mask,data);
  92.  
  93.         if (!test)
  94.         {
  95.             test = FindNextLevel(children,num_children,func,mask,popups,data);
  96.         }
  97.  
  98.         if (test)
  99.         {
  100.             return test;
  101.         }
  102.     }
  103.  
  104.     /* Look at the popup children */
  105.     if (XtIsWidget(w) && popups)
  106.     {
  107.         WidgetList    children = _XfePopupList(w);
  108.         Cardinal    num_children = _XfeNumPopups(w);
  109.         Widget        test;
  110.  
  111.         test = FindThisLevel(children,num_children,func,mask,data);
  112.  
  113.         if (!test)
  114.         {
  115.             test = FindNextLevel(children,num_children,func,mask,popups,data);
  116.         }
  117.  
  118.         if (test)
  119.         {
  120.             return test;
  121.         }
  122.     }
  123.  
  124.     return NULL;
  125. }
  126. /*----------------------------------------------------------------------*/
  127. /* extern */ Widget
  128. XfeDescendantFindByName(Widget w,String name,int mask,Boolean popups)
  129. {
  130.     return XfeDescendantFindByFunction(w,
  131.                                        FindTestName,
  132.                                        mask,
  133.                                        popups,
  134.                                        (XtPointer) name);
  135. }
  136. /*----------------------------------------------------------------------*/
  137. /* extern */ Widget
  138. XfeDescendantFindByClass(Widget w,WidgetClass wc,int mask,Boolean popups)
  139. {
  140.     return XfeDescendantFindByFunction(w,
  141.                                        FindTestClass,
  142.                                        mask,
  143.                                        popups,
  144.                                        (XtPointer) wc);
  145. }
  146. /*----------------------------------------------------------------------*/
  147. /* extern */ Widget
  148. XfeDescendantFindByWindow(Widget w,Window window,int mask,Boolean popups)
  149. {
  150.     return XfeDescendantFindByFunction(w,
  151.                                        FindTestWindow,
  152.                                        mask & XfeFIND_REALIZED,
  153.                                        popups,
  154.                                        (XtPointer) window);
  155. }
  156. /*----------------------------------------------------------------------*/
  157. /* extern */ Widget
  158. XfeDescendantFindByCoordinates(Widget w,Position x,Position y)
  159. {
  160.     XPoint p;
  161.  
  162.     p.x = x;
  163.     p.y = y;
  164.  
  165.     return XfeDescendantFindByFunction(w,FindTestCoord,XfeFIND_ALL,
  166.                                        False,(XtPointer) &p);
  167. }
  168. /*----------------------------------------------------------------------*/
  169. /* extern */ Widget
  170. XfeChildFindByIndex(Widget w,Cardinal i)
  171. {
  172.     Widget result = NULL;
  173.  
  174.     assert( _XfeIsAlive(w) );
  175.     assert( XtIsComposite(w) );
  176.  
  177.     if ((i < _XfemNumChildren(w)) && _XfemChildren(w))
  178.     {
  179.         result = _XfemChildren(w)[i];
  180.     }
  181.     
  182.     return result;
  183. }
  184. /*----------------------------------------------------------------------*/
  185.  
  186. /*----------------------------------------------------------------------*/
  187. /*                                                                        */
  188. /* Find ancestor functions.                                                */
  189. /*                                                                        */
  190. /*----------------------------------------------------------------------*/
  191. /* extern */ Widget
  192. XfeAncestorFindByFunction(Widget                w,
  193.                           XfeWidgetTestFunc        func,
  194.                           int                    mask,
  195.                           XtPointer                data)
  196. {
  197.     Widget test = w;
  198.  
  199.     assert( w != NULL );
  200.     assert( func != NULL );
  201.  
  202.     if (!w || !func)
  203.     {
  204.         return NULL;
  205.     }
  206.  
  207.     while(test)
  208.     {
  209.         Boolean flag_alive = FLAG_ALIVE(test,mask);
  210.         Boolean flag_realized = (flag_alive && FLAG_REALIZED(test,mask));
  211.         Boolean flag_managed = (flag_realized && FLAG_MANAGED(test,mask));
  212.  
  213.         if (flag_alive && 
  214.             flag_realized && 
  215.             flag_managed && 
  216.             (*func)(test,data))
  217.         {
  218.             return test;
  219.         }
  220.         
  221.         test = _XfeParent(test);
  222.     }
  223.     
  224.     return NULL;
  225. }
  226. /*----------------------------------------------------------------------*/
  227. /* extern */ Widget
  228. XfeAncestorFindByName(Widget w,String name,int mask)
  229. {
  230.     return XfeAncestorFindByFunction(w,
  231.                                      FindTestName,
  232.                                      mask,
  233.                                      (XtPointer) name);
  234. }
  235. /*----------------------------------------------------------------------*/
  236. /* extern */ Widget
  237. XfeAncestorFindByClass(Widget w,WidgetClass wc,int mask)
  238. {
  239.     return XfeAncestorFindByFunction(w,
  240.                                      FindTestClass,
  241.                                      mask,
  242.                                      (XtPointer) wc);
  243. }
  244. /*----------------------------------------------------------------------*/
  245. /* extern */ Widget
  246. XfeAncestorFindByWindow(Widget w,Window window,int mask)
  247. {
  248.     return XfeAncestorFindByFunction(w,
  249.                                      FindTestWindow,
  250.                                      mask & XfeFIND_REALIZED,
  251.                                      (XtPointer) window);
  252. }
  253. /*----------------------------------------------------------------------*/
  254. /* extern */ Widget
  255. XfeAncestorFindTopLevelShell(Widget w)
  256. {
  257.     return XfeAncestorFindByClass(w,topLevelShellWidgetClass,XfeFIND_ANY);
  258. }
  259. /*----------------------------------------------------------------------*/
  260. /* extern */ Widget
  261. XfeAncestorFindApplicationShell(Widget w)
  262. {
  263.     return XfeAncestorFindByClass(w,applicationShellWidgetClass,XfeFIND_ANY);
  264. }
  265. /*----------------------------------------------------------------------*/
  266.  
  267. /*----------------------------------------------------------------------*/
  268. /*                                                                        */
  269. /* Private find functions                                                */
  270. /*                                                                        */
  271. /*----------------------------------------------------------------------*/
  272. static Widget
  273. FindThisLevel(WidgetList            children,
  274.               Cardinal                num_children,
  275.               XfeWidgetTestFunc        func,
  276.               int                    mask,
  277.               XtPointer                data)
  278. {
  279.     if (children && num_children)
  280.     {
  281.         Cardinal    i;
  282.         Widget *    pw;
  283.         
  284.         /* Try to find a match in the current level first */
  285.         for (i = 0,pw = children; i < num_children; i++,pw++)
  286.         {
  287.             Boolean flag_alive = False;
  288.             Boolean flag_managed = False;
  289.             Boolean flag_realized = False;
  290.  
  291.             flag_alive = FLAG_ALIVE(*pw,mask);
  292.             flag_realized = (flag_alive && FLAG_REALIZED(*pw,mask));
  293.             flag_managed = (flag_realized && FLAG_MANAGED(*pw,mask));
  294.                 
  295.             if (flag_alive && 
  296.                 flag_realized && 
  297.                 flag_managed && 
  298.                 (*func)(*pw,data))
  299.             {
  300.                 return *pw;
  301.             }
  302.         }
  303.     }
  304.  
  305.     return NULL;
  306. }
  307. /*----------------------------------------------------------------------*/
  308. static Widget
  309. FindNextLevel(WidgetList            children,
  310.               Cardinal                num_children,
  311.               XfeWidgetTestFunc        func,
  312.               int                    mask,
  313.               Boolean                popups,
  314.               XtPointer                data)
  315. {
  316.     if (children && num_children)
  317.     {
  318.         Cardinal    i;
  319.         Widget *    pw;
  320.         Widget        test;
  321.         
  322.         /* Recurse into the next level */
  323.         for(i = 0,pw = children; i < num_children; i++,pw++)
  324.         {
  325.             test = XfeDescendantFindByFunction(*pw,func,mask,popups,data);
  326.  
  327.             if (test)
  328.             {
  329.                 return test;
  330.             }
  331.         }
  332.     }
  333.  
  334.     return NULL;
  335. }
  336. /*----------------------------------------------------------------------*/
  337. static Boolean
  338. FindTestName(Widget w,XtPointer data)
  339. {
  340.     String name = (String) data;
  341.  
  342.     if (w && name && *name && (strcmp(XtName(w),name) == 0))
  343.     {
  344.         return True;
  345.     }
  346.  
  347.     return False;
  348. }
  349. /*----------------------------------------------------------------------*/
  350. static Boolean
  351. FindTestClass(Widget w,XtPointer data)
  352. {
  353.     WidgetClass wc = (WidgetClass) data;
  354.  
  355.     if (w && wc && XtIsSubclass(w,wc))
  356.     {
  357.         return True;
  358.     }
  359.  
  360.     return False;
  361. }
  362. /*----------------------------------------------------------------------*/
  363. static Boolean
  364. FindTestWindow(Widget w,XtPointer data)
  365. {
  366.     Window window = (Window) data;
  367.  
  368.     if (w && window && (_XfeWindow(w) == window))
  369.     {
  370.         return True;
  371.     }
  372.  
  373.     return False;
  374. }
  375. /*----------------------------------------------------------------------*/
  376. static Boolean
  377. FindTestCoord(Widget w,XtPointer data)
  378. {
  379.     XPoint * p = (XPoint *) data;
  380.  
  381.     if ((p->x >= _XfeX(w)) && 
  382.         (p->x <= _XfeX(w) + _XfeWidth(w)) &&
  383.         (p->y >= _XfeY(w)) && 
  384.         (p->y <= _XfeY(w) + _XfeHeight(w)))
  385.     {
  386.         return True;
  387.     }
  388.  
  389.     return False;
  390. }
  391. /*----------------------------------------------------------------------*/
  392.  
  393.