home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / Composer / meditor.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  10.2 KB  |  343 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. #include "meditor.h"    // HandleModalDialog
  20. #include "StBlockingDialogHandler.h"
  21. #include "CEditView.h"
  22. #include "macgui.h"        // UGraphics::MakeLOColor
  23. #include "CPaneEnabler.h"
  24. #include "resgui.h"        // EDITDLG_AUTOSAVE
  25. #include "edt.h"
  26. #include "fe_proto.h"
  27. #include "prefapi.h"    // PREF_GetBoolPref, PREF_GetIntPref
  28. #include "shist.h"        // SHIST_GetCurrent
  29. #include "uapp.h"        // CFrontApp
  30. #include "CColorPopup.h"
  31.  
  32. extern "C" {
  33. #include "xpgetstr.h"
  34. #define WANT_ENUM_STRING_IDS
  35. #include "allxpstr.h"
  36. #undef WANT_ENUM_STRING_IDS
  37. }
  38.  
  39. #include "CNSContext.h"    // ExtractHyperView
  40.  
  41.  
  42. // takes pascal-strings
  43. MessageT HandleModalDialog( int id, const unsigned char *prompt1, const unsigned char* prompt2)
  44. {
  45.     StPrepareForDialog        prepare;
  46.     
  47.     StBlockingDialogHandler handler( id, NULL );
  48.     LDialogBox* dialog = (LDialogBox *)handler.GetDialog();
  49.     if ( prompt1 )
  50.     {
  51.         LCaption *caption = (LCaption *)dialog->FindPaneByID( '^1  ' );
  52.         if ( caption )
  53.             caption->SetDescriptor( prompt1 );
  54.     }
  55.     if ( prompt2 )
  56.     {
  57.         LCaption *caption = (LCaption *)dialog->FindPaneByID( '^2  ' );
  58.         if ( caption )
  59.             caption->SetDescriptor( prompt2 );
  60.     }
  61.     
  62.     MessageT message;
  63.     do {
  64.         message = handler.DoDialog();
  65.     }
  66.     while ( message == 0 );
  67.     
  68.     return message;
  69. }
  70.  
  71. /* Set default colors, background from user Preferences via the Page Data structure 
  72. */
  73. void FE_SetNewDocumentProperties(MWContext * pContext)
  74. {
  75.     if ( pContext && pContext->is_editor && pContext->bIsComposeWindow )
  76.         return;
  77.  
  78.     EDT_PageData *pageData = EDT_NewPageData();
  79.  
  80.     if (pageData == NULL)  return;
  81.  
  82.     if (CPrefs::GetBoolean(CPrefs::EditorUseCustomColors )) {
  83.     
  84.         LO_Color EditorText = UGraphics::MakeLOColor(CPrefs::GetColor(CPrefs::EditorText));
  85.         LO_Color EditorLink = UGraphics::MakeLOColor(CPrefs::GetColor(CPrefs::EditorLink));
  86.         LO_Color EditorActiveLink = UGraphics::MakeLOColor(CPrefs::GetColor(CPrefs::EditorActiveLink));
  87.         LO_Color EditorFollowedLink = UGraphics::MakeLOColor(CPrefs::GetColor(CPrefs::EditorFollowedLink));
  88.         LO_Color EditorBackground = UGraphics::MakeLOColor(CPrefs::GetColor(CPrefs::EditorBackground));
  89.  
  90.         pageData->pColorText = &EditorText;
  91.         pageData->pColorLink= &EditorLink;
  92.         pageData->pColorActiveLink = &EditorActiveLink;
  93.         pageData->pColorFollowedLink = &EditorFollowedLink;
  94.         pageData->pColorBackground = &EditorBackground;
  95.     
  96.     } else {
  97.     
  98.         pageData->pColorText = NULL;            // I assume this is how we get the browser defaults...
  99.         pageData->pColorLink=  NULL;
  100.         pageData->pColorActiveLink =  NULL;
  101.         pageData->pColorFollowedLink =  NULL;
  102.         pageData->pColorBackground =  NULL;
  103.         
  104.     }
  105.  
  106.     Bool hasBackgroundImage;
  107.     if ( ( PREF_GetBoolPref( "editor.use_background_image", &hasBackgroundImage ) == PREF_NOERROR )
  108.         && hasBackgroundImage )
  109.     {
  110.         pageData->pBackgroundImage = CPrefs::GetCharPtr(CPrefs::EditorBackgroundImage);
  111.         if (pageData->pBackgroundImage && XP_STRLEN(pageData->pBackgroundImage) == 0)            // if there is really nothing there, skip it.
  112.             pageData->pBackgroundImage = NULL;
  113.     }
  114.     else
  115.         pageData->pBackgroundImage = NULL;
  116.         
  117.     if ( pContext && pContext->title )
  118.         pageData->pTitle = XP_STRDUP(pContext->title);
  119.         
  120.  
  121.     EDT_SetPageData(pContext, pageData);
  122.     
  123.     pageData->pColorText = NULL;        //  don't free out lacal data!!!
  124.     pageData->pColorLink=  NULL;
  125.     pageData->pColorActiveLink =  NULL;
  126.     pageData->pColorFollowedLink =  NULL;
  127.     pageData->pColorBackground =  NULL;
  128.     pageData->pBackgroundImage =  NULL;
  129.     
  130.     EDT_FreePageData(pageData);
  131.  
  132.     // Set Author name:
  133.  
  134. //    CStr255    EditorAuthor(CPrefs::GetString(CPrefs::EditorAuthor));
  135.     
  136. //    FE_UsersFullName();
  137.     
  138.  
  139.     EDT_MetaData *metaData = EDT_NewMetaData();
  140.     if (metaData == NULL) return;
  141.     metaData->bHttpEquiv = FALSE;
  142.     metaData->pName = XP_STRDUP("Author");
  143.     metaData->pContent = XP_STRDUP(CPrefs::GetString(CPrefs::EditorAuthor));
  144.     EDT_SetMetaData(pContext, metaData);     
  145.     EDT_FreeMetaData(metaData);
  146. }
  147.  
  148.  
  149. /* 
  150.  * Brings up a modal image load dialog and returns.  Calls
  151.  *  EDT_ImageLoadCancel() if the cancel button is pressed
  152. */
  153. void FE_ImageLoadDialog( MWContext * /* pContext */ )
  154. {
  155. }
  156.  
  157. /* 
  158.  * called by the editor engine after the image has been loaded
  159. */
  160. void FE_ImageLoadDialogDestroy( MWContext * /* pContext */ )
  161. {
  162. }
  163.  
  164. void FE_EditorDocumentLoaded( MWContext *pContext )
  165. {
  166.     if (pContext == NULL || !EDT_IS_EDITOR(pContext))
  167.         return;
  168.     
  169.     CEditView *editView = (CEditView *)ExtractHyperView(pContext);
  170.  
  171.     int32 iSave;
  172.     if ( pContext->bIsComposeWindow )
  173.     {
  174.         iSave = 0;    // auto-save
  175.         
  176.         CMailEditView *mailEditView = dynamic_cast<CMailEditView *>(editView);
  177.         if ( mailEditView )
  178.             mailEditView->InitMailCompose();
  179.     }
  180.     else
  181.     { 
  182.         XP_Bool doAutoSave;
  183.         PREF_GetBoolPref( "editor.auto_save", &doAutoSave );
  184.         if ( doAutoSave )
  185.             PREF_GetIntPref( "editor.auto_save_delay", &iSave );
  186.         else
  187.             iSave = 0;
  188.     }
  189.     
  190.     EDT_SetAutoSavePeriod(pContext, iSave );
  191.     
  192.     // remember when the file was (last) modified
  193.     // initializes date/time stamp for external editor warning
  194.     EDT_IsFileModified(pContext);
  195.     
  196.     // We had disabled everything, now we have to enable it again. This happens automatically on activate, but we might not get an activate
  197.     // if we don't have a dialog poping up (like if the user just creates a new document, there is no dialog...)
  198.  
  199.     // set this after calling InitMailCompose
  200.     if ( editView )
  201.     {
  202.         editView->mEditorDoneLoading = true;
  203.             
  204.         // set color popup control to show correct default color (now that we have an mwcontext)
  205.         editView->mColorPopup->InitializeCurrentColor();
  206.     }
  207.     
  208.     InitCursor();
  209.     (CFrontApp::GetApplication())->UpdateMenus();
  210. }
  211.  
  212.  
  213. Bool FE_CheckAndAutoSaveDocument(MWContext *pContext)
  214. {
  215.     if (pContext == NULL || !EDT_IS_EDITOR(pContext) || ExtractHyperView(pContext) == NULL )
  216.         return FALSE;
  217.     
  218.     if ( pContext->bIsComposeWindow )
  219.         return FALSE;
  220.     
  221.     CEditView *editView = (CEditView *)ExtractHyperView(pContext);
  222.     if ( FrontWindow() != editView->GetMacPort() )
  223.         return true;
  224.     
  225.     if (!EDT_DirtyFlag(pContext) && !EDT_IS_NEW_DOCUMENT(pContext))
  226.         return TRUE;
  227.     
  228.     History_entry*    newEntry = SHIST_GetCurrent(&pContext->hist);
  229.     CStr255 fileName;
  230.     if ( newEntry && newEntry->address )
  231.         fileName = newEntry->address;
  232.                 
  233.     MessageT itemHit = HandleModalDialog(EDITDLG_AUTOSAVE, fileName, NULL );
  234.     if (itemHit != ok)
  235.         return FALSE;
  236.         
  237.     return ((CEditView *)ExtractHyperView(pContext))->SaveDocument();
  238. }
  239.  
  240.  
  241. void FE_FinishedSave( MWContext * /* pMWContext */, int /* status */, char * /* pDestURL */, int /* iFileNumber */ )
  242. {
  243. }
  244.  
  245. // in xp_file.h
  246. // Create a backup filename for renaming current file before saving data
  247. //    Input should be be URL file type "file:///..."
  248. //    Caller must free the string with XP_FREE
  249.  
  250. /*
  251.  * I don't know what the logic here should be, so I mostly copied this from the Windows code in:
  252.  * src/ns/cmd/winfe/fegui.cpp#XP_BackupFileName()
  253.  * (I didn't copy all the Windows code which deals with 8.3 filenames.)
  254.  */
  255.  
  256. char * XP_BackupFileName( const char * szURL )
  257. {
  258.     // Must have "file:" URL type and at least 1 character after "///"
  259.     if ( szURL == NULL || !NET_IsLocalFileURL((char*)szURL) || XP_STRLEN(szURL) <= 8 )
  260.         return NULL;
  261.  
  262.     // Add extra space for '\0' and '.BAK', but subtract space for "file:///"
  263.  
  264.     char *szFileName = (char *)XP_ALLOC((XP_STRLEN(szURL)+1+4-7)*sizeof(char));
  265.     if ( szFileName == NULL )
  266.         return NULL;
  267.  
  268.     // Get filename but ignore "file:///"
  269.  
  270. //    {
  271. //        char* filename = WH_FileName(szURL+7, xpURL);
  272. //        if (!filename) return NULL;
  273. //        XP_STRCPY(szFileName,filename);
  274. //        XP_FREE(filename);
  275. //    }
  276.     XP_STRCPY(szFileName, szURL+7);
  277.  
  278.     // Add extension to the filename
  279.     XP_STRCAT( szFileName, ".BAK" );
  280.  
  281.     return szFileName;
  282. }
  283.  
  284.    
  285.  
  286. // If pszLocalName is not NULL, we return the full pathname
  287. //   in local platform syntax, even if file is not found.
  288. //   Caller must free this string.
  289. // Returns TRUE if file already exists
  290. //
  291.  
  292. /*
  293.  * I don't know what the logic here should be, so I mostly copied this from the Windows code in:
  294.  * src/ns/cmd/winfe/fegui.cpp#XP_ConvertUrlToLocalFile()
  295.  * (I didn't copy all the Windows code which deals with 8.3 filenames.)
  296.  */
  297.  
  298. // The results of this call are passed directly to functions like XP_Stat and XP_FileOpen.
  299. // brade--use xpURL format
  300. Bool XP_ConvertUrlToLocalFile(const char * szURL, char **pszLocalName)        // return TRUE if the file exists!! or return FALSE;
  301. {
  302.     // Default assumes no file found - no local filename
  303.     
  304.     Boolean bFileFound = FALSE;
  305.     if ( pszLocalName )
  306.         *pszLocalName = NULL;
  307.  
  308.     // if "file:///Untitled" fail to convert
  309.     if ( szURL && XP_STRCMP( szURL, XP_GetString(XP_EDIT_NEW_DOC_NAME) ) == 0 )
  310.         return bFileFound;
  311.  
  312.     // Must have "file:" URL type and at least 1 character after "///"
  313.     if ( szURL == NULL || !NET_IsLocalFileURL((char*)szURL) || XP_STRLEN(szURL) <= 8 )
  314.         return FALSE;
  315.  
  316.  
  317.     // Extract file path from URL: e.g. "/c|/foo/file.html"
  318.  
  319.     char *szFileName = NET_ParseURL( szURL, GET_PATH_PART);
  320.     if (szFileName == NULL)
  321.         return FALSE;
  322.  
  323.     // NET_UnEscape(szFileName);  This will be done in WH_FileName, so don't unescape twice.
  324.  
  325.     // Test if file exists
  326.     XP_StatStruct statinfo; 
  327.     if ( -1 != XP_Stat(szFileName, &statinfo, xpURL)            // if the file exists
  328.         && statinfo.st_mode & S_IFREG )                            // and its a normal file
  329.             bFileFound = TRUE;                                    // We found it!
  330.  
  331.     if ( pszLocalName )
  332.     {
  333.         // Pass string to caller
  334.         *pszLocalName = WH_FileName(szFileName, xpURL);
  335.         if (szFileName)
  336.             XP_FREE( szFileName );
  337.     }
  338.     else 
  339.         XP_FREE(szFileName);
  340.  
  341.     return bFileFound;
  342. }
  343.