]> git.saurik.com Git - wxWidgets.git/blob - src/generic/tabg.cpp
fix for w/o expat
[wxWidgets.git] / src / generic / tabg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tabg.cpp
3 // Purpose: Generic tabbed dialogs
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c)
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "tabg.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 #if wxUSE_TAB_DIALOG
24
25 #ifndef WX_PRECOMP
26 #include "wx/settings.h"
27 #include "wx/intl.h"
28 #include "wx/dcclient.h"
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34
35 #include "wx/math.h"
36 #include "wx/tab.h"
37 #include "wx/listimpl.cpp"
38
39 WX_DEFINE_LIST(wxTabLayerList);
40
41 // not defined: use old, square tab implementation (fills in tabs)
42 // defined: use new, rounded tab implementation (doesn't colour in tabs)
43 // #define wxUSE_NEW_METHOD
44
45 IMPLEMENT_DYNAMIC_CLASS(wxTabControl, wxObject)
46
47 // IMPLEMENT_DYNAMIC_CLASS(wxTabLayer, wxList)
48
49 wxTabControl::wxTabControl(wxTabView *v)
50 {
51 m_view = v;
52 m_isSelected = false;
53 m_offsetX = 0;
54 m_offsetY = 0;
55 m_width = 0;
56 m_height = 0;
57 m_id = 0;
58 m_rowPosition = 0;
59 m_colPosition = 0;
60 }
61
62 wxTabControl::~wxTabControl(void)
63 {
64 }
65
66 void wxTabControl::OnDraw(wxDC& dc, bool lastInRow)
67 {
68 // Old, but in some ways better (drawing opaque tabs)
69 #ifndef wxUSE_NEW_METHOD
70 if (!m_view)
71 return;
72
73 // Top-left of tab view area
74 int viewX = m_view->GetViewRect().x;
75 int viewY = m_view->GetViewRect().y;
76
77 // Top-left of tab control
78 int tabX = GetX() + viewX;
79 int tabY = GetY() + viewY;
80 int tabHeightInc = 0;
81 if (m_isSelected)
82 {
83 tabHeightInc = (m_view->GetTabSelectionHeight() - m_view->GetTabHeight());
84 tabY -= tabHeightInc;
85 }
86
87 dc.SetPen(*wxTRANSPARENT_PEN);
88
89 // Draw grey background
90 if (m_view->GetTabStyle() & wxTAB_STYLE_COLOUR_INTERIOR)
91 {
92 dc.SetBrush(*m_view->GetBackgroundBrush());
93
94 // Add 1 because the pen is transparent. Under Motif, may be different.
95 #ifdef __WXMOTIF__
96 dc.DrawRectangle(tabX, tabY, (GetWidth()+1), (GetHeight() + tabHeightInc));
97 #else
98 dc.DrawRectangle(tabX, tabY, (GetWidth()+1), (GetHeight() + 1 + tabHeightInc));
99 #endif
100 }
101
102 // Draw highlight and shadow
103 dc.SetPen(*m_view->GetHighlightPen());
104
105 // Calculate the top of the tab beneath. It's the height of the tab, MINUS
106 // a bit if the tab below happens to be selected. Check.
107 wxTabControl *tabBeneath = NULL;
108 int subtractThis = 0;
109 if (GetColPosition() > 0)
110 tabBeneath = m_view->FindTabControlForPosition(GetColPosition() - 1, GetRowPosition());
111 if (tabBeneath && tabBeneath->IsSelected())
112 subtractThis = (m_view->GetTabSelectionHeight() - m_view->GetTabHeight());
113
114 // Vertical highlight: if first tab, draw to bottom of view
115 if (tabX == m_view->GetViewRect().x && (m_view->GetTabStyle() & wxTAB_STYLE_DRAW_BOX))
116 dc.DrawLine(tabX, tabY, tabX, (m_view->GetViewRect().y + m_view->GetViewRect().height));
117 else if (tabX == m_view->GetViewRect().x)
118 // Not box drawing, just to top of view.
119 dc.DrawLine(tabX, tabY, tabX, (m_view->GetViewRect().y));
120 else
121 dc.DrawLine(tabX, tabY, tabX, (tabY + GetHeight() + tabHeightInc - subtractThis));
122
123 dc.DrawLine(tabX, tabY, (tabX + GetWidth()), tabY);
124 dc.SetPen(*m_view->GetShadowPen());
125
126 // Test if we're outside the right-hand edge of the view area
127 if (((tabX + GetWidth()) >= m_view->GetViewRect().x + m_view->GetViewRect().width) && (m_view->GetTabStyle() & wxTAB_STYLE_DRAW_BOX))
128 {
129 int bottomY = m_view->GetViewRect().y + m_view->GetViewRect().height + GetY() + m_view->GetTabHeight() + m_view->GetTopMargin();
130 // Add a tab height since we wish to draw to the bottom of the view.
131 dc.DrawLine((tabX + GetWidth()), tabY,
132 (tabX + GetWidth()), bottomY);
133
134 // Calculate the far-right of the view, since we don't wish to
135 // draw inside that
136 int rightOfView = m_view->GetViewRect().x + m_view->GetViewRect().width + 1;
137
138 // Draw the horizontal bit to connect to the view rectangle
139 dc.DrawLine((wxMax((tabX + GetWidth() - m_view->GetHorizontalTabOffset()), rightOfView)), (bottomY-1),
140 (tabX + GetWidth()), (bottomY-1));
141
142 // Draw black line to emphasize shadow
143 dc.SetPen(*wxBLACK_PEN);
144 dc.DrawLine((tabX + GetWidth() + 1), (tabY+1),
145 (tabX + GetWidth() + 1), bottomY);
146
147 // Draw the horizontal bit to connect to the view rectangle
148 dc.DrawLine((wxMax((tabX + GetWidth() - m_view->GetHorizontalTabOffset()), rightOfView)), (bottomY),
149 (tabX + GetWidth() + 1), (bottomY));
150 }
151 else
152 {
153 if (lastInRow)
154 {
155 // 25/5/97 UNLESS it's less than the max number of positions in this row
156
157 int topY = m_view->GetViewRect().y - m_view->GetTopMargin();
158
159 int maxPositions = ((wxTabLayer *)m_view->GetLayers().Item(0)->GetData())->GetCount();
160
161 // Only down to the bottom of the tab, not to the top of the view
162 if ( GetRowPosition() < (maxPositions - 1) )
163 topY = tabY + GetHeight() + tabHeightInc;
164
165 #ifdef __WXMOTIF__
166 topY -= 1;
167 #endif
168
169 // Shadow
170 dc.DrawLine((tabX + GetWidth()), tabY, (tabX + GetWidth()), topY);
171 // Draw black line to emphasize shadow
172 dc.SetPen(*wxBLACK_PEN);
173 dc.DrawLine((tabX + GetWidth() + 1), (tabY+1), (tabX + GetWidth() + 1),
174 topY);
175 }
176 else
177 {
178 // Calculate the top of the tab beneath. It's the height of the tab, MINUS
179 // a bit if the tab below (and next col along) happens to be selected. Check.
180 wxTabControl *tabBeneath = NULL;
181 int subtractThis = 0;
182 if (GetColPosition() > 0)
183 tabBeneath = m_view->FindTabControlForPosition(GetColPosition() - 1, GetRowPosition() + 1);
184 if (tabBeneath && tabBeneath->IsSelected())
185 subtractThis = (m_view->GetTabSelectionHeight() - m_view->GetTabHeight());
186
187 #ifdef __WXMOTIF__
188 subtractThis += 1;
189 #endif
190
191 // Draw only to next tab down.
192 dc.DrawLine((tabX + GetWidth()), tabY,
193 (tabX + GetWidth()), (tabY + GetHeight() + tabHeightInc - subtractThis));
194
195 // Draw black line to emphasize shadow
196 dc.SetPen(*wxBLACK_PEN);
197 dc.DrawLine((tabX + GetWidth() + 1), (tabY+1), (tabX + GetWidth() + 1),
198 (tabY + GetHeight() + tabHeightInc - subtractThis));
199 }
200 }
201
202 // Draw centered text
203 int textY = tabY + m_view->GetVerticalTabTextSpacing() + tabHeightInc;
204
205 if (m_isSelected)
206 dc.SetFont(* m_view->GetSelectedTabFont());
207 else
208 dc.SetFont(* GetFont());
209
210 wxColour col(m_view->GetTextColour());
211 dc.SetTextForeground(col);
212 dc.SetBackgroundMode(wxTRANSPARENT);
213 long textWidth, textHeight;
214 dc.GetTextExtent(GetLabel(), &textWidth, &textHeight);
215
216 int textX = (int)(tabX + (GetWidth() - textWidth)/2.0);
217 if (textX < (tabX + 2))
218 textX = (tabX + 2);
219
220 dc.SetClippingRegion(tabX, tabY, GetWidth(), GetHeight());
221 dc.DrawText(GetLabel(), textX, textY);
222 dc.DestroyClippingRegion();
223
224 if (m_isSelected)
225 {
226 dc.SetPen(*m_view->GetHighlightPen());
227
228 // Draw white highlight from the tab's left side to the left hand edge of the view
229 dc.DrawLine(m_view->GetViewRect().x, (tabY + GetHeight() + tabHeightInc),
230 tabX, (tabY + GetHeight() + tabHeightInc));
231
232 // Draw white highlight from the tab's right side to the right hand edge of the view
233 dc.DrawLine((tabX + GetWidth()), (tabY + GetHeight() + tabHeightInc),
234 m_view->GetViewRect().x + m_view->GetViewRect().width, (tabY + GetHeight() + tabHeightInc));
235 }
236 #else
237 // New HEL version with rounder tabs
238
239 if (!m_view) return;
240
241 int tabInc = 0;
242 if (m_isSelected)
243 {
244 tabInc = m_view->GetTabSelectionHeight() - m_view->GetTabHeight();
245 }
246 int tabLeft = GetX() + m_view->GetViewRect().x;
247 int tabTop = GetY() + m_view->GetViewRect().y - tabInc;
248 int tabRight = tabLeft + m_view->GetTabWidth();
249 int left = m_view->GetViewRect().x;
250 int top = tabTop + m_view->GetTabHeight() + tabInc;
251 int right = left + m_view->GetViewRect().width;
252 int bottom = top + m_view->GetViewRect().height;
253
254 if (m_isSelected)
255 {
256 // TAB is selected - draw TAB and the View's full outline
257
258 dc.SetPen(*(m_view->GetHighlightPen()));
259 wxPoint pnts[10];
260 int n = 0;
261 pnts[n].x = left; pnts[n++].y = bottom;
262 pnts[n].x = left; pnts[n++].y = top;
263 pnts[n].x = tabLeft; pnts[n++].y = top;
264 pnts[n].x = tabLeft; pnts[n++].y = tabTop + 2;
265 pnts[n].x = tabLeft + 2; pnts[n++].y = tabTop;
266 pnts[n].x = tabRight - 1; pnts[n++].y = tabTop;
267 dc.DrawLines(n, pnts);
268 if (!lastInRow)
269 {
270 dc.DrawLine(
271 (tabRight + 2),
272 top,
273 right,
274 top
275 );
276 }
277
278 dc.SetPen(*(m_view->GetShadowPen()));
279 dc.DrawLine(
280 tabRight,
281 tabTop + 2,
282 tabRight,
283 top
284 );
285 dc.DrawLine(
286 right,
287 top,
288 right,
289 bottom
290 );
291 dc.DrawLine(
292 right,
293 bottom,
294 left,
295 bottom
296 );
297
298 dc.SetPen(*wxBLACK_PEN);
299 dc.DrawPoint(
300 tabRight,
301 tabTop + 1
302 );
303 dc.DrawPoint(
304 tabRight + 1,
305 tabTop + 2
306 );
307 if (lastInRow)
308 {
309 dc.DrawLine(
310 tabRight + 1,
311 bottom,
312 tabRight + 1,
313 tabTop + 1
314 );
315 }
316 else
317 {
318 dc.DrawLine(
319 tabRight + 1,
320 tabTop + 2,
321 tabRight + 1,
322 top
323 );
324 dc.DrawLine(
325 right + 1,
326 top,
327 right + 1,
328 bottom + 1
329 );
330 }
331 dc.DrawLine(
332 right + 1,
333 bottom + 1,
334 left + 1,
335 bottom + 1
336 );
337 }
338 else
339 {
340 // TAB is not selected - just draw TAB outline and RH edge
341 // if the TAB is the last in the row
342
343 int maxPositions = ((wxTabLayer*)m_view->GetLayers().Item(0)->GetData())->GetCount();
344 wxTabControl* tabBelow = 0;
345 wxTabControl* tabBelowRight = 0;
346 if (GetColPosition() > 0)
347 {
348 tabBelow = m_view->FindTabControlForPosition(
349 GetColPosition() - 1,
350 GetRowPosition()
351 );
352 }
353 if (!lastInRow && GetColPosition() > 0)
354 {
355 tabBelowRight = m_view->FindTabControlForPosition(
356 GetColPosition() - 1,
357 GetRowPosition() + 1
358 );
359 }
360
361 float raisedTop = top - m_view->GetTabSelectionHeight() +
362 m_view->GetTabHeight();
363
364 dc.SetPen(*(m_view->GetHighlightPen()));
365 wxPoint pnts[10];
366 int n = 0;
367
368 pnts[n].x = tabLeft;
369
370 if (tabBelow && tabBelow->IsSelected())
371 {
372 pnts[n++].y = (long)raisedTop;
373 }
374 else
375 {
376 pnts[n++].y = top;
377 }
378 pnts[n].x = tabLeft; pnts[n++].y = tabTop + 2;
379 pnts[n].x = tabLeft + 2; pnts[n++].y = tabTop;
380 pnts[n].x = tabRight - 1; pnts[n++].y = tabTop;
381 dc.DrawLines(n, pnts);
382
383 dc.SetPen(*(m_view->GetShadowPen()));
384 if (GetRowPosition() >= maxPositions - 1)
385 {
386 dc.DrawLine(
387 tabRight,
388 (tabTop + 2),
389 tabRight,
390 bottom
391 );
392 dc.DrawLine(
393 tabRight,
394 bottom,
395 (tabRight - m_view->GetHorizontalTabOffset()),
396 bottom
397 );
398 }
399 else
400 {
401 if (tabBelowRight && tabBelowRight->IsSelected())
402 {
403 dc.DrawLine(
404 tabRight,
405 (long)raisedTop,
406 tabRight,
407 tabTop + 1
408 );
409 }
410 else
411 {
412 dc.DrawLine(
413 tabRight,
414 top - 1,
415 tabRight,
416 tabTop + 1
417 );
418 }
419 }
420
421 dc.SetPen(*wxBLACK_PEN);
422 dc.DrawPoint(
423 tabRight,
424 tabTop + 1
425 );
426 dc.DrawPoint(
427 tabRight + 1,
428 tabTop + 2
429 );
430 if (GetRowPosition() >= maxPositions - 1)
431 {
432 // draw right hand edge to bottom of view
433 dc.DrawLine(
434 tabRight + 1,
435 bottom + 1,
436 tabRight + 1,
437 tabTop + 2
438 );
439 dc.DrawLine(
440 tabRight + 1,
441 bottom + 1,
442 (tabRight - m_view->GetHorizontalTabOffset()),
443 bottom + 1
444 );
445 }
446 else
447 {
448 // draw right hand edge of TAB
449 if (tabBelowRight && tabBelowRight->IsSelected())
450 {
451 dc.DrawLine(
452 tabRight + 1,
453 (long)(raisedTop - 1),
454 tabRight + 1,
455 tabTop + 2
456 );
457 }
458 else
459 {
460 dc.DrawLine(
461 tabRight + 1,
462 top - 1,
463 tabRight + 1,
464 tabTop + 2
465 );
466 }
467 }
468 }
469
470 // Draw centered text
471 dc.SetPen(*wxBLACK_PEN);
472 if (m_isSelected)
473 {
474 dc.SetFont(*(m_view->GetSelectedTabFont()));
475 }
476 else
477 {
478 dc.SetFont(*(GetFont()));
479 }
480
481 wxColour col(m_view->GetTextColour());
482 dc.SetTextForeground(col);
483 dc.SetBackgroundMode(wxTRANSPARENT);
484 long textWidth, textHeight;
485 dc.GetTextExtent(GetLabel(), &textWidth, &textHeight);
486
487 float textX = (tabLeft + tabRight - textWidth) / 2;
488 float textY = (tabInc + tabTop + m_view->GetVerticalTabTextSpacing());
489
490 dc.DrawText(GetLabel(), (long)textX, (long)textY);
491 #endif
492 }
493
494 bool wxTabControl::HitTest(int x, int y) const
495 {
496 // Top-left of tab control
497 int tabX1 = GetX() + m_view->GetViewRect().x;
498 int tabY1 = GetY() + m_view->GetViewRect().y;
499
500 // Bottom-right
501 int tabX2 = tabX1 + GetWidth();
502 int tabY2 = tabY1 + GetHeight();
503
504 if (x >= tabX1 && y >= tabY1 && x <= tabX2 && y <= tabY2)
505 return true;
506 else
507 return false;
508 }
509
510 IMPLEMENT_DYNAMIC_CLASS(wxTabView, wxObject)
511
512 wxTabView::wxTabView(long style)
513 {
514 m_noTabs = 0;
515 m_tabStyle = style;
516 m_tabSelection = -1;
517 m_tabHeight = 20;
518 m_tabSelectionHeight = m_tabHeight + 2;
519 m_tabWidth = 80;
520 m_tabHorizontalOffset = 10;
521 m_tabHorizontalSpacing = 2;
522 m_tabVerticalTextSpacing = 3;
523 m_topMargin = 5;
524 m_tabViewRect.x = 20;
525 m_tabViewRect.y = 20;
526 m_tabViewRect.width = 300;
527 m_tabViewRect.x = 300;
528 m_highlightColour = *wxWHITE;
529 m_shadowColour = wxColour(128, 128, 128);
530 m_backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_BTNFACE);
531 m_textColour = *wxBLACK;
532 m_highlightPen = wxWHITE_PEN;
533 m_shadowPen = wxGREY_PEN;
534 SetBackgroundColour(m_backgroundColour);
535 m_tabFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
536 m_tabSelectedFont = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
537 m_window = (wxWindow *) NULL;
538 }
539
540 wxTabView::~wxTabView()
541 {
542 ClearTabs(true);
543 }
544
545 // Automatically positions tabs
546 // TODO: this should just add the tab to a list, and then
547 // a layout function (e.g. Realize) should be called when all tabs have been added.
548 // The view rect could easily change as the view window is resized.
549 wxTabControl *wxTabView::AddTab(int id, const wxString& label, wxTabControl *existingTab)
550 {
551 // First, find which layer we should be adding to.
552 wxTabLayerList::compatibility_iterator node = m_layers.GetLast();
553 if (!node)
554 {
555 wxTabLayer *newLayer = new wxTabLayer;
556 node = m_layers.Append(newLayer);
557 }
558 // Check if adding another tab control would go off the
559 // right-hand edge of the layer.
560 wxTabLayer *tabLayer = (wxTabLayer *)node->GetData();
561 wxList::compatibility_iterator lastTabNode = tabLayer->GetLast();
562 if (lastTabNode)
563 {
564 wxTabControl *lastTab = (wxTabControl *)lastTabNode->GetData();
565 // Start another layer (row).
566 // Tricky choice: can't just check if will be overlapping the edge, because
567 // this happens anyway for 2nd and subsequent rows.
568 // Should check this for 1st row, and then subsequent rows should not exceed 1st
569 // in length.
570 if (((tabLayer == m_layers.GetFirst()->GetData()) && ((lastTab->GetX() + 2*lastTab->GetWidth() + GetHorizontalTabSpacing())
571 > GetViewRect().width)) ||
572 ((tabLayer != m_layers.GetFirst()->GetData()) && (tabLayer->GetCount() == ((wxTabLayer *)m_layers.GetFirst()->GetData())->GetCount())))
573 {
574 tabLayer = new wxTabLayer;
575 m_layers.Append(tabLayer);
576 lastTabNode = wxList::compatibility_iterator();
577 }
578 }
579 int layer = m_layers.GetCount() - 1;
580
581 wxTabControl *tabControl = existingTab;
582 if (!existingTab)
583 tabControl = OnCreateTabControl();
584 tabControl->SetRowPosition(tabLayer->GetCount());
585 tabControl->SetColPosition(layer);
586
587 wxTabControl *lastTab = (wxTabControl *) NULL;
588 if (lastTabNode)
589 lastTab = (wxTabControl *)lastTabNode->GetData();
590
591 // Top of new tab
592 int verticalOffset = (- GetTopMargin()) - ((layer+1)*GetTabHeight());
593 // Offset from view top-left
594 int horizontalOffset = 0;
595 if (!lastTab)
596 horizontalOffset = layer*GetHorizontalTabOffset();
597 else
598 horizontalOffset = lastTab->GetX() + GetTabWidth() + GetHorizontalTabSpacing();
599
600 tabControl->SetPosition(horizontalOffset, verticalOffset);
601 tabControl->SetSize(GetTabWidth(), GetTabHeight());
602 tabControl->SetId(id);
603 tabControl->SetLabel(label);
604 tabControl->SetFont(* GetTabFont());
605
606 tabLayer->Append(tabControl);
607 m_noTabs ++;
608
609 return tabControl;
610 }
611
612 // Remove the tab without deleting the window
613 bool wxTabView::RemoveTab(int id)
614 {
615 wxTabLayerList::compatibility_iterator layerNode = m_layers.GetFirst();
616 while (layerNode)
617 {
618 wxTabLayer *layer = (wxTabLayer *)layerNode->GetData();
619 wxList::compatibility_iterator tabNode = layer->GetFirst();
620 while (tabNode)
621 {
622 wxTabControl *tab = (wxTabControl *)tabNode->GetData();
623 if (tab->GetId() == id)
624 {
625 if (id == m_tabSelection)
626 m_tabSelection = -1;
627 delete tab;
628 layer->Erase(tabNode);
629 m_noTabs --;
630
631 // The layout has changed
632 LayoutTabs();
633 return true;
634 }
635 tabNode = tabNode->GetNext();
636 }
637 layerNode = layerNode->GetNext();
638 }
639 return false;
640 }
641
642 bool wxTabView::SetTabText(int id, const wxString& label)
643 {
644 wxTabControl* control = FindTabControlForId(id);
645 if (!control)
646 return false;
647 control->SetLabel(label);
648 return true;
649 }
650
651 wxString wxTabView::GetTabText(int id) const
652 {
653 wxTabControl* control = FindTabControlForId(id);
654 if (!control)
655 return wxEmptyString;
656 else
657 return control->GetLabel();
658 }
659
660 // Returns the total height of the tabs component -- this may be several
661 // times the height of a tab, if there are several tab layers (rows).
662 int wxTabView::GetTotalTabHeight()
663 {
664 int minY = 0;
665
666 wxTabLayerList::compatibility_iterator layerNode = m_layers.GetFirst();
667 while (layerNode)
668 {
669 wxTabLayer *layer = (wxTabLayer *)layerNode->GetData();
670 wxList::compatibility_iterator tabNode = layer->GetFirst();
671 while (tabNode)
672 {
673 wxTabControl *tab = (wxTabControl *)tabNode->GetData();
674
675 if (tab->GetY() < minY)
676 minY = tab->GetY();
677
678 tabNode = tabNode->GetNext();
679 }
680 layerNode = layerNode->GetNext();
681 }
682
683 return - minY;
684 }
685
686 void wxTabView::ClearTabs(bool deleteTabs)
687 {
688 wxTabLayerList::compatibility_iterator layerNode = m_layers.GetFirst();
689 while (layerNode)
690 {
691 wxTabLayer *layer = (wxTabLayer *)layerNode->GetData();
692 wxList::compatibility_iterator tabNode = layer->GetFirst();
693 while (tabNode)
694 {
695 wxTabControl *tab = (wxTabControl *)tabNode->GetData();
696 if (deleteTabs)
697 delete tab;
698 wxList::compatibility_iterator next = tabNode->GetNext();
699 layer->Erase(tabNode);
700 tabNode = next;
701 }
702 wxTabLayerList::compatibility_iterator nextLayerNode = layerNode->GetNext();
703 delete layer;
704 m_layers.Erase(layerNode);
705 layerNode = nextLayerNode;
706 }
707 m_noTabs = 0;
708 m_tabSelection = -1;
709 }
710
711
712 // Layout tabs (optional, e.g. if resizing window)
713 void wxTabView::LayoutTabs(void)
714 {
715 // Make a list of the tab controls, deleting the wxTabLayers.
716 wxList controls;
717
718 wxTabLayerList::compatibility_iterator layerNode = m_layers.GetFirst();
719 while (layerNode)
720 {
721 wxTabLayer *layer = (wxTabLayer *)layerNode->GetData();
722 wxList::compatibility_iterator tabNode = layer->GetFirst();
723 while (tabNode)
724 {
725 wxTabControl *tab = (wxTabControl *)tabNode->GetData();
726 controls.Append(tab);
727 wxList::compatibility_iterator next = tabNode->GetNext();
728 layer->Erase(tabNode);
729 tabNode = next;
730 }
731 wxTabLayerList::compatibility_iterator nextLayerNode = layerNode->GetNext();
732 delete layer;
733 m_layers.Erase(layerNode);
734 layerNode = nextLayerNode;
735 }
736
737 wxTabControl *lastTab = (wxTabControl *) NULL;
738
739 wxTabLayer *currentLayer = new wxTabLayer;
740 m_layers.Append(currentLayer);
741
742 wxList::compatibility_iterator node = controls.GetFirst();
743 while (node)
744 {
745 wxTabControl *tabControl = (wxTabControl *)node->GetData();
746 if (lastTab)
747 {
748 // Start another layer (row).
749 // Tricky choice: can't just check if will be overlapping the edge, because
750 // this happens anyway for 2nd and subsequent rows.
751 // Should check this for 1st row, and then subsequent rows should not exceed 1st
752 // in length.
753 if (((currentLayer == m_layers.GetFirst()->GetData()) && ((lastTab->GetX() + 2*lastTab->GetWidth() + GetHorizontalTabSpacing())
754 > GetViewRect().width)) ||
755 ((currentLayer != m_layers.GetFirst()->GetData()) && (currentLayer->GetCount() == ((wxTabLayer *)m_layers.GetFirst()->GetData())->GetCount())))
756 {
757 currentLayer = new wxTabLayer;
758 m_layers.Append(currentLayer);
759 lastTab = (wxTabControl *) NULL;
760 }
761 }
762
763 int layer = m_layers.GetCount() - 1;
764
765 tabControl->SetRowPosition(currentLayer->GetCount());
766 tabControl->SetColPosition(layer);
767
768 // Top of new tab
769 int verticalOffset = (- GetTopMargin()) - ((layer+1)*GetTabHeight());
770 // Offset from view top-left
771 int horizontalOffset = 0;
772 if (!lastTab)
773 horizontalOffset = layer*GetHorizontalTabOffset();
774 else
775 horizontalOffset = lastTab->GetX() + GetTabWidth() + GetHorizontalTabSpacing();
776
777 tabControl->SetPosition(horizontalOffset, verticalOffset);
778 tabControl->SetSize(GetTabWidth(), GetTabHeight());
779
780 currentLayer->Append(tabControl);
781 lastTab = tabControl;
782
783 node = node->GetNext();
784 }
785
786 // Move the selected tab to the bottom
787 wxTabControl *control = FindTabControlForId(m_tabSelection);
788 if (control)
789 MoveSelectionTab(control);
790
791 }
792
793 // Draw all tabs
794 void wxTabView::Draw(wxDC& dc)
795 {
796 // Don't draw anything if there are no tabs.
797 if (GetNumberOfTabs() == 0)
798 return;
799
800 // Draw top margin area (beneath tabs and above view area)
801 if (GetTabStyle() & wxTAB_STYLE_COLOUR_INTERIOR)
802 {
803 dc.SetPen(*wxTRANSPARENT_PEN);
804 dc.SetBrush(*GetBackgroundBrush());
805
806 // Add 1 because the pen is transparent. Under Motif, may be different.
807 dc.DrawRectangle(
808 m_tabViewRect.x,
809 (m_tabViewRect.y - m_topMargin),
810 (m_tabViewRect.width + 1),
811 (m_topMargin + 1)
812 );
813 }
814
815 // Draw layers in reverse order
816 wxTabLayerList::compatibility_iterator node = m_layers.GetLast();
817 while (node)
818 {
819 wxTabLayer *layer = (wxTabLayer *)node->GetData();
820 wxList::compatibility_iterator node2 = layer->GetFirst();
821 while (node2)
822 {
823 wxTabControl *control = (wxTabControl *)node2->GetData();
824 control->OnDraw(dc, (!node2->GetNext()));
825 node2 = node2->GetNext();
826 }
827
828 node = node->GetPrevious();
829 }
830
831
832 #ifndef wxUSE_NEW_METHOD
833 if (GetTabStyle() & wxTAB_STYLE_DRAW_BOX)
834 {
835 dc.SetPen(* GetShadowPen());
836
837 // Draw bottom line
838 dc.DrawLine(
839 (GetViewRect().x + 1),
840 (GetViewRect().y + GetViewRect().height),
841 (GetViewRect().x + GetViewRect().width + 1),
842 (GetViewRect().y + GetViewRect().height)
843 );
844
845 // Draw right line
846 dc.DrawLine(
847 (GetViewRect().x + GetViewRect().width),
848 (GetViewRect().y - GetTopMargin() + 1),
849 (GetViewRect().x + GetViewRect().width),
850 (GetViewRect().y + GetViewRect().height)
851 );
852
853 dc.SetPen(* wxBLACK_PEN);
854
855 // Draw bottom line
856 dc.DrawLine(
857 (GetViewRect().x),
858 (GetViewRect().y + GetViewRect().height + 1),
859 #if defined(__WXMOTIF__)
860 (GetViewRect().x + GetViewRect().width + 1),
861 #else
862 (GetViewRect().x + GetViewRect().width + 2),
863 #endif
864
865 (GetViewRect().y + GetViewRect().height + 1)
866 );
867
868 // Draw right line
869 dc.DrawLine(
870 (GetViewRect().x + GetViewRect().width + 1),
871 (GetViewRect().y - GetTopMargin()),
872 (GetViewRect().x + GetViewRect().width + 1),
873 (GetViewRect().y + GetViewRect().height + 1)
874 );
875 }
876 #endif
877 }
878
879 // Process mouse event, return false if we didn't process it
880 bool wxTabView::OnEvent(wxMouseEvent& event)
881 {
882 if (!event.LeftDown())
883 return false;
884
885 wxCoord x, y;
886 event.GetPosition(&x, &y);
887
888 wxTabControl *hitControl = (wxTabControl *) NULL;
889
890 wxTabLayerList::compatibility_iterator node = m_layers.GetFirst();
891 while (node)
892 {
893 wxTabLayer *layer = (wxTabLayer *)node->GetData();
894 wxList::compatibility_iterator node2 = layer->GetFirst();
895 while (node2)
896 {
897 wxTabControl *control = (wxTabControl *)node2->GetData();
898 if (control->HitTest((int)x, (int)y))
899 {
900 hitControl = control;
901 node = wxTabLayerList::compatibility_iterator();
902 node2 = wxList::compatibility_iterator();
903 }
904 else
905 node2 = node2->GetNext();
906 }
907
908 if (node)
909 node = node->GetNext();
910 }
911
912 if (!hitControl)
913 return false;
914
915 wxTabControl *currentTab = FindTabControlForId(m_tabSelection);
916
917 if (hitControl == currentTab)
918 return false;
919
920 ChangeTab(hitControl);
921
922 return true;
923 }
924
925 bool wxTabView::ChangeTab(wxTabControl *control)
926 {
927 wxTabControl *currentTab = FindTabControlForId(m_tabSelection);
928 int oldTab = -1;
929 if (currentTab)
930 oldTab = currentTab->GetId();
931
932 if (control == currentTab)
933 return true;
934
935 if (m_layers.GetCount() == 0)
936 return false;
937
938 if (!OnTabPreActivate(control->GetId(), oldTab))
939 return false;
940
941 // Move the tab to the bottom
942 MoveSelectionTab(control);
943
944 if (currentTab)
945 currentTab->SetSelected(false);
946
947 control->SetSelected(true);
948 m_tabSelection = control->GetId();
949
950 OnTabActivate(control->GetId(), oldTab);
951
952 // Leave window refresh for the implementing window
953
954 return true;
955 }
956
957 // Move the selected tab to the bottom layer, if necessary,
958 // without calling app activation code
959 bool wxTabView::MoveSelectionTab(wxTabControl *control)
960 {
961 if (m_layers.GetCount() == 0)
962 return false;
963
964 wxTabLayer *firstLayer = (wxTabLayer *)m_layers.GetFirst()->GetData();
965
966 // Find what column this tab is at, so we can swap with the one at the bottom.
967 // If we're on the bottom layer, then no need to swap.
968 if (!firstLayer->Member(control))
969 {
970 // Do a swap
971 int col = 0;
972 wxList::compatibility_iterator thisNode = FindTabNodeAndColumn(control, &col);
973 if (!thisNode)
974 return false;
975 wxList::compatibility_iterator otherNode = firstLayer->Item(col);
976 if (!otherNode)
977 return false;
978
979 // If this is already in the bottom layer, return now
980 if (otherNode == thisNode)
981 return true;
982
983 wxTabControl *otherTab = (wxTabControl *)otherNode->GetData();
984
985 // We now have pointers to the tab to be changed to,
986 // and the tab on the first layer. Swap tab structures and
987 // position details.
988
989 int thisX = control->GetX();
990 int thisY = control->GetY();
991 int thisColPos = control->GetColPosition();
992 int otherX = otherTab->GetX();
993 int otherY = otherTab->GetY();
994 int otherColPos = otherTab->GetColPosition();
995
996 control->SetPosition(otherX, otherY);
997 control->SetColPosition(otherColPos);
998 otherTab->SetPosition(thisX, thisY);
999 otherTab->SetColPosition(thisColPos);
1000
1001 // Swap the data for the nodes
1002 thisNode->SetData(otherTab);
1003 otherNode->SetData(control);
1004 }
1005 return true;
1006 }
1007
1008 // Called when a tab is activated
1009 void wxTabView::OnTabActivate(int /*activateId*/, int /*deactivateId*/)
1010 {
1011 }
1012
1013 void wxTabView::SetHighlightColour(const wxColour& col)
1014 {
1015 m_highlightColour = col;
1016 m_highlightPen = wxThePenList->FindOrCreatePen(col, 1, wxSOLID);
1017 }
1018
1019 void wxTabView::SetShadowColour(const wxColour& col)
1020 {
1021 m_shadowColour = col;
1022 m_shadowPen = wxThePenList->FindOrCreatePen(col, 1, wxSOLID);
1023 }
1024
1025 void wxTabView::SetBackgroundColour(const wxColour& col)
1026 {
1027 m_backgroundColour = col;
1028 m_backgroundPen = wxThePenList->FindOrCreatePen(col, 1, wxSOLID);
1029 m_backgroundBrush = wxTheBrushList->FindOrCreateBrush(col, wxSOLID);
1030 }
1031
1032 void wxTabView::SetTabSelection(int sel, bool activateTool)
1033 {
1034 if ( sel==m_tabSelection )
1035 return;
1036
1037 int oldSel = m_tabSelection;
1038 wxTabControl *control = FindTabControlForId(sel);
1039 wxTabControl *oldControl = FindTabControlForId(m_tabSelection);
1040
1041 if (!OnTabPreActivate(sel, oldSel))
1042 return;
1043
1044 if (control)
1045 control->SetSelected((sel != -1)); // TODO ??
1046 else if (sel != -1)
1047 {
1048 wxFAIL_MSG(_("Could not find tab for id"));
1049 return;
1050 }
1051
1052 if (oldControl)
1053 oldControl->SetSelected(false);
1054
1055 m_tabSelection = sel;
1056
1057 if (control)
1058 MoveSelectionTab(control);
1059
1060 if (activateTool)
1061 OnTabActivate(sel, oldSel);
1062 }
1063
1064 // Find tab control for id
1065 wxTabControl *wxTabView::FindTabControlForId(int id) const
1066 {
1067 wxTabLayerList::compatibility_iterator node1 = m_layers.GetFirst();
1068 while (node1)
1069 {
1070 wxTabLayer *layer = (wxTabLayer *)node1->GetData();
1071 wxList::compatibility_iterator node2 = layer->GetFirst();
1072 while (node2)
1073 {
1074 wxTabControl *control = (wxTabControl *)node2->GetData();
1075 if (control->GetId() == id)
1076 return control;
1077 node2 = node2->GetNext();
1078 }
1079 node1 = node1->GetNext();
1080 }
1081 return (wxTabControl *) NULL;
1082 }
1083
1084 // Find tab control for layer, position (starting from zero)
1085 wxTabControl *wxTabView::FindTabControlForPosition(int layer, int position) const
1086 {
1087 wxTabLayerList::compatibility_iterator node1 = m_layers.Item(layer);
1088 if (!node1)
1089 return (wxTabControl *) NULL;
1090 wxTabLayer *tabLayer = (wxTabLayer *)node1->GetData();
1091 wxList::compatibility_iterator node2 = tabLayer->Item(position);
1092 if (!node2)
1093 return (wxTabControl *) NULL;
1094 return (wxTabControl *)node2->GetData();
1095 }
1096
1097 // Find the node and the column at which this control is positioned.
1098 wxList::compatibility_iterator wxTabView::FindTabNodeAndColumn(wxTabControl *control, int *col) const
1099 {
1100 wxTabLayerList::compatibility_iterator node1 = m_layers.GetFirst();
1101 while (node1)
1102 {
1103 wxTabLayer *layer = (wxTabLayer *)node1->GetData();
1104 int c = 0;
1105 wxList::compatibility_iterator node2 = layer->GetFirst();
1106 while (node2)
1107 {
1108 wxTabControl *cnt = (wxTabControl *)node2->GetData();
1109 if (cnt == control)
1110 {
1111 *col = c;
1112 return node2;
1113 }
1114 node2 = node2->GetNext();
1115 c ++;
1116 }
1117 node1 = node1->GetNext();
1118 }
1119 return wxList::compatibility_iterator();
1120 }
1121
1122 int wxTabView::CalculateTabWidth(int noTabs, bool adjustView)
1123 {
1124 m_tabWidth = (int)((m_tabViewRect.width - ((noTabs - 1)*GetHorizontalTabSpacing()))/noTabs);
1125 if (adjustView)
1126 {
1127 m_tabViewRect.width = noTabs*m_tabWidth + ((noTabs-1)*GetHorizontalTabSpacing());
1128 }
1129 return m_tabWidth;
1130 }
1131
1132 /*
1133 * wxTabbedDialog
1134 */
1135
1136 IMPLEMENT_CLASS(wxTabbedDialog, wxDialog)
1137
1138 BEGIN_EVENT_TABLE(wxTabbedDialog, wxDialog)
1139 EVT_CLOSE(wxTabbedDialog::OnCloseWindow)
1140 EVT_MOUSE_EVENTS(wxTabbedDialog::OnMouseEvent)
1141 EVT_PAINT(wxTabbedDialog::OnPaint)
1142 END_EVENT_TABLE()
1143
1144 wxTabbedDialog::wxTabbedDialog(wxWindow *parent, wxWindowID id,
1145 const wxString& title,
1146 const wxPoint& pos, const wxSize& size,
1147 long windowStyle, const wxString& name):
1148 wxDialog(parent, id, title, pos, size, windowStyle, name)
1149 {
1150 m_tabView = (wxTabView *) NULL;
1151 }
1152
1153 wxTabbedDialog::~wxTabbedDialog(void)
1154 {
1155 if (m_tabView)
1156 delete m_tabView;
1157 }
1158
1159 void wxTabbedDialog::OnCloseWindow(wxCloseEvent& WXUNUSED(event) )
1160 {
1161 Destroy();
1162 }
1163
1164 void wxTabbedDialog::OnMouseEvent(wxMouseEvent& event )
1165 {
1166 if (m_tabView)
1167 m_tabView->OnEvent(event);
1168 }
1169
1170 void wxTabbedDialog::OnPaint(wxPaintEvent& WXUNUSED(event) )
1171 {
1172 wxPaintDC dc(this);
1173 if (m_tabView)
1174 m_tabView->Draw(dc);
1175 }
1176
1177 /*
1178 * wxTabbedPanel
1179 */
1180
1181 IMPLEMENT_CLASS(wxTabbedPanel, wxPanel)
1182
1183 BEGIN_EVENT_TABLE(wxTabbedPanel, wxPanel)
1184 EVT_MOUSE_EVENTS(wxTabbedPanel::OnMouseEvent)
1185 EVT_PAINT(wxTabbedPanel::OnPaint)
1186 END_EVENT_TABLE()
1187
1188 wxTabbedPanel::wxTabbedPanel(wxWindow *parent, wxWindowID id, const wxPoint& pos,
1189 const wxSize& size, long windowStyle, const wxString& name):
1190 wxPanel(parent, id, pos, size, windowStyle, name)
1191 {
1192 m_tabView = (wxTabView *) NULL;
1193 }
1194
1195 wxTabbedPanel::~wxTabbedPanel(void)
1196 {
1197 delete m_tabView;
1198 }
1199
1200 void wxTabbedPanel::OnMouseEvent(wxMouseEvent& event)
1201 {
1202 if (m_tabView)
1203 m_tabView->OnEvent(event);
1204 }
1205
1206 void wxTabbedPanel::OnPaint(wxPaintEvent& WXUNUSED(event) )
1207 {
1208 wxPaintDC dc(this);
1209 if (m_tabView)
1210 m_tabView->Draw(dc);
1211 }
1212
1213 /*
1214 * wxPanelTabView
1215 */
1216
1217 IMPLEMENT_CLASS(wxPanelTabView, wxTabView)
1218
1219 wxPanelTabView::wxPanelTabView(wxPanel *pan, long style)
1220 : wxTabView(style)
1221 {
1222 m_panel = pan;
1223 m_currentWindow = (wxWindow *) NULL;
1224
1225 if (m_panel->IsKindOf(CLASSINFO(wxTabbedDialog)))
1226 ((wxTabbedDialog *)m_panel)->SetTabView(this);
1227 else if (m_panel->IsKindOf(CLASSINFO(wxTabbedPanel)))
1228 ((wxTabbedPanel *)m_panel)->SetTabView(this);
1229
1230 SetWindow(m_panel);
1231 }
1232
1233 wxPanelTabView::~wxPanelTabView(void)
1234 {
1235 ClearWindows(true);
1236 }
1237
1238 // Called when a tab is activated
1239 void wxPanelTabView::OnTabActivate(int activateId, int deactivateId)
1240 {
1241 if (!m_panel)
1242 return;
1243
1244 wxWindow *oldWindow = ((deactivateId == -1) ? 0 : GetTabWindow(deactivateId));
1245 wxWindow *newWindow = GetTabWindow(activateId);
1246
1247 if (oldWindow)
1248 oldWindow->Show(false);
1249 if (newWindow)
1250 newWindow->Show(true);
1251
1252 m_panel->Refresh();
1253 }
1254
1255
1256 void wxPanelTabView::AddTabWindow(int id, wxWindow *window)
1257 {
1258 wxASSERT(m_tabWindows.find(id) == m_tabWindows.end());
1259 m_tabWindows[id] = window;
1260 window->Show(false);
1261 }
1262
1263 wxWindow *wxPanelTabView::GetTabWindow(int id) const
1264 {
1265 wxIntToWindowHashMap::const_iterator it = m_tabWindows.find(id);
1266 return it == m_tabWindows.end() ? NULL : it->second;
1267 }
1268
1269 void wxPanelTabView::ClearWindows(bool deleteWindows)
1270 {
1271 if (deleteWindows)
1272 WX_CLEAR_HASH_MAP(wxIntToWindowHashMap, m_tabWindows);
1273 m_tabWindows.clear();
1274 }
1275
1276 void wxPanelTabView::ShowWindowForTab(int id)
1277 {
1278 wxWindow *newWindow = GetTabWindow(id);
1279 if (newWindow == m_currentWindow)
1280 return;
1281 if (m_currentWindow)
1282 m_currentWindow->Show(false);
1283 newWindow->Show(true);
1284 newWindow->Refresh();
1285 }
1286
1287 #endif // wxUSE_TAB_DIALOG