1/////////////////////////////////////////////////////////////////////////////
2// Name:        cbcustom.cpp
3// Purpose:     cbSimpleCustomizationPlugin class declaration
4// Author:      Aleksandras Gluchovas
5// Modified by:
6// Created:     06/09/98
7// RCS-ID:      $Id: cbcustom.cpp 36189 2005-11-16 17:27:38Z ABX $
8// Copyright:   (c) Aleksandras Gluchovas
9// Licence:     wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
22
23#include "wx/fl/cbcustom.h"
24
25// helper class to receive menu customization event
26
27class cbContextMenuHandler : public wxEvtHandler
28{
29public:
30    cbSimpleCustomizationPlugin* mpBackRef;
31
32public:
33    void OnCommandEvents( wxCommandEvent& evt );
34
35    DECLARE_EVENT_TABLE()
36};
37
38// FIXME:: is this "safe" ?
39
40#define CB_CUSTOMIZE_MENU_FIRST_ITEM_ID 17500
41
42/***** Implementation for helper class cbContextMenuHandler *****/
43
44BEGIN_EVENT_TABLE( cbContextMenuHandler, wxEvtHandler )
45
46    // FIXME:: what is the right range for these ids ? so that they
47    //         would not collide with user commands?
48
49    EVT_COMMAND_RANGE( CB_CUSTOMIZE_MENU_FIRST_ITEM_ID,
50                       CB_CUSTOMIZE_MENU_FIRST_ITEM_ID + 300,
51                       wxEVT_COMMAND_MENU_SELECTED,
52                       cbContextMenuHandler::OnCommandEvents )
53
54END_EVENT_TABLE()
55
56void cbContextMenuHandler::OnCommandEvents( wxCommandEvent& evt )
57{
58    //wxMessageBox("Wowwwww, Yeah!");
59
60    mpBackRef->OnMenuItemSelected( evt );
61}
62
63/***** Implementation for class cbSimpleCustomizationPlugin *****/
64
65IMPLEMENT_DYNAMIC_CLASS( cbSimpleCustomizationPlugin, cbPluginBase )
66
67BEGIN_EVENT_TABLE( cbSimpleCustomizationPlugin, cbPluginBase )
68
69    EVT_PL_CUSTOMIZE_BAR   ( cbSimpleCustomizationPlugin::OnCustomizeBar    )
70    EVT_PL_CUSTOMIZE_LAYOUT( cbSimpleCustomizationPlugin::OnCustomizeLayout )
71
72END_EVENT_TABLE()
73
74cbSimpleCustomizationPlugin::cbSimpleCustomizationPlugin(void)
75{}
76
77cbSimpleCustomizationPlugin::cbSimpleCustomizationPlugin( wxFrameLayout* pPanel, int paneMask )
78
79    : cbPluginBase( pPanel, paneMask )
80{}
81
82void cbSimpleCustomizationPlugin::OnCustomizeBar( cbCustomizeBarEvent& event )
83{
84    // ingnore bar customization, treat it
85    // as layout-customization...ugly, eh?
86
87    cbCustomizeLayoutEvent clEvt( event.mClickPos );
88
89    OnCustomizeLayout( clEvt );
90}
91
92void cbSimpleCustomizationPlugin::OnCustomizeLayout( cbCustomizeLayoutEvent& event )
93{
94    wxString helpStr1 = wxT("Select this item to show the corresponding control bar");
95    wxString helpStr2 = wxT("Select this itme to hide the corresponding control bar");
96
97    int id = CB_CUSTOMIZE_MENU_FIRST_ITEM_ID;
98
99    wxMenu* pMenu = new wxMenu();
100
101    BarArrayT& bars = mpLayout->GetBars();
102
103    for( size_t i = 0; i != bars.GetCount(); ++i )
104    {
105        cbBarInfo& bar = *bars[i];
106
107        bool isHidden = ( bar.mState == wxCBAR_HIDDEN );
108
109        wxString* pHelpStr = ( isHidden ) ? &helpStr1 : &helpStr2;
110
111        pMenu->Append( id, bar.mName, *pHelpStr, true );
112
113        pMenu->Check( id, (isHidden == false) );
114
115        ++id;
116    }
117
118    // Customization dialog not implemented, so don't show the menu item
119#if 0
120    pMenu->AppendSeparator();
121    pMenu->Append( id, "Customize...", "Show layout customization dialog", false );
122#endif
123    mCustMenuItemId = id;
124
125    cbContextMenuHandler* pHandler = new cbContextMenuHandler();
126    pHandler->mpBackRef            = this;
127
128    wxWindow* pFrm = &mpLayout->GetParentFrame();
129
130    // FOR NOW FOR NOW:: to work-around wxFrame's (MSW) nasty event-handling bugs!!!
131
132    wxWindow* pTmpWnd = new wxWindow( pFrm, wxID_ANY, event.mClickPos, wxSize(0,0) );
133
134    pMenu->SetEventHandler( pHandler );
135
136    pTmpWnd->PopupMenu( pMenu, 0,0 );
137
138    pTmpWnd->Destroy();
139
140    delete pMenu;
141    delete pHandler;
142
143    // event is "eaten" by this plugin
144}
145
146void cbSimpleCustomizationPlugin::OnMenuItemSelected( wxCommandEvent& event )
147{
148    if ( event.GetId() == mCustMenuItemId )
149    {
150        wxMessageBox(wxT("Customization dialog box is not supported by this plugin yet"));
151
152        return;
153    }
154    else
155    {
156        cbBarInfo* pBar = mpLayout->GetBars()[ event.GetId() -  CB_CUSTOMIZE_MENU_FIRST_ITEM_ID ];
157
158        wxASSERT( pBar ); // DBG::
159
160        // "inverse" bar-visibility of the selected bar
161
162        int newState;
163
164        if ( pBar->mState == wxCBAR_HIDDEN )
165        {
166            if ( pBar->mAlignment == -1 )
167            {
168                pBar->mAlignment = 0;       // just remove "-1" marking
169                newState = wxCBAR_FLOATING;
170            }
171            else
172                if ( pBar->mAlignment == FL_ALIGN_TOP ||
173                     pBar->mAlignment == FL_ALIGN_BOTTOM )
174
175                    newState = wxCBAR_DOCKED_HORIZONTALLY;
176                else
177                    newState = wxCBAR_DOCKED_VERTICALLY;
178        }
179        else
180        {
181            newState = wxCBAR_HIDDEN;
182
183            if ( pBar->mState == wxCBAR_FLOATING )
184
185                pBar->mAlignment = -1;
186        }
187
188        mpLayout->SetBarState( pBar, newState, true );
189
190        if ( newState == wxCBAR_FLOATING )
191            mpLayout->RepositionFloatedBar( pBar );
192    }
193
194    // menu-item-selected event is "eaten"
195}
196