Back to index

lightning-sunbird  0.9+nobinonly
nsComboboxControlFrame.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Dean Tessman <dean_tessman@hotmail.com>
00024  *   Mats Palmgren <mats.palmgren@bredband.net>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef nsComboboxControlFrame_h___
00041 #define nsComboboxControlFrame_h___
00042 
00043 #ifdef DEBUG_evaughan
00044 //#define DEBUG_rods
00045 #endif
00046 
00047 #ifdef DEBUG_rods
00048 //#define DO_REFLOW_DEBUG
00049 //#define DO_REFLOW_COUNTER
00050 //#define DO_UNCONSTRAINED_CHECK
00051 //#define DO_PIXELS
00052 //#define DO_NEW_REFLOW
00053 #endif
00054 
00055 //Mark used to indicate when onchange has been fired for current combobox item
00056 #define NS_SKIP_NOTIFY_INDEX -2
00057 
00058 #include "nsAreaFrame.h"
00059 #include "nsIFormControlFrame.h"
00060 #include "nsIComboboxControlFrame.h"
00061 #include "nsVoidArray.h"
00062 #include "nsIAnonymousContentCreator.h"
00063 #include "nsISelectControlFrame.h"
00064 #include "nsIRollupListener.h"
00065 #include "nsPresState.h"
00066 #include "nsCSSFrameConstructor.h"
00067 #include "nsITextContent.h"
00068 #include "nsIScrollableViewProvider.h"
00069 #include "nsIStatefulFrame.h"
00070 #include "nsIDOMMouseListener.h"
00071 
00072 class nsIView;
00073 class nsStyleContext;
00074 class nsIListControlFrame;
00075 class nsIScrollableView;
00076 
00081 #define NS_COMBO_FRAME_POPUP_LIST_INDEX   (NS_BLOCK_FRAME_ABSOLUTE_LIST_INDEX + 1)
00082 
00083 class nsComboboxControlFrame : public nsAreaFrame,
00084                                public nsIFormControlFrame,
00085                                public nsIComboboxControlFrame,
00086                                public nsIAnonymousContentCreator,
00087                                public nsISelectControlFrame,
00088                                public nsIRollupListener,
00089                                public nsIScrollableViewProvider,
00090                                public nsIStatefulFrame
00091 {
00092 public:
00093   friend nsresult NS_NewComboboxControlFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame, PRUint32 aFlags);
00094   friend class RedisplayTextEvent;
00095 
00096   nsComboboxControlFrame();
00097   ~nsComboboxControlFrame();
00098 
00099    // nsISupports
00100   NS_IMETHOD QueryInterface(const nsIID& aIID, void** aInstancePtr);
00101   
00102    // nsIAnonymousContentCreator
00103   NS_IMETHOD CreateAnonymousContent(nsPresContext* aPresContext,
00104                                     nsISupportsArray& aChildList);
00105   NS_IMETHOD CreateFrameFor(nsPresContext*   aPresContext,
00106                             nsIContent *      aContent,
00107                             nsIFrame**        aFrame);
00108 
00109 #ifdef ACCESSIBILITY
00110   NS_IMETHOD GetAccessible(nsIAccessible** aAccessible);
00111 #endif
00112 
00113    // nsIFrame
00114   NS_IMETHOD Init(nsPresContext*  aPresContext,
00115               nsIContent*      aContent,
00116               nsIFrame*        aParent,
00117               nsStyleContext*  aContext,
00118               nsIFrame*        aPrevInFlow);
00119 
00120   NS_IMETHOD Reflow(nsPresContext*          aCX,
00121                     nsHTMLReflowMetrics&     aDesiredSize,
00122                     const nsHTMLReflowState& aReflowState,
00123                     nsReflowStatus&          aStatus);
00124 
00125   NS_IMETHOD HandleEvent(nsPresContext* aPresContext,
00126                          nsGUIEvent* aEvent,
00127                          nsEventStatus* aEventStatus);
00128 
00129   NS_IMETHOD Paint(nsPresContext*      aPresContext,
00130                    nsIRenderingContext& aRenderingContext,
00131                    const nsRect&        aDirtyRect,
00132                    nsFramePaintLayer    aWhichLayer,
00133                    PRUint32             aFlags = 0);
00134 #ifdef NS_DEBUG
00135   NS_IMETHOD GetFrameName(nsAString& aResult) const;
00136 #endif
00137   NS_IMETHOD Destroy(nsPresContext* aPresContext);
00138   virtual nsIFrame* GetFirstChild(nsIAtom* aListName) const;
00139   NS_IMETHOD SetInitialChildList(nsPresContext* aPresContext,
00140                                nsIAtom*        aListName,
00141                                nsIFrame*       aChildList);
00142   virtual nsIAtom* GetAdditionalChildListName(PRInt32 aIndex) const;
00143 
00144   NS_IMETHOD GetFrameForPoint(const nsPoint& aPoint, nsFramePaintLayer aWhichLayer, nsIFrame** aFrame);
00145 
00146   virtual nsIFrame* GetContentInsertionFrame();
00147 
00148      // nsIFormControlFrame
00149   NS_IMETHOD SetSuggestedSize(nscoord aWidth, nscoord aHeight);
00150   NS_IMETHOD GetName(nsAString* aName);
00151   NS_IMETHOD_(PRInt32) GetFormControlType() const;
00152   NS_IMETHOD SetProperty(nsPresContext* aPresContext, nsIAtom* aName, const nsAString& aValue);
00153   NS_IMETHOD GetProperty(nsIAtom* aName, nsAString& aValue); 
00162   void       SetFocus(PRBool aOn, PRBool aRepaint);
00163   void       ScrollIntoView(nsPresContext* aPresContext);
00164   virtual void InitializeControl(nsPresContext* aPresContext);
00165   NS_IMETHOD OnContentReset();
00166   NS_IMETHOD GetFormContent(nsIContent*& aContent) const;
00167   virtual nscoord GetVerticalBorderWidth(float aPixToTwip) const;
00168   virtual nscoord GetHorizontalBorderWidth(float aPixToTwip) const;
00169   virtual nscoord GetVerticalInsidePadding(nsPresContext* aPresContext,
00170                                            float aPixToTwip,
00171                                            nscoord aInnerHeight) const;
00172   virtual nscoord GetHorizontalInsidePadding(nsPresContext* aPresContext,
00173                                              float aPixToTwip, 
00174                                              nscoord aInnerWidth,
00175                                              nscoord aCharWidth) const;
00176 
00177   //nsIComboboxControlFrame
00178   NS_IMETHOD IsDroppedDown(PRBool * aDoDropDown) { *aDoDropDown = mDroppedDown; return NS_OK; }
00182   NS_IMETHOD ShowDropDown(PRBool aDoDropDown);
00183   NS_IMETHOD GetDropDown(nsIFrame** aDropDownFrame);
00184   NS_IMETHOD SetDropDown(nsIFrame* aDropDownFrame);
00188   NS_IMETHOD RollupFromList(nsPresContext* aPresContext);
00189   NS_IMETHOD AbsolutelyPositionDropDown();
00190   NS_IMETHOD GetAbsoluteRect(nsRect* aRect);
00191   NS_IMETHOD GetIndexOfDisplayArea(PRInt32* aSelectedIndex);
00192   NS_IMETHOD RedisplaySelectedText();
00193   NS_IMETHOD_(PRInt32) UpdateRecentIndex(PRInt32 aIndex);
00194 
00195   // nsISelectControlFrame
00196   NS_IMETHOD AddOption(nsPresContext* aPresContext, PRInt32 index);
00197   NS_IMETHOD RemoveOption(nsPresContext* aPresContext, PRInt32 index);
00198   NS_IMETHOD GetOptionSelected(PRInt32 aIndex, PRBool* aValue);
00199   NS_IMETHOD DoneAddingChildren(PRBool aIsDone);
00200   NS_IMETHOD OnOptionSelected(nsPresContext* aPresContext,
00201                               PRInt32 aIndex,
00202                               PRBool aSelected);
00203   NS_IMETHOD GetDummyFrame(nsIFrame** aFrame);
00204   NS_IMETHOD SetDummyFrame(nsIFrame* aFrame);
00205   NS_IMETHOD OnSetSelectedIndex(PRInt32 aOldIndex, PRInt32 aNewIndex);
00206 
00207   //nsIRollupListener
00212   NS_IMETHOD Rollup();
00217   NS_IMETHOD ShouldRollupOnMouseWheelEvent(PRBool *aShouldRollup)
00218     { *aShouldRollup = PR_TRUE; return NS_OK;}
00219 
00224   NS_IMETHOD ShouldRollupOnMouseActivate(PRBool *aShouldRollup)
00225     { *aShouldRollup = PR_FALSE; return NS_OK;}
00226 
00227   // nsIScrollableViewProvider
00228   virtual nsIScrollableView* GetScrollableView();
00229 
00230   //nsIStatefulFrame
00231   NS_IMETHOD SaveState(nsPresContext* aPresContext, nsPresState** aState);
00232   NS_IMETHOD RestoreState(nsPresContext* aPresContext, nsPresState* aState);
00233 
00234   static PRBool ToolkitHasNativePopup();
00235 
00236 protected:
00237 
00238   NS_IMETHOD CreateDisplayFrame(nsPresContext* aPresContext);
00239 
00240 #ifdef DO_NEW_REFLOW
00241   NS_IMETHOD ReflowItems(nsPresContext* aPresContext,
00242                          const nsHTMLReflowState& aReflowState,
00243                          nsHTMLReflowMetrics& aDesiredSize);
00244 #endif
00245 
00246    // nsHTMLContainerFrame
00247   virtual PRIntn GetSkipSides() const;
00248 
00249    // Utilities
00250   nsresult ReflowComboChildFrame(nsIFrame*           aFrame, 
00251                             nsPresContext*          aPresContext, 
00252                             nsHTMLReflowMetrics&     aDesiredSize,
00253                             const nsHTMLReflowState& aReflowState, 
00254                             nsReflowStatus&          aStatus,
00255                             nscoord                  aAvailableWidth,
00256                             nscoord                  aAvailableHeight);
00257 
00258 public:
00259   nsresult PositionDropdown(nsPresContext* aPresContext,
00260                             nscoord aHeight, 
00261                             nsRect aAbsoluteTwipsRect, 
00262                             nsRect aAbsolutePixelRect);
00263 protected:
00268   void ShowPopup(PRBool aShowPopup);
00269 
00276   PRBool ShowList(nsPresContext* aPresContext, PRBool aShowList);
00277   void SetChildFrameSize(nsIFrame* aFrame, nscoord aWidth, nscoord aHeight);
00278   void CheckFireOnChange();
00279   void FireValueChangeEvent();
00280   nsresult RedisplayText(PRInt32 aIndex);
00281   void HandleRedisplayTextEvent();
00282   void ActuallyDisplayText(PRBool aNotify);
00283   nsresult GetPrimaryComboFrame(nsPresContext* aPresContext, nsIContent* aContent, nsIFrame** aFrame);
00284 
00285   void ReflowCombobox(nsPresContext *         aPresContext,
00286                       const nsHTMLReflowState& aReflowState,
00287                       nsHTMLReflowMetrics&     aDesiredSize,
00288                       nsReflowStatus&          aStatus,
00289                       nsIFrame *               aDisplayFrame,
00290                       nsIFrame *               aDropDownBtn,
00291                       nscoord&                 aDisplayWidth,
00292                       nscoord                  aBtnWidth,
00293                       const nsMargin&          aBorderPadding,
00294                       nscoord                  aFallBackHgt = -1,
00295                       PRBool                   aCheckHeight = PR_FALSE);
00296 
00297   nsFrameList              mPopupFrames;             // additional named child list
00298   nsPresContext*          mPresContext;             // XXX: Remove the need to cache the pres context.
00299   nsCOMPtr<nsITextContent> mDisplayContent;          // Anonymous content used to display the current selection
00300   nsIFrame*                mDisplayFrame;            // frame to display selection
00301   nsIFrame*                mButtonFrame;             // button frame
00302   nsIFrame*                mDropdownFrame;           // dropdown list frame
00303   nsIFrame*                mTextFrame;               // display area frame
00304   nsIListControlFrame *    mListControlFrame;        // ListControl Interface for the dropdown frame
00305 
00306   // Resize Reflow Optimization
00307   nsSize                mCacheSize;
00308   nsSize                mCachedAvailableSize;
00309   nscoord               mCachedMaxElementWidth;
00310   nscoord               mCachedAscent;
00311 
00312   nsSize                mCachedUncDropdownSize;
00313   nsSize                mCachedUncComboSize;
00314 
00315   nscoord               mItemDisplayWidth;
00316   //nscoord               mItemDisplayHeight;
00317 
00318   PRPackedBool          mDroppedDown;             // Current state of the dropdown list, PR_TRUE is dropped down
00319   PRPackedBool          mGoodToGo;
00320   PRPackedBool          mInRedisplayText;
00321   PRPackedBool          mRedisplayTextEventPosted;
00322 
00323   PRInt32               mRecentSelectedIndex;
00324   PRInt32               mDisplayedIndex;
00325   nsString              mDisplayedOptionText;
00326 
00327   // make someone to listen to the button. If its programmatically pressed by someone like Accessibility
00328   // then open or close the combo box.
00329   nsCOMPtr<nsIDOMMouseListener> mButtonListener;
00330 
00331   nsCOMPtr<nsIEventQueueService> mEventQueueService;
00332 
00333   // static class data member for Bug 32920
00334   // only one control can be focused at a time
00335   static nsComboboxControlFrame * mFocused;
00336 
00337 #ifdef DO_REFLOW_COUNTER
00338   PRInt32 mReflowId;
00339 #endif
00340 
00341 private:
00342   NS_IMETHOD_(nsrefcnt) AddRef() { return NS_OK; }
00343   NS_IMETHOD_(nsrefcnt) Release() { return NS_OK; }
00344 };
00345 
00346 #endif
00347 
00348