/* AbiSource Program Utilities * Copyright (C) 1998-2000 AbiSource, Inc. * * 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. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ #include #include #include #include "ap_Features.h" #include "ut_assert.h" #include "ut_debugmsg.h" #include "ut_string.h" #include "ev_UnixToolbar.h" #include "xap_Types.h" #include "xap_UnixApp.h" #include "xap_UnixFrameImpl.h" #include "ev_Toolbar_Actions.h" #include "ev_Toolbar_Layouts.h" #include "ev_Toolbar_Labels.h" #include "ev_Toolbar_Control.h" #include "ev_EditEventMapper.h" #include "xap_UnixTableWidget.h" #include "xap_UnixToolbar_Icons.h" #include "xap_UnixFontManager.h" #include "ev_UnixToolbar_ViewListener.h" #include "xav_View.h" #include "xap_Prefs.h" #include "fv_View.h" #include "pd_Style.h" #include "xap_EncodingManager.h" #include "xap_UnixDialogHelper.h" #include "gr_UnixGraphics.h" #include "ut_string_class.h" #include "TopazToolbar.h" #include "abi-topaz-toolbar.h" // hack #include "ap_Toolbar_Id.h" #define UT_DEBUGMSG(M) M static void toolbar_cb (AbiTopazToolbar *widget, const gchar *method, gpointer data) { printf ("toolbar_cb() '%s'\n", method); TopazToolbar *pToolbar = static_cast(data); pToolbar->event(method); } TopazToolbar::TopazToolbar(XAP_UnixApp * pUnixApp, XAP_Frame *pFrame, const char * szToolbarLayoutName, const char * szToolbarLabelSetName) : EV_UnixToolbar(pUnixApp) { m_pUnixApp = pUnixApp; m_pFrame = pFrame; m_pViewListener = NULL; m_eEvent = NULL; m_wToolbar = NULL; m_wHandleBox = NULL; m_pFontPreview = NULL; m_bHidden = FALSE; } TopazToolbar::~TopazToolbar(void) { printf ("TopazToolbar::~TopazToolbar()\n"); _releaseListener(); } void TopazToolbar::event(const gchar *method) { AV_View * pView = m_pFrame->getCurrentView(); const EV_EditMethodContainer * pEMC = m_pUnixApp->getEditMethodContainer(); UT_ASSERT(pEMC); EV_EditMethod * pEM = pEMC->findEditMethodByName(method); UT_ASSERT(pEM); invokeToolbarMethod(pView, pEM, NULL, NULL); } /* * This method destroys the container widget here and returns the position in * the overall vbox container. */ UT_sint32 TopazToolbar::destroy(void) { GtkWidget * wVBox = static_cast(m_pFrame->getFrameImpl())->getVBoxWidget(); UT_sint32 pos = 0; // Code gratutiously stolen from gtkbox.c GList *list = NULL; bool bFound = false; for( list = GTK_BOX(wVBox)->children; !bFound && list; list = list->next) { GtkBoxChild * child = static_cast(list->data); if(child->widget == m_wHandleBox) { bFound = true; break; } pos++; } UT_ASSERT(bFound); if(!bFound) { pos = -1; } // Now remove the view listener AV_View * pView = getFrame()->getCurrentView(); pView->removeListener(m_lid); _releaseListener(); // Finally destroy the old toolbar widget if (m_wToolbar) { gtk_widget_destroy(m_wToolbar); m_wToolbar = NULL; } return pos; } /*! * This method rebuilds the toolbar and places it in the position it previously * occupied. */ void TopazToolbar::rebuildToolbar(UT_sint32 oldpos) { // ROB: i think we don't need this here UT_DEBUGMSG (("TopazToolbar::rebuildToolbar()\n")); } /* * get toolbar button appearance from the preferences */ GtkToolbarStyle TopazToolbar::getStyle(void) { // hardcoded, will maybe change UT_DEBUGMSG (("TopazToolbar::getStyle()\n")); return GTK_TOOLBAR_BOTH; } bool TopazToolbar::synthesize(void) { UT_DEBUGMSG (("TopazToolbar::synthesize()\n")); if (m_wToolbar == NULL) { GtkWidget *vbox = reinterpret_cast(m_pFrame->getFrameImpl())->getVBoxWidget(); m_wToolbar = abi_topaz_toolbar_new (); g_signal_connect (G_OBJECT (m_wToolbar), "activate", G_CALLBACK (toolbar_cb), (gpointer) this); gtk_box_pack_start (GTK_BOX (vbox), m_wToolbar, FALSE, FALSE, 0); gtk_widget_show_all (m_wToolbar); populateFonts(); } return TRUE; } void TopazToolbar::_releaseListener(void) { UT_DEBUGMSG (("TopazToolbar::_releaseListener()\n")); if (m_pViewListener && m_lid) { delete m_pViewListener; m_pViewListener = 0; m_lid = 0; } } bool TopazToolbar::bindListenerToView(AV_View * pView) { UT_DEBUGMSG (("TopazToolbar::bindListener()\n")); _releaseListener(); m_pViewListener = new EV_UnixToolbar_ViewListener(this, pView); UT_ASSERT(m_pViewListener); bool bResult = pView->addListener(static_cast(m_pViewListener), &m_lid); UT_ASSERT(bResult); m_pViewListener->setLID(m_lid); if(pView->isDocumentPresent()) { refreshToolbar(pView, AV_CHG_ALL); } return true; } bool TopazToolbar::refreshToolbar(AV_View * pView, AV_ChangeMask mask) { UT_DEBUGMSG (("TopazToolbar::refreshToolbar()\n")); if (mask & AV_CHG_DO) {} if (mask & AV_CHG_DIRTY) {} if (mask & AV_CHG_EMPTYSEL) {} if (mask & AV_CHG_FILENAME) {} if (mask & AV_CHG_FMTBLOCK) {} if (mask & AV_CHG_FMTCHAR) {} if (mask & AV_CHG_CLIPBOARD) {} if (mask & AV_CHG_PAGECOUNT) {} if (mask & AV_CHG_WINDOWSIZE) {} if (mask & AV_CHG_FMTSECTION) {} if (mask & AV_CHG_COLUMN) {} if (mask & AV_CHG_INPUTMODE) {} if (mask & AV_CHG_FMTSTYLE) {} if (mask & AV_CHG_INSERTMODE) {} if (mask & AV_CHG_HDRFTR) {} if (mask & AV_CHG_DIRECTIONMODE) {} if (mask & AV_CHG_FRAMEDATA) {} if (mask & AV_CHG_KEYPRESSED) {} if (mask & AV_CHG_BLOCKCHECK) {} if (mask & AV_CHG_FOCUS) {} return TRUE; /* // make the toolbar reflect the current state of the document // at the current insertion point or selection. const EV_Toolbar_ActionSet * pToolbarActionSet = m_pUnixApp->getToolbarActionSet(); UT_ASSERT(pToolbarActionSet); UT_uint32 nrLabelItemsInLayout = m_pToolbarLayout->getLayoutItemCount(); for (UT_uint32 k=0; (k < nrLabelItemsInLayout); k++) { EV_Toolbar_LayoutItem * pLayoutItem = m_pToolbarLayout->getLayoutItem(k); UT_ASSERT(pLayoutItem); XAP_Toolbar_Id id = pLayoutItem->getToolbarId(); EV_Toolbar_Action * pAction = pToolbarActionSet->getAction(id); UT_ASSERT(pAction); AV_ChangeMask maskOfInterest = pAction->getChangeMaskOfInterest(); if ((maskOfInterest & mask) == 0) // if this item doesn't care about continue; // changes of this type, skip it... switch (pLayoutItem->getToolbarLayoutFlags()) { case EV_TLF_Normal: { const char * szState = 0; EV_Toolbar_ItemState tis = pAction->getToolbarItemState(pView,&szState); switch (pAction->getItemType()) { case EV_TBIT_PushButton: { bool bGrayed = EV_TIS_ShouldBeGray(tis); _wd * wd = m_vecToolbarWidgets.getNthItem(k); UT_ASSERT(wd); GtkButton * item = GTK_BUTTON(wd->m_widget); UT_ASSERT(item); // Disable/enable toolbar item gtk_widget_set_sensitive(GTK_WIDGET(item), !bGrayed); } break; case EV_TBIT_ToggleButton: case EV_TBIT_GroupButton: { bool bGrayed = EV_TIS_ShouldBeGray(tis); bool bToggled = EV_TIS_ShouldBeToggled(tis); _wd * wd = m_vecToolbarWidgets.getNthItem(k); UT_ASSERT(wd); GtkToggleButton * item = GTK_TOGGLE_BUTTON(wd->m_widget); UT_ASSERT(item); // Block the signal, throw the toggle event bool wasBlocked = wd->m_blockSignal; wd->m_blockSignal = true; gtk_toggle_button_set_active(item, bToggled); wd->m_blockSignal = wasBlocked; // Disable/enable toolbar item gtk_widget_set_sensitive(GTK_WIDGET(item), !bGrayed); } break; case EV_TBIT_EditText: break; case EV_TBIT_DropDown: break; case EV_TBIT_ComboBox: { bool bGrayed = EV_TIS_ShouldBeGray(tis); _wd * wd = m_vecToolbarWidgets.getNthItem(k); UT_ASSERT(wd); GtkCombo * item = GTK_COMBO(wd->m_widget); UT_ASSERT(item); // Disable/enable toolbar item gtk_widget_set_sensitive(GTK_WIDGET(item), !bGrayed); // Block the signal, set the contents bool wasBlocked = wd->m_blockSignal; wd->m_blockSignal = true; if (szState) { if ( wd->m_id==AP_TOOLBAR_ID_FMT_SIZE ) { const char * fsz = XAP_EncodingManager::fontsizes_mapping.lookupBySource(szState); if ( fsz ) gtk_entry_set_text(GTK_ENTRY(item->entry), fsz); else gtk_entry_set_text(GTK_ENTRY(item->entry), ""); } else gtk_entry_set_text(GTK_ENTRY(item->entry), szState); } else { gtk_entry_set_text(GTK_ENTRY(item->entry), ""); } wd->m_blockSignal = wasBlocked; } break; case EV_TBIT_ColorFore: case EV_TBIT_ColorBack: { bool bGrayed = EV_TIS_ShouldBeGray(tis); _wd * wd = m_vecToolbarWidgets.getNthItem(k); UT_ASSERT(wd); UT_ASSERT(wd->m_widget); gtk_widget_set_sensitive(GTK_WIDGET(wd->m_widget), !bGrayed); // Disable/enable toolbar item } break; case EV_TBIT_StaticLabel: break; case EV_TBIT_Spacer: break; case EV_TBIT_BOGUS: break; default: UT_ASSERT(0); break; } } break; case EV_TLF_Spacer: break; default: UT_ASSERT(0); break; } } return true; */ } void TopazToolbar::show(void) { UT_DEBUGMSG (("TopazToolbar::show()\n")); gtk_widget_show (m_wToolbar); EV_Toolbar::show(); } void TopazToolbar::hide(void) { UT_DEBUGMSG (("TopazToolbar::hide()\n")); /* gtk_widget_hide(m_wHandleBox); EV_Toolbar::hide(); */ } /*! * This method examines the current document and repopulates the Styles * Combo box with what is in the document. It returns false if no styles * combo box was found. True if it all worked. */ bool TopazToolbar::repopulateStyles(void) { printf ("TopazToolbar::repopulateStyles()\n"); abi_topaz_toolbar_clear_styles (ABI_TOPAZ_TOOLBAR (m_wToolbar)); AD_Document * pAD_Doc = m_pFrame->getCurrentDoc(); if(!pAD_Doc) { return false; } PD_Document *pDocument = static_cast(pAD_Doc); const char * szName; PD_Style * pStyle; GSList *list = NULL; for (UT_uint32 k=0; (pDocument->enumStyles(k,&szName,const_cast(&pStyle))); k++) { if (!pStyle) { UT_DEBUGMSG(("no style instance for '%s'\n", szName)); } /* TODO Numbered Headings are also lists, we need to show them tough */ if (pStyle->isList()) { continue; } list = g_slist_prepend (list, (char *)szName); } if (list) { // OBlist = g_slist_sort(list, (GCompareFunc)sort_cb); list = g_slist_sort(list, (GCompareFunc)g_ascii_strcasecmp); GSList *item = list; do { abi_topaz_toolbar_add_style (ABI_TOPAZ_TOOLBAR (m_wToolbar), (const gchar*)item->data); } while (item = item->next); } return true; } void TopazToolbar::populateFonts(void) { XAP_UnixApp *pUnixApp = reinterpret_cast(XAP_App::getApp()); UT_GenericVector* list = pUnixApp->getFontManager()->getAllFonts(); UT_ASSERT(list); UT_uint32 count = list->size(); char *szName = NULL; for (UT_uint32 i = 0; i < count; i++) { XAP_UnixFont * pFont = list->getNthItem(i); // sort-out duplicates if (szName && strcmp (szName, pFont->getName()) == 0) { continue; } szName = (char *)pFont->getName(); abi_topaz_toolbar_add_font (ABI_TOPAZ_TOOLBAR (m_wToolbar), szName); } DELETEP(list); }