home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / xfe / src / FrameListMenu.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.0 KB  |  269 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. /*                                                                        */
  20. /* Name:        FrameListMenu.cpp                                        */
  21. /* Description:    XFE_FrameListMenu component implementation.                */
  22. /*                These are the menu items that appear at the end of the    */
  23. /*                                                                        */
  24. /* Author:        Ramiro Estrugo <ramiro@netscape.com>                    */
  25.  
  26.  
  27. /* Date:        Sun Mar  2 01:34:13 PST 1997                            */
  28. /*                                                                        */
  29. /*----------------------------------------------------------------------*/
  30.  
  31. #include "FrameListMenu.h"
  32. #include "MozillaApp.h"
  33. #include "felocale.h"
  34. #include "intl_csi.h"
  35.  
  36. #include <Xm/PushBG.h>
  37. #include <Xm/ToggleBG.h>
  38. #include <Xfe/Xfe.h>
  39.  
  40. #define MAX_ITEM_WIDTH 40
  41.  
  42. //////////////////////////////////////////////////////////////////////////
  43. XFE_FrameListMenu::XFE_FrameListMenu(Widget w, XFE_Frame * frame)
  44. {
  45.     m_cascade = w;
  46.     m_parentFrame = frame;
  47.  
  48.     XP_ASSERT( m_parentFrame != NULL );
  49.     
  50.     XtAddCallback(m_cascade,
  51.                   XmNdestroyCallback,
  52.                   &XFE_FrameListMenu::destroy_cb,
  53.                   (XtPointer) this);
  54.     
  55.     XtAddCallback(m_cascade,
  56.                   XmNcascadingCallback,
  57.                   &XFE_FrameListMenu::cascading_cb,
  58.                   (XtPointer) this);
  59.     
  60.     XtVaGetValues(m_cascade,XmNsubMenuId,&m_submenu,NULL);
  61.     
  62.     XtVaGetValues(m_submenu,XmNnumChildren,&m_firstslot,NULL);
  63.     
  64.     XP_ASSERT(m_submenu);
  65. }
  66. //////////////////////////////////////////////////////////////////////////
  67. XFE_FrameListMenu::~XFE_FrameListMenu()
  68. {
  69. }
  70. //////////////////////////////////////////////////////////////////////////
  71. void
  72. XFE_FrameListMenu::generate(Widget cascade, XtPointer, XFE_Frame *frame)
  73. {
  74.     XFE_FrameListMenu *obj;
  75.     
  76.     obj = new XFE_FrameListMenu(cascade, frame);
  77. }
  78. //////////////////////////////////////////////////////////////////////////
  79. void
  80. XFE_FrameListMenu::cascading()
  81. {
  82.     XP_List *    frame_list = getShownFrames();
  83.  
  84.     XFE_Frame *    frame;
  85.     int            i;
  86.     int            frame_count = XP_ListCount(frame_list);
  87.  
  88.     Cardinal    num_children;
  89.     WidgetList    children;
  90.  
  91.     int            total_slots_needed;
  92.     int            slots_to_add;
  93.     int            count;
  94.  
  95.     XfeChildrenGet(m_submenu,&children,&num_children);
  96.  
  97.     XP_ASSERT( num_children > 1 );
  98.  
  99.     // Total number of slots needed
  100.     total_slots_needed = m_firstslot + 1 + frame_count;
  101.  
  102.     // Number of slots to add
  103.     slots_to_add = total_slots_needed - num_children;
  104.  
  105.     // Add more slots if needed
  106.     if (slots_to_add > 0)
  107.     {
  108.         for (i = 0; i < slots_to_add; i++)
  109.         {
  110.             Widget item = XtVaCreateWidget(xfeCmdWindowListRaiseItem,
  111.                                            //xmToggleButtonGadgetClass,
  112.                                            xmPushButtonGadgetClass,
  113.                                            m_submenu,
  114.                                            NULL);
  115.  
  116.             XtAddCallback(item,
  117.                           XmNactivateCallback,
  118.                           //XmNvalueChangedCallback,
  119.                           &XFE_FrameListMenu::item_activate_cb,
  120.                           (XtPointer) this);
  121.         }
  122.  
  123.         // Update num_slots, since we added stuff
  124.         XfeChildrenGet(m_submenu,&children,&num_children);
  125.     }
  126.  
  127.     count = 1;
  128.  
  129.     // Configure the items
  130.     for (i = (int) m_firstslot + 1; i < (int) num_children; i++)
  131.     {
  132.         // Get the next frame
  133.         frame = (XFE_Frame*) XP_ListNextObject(frame_list);
  134.  
  135.         // If the frame is valid, add its title to the slot buttons
  136.         if (frame)
  137.         {
  138.             MWContext *            context = m_parentFrame->getContext();
  139.             INTL_CharSetInfo    c = LO_GetDocumentCharacterSetInfo(context);
  140.             XmFontList            font_list;
  141.             char                name[1024];
  142.  
  143.             XP_SPRINTF(name,"%d. %s",count++,frame->getTitle());
  144.  
  145.             INTL_MidTruncateString(INTL_GetCSIWinCSID(c), 
  146.                                    name, 
  147.                                    name,
  148.                                    MAX_ITEM_WIDTH);
  149.  
  150.             XmString label = fe_ConvertToXmString((unsigned char *) name,
  151.                                                   INTL_GetCSIWinCSID(c), 
  152.                                                   NULL, 
  153.                                                   XmFONT_IS_FONT,
  154.                                                   &font_list);
  155.  
  156.             if (label)
  157.             {
  158.                 XtVaSetValues(children[i],XmNlabelString,label,NULL);
  159.  
  160.                 XmStringFree(label);
  161.             }
  162.  
  163.             XtManageChild(children[i]);
  164.         }
  165.         // If the frame is not valid, the unmanage the slot button
  166.         else
  167.         {
  168.             XtUnmanageChild(children[i]);
  169.         }
  170.     }
  171.  
  172.     // Update the display so that the gadget buttons get drawn
  173.     XmUpdateDisplay(m_submenu);
  174.  
  175.     if (frame_list)
  176.     {
  177.         XP_ListDestroy(frame_list);
  178.     }
  179. }
  180. //////////////////////////////////////////////////////////////////////////
  181. void
  182. XFE_FrameListMenu::item_activate(Widget item)
  183. {
  184.     XP_ASSERT( XfeIsAlive(m_submenu) );
  185.  
  186.     int i = XfeChildGetIndex(item) - m_firstslot;
  187.  
  188.     XP_ASSERT( i > 0 );
  189.  
  190.     if (i <= 0)
  191.     {
  192.         return;
  193.     }
  194.  
  195.     XP_List * frame_list = getShownFrames();
  196.     
  197.     XFE_Frame * frame = (XFE_Frame *) XP_ListGetObjectNum(frame_list,i);
  198.     
  199.     XP_ASSERT( frame != NULL );
  200.     
  201.     if (frame)
  202.     {
  203.         frame->show();
  204.     }
  205.  
  206.     XmUpdateDisplay(frame->getBaseWidget());
  207.  
  208.     if (frame_list)
  209.     {
  210.         XP_ListDestroy(frame_list);
  211.     }
  212. }
  213. //////////////////////////////////////////////////////////////////////////
  214. void
  215. XFE_FrameListMenu::destroy_cb(Widget, XtPointer clientData, XtPointer)
  216. {
  217.   XFE_FrameListMenu *obj = (XFE_FrameListMenu*)clientData;
  218.  
  219.   delete obj;
  220. }
  221. //////////////////////////////////////////////////////////////////////////
  222. void
  223. XFE_FrameListMenu::cascading_cb(Widget, XtPointer clientData, XtPointer)
  224. {
  225.   XFE_FrameListMenu *obj = (XFE_FrameListMenu*)clientData;
  226.  
  227.   obj->cascading();
  228. }
  229. //////////////////////////////////////////////////////////////////////////
  230. void
  231. XFE_FrameListMenu::item_activate_cb(Widget item, XtPointer clientData, XtPointer)
  232. {
  233.     XFE_FrameListMenu *obj = (XFE_FrameListMenu*)clientData;
  234.  
  235.     obj->item_activate(item);
  236. }
  237. //////////////////////////////////////////////////////////////////////////
  238. XP_List *
  239. XFE_FrameListMenu::getShownFrames()
  240. {
  241.     XP_List *    frame_list = XFE_MozillaApp::theApp()->getAllFrameList();
  242.     Cardinal    frame_count = XP_ListCount(frame_list);
  243.  
  244.     XP_List *    shown_frame_list = NULL;
  245.     Cardinal    i;
  246.  
  247.     // Find the shown frames and add them to a list
  248.     for (i = 0; i < frame_count; i++)
  249.     {
  250.         // Get the next frame
  251.         XFE_Frame * frame = (XFE_Frame*) XP_ListNextObject(frame_list);
  252.  
  253.         // Add it to list if valid and shown
  254.         if (frame && XfeIsAlive(frame->getBaseWidget()) && frame->isShown())
  255.         {
  256.             // Create a new list as soon as we find the first shown item
  257.             if (!shown_frame_list)
  258.             {
  259.                 shown_frame_list = XP_ListNew();
  260.             }
  261.  
  262.             XP_ListAddObject(shown_frame_list,frame);
  263.         }
  264.     }
  265.  
  266.     return shown_frame_list;
  267. }
  268. //////////////////////////////////////////////////////////////////////////
  269.