| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: tbarsmpl.cpp |
| 3 | // Purpose: wxToolBarSimple |
| 4 | // Author: Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 04/01/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Julian Smart and Markus Holzem |
| 9 | // Licence: wxWindows license |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "tbarsmpl.h" |
| 14 | #endif |
| 15 | |
| 16 | // For compilers that support precompilation, includes "wx.h". |
| 17 | #include "wx/wxprec.h" |
| 18 | |
| 19 | #ifdef __BORLANDC__ |
| 20 | #pragma hdrstop |
| 21 | #endif |
| 22 | |
| 23 | #ifndef WX_PRECOMP |
| 24 | #include "wx/wx.h" |
| 25 | #endif |
| 26 | |
| 27 | #if wxUSE_TOOLBAR |
| 28 | |
| 29 | #include "wx/tbarsmpl.h" |
| 30 | |
| 31 | #if !USE_SHARED_LIBRARY |
| 32 | IMPLEMENT_DYNAMIC_CLASS(wxToolBarSimple, wxToolBarBase) |
| 33 | |
| 34 | BEGIN_EVENT_TABLE(wxToolBarSimple, wxToolBarBase) |
| 35 | EVT_SIZE(wxToolBarSimple::OnSize) |
| 36 | EVT_PAINT(wxToolBarSimple::OnPaint) |
| 37 | EVT_KILL_FOCUS(wxToolBarSimple::OnKillFocus) |
| 38 | EVT_MOUSE_EVENTS(wxToolBarSimple::OnMouseEvent) |
| 39 | END_EVENT_TABLE() |
| 40 | #endif |
| 41 | |
| 42 | wxToolBarSimple::wxToolBarSimple(void) |
| 43 | { |
| 44 | m_currentRowsOrColumns = 0; |
| 45 | m_lastX = 0; |
| 46 | m_lastY = 0; |
| 47 | } |
| 48 | |
| 49 | bool wxToolBarSimple::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, |
| 50 | const wxString& name ) |
| 51 | { |
| 52 | if ( ! wxWindow::Create(parent, id, pos, size, style, name) ) |
| 53 | return FALSE; |
| 54 | |
| 55 | // Set it to grey (or other 3D face colour) |
| 56 | wxSystemSettings settings; |
| 57 | SetBackgroundColour(settings.GetSystemColour(wxSYS_COLOUR_3DFACE)); |
| 58 | |
| 59 | if ( GetWindowStyleFlag() & wxTB_VERTICAL ) |
| 60 | { m_lastX = 7; m_lastY = 3; } |
| 61 | else |
| 62 | { m_lastX = 3; m_lastY = 7; } |
| 63 | m_maxWidth = m_maxHeight = 0; |
| 64 | m_pressedTool = m_currentTool = -1; |
| 65 | m_xMargin = 0; |
| 66 | m_yMargin = 0; |
| 67 | m_toolPacking = 1; |
| 68 | m_toolSeparation = 5; |
| 69 | |
| 70 | return TRUE; |
| 71 | } |
| 72 | |
| 73 | wxToolBarSimple::~wxToolBarSimple () |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | void wxToolBarSimple::OnPaint (wxPaintEvent& event) |
| 78 | { |
| 79 | wxPaintDC dc(this); |
| 80 | PrepareDC(dc); |
| 81 | |
| 82 | static int count = 0; |
| 83 | // Prevent reentry of OnPaint which would cause wxMemoryDC errors. |
| 84 | if ( count > 0 ) |
| 85 | return; |
| 86 | count++; |
| 87 | |
| 88 | wxMemoryDC mem_dc; |
| 89 | |
| 90 | for ( wxNode *node = m_tools.First(); node; node = node->Next() ) |
| 91 | { |
| 92 | wxToolBarTool *tool = (wxToolBarTool *)node->Data(); |
| 93 | if (tool->m_toolStyle == wxTOOL_STYLE_BUTTON) |
| 94 | DrawTool(dc, mem_dc, tool); |
| 95 | } |
| 96 | |
| 97 | count--; |
| 98 | } |
| 99 | |
| 100 | void wxToolBarSimple::OnSize ( wxSizeEvent& event ) |
| 101 | { |
| 102 | wxToolBarBase::OnSize(event); |
| 103 | } |
| 104 | |
| 105 | void wxToolBarSimple::OnKillFocus (wxFocusEvent& event) |
| 106 | { |
| 107 | OnMouseEnter(m_pressedTool = m_currentTool = -1); |
| 108 | } |
| 109 | |
| 110 | void wxToolBarSimple::OnMouseEvent ( wxMouseEvent & event ) |
| 111 | { |
| 112 | long x, y; |
| 113 | event.Position(&x, &y); |
| 114 | wxToolBarTool *tool = FindToolForPosition(x, y); |
| 115 | |
| 116 | if (event.LeftDown()) |
| 117 | { |
| 118 | CaptureMouse(); |
| 119 | } |
| 120 | if (event.LeftUp()) |
| 121 | { |
| 122 | ReleaseMouse(); |
| 123 | } |
| 124 | |
| 125 | if (!tool) |
| 126 | { |
| 127 | if (m_currentTool > -1) |
| 128 | { |
| 129 | if (event.LeftIsDown()) |
| 130 | SpringUpButton(m_currentTool); |
| 131 | m_currentTool = -1; |
| 132 | OnMouseEnter(-1); |
| 133 | } |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | if (!event.IsButton()) |
| 138 | { |
| 139 | if (tool->m_index != m_currentTool) |
| 140 | { |
| 141 | // If the left button is kept down and moved over buttons, |
| 142 | // press those buttons. |
| 143 | if (event.LeftIsDown() && tool->m_enabled) |
| 144 | { |
| 145 | SpringUpButton(m_currentTool); |
| 146 | tool->m_toggleState = !tool->m_toggleState; |
| 147 | wxMemoryDC *dc2 = new wxMemoryDC; |
| 148 | wxClientDC dc(this); |
| 149 | DrawTool(dc, *dc2, tool); |
| 150 | delete dc2; |
| 151 | } |
| 152 | m_currentTool = tool->m_index; |
| 153 | OnMouseEnter(tool->m_index); |
| 154 | } |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | // Left button pressed. |
| 159 | if (event.LeftDown() && tool->m_enabled) |
| 160 | { |
| 161 | if (tool->m_isToggle) |
| 162 | { |
| 163 | tool->m_toggleState = !tool->m_toggleState; |
| 164 | } |
| 165 | |
| 166 | wxMemoryDC *dc2 = new wxMemoryDC; |
| 167 | wxClientDC dc(this); |
| 168 | DrawTool(dc, *dc2, tool); |
| 169 | delete dc2; |
| 170 | |
| 171 | } |
| 172 | else if (event.RightDown()) |
| 173 | { |
| 174 | OnRightClick(tool->m_index, x, y); |
| 175 | } |
| 176 | |
| 177 | // Left Button Released. Only this action confirms selection. |
| 178 | // If the button is enabled and it is not a toggle tool and it is |
| 179 | // in the pressed state, then raise the button and call OnLeftClick. |
| 180 | // |
| 181 | if (event.LeftUp() && tool->m_enabled && |
| 182 | (tool->m_toggleState || tool->m_isToggle)) |
| 183 | { |
| 184 | if (!tool->m_isToggle) |
| 185 | tool->m_toggleState = FALSE; |
| 186 | |
| 187 | // Pass the OnLeftClick event to tool |
| 188 | if (!OnLeftClick(tool->m_index, tool->m_toggleState) && tool->m_isToggle) |
| 189 | { |
| 190 | // If it was a toggle, and OnLeftClick says No Toggle allowed, |
| 191 | // then change it back |
| 192 | tool->m_toggleState = !tool->m_toggleState; |
| 193 | } |
| 194 | |
| 195 | wxClientDC dc(this); |
| 196 | wxMemoryDC *dc2 = new wxMemoryDC; |
| 197 | DrawTool(dc, *dc2, tool); |
| 198 | delete dc2; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | void wxToolBarSimple::DrawTool(wxDC& dc, wxMemoryDC& memDC, wxToolBarTool *tool) |
| 203 | { |
| 204 | PrepareDC(dc); |
| 205 | |
| 206 | wxPen dark_grey_pen(wxColour( 85,85,85 ), 1, wxSOLID); |
| 207 | wxPen white_pen("WHITE", 1, wxSOLID); |
| 208 | wxPen black_pen("BLACK", 1, wxSOLID); |
| 209 | |
| 210 | wxBitmap *bitmap = tool->m_toggleState ? (& tool->m_bitmap2) : (& tool->m_bitmap1); |
| 211 | |
| 212 | if (bitmap && bitmap->Ok()) |
| 213 | { |
| 214 | if (bitmap->GetPalette()) |
| 215 | memDC.SetPalette(*bitmap->GetPalette()); |
| 216 | |
| 217 | int ax = (int)tool->m_x, |
| 218 | ay = (int)tool->m_y, |
| 219 | bx = (int)(tool->m_x+tool->GetWidth()), |
| 220 | by = (int)(tool->m_y+tool->GetHeight()); |
| 221 | |
| 222 | memDC.SelectObject(*bitmap); |
| 223 | if (m_windowStyle & wxTB_3DBUTTONS) |
| 224 | { |
| 225 | dc.SetClippingRegion(ax, ay, (bx-ax+1), (by-ay+1)); |
| 226 | dc.Blit((ax+1), (ay+1), (bx-ax-2), (by-ay-2), &memDC, 0, 0); |
| 227 | wxPen * old_pen = & dc.GetPen(); |
| 228 | dc.SetPen( white_pen ); |
| 229 | dc.DrawLine(ax,(by-1),ax,ay); |
| 230 | dc.DrawLine(ax,ay,(bx-1),ay); |
| 231 | dc.SetPen( dark_grey_pen ); |
| 232 | dc.DrawLine((bx-1),(ay+1),(bx-1),(by-1)); |
| 233 | dc.DrawLine((bx-1),(by-1),(ax+1),(by-1)); |
| 234 | dc.SetPen( black_pen ); |
| 235 | dc.DrawLine(bx,ay,bx,by); |
| 236 | dc.DrawLine(bx,by,ax,by); |
| 237 | dc.SetPen( *old_pen ); |
| 238 | dc.DestroyClippingRegion(); |
| 239 | // Select bitmap out of the DC |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | dc.Blit(tool->m_x, tool->m_y, |
| 244 | bitmap->GetWidth(), bitmap->GetHeight(), |
| 245 | &memDC, 0, 0); |
| 246 | } |
| 247 | memDC.SelectObject(wxNullBitmap); |
| 248 | memDC.SetPalette(wxNullPalette); |
| 249 | } |
| 250 | // No second bitmap, so draw a thick line around bitmap, or invert if mono |
| 251 | else if (tool->m_toggleState) |
| 252 | { |
| 253 | bool drawBorder = FALSE; |
| 254 | #ifdef __X__ // X doesn't invert properly on colour |
| 255 | drawBorder = wxColourDisplay(); |
| 256 | #else // Inversion works fine under Windows |
| 257 | drawBorder = FALSE; |
| 258 | #endif |
| 259 | |
| 260 | if (!drawBorder) |
| 261 | { |
| 262 | memDC.SelectObject(tool->m_bitmap1); |
| 263 | dc.Blit(tool->m_x, tool->m_y, tool->GetWidth(), tool->GetHeight(), |
| 264 | &memDC, 0, 0, wxSRC_INVERT); |
| 265 | memDC.SelectObject(wxNullBitmap); |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | if (m_windowStyle & wxTB_3DBUTTONS) |
| 270 | { |
| 271 | int ax = (int)tool->m_x, |
| 272 | ay = (int)tool->m_y, |
| 273 | bx = (int)(tool->m_x+tool->GetWidth()), |
| 274 | by = (int)(tool->m_y+tool->GetHeight()); |
| 275 | |
| 276 | memDC.SelectObject(tool->m_bitmap1); |
| 277 | dc.SetClippingRegion(ax, ay, (bx-ax+1), (by-ay+1)); |
| 278 | dc.Blit((ax+2), (ay+2), (bx-ax-2), (by-ay-2), &memDC, 0, 0); |
| 279 | wxPen * old_pen = & dc.GetPen(); |
| 280 | dc.SetPen( black_pen ); |
| 281 | dc.DrawLine(ax,(by-1),ax,ay); |
| 282 | dc.DrawLine(ax,ay,(bx-1),ay); |
| 283 | dc.SetPen( dark_grey_pen ); |
| 284 | dc.DrawLine((ax+1),(by-2),(ax+1),(ay+1)); |
| 285 | dc.DrawLine((ax+1),(ay+1),(bx-2),(ay+1)); |
| 286 | dc.SetPen( white_pen ); |
| 287 | dc.DrawLine(bx,ay,bx,by); |
| 288 | dc.DrawLine(bx,by,ax,by); |
| 289 | dc.SetPen( *old_pen ); |
| 290 | dc.DestroyClippingRegion(); |
| 291 | memDC.SelectObject(wxNullBitmap); |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | long x = tool->m_x; |
| 296 | long y = tool->m_y; |
| 297 | long w = tool->m_bitmap1.GetWidth(); |
| 298 | long h = tool->m_bitmap1.GetHeight(); |
| 299 | wxPen thick_black_pen("BLACK", 3, wxSOLID); |
| 300 | |
| 301 | memDC.SelectObject(tool->m_bitmap1); |
| 302 | dc.SetClippingRegion(tool->m_x, tool->m_y, w, h); |
| 303 | dc.Blit(tool->m_x, tool->m_y, w, h, |
| 304 | &memDC, 0, 0); |
| 305 | dc.SetPen(thick_black_pen); |
| 306 | dc.SetBrush(*wxTRANSPARENT_BRUSH); |
| 307 | dc.DrawRectangle(x, y, w-1, h-1); |
| 308 | dc.DestroyClippingRegion(); |
| 309 | memDC.SelectObject(wxNullBitmap); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | void wxToolBarSimple::ToggleTool(int index, bool toggle) |
| 316 | { |
| 317 | wxNode *node = m_tools.Find((long)index); |
| 318 | if (node) |
| 319 | { |
| 320 | wxToolBarTool *tool = (wxToolBarTool *)node->Data(); |
| 321 | if (tool && tool->m_isToggle) |
| 322 | { |
| 323 | bool oldState = tool->m_toggleState; |
| 324 | tool->m_toggleState = toggle; |
| 325 | |
| 326 | if (oldState != toggle) |
| 327 | { |
| 328 | wxMemoryDC memDC; |
| 329 | wxClientDC dc(this); |
| 330 | DrawTool(dc, memDC, tool); |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | // Okay, so we've left the tool we're in ... we must check if |
| 337 | // the tool we're leaving was a 'sprung push button' and if so, |
| 338 | // spring it back to the up state. |
| 339 | // |
| 340 | void wxToolBarSimple::SpringUpButton(int index) |
| 341 | { |
| 342 | wxNode *node=m_tools.Find((long)index); |
| 343 | if (node) { |
| 344 | wxToolBarTool *tool = (wxToolBarTool *)node->Data(); |
| 345 | if (tool && !tool->m_isToggle && tool->m_toggleState){ |
| 346 | tool->m_toggleState = FALSE; |
| 347 | wxMemoryDC memDC; |
| 348 | wxClientDC dc(this); |
| 349 | DrawTool(dc, memDC, tool); |
| 350 | } |
| 351 | else if (tool && tool->m_isToggle){ |
| 352 | tool->m_toggleState = !tool->m_toggleState; |
| 353 | wxMemoryDC memDC; |
| 354 | wxClientDC dc(this); |
| 355 | DrawTool(dc, memDC, tool); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | void wxToolBarSimple::Layout(void) |
| 361 | { |
| 362 | m_currentRowsOrColumns = 0; |
| 363 | m_lastX = m_xMargin; |
| 364 | m_lastY = m_yMargin; |
| 365 | int maxToolWidth = 0; |
| 366 | int maxToolHeight = 0; |
| 367 | m_maxWidth = 0; |
| 368 | m_maxHeight = 0; |
| 369 | |
| 370 | // Find the maximum tool width and height |
| 371 | wxNode *node = m_tools.First(); |
| 372 | while (node) |
| 373 | { |
| 374 | wxToolBarTool *tool = (wxToolBarTool *)node->Data(); |
| 375 | if (tool->GetWidth() > maxToolWidth) |
| 376 | maxToolWidth = (int)tool->GetWidth(); |
| 377 | if (tool->GetHeight() > maxToolHeight) |
| 378 | maxToolHeight = (int)tool->GetHeight(); |
| 379 | node = node->Next(); |
| 380 | } |
| 381 | |
| 382 | int separatorSize = m_toolSeparation; |
| 383 | |
| 384 | node = m_tools.First(); |
| 385 | while (node) |
| 386 | { |
| 387 | wxToolBarTool *tool = (wxToolBarTool *)node->Data(); |
| 388 | if (tool->m_toolStyle == wxTOOL_STYLE_SEPARATOR) |
| 389 | { |
| 390 | if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) |
| 391 | { |
| 392 | if (m_currentRowsOrColumns >= m_maxCols) |
| 393 | m_lastY += separatorSize; |
| 394 | else |
| 395 | m_lastX += separatorSize; |
| 396 | } |
| 397 | else |
| 398 | { |
| 399 | if (m_currentRowsOrColumns >= m_maxRows) |
| 400 | m_lastX += separatorSize; |
| 401 | else |
| 402 | m_lastY += separatorSize; |
| 403 | } |
| 404 | } |
| 405 | else if (tool->m_toolStyle == wxTOOL_STYLE_BUTTON) |
| 406 | { |
| 407 | if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) |
| 408 | { |
| 409 | if (m_currentRowsOrColumns >= m_maxCols) |
| 410 | { |
| 411 | m_currentRowsOrColumns = 0; |
| 412 | m_lastX = m_xMargin; |
| 413 | m_lastY += maxToolHeight + m_toolPacking; |
| 414 | } |
| 415 | tool->m_x = (long) (m_lastX + (maxToolWidth - tool->GetWidth())/2.0); |
| 416 | tool->m_y = (long) (m_lastY + (maxToolHeight - tool->GetHeight())/2.0); |
| 417 | |
| 418 | m_lastX += maxToolWidth + m_toolPacking; |
| 419 | } |
| 420 | else |
| 421 | { |
| 422 | if (m_currentRowsOrColumns >= m_maxRows) |
| 423 | { |
| 424 | m_currentRowsOrColumns = 0; |
| 425 | m_lastX += (maxToolWidth + m_toolPacking); |
| 426 | m_lastY = m_yMargin; |
| 427 | } |
| 428 | tool->m_x = (long) (m_lastX + (maxToolWidth - tool->GetWidth())/2.0); |
| 429 | tool->m_y = (long) (m_lastY + (maxToolHeight - tool->GetHeight())/2.0); |
| 430 | |
| 431 | m_lastY += maxToolHeight + m_toolPacking; |
| 432 | } |
| 433 | m_currentRowsOrColumns ++; |
| 434 | } |
| 435 | |
| 436 | if (m_lastX > m_maxWidth) |
| 437 | m_maxWidth = m_lastX; |
| 438 | if (m_lastY > m_maxHeight) |
| 439 | m_maxHeight = m_lastY; |
| 440 | |
| 441 | node = node->Next(); |
| 442 | } |
| 443 | if ( GetWindowStyleFlag() & wxTB_HORIZONTAL ) |
| 444 | m_maxWidth += maxToolWidth; |
| 445 | else |
| 446 | m_maxHeight += maxToolHeight; |
| 447 | |
| 448 | m_maxWidth += m_xMargin; |
| 449 | m_maxHeight += m_yMargin; |
| 450 | } |
| 451 | |
| 452 | |
| 453 | #endif |