home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / RadioGroup.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  5.3 KB  |  233 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.    RadioGroup.cpp -- Radio groups that don't have to share the same row column parent.
  20.    Created: Chris Toshok <toshok@netscape.com>, 28-Sep-96.
  21.  */
  22.  
  23.  
  24.  
  25. #include "xp_core.h"
  26. #include "xpassert.h"
  27. #include "xp_str.h"
  28. #include "xp_mem.h"
  29. #include "xp_mcom.h"  // For XP_STRDUP(). 
  30. #include "RadioGroup.h"
  31.  
  32. #include <Xm/ToggleB.h>
  33. #include <Xm/ToggleBG.h>
  34.  
  35. #if DEBUG_slamm
  36. #define D(x) x
  37. #else
  38. #define D(x)
  39. #endif
  40.  
  41. XP_List *XFE_RadioGroup::groups = NULL;
  42.  
  43. XFE_RadioGroup::XFE_RadioGroup(char *name, XFE_Frame *frame)
  44. {
  45.   m_children = XP_ListNew();
  46.   m_name = XP_STRDUP(name);
  47.   m_frame = frame;
  48.   m_incallback = False;
  49.  
  50.   registerGroup(this);
  51. }
  52.  
  53. XFE_RadioGroup::~XFE_RadioGroup()
  54. {
  55.   XP_ListDestroy(m_children);
  56.   XP_FREE(m_name);
  57.   unregisterGroup(this);
  58. }
  59.  
  60. char *
  61. XFE_RadioGroup::getName()
  62. {
  63.   return m_name;
  64. }
  65.  
  66. XFE_Frame *
  67. XFE_RadioGroup::getFrame()
  68. {
  69.   return m_frame;
  70. }
  71.  
  72. void
  73. XFE_RadioGroup::addChild(Widget w)
  74. {
  75.   XP_ListAddObject(m_children, w);
  76.  
  77.   if (XmIsToggleButton(w) || XmIsToggleButtonGadget(w))
  78.     {
  79.       XtVaSetValues(w, 
  80.                     XmNindicatorType, XmONE_OF_MANY,
  81.                     NULL);
  82.  
  83.       XtAddCallback(w, XmNvalueChangedCallback, toggle_cb, this);
  84.     }
  85. }
  86.  
  87. void
  88. XFE_RadioGroup::removeChild(Widget w)
  89. {
  90.   XP_ListRemoveObject(m_children, w);
  91.  
  92.   if (XmIsToggleButton(w) || XmIsToggleButtonGadget(w))
  93.     XtRemoveCallback(w, XmNvalueChangedCallback, toggle_cb, this);
  94. }
  95.  
  96. XFE_RadioGroup *
  97. XFE_RadioGroup::getByName(char *name, XFE_Frame *forFrame)
  98. {
  99.   XP_List *cur;
  100.   XFE_RadioGroup *new_group;
  101.  
  102.   if (!groups)
  103.     groups = XP_ListNew();
  104.  
  105.   for (cur = groups;
  106.        cur != NULL;
  107.        cur = cur->next)
  108.     {
  109.       XFE_RadioGroup *g = (XFE_RadioGroup*)cur->object;
  110.  
  111.       if (g && 
  112.       !strcmp(name, g->getName()) &&
  113.       forFrame == g->getFrame())
  114.     {
  115.       return g;
  116.     }
  117.     }
  118.  
  119.   new_group = new XFE_RadioGroup(name, forFrame);
  120.  
  121.   registerGroup(new_group);
  122.  
  123.   return new_group;
  124. }
  125.  
  126. void
  127. XFE_RadioGroup::registerGroup(XFE_RadioGroup *group)
  128. {
  129.   XP_ListAddObject(groups, group);
  130. }
  131.  
  132. void
  133. XFE_RadioGroup::unregisterGroup(XFE_RadioGroup *group)
  134. {
  135.   XP_ListRemoveObject(groups, group);
  136. }
  137.  
  138. void
  139. XFE_RadioGroup::toggle(Widget w)
  140. {
  141.   XP_List *cur;
  142.  
  143.   if (m_incallback)
  144.     {
  145.       // keep from doing all this over and over again.
  146.       // we can't set the callCallbacks parameter to XmToggleButtonSetStatus
  147.       // to false because we also want other entities out there to know
  148.       // what's happening.
  149.       return;
  150.     }
  151.  
  152.   m_incallback = True;
  153.   m_callbackwidget = w;
  154.  
  155.   D( printf ("XFE_RadioGroup::toggle: Untoggling all except: %s\n",
  156.              getLabel(w)); )
  157.  
  158.   for (cur = m_children;
  159.        cur != NULL;
  160.        cur = cur->next)
  161.     {
  162.       Widget cur_w = (Widget)cur->object;
  163.  
  164.       if (cur_w && cur_w != w)
  165.         {
  166.           if (XmIsToggleButton(cur_w))
  167.             {
  168.               XmToggleButtonSetState(cur_w, False, True);
  169.             }
  170.           else if (XmIsToggleButtonGadget(cur_w))
  171.             {
  172.               XmToggleButtonGadgetSetState(cur_w, False, True);
  173.             }
  174.         }
  175.     }
  176.   m_incallback = False;
  177. }
  178.  
  179. void
  180. XFE_RadioGroup::toggle_cb(Widget w, XtPointer clientData, XtPointer callData)
  181. {
  182.   XFE_RadioGroup *obj = (XFE_RadioGroup*)clientData;
  183.   XmToggleButtonCallbackStruct *cbs = (XmToggleButtonCallbackStruct*)callData;
  184.  
  185.   if (cbs->set)
  186.     obj->toggle(w);
  187.   else if (obj->m_incallback == False)
  188.     {
  189.       // don't know if we always want this to happen, but
  190.       // for now, always have one toggle set (or more to the
  191.       // point, don't let them unset a toggle.)
  192.       if (XmIsToggleButton(w))
  193.         {
  194.           D( printf ("Resetting toggle button: %s\n", obj->getLabel(w)); )
  195.           XmToggleButtonSetState(w, True, False);
  196.         }
  197.       else if (XmIsToggleButtonGadget(w))
  198.         {
  199.           D( printf ("Resetting toggle button gadget: %s\n",
  200.                      obj->getLabel(w)); )
  201.           XmToggleButtonGadgetSetState(w, True, False);
  202.         }
  203.     }
  204. }
  205.  
  206. #ifdef DEBUG
  207. char *
  208. XFE_RadioGroup::getLabel(Widget w)
  209. {
  210.   XP_List *cur;
  211.  
  212.   for (cur = m_children;
  213.        cur != NULL;
  214.        cur = cur->next)
  215.     {
  216.       Widget cur_w = (Widget)cur->object;
  217.  
  218.       if (cur_w == w)
  219.         {
  220.           char *text;
  221.           XmString label;
  222.           XtVaGetValues(w,
  223.                         XmNlabelString,   &label,
  224.                         NULL);
  225.           XmStringGetLtoR(label, XmFONTLIST_DEFAULT_TAG, &text);
  226.           return text;
  227.         }
  228.     }
  229.  
  230.   return "(null)";
  231. }
  232. #endif
  233.