home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / OutlinerDrop.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.7 KB  |  349 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.    OutlinerDrop.cpp -- Drag and drop for our outliners.
  20.    Created: Chris Toshok <toshok@netscape.com>, 10-Sep-97
  21.    */
  22.  
  23.  
  24.  
  25. #include "structs.h"
  26. #include "ntypes.h"
  27. #include "xfe.h"
  28. #include "OutlinerDrop.h"
  29. #include "Outliner.h"
  30. #include "Outlinable.h"
  31.  
  32. #if defined(DEBUG_toshok) || defined(DEBUG_Tao)
  33. #define D(x) printf x
  34. #else
  35. #define D(x)
  36. #endif
  37.  
  38. Atom XFE_OutlinerDrop::_XA_NETSCAPE_OUTLINER_COLUMN=None;
  39.  
  40. Atom XFE_OutlinerDrop::_XA_NETSCAPE_BOOKMARK=None;
  41.  
  42. Atom XFE_OutlinerDrop::_XA_NETSCAPE_MAIL_SERVER=None;
  43. Atom XFE_OutlinerDrop::_XA_NETSCAPE_NEWS_SERVER=None;
  44. Atom XFE_OutlinerDrop::_XA_NETSCAPE_MAIL_FOLDER=None;
  45. Atom XFE_OutlinerDrop::_XA_NETSCAPE_NEWSGROUP=None;
  46.  
  47. Atom XFE_OutlinerDrop::_XA_NETSCAPE_MAIL_MESSAGE=None;
  48. Atom XFE_OutlinerDrop::_XA_NETSCAPE_NEWS_MESSAGE=None;
  49.  
  50. Atom XFE_OutlinerDrop::_XA_NETSCAPE_PAB=None;
  51. Atom XFE_OutlinerDrop::_XA_NETSCAPE_DIRSERV=None;
  52.  
  53. static void
  54. initTargets(Widget widget)
  55. {
  56.   static int init_done = 0;
  57.  
  58.   D(("initTargets()\n"));
  59.  
  60.   if (init_done)
  61.     return;
  62.  
  63.   init_done = 1;
  64.  
  65.   XFE_OutlinerDrop::_XA_NETSCAPE_OUTLINER_COLUMN =
  66.     XmInternAtom(XtDisplay(widget), "_NETSCAPE_OUTLINER_COLUMN", FALSE);
  67.   XFE_OutlinerDrop::_XA_NETSCAPE_BOOKMARK =
  68.     XmInternAtom(XtDisplay(widget), "_NETSCAPE_BOOKMARK", FALSE);
  69.   XFE_OutlinerDrop::_XA_NETSCAPE_MAIL_SERVER =
  70.     XmInternAtom(XtDisplay(widget), "_NETSCAPE_MAIL_SERVER", FALSE);
  71.   XFE_OutlinerDrop::_XA_NETSCAPE_NEWS_SERVER =
  72.     XmInternAtom(XtDisplay(widget), "_NETSCAPE_NEWS_SERVER", FALSE);
  73.   XFE_OutlinerDrop::_XA_NETSCAPE_MAIL_FOLDER =
  74.     XmInternAtom(XtDisplay(widget), "_NETSCAPE_MAIL_FOLDER", FALSE);
  75.   XFE_OutlinerDrop::_XA_NETSCAPE_NEWSGROUP =
  76.     XmInternAtom(XtDisplay(widget), "_NETSCAPE_NEWSGROUP", FALSE);
  77.   XFE_OutlinerDrop::_XA_NETSCAPE_MAIL_MESSAGE =
  78.     XmInternAtom(XtDisplay(widget), "_NETSCAPE_MAIL_MESSAGE", FALSE);
  79.   XFE_OutlinerDrop::_XA_NETSCAPE_NEWS_MESSAGE =
  80.     XmInternAtom(XtDisplay(widget), "_NETSCAPE_NEWS_MESSAGE", FALSE);
  81.   XFE_OutlinerDrop::_XA_NETSCAPE_PAB =
  82.     XmInternAtom(XtDisplay(widget), "_XA_NETSCAPE_PAB", FALSE);
  83.   XFE_OutlinerDrop::_XA_NETSCAPE_DIRSERV =
  84.     XmInternAtom(XtDisplay(widget), "_XA_NETSCAPE_DIRSERV", FALSE);
  85. }
  86.  
  87. XFE_OutlinerDrop::XFE_OutlinerDrop(Widget parent, XFE_Outliner *outliner)
  88.   : XFE_DropNetscape(parent)
  89. {
  90.   m_outliner = outliner;
  91.  
  92.   D(("XFE_OutlinerDrop::XFE_OutlinerDrop()\n"));
  93.  
  94.   initTargets(parent);
  95. }
  96.  
  97. XFE_OutlinerDrop::~XFE_OutlinerDrop()
  98. {
  99.   D(("XFE_OutlinerDrop::~XFE_OutlinerDrop()\n"));
  100. }
  101.  
  102. void
  103. XFE_OutlinerDrop::targets()
  104. {
  105.   Atom *new_targets;
  106.   int new_numTargets;
  107.   int i;
  108.  
  109.   D(("XFE_OutlinerDrop::targets()\n"));
  110.  
  111.   m_outliner->getDropTargets(&_targets, &_numTargets);
  112.  
  113.   new_numTargets = _numTargets + 1;
  114.   new_targets = new Atom[new_numTargets];
  115.  
  116.   for (i = 0; i < _numTargets; i ++)
  117.     {
  118.       new_targets[i] = _targets[i];
  119.     }
  120.  
  121.   new_targets[new_numTargets - 1] = _XA_NETSCAPE_OUTLINER_COLUMN;
  122.  
  123.   for (i = 0; i < new_numTargets; i ++)
  124.     {
  125.       D(("  [%d] %s\n",i,XmGetAtomName(XtDisplay(_widget),new_targets[i])));
  126.     }
  127.  
  128.   if (_targets)
  129.     delete [] _targets;
  130.  
  131.   _numTargets = new_numTargets;
  132.   _targets = new_targets;
  133. }
  134.  
  135. void
  136. XFE_OutlinerDrop::operations()
  137. {
  138.   _operations = (unsigned int)XmDROP_COPY;
  139. }
  140.  
  141. int
  142. XFE_OutlinerDrop::processTargets(Atom *targets,
  143.                                  const char **data,
  144.                                  int numItems)
  145. {
  146.   int i;
  147.   int drop_column, drop_row;
  148.   unsigned char rowtype, coltype;
  149.  
  150.   D(("XFE_OutlinerDrop::processTargets()\n"));
  151.  
  152.   _sameDragSource = TRUE; /* XXX */
  153.  
  154.   for (i = 0; i<numItems; i ++)
  155.     {
  156.       if (targets[i]==None || data[i]==NULL || strlen(data[i])==0)
  157.         continue;
  158.  
  159.       D(("  [%d] %s: \"%s\"\n",i,XmGetAtomName(XtDisplay(_widget),targets[i]),data[i]));
  160.       
  161.       if (XmLGridXYToRowColumn(m_outliner->getBaseWidget(),
  162.                                _dropEventX, _dropEventY,
  163.                                &rowtype, &drop_row,
  164.                                &coltype, &drop_column) < 0)
  165.         {
  166.           D(("Not dropping column in a valid position\n"));
  167.           return FALSE;
  168.         }
  169.       
  170.       if (targets[i] == _XA_NETSCAPE_OUTLINER_COLUMN
  171.           && _sameDragSource)
  172.         {
  173.           int column = atoi(data[i]);
  174.           
  175.           if (column != -1)
  176.             {
  177.               if (drop_column != column)
  178.                 m_outliner->moveColumn(column, drop_column);
  179.               
  180.               return TRUE;
  181.             }
  182.         }
  183.     }
  184.  
  185.   return m_outliner->processTargets(drop_row, drop_column,
  186.                                     targets, data, numItems);
  187. }
  188.  
  189. void
  190. XFE_OutlinerDrop::dragIn()
  191. {
  192.   /* not much to do... */
  193.   D(("XFE_OutlinerDrop::dragIn()\n"));
  194. }
  195.  
  196. void
  197. XFE_OutlinerDrop::dragOut()
  198. {
  199.   D(("XFE_OutlinerDrop::dragOut()\n"));
  200.   m_outliner->outlineLine(-1);
  201. }
  202.  
  203. void
  204. XFE_OutlinerDrop::dragMotion()
  205. {
  206.   int row = m_outliner->XYToRow(_dropEventX, _dropEventY);
  207.  
  208.   if (row != -1) m_outliner->outlineLine(row);
  209. }
  210.  
  211.  
  212. /* XFE_OutlinerDrag */
  213.  
  214. XFE_OutlinerDrag::XFE_OutlinerDrag(Widget parent,
  215.                                    XFE_Outliner *outliner)
  216.   : XFE_DragNetscape(parent)
  217. {
  218.   m_outliner = outliner;
  219.  
  220.   initTargets(parent);
  221. }
  222.  
  223. XFE_OutlinerDrag::~XFE_OutlinerDrag()
  224. {
  225. }
  226.  
  227. void
  228. XFE_OutlinerDrag::buttonPressCb(XButtonPressedEvent *event)
  229. {
  230.   /* we pass through the button press event if it's not on one of the
  231.      grid's resize controls. */
  232.   if (!XmLGridPosIsResize(m_outliner->getBaseWidget(),
  233.                           event->x,
  234.                           event->y))
  235.     XFE_DragNetscape::buttonPressCb(event);
  236. }
  237.  
  238. fe_icon_data *
  239. XFE_OutlinerDrag::createFakeHeaderIconData(int col)
  240. {
  241.   /* XXX ?? for now rely on the outlinable to give us the right icon.
  242.      In the future we should try and do what windows does -- drag a 
  243.      representation of the column header (or perhaps the entire column. */
  244.   return m_outliner->getDragIconData(-1, col);
  245. }
  246.  
  247. /* we return FALSE if we're dragging from a row/column that doesn't exist
  248.    int the outliner (if we drag from a blank spot below any real rows.)
  249.    This short circuits the drag. */
  250. int
  251. XFE_OutlinerDrag::dragStart(int x, int y)
  252. {
  253.   int col, row;
  254.   unsigned char rowtype, coltype;
  255.   
  256.   if (XmLGridXYToRowColumn(m_outliner->getBaseWidget(),
  257.                            x, y,
  258.                            &rowtype, &row,
  259.                            &coltype, &col) < 0)
  260.     return FALSE;
  261.   else
  262.     return TRUE;
  263. }
  264.  
  265. /* XFE_OutlinerDrag::dragStart is called before this method.  So, checking the
  266.    return value of XmLGridXYToRowColumn is unnecessary -- a -1 would have short
  267.    circuited the entire process before we made it to this method. 
  268. */
  269. void
  270. XFE_OutlinerDrag::targets()
  271. {
  272.   int row, col;
  273.   unsigned char rowtype, coltype;
  274.  
  275.   D(("XFE_OutlinerDrag::targets()\n"));
  276.  
  277.   XmLGridXYToRowColumn(m_outliner->getBaseWidget(),
  278.                        _dragStartX, _dragStartY,
  279.                        &rowtype, &row,
  280.                        &coltype, &col);
  281.  
  282.   m_dragcolumn = col;
  283.   m_dragrow = row;
  284.  
  285.   /*
  286.   ** if the user drags one of the headers, we never let the outlinable know about
  287.   ** it -- we handle the entire process in this file.  The atom type is 
  288.   ** _XA_NETSCAPE_OUTLINER_COLUMN and the data is the index of the column.
  289.   ** the column's index is available in m_dragcolumn, and the row in m_dragrow.
  290.   */
  291.   if (rowtype == XmHEADING)
  292.     {
  293.       if (_targets)
  294.         delete [] _targets;
  295.  
  296.       row = -1;
  297.       setDragIcon(createFakeHeaderIconData(col));
  298.  
  299.       _numTargets = 2;
  300.       
  301.       _targets = new Atom[_numTargets];
  302.       
  303.       _targets[0] = XFE_OutlinerDrop::_XA_NETSCAPE_OUTLINER_COLUMN;
  304.     }
  305.   else
  306.     {
  307.       setDragIcon(m_outliner->getDragIconData(row, col));
  308.   
  309.       m_outliner->getDragTargets(row, col,
  310.                                  &_targets, &_numTargets);
  311.     }
  312. }
  313.  
  314. void
  315. XFE_OutlinerDrag::operations()
  316. {
  317.   _operations = (unsigned int)XmDROP_COPY | XmDROP_MOVE;
  318. }
  319.  
  320. char *
  321. XFE_OutlinerDrag::getTargetData(Atom atom)
  322. {
  323.   /* if the atom's type is _XA_NETSCAPE_OUTLINER_COLUMN, we handle
  324.      it here.  Otherwise the outliner (or outlinable) handles it. */
  325.   if (atom == XFE_OutlinerDrop::_XA_NETSCAPE_OUTLINER_COLUMN
  326.       && m_dragcolumn != -1)
  327.     {
  328.       char buf[10];
  329.       PR_snprintf(buf, 10, "%d", m_dragcolumn);
  330.       return (char*)XtNewString(buf);
  331.     }
  332.   else
  333.     {
  334.         /* TJC: added "return"; was missing??
  335.          */
  336.         return m_outliner->dragConvert(atom);
  337.     }
  338. }
  339.  
  340. void
  341. XFE_OutlinerDrag::deleteTarget()
  342. {
  343. }
  344.  
  345. void
  346. XFE_OutlinerDrag::dragComplete()
  347. {
  348. }
  349.