michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.menu; michael@0: michael@0: import org.mozilla.gecko.R; michael@0: michael@0: import android.content.Context; michael@0: import android.content.res.Resources; michael@0: import android.graphics.Rect; michael@0: import android.graphics.drawable.Drawable; michael@0: import android.util.AttributeSet; michael@0: import android.widget.TextView; michael@0: michael@0: public class MenuItemDefault extends TextView michael@0: implements GeckoMenuItem.Layout { michael@0: private static final int[] STATE_MORE = new int[] { R.attr.state_more }; michael@0: private static final int[] STATE_CHECKED = new int[] { android.R.attr.state_checkable, android.R.attr.state_checked }; michael@0: private static final int[] STATE_UNCHECKED = new int[] { android.R.attr.state_checkable }; michael@0: michael@0: private Drawable mIcon; michael@0: private Drawable mState; michael@0: private static Rect sIconBounds; michael@0: michael@0: private boolean mCheckable = false; michael@0: private boolean mChecked = false; michael@0: private boolean mHasSubMenu = false; michael@0: private boolean mShowIcon = false; michael@0: michael@0: public MenuItemDefault(Context context) { michael@0: this(context, null); michael@0: } michael@0: michael@0: public MenuItemDefault(Context context, AttributeSet attrs) { michael@0: this(context, attrs, R.attr.menuItemDefaultStyle); michael@0: } michael@0: michael@0: public MenuItemDefault(Context context, AttributeSet attrs, int defStyle) { michael@0: super(context, attrs, defStyle); michael@0: michael@0: Resources res = getResources(); michael@0: int width = res.getDimensionPixelSize(R.dimen.menu_item_row_width); michael@0: int height = res.getDimensionPixelSize(R.dimen.menu_item_row_height); michael@0: setMinimumWidth(width); michael@0: setMinimumHeight(height); michael@0: michael@0: int stateIconSize = res.getDimensionPixelSize(R.dimen.menu_item_state_icon); michael@0: Rect stateIconBounds = new Rect(0, 0, stateIconSize, stateIconSize); michael@0: michael@0: mState = res.getDrawable(R.drawable.menu_item_state).mutate(); michael@0: mState.setBounds(stateIconBounds); michael@0: michael@0: if (sIconBounds == null) { michael@0: int iconSize = res.getDimensionPixelSize(R.dimen.menu_item_icon); michael@0: sIconBounds = new Rect(0, 0, iconSize, iconSize); michael@0: } michael@0: michael@0: setCompoundDrawables(mIcon, null, mState, null); michael@0: } michael@0: michael@0: @Override michael@0: public int[] onCreateDrawableState(int extraSpace) { michael@0: final int[] drawableState = super.onCreateDrawableState(extraSpace + 2); michael@0: michael@0: if (mHasSubMenu) michael@0: mergeDrawableStates(drawableState, STATE_MORE); michael@0: else if (mCheckable && mChecked) michael@0: mergeDrawableStates(drawableState, STATE_CHECKED); michael@0: else if (mCheckable && !mChecked) michael@0: mergeDrawableStates(drawableState, STATE_UNCHECKED); michael@0: michael@0: return drawableState; michael@0: } michael@0: michael@0: @Override michael@0: public void initialize(GeckoMenuItem item) { michael@0: if (item == null) michael@0: return; michael@0: michael@0: setTitle(item.getTitle()); michael@0: setIcon(item.getIcon()); michael@0: setEnabled(item.isEnabled()); michael@0: setCheckable(item.isCheckable()); michael@0: setChecked(item.isChecked()); michael@0: setSubMenuIndicator(item.hasSubMenu()); michael@0: } michael@0: michael@0: private void refreshIcon() { michael@0: setCompoundDrawables(mShowIcon ? mIcon : null, null, mState, null); michael@0: } michael@0: michael@0: void setIcon(Drawable icon) { michael@0: mIcon = icon; michael@0: michael@0: if (mIcon != null) { michael@0: mIcon.setBounds(sIconBounds); michael@0: mIcon.setAlpha(isEnabled() ? 255 : 99); michael@0: } michael@0: michael@0: refreshIcon(); michael@0: } michael@0: michael@0: void setIcon(int icon) { michael@0: setIcon((icon == 0) ? null : getResources().getDrawable(icon)); michael@0: } michael@0: michael@0: void setTitle(CharSequence title) { michael@0: setText(title); michael@0: } michael@0: michael@0: @Override michael@0: public void setEnabled(boolean enabled) { michael@0: super.setEnabled(enabled); michael@0: michael@0: if (mIcon != null) michael@0: mIcon.setAlpha(enabled ? 255 : 99); michael@0: michael@0: if (mState != null) michael@0: mState.setAlpha(enabled ? 255 : 99); michael@0: } michael@0: michael@0: private void setCheckable(boolean checkable) { michael@0: if (mCheckable != checkable) { michael@0: mCheckable = checkable; michael@0: refreshDrawableState(); michael@0: } michael@0: } michael@0: michael@0: private void setChecked(boolean checked) { michael@0: if (mChecked != checked) { michael@0: mChecked = checked; michael@0: refreshDrawableState(); michael@0: } michael@0: } michael@0: michael@0: @Override michael@0: public void setShowIcon(boolean show) { michael@0: if (mShowIcon != show) { michael@0: mShowIcon = show; michael@0: refreshIcon(); michael@0: } michael@0: } michael@0: michael@0: void setSubMenuIndicator(boolean hasSubMenu) { michael@0: if (mHasSubMenu != hasSubMenu) { michael@0: mHasSubMenu = hasSubMenu; michael@0: refreshDrawableState(); michael@0: } michael@0: } michael@0: }