home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / AttachmentMenu.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.8 KB  |  188 lines

  1. /* -*- Mode: C++; tab-width: 8; 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.    AttachmentMenu.cpp -- class for doing the dynamic attachment menus.
  20.    Created: Chris Toshok <toshok@netscape.com>, 3-Jan-1997.
  21.  */
  22.  
  23.  
  24.  
  25. #include "AttachmentMenu.h"
  26. #include "MsgView.h"
  27. #include "felocale.h"
  28.  
  29. XFE_AttachmentMenu::XFE_AttachmentMenu(Widget w, XFE_Frame *frame)
  30. {
  31.   m_cascade = w;
  32.   m_parentFrame = frame;
  33.  
  34.   XtAddCallback(m_cascade, XmNdestroyCallback, destroy_cb, this);
  35.   
  36.   XtVaGetValues(m_cascade,
  37.         XmNsubMenuId, &m_submenu,
  38.         NULL);
  39.  
  40.   XtVaGetValues(m_submenu,
  41.         XmNnumChildren, &m_firstslot,
  42.         NULL);
  43.  
  44.   XP_ASSERT(m_submenu);
  45.  
  46.   frame->registerInterest(XFE_MsgView::messageHasChanged,
  47.               this,
  48.               (XFE_FunctionNotification)attachmentsHaveChanged_cb);
  49.  
  50.   m_attachmentData = NULL;
  51.  
  52.   // make sure we initially install an update callback
  53.   attachmentsHaveChanged(NULL, NULL, NULL);
  54. }
  55.  
  56. XFE_AttachmentMenu::~XFE_AttachmentMenu()
  57. {
  58.   m_parentFrame->unregisterInterest(XFE_MsgView::messageHasChanged,
  59.                     this,
  60.                     (XFE_FunctionNotification)attachmentsHaveChanged_cb);
  61. }
  62.  
  63. void
  64. XFE_AttachmentMenu::generate(Widget cascade, XtPointer, XFE_Frame *frame)
  65. {
  66.   XFE_AttachmentMenu *obj;
  67.  
  68.   obj = new XFE_AttachmentMenu(cascade, frame);
  69. }
  70.  
  71. void
  72. XFE_AttachmentMenu::activate_cb(Widget widget, XtPointer cd, XtPointer)
  73. {
  74.   XFE_AttachmentMenu *obj = (XFE_AttachmentMenu*)cd;
  75.   MSG_AttachmentData *data;
  76.  
  77.   XtVaGetValues(widget, XmNuserData, &data, 0);
  78.   
  79.   XP_ASSERT(data);
  80.  
  81.   if (obj->m_parentFrame->handlesCommand(xfeCmdOpenUrl, (void*)data->url))
  82.     obj->m_parentFrame->doCommand(xfeCmdOpenUrl, (void*)data->url);
  83. }
  84.  
  85. void
  86. XFE_AttachmentMenu::add_attachment_menu_items(Widget menu)
  87. {
  88.   const MSG_AttachmentData *data;
  89.   Arg av [20];
  90.   int ac;
  91.   XmString xmname;
  92.  
  93.   if (!m_attachmentData)
  94.     return;
  95.  
  96.   for (data = m_attachmentData;
  97.        data != NULL;
  98.        data ++)
  99.     {
  100.       char buf[1000];
  101.       char *name, *description;
  102.       Widget button;
  103.  
  104.       name = data->real_name;
  105.       if (name == NULL) name = "";
  106.       
  107.       description = data->description;
  108.       if (description == NULL) description = "";
  109.  
  110.       PR_snprintf(buf, sizeof(buf), "%s (%s)", name, description);
  111.  
  112.       xmname = XmStringCreate(buf, XmFONTLIST_DEFAULT_TAG);
  113.  
  114.       ac = 0;
  115.       XtSetArg (av[ac], XmNlabelString, xmname); ac++;
  116.       XtSetArg (av[ac], XmNuserData, data); ac++;      
  117.  
  118.       button = XmCreatePushButtonGadget(menu, "openAttachment", av, ac);
  119.       
  120.       XtAddCallback(button, XmNactivateCallback, activate_cb, this);
  121.       XtManageChild(button);
  122.  
  123.       XmStringFree(xmname);
  124.     }
  125. }
  126.  
  127. void
  128. XFE_AttachmentMenu::update()
  129. {
  130.   Widget *kids;
  131.   int nkids;
  132.  
  133.   XtVaGetValues (m_submenu, XmNchildren, &kids, XmNnumChildren, &nkids, 0);
  134.  
  135.   XtUnrealizeWidget(m_submenu);
  136.  
  137.   if (nkids) 
  138.     {
  139.       kids = &(kids[m_firstslot]);
  140.       nkids -= m_firstslot;
  141.  
  142.       XtUnmanageChildren (kids, nkids);
  143.       fe_DestroyWidgetTree(kids, nkids);
  144.     }
  145.  
  146.   add_attachment_menu_items(m_submenu);
  147.  
  148.   XtRealizeWidget(m_submenu);
  149.  
  150.   XtRemoveCallback(m_cascade, XmNcascadingCallback, update_cb, this);
  151. }
  152.  
  153. void
  154. XFE_AttachmentMenu::update_cb(Widget, XtPointer clientData, XtPointer)
  155. {
  156.   XFE_AttachmentMenu *obj = (XFE_AttachmentMenu*)clientData;
  157.  
  158.   obj->update();
  159. }
  160.  
  161. void
  162. XFE_AttachmentMenu::destroy_cb(Widget, XtPointer clientData, XtPointer)
  163. {
  164.   XFE_AttachmentMenu *obj = (XFE_AttachmentMenu*)clientData;
  165.  
  166.   delete obj;
  167. }
  168.  
  169. XFE_CALLBACK_DEFN(XFE_AttachmentMenu, attachmentsHaveChanged)(XFE_NotificationCenter *,
  170.                                   void *,
  171.                                   void */*calldata*/)
  172. {
  173. #if 0
  174.   if (calldata)
  175.     m_attachmentData = MSG_GetAttachmentList((MSG_Pane*)calldata);
  176.  
  177.   // This may seem stupid, but it keeps us from having more than
  178.   // one reference to this particular callback without having
  179.   // to worry about other cascadingCallbacks.
  180.  
  181.   // remove it if it's already there
  182.   XtRemoveCallback(m_cascade, XmNcascadingCallback, update_cb, this);
  183.  
  184.   // and then add it back.
  185.   XtAddCallback(m_cascade, XmNcascadingCallback, update_cb, this);
  186. #endif
  187. }
  188.