Back to index

lightning-sunbird  0.9+nobinonly
EditorFrame.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: Mozilla-sample-code 1.0
00003  *
00004  * Copyright (c) 2002 Netscape Communications Corporation and
00005  * other contributors
00006  *
00007  * Permission is hereby granted, free of charge, to any person obtaining a
00008  * copy of this Mozilla sample software and associated documentation files
00009  * (the "Software"), to deal in the Software without restriction, including
00010  * without limitation the rights to use, copy, modify, merge, publish,
00011  * distribute, sublicense, and/or sell copies of the Software, and to permit
00012  * persons to whom the Software is furnished to do so, subject to the
00013  * following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included
00016  * in all copies or substantial portions of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00021  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00023  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00024  * DEALINGS IN THE SOFTWARE.
00025  *
00026  * Contributor(s):
00027  *   Adam Lock <adamlock@netscape.com>
00028  *
00029  * ***** END LICENSE BLOCK ***** */
00030  
00031 #include "global.h"
00032 
00033 #include "nsIWebNavigation.h"
00034 #include "nsIDOMDocument.h"
00035 #include "nsIDOMHTMLDocument.h"
00036 #include "nsIDOMWindow.h"
00037 #include "nsIEditingSession.h"
00038 
00039 #include "EditorFrame.h"
00040 
00041 BEGIN_EVENT_TABLE(EditorFrame, GeckoFrame)
00042     EVT_MENU(XRCID("edit_bold"),           EditorFrame::OnEditBold)
00043     EVT_MENU(XRCID("edit_italic"),         EditorFrame::OnEditItalic)
00044     EVT_MENU(XRCID("edit_underline"),      EditorFrame::OnEditUnderline)
00045     EVT_UPDATE_UI(XRCID("edit_bold"),      EditorFrame::OnUpdateToggleCmd)
00046     EVT_UPDATE_UI(XRCID("edit_italic"),    EditorFrame::OnUpdateToggleCmd)
00047     EVT_UPDATE_UI(XRCID("edit_underline"), EditorFrame::OnUpdateToggleCmd)
00048     EVT_MENU(XRCID("edit_indent"),         EditorFrame::OnEditIndent)
00049     EVT_MENU(XRCID("edit_outdent"),        EditorFrame::OnEditOutdent)
00050     EVT_MENU(XRCID("edit_aleft"),          EditorFrame::OnEditAlignLeft)
00051     EVT_MENU(XRCID("edit_acenter"),        EditorFrame::OnEditAlignCenter)
00052     EVT_MENU(XRCID("edit_aright"),         EditorFrame::OnEditAlignRight)
00053 END_EVENT_TABLE()
00054 
00055 EditorFrame::EditorFrame(wxWindow* aParent)
00056 {
00057     wxXmlResource::Get()->LoadFrame(this, aParent, wxT("editor_frame"));
00058 
00059     SetName("editor");
00060 
00061     SetIcon(wxICON(appicon));
00062 
00063     SetupDefaultGeckoWindow();
00064 
00065     SendSizeEvent(); // 
00066     
00067     CreateStatusBar();
00068 
00069     MakeEditable();
00070 
00071     mCommandManager = do_GetInterface(mWebBrowser);
00072 
00073     nsCOMPtr<nsIWebNavigation> webNav = do_QueryInterface(mWebBrowser);
00074     webNav->LoadURI(NS_ConvertASCIItoUCS2("www.mozilla.org").get(),
00075         nsIWebNavigation::LOAD_FLAGS_NONE, nsnull, nsnull, nsnull);
00076 
00077 }
00078 
00079 void EditorFrame::MakeEditable()
00080 {
00081     nsCOMPtr<nsIDOMWindow> domWindow;
00082     mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
00083 
00084     nsCOMPtr<nsIEditingSession> editingSession = do_GetInterface(mWebBrowser);
00085     if (!editingSession)
00086         return;// NS_ERROR_FAILURE;
00087     editingSession->MakeWindowEditable(domWindow, NULL, PR_TRUE);
00088 }
00089 
00090 nsresult EditorFrame::DoCommand(const char *aCommand, nsICommandParams *aCommandParams)
00091 {
00092     if (mCommandManager)
00093     {
00094         nsCOMPtr<nsIDOMWindow> domWindow;
00095         mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
00096         return mCommandManager->DoCommand(aCommand, aCommandParams, domWindow);
00097     }
00098     return NS_ERROR_FAILURE;
00099 }
00100 
00101 void EditorFrame::IsCommandEnabled(const char *aCommand, PRBool *retval)
00102 {
00103     if (mCommandManager)
00104     {
00105         nsCOMPtr<nsIDOMWindow> domWindow;
00106         mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
00107         mCommandManager->IsCommandEnabled(aCommand, domWindow, retval);
00108     }
00109 }
00110 
00111 void EditorFrame::GetCommandState(const char *aCommand, nsICommandParams *aCommandParams)
00112 {
00113     if (mCommandManager)
00114     {
00115         nsCOMPtr<nsIDOMWindow> domWindow;
00116         mWebBrowser->GetContentDOMWindow(getter_AddRefs(domWindow));
00117         mCommandManager->GetCommandState(aCommand, domWindow, aCommandParams);
00118     }
00119 }
00120 
00121 void EditorFrame::UpdateStatusBarText(const PRUnichar* aStatusText)
00122 {
00123     SetStatusText(aStatusText);
00124 }
00125 
00126 static
00127 nsresult MakeCommandParams(nsICommandParams **aParams)
00128 {
00129     nsresult rv;
00130     nsCOMPtr<nsICommandParams> params = do_CreateInstance(NS_COMMAND_PARAMS_CONTRACTID,&rv);
00131     if (NS_FAILED(rv))
00132         return rv;
00133     if (!params)
00134         return NS_ERROR_FAILURE;
00135     *aParams = params;
00136     NS_ADDREF(*aParams);
00137     return rv;
00138 }
00139 
00140 static const char kCmdBold[]      = "cmd_bold";
00141 static const char kCmdItalic[]    = "cmd_italic";
00142 static const char kCmdUnderline[] = "cmd_underline";
00143 static const char kCmdIndent[]    = "cmd_indent";
00144 static const char kCmdOutdent[]   = "cmd_outdent";
00145 static const char kCmdAlign[]     = "cmd_align";
00146 
00147 void EditorFrame::OnEditBold(wxCommandEvent &event)
00148 {
00149     DoCommand(kCmdBold, nsnull);
00150 }
00151 
00152 void EditorFrame::OnEditItalic(wxCommandEvent &event)
00153 {
00154     DoCommand(kCmdItalic, nsnull);
00155 }
00156 
00157 void EditorFrame::OnEditUnderline(wxCommandEvent &event)
00158 {
00159     DoCommand(kCmdUnderline, nsnull);
00160 }
00161 
00162 void EditorFrame::OnEditIndent(wxCommandEvent &event)
00163 {
00164     DoCommand(kCmdIndent, nsnull);
00165 }
00166 
00167 void EditorFrame::OnEditOutdent(wxCommandEvent &event)
00168 {
00169     DoCommand(kCmdOutdent, nsnull);
00170 }
00171 
00172 const char kAlignLeft[]   = "left";
00173 const char kAlignRight[]  = "right";
00174 const char kAlignCenter[] = "center";
00175 
00176 
00177 void EditorFrame::OnEditAlignLeft(wxCommandEvent &event)
00178 {
00179     ExecuteAttribParam(kCmdAlign, kAlignLeft);
00180 }
00181 
00182 void EditorFrame::OnEditAlignRight(wxCommandEvent &event)
00183 {
00184     ExecuteAttribParam(kCmdAlign, kAlignRight);
00185 }
00186 
00187 void EditorFrame::OnEditAlignCenter(wxCommandEvent &event)
00188 {
00189     ExecuteAttribParam(kCmdAlign, kAlignCenter);
00190 }
00191 
00192 const char kStateAll[]       = "state_all";
00193 const char kStateMixed[]     = "state_mixed";
00194 const char kStateAttribute[] = "state_attribute";
00195 const char kStateEnabled[]   = "state_enabled";
00196 
00197 void EditorFrame::OnUpdateToggleCmd(wxUpdateUIEvent &event)
00198 {
00199     const char *cmd = NULL;
00200     if (event.GetId() == XRCID("edit_bold"))
00201     {
00202         cmd = kCmdBold;
00203     }
00204     else if (event.GetId() == XRCID("edit_italic"))
00205     {
00206         cmd = kCmdItalic;
00207     }
00208     else if (event.GetId() == XRCID("edit_underline"))
00209     {
00210         cmd = kCmdUnderline;
00211     }
00212 
00213     // command manager GetCommandState cmd_bold
00214     nsCOMPtr<nsICommandParams> params;
00215     if (NS_SUCCEEDED(MakeCommandParams(getter_AddRefs(params))))
00216     {
00217         GetCommandState(cmd, params);
00218 
00219         // Does our current selection span mixed styles?
00220         // If so, set the toolbar button to an indeterminate
00221         // state
00222         //
00223         PRBool bMixedStyle = PR_FALSE;
00224         params->GetBooleanValue(kStateMixed, &bMixedStyle);
00225 
00226         // We're not in STATE_MIXED. Enable/Disable the
00227         // toolbar button based on it's current state
00228         //
00229         PRBool bCmdEnabled = PR_FALSE;
00230         params->GetBooleanValue(kStateAll, &bCmdEnabled);
00231 
00232         event.Check((bMixedStyle || bCmdEnabled) ? TRUE : FALSE);
00233     }
00234 }
00235 
00236 nsresult EditorFrame::ExecuteAttribParam(const char *aCommand, const char *aAttribute)
00237 {
00238     nsresult rv;
00239     nsCOMPtr<nsICommandParams> params;
00240     rv = MakeCommandParams(getter_AddRefs(params));
00241     if (NS_FAILED(rv))
00242         return rv;
00243     if (!params)
00244         return NS_ERROR_FAILURE;
00245     params->SetCStringValue(kStateAttribute, aAttribute);
00246     return DoCommand(aCommand, params);
00247 }