1/* 2 * Copyright 2011 Stephan A��mus <superstippi@gmx.de> 3 * All rights reserved. Distributed under the terms of the MIT license. 4 */ 5#include "ToolBarView.h" 6 7#include <ControlLook.h> 8#include <IconButton.h> 9#include <Message.h> 10#include <SeparatorView.h> 11#include <SpaceLayoutItem.h> 12 13 14ToolBarView::ToolBarView(BRect frame) 15 : 16 BGroupView(B_HORIZONTAL) 17{ 18 float inset = ceilf(be_control_look->DefaultItemSpacing() / 2); 19 GroupLayout()->SetInsets(inset, 2, inset, 3); 20 GroupLayout()->SetSpacing(inset); 21 22 SetFlags(Flags() | B_FRAME_EVENTS | B_PULSE_NEEDED); 23 24 MoveTo(frame.LeftTop()); 25 ResizeTo(frame.Width(), frame.Height()); 26 SetResizingMode(B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP); 27} 28 29 30ToolBarView::~ToolBarView() 31{ 32} 33 34 35void 36ToolBarView::Hide() 37{ 38 BView::Hide(); 39 // TODO: This could be fixed in BView instead. Looking from the 40 // BIconButtons, they are not hidden though, only their parent is... 41 _HideToolTips(); 42} 43 44 45void 46ToolBarView::AddAction(uint32 command, BHandler* target, const BBitmap* icon, 47 const char* toolTipText) 48{ 49 AddAction(new BMessage(command), target, icon, toolTipText); 50} 51 52 53void 54ToolBarView::AddAction(BMessage* message, BHandler* target, 55 const BBitmap* icon, const char* toolTipText) 56{ 57 BIconButton* button = new BIconButton(NULL, NULL, message, target); 58 button->SetIcon(icon); 59 if (toolTipText != NULL) 60 button->SetToolTip(toolTipText); 61 _AddView(button); 62} 63 64 65void 66ToolBarView::AddSeparator() 67{ 68 _AddView(new BSeparatorView(B_VERTICAL, B_PLAIN_BORDER)); 69} 70 71 72void 73ToolBarView::AddGlue() 74{ 75 GroupLayout()->AddItem(BSpaceLayoutItem::CreateGlue()); 76} 77 78 79void 80ToolBarView::SetActionEnabled(uint32 command, bool enabled) 81{ 82 if (BIconButton* button = _FindIconButton(command)) 83 button->SetEnabled(enabled); 84} 85 86 87void 88ToolBarView::SetActionPressed(uint32 command, bool pressed) 89{ 90 if (BIconButton* button = _FindIconButton(command)) 91 button->SetPressed(pressed); 92} 93 94 95void 96ToolBarView::SetActionVisible(uint32 command, bool visible) 97{ 98 BIconButton* button = _FindIconButton(command); 99 if (button == NULL) 100 return; 101 for (int32 i = 0; BLayoutItem* item = GroupLayout()->ItemAt(i); i++) { 102 if (item->View() != button) 103 continue; 104 item->SetVisible(visible); 105 break; 106 } 107} 108 109 110void 111ToolBarView::Pulse() 112{ 113 // TODO: Perhaps this could/should be addressed in BView instead. 114 if (IsHidden()) 115 _HideToolTips(); 116} 117 118 119void 120ToolBarView::FrameResized(float width, float height) 121{ 122 // TODO: There seems to be a bug in app_server which does not 123 // correctly trigger invalidation of views which are shown, when 124 // the resulting dirty area is somehow already part of an update region. 125 Invalidate(); 126} 127 128 129void 130ToolBarView::_AddView(BView* view) 131{ 132 GroupLayout()->AddView(view); 133} 134 135 136BIconButton* 137ToolBarView::_FindIconButton(uint32 command) const 138{ 139 for (int32 i = 0; BView* view = ChildAt(i); i++) { 140 BIconButton* button = dynamic_cast<BIconButton*>(view); 141 if (button == NULL) 142 continue; 143 BMessage* message = button->Message(); 144 if (message == NULL) 145 continue; 146 if (message->what == command) { 147 return button; 148 // Assumes there is only one button with this message... 149 break; 150 } 151 } 152 return NULL; 153} 154 155 156void 157ToolBarView::_HideToolTips() const 158{ 159 for (int32 i = 0; BView* view = ChildAt(i); i++) 160 view->HideToolTip(); 161} 162 163