1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is BlueGriffon.
16 * The Initial Developer of the Original Code is
17 * Disruptive Innovations SARL.
18 * Portions created by the Initial Developer are Copyright (C) 2006
19 * the Initial Developer. All Rights Reserved.
22 * Daniel Glazman <daniel.glazman@disruptive-innovations.com>, Original author
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 const kBASE_COMMAND_CONTROLLER_CID = "@mozilla.org/embedcomp/base-command-controller;1";
40 const nsIControllerContext = interfaces.nsIControllerContext;
41 const nsIInterfaceRequestor = interfaces.nsIInterfaceRequestor;
42 const nsIControllerCommandTable = interfaces.nsIControllerCommandTable;
44 var ComposerCommands = {
46 mComposerJSCommandControllerID: null,
47 mSelectionTimeOutId: null,
49 getComposerCommandTable: function getComposerCommandTable()
52 if (this.mComposerJSCommandControllerID)
55 controller = window.content.controllers.getControllerById(this.mComposerJSCommandControllerID);
61 controller = Components.classes[kBASE_COMMAND_CONTROLLER_CID].createInstance();
63 var editorController = controller.QueryInterface(nsIControllerContext);
64 editorController.init(null);
65 editorController.setCommandContext(null);
66 window.controllers.insertControllerAt(0, controller);
68 // Store the controller ID so we can be sure to get the right one later
69 this.mComposerJSCommandControllerID = window.controllers.getControllerId(controller);
74 var interfaceRequestor = controller.QueryInterface(nsIInterfaceRequestor);
75 return interfaceRequestor.getInterface(nsIControllerCommandTable);
80 goUpdateComposerMenuItems: function goUpdateComposerMenuItems(commandset)
82 dump("Updating commands for " + commandset.id + "\n");
83 for (var i = 0; i < commandset.childNodes.length; i++)
85 var commandNode = commandset.childNodes[i];
86 var commandID = commandNode.id;
89 goUpdateCommand(commandID); // enable or disable
90 if (commandNode.hasAttribute("state"))
91 this.goUpdateCommandState(commandID);
96 goUpdateCommandState: function goUpdateCommandState(command)
100 var controller = top.document.commandDispatcher.getControllerForCommand(command);
101 if (!(controller instanceof Components.interfaces.nsICommandController))
104 var params = this.newCommandParams();
107 controller.getCommandStateWithParams(command, params);
113 case "cmd_underline":
123 this.pokeStyleUI(command, params.getBooleanValue("state_all"));
126 case "cmd_paragraphState":
130 this.pokeMultiStateUI(command, params);
137 default: dump("no update for command: " +command+"\n");
140 catch (e) { dump("An error occurred updating the "+command+" command: \n"+e+"\n"); }
143 pokeStyleUI: function pokeStyleUI(uiID, aDesiredState)
146 var commandNode = top.document.getElementById(uiID);
150 var uiState = ("true" == commandNode.getAttribute("state"));
151 if (aDesiredState != uiState)
158 commandNode.setAttribute("state", newState);
160 } catch(e) { dump("poking UI for "+uiID+" failed: "+e+"\n"); }
163 newCommandParams: function newCommandParams()
166 return Components.classes["@mozilla.org/embedcomp/command-params;1"].createInstance(Components.interfaces.nsICommandParams);
168 catch(e) { dump("error thrown in newCommandParams: "+e+"\n"); }
172 pokeMultiStateUI: function pokeMultiStateUI(uiID, cmdParams)
176 var commandNode = document.getElementById(uiID);
180 var isMixed = cmdParams.getBooleanValue("state_mixed");
183 desiredAttrib = "mixed";
185 desiredAttrib = cmdParams.getCStringValue("state_attribute");
187 var uiState = commandNode.getAttribute("state");
188 if (desiredAttrib != uiState)
190 commandNode.setAttribute("state", desiredAttrib);
195 doStyleUICommand: function doStyleUICommand(cmdStr)
199 var cmdParams = this.newCommandParams();
200 this.goDoCommandParams(cmdStr, cmdParams);
204 doStatefulCommand: function doStatefulCommand(commandID, newState)
206 var commandNode = document.getElementById(commandID);
208 commandNode.setAttribute("state", newState);
212 var cmdParams = this.newCommandParams();
213 if (!cmdParams) return;
215 cmdParams.setCStringValue("state_attribute", newState);
216 this.goDoCommandParams(commandID, cmdParams);
218 this.pokeMultiStateUI(commandID, cmdParams);
220 } catch(e) { dump("error thrown in doStatefulCommand: "+e+"\n"); }
223 goDoCommandParams: function goDoCommandParams(command, params)
227 var controller = top.document.commandDispatcher.getControllerForCommand(command);
228 if (controller && controller.isCommandEnabled(command))
230 if (controller instanceof Components.interfaces.nsICommandController)
232 controller.doCommandWithParams(command, params);
234 // the following two lines should be removed when we implement observers
236 controller.getCommandStateWithParams(command, params);
240 controller.doCommand(command);
246 dump("An error occurred executing the "+command+" command\n");
250 setupMainCommands: function setupMainCommands()
252 var commandTable = this.getComposerCommandTable();
256 //dump("Registering plain text editor commands\n");
257 commandTable.registerCommand("cmd_stopLoading", cmdStopLoading);
258 commandTable.registerCommand("cmd_open", cmdOpen);
259 commandTable.registerCommand("cmd_save", cmdSave);
260 commandTable.registerCommand("cmd_saveAs", cmdSaveAs);
261 commandTable.registerCommand("cmd_fullScreen", cmdFullScreen);
262 commandTable.registerCommand("cmd_new", cmdNew);
263 commandTable.registerCommand("cmd_newWizard", cmdNewWizard);
264 commandTable.registerCommand("cmd_renderedHTMLEnabler", cmdDummyHTML);
265 commandTable.registerCommand("cmd_cleanup", cmdMarkupCleaner);
267 commandTable.registerCommand("cmd_table", cmdInsertOrEditTableCommand);
269 commandTable.registerCommand("cmd_class", cmdClass);
272 setupFormatCommands: function setupFormatCommands()
275 var commandManager = EditorUtils.getCurrentCommandManager();
277 commandManager.addCommandObserver(gEditorDocumentObserver, "obs_documentCreated");
278 commandManager.addCommandObserver(gEditorDocumentObserver, "cmd_setDocumentModified");
279 commandManager.addCommandObserver(gEditorDocumentObserver, "obs_documentWillBeDestroyed");
280 commandManager.addCommandObserver(gEditorDocumentObserver, "obs_documentLocationChanged");
282 // cmd_bold is a proxy, that's the only style command we add here
283 commandManager.addCommandObserver(gEditorDocumentObserver, "cmd_bold");
284 } catch (e) { alert(e); }
287 updateSelectionBased: function updateSelectionBased(aElt, aEvent)
290 var mixed = EditorUtils.getSelectionContainer();
292 var element = mixed.node;
293 var oneElementSelected = mixed.oneElementSelected;
295 if (!element) return;
297 if (this.mSelectionTimeOutId)
298 clearTimeout(this.mSelectionTimeOutId);
300 this.mSelectionTimeOutId = setTimeout(this._updateSelectionBased, 100, element, oneElementSelected);
305 _updateSelectionBased: function _updateSelectionBased(aElement, aOneElementSelected)
307 NotifierUtils.notify("selection", aElement, aOneElementSelected);
310 onStateButtonUpdate: function onStateButtonUpdate(button, commmandID, onState)
312 var commandNode = document.getElementById(commmandID);
313 var state = commandNode.getAttribute("state");
315 button.checked = state == onState;
319 notifySelectionChanged: function(doc, sel, reason)
321 ComposerCommands.updateSelectionBased();
327 #include navigationCommands.inc
328 #include fileCommands.inc
329 #include viewCommands.inc
330 #include dummyCommands.inc
331 #include formatCommands.inc
332 #include insertionCommands.inc