~ubuntu-branches/ubuntu/oneiric/codelite/oneiric

« back to all changes in this revision

Viewing changes to Plugin/notebook_ex.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2010-04-09 02:03:19 UTC
  • mfrom: (0.1.3 sid) (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100409020319-302693n3vroavxwe
Tags: 2.5.2.4031~dfsg-1
* New upstream release.
* Refresh patches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <wx/app.h>
 
2
#include "cl_aui_notebook_art.h"
 
3
#include "drawingutils.h"
 
4
#include <wx/xrc/xmlres.h>
 
5
#include <wx/choicebk.h>
 
6
#include <wx/notebook.h>
 
7
#include "notebook_ex_nav_dlg.h"
 
8
#include "notebook_ex.h"
 
9
#include <wx/button.h>
 
10
#include "wx/sizer.h"
 
11
#include <wx/log.h>
 
12
#include <wx/wupdlock.h>
 
13
 
 
14
#ifdef __WXMSW__
 
15
#    include <wx/button.h>
 
16
#    include <wx/imaglist.h>
 
17
#    include <wx/image.h>
 
18
#elif defined(__WXGTK__)
 
19
#    include <gtk/gtk.h>
 
20
#    include <wx/imaglist.h>
 
21
#endif
 
22
 
 
23
const wxEventType wxEVT_COMMAND_BOOK_PAGE_CHANGED         = XRCID("notebook_page_changing");
 
24
const wxEventType wxEVT_COMMAND_BOOK_PAGE_CHANGING        = XRCID("notebook_page_changed");
 
25
const wxEventType wxEVT_COMMAND_BOOK_PAGE_CLOSING         = XRCID("notebook_page_closing");
 
26
const wxEventType wxEVT_COMMAND_BOOK_PAGE_CLOSED          = XRCID("notebook_page_closed");
 
27
const wxEventType wxEVT_COMMAND_BOOK_PAGE_MIDDLE_CLICKED  = XRCID("notebook_page_middle_clicked");
 
28
const wxEventType wxEVT_COMMAND_BOOK_PAGE_X_CLICKED       = XRCID("notebook_page_x_btn_clicked");
 
29
const wxEventType wxEVT_COMMAND_BOOK_BG_DCLICK            = XRCID("notebook_page_bg_dclick");
 
30
 
 
31
#define SHOW_POPUP_MENU 1234534
 
32
 
 
33
#define X_IMG_NONE    -1
 
34
#define X_IMG_NORMAL   0
 
35
#define X_IMG_PRESSED  1
 
36
#define X_IMG_DISABLED 2
 
37
 
 
38
Notebook::Notebook(wxWindow *parent, wxWindowID id, const wxPoint &pos, const wxSize &size, long style)
 
39
                : wxAuiNotebook(parent, id, pos, size, style | wxNO_BORDER | wxAUI_NB_TAB_MOVE | wxAUI_NB_TAB_SPLIT)
 
40
                , m_popupWin(NULL)
 
41
                , m_contextMenu(NULL)
 
42
                , m_style(style)
 
43
                , m_leftDownTabIdx(npos)
 
44
                , m_notify (true)
 
45
{
 
46
        Initialize();
 
47
 
 
48
        m_leftDownPos = wxPoint();
 
49
 
 
50
        // Connect events
 
51
        Connect(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGED,       wxAuiNotebookEventHandler(Notebook::OnInternalPageChanged),  NULL, this);
 
52
        Connect(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING,      wxAuiNotebookEventHandler(Notebook::OnInternalPageChanging), NULL, this);
 
53
        Connect(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CLOSED,        wxAuiNotebookEventHandler(Notebook::OnInternalPageClosed),   NULL, this);
 
54
        Connect(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CLOSE,         wxAuiNotebookEventHandler(Notebook::OnInternalPageClosing),  NULL, this);
 
55
        Connect(wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_DOWN,    wxAuiNotebookEventHandler(Notebook::OnTabMiddle),            NULL, this);
 
56
        Connect(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_DOWN,     wxAuiNotebookEventHandler(Notebook::OnTabRightDown),         NULL, this);
 
57
        Connect(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_UP,       wxAuiNotebookEventHandler(Notebook::OnTabRightUp),           NULL, this);
 
58
        Connect(wxEVT_COMMAND_AUINOTEBOOK_BG_DCLICK,          wxAuiNotebookEventHandler(Notebook::OnBgDclick),             NULL, this);
 
59
 
 
60
#ifdef __WXMSW__
 
61
        Connect(wxEVT_SET_FOCUS,                              wxFocusEventHandler(Notebook::OnFocus),                      NULL, this);
 
62
#endif
 
63
        Connect(wxEVT_NAVIGATION_KEY,                         wxNavigationKeyEventHandler(Notebook::OnNavigationKey),      NULL, this);
 
64
        Connect(SHOW_POPUP_MENU, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(Notebook::OnInternalMenu),             NULL, this);
 
65
 
 
66
        // On Linux, we disable the transparent drag since it will lead to crash when used with compiz / KDE4
 
67
#if defined (__WXGTK__) && defined (__WXDEBUG__)
 
68
        m_mgr.SetFlags(wxAUI_MGR_ALLOW_FLOATING|wxAUI_MGR_ALLOW_ACTIVE_PANE|wxAUI_MGR_TRANSPARENT_DRAG|wxAUI_MGR_RECTANGLE_HINT);
 
69
 
 
70
#elif defined(__WXGTK__)
 
71
    m_mgr.SetFlags((m_mgr.GetFlags() | wxAUI_MGR_VENETIAN_BLINDS_HINT) & ~wxAUI_MGR_TRANSPARENT_HINT);
 
72
 
 
73
#endif  //
 
74
 
 
75
        SetArtProvider(new clAuiTabArt());
 
76
}
 
77
 
 
78
Notebook::~Notebook()
 
79
{
 
80
        Disconnect(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGED,    wxAuiNotebookEventHandler(Notebook::OnInternalPageChanged),  NULL, this);
 
81
        Disconnect(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CHANGING,   wxAuiNotebookEventHandler(Notebook::OnInternalPageChanging), NULL, this);
 
82
        Disconnect(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CLOSED,     wxAuiNotebookEventHandler(Notebook::OnInternalPageClosed),   NULL, this);
 
83
        Disconnect(wxEVT_COMMAND_AUINOTEBOOK_PAGE_CLOSE,      wxAuiNotebookEventHandler(Notebook::OnInternalPageClosing),  NULL, this);
 
84
        Disconnect(wxEVT_COMMAND_AUINOTEBOOK_TAB_MIDDLE_DOWN, wxAuiNotebookEventHandler(Notebook::OnTabMiddle),            NULL, this);
 
85
        Disconnect(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_DOWN,  wxAuiNotebookEventHandler(Notebook::OnTabRightDown),         NULL, this);
 
86
        Disconnect(wxEVT_COMMAND_AUINOTEBOOK_BG_DCLICK,       wxAuiNotebookEventHandler(Notebook::OnBgDclick),             NULL, this);
 
87
        Disconnect(wxEVT_COMMAND_AUINOTEBOOK_TAB_RIGHT_UP,    wxAuiNotebookEventHandler(Notebook::OnTabRightUp),           NULL, this);
 
88
        
 
89
        Disconnect(wxEVT_NAVIGATION_KEY,                 wxNavigationKeyEventHandler(Notebook::OnNavigationKey),  NULL, this);
 
90
#ifdef __WXMSW__
 
91
        Disconnect(wxEVT_SET_FOCUS,                              wxFocusEventHandler(Notebook::OnFocus),                      NULL, this);
 
92
#endif
 
93
        Disconnect(SHOW_POPUP_MENU, wxEVT_COMMAND_MENU_SELECTED,          wxCommandEventHandler(Notebook::OnInternalMenu),   NULL, this);
 
94
}
 
95
 
 
96
bool Notebook::AddPage(wxWindow *win, const wxString &text, bool selected, const wxBitmap& bmp)
 
97
{
 
98
        wxWindowUpdateLocker locker(this);
 
99
        win->Reparent(this);
 
100
        if(wxAuiNotebook::InsertPage(GetPageCount(), win, text, selected, bmp)){
 
101
                win->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(Notebook::OnKeyDown),  NULL, this);
 
102
                PushPageHistory(win);
 
103
                return true;
 
104
        }
 
105
        return false;
 
106
}
 
107
 
 
108
bool Notebook::InsertPage(size_t index, wxWindow* win, const wxString& text, bool selected, const wxBitmap &bmp)
 
109
{
 
110
        win->Reparent(this);
 
111
        if(wxAuiNotebook::InsertPage(index, win, text, selected, bmp)){
 
112
                win->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(Notebook::OnKeyDown),  NULL, this);
 
113
                PushPageHistory(win);
 
114
                return true;
 
115
        }
 
116
        return false;
 
117
}
 
118
 
 
119
 
 
120
void Notebook::Initialize()
 
121
{
 
122
}
 
123
 
 
124
void Notebook::SetSelection(size_t page, bool notify)
 
125
{
 
126
        if (page >= GetPageCount())
 
127
                return;
 
128
 
 
129
        m_notify = notify;
 
130
        wxAuiNotebook::SetSelection(page);
 
131
        m_notify = true;
 
132
 
 
133
        PushPageHistory(GetPage(page));
 
134
}
 
135
 
 
136
size_t Notebook::GetSelection()
 
137
{
 
138
        return static_cast<size_t>(wxAuiNotebook::GetSelection());
 
139
}
 
140
 
 
141
wxWindow* Notebook::GetPage(size_t page)
 
142
{
 
143
        if (page >= GetPageCount())
 
144
                return NULL;
 
145
 
 
146
        return wxAuiNotebook::GetPage(page);
 
147
}
 
148
 
 
149
bool Notebook::RemovePage(size_t page, bool notify)
 
150
{
 
151
        if (notify) {
 
152
                //send event to noitfy that the page has changed
 
153
                NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_CLOSING, GetId());
 
154
                event.SetSelection( page );
 
155
                event.SetEventObject( this );
 
156
                GetEventHandler()->ProcessEvent(event);
 
157
 
 
158
                if (!event.IsAllowed()) {
 
159
                        return false;
 
160
                }
 
161
        }
 
162
 
 
163
        wxWindow* win = GetPage(page);
 
164
        win->Disconnect(wxEVT_KEY_DOWN, wxKeyEventHandler(Notebook::OnKeyDown),  NULL, this);
 
165
 
 
166
        bool rc = wxAuiNotebook::RemovePage(page);
 
167
        if (rc) {
 
168
                PopPageHistory(win);
 
169
        }
 
170
 
 
171
        if (rc && notify) {
 
172
                //send event to noitfy that the page has been closed
 
173
                NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_CLOSED, GetId());
 
174
                event.SetSelection( page );
 
175
                event.SetEventObject( this );
 
176
                GetEventHandler()->AddPendingEvent(event);
 
177
        }
 
178
 
 
179
        return rc;
 
180
}
 
181
 
 
182
bool Notebook::DeletePage(size_t page, bool notify)
 
183
{
 
184
        if (page >= GetPageCount())
 
185
                return false;
 
186
 
 
187
        if (notify) {
 
188
                //send event to noitfy that the page has changed
 
189
                NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_CLOSING, GetId());
 
190
                event.SetSelection( page );
 
191
                event.SetEventObject( this );
 
192
                GetEventHandler()->ProcessEvent(event);
 
193
 
 
194
                if (!event.IsAllowed()) {
 
195
                        return false;
 
196
                }
 
197
        }
 
198
 
 
199
        wxWindow* win = GetPage(page);
 
200
        win->Disconnect(wxEVT_KEY_DOWN, wxKeyEventHandler(Notebook::OnKeyDown),  NULL, this);
 
201
 
 
202
        bool rc = wxAuiNotebook::DeletePage(page);
 
203
        if (rc) {
 
204
                PopPageHistory(win);
 
205
        }
 
206
 
 
207
        if (rc && notify) {
 
208
                //send event to noitfy that the page has been closed
 
209
                NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_CLOSED, GetId());
 
210
                event.SetSelection( page );
 
211
                event.SetEventObject( this );
 
212
                GetEventHandler()->ProcessEvent(event);
 
213
        }
 
214
 
 
215
        return rc;
 
216
 
 
217
}
 
218
 
 
219
wxString Notebook::GetPageText(size_t page) const
 
220
{
 
221
        if (page >= GetPageCount())
 
222
                return wxT("");
 
223
 
 
224
        return wxAuiNotebook::GetPageText(page);
 
225
}
 
226
 
 
227
void Notebook::OnNavigationKey(wxNavigationKeyEvent &e)
 
228
{
 
229
        if ( e.IsWindowChange() ) {
 
230
                if (DoNavigate())
 
231
                        return;
 
232
        }
 
233
        e.Skip();
 
234
}
 
235
 
 
236
const wxArrayPtrVoid& Notebook::GetHistory() const
 
237
{
 
238
        return m_history;
 
239
}
 
240
 
 
241
void Notebook::SetRightClickMenu(wxMenu* menu)
 
242
{
 
243
        m_contextMenu = menu;
 
244
}
 
245
 
 
246
wxWindow* Notebook::GetCurrentPage()
 
247
{
 
248
        size_t selection = GetSelection();
 
249
        if (selection != Notebook::npos) {
 
250
                return GetPage(selection);
 
251
        }
 
252
        return NULL;
 
253
}
 
254
 
 
255
size_t Notebook::GetPageIndex(wxWindow *page)
 
256
{
 
257
        if ( !page )
 
258
                return Notebook::npos;
 
259
 
 
260
        for (size_t i=0; i< GetPageCount(); i++) {
 
261
                if (GetPage(i) == page) {
 
262
                        return i;
 
263
                }
 
264
        }
 
265
        return Notebook::npos;
 
266
}
 
267
 
 
268
size_t Notebook::GetPageIndex(const wxString& text)
 
269
{
 
270
        for (size_t i=0; i< GetPageCount(); i++) {
 
271
 
 
272
                if (GetPageText(i) == text) {
 
273
                        return i;
 
274
                }
 
275
        }
 
276
        return Notebook::npos;
 
277
}
 
278
 
 
279
bool Notebook::SetPageText(size_t index, const wxString &text)
 
280
{
 
281
        if (index >= GetPageCount())
 
282
                return false;
 
283
        return wxAuiNotebook::SetPageText(index, text);
 
284
}
 
285
 
 
286
bool Notebook::DeleteAllPages(bool notify)
 
287
{
 
288
        bool res = true;
 
289
        size_t count = GetPageCount();
 
290
        for (size_t i=0; i<count && res; i++) {
 
291
                res = this->DeletePage(0, notify);
 
292
        }
 
293
        return res;
 
294
}
 
295
 
 
296
void Notebook::PushPageHistory(wxWindow *page)
 
297
{
 
298
        if (page == NULL)
 
299
                return;
 
300
 
 
301
        int where = m_history.Index(page);
 
302
        //remove old entry of this page and re-insert it as first
 
303
        if (where != wxNOT_FOUND) {
 
304
                m_history.Remove(page);
 
305
        }
 
306
        m_history.Insert(page, 0);
 
307
}
 
308
 
 
309
void Notebook::PopPageHistory(wxWindow *page)
 
310
{
 
311
        if( !page )
 
312
                return;
 
313
 
 
314
        int where = m_history.Index(page);
 
315
        while (where != wxNOT_FOUND) {
 
316
                wxWindow *tab = static_cast<wxWindow *>(m_history.Item(where));
 
317
                m_history.Remove(tab);
 
318
 
 
319
                //remove all appearances of this page
 
320
                where = m_history.Index(page);
 
321
        }
 
322
}
 
323
 
 
324
wxWindow* Notebook::GetPreviousSelection()
 
325
{
 
326
        if (m_history.empty()) {
 
327
                return NULL;
 
328
        }
 
329
        //return the top of the heap
 
330
        return static_cast<wxWindow*>( m_history.Item(0));
 
331
}
 
332
 
 
333
void Notebook::OnInternalPageChanged(wxAuiNotebookEvent &e)
 
334
{
 
335
        DoPageChangedEvent(e);
 
336
}
 
337
 
 
338
void Notebook::OnInternalPageChanging(wxAuiNotebookEvent &e)
 
339
{
 
340
        DoPageChangingEvent(e);
 
341
}
 
342
 
 
343
void Notebook::DoPageChangedEvent(wxAuiNotebookEvent& e)
 
344
{
 
345
        if (!m_notify) {
 
346
                e.Skip();
 
347
                return;
 
348
        }
 
349
 
 
350
        //send event to noitfy that the page is changing
 
351
        NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_CHANGED, GetId());
 
352
        event.SetSelection   ( e.GetSelection()    );
 
353
        event.SetOldSelection( e.GetOldSelection() );
 
354
        event.SetEventObject ( this );
 
355
        GetEventHandler()->AddPendingEvent(event);
 
356
 
 
357
        PushPageHistory( GetPage(e.GetSelection()) );
 
358
        e.Skip();
 
359
}
 
360
 
 
361
void Notebook::DoPageChangingEvent(wxAuiNotebookEvent& e)
 
362
{
 
363
        if (!m_notify) {
 
364
                e.Skip();
 
365
                return;
 
366
        }
 
367
 
 
368
        //send event to noitfy that the page is changing
 
369
        NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_CHANGING, GetId());
 
370
        event.SetSelection   ( e.GetSelection()    );
 
371
        event.SetOldSelection( e.GetOldSelection() );
 
372
        event.SetEventObject ( this );
 
373
        GetEventHandler()->ProcessEvent(event);
 
374
 
 
375
        if ( !event.IsAllowed() ) {
 
376
                e.Veto();
 
377
        }
 
378
        e.Skip();
 
379
}
 
380
 
 
381
void Notebook::OnKeyDown(wxKeyEvent& e)
 
382
{
 
383
        if (e.GetKeyCode() == WXK_TAB && e.m_controlDown ) {
 
384
                if (DoNavigate())
 
385
                        return;
 
386
 
 
387
        } else {
 
388
                e.Skip();
 
389
        }
 
390
}
 
391
 
 
392
bool Notebook::DoNavigate()
 
393
{
 
394
        if ( !m_popupWin && GetPageCount() > 1) {
 
395
 
 
396
                m_popupWin = new NotebookNavDialog( this );
 
397
                m_popupWin->ShowModal();
 
398
 
 
399
                wxWindow *page = m_popupWin->GetSelection();
 
400
                m_popupWin->Destroy();
 
401
                m_popupWin = NULL;
 
402
 
 
403
                SetSelection( GetPageIndex(page), true );
 
404
 
 
405
 
 
406
                return true;
 
407
        }
 
408
        return false;
 
409
}
 
410
 
 
411
void Notebook::OnFocus(wxFocusEvent& e)
 
412
{
 
413
        if( m_style & wxVB_PASS_FOCUS) {
 
414
                wxWindow *w = GetCurrentPage();
 
415
                if(w) {
 
416
                        w->SetFocus();
 
417
                }
 
418
        }
 
419
        e.Skip();
 
420
}
 
421
 
 
422
void Notebook::OnInternalPageClosing(wxAuiNotebookEvent& e)
 
423
{
 
424
        e.Skip();
 
425
 
 
426
        NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_CLOSING, GetId());
 
427
        event.SetSelection( (int)GetSelection() );
 
428
        event.SetEventObject( this );
 
429
        GetEventHandler()->ProcessEvent(event);
 
430
 
 
431
        if (!event.IsAllowed()) {
 
432
                e.Veto();
 
433
 
 
434
        } else {
 
435
                // This page is likely to be removed, remove it from the history
 
436
                PopPageHistory( GetPage( static_cast<size_t>( GetSelection() ) ) );
 
437
        }
 
438
}
 
439
 
 
440
void Notebook::OnInternalPageClosed(wxAuiNotebookEvent& e)
 
441
{
 
442
        e.Skip();
 
443
 
 
444
        NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_CLOSED, GetId());
 
445
        event.SetSelection( (int) GetSelection() );
 
446
        event.SetEventObject( this );
 
447
        GetEventHandler()->AddPendingEvent(event);
 
448
 
 
449
}
 
450
 
 
451
void Notebook::OnTabMiddle(wxAuiNotebookEvent& e)
 
452
{
 
453
        if (e.GetSelection() != wxNOT_FOUND && HasCloseMiddle()) {
 
454
                //send event to noitfy that the page is changing
 
455
                NotebookEvent event(wxEVT_COMMAND_BOOK_PAGE_MIDDLE_CLICKED, GetId());
 
456
                event.SetSelection   ( e.GetSelection() );
 
457
                event.SetOldSelection( wxNOT_FOUND );
 
458
                event.SetEventObject ( this );
 
459
                GetEventHandler()->AddPendingEvent(event);
 
460
        }
 
461
}
 
462
 
 
463
void Notebook::OnTabRightDown(wxAuiNotebookEvent& e)
 
464
{
 
465
        if(m_contextMenu) {
 
466
 
 
467
                int where = e.GetSelection();
 
468
                if(where != wxNOT_FOUND && where == static_cast<int>(GetSelection())) {
 
469
 
 
470
                        PopupMenu(m_contextMenu);
 
471
                        
 
472
                } else {
 
473
                        
 
474
                        e.Skip();
 
475
 
 
476
                }
 
477
        } else {
 
478
 
 
479
                e.Skip();
 
480
 
 
481
        }
 
482
}
 
483
 
 
484
void Notebook::OnInternalMenu(wxCommandEvent& e)
 
485
{
 
486
        if(m_contextMenu) {
 
487
                PopupMenu(m_contextMenu);
 
488
        }
 
489
}
 
490
 
 
491
void Notebook::OnBgDclick(wxAuiNotebookEvent& e)
 
492
{
 
493
        NotebookEvent event(wxEVT_COMMAND_BOOK_BG_DCLICK, GetId());
 
494
        event.SetSelection   ( wxNOT_FOUND );
 
495
        event.SetOldSelection( wxNOT_FOUND );
 
496
        event.SetEventObject ( this );
 
497
        GetEventHandler()->AddPendingEvent(event);
 
498
}
 
499
 
 
500
void Notebook::OnTabRightUp(wxAuiNotebookEvent& e)
 
501
{
 
502
        e.Skip();
 
503
}