home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / Command.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  13.2 KB  |  627 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.    Command.cpp -- command goop
  20.    Created: Chris Toshok <toshok@netscape.com>, 21-Sep-96.
  21.  */
  22.  
  23.  
  24. #include "Command.h"
  25. #include "Frame.h"
  26. #include "View.h"
  27. #include "ViewGlue.h"
  28. #include "commands.h"
  29.  
  30. #include <xpgetstr.h>     /* for XP_GetString() */
  31.  
  32. extern int XFE_ACTION_SYNTAX_ERROR;
  33.  
  34. const char * Command::commandArmedCallback = "Command::commandArmedCallback";
  35. const char * Command::commandDispatchedCallback = "Command::commandDispatchedCallback";
  36. const char * Command::commandDisarmedCallback = "Command::commandDisarmedCallback";
  37. const char * Command::doCommandCallback = "Command::doCommandCallback";
  38.  
  39. XFE_CommandInfo::XFE_CommandInfo(XFE_CommandEventType t,
  40.                                  Widget w, XEvent* e,
  41.                                  String* p,
  42.                                  Cardinal n) {
  43.     type = t;
  44.     widget = w;
  45.     if (e != NULL ) {
  46.         event = e;
  47.     } else {
  48.         if (XtIsSubclass(widget, xmGadgetClass))
  49.             widget = XtParent(widget);
  50.         event_store.type = 0;
  51.         event_store.xany.serial = 0;
  52.         event_store.xany.send_event = False;
  53.         event_store.xany.display = XtDisplay(widget);
  54.         event_store.xany.window = XtWindow(widget);
  55.         event = &event_store;
  56.     }
  57.     params = p;
  58.  
  59.     if (n == 0) {
  60.         for (; p != NULL && p[n] != NULL; n++)
  61.             ;
  62.     }
  63.  
  64.     nparams_store = n;
  65.     nparams = &nparams_store;
  66. }
  67.  
  68. //
  69. //    These methods impliment simple defaults, so a subclass needs
  70. //    only impliment doCommand.
  71. //
  72. XFE_Command::XFE_Command(CommandType id)
  73. {
  74.     m_id = id;
  75. }
  76.  
  77. #if 0
  78. // inlined.
  79. CommandType
  80. XFE_Command::getId()
  81. {
  82.     return m_id;
  83. }
  84. #endif
  85.  
  86. char*
  87. XFE_Command::getName()
  88. {
  89.     return Command::getString(m_id);
  90. }
  91.  
  92. XP_Bool
  93. XFE_Command::isDynamic()
  94. {
  95.     return FALSE;
  96. }
  97.  
  98. XP_Bool
  99. XFE_Command::isSlow()
  100. {
  101.     return TRUE; // of course
  102. }
  103.  
  104. XP_Bool
  105. XFE_Command::isEnabled(XFE_View*, XFE_CommandInfo*)
  106. {
  107.     return TRUE;
  108. }
  109.  
  110. XP_Bool
  111. XFE_Command::isSelected(XFE_View*, XFE_CommandInfo*)
  112. {
  113.     return FALSE;
  114. }
  115.  
  116. XP_Bool
  117. XFE_Command::isDeterminate(XFE_View*, XFE_CommandInfo*)
  118. {
  119.     return TRUE;
  120. }
  121.  
  122. XFE_CommandParameters*
  123. XFE_Command::getParameters(XFE_View*)
  124. {
  125.     return NULL;
  126. }
  127.  
  128. int
  129. XFE_Command::getParameterIndex(XFE_View*)
  130. {
  131.     return 0;
  132. }
  133.  
  134. void
  135. XFE_Command::setParameterIndex(XFE_View*, unsigned)
  136. {
  137. }
  138.  
  139. char*
  140. XFE_Command::getLabel(XFE_View*, XFE_CommandInfo*)
  141. {
  142.     return NULL; // we could probably impliment this.
  143. }
  144.  
  145. char*
  146. XFE_Command::getTipString(XFE_View*, XFE_CommandInfo*)
  147. {
  148.     return NULL; // we could probably impliment this.
  149. }
  150.  
  151. char*
  152. XFE_Command::getDocString(XFE_View*, XFE_CommandInfo*)
  153. {
  154.     return NULL; // we could probably impliment this.
  155. }
  156.  
  157. XP_Bool
  158. XFE_Command::isEnabled(XFE_Frame*, XFE_CommandInfo*)
  159. {
  160.     return TRUE;
  161. }
  162.  
  163. XP_Bool
  164. XFE_Command::isSelected(XFE_Frame*, XFE_CommandInfo*)
  165. {
  166.     return FALSE;
  167. }
  168.  
  169. XP_Bool
  170. XFE_Command::isDeterminate(XFE_Frame*, XFE_CommandInfo*)
  171. {
  172.     return TRUE;
  173. }
  174.  
  175. XFE_CommandParameters*
  176. XFE_Command::getParameters(XFE_Frame*)
  177. {
  178.     return NULL;
  179. }
  180.  
  181. int
  182. XFE_Command::getParameterIndex(XFE_Frame*)
  183. {
  184.     return 0;
  185. }
  186.  
  187. void
  188. XFE_Command::setParameterIndex(XFE_Frame*, unsigned)
  189. {
  190. }
  191.  
  192. char*
  193. XFE_Command::getLabel(XFE_Frame*, XFE_CommandInfo*)
  194. {
  195.     return NULL;
  196. }
  197.  
  198. char* 
  199. XFE_Command::getTipString(XFE_Frame*, XFE_CommandInfo*)
  200. {
  201.     return NULL;
  202. }
  203.  
  204. char*
  205. XFE_Command::getDocString(XFE_Frame*, XFE_CommandInfo*)
  206. {
  207.     return NULL;
  208. }
  209.  
  210. void
  211. CommandDoSyntaxErrorAlert(MWContext* context, 
  212.                           XFE_Command* command,
  213.                           XFE_CommandInfo* info)
  214. {
  215.     char buf[1024];
  216.     char buf2[1024];
  217.  
  218.     XP_STRCPY(buf, command->getName());
  219.     XP_STRCAT(buf, "(");
  220.     if (info != NULL) {
  221.         for (unsigned n = 0; n < *info->nparams; n++) {
  222.             if (n != 0)
  223.                 XP_STRCAT(buf, ",");
  224.             XP_STRCAT(buf, info->params[n]);
  225.         }
  226.     }
  227.     XP_STRCAT(buf, ")");
  228.  
  229.     sprintf(buf2, XP_GetString(XFE_ACTION_SYNTAX_ERROR), buf);
  230.  
  231.     FE_Alert(context, buf2);
  232. }
  233.  
  234. static XFE_View*
  235. frame_to_view(XFE_Command* command, XFE_Frame* frame, XFE_CommandInfo*)
  236. {
  237.     XFE_View* view = frame->getView();
  238.     XFE_View* sub_view = view->getCommandView(command);
  239.  
  240.     if (sub_view)
  241.         return sub_view;
  242.     else
  243.         return NULL;
  244. }
  245.  
  246. XP_Bool
  247. XFE_ViewCommand::isEnabled(XFE_Frame* frame, XFE_CommandInfo* info)
  248. {
  249.     XFE_View* view = frame_to_view(this, frame, info);
  250.  
  251.     return ((XFE_AbstractCommand*)this)->isEnabled(view, info);
  252. }
  253.  
  254. XP_Bool
  255. XFE_ViewCommand::isSelected(XFE_Frame* frame, XFE_CommandInfo* info)
  256. {
  257.     XFE_View* view = frame_to_view(this, frame, info);
  258.  
  259.     return ((XFE_AbstractCommand*)this)->isSelected(view, info);
  260. }
  261.  
  262. XP_Bool
  263. XFE_ViewCommand::isDeterminate(XFE_Frame* frame, XFE_CommandInfo* info)
  264. {
  265.     XFE_View* view = frame_to_view(this, frame, info);
  266.  
  267.     return ((XFE_AbstractCommand*)this)->isDeterminate(view, info);
  268. }
  269.  
  270. XFE_CommandParameters*
  271. XFE_ViewCommand::getParameters(XFE_Frame* frame)
  272. {
  273.     XFE_View* view = frame_to_view(this, frame, NULL);
  274.  
  275.     return ((XFE_AbstractCommand*)this)->getParameters(view);
  276. }
  277.  
  278. void
  279. XFE_ViewCommand::setParameterIndex(XFE_Frame* frame, unsigned index)
  280. {
  281.     XFE_View* view = frame_to_view(this, frame, NULL);
  282.  
  283.     ((XFE_AbstractCommand*)this)->setParameterIndex(view, index);
  284. }
  285.  
  286. int
  287. XFE_ViewCommand::getParameterIndex(XFE_Frame* frame)
  288. {
  289.     XFE_View* view = frame_to_view(this, frame, NULL);
  290.  
  291.     return ((XFE_AbstractCommand*)this)->getParameterIndex(view);
  292. }
  293.  
  294. char*
  295. XFE_ViewCommand::getLabel(XFE_Frame* frame, XFE_CommandInfo* info)
  296. {
  297.     XFE_View* view = frame_to_view(this, frame, info);
  298.  
  299.     return ((XFE_AbstractCommand*)this)->getLabel(view, info);
  300. }
  301.  
  302. char*
  303. XFE_ViewCommand::getTipString(XFE_Frame* frame, XFE_CommandInfo* info)
  304. {
  305.     XFE_View* view = frame_to_view(this, frame, info);
  306.  
  307.     return ((XFE_AbstractCommand*)this)->getTipString(view, info);
  308. }
  309.  
  310. char*
  311. XFE_ViewCommand::getDocString(XFE_Frame* frame, XFE_CommandInfo* info)
  312. {
  313.     XFE_View* view = frame_to_view(this, frame, info);
  314.  
  315.     return ((XFE_AbstractCommand*)this)->getDocString(view, info);
  316. }
  317.  
  318. void
  319. XFE_ViewCommand::doCommand(XFE_Frame* frame, XFE_CommandInfo* info)
  320. {
  321.     XFE_View* view = frame_to_view(this, frame, info);
  322.  
  323.     ((XFE_AbstractCommand*)this)->doCommand(view, info);
  324. }
  325.  
  326. void
  327. XFE_ViewCommand::doSyntaxErrorAlert(XFE_View* view,  XFE_CommandInfo* info)
  328. {
  329.     CommandDoSyntaxErrorAlert(view->getContext(), this, info);
  330. }
  331.  
  332. XFE_Frame*
  333. XFE_FrameCommand::getFrame(XFE_View* view)
  334. {
  335.     return ViewGlue_getFrame(view->getContext());
  336. }
  337.  
  338. XP_Bool
  339. XFE_FrameCommand::isEnabled(XFE_View* view, XFE_CommandInfo* info)
  340. {
  341.     return ((XFE_AbstractCommand*)this)->isEnabled(getFrame(view), info);
  342. }
  343.  
  344. XP_Bool
  345. XFE_FrameCommand::isSelected(XFE_View* view, XFE_CommandInfo* info)
  346. {
  347.     return ((XFE_AbstractCommand*)this)->isSelected(getFrame(view), info);
  348. }
  349.  
  350. XP_Bool
  351. XFE_FrameCommand::isDeterminate(XFE_View* view, XFE_CommandInfo* info)
  352. {
  353.     return ((XFE_AbstractCommand*)this)->isDeterminate(getFrame(view), info);
  354. }
  355.  
  356. XFE_CommandParameters*
  357. XFE_FrameCommand::getParameters(XFE_View* view)
  358. {
  359.     return ((XFE_AbstractCommand*)this)->getParameters(getFrame(view));
  360. }
  361.  
  362. int
  363. XFE_FrameCommand::getParameterIndex(XFE_View* view)
  364. {
  365.     return ((XFE_AbstractCommand*)this)->getParameterIndex(getFrame(view));
  366. }
  367.  
  368. void
  369. XFE_FrameCommand::setParameterIndex(XFE_View* view, unsigned index)
  370. {
  371.     XFE_Frame* frame = getFrame(view);
  372.     ((XFE_AbstractCommand*)this)->setParameterIndex(frame, index);
  373. }
  374.  
  375. char*
  376. XFE_FrameCommand::getLabel(XFE_View* view, XFE_CommandInfo* info)
  377. {
  378.     return ((XFE_AbstractCommand*)this)->getLabel(getFrame(view), info);
  379. }
  380.  
  381. char*
  382. XFE_FrameCommand::getTipString(XFE_View* view, XFE_CommandInfo* info)
  383. {
  384.     return ((XFE_AbstractCommand*)this)->getTipString(getFrame(view), info);
  385. }
  386.  
  387. char*
  388. XFE_FrameCommand::getDocString(XFE_View* view, XFE_CommandInfo* info)
  389. {
  390.     return ((XFE_AbstractCommand*)this)->getDocString(getFrame(view), info);
  391. }
  392.  
  393. void
  394. XFE_FrameCommand::doCommand(XFE_View* view, XFE_CommandInfo* info)
  395. {
  396.     ((XFE_AbstractCommand*)this)->doCommand(getFrame(view), info);
  397. }
  398.  
  399. void
  400. XFE_FrameCommand::doSyntaxErrorAlert(XFE_Frame* frame,  XFE_CommandInfo* info)
  401. {
  402.     CommandDoSyntaxErrorAlert(frame->getContext(), this, info);
  403. }
  404.  
  405. XFE_CommandList::XFE_CommandList(XFE_CommandList* list, XFE_Command* command)
  406. {
  407.     m_next = list;
  408.     m_command = command;
  409. }
  410.  
  411. XFE_CommandList*
  412. registerCommand(XFE_CommandList*& list, XFE_Command* command)
  413. {
  414.     return list = new XFE_CommandList(list, command);
  415. }
  416.  
  417. XFE_Command*
  418. findCommand(XFE_CommandList* list, CommandType id)
  419. {
  420.     while (list != NULL) {
  421.         if (list->m_command != NULL && list->m_command->getId() == id) {
  422.             return list->m_command;
  423.         }
  424.         list = list->m_next;
  425.     }
  426.     return NULL;
  427. }
  428.  
  429. XFE_ObjectIsCommand::XFE_ObjectIsCommand() : XFE_ViewCommand(xfeCmdObjectIs)
  430. {
  431. };
  432.  
  433. void
  434. XFE_ObjectIsCommand::doCommand(XFE_View* view, XFE_CommandInfo* info)
  435. {
  436.     if (info == NULL)
  437.         return;
  438.     
  439.     char*     c_type = getObjectType(view);
  440.     char*     s_type;
  441.     unsigned  depth;
  442.     String*   av = info->params;
  443.     Cardinal* ac = info->nparams;
  444.     unsigned  n;
  445.     unsigned  m;
  446.     
  447.     for (n = 0; n + 1 < *ac; n++) {
  448.         
  449.         s_type = av[n++];
  450.         
  451.         if (XP_STRCMP(av[n], "{") == 0) {
  452.             n++;
  453.         } else {
  454.             // syntax error.
  455.             return;
  456.         }
  457.         
  458.         for (depth = 1, m = n; av[m] != NULL && m < *ac; m++) {
  459.             if (XP_STRCMP(av[m], "{") == 0)
  460.                 depth++;
  461.             else if (XP_STRCMP(av[m], "}") == 0)
  462.                 depth--;
  463.             
  464.             if (depth == 0)
  465.                 break;
  466.         }
  467.         
  468.         if (XP_STRCASECMP(c_type, s_type) == 0 && m > n && av[m] != NULL) {
  469.             XtCallActionProc(info->widget, "xfeDoCommand", info->event,
  470.                              &av[n], m - n);
  471.             break; // done
  472.         }
  473.         
  474.         n = m;
  475.     }
  476. };
  477.  
  478. int
  479. XFE_commandMatchParameters(XFE_CommandParameters* params, char* name)
  480. {
  481.     int index;
  482.  
  483.     if (params != NULL) {
  484.         for (index = 0; params[index].name != NULL; index++) {
  485.             if (XP_STRCMP(name, params[index].name) == 0)
  486.                 return index;
  487.         }
  488.     }
  489.     return -1;
  490. }
  491.  
  492. struct CommandList_t
  493. {
  494.     char*          name;
  495.     CommandList_t* next;
  496. };
  497.  
  498. static CommandList_t* intern_list;
  499.  
  500. char *
  501. Command::intern(char *foo)
  502. {
  503.     int middle, first, last;
  504.  
  505.     if (foo == NULL) return NULL;
  506.  
  507.     /* do a binary search through the _XfeCommandIndices table, and return
  508.        the correct string */
  509.  
  510.     first = 0;
  511.     last = _XfeNumCommands - 1;
  512.  
  513.     while (first <= last)
  514.         {
  515.             int compare;
  516.             char *command;
  517.  
  518.             middle = (first + last) / 2;
  519.             
  520.             command = &_XfeCommands[_XfeCommandIndices[middle]];
  521.  
  522.             compare = strcmp(foo, command);
  523.  
  524.             if (compare == 0)
  525.                 {
  526.                     return command;
  527.                 }
  528.             else if (compare < 0)
  529.                 {
  530.                     last = middle - 1;
  531.                 }
  532.             else
  533.                 {
  534.                     first = middle + 1;
  535.                 }
  536.         }
  537.  
  538.     //
  539.     //    Didn't find it in static list, look in dynamic list
  540.     //
  541.     CommandList_t* bar;
  542.     for (bar = intern_list; bar != NULL; bar = bar->next) {
  543.         if (strcmp(foo, bar->name) == 0)
  544.             return bar->name;
  545.     }
  546.  
  547.     //    Didn't match, add a new dude..
  548.     bar = new CommandList_t;
  549.     bar->name = XP_STRDUP(foo);
  550.     bar->next = intern_list;
  551.     intern_list = bar;
  552.  
  553.     return bar->name;
  554. }
  555.  
  556. struct cmd_mapping {
  557.     char *old_remote_cmd;
  558.     char *xfe_do_cmd;
  559. };
  560.  
  561. static struct cmd_mapping mapping[] = {
  562.     /* bringing up new pages. */
  563.     { "new",            xfeCmdOpenBrowser },
  564.     { "openFile",        xfeCmdOpenPageChooseFile },
  565.     { "saveAs",            xfeCmdSaveAs },
  566. #ifdef MOZ_MAIL_NEW
  567.     { "mailNew",        xfeCmdNewMessage },
  568. #endif
  569.     { "docInfo",        xfeCmdViewPageInfo },
  570.     { "print",            xfeCmdPrint },
  571.     { "delete",            xfeCmdClose },
  572.     { "quit",            xfeCmdExit },
  573.  
  574.     /* edit menu functions. */
  575.     { "find",            xfeCmdFindInObject },
  576.     { "findAgain",        xfeCmdFindAgain },
  577.  
  578.     /* view menu functions. */
  579.     { "reload",            xfeCmdReload },
  580.     { "loadImages",     xfeCmdShowImage },
  581.     { "source",            xfeCmdViewPageSource },
  582.  
  583.     /* go menu functions. */
  584.     { "back",            xfeCmdBack },
  585.     { "forward",        xfeCmdForward },
  586.     { "home",            xfeCmdHome },
  587.     { "abort",            xfeCmdStopLoading },
  588.     { "viewHistory",    xfeCmdOpenHistory },
  589.  
  590.     { "viewBookmark",    xfeCmdOpenBookmarks },
  591.     { "preferences",    xfeCmdEditPreferences },
  592.  
  593. #ifdef MOZ_MAIL_NEWS
  594.     { "openInbox",        xfeCmdOpenInbox },
  595.     { "inbox",            xfeCmdOpenInbox },
  596.     { "openAddrBook",    xfeCmdOpenAddressBook },
  597.     { "addrbk",            xfeCmdOpenAddressBook },
  598.     { "openNews",        xfeCmdOpenNewsgroups },
  599.     { "news",            xfeCmdOpenNewsgroups },
  600. #endif
  601. #ifdef EDITOR
  602.     { "editor",            xfeCmdOpenEditor },
  603.     { "openEditor",        xfeCmdOpenEditor },
  604.     { "edit",           xfeCmdNewBlank },
  605.     { "editURL",        xfeCmdNewBlank },
  606. #endif
  607. #ifdef MOZ_MAIL_NEWS
  608.     { "openConference", xfeCmdOpenConference }
  609. #endif
  610.  
  611. };
  612. static int num_old_cmds = sizeof(mapping) / sizeof(mapping[0]);
  613.  
  614. char *
  615. Command::convertOldRemote(char *foo)
  616. {
  617.     int i;
  618.  
  619.     for (i = 0; i < num_old_cmds; i ++)
  620.         {
  621.             if (!XP_STRCASECMP(foo, mapping[i].old_remote_cmd))
  622.                 return mapping[i].xfe_do_cmd;
  623.         }
  624.  
  625.     return Command::intern(foo);
  626. }
  627.