| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: toolbar.cpp |
| 3 | // Purpose: wxToolBar |
| 4 | // Author: AUTHOR |
| 5 | // Modified by: |
| 6 | // Created: 04/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) AUTHOR |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "toolbar.h" |
| 14 | #endif |
| 15 | |
| 16 | #include "wx/wx.h" |
| 17 | |
| 18 | #if wxUSE_TOOLBAR |
| 19 | |
| 20 | #include "wx/toolbar.h" |
| 21 | |
| 22 | IMPLEMENT_DYNAMIC_CLASS(wxToolBar, wxToolBarBase) |
| 23 | |
| 24 | BEGIN_EVENT_TABLE(wxToolBar, wxToolBarBase) |
| 25 | END_EVENT_TABLE() |
| 26 | |
| 27 | #include <wx/mac/uma.h> |
| 28 | |
| 29 | wxToolBar::wxToolBar() |
| 30 | { |
| 31 | m_maxWidth = -1; |
| 32 | m_maxHeight = -1; |
| 33 | m_defaultWidth = 24; |
| 34 | m_defaultHeight = 22; |
| 35 | // TODO |
| 36 | } |
| 37 | |
| 38 | bool wxToolBar::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, |
| 39 | long style, const wxString& name) |
| 40 | { |
| 41 | m_maxWidth = m_maxHeight = 0; |
| 42 | |
| 43 | m_defaultWidth = 24; |
| 44 | m_defaultHeight = 22; |
| 45 | |
| 46 | int x = pos.x; |
| 47 | int y = pos.y; |
| 48 | int width = size.x; |
| 49 | int height = size.y; |
| 50 | |
| 51 | if (width <= 0) |
| 52 | width = 100; |
| 53 | if (height <= 0) |
| 54 | height = 30; |
| 55 | if (x < 0) |
| 56 | x = 0; |
| 57 | if (y < 0) |
| 58 | y = 0; |
| 59 | |
| 60 | Rect bounds ; |
| 61 | Str255 title ; |
| 62 | |
| 63 | MacPreControlCreate( parent , id , "" , wxPoint( x , y ) , wxSize( width , height ) ,style, *((wxValidator*)NULL) , name , &bounds , title ) ; |
| 64 | |
| 65 | m_macControl = UMANewControl( parent->GetMacRootWindow() , &bounds , "\p" , true , 0 , 0 , 1, |
| 66 | kControlPlacardProc , (long) this ) ; |
| 67 | MacPostControlCreate() ; |
| 68 | |
| 69 | return TRUE; |
| 70 | } |
| 71 | |
| 72 | wxToolBar::~wxToolBar() |
| 73 | { |
| 74 | // TODO |
| 75 | } |
| 76 | |
| 77 | PicHandle MakePict(GWorldPtr wp) |
| 78 | { |
| 79 | CGrafPtr origPort ; |
| 80 | GDHandle origDev ; |
| 81 | |
| 82 | PicHandle pict; // this is the Picture we give back |
| 83 | |
| 84 | RGBColor gray = { 0xCCCC ,0xCCCC , 0xCCCC } ; |
| 85 | |
| 86 | GetGWorld( &origPort , &origDev ) ; |
| 87 | SetGWorld( wp , NULL ) ; |
| 88 | |
| 89 | pict = OpenPicture(&wp->portRect); // open a picture, this disables drawing |
| 90 | if(!pict) |
| 91 | return NULL; |
| 92 | |
| 93 | RGBBackColor( &gray ) ; |
| 94 | EraseRect(&wp->portRect) ; |
| 95 | CopyBits((BitMap*)*wp->portPixMap, // src PixMap - we copy image over itself - |
| 96 | (BitMap*)*wp->portPixMap, // dst PixMap - no drawing occurs - |
| 97 | &wp->portRect, // srcRect - it will be recorded and compressed - |
| 98 | &wp->portRect, // dstRect - into the picture that is open - |
| 99 | srcCopy,NULL); // copyMode and no clip region |
| 100 | |
| 101 | ClosePicture(); // We are done recording the picture |
| 102 | SetGWorld( origPort , origDev ) ; |
| 103 | return pict; // return our groovy pict handle |
| 104 | } |
| 105 | |
| 106 | PicHandle MakePictWhite(GWorldPtr wp) |
| 107 | { |
| 108 | CGrafPtr origPort ; |
| 109 | GDHandle origDev ; |
| 110 | |
| 111 | PicHandle pict; // this is the Picture we give back |
| 112 | |
| 113 | RGBColor white = { 0xFFFF ,0xFFFF , 0xFFFF } ; |
| 114 | |
| 115 | GetGWorld( &origPort , &origDev ) ; |
| 116 | SetGWorld( wp , NULL ) ; |
| 117 | |
| 118 | pict = OpenPicture(&wp->portRect); // open a picture, this disables drawing |
| 119 | if(!pict) |
| 120 | return NULL; |
| 121 | |
| 122 | RGBBackColor( &white ) ; |
| 123 | EraseRect(&wp->portRect) ; |
| 124 | CopyBits((BitMap*)*wp->portPixMap, // src PixMap - we copy image over itself - |
| 125 | (BitMap*)*wp->portPixMap, // dst PixMap - no drawing occurs - |
| 126 | &wp->portRect, // srcRect - it will be recorded and compressed - |
| 127 | &wp->portRect, // dstRect - into the picture that is open - |
| 128 | srcCopy,NULL); // copyMode and no clip region |
| 129 | |
| 130 | ClosePicture(); // We are done recording the picture |
| 131 | SetGWorld( origPort , origDev ) ; |
| 132 | return pict; // return our groovy pict handle |
| 133 | } |
| 134 | |
| 135 | const short kwxMacToolBarTopMargin = 2 ; |
| 136 | const short kwxMacToolBarLeftMargin = 2 ; |
| 137 | |
| 138 | bool wxToolBar::CreateTools() |
| 139 | { |
| 140 | if (m_tools.Number() == 0) |
| 141 | return FALSE; |
| 142 | |
| 143 | Rect toolbarrect = { m_y , m_x , m_y + m_height , m_x + m_width } ; |
| 144 | ControlFontStyleRec controlstyle ; |
| 145 | WindowPtr window = GetMacRootWindow() ; |
| 146 | controlstyle.flags = kControlUseFontMask ; |
| 147 | controlstyle.font = kControlFontSmallSystemFont ; |
| 148 | |
| 149 | wxNode *node = m_tools.First(); |
| 150 | int noButtons = 0; |
| 151 | int x = 0 ; |
| 152 | wxSize toolSize = GetToolSize() ; |
| 153 | int tw, th; |
| 154 | GetSize(& tw, & th); |
| 155 | while (node) |
| 156 | { |
| 157 | wxToolBarTool *tool = (wxToolBarTool *)node->Data(); |
| 158 | wxBitmapRefData * bmap = (wxBitmapRefData*) ( tool->m_bitmap1.GetRefData()) ; |
| 159 | |
| 160 | if( tool->m_toolStyle != wxTOOL_STYLE_SEPARATOR ) |
| 161 | { |
| 162 | Rect toolrect = { toolbarrect.top + kwxMacToolBarTopMargin , toolbarrect.left + x + kwxMacToolBarLeftMargin , 0 , 0 } ; |
| 163 | toolrect.right = toolrect.left + toolSize.x ; |
| 164 | toolrect.bottom = toolrect.top + toolSize.y ; |
| 165 | |
| 166 | PicHandle icon = NULL ; |
| 167 | if ( bmap ) |
| 168 | { |
| 169 | if ( bmap->m_bitmapType == kMacBitmapTypePict ) |
| 170 | icon = bmap->m_hPict ; |
| 171 | else if ( bmap->m_bitmapType == kMacBitmapTypeGrafWorld ) |
| 172 | { |
| 173 | icon = MakePict( bmap->m_hBitmap ) ; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | ControlHandle m_macToolHandle ; |
| 178 | |
| 179 | if ( icon ) |
| 180 | { |
| 181 | m_macToolHandle = UMANewControl( window , &toolrect , "\p" , true , 0 , |
| 182 | kControlBehaviorOffsetContents + kControlContentPictHandle , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ; |
| 183 | ControlButtonContentInfo info ; |
| 184 | |
| 185 | info.contentType = kControlContentPictHandle ; |
| 186 | info.u.picture = icon ; |
| 187 | |
| 188 | UMASetControlData( m_macToolHandle , kControlButtonPart , kControlBevelButtonContentTag , sizeof(info) , (char*) &info ) ; |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | m_macToolHandle = UMANewControl( window , &toolrect , "\p" , true , 0 , |
| 193 | kControlBehaviorOffsetContents , 0 , kControlBevelButtonNormalBevelProc , (long) this ) ; |
| 194 | } |
| 195 | m_macToolHandles.Add( m_macToolHandle ) ; |
| 196 | UMASetControlFontStyle( m_macToolHandle , &controlstyle ) ; |
| 197 | UMAEmbedControl( m_macToolHandle , m_macControl ) ; |
| 198 | |
| 199 | x += (int)toolSize.x; |
| 200 | noButtons ++; |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | m_macToolHandles.Add( NULL ) ; |
| 205 | x += (int)toolSize.x / 4; |
| 206 | } |
| 207 | if ( toolbarrect.left + x + kwxMacToolBarLeftMargin > m_maxWidth) |
| 208 | m_maxWidth = toolbarrect.left + x + kwxMacToolBarLeftMargin; |
| 209 | if (toolbarrect.top + kwxMacToolBarTopMargin + toolSize.y > m_maxHeight) |
| 210 | m_maxHeight = toolbarrect.top + kwxMacToolBarTopMargin ; |
| 211 | |
| 212 | node = node->Next(); |
| 213 | } |
| 214 | |
| 215 | if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) |
| 216 | { |
| 217 | m_maxWidth = tw ; // +=toolSize.x; |
| 218 | m_maxHeight += toolSize.y; |
| 219 | m_maxHeight += m_yMargin; |
| 220 | } |
| 221 | else |
| 222 | { |
| 223 | m_maxHeight = th ;// += toolSize.y; |
| 224 | m_maxWidth += toolSize.x; |
| 225 | m_maxWidth += m_xMargin; |
| 226 | } |
| 227 | |
| 228 | SetSize(m_maxWidth, m_maxHeight); |
| 229 | |
| 230 | return TRUE; |
| 231 | } |
| 232 | |
| 233 | void wxToolBar::SetToolBitmapSize(const wxSize& size) |
| 234 | { |
| 235 | m_defaultWidth = size.x; m_defaultHeight = size.y; |
| 236 | } |
| 237 | |
| 238 | wxSize wxToolBar::GetMaxSize() const |
| 239 | { |
| 240 | return wxSize(m_maxWidth, m_maxHeight); |
| 241 | } |
| 242 | |
| 243 | // The button size is bigger than the bitmap size |
| 244 | wxSize wxToolBar::GetToolSize() const |
| 245 | { |
| 246 | return wxSize(m_defaultWidth + 8, m_defaultHeight + 7); |
| 247 | } |
| 248 | |
| 249 | void wxToolBar::MacHandleControlClick( ControlHandle control , SInt16 controlpart ) |
| 250 | { |
| 251 | int index = 0 ; |
| 252 | for ( index = 0 ; index < m_macToolHandles.Count() ; ++index ) |
| 253 | { |
| 254 | if ( m_macToolHandles[index] == (void*) control ) |
| 255 | { |
| 256 | OnLeftClick( ( (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ) ->m_index , ( (wxToolBarTool*) (m_tools.Nth( index )->Data() ) ) ->m_toggleState ) ; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | void wxToolBar::EnableTool(int toolIndex, bool enable) |
| 262 | { |
| 263 | wxNode *node = m_tools.Find((long)toolIndex); |
| 264 | if (node) |
| 265 | { |
| 266 | wxToolBarTool *tool = (wxToolBarTool *)node->Data(); |
| 267 | tool->m_enabled = enable; |
| 268 | // TODO enable button |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | void wxToolBar::ToggleTool(int toolIndex, bool toggle) |
| 273 | { |
| 274 | wxNode *node = m_tools.Find((long)toolIndex); |
| 275 | if (node) |
| 276 | { |
| 277 | wxToolBarTool *tool = (wxToolBarTool *)node->Data(); |
| 278 | if (tool->m_isToggle) |
| 279 | { |
| 280 | tool->m_toggleState = toggle; |
| 281 | // TODO: set toggle state |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | void wxToolBar::ClearTools() |
| 287 | { |
| 288 | // TODO |
| 289 | wxToolBarBase::ClearTools(); |
| 290 | } |
| 291 | |
| 292 | // If pushedBitmap is NULL, a reversed version of bitmap is |
| 293 | // created and used as the pushed/toggled image. |
| 294 | // If toggle is TRUE, the button toggles between the two states. |
| 295 | |
| 296 | wxToolBarTool *wxToolBar::AddTool(int index, const wxBitmap& bitmap, const wxBitmap& pushedBitmap, |
| 297 | bool toggle, long xPos, long yPos, wxObject *clientData, const wxString& helpString1, const wxString& helpString2) |
| 298 | { |
| 299 | wxToolBarTool *tool = new wxToolBarTool(index, bitmap, wxNullBitmap, toggle, xPos, yPos, helpString1, helpString2); |
| 300 | tool->m_clientData = clientData; |
| 301 | |
| 302 | if (xPos > -1) |
| 303 | tool->m_x = xPos; |
| 304 | else |
| 305 | tool->m_x = m_xMargin; |
| 306 | |
| 307 | if (yPos > -1) |
| 308 | tool->m_y = yPos; |
| 309 | else |
| 310 | tool->m_y = m_yMargin; |
| 311 | |
| 312 | tool->SetSize(GetToolSize().x, GetToolSize().y); |
| 313 | |
| 314 | if ((tool->m_x + bitmap.GetWidth() + m_xMargin) > m_maxWidth) |
| 315 | m_maxWidth = (tool->m_x + tool->GetWidth() + m_xMargin); |
| 316 | |
| 317 | if ((tool->m_y + bitmap.GetHeight() + m_yMargin) > m_maxHeight) |
| 318 | m_maxHeight = (tool->m_y + tool->GetHeight() + m_yMargin); |
| 319 | |
| 320 | m_tools.Append((long)index, tool); |
| 321 | return tool; |
| 322 | } |
| 323 | |
| 324 | #endif // wxUSE_TOOLBAR |
| 325 | |