home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / ABAddresseeView.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  14.0 KB  |  571 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.    ABAddresseeView.cpp -- class definition for XFE_AddresseeView
  20.    Created: Tao Cheng <tao@netscape.com>, 20-nov-96
  21.  */
  22.  
  23. #include "ABAddresseeView.h"
  24.  
  25. #include <Xm/Form.h>
  26. #include <Xm/PushB.h>
  27.  
  28. #include "icondata.h"
  29.  
  30. #include "xpgetstr.h"
  31.  
  32. extern int XFE_AB_STATE;
  33. extern int XFE_AB_HEADER_NAME;
  34. extern int XFE_AB_PICKER_TO;
  35. extern int XFE_AB_PICKER_CC;
  36. extern int XFE_AB_PICKER_BCC;
  37.  
  38. #ifndef AB_MAX_STRLEN
  39. #define AB_MAX_STRLEN 1024
  40. #endif
  41.  
  42. #define OUTLINER_GEOMETRY_PREF "addressbook.addresseepane.outliner_geometry"
  43.  
  44. // icons
  45. fe_icon XFE_AddresseeView::m_personIcon = { 0 };
  46. fe_icon XFE_AddresseeView::m_listIcon = { 0 };
  47.  
  48. XFE_AddresseeView::XFE_AddresseeView(XFE_Component *toplevel_component,
  49.                      Widget parent /* the form */,
  50.                      XFE_View *parent_view, MWContext *context
  51.                      ):
  52.   XFE_View(toplevel_component, parent_view, context),
  53.   m_pair(0),
  54.   m_pairs(0),
  55.   m_numPairs(0)
  56. {
  57.   m_okBtn = NULL;
  58.  
  59.   int ac;
  60.   Arg av[20];
  61.  
  62.   /* Create Type-in Filter Box
  63.    */
  64.   /* remove button
  65.    */
  66.   Widget cmdForm;
  67.  
  68.   cmdForm = XtVaCreateManagedWidget("cmdForm",
  69.                                      xmFormWidgetClass,
  70.                                      parent,
  71.                                      XmNleftAttachment, XmATTACH_FORM,
  72.                                      XmNtopAttachment, XmATTACH_NONE,
  73.                                      XmNrightAttachment, XmATTACH_NONE,
  74.                                      XmNbottomAttachment, XmATTACH_FORM,
  75.                                      NULL);
  76.   
  77.   Widget dummy = 
  78.       XtVaCreateManagedWidget("dummyRowcol", 
  79.                               xmRowColumnWidgetClass, cmdForm,
  80.                               XmNorientation, XmHORIZONTAL,
  81.                               XmNleftAttachment, XmATTACH_FORM,
  82.                               XmNtopAttachment, XmATTACH_FORM,
  83.                               XmNrightAttachment, XmATTACH_FORM,
  84.                               XmNbottomAttachment, XmATTACH_FORM,
  85.                               NULL);
  86.   ac = 0;
  87.   XtSetArg(av[ac], XtNsensitive, False), ac++;
  88.   m_removeBtn = XmCreatePushButton(dummy, 
  89.                                    "removeBtn", 
  90.                                    av, ac);
  91.   XtManageChild(m_removeBtn);
  92.  
  93.   /* For outliner
  94.    */
  95.   Widget listForm;
  96.   listForm = XtVaCreateManagedWidget("listForm",
  97.                                      xmFormWidgetClass,
  98.                                      parent,
  99.                                      XmNleftAttachment, XmATTACH_FORM,
  100.                                      XmNtopAttachment, XmATTACH_FORM,
  101.                                      XmNrightAttachment, XmATTACH_FORM,
  102.                                      XmNbottomAttachment, XmATTACH_WIDGET,
  103.                                      XmNbottomWidget, cmdForm,
  104.                                      NULL);
  105.   int num_columns = OUTLINER_COLUMN_LAST;
  106.   static int column_widths[] = {5, 45};
  107.   int count = 5;
  108.   m_outliner = new XFE_Outliner("addresseeList",
  109.                                 this,
  110.                                 toplevel_component,
  111.                                 listForm,
  112.                                 False, // constantSize
  113.                                 False,  // hasHeadings
  114.                                 num_columns,
  115.                                 num_columns,
  116.                                 column_widths,
  117.                                 OUTLINER_GEOMETRY_PREF);
  118.   /* BEGIN_3P: XmLGrid
  119.    */ 
  120.   XtVaSetValues(m_outliner->getBaseWidget(),
  121.                 XtVaTypedArg, XmNblankBackground, XmRString, "white", 6,
  122.                 XmNselectionPolicy, XmSELECT_MULTIPLE_ROW,
  123.                 XmNvisibleRows, count,
  124.                 NULL);
  125.   XtVaSetValues(m_outliner->getBaseWidget(),
  126.                 XmNcellDefaults, True,
  127.                 XtVaTypedArg, XmNcellBackground, XmRString, "white", 6,    
  128.                 NULL);
  129.   /* END_3P: XmLGrid
  130.    */
  131.  
  132.   /* 
  133.    * XFE_Outliner constructor does not allocate any content row
  134.    * XFE_Outliner::change(int first, int length, int newnumrows)
  135.    */
  136.   // m_outliner->change(0, count, count);
  137.   m_outliner->show();
  138.   setBaseWidget(m_outliner->getBaseWidget());
  139.  
  140.   /*
  141.    * Provide callbacks as for entries outside world
  142.    */
  143.   XtAddCallback(m_removeBtn, 
  144.         XmNactivateCallback, 
  145.         XFE_AddresseeView::removeCallback, 
  146.         this);
  147.  
  148.   XtAddCallback(m_outliner->getBaseWidget(), 
  149.         XmNselectCallback, 
  150.         XFE_AddresseeView::selectCallback, 
  151.         this);
  152.  
  153.  
  154.   /* register interest
  155.    */
  156.   getToplevel()->registerInterest(XFE_View::chromeNeedsUpdating,
  157.                                   (XFE_NotificationCenter *) this,
  158.                                   (XFE_FunctionNotification) updateCommands_cb);
  159.  
  160.   XtVaSetValues(m_outliner->getBaseWidget(),
  161.           XmNleftAttachment, XmATTACH_FORM,
  162.         XmNtopAttachment, XmATTACH_FORM,
  163.         XmNrightAttachment, XmATTACH_FORM,
  164.         XmNbottomAttachment, XmATTACH_WIDGET,
  165.         XmNbottomWidget, dummy,
  166.         NULL);
  167.  
  168.   // initialize the icons if they haven't already been
  169.   {
  170.     Pixel bg_pixel;
  171.     
  172.     XtVaGetValues(m_outliner->getBaseWidget(), XmNbackground, &bg_pixel, 0);
  173.     if (!m_personIcon.pixmap)
  174.       fe_NewMakeIcon(getToplevel()->getBaseWidget(),
  175.              /* umm. fix me
  176.               */
  177.              BlackPixelOfScreen(XtScreen(m_outliner->getBaseWidget())),
  178.              bg_pixel,
  179.              &m_personIcon,
  180.              NULL, 
  181.              MN_Person.width, 
  182.              MN_Person.height,
  183.              MN_Person.mono_bits, 
  184.              MN_Person.color_bits, 
  185.              MN_Person.mask_bits, 
  186.              FALSE);
  187.  
  188.     if (!m_listIcon.pixmap)
  189.       fe_NewMakeIcon(getToplevel()->getBaseWidget(),
  190.              /* umm. fix me
  191.               */
  192.              BlackPixelOfScreen(XtScreen(m_outliner->getBaseWidget())),
  193.              bg_pixel,
  194.              &m_listIcon,
  195.              NULL, 
  196.              MN_People.width, 
  197.              MN_People.height,
  198.              MN_People.mono_bits, 
  199.              MN_People.color_bits, 
  200.              MN_People.mask_bits, 
  201.              FALSE);
  202.  
  203.   }
  204. }/* XFE_AddresseeView::XFE_AddresseeView */
  205.  
  206. XFE_AddresseeView::~XFE_AddresseeView()
  207. {
  208.     getToplevel()->unregisterInterest(XFE_View::chromeNeedsUpdating,
  209.                                       (XFE_NotificationCenter *)this,
  210.                                       (XFE_FunctionNotification)updateCommands_cb);
  211.  
  212. }
  213.  
  214. void XFE_AddresseeView::setOKBtn(Widget w)
  215. {
  216.     m_okBtn = w;
  217. }
  218.  
  219. XFE_CALLBACK_DEFN(XFE_AddresseeView, updateCommands)(XFE_NotificationCenter*,
  220.                                                       void * /* clientData */, 
  221.                                                       void * /* callData */)
  222. {
  223.  
  224.  
  225.     /* check which is selected
  226.      */
  227.     int count = 0; // = XmLGridGetSelectedRowCount(w);
  228.     const int *indices = 0;
  229.     m_outliner->getSelection(&indices, &count);
  230.     if (m_numPairs > 0) {
  231.         if (m_okBtn)
  232.             XtSetSensitive(m_okBtn, True);        
  233.         
  234.         if (count > 0 && 
  235.             indices) {
  236.             XtSetSensitive(m_removeBtn, True);        
  237.         }/* if */
  238.         else
  239.             XtSetSensitive(m_removeBtn, False);    
  240.     }/* if */
  241.     else {
  242.         if (m_okBtn)
  243.             XtSetSensitive(m_okBtn, False);    
  244.         XtSetSensitive(m_removeBtn, False);    
  245.     }/* else */    
  246. }
  247.  
  248. void XFE_AddresseeView::addEntry(StatusID_t* pair)
  249. {
  250.   if (!m_pairs)
  251.     m_pairs = (StatusID_t**) calloc(1, 
  252.                     sizeof(StatusID_t*));
  253.   else
  254.     m_pairs = (StatusID_t**) realloc(m_pairs, 
  255.                      (m_numPairs+1)*sizeof(StatusID_t*));
  256.  
  257.   m_pairs[m_numPairs++] = pair;
  258.   m_outliner->change(0, m_numPairs, m_numPairs);
  259.   if (m_numPairs)
  260.       m_outliner->makeVisible(m_numPairs-1);
  261.  
  262.   getToplevel()->notifyInterested(XFE_View::chromeNeedsUpdating);
  263. }
  264.  
  265. /* Methods for the outlinable interface.
  266.  */
  267.  
  268. char *XFE_AddresseeView::getCellTipString(int row, int column)
  269. {
  270.     char *tmp = 0;
  271.     static char tipstr[128];
  272.     tipstr[0] = '\0';
  273.  
  274.     if (row < 0) {
  275.         /* header
  276.          */
  277.         tmp = getColumnHeaderText(column);
  278.     }/* if */
  279.     else {
  280.         if (row >= m_numPairs)
  281.             return 0;
  282.  
  283.         StatusID_t *pair = m_pair;
  284.         m_pair = m_pairs[row];
  285.  
  286.         /* content 
  287.          */
  288.         tmp = getColumnText(column);
  289.  
  290.         /* reset
  291.          */
  292.         m_pair = pair;
  293.     }/* else */
  294.     if (tmp && 
  295.         (!m_outliner->isColTextFit(tmp, row, column)))
  296.         XP_STRCPY(tipstr,tmp);
  297.     return tipstr;
  298. }
  299.  
  300. char *XFE_AddresseeView::getCellDocString(int /* row */, int /* column */)
  301. {
  302.     return NULL;
  303. }
  304. // Converts between an index and some non positional data type.
  305. // Used to maintain selection when the list is reordered.
  306. void *
  307. XFE_AddresseeView::ConvFromIndex(int /*index*/)
  308. {
  309.   return 0;
  310. }
  311.  
  312. // Converts between an index and some non positional data type.
  313. // Used to maintain selection when the list is reordered.
  314. int
  315. XFE_AddresseeView::ConvToIndex(void */*item*/)
  316. {
  317.   return 0;
  318. }
  319.  
  320. /* This method acquires one line of data: entryID is set for getColumnText
  321.  */
  322. void*
  323. XFE_AddresseeView::acquireLineData(int line)
  324. {
  325.   /* Test if 
  326.    * 1. any pair has been added into addressee view;
  327.    * 2. line # falls into the right range
  328.    */
  329.   if (!m_pairs || 
  330.       (line >= m_numPairs))
  331.     return 0;
  332.  
  333.   m_pair = m_pairs[line];
  334.   return m_pair;
  335. }
  336.  
  337. /* The Outlinable interface.
  338.  */
  339. /* Returns the text and/or icon to display at the top of the column.
  340.  */
  341. char*
  342. XFE_AddresseeView::getColumnName(int column)
  343. {
  344.   switch (column) {
  345.   case OUTLINER_COLUMN_STATUS:
  346.       return XP_GetString(XFE_AB_STATE);
  347.   case OUTLINER_COLUMN_NAME:
  348.       return XP_GetString(XFE_AB_HEADER_NAME);
  349.   }/* switch () */
  350.   return "unDefined";
  351. }
  352.  
  353. char*
  354. XFE_AddresseeView::getColumnHeaderText(int /* column */)
  355. {
  356.   return 0;
  357. }
  358.  
  359. // Returns the text and/or icon to display at the top of the column.
  360. fe_icon*
  361. XFE_AddresseeView::getColumnHeaderIcon(int /*column*/)
  362. {
  363.   return 0;
  364. }
  365.  
  366. // Returns the text and/or icon to display at the top of the column.
  367. EOutlinerTextStyle 
  368. XFE_AddresseeView::getColumnHeaderStyle(int /*column*/)
  369. {
  370.   return OUTLINER_Default;
  371. }
  372.  
  373. /*
  374.  * The following 4 requests deal with the currently acquired line.
  375.  */
  376. EOutlinerTextStyle 
  377. XFE_AddresseeView::getColumnStyle(int /*column*/)
  378. {
  379.   /* To be refined
  380.    */
  381.   return OUTLINER_Default;
  382. }
  383.  
  384. char*
  385. XFE_AddresseeView::getColumnText(int column)
  386. {
  387.   static char a_line[AB_MAX_STRLEN];
  388.   XP_STRCPY(a_line, "");
  389.   switch (column) {
  390.   case OUTLINER_COLUMN_STATUS:
  391.     if (m_pair->status == TO)
  392.         XP_SAFE_SPRINTF(a_line, sizeof(a_line), "%s",
  393.                         XP_GetString(XFE_AB_PICKER_TO));
  394.     else if (m_pair->status == CC)
  395.          XP_SAFE_SPRINTF(a_line, sizeof(a_line), "%s",
  396.                         XP_GetString(XFE_AB_PICKER_CC));
  397.     else if (m_pair->status == BCC)
  398.          XP_SAFE_SPRINTF(a_line, sizeof(a_line), "%s",
  399.                         XP_GetString(XFE_AB_PICKER_BCC));
  400.       
  401.     break;
  402. #if 0
  403.   case OUTLINER_COLUMN_TYPE:
  404.     break;
  405. #endif
  406.   case OUTLINER_COLUMN_NAME:
  407.     XP_STRCPY(a_line, m_pair->dplyStr);
  408.  
  409.     break;
  410.  
  411.   }/* switch () */
  412.   return a_line;
  413. }
  414.  
  415. fe_icon*
  416. XFE_AddresseeView::getColumnIcon(int /* column */)
  417. {
  418.   fe_icon *myIcon = 0;
  419. #if US_WEST_01
  420.   switch (column) {
  421.   case OUTLINER_COLUMN_TYPE:
  422.       if (m_pair) {
  423.           if (m_pair->type == ABTypePerson)
  424.               myIcon = &m_personIcon; /* shall call make/initialize icons */
  425.           else if (m_pair->type == ABTypeList)
  426.               myIcon = &m_listIcon;
  427.       }/* if */
  428.       break;
  429.   }/* switch () */
  430. #endif
  431.   return myIcon;
  432. }
  433.  
  434. //
  435. void
  436. XFE_AddresseeView::getTreeInfo(XP_Bool */*expandable*/,
  437.                    XP_Bool */*is_expanded*/, 
  438.                    int *depth, 
  439.                    OutlinerAncestorInfo **/*ancestor*/)
  440. {
  441.   depth = 0;
  442. }
  443.  
  444. //
  445. void 
  446. XFE_AddresseeView::Buttonfunc(const OutlineButtonFuncData *data)
  447. {
  448.     int row = data->row, 
  449.         clicks = data->clicks;
  450.  
  451.     if (row < 0) {
  452.         return;
  453.     } 
  454.     else {
  455.         /* content row 
  456.          */
  457.         ABID entry = m_pairs[row]->id;
  458.     
  459.         if (entry == MSG_VIEWINDEXNONE) 
  460.             return;
  461.  
  462.         if (clicks == 2) {
  463.             m_outliner->selectItemExclusive(data->row);
  464.         }/* clicks == 2 */
  465.         else if (clicks == 1) {
  466.             if (data->shift) {
  467.                 // select the range.
  468.                 const int *selected;
  469.                 int count;
  470.   
  471.                 m_outliner->getSelection(&selected, &count);
  472.                 
  473.                 if (count == 0) { /* there wasn't anything selected yet. */
  474.       
  475.                     m_outliner->selectItemExclusive(data->row);
  476.                 }/* if count == 0 */
  477.                 else if (count == 1) {
  478.                     /* there was only one, so we select the range from
  479.                        that item to the new one. */
  480.                     
  481.                     m_outliner->selectRangeByIndices(selected[0], 
  482.                                                      data->row);
  483.                 }/* count == 1 */
  484.                 else {
  485.                     /* we had a range of items selected, 
  486.                      * so let's do something really
  487.                      * nice with them. */
  488.       
  489.                     m_outliner->trimOrExpandSelection(data->row);
  490.                 }/* else */
  491.             }/* if */
  492.             else {
  493.                 m_outliner->selectItemExclusive(data->row);
  494.             }/* else */
  495.             
  496.             getToplevel()->notifyInterested(XFE_View::chromeNeedsUpdating);
  497.         }/* clicks == 1 */
  498.     }/* else */
  499.     
  500. }/* XFE_AddresseeView::Buttonfunc() */
  501.  
  502. void 
  503. XFE_AddresseeView::Flippyfunc(const OutlineFlippyFuncData */*data*/)
  504. {
  505. }
  506.  
  507. /* Tells the Outlinable object that the line data is no
  508.  * longer needed, and it can free any storage associated with it.
  509.  */ 
  510. void
  511. XFE_AddresseeView::releaseLineData()
  512. {
  513. }
  514.  
  515. void
  516. XFE_AddresseeView::removeCallback(Widget w, 
  517.                  XtPointer clientData, 
  518.                  XtPointer callData)
  519. {
  520.   XFE_AddresseeView *obj = (XFE_AddresseeView *) clientData;
  521.   obj->removeCB(w, callData);
  522. }
  523.  
  524. void
  525. XFE_AddresseeView::removeCB(Widget /* w */, 
  526.                XtPointer /* callData */)
  527. {
  528.   /* Get selection
  529.    */
  530.   /* check which is selected
  531.    */
  532.   int count = 0; // = XmLGridGetSelectedRowCount(w);
  533.   const int *indices = 0;
  534.   m_outliner->getSelection(&indices, &count);
  535.   if (count > 0 && indices && m_numPairs) {
  536.     StatusID_t **tmp = (StatusID_t **) XP_CALLOC(m_numPairs-1, 
  537.                          sizeof(StatusID_t*));
  538.     int j = 0;
  539.     for (int i=0; i < m_numPairs; i++)
  540.       if (i != indices[0]) 
  541.     tmp[j++] = m_pairs[i];
  542.       else
  543.     XP_FREE((StatusID_t *) m_pairs[i]);
  544.     XP_FREE((StatusID_t **) m_pairs);
  545.     m_pairs = tmp;
  546.     m_numPairs--;
  547.     m_outliner->change(0, m_numPairs, m_numPairs);
  548.     getToplevel()->notifyInterested(XFE_View::chromeNeedsUpdating);
  549.   }/* if */
  550.  
  551. }/* XFE_AddresseeView::removeCB() */
  552.  
  553. void
  554. XFE_AddresseeView::selectCallback(Widget w, 
  555.                  XtPointer clientData, 
  556.                  XtPointer callData)
  557. {
  558.   XFE_AddresseeView *obj = (XFE_AddresseeView *) clientData;
  559.   obj->selectCB(w, callData);
  560. }
  561.  
  562. void
  563. XFE_AddresseeView::selectCB(Widget /*w*/,
  564.                XtPointer /* callData */)
  565. {
  566.   int count = 0; // = XmLGridGetSelectedRowCount(w);
  567.   const int *indices = 0;
  568.   m_outliner->getSelection(&indices, &count);
  569. }/* XFE_AddresseeView::selectCB() */
  570.  
  571.