1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// Copyright : (C) 2015 Eran Ifrah
// File name : Notebook.h
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#ifndef NOTEBOOK_H
#define NOTEBOOK_H
#include <wx/panel.h>
#include <wx/simplebook.h>
#include <vector>
#include <list>
#include <wx/settings.h>
#include <wx/dcmemory.h>
#include <wx/sharedptr.h>
#include <wx/bookctrl.h>
#include "windowstack.h"
#include <wx/dynarray.h>
#include <wx/dnd.h>
#include "clTabRenderer.h"
class Notebook;
class wxMenu;
class clTabCtrl;
// DnD support of tabs
class WXDLLIMPEXP_SDK clTabCtrlDropTarget : public wxTextDropTarget
{
clTabCtrl* m_tabCtrl;
public:
clTabCtrlDropTarget(clTabCtrl* tabCtrl);
virtual ~clTabCtrlDropTarget();
virtual bool OnDropText(wxCoord x, wxCoord y, const wxString& data);
};
class WXDLLIMPEXP_SDK clTabHistory
{
wxArrayPtrVoid m_history;
wxWindow* m_page; /// The page to add to the hisotry
public:
typedef wxSharedPtr<clTabHistory> Ptr_t;
public:
clTabHistory();
virtual ~clTabHistory();
void Push(wxWindow* page);
void Pop(wxWindow* page);
wxWindow* PrevPage();
/**
* @brief clear the history
*/
void Clear();
/**
* @brief return the tabbing history
* @return
*/
const wxArrayPtrVoid& GetHistory() const { return m_history; }
};
/**
* @class clTabCtrl
* @author Eran Ifrah
* @brief The Window that all the tabs are drawn on
*/
class WXDLLIMPEXP_SDK clTabCtrl : public wxPanel
{
int m_height;
int m_vTabsWidth;
clTabInfo::Vec_t m_tabs;
friend class Notebook;
friend class clTabCtrlDropTarget;
size_t m_style;
clTabColours m_colours;
clTabInfo::Vec_t m_visibleTabs;
int m_closeButtonClickedIndex;
wxMenu* m_contextMenu;
wxRect m_chevronRect;
clTabHistory::Ptr_t m_history;
clTabRenderer::Ptr_t m_art;
void DoChangeSelection(size_t index);
protected:
void OnPaint(wxPaintEvent& e);
void OnEraseBG(wxEraseEvent& e);
void OnSize(wxSizeEvent& event);
void OnWindowKeyDown(wxKeyEvent& event);
void OnLeftDown(wxMouseEvent& event);
void OnRightUp(wxMouseEvent& event);
void OnLeftUp(wxMouseEvent& event);
void OnLeftDClick(wxMouseEvent& event);
void OnMouseMotion(wxMouseEvent& event);
void OnMouseMiddleClick(wxMouseEvent& event);
void OnContextMenu(wxContextMenuEvent& event);
int DoGetPageIndex(wxWindow* win) const;
int DoGetPageIndex(const wxString& label) const;
void DoDrawBottomBox(clTabInfo::Ptr_t activeTab, const wxRect& clientRect, wxDC& dc, const clTabColours& colours);
bool ShiftRight(clTabInfo::Vec_t& tabs);
bool ShiftBottom(clTabInfo::Vec_t& tabs);
bool IsActiveTabInList(const clTabInfo::Vec_t& tabs) const;
bool IsActiveTabVisible(const clTabInfo::Vec_t& tabs) const;
/**
* @brief loop over the tabs and set their coordiantes
*/
void DoUpdateCoordiantes(clTabInfo::Vec_t& tabs);
/**
* @brief get list of tabs to draw. This call always returns the active tab as part of the list
* It also ensures that we draw as much tabs as we can.
* @param offset reset the 0 based index from m_tabs
*/
void UpdateVisibleTabs();
/**
* @brief calculate and set the tab ctrl size
*/
void DoSetBestSize();
clTabInfo::Ptr_t GetTabInfo(size_t index);
clTabInfo::Ptr_t GetTabInfo(size_t index) const;
clTabInfo::Ptr_t GetTabInfo(wxWindow* page);
clTabInfo::Ptr_t GetActiveTabInfo();
WindowStack* GetStack();
void DoDeletePage(size_t page) { RemovePage(page, true, true); }
void DoShowTabList();
void DoUpdateXCoordFromPage(wxWindow* page, int diff);
public:
clTabCtrl(wxWindow* notebook, size_t style);
virtual ~clTabCtrl();
/**
* @brief return the art class used by this tab control
*/
clTabRenderer::Ptr_t GetArt() { return m_art; }
/**
* @brief replace the art used by this tab control
*/
void SetArt(clTabRenderer::Ptr_t art)
{
m_art = art;
DoSetBestSize();
Refresh();
}
bool IsVerticalTabs() const;
void SetColours(const clTabColours& colours) { this->m_colours = colours; }
const clTabColours& GetColours() const { return m_colours; }
/**
* @brief test if pt is on one of the visible tabs return its index
* @param pt mouse click position
* @param realPosition [output] the index position in the m_tabs array
* @param tabHit [output] the index position in the m_visibleTabs array
*/
void TestPoint(const wxPoint& pt, int& realPosition, int& tabHit);
/**
* @brief Move the active tab to a new position
* @param newIndex the new position. 0-based index in the m_tabs array
*/
bool MoveActiveToIndex(int newIndex);
/**
* @brief return true if index is in the tabs vector range
*/
bool IsIndexValid(size_t index) const;
void SetStyle(size_t style);
size_t GetStyle() const { return m_style; }
/**
* @brief update the selected tab. This function also fires an event
*/
int SetSelection(size_t tabIdx);
/**
* @brief update the selected tab. This function does not fire an event
*/
int ChangeSelection(size_t tabIdx);
/**
* @brief return the current selection. return wxNOT_FOUND if non is selected
*/
int GetSelection() const;
/**
* @brief Sets the text for the given page.
*/
bool SetPageText(size_t page, const wxString& text);
wxString GetPageText(size_t page) const;
void AddPage(clTabInfo::Ptr_t tab);
bool InsertPage(size_t index, clTabInfo::Ptr_t tab);
void SetPageBitmap(size_t index, const wxBitmap& bmp);
wxBitmap GetPageBitmap(size_t index) const;
wxWindow* GetPage(size_t index) const;
void GetAllPages(std::vector<wxWindow*>& pages);
int FindPage(wxWindow* page) const;
bool RemovePage(size_t page, bool notify, bool deletePage);
bool DeleteAllPages();
void SetMenu(wxMenu* menu);
bool SetPageToolTip(size_t page, const wxString& tooltip);
const clTabInfo::Vec_t& GetTabs() const { return m_tabs; }
clTabHistory::Ptr_t GetHistory() const { return m_history; }
};
/**
* @class Notebook
* @author Eran Ifrah
* @brief A modern notebook (similar to the ones seen on Sublime Text and Atom editors
* for wxWidgets. The class implementation uses wxSimplebook as the tab container and a
* custom drawing tab area (see above the class clTabCtrl)
*/
class WXDLLIMPEXP_SDK Notebook : public wxPanel
{
WindowStack* m_windows;
clTabCtrl* m_tabCtrl;
friend class clTabCtrl;
protected:
void DoChangeSelection(wxWindow* page);
bool IsVerticalTabs() const { return m_tabCtrl->IsVerticalTabs(); }
public:
/**
* Constructor
*/
Notebook(wxWindow* parent,
wxWindowID id = wxID_ANY,
const wxPoint& pos = wxDefaultPosition,
const wxSize& size = wxDefaultSize,
long style = 0,
const wxString& name = wxEmptyString);
/**
* @brief update the notebook art class and refresh
*/
void SetArt(clTabRenderer::Ptr_t art) { m_tabCtrl->SetArt(art); }
/**
* @brief set the notebook style. The style bits are kNotebook_* (you can set several
* styles OR-ed)
*/
void SetStyle(size_t style);
/**
* @brief set the tab direction
*/
void SetTabDirection(wxDirection d);
/**
* @brief return the book style
*/
size_t GetStyle() const { return m_tabCtrl->GetStyle(); }
/**
* @brief enable a specific style in the notebook
*/
void EnableStyle(NotebookStyle style, bool enable);
/**
* destructor
*/
virtual ~Notebook();
/**
* @brief append page to the notebook
*/
void AddPage(wxWindow* page, const wxString& label, bool selected = false, const wxBitmap& bmp = wxNullBitmap);
/**
* @brief insert page at a specified position
*/
bool InsertPage(
size_t index, wxWindow* page, const wxString& label, bool selected = false, const wxBitmap& bmp = wxNullBitmap);
/**
* @brief return the currently selected page or null
*/
wxWindow* GetCurrentPage() const;
/**
* @brief Returns the index of the specified tab window or wxNOT_FOUND if not found
*/
int FindPage(wxWindow* page) const;
/**
* @brief Deletes the specified page, without deleting the associated window
*/
bool RemovePage(size_t page, bool notify = false);
/**
* @brief Deletes the specified page and the associated window
*/
bool DeletePage(size_t page, bool notify = true);
/**
* @brief Deletes all pages
*/
bool DeleteAllPages();
/**
* @brief set a new selection. This function fires an event that can be vetoed
*/
int SetSelection(size_t selection) { return m_tabCtrl->SetSelection(selection); }
/**
* @brief set new selection. No events are fired
*/
int ChangeSelection(size_t selection) { return m_tabCtrl->ChangeSelection(selection); }
/**
* @brief return the currently selected page, return wxNOT_FOUND if non found
*/
int GetSelection() const { return m_tabCtrl->GetSelection(); }
/**
* @brief Sets the text for the given page.
*/
bool SetPageText(size_t page, const wxString& text) { return m_tabCtrl->SetPageText(page, text); }
/**
* @brief Returns the string for the given page
*/
wxString GetPageText(size_t page) const { return m_tabCtrl->GetPageText(page); }
/**
* @brief set the image for the given page
*/
void SetPageBitmap(size_t index, const wxBitmap& bmp) { m_tabCtrl->SetPageBitmap(index, bmp); }
/**
* @brief return bitmap for a given page. Return wxNullBitmap if invalid page
*/
wxBitmap GetPageBitmap(size_t index) const { return m_tabCtrl->GetPageBitmap(index); }
// Base class members...
virtual bool SetPageImage(size_t page, int image)
{
wxUnusedVar(page);
wxUnusedVar(image);
return false;
}
virtual int GetPageImage(size_t n) const { return wxNOT_FOUND; }
/**
* @brief return the index of a given window in the tab control
* @param window
* @return return window index, or wxNOT_FOUND
*/
int GetPageIndex(wxWindow* window) const { return m_tabCtrl->DoGetPageIndex(window); }
/**
* @brief return the index of a given window by its title
*/
int GetPageIndex(const wxString& label) const { return m_tabCtrl->DoGetPageIndex(label); }
/**
* @brief Returns the number of pages in the control
*/
size_t GetPageCount() const { return m_tabCtrl->GetTabs().size(); }
/**
* @brief Returns the window at the given page position.
*/
wxWindow* GetPage(size_t index) const { return m_tabCtrl->GetPage(index); }
/**
* @brief return an array of all the windows managed by this notebook
*/
void GetAllPages(std::vector<wxWindow*>& pages) { m_tabCtrl->GetAllPages(pages); }
/**
* @brief return all tabs info
* @param tabs [output]
*/
size_t GetAllTabs(clTabInfo::Vec_t& tabs)
{
tabs = m_tabCtrl->GetTabs();
return tabs.size();
}
/**
* @brief set a context menu to be shown whe context menu is requested
* on a tab label
*/
void SetMenu(wxMenu* menu) { m_tabCtrl->SetMenu(menu); }
/**
* @brief Sets the tool tip displayed when hovering over the tab label of the page
* @return true if tool tip was updated, false if it failed, e.g. because the page index is invalid.
*/
bool SetPageToolTip(size_t page, const wxString& tooltip) { return m_tabCtrl->SetPageToolTip(page, tooltip); }
/**
* @brief return the tabbing history
* @return
*/
clTabHistory::Ptr_t GetHistory() const { return m_tabCtrl->GetHistory(); }
};
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_SDK, wxEVT_BOOK_PAGE_CHANGING, wxBookCtrlEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_SDK, wxEVT_BOOK_PAGE_CHANGED, wxBookCtrlEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_SDK, wxEVT_BOOK_PAGE_CLOSING, wxBookCtrlEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_SDK, wxEVT_BOOK_PAGE_CLOSED, wxBookCtrlEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_SDK, wxEVT_BOOK_TAB_CONTEXT_MENU, wxBookCtrlEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_SDK, wxEVT_BOOK_PAGE_CLOSE_BUTTON, wxBookCtrlEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_SDK, wxEVT_BOOK_TAB_DCLICKED, wxBookCtrlEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_SDK, wxEVT_BOOK_NAVIGATING, wxBookCtrlEvent);
wxDECLARE_EXPORTED_EVENT(WXDLLIMPEXP_SDK, wxEVT_BOOK_TABAREA_DCLICKED, wxBookCtrlEvent);
#endif // NOTEBOOK_H
|