home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / Composer / CFontMenuAttachment.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  7.9 KB  |  300 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 "CFontMenuAttachment.h"
  20. #include "CEditView.h"
  21. #include "CBrowserContext.h"        // operator MWContext*()
  22. #include "resgui.h"                    // cmd_FormatViewerFont, cmd_FormatFixedFont, FONT_MENU_BASE
  23. #include "macutil.h"                // CMediatedWindow
  24. #include "edt.h"
  25.  
  26.  
  27.  
  28. LMenu *CFontMenuAttachment::sMenu = NULL;
  29.  
  30. //===========================================================
  31. // CFontMenuAttachment
  32. //===========================================================
  33. CFontMenuAttachment::CFontMenuAttachment()
  34. {
  35.     UpdateMenu();
  36. }
  37.  
  38. MWContext *CFontMenuAttachment::GetTopWindowContext()
  39. {
  40.                             
  41.     // Ok, ok. I know this is skanky,
  42.     // but there is no common way to get the context from a window: it depends on the window type.
  43.     // So, we look around for a CEditView somewhere in the top window.
  44.     // A CEditView we understand (and get an MWContext from).
  45.     
  46.     CMediatedWindow* topWin = NULL;        // find the top window to use the plugin in
  47.     CWindowIterator iter(WindowType_Any);
  48.     iter.Next(topWin);
  49.     
  50.     if (topWin == NULL
  51.     || ! (topWin->GetWindowType() == WindowType_Editor || topWin->GetWindowType() == WindowType_Compose) )
  52.         return NULL;
  53.     
  54.     CEditView *editView = (CEditView *)(topWin->FindPaneByID(CEditView::pane_ID));
  55.         
  56.     if (editView == NULL || editView->GetNSContext() == NULL)
  57.         return NULL;
  58.         
  59.     return editView->GetNSContext()->operator MWContext*();
  60. }
  61.  
  62.  
  63. // Processes:
  64. // 
  65. void CFontMenuAttachment::ExecuteSelf( MessageT inMessage, void* ioParam )
  66. {
  67.     mExecuteHost = FALSE;
  68.     switch ( inMessage )    
  69.     {
  70.         case msg_CommandStatus:
  71.         {
  72.             SCommandStatus*    status = (SCommandStatus*)ioParam;
  73.  
  74.             EDT_CharacterData* better;
  75.             
  76.             if ( status->command == cmd_FormatViewerFont || status->command == cmd_FormatFixedFont 
  77.                 || ( status->command >= FONT_MENU_BASE && status->command <= FONT_MENU_BASE_LAST ) )
  78.             {
  79.                 *(status->enabled) = true;
  80.                 *(status->usesMark) = false;
  81.  
  82.                 better = NULL;
  83.                 MWContext *cntxt = GetTopWindowContext();
  84.                 if ( cntxt )
  85.                     better = EDT_GetCharacterData( cntxt );
  86.                 if ( better == NULL )
  87.                 {
  88.                     *(status->enabled) = false;
  89.                     return;
  90.                 }
  91.             }
  92.             
  93.             switch ( status->command )
  94.             {
  95.                 case cmd_FormatViewerFont:
  96.                     *(status->usesMark) = ( ! ( better->values & TF_FIXED ) && !( better->values & TF_FONT_FACE ) );
  97.                     *(status->mark) = *(status->usesMark) ? checkMark : 0;
  98.                     *(status->usesMark) = true;
  99.                     
  100.                     EDT_FreeCharacterData(better);
  101.                     mExecuteHost = false;
  102.                      return;
  103.                     break;
  104.                 
  105.                 case cmd_FormatFixedFont:
  106.                     *(status->usesMark) = ( better->values & TF_FIXED ) != 0;
  107.                     *(status->mark) = *(status->usesMark) ? checkMark : 0;
  108.                     *(status->usesMark) = true;
  109.                     
  110.                     EDT_FreeCharacterData(better);
  111.                     mExecuteHost = false;
  112.                     return;
  113.                     break;
  114.                 
  115.                 default:
  116.                     if ( status->command >= FONT_MENU_BASE && status->command <= FONT_MENU_BASE_LAST )
  117.                     {
  118.                         // get font menu item
  119.                         Str255    fontItemString;
  120.                         fontItemString[0] = 0;
  121.                         MenuHandle menuh = GetMenu()->GetMacMenuH();
  122.                         ::GetMenuItemText ( menuh, status->command - FONT_MENU_BASE + PERM_FONT_ITEMS + 1, fontItemString );
  123.                         p2cstr( fontItemString );
  124.  
  125. // in mixed situation the mask bit will be cleared
  126.                         *(status->usesMark) = ( better->values & TF_FONT_FACE && better->pFontFace && XP_STRLEN((char *)fontItemString) > 0 
  127.                                                 && XP_STRSTR( better->pFontFace, (char *)fontItemString ) != NULL );
  128.                         *(status->mark) = *(status->usesMark) ? checkMark : 0;
  129.                         *(status->usesMark) = true;
  130.  
  131.                         EDT_FreeCharacterData(better);
  132.                         mExecuteHost = false;
  133.                         return;
  134.                     }
  135.             }
  136.         }
  137.         break;
  138.         
  139.         case cmd_FormatViewerFont:
  140.             MWContext *cntxt2 = GetTopWindowContext();
  141.             if ( cntxt2 )
  142.                 EDT_SetFontFace( cntxt2, NULL, 0, NULL );
  143.             break;
  144.         
  145.         case cmd_FormatFixedFont:
  146.             MWContext *cntxt3 = GetTopWindowContext();
  147.             if ( cntxt3 )
  148.                 EDT_SetFontFace( cntxt3, NULL, 1, NULL );
  149.             break;
  150.         
  151.         default:
  152.         {
  153.             if ( inMessage >= FONT_MENU_BASE && inMessage <= FONT_MENU_BASE_LAST )
  154.             {
  155.                 MWContext *cntxt2 = GetTopWindowContext();
  156.                 if ( cntxt2 )
  157.                 {
  158.                     // get font menu item
  159.                     Str255    newFontItemString;
  160.                     newFontItemString[0] = 0;
  161.                     MenuHandle menuh = GetMenu()->GetMacMenuH();
  162.                     ::GetMenuItemText ( menuh, inMessage - FONT_MENU_BASE + PERM_FONT_ITEMS + 1, newFontItemString );
  163.                     p2cstr( newFontItemString );
  164.                     
  165.                     EDT_SetFontFace( cntxt2, NULL, -1, (char *)newFontItemString );
  166.                     
  167.                     mExecuteHost = false;
  168.                     return;
  169.                  }
  170.             }
  171.         }
  172.         break;            
  173.     }
  174.     
  175.     mExecuteHost = TRUE;    // Let application handle it
  176. }
  177.  
  178. LMenu *CFontMenuAttachment::GetMenu()
  179. {
  180.     if (!sMenu)
  181.         sMenu = new LMenu(cFontMenuID);
  182.     
  183.     return sMenu;
  184. }
  185.  
  186. // build the font menu from the system
  187. void CFontMenuAttachment::UpdateMenu()
  188. {
  189.     if (!GetMenu() || !LMenuBar::GetCurrentMenuBar())
  190.         return;
  191.     
  192.     int i;
  193.     
  194.     // Ñ delete all the menu items after the separator line
  195.     MenuHandle menu = sMenu->GetMacMenuH();
  196.     if ( menu )
  197.     {
  198.         for ( i = ::CountMItems( menu ); i > PERM_FONT_ITEMS; i-- )
  199.             sMenu->RemoveItem( i );
  200.     }
  201.     
  202.     Try_
  203.     {
  204.         ThrowIfNil_( menu );
  205.     
  206.         // Add fonts to menu
  207.         ::InsertResMenu( menu, 'FONT', PERM_FONT_ITEMS );
  208.         
  209.         int    commandNum = FONT_MENU_BASE;
  210.         int newHowMany = ::CountMItems( menu );
  211.         for (i = PERM_FONT_ITEMS + 1; i <= newHowMany; i++ )
  212.             sMenu->SetCommand( i, commandNum++ );
  213.     }
  214.     Catch_( inErr )
  215.     {
  216.     }
  217.     EndCatch_
  218. }
  219.  
  220.  
  221. void CFontMenuAttachment::RemoveMenus()
  222. {
  223.     if (sMenu)
  224.     {
  225.         LMenuBar *currentMenuBar = LMenuBar::GetCurrentMenuBar();
  226.         if (currentMenuBar)
  227.             currentMenuBar->RemoveMenu(sMenu);
  228.     }
  229. }
  230.  
  231.  
  232. void CFontMenuAttachment::InstallMenus()
  233. {
  234.     if (sMenu)
  235.     {
  236.         LMenuBar *currentMenuBar = LMenuBar::GetCurrentMenuBar();
  237.         if (currentMenuBar)
  238.         {
  239.             StValueChanger<EDebugAction> okayToFail(gDebugThrow, debugAction_Nothing);
  240.             currentMenuBar->InstallMenu(sMenu, hierMenu);
  241.             
  242.             ResIDT resID;
  243.             MenuHandle menuh;
  244.             Int16 whichItem;
  245.             currentMenuBar->FindMenuItem( cmd_ID_toSearchFor, resID, menuh, whichItem );
  246.             if ( menuh )
  247.             {
  248.                 // make it hierarchical
  249.                 ::SetItemCmd( menuh, whichItem, hMenuCmd );
  250.                 ::SetItemMark( menuh, whichItem, menu_ID );
  251.             }
  252.         }
  253.     }
  254. }
  255.  
  256. #pragma mark -
  257.  
  258. CFontMenuPopup::CFontMenuPopup( LStream *inStream ) : CPatternButtonPopupText( inStream )
  259. {
  260. }
  261.  
  262. CFontMenuPopup::~CFontMenuPopup()
  263. {
  264. }
  265.  
  266. void CFontMenuPopup::FinishCreateSelf( void )
  267. {
  268.     CPatternButtonPopupText::FinishCreateSelf();
  269.  
  270.     int i;
  271.     
  272.     // Ñ delete all the menu items after the separator line
  273.     LMenu *ppmenu = GetMenu();
  274.     MenuHandle menuh = ppmenu ? ppmenu->GetMacMenuH() : NULL;
  275.     if ( menuh )
  276.     {
  277.         for ( i = ::CountMItems( menuh ); i > CFontMenuAttachment::PERM_FONT_ITEMS; i-- )
  278.             ppmenu->RemoveItem( i );
  279.     }
  280.     
  281.     Try_
  282.     {
  283.         ThrowIfNil_( menuh );
  284.     
  285.         // Add fonts to menu
  286.         ::InsertResMenu( menuh, 'FONT', CFontMenuAttachment::PERM_FONT_ITEMS );
  287.         
  288.         int    commandNum = FONT_MENU_BASE;
  289.         int newHowMany = ::CountMItems( menuh );
  290.         for (i = CFontMenuAttachment::PERM_FONT_ITEMS + 1; i <= newHowMany; i++ )
  291.             ppmenu->SetCommand( i, commandNum++ );
  292.         
  293.         SetMaxValue( newHowMany );
  294.     }
  295.     Catch_( inErr )
  296.     {
  297.     }
  298.     EndCatch_
  299. }
  300.