+
+/// Lay out the controls
+void wxRichTextStyleListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
+{
+ if (GetAutoLayout())
+ Layout();
+}
+
+/// Get the choice index for style type
+int wxRichTextStyleListCtrl::StyleTypeToIndex(wxRichTextStyleListBox::wxRichTextStyleType styleType)
+{
+ if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL)
+ {
+ return 0;
+ }
+ else if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH)
+ {
+ return 1;
+ }
+ else if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER)
+ {
+ return 2;
+ }
+ else if (styleType == wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST)
+ {
+ return 3;
+ }
+ return 0;
+}
+
+/// Get the style type for choice index
+wxRichTextStyleListBox::wxRichTextStyleType wxRichTextStyleListCtrl::StyleIndexToType(int i)
+{
+ if (i == 1)
+ return wxRichTextStyleListBox::wxRICHTEXT_STYLE_PARAGRAPH;
+ else if (i == 2)
+ return wxRichTextStyleListBox::wxRICHTEXT_STYLE_CHARACTER;
+ else if (i == 3)
+ return wxRichTextStyleListBox::wxRICHTEXT_STYLE_LIST;
+
+ return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL;
+}
+
+/// Associates the control with a style manager
+void wxRichTextStyleListCtrl::SetStyleSheet(wxRichTextStyleSheet* styleSheet)
+{
+ if (m_styleListBox)
+ m_styleListBox->SetStyleSheet(styleSheet);
+}
+
+wxRichTextStyleSheet* wxRichTextStyleListCtrl::GetStyleSheet() const
+{
+ if (m_styleListBox)
+ return m_styleListBox->GetStyleSheet();
+ else
+ return NULL;
+}
+
+/// Associates the control with a wxRichTextCtrl
+void wxRichTextStyleListCtrl::SetRichTextCtrl(wxRichTextCtrl* ctrl)
+{
+ if (m_styleListBox)
+ m_styleListBox->SetRichTextCtrl(ctrl);
+}
+
+wxRichTextCtrl* wxRichTextStyleListCtrl::GetRichTextCtrl() const
+{
+ if (m_styleListBox)
+ return m_styleListBox->GetRichTextCtrl();
+ else
+ return NULL;
+}
+
+/// Set/get the style type to display
+void wxRichTextStyleListCtrl::SetStyleType(wxRichTextStyleListBox::wxRichTextStyleType styleType)
+{
+ if ( !m_styleListBox )
+ return;
+
+ m_styleListBox->SetStyleType(styleType);
+
+ m_dontUpdate = true;
+
+ if (m_styleChoice)
+ {
+ int i = StyleTypeToIndex(m_styleListBox->GetStyleType());
+ m_styleChoice->SetSelection(i);
+ }
+
+ m_dontUpdate = false;
+}
+
+wxRichTextStyleListBox::wxRichTextStyleType wxRichTextStyleListCtrl::GetStyleType() const
+{
+ if (m_styleListBox)
+ return m_styleListBox->GetStyleType();
+ else
+ return wxRichTextStyleListBox::wxRICHTEXT_STYLE_ALL;
+}
+
+/// Updates the style list box
+void wxRichTextStyleListCtrl::UpdateStyles()
+{
+ if (m_styleListBox)
+ m_styleListBox->UpdateStyles();
+}
+
+#if wxUSE_COMBOCTRL
+
+/*!
+ * Style drop-down for a wxComboCtrl
+ */
+
+
+BEGIN_EVENT_TABLE(wxRichTextStyleComboPopup, wxRichTextStyleListBox)
+ EVT_MOTION(wxRichTextStyleComboPopup::OnMouseMove)
+ EVT_LEFT_DOWN(wxRichTextStyleComboPopup::OnMouseClick)
+END_EVENT_TABLE()
+
+bool wxRichTextStyleComboPopup::Create( wxWindow* parent )
+{
+ int borderStyle = GetDefaultBorder();
+ if (borderStyle == wxBORDER_SUNKEN)
+ borderStyle = wxBORDER_SIMPLE;
+
+ return wxRichTextStyleListBox::Create(parent, wxID_ANY,
+ wxPoint(0,0), wxDefaultSize,
+ borderStyle);
+}
+
+void wxRichTextStyleComboPopup::SetStringValue( const wxString& s )
+{
+ m_value = SetStyleSelection(s);
+}
+
+wxString wxRichTextStyleComboPopup::GetStringValue() const
+{
+ int sel = m_value;
+ if (sel > -1)
+ {
+ wxRichTextStyleDefinition* def = GetStyle(sel);
+ if (def)
+ return def->GetName();
+ }
+ return wxEmptyString;
+}
+
+//
+// Popup event handlers
+//
+
+// Mouse hot-tracking
+void wxRichTextStyleComboPopup::OnMouseMove(wxMouseEvent& event)
+{
+ // Move selection to cursor if it is inside the popup
+
+ int itemHere = wxRichTextStyleListBox::VirtualHitTest(event.GetPosition().y);
+ if ( itemHere >= 0 )
+ {
+ wxRichTextStyleListBox::SetSelection(itemHere);
+ m_itemHere = itemHere;
+ }
+ event.Skip();
+}
+
+// On mouse left, set the value and close the popup
+void wxRichTextStyleComboPopup::OnMouseClick(wxMouseEvent& WXUNUSED(event))
+{
+ if (m_itemHere >= 0)
+ m_value = m_itemHere;
+
+ // Ordering is important, so we don't dismiss this popup accidentally
+ // by setting the focus elsewhere e.g. in ApplyStyle
+ Dismiss();
+
+ if (m_itemHere >= 0)
+ wxRichTextStyleListBox::ApplyStyle(m_itemHere);
+}
+
+/*!
+ * wxRichTextStyleComboCtrl
+ * A combo for applying styles.
+ */
+
+IMPLEMENT_CLASS(wxRichTextStyleComboCtrl, wxComboCtrl)
+
+BEGIN_EVENT_TABLE(wxRichTextStyleComboCtrl, wxComboCtrl)
+ EVT_IDLE(wxRichTextStyleComboCtrl::OnIdle)
+END_EVENT_TABLE()
+
+bool wxRichTextStyleComboCtrl::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos,
+ const wxSize& size, long style)
+{
+ if (!wxComboCtrl::Create(parent, id, wxEmptyString, pos, size, style))
+ return false;
+
+ SetPopupMaxHeight(400);
+
+ m_stylePopup = new wxRichTextStyleComboPopup;
+
+ SetPopupControl(m_stylePopup);
+
+ return true;
+}
+
+/// Auto-select from style under caret in idle time
+
+// TODO: must be able to show italic, bold, combinations
+// in style box. Do we have a concept of automatic, temporary
+// styles that are added whenever we wish to show a style
+// that doesn't exist already? E.g. "Bold, Italic, Underline".
+// Word seems to generate these things on the fly.
+// If there's a named style already, it uses e.g. Heading1 + Bold, Italic
+// If you unembolden text in a style that has bold, it uses the
+// term "Not bold".
+// TODO: order styles alphabetically. This means indexes can change,
+// so need a different way to specify selections, i.e. by name.
+
+void wxRichTextStyleComboCtrl::OnIdle(wxIdleEvent& event)
+{
+ event.Skip();
+
+ if ( !m_stylePopup )
+ return;
+
+ wxRichTextCtrl * const richtext = GetRichTextCtrl();
+ if ( !richtext )
+ return;
+
+ if ( !IsPopupShown() && IsShownOnScreen() && wxWindow::FindFocus() != this )
+ {
+ wxString styleName =
+ wxRichTextStyleListBox::GetStyleToShowInIdleTime(richtext, m_stylePopup->GetStyleType());
+
+ wxString currentValue = GetValue();
+ if (!styleName.IsEmpty())
+ {
+ // Don't do the selection if it's already set
+ if (currentValue == styleName)
+ return;
+
+ SetValue(styleName);
+ }
+ else if (!currentValue.IsEmpty())
+ SetValue(wxEmptyString);
+ }
+}
+