home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / XfeWidgets / Xfe / Geometry.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  12.0 KB  |  557 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/Geometry.c>                                        */
  21. /* Description:    Xfe widgets geometry utilities.                            */
  22. /* Author:        Ramiro Estrugo <ramiro@netscape.com>                    */
  23. /*                                                                        */
  24. /*----------------------------------------------------------------------*/
  25.  
  26.  
  27. #include <stdarg.h>
  28.  
  29. #include <Xfe/PrimitiveP.h>
  30. #include <Xfe/ManagerP.h>
  31.  
  32. #if 1
  33. #ifdef DEBUG_ramiro
  34. #define DEBUG_DIMENSIONS 1
  35. #endif
  36. #endif
  37.  
  38. /*----------------------------------------------------------------------*/
  39. /*                                                                        */
  40. /* Access functions                                                        */
  41. /*                                                                        */
  42. /*----------------------------------------------------------------------*/
  43. /* extern */ Dimension
  44. XfeBorderWidth(Widget w)
  45. {
  46.     assert( w != NULL );
  47.  
  48. #if DEBUG_DIMENSIONS
  49.     {
  50.         Dimension border_width;
  51.  
  52.         XtVaGetValues(w,XmNborderWidth,&border_width,NULL);
  53.  
  54.         assert( border_width == _XfeBorderWidth(w) );
  55.     }
  56. #endif
  57.  
  58.     return _XfeBorderWidth(w);
  59. }
  60. /*----------------------------------------------------------------------*/
  61. /* extern */ Dimension
  62. XfeWidth(Widget w)
  63. {
  64.     assert( w != NULL );
  65.  
  66. #if DEBUG_DIMENSIONS
  67.     {
  68.         Dimension width;
  69.  
  70.         XtVaGetValues(w,XmNwidth,&width,NULL);
  71.  
  72.         assert( width == _XfeWidth(w) );
  73.     }
  74. #endif
  75.     
  76.     return _XfeWidth(w);
  77. }
  78. /*----------------------------------------------------------------------*/
  79. /* extern */ Dimension
  80. XfeHeight(Widget w)
  81. {
  82.     assert( w != NULL );
  83.  
  84. #if DEBUG_DIMENSIONS
  85.     {
  86.         Dimension height;
  87.  
  88.         XtVaGetValues(w,XmNheight,&height,NULL);
  89.  
  90.         assert( height == _XfeHeight(w) );
  91.     }
  92. #endif
  93.     
  94.     return _XfeHeight(w);
  95. }
  96. /*----------------------------------------------------------------------*/
  97. /* extern */ Dimension
  98. XfeScreenWidth(Widget w)
  99. {
  100.     assert( _XfeIsAlive(w) );
  101.  
  102.     if (!_XfeIsAlive(w))
  103.     {
  104.         return 0;
  105.     }
  106.  
  107.     return DisplayWidth(XtDisplay(w),XScreenNumberOfScreen(_XfeScreen(w)));
  108. }
  109. /*----------------------------------------------------------------------*/
  110. /* extern */ Dimension
  111. XfeScreenHeight(Widget w)
  112. {
  113.     assert( _XfeIsAlive(w) );
  114.  
  115.     if (!_XfeIsAlive(w))
  116.     {
  117.         return 0;
  118.     }
  119.  
  120.     return DisplayHeight(XtDisplay(w),XScreenNumberOfScreen(_XfeScreen(w)));
  121. }
  122. /*----------------------------------------------------------------------*/
  123. /* extern */ Position
  124. XfeX(Widget w)
  125. {
  126.     assert( _XfeIsAlive(w) );
  127.  
  128.     if (!_XfeIsAlive(w))
  129.     {
  130.         return 0;
  131.     }
  132.     
  133.     return _XfeX(w);
  134. }
  135. /*----------------------------------------------------------------------*/
  136. /* extern */ Position
  137. XfeY(Widget w)
  138. {
  139.     assert( _XfeIsAlive(w) );
  140.  
  141.     if (!_XfeIsAlive(w))
  142.     {
  143.         return 0;
  144.     }
  145.     
  146.     return _XfeY(w);
  147. }
  148. /*----------------------------------------------------------------------*/
  149. /* extern */ Position
  150. XfeRootX(Widget w)
  151. {
  152.     Position root_x;
  153.  
  154.     assert( _XfeIsAlive(w) );
  155.  
  156.     if (!_XfeIsAlive(w))
  157.     {
  158.         return 0;
  159.     }
  160.  
  161.     XtTranslateCoords(w,0,0,&root_x,NULL);
  162.  
  163.     return root_x;
  164. }
  165. /*----------------------------------------------------------------------*/
  166. /* extern */ Position
  167. XfeRootY(Widget w)
  168. {
  169.     Position root_y;
  170.  
  171.     assert( _XfeIsAlive(w) );
  172.  
  173.     if (!_XfeIsAlive(w))
  174.     {
  175.         return 0;
  176.     }
  177.     
  178.     XtTranslateCoords(w,0,0,NULL,&root_y);
  179.  
  180.     return root_y;
  181. }
  182. /*----------------------------------------------------------------------*/
  183.  
  184. /*----------------------------------------------------------------------*/
  185. /*                                                                        */
  186. /* Biggest, widest and tallest functions                                */
  187. /*                                                                        */
  188. /*----------------------------------------------------------------------*/
  189. /* extern */ Widget
  190. XfeBiggestWidget(Boolean horizontal,WidgetList widgets,Cardinal n)
  191. {
  192.     Dimension        biggest_dim = 0;
  193.     Dimension        dim;
  194.     Widget            max_widget = NULL;
  195.     Widget            widget;
  196.     Cardinal        i;
  197.  
  198.     assert( widgets != NULL );
  199.     
  200.     if (!widgets)
  201.     {
  202.         return NULL;
  203.     }
  204.  
  205.     for (i = 0; i < n; i++) 
  206.     {
  207.         widget = widgets[i];
  208.  
  209.         assert( _XfeIsAlive(widget) );
  210.  
  211.         dim = (horizontal ? _XfeWidth(widget) : _XfeHeight(widget));
  212.  
  213.         if (dim > biggest_dim) 
  214.         {
  215.             biggest_dim = dim;
  216.             max_widget = widget;
  217.         }
  218.     }
  219.     
  220.     return max_widget;
  221. }
  222. /*----------------------------------------------------------------------*/
  223. /* extern */ Dimension
  224. XfeVaGetWidestWidget(Widget widget, ...)
  225. {
  226.     va_list        vargs;
  227.     Widget        current;
  228.     Dimension    width;
  229.     Dimension    widest_width;
  230.  
  231.     /*
  232.      * Get the first widget's width
  233.      * (because of the way var args works
  234.      *  we have to start the loop at the second widget)
  235.      */
  236.     widest_width = _XfeWidth(widget);
  237.  
  238.     /*
  239.      * Get the widest width
  240.      */
  241.     va_start (vargs, widget);
  242.  
  243.     while ((current = va_arg (vargs, Widget))) 
  244.     {
  245.         width = _XfeWidth(current);
  246.  
  247.         if (width > widest_width)
  248.         {
  249.             widest_width = width;
  250.         }
  251.     }
  252.  
  253.     va_end (vargs);
  254.  
  255.     return widest_width;
  256. }
  257. /*----------------------------------------------------------------------*/
  258. /* extern */ Dimension
  259. XfeVaGetTallestWidget(Widget widget, ...)
  260. {
  261.     va_list        vargs;
  262.     Widget        current;
  263.     Dimension    height;
  264.     Dimension    widest_height;
  265.  
  266.     /*
  267.      * Get the first widget's height
  268.      * (because of the way var args works
  269.      *  we have to start the loop at the second widget)
  270.      */
  271.     widest_height = _XfeHeight(widget);
  272.  
  273.     /*
  274.      * Get the widest height
  275.      */
  276.     va_start (vargs, widget);
  277.  
  278.     while ((current = va_arg (vargs, Widget))) 
  279.     {
  280.         height = _XfeHeight(current);
  281.  
  282.         if (height > widest_height)
  283.         {
  284.             widest_height = height;
  285.         }
  286.     }
  287.  
  288.     va_end (vargs);
  289.  
  290.     return widest_height;
  291. }
  292. /*----------------------------------------------------------------------*/
  293.  
  294. /*----------------------------------------------------------------------*/
  295. /*                                                                        */
  296. /* Misc                                                                    */
  297. /*                                                                        */
  298. /*----------------------------------------------------------------------*/
  299. /* extern */ Dimension
  300. _XfeHeightCenter(Widget one,Widget two)
  301. {
  302.     assert( one != NULL );
  303.     assert( _XfeIsAlive(one) );
  304.     assert( two != NULL );
  305.     assert( _XfeIsAlive(two) );
  306.  
  307.     return
  308.         (_XfeHeight(one) > _XfeHeight(two)) ? 
  309.         ((_XfeHeight(one) - _XfeHeight(two)) / 2) :
  310.         0;
  311. }
  312. /*----------------------------------------------------------------------*/
  313. /* extern */ Dimension
  314. _XfeWidthCenter(Widget one,Widget two)
  315. {
  316.     assert( one != NULL );
  317.     assert( _XfeIsAlive(one) );
  318.     assert( two != NULL );
  319.     assert( _XfeIsAlive(two) );
  320.  
  321.     return
  322.         (_XfeWidth(one) > _XfeWidth(two)) ? 
  323.         ((_XfeWidth(one) - _XfeWidth(two)) / 2) :
  324.         0;
  325. }
  326. /*----------------------------------------------------------------------*/
  327.  
  328.  
  329. /*----------------------------------------------------------------------*/
  330. /*                                                                        */
  331. /* Geometry                                                                */
  332. /*                                                                        */
  333. /*----------------------------------------------------------------------*/
  334. /* extern */ void
  335. _XfeConfigureWidget(Widget w,int x,int y,int width,int height)
  336. {
  337.     assert( _XfeIsAlive(w) );
  338.  
  339. #if 0
  340.     printf("_XfeConfigureWidget(%s,%d,%d,%d,%d)\n",XtName(w),x,y,width,height);
  341. #endif
  342.  
  343. #if 0
  344.     assert( x >= 0 );
  345.     assert( y >= 0 );
  346.  
  347.     assert( width > 0 );
  348.     assert( height > 0 );
  349. #endif
  350.  
  351.     /* Ignore this request if width or height are 0 */
  352.     if (!width || !height)
  353.     {
  354.         return;
  355.     }
  356.  
  357.     /* Make sure the positions and dimensions are different */
  358.     if ((_XfeX(w) == x) && (_XfeY(w) == y) && 
  359.         (_XfeWidth(w) == width) && (_XfeHeight(w) == height))
  360.     {
  361.         return;
  362.     }
  363.  
  364.     /* Configure XfePrimitive class */
  365.     if (XfeIsPrimitive(w))
  366.     {
  367.         Boolean use_preferred_height;
  368.         Boolean use_preferred_width;
  369.         
  370.         use_preferred_width = _XfeUsePreferredWidth(w);
  371.         use_preferred_height = _XfeUsePreferredHeight(w);
  372.         
  373.         _XfeUsePreferredWidth(w) = False;
  374.         _XfeUsePreferredHeight(w) = False;
  375.         
  376.         XtConfigureWidget(w,x,y,width,height,_XfeBorderWidth(w));
  377.         
  378.         _XfeUsePreferredWidth(w) = use_preferred_width;
  379.         _XfeUsePreferredHeight(w) = use_preferred_height;
  380.     }
  381.     /* Configure XfeManager class */
  382.     else if (XfeIsManager(w))
  383.     {
  384.         Boolean use_preferred_height;
  385.         Boolean use_preferred_width;
  386.  
  387.         _XfemOldWidth(w) = _XfeWidth(w);
  388.         _XfemOldHeight(w) = _XfeHeight(w);
  389.         
  390.         use_preferred_width = _XfemUsePreferredWidth(w);
  391.         use_preferred_height = _XfemUsePreferredHeight(w);
  392.         
  393.         _XfemUsePreferredWidth(w) = False;
  394.         _XfemUsePreferredHeight(w) = False;
  395.         
  396.         XtConfigureWidget(w,x,y,width,height,_XfeBorderWidth(w));
  397.         
  398.         _XfemUsePreferredWidth(w) = use_preferred_width;
  399.         _XfemUsePreferredHeight(w) = use_preferred_height;
  400.     }
  401.     /* Configure any other class */
  402.     else
  403.     {
  404.         _XmConfigureObject(w,x,y,width,height,_XfeBorderWidth(w));
  405.     }
  406. }
  407. /*----------------------------------------------------------------------*/
  408. /* extern */ void
  409. _XfeConfigureOrHideWidget(Widget w,int x,int y,int width,int height)
  410. {
  411.     assert( _XfeIsAlive(w) );
  412.  
  413.     /* Make sure the widget is alive */
  414.     if (!_XfeIsAlive(w))
  415.     {
  416.         return;
  417.     }
  418.  
  419.     /* Make sure the positions and dimensions are different */
  420.     if ((_XfeX(w) == x) && (_XfeY(w) == y) && 
  421.         (_XfeWidth(w) == width) && (_XfeHeight(w) == height))
  422.     {
  423.         return;
  424.     }
  425.  
  426.     /* Hide the widget if its dimensions are less thanb zero */
  427.     if (!width || !height)
  428.     {
  429.         _XfeSetMappedWhenManaged(w,False);
  430.  
  431.         return;
  432.     }
  433.  
  434.     _XfeConfigureWidget(w,x,y,width,height);
  435.  
  436.     /* Show the widget */
  437.     _XfeSetMappedWhenManaged(w,True);
  438. }
  439. /*----------------------------------------------------------------------*/
  440. /* extern */ void
  441. _XfeResizeWidget(Widget w,int width,int height)
  442. {
  443.     assert( _XfeIsAlive(w) );
  444.  
  445. #if 0
  446.     printf("_XfeResizeWidget(%s,%d,%d)\n",XtName(w),width,height);
  447. #endif
  448.  
  449. #if 0
  450.     assert( width > 0 );
  451.     assert( height > 0 );
  452. #endif
  453.  
  454.     /* Ignore this request if width or height are 0 */
  455.     if (!width || !height)
  456.     {
  457.         return;
  458.     }
  459.  
  460.     /* Make sure the dimension are different */
  461.     if ((_XfeWidth(w) == width) && (_XfeHeight(w) == height))
  462.     {
  463.         return;
  464.     }
  465.  
  466.     _XfeConfigureWidget(w,_XfeX(w),_XfeY(w),width,height);
  467. }
  468. /*----------------------------------------------------------------------*/
  469. /* extern */ void
  470. _XfeMoveWidget(Widget w,int x,int y)
  471. {
  472.     assert( _XfeIsAlive(w) );
  473.  
  474. #if 0
  475.     printf("_XfeMoveWidget(%s,%d,%d)\n",XtName(w),width,height);
  476. #endif
  477.  
  478. #if 0
  479.     assert( x >= 0 );
  480.     assert( y >= 0 );
  481. #endif
  482.  
  483.     /* Make sure the positions are different */
  484.     if ((_XfeX(w) == x) && (_XfeY(w) == y))
  485.     {
  486.         return;
  487.     }
  488.  
  489.     /* Configure XfePrimitive or XfeManager classes */
  490.     if (XfeIsPrimitive(w) || XfeIsManager(w))
  491.     {
  492.         XtMoveWidget(w,x,y);
  493.     }
  494.  
  495.     /* Configure any other class */
  496.     else
  497.     {
  498.         _XmMoveObject(w,x,y);
  499.     }
  500. }
  501. /*----------------------------------------------------------------------*/
  502. /* extern */ void
  503. _XfePreferredGeometry(Widget w,Dimension *width_out,Dimension *height_out)
  504. {
  505.     Dimension width;
  506.     Dimension height;
  507.  
  508.     assert( _XfeIsAlive(w) );
  509.  
  510.     if (XfeIsPrimitive(w))
  511.     {
  512.         width  = _XfePreferredWidth(w);
  513.         height = _XfePreferredHeight(w);
  514.     }
  515.     else if (XfeIsManager(w))
  516.     {
  517.         width  = _XfemPreferredWidth(w);
  518.         height = _XfemPreferredHeight(w);
  519.     }
  520.     else
  521.     {
  522.         XtWidgetGeometry reply;
  523.  
  524.         XtQueryGeometry(w,NULL,&reply);
  525.  
  526.         if (reply.request_mode & CWWidth)
  527.         {
  528.             width = reply.width;
  529.         }
  530.         else
  531.         {
  532.             width = _XfeWidth(w);
  533.         }
  534.  
  535.  
  536.         if (reply.request_mode & CWHeight)
  537.         {
  538.             height = reply.height;
  539.         }
  540.         else
  541.         {
  542.             height = _XfeHeight(w);
  543.         }
  544.     }
  545.  
  546.     if (width_out)
  547.     {
  548.         *width_out = width;
  549.     }
  550.  
  551.     if (height_out)
  552.     {
  553.         *height_out = height;
  554.     }
  555. }
  556. /*----------------------------------------------------------------------*/
  557.