]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/rearrangectrl.cpp
Fix wxHtmlHelpData::SetTempDir() to behave correctly without trailing slash.
[wxWidgets.git] / src / common / rearrangectrl.cpp
index de8b901fb25bf63031488fbe22c4fc55d5d284f5..a6865f9036b9562cbe40fe8adb7f0297f016ac20 100644 (file)
@@ -3,7 +3,6 @@
 // Purpose:     implementation of classes in wx/rearrangectrl.h
 // Author:      Vadim Zeitlin
 // Created:     2008-12-15
-// RCS-ID:      $Id$
 // Copyright:   (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
 // Licence:     wxWindows licence
 ///////////////////////////////////////////////////////////////////////////////
     #pragma hdrstop
 #endif
 
+#if wxUSE_REARRANGECTRL
+
 #ifndef WX_PRECOMP
     #include "wx/button.h"
+    #include "wx/stattext.h"
     #include "wx/sizer.h"
 #endif // WX_PRECOMP
 
@@ -122,16 +124,45 @@ bool wxRearrangeList::MoveCurrentDown()
 
 void wxRearrangeList::Swap(int pos1, int pos2)
 {
+    // update the internally stored order
     wxSwap(m_order[pos1], m_order[pos2]);
 
-    const wxString stringTmp = GetString(pos1);
-    const bool checkedTmp = IsChecked(pos1);
 
-    SetString(pos1, GetString(pos2));
-    Check(pos1, IsChecked(pos2));
+    // and now also swap all the attributes of the items
 
+    // first the label
+    const wxString stringTmp = GetString(pos1);
+    SetString(pos1, GetString(pos2));
     SetString(pos2, stringTmp);
+
+    // then the checked state
+    const bool checkedTmp = IsChecked(pos1);
+    Check(pos1, IsChecked(pos2));
     Check(pos2, checkedTmp);
+
+    // and finally the client data, if necessary
+    switch ( GetClientDataType() )
+    {
+        case wxClientData_None:
+            // nothing to do
+            break;
+
+        case wxClientData_Object:
+            {
+                wxClientData * const dataTmp = DetachClientObject(pos1);
+                SetClientObject(pos1, DetachClientObject(pos2));
+                SetClientObject(pos2, dataTmp);
+            }
+            break;
+
+        case wxClientData_Void:
+            {
+                void * const dataTmp = GetClientData(pos1);
+                SetClientData(pos1, GetClientData(pos2));
+                SetClientData(pos2, dataTmp);
+            }
+            break;
+    }
 }
 
 void wxRearrangeList::OnCheck(wxCommandEvent& event)
@@ -139,11 +170,10 @@ void wxRearrangeList::OnCheck(wxCommandEvent& event)
     // update the internal state to match the new item state
     const int n = event.GetInt();
 
-    const bool checked = IsChecked(n);
-    wxASSERT_MSG( (m_order[n] >= 0) == !checked,
-                  "discrepancy between internal state and GUI" );
-
     m_order[n] = ~m_order[n];
+
+    wxASSERT_MSG( (m_order[n] >= 0) == IsChecked(n),
+                  "discrepancy between internal state and GUI" );
 }
 
 // ============================================================================
@@ -221,27 +251,89 @@ void wxRearrangeCtrl::OnButton(wxCommandEvent& event)
 extern
 WXDLLIMPEXP_DATA_CORE(const char) wxRearrangeDialogNameStr[] = "wxRearrangeDlg";
 
-wxRearrangeDialog::wxRearrangeDialog(wxWindow *parent,
-                                     const wxString& message,
-                                     const wxString& title,
-                                     const wxArrayInt& order,
-                                     const wxArrayString& items,
-                                     const wxPoint& pos,
-                                     const wxString& name)
-                 : wxDialog(parent, wxID_ANY, title,
-                            pos, wxDefaultSize,
-                            wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER,
-                            name)
+namespace
+{
+
+enum wxRearrangeDialogSizerPositions
+{
+    Pos_Label,
+    Pos_Ctrl,
+    Pos_Buttons,
+    Pos_Max
+};
+
+} // anonymous namespace
+
+bool wxRearrangeDialog::Create(wxWindow *parent,
+                               const wxString& message,
+                               const wxString& title,
+                               const wxArrayInt& order,
+                               const wxArrayString& items,
+                               const wxPoint& pos,
+                               const wxString& name)
 {
+    if ( !wxDialog::Create(parent, wxID_ANY, title,
+                           pos, wxDefaultSize,
+                           wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER,
+                           name) )
+        return false;
+
     m_ctrl = new wxRearrangeCtrl(this, wxID_ANY, wxDefaultPosition, wxDefaultSize,
                                  order, items);
 
+    // notice that the items in this sizer should be inserted accordingly to
+    // wxRearrangeDialogSizerPositions order
     wxSizer * const sizerTop = new wxBoxSizer(wxVERTICAL);
-    sizerTop->Add(new wxStaticText(this, wxID_ANY, message),
-                  wxSizerFlags().DoubleBorder());
+
+    if ( !message.empty() )
+    {
+        sizerTop->Add(new wxStaticText(this, wxID_ANY, message),
+                      wxSizerFlags().Border());
+    }
+    else
+    {
+        // for convenience of other wxRearrangeDialog code that depends on
+        // positions of sizer items, insert a dummy zero-sized item
+        sizerTop->AddSpacer(0);
+    }
+
     sizerTop->Add(m_ctrl,
                   wxSizerFlags(1).Expand().Border());
     sizerTop->Add(CreateSeparatedButtonSizer(wxOK | wxCANCEL),
                   wxSizerFlags().Expand().Border());
     SetSizerAndFit(sizerTop);
+
+    return true;
 }
+
+void wxRearrangeDialog::AddExtraControls(wxWindow *win)
+{
+    wxSizer * const sizer = GetSizer();
+    wxCHECK_RET( sizer, "the dialog must be created first" );
+
+    wxASSERT_MSG( sizer->GetChildren().GetCount() == Pos_Max,
+                  "calling AddExtraControls() twice?" );
+
+    sizer->Insert(Pos_Buttons, win, wxSizerFlags().Expand().Border());
+
+    win->MoveAfterInTabOrder(m_ctrl);
+
+    // we need to update the initial/minimal window size
+    sizer->SetSizeHints(this);
+}
+
+wxRearrangeList *wxRearrangeDialog::GetList() const
+{
+    wxCHECK_MSG( m_ctrl, NULL, "the dialog must be created first" );
+
+    return m_ctrl->GetList();
+}
+
+wxArrayInt wxRearrangeDialog::GetOrder() const
+{
+    wxCHECK_MSG( m_ctrl, wxArrayInt(), "the dialog must be created first" );
+
+    return m_ctrl->GetList()->GetCurrentOrder();
+}
+
+#endif // wxUSE_REARRANGECTRL