|
From: Conrado P. <con...@us...> - 2006-12-09 22:06:17
|
Update of /cvsroot/smartwin/SmartWin/include/smartwin/widgets In directory sc8-pr-cvs8.sourceforge.net:/tmp/cvs-serv10726/include/smartwin/widgets Modified Files: WidgetMenu.h WidgetMenuExtended.h Log Message: Added getCount, isSystemMenu to WidgetMenu Added getCount, onPopup, removeAllItems, isSystemMenu to WidgetMenuExtended Index: WidgetMenuExtended.h =================================================================== RCS file: /cvsroot/smartwin/SmartWin/include/smartwin/widgets/WidgetMenuExtended.h,v retrieving revision 1.44 retrieving revision 1.45 diff -u -d -r1.44 -r1.45 --- WidgetMenuExtended.h 8 Dec 2006 22:15:55 -0000 1.44 +++ WidgetMenuExtended.h 9 Dec 2006 22:06:11 -0000 1.45 @@ -245,9 +245,9 @@ SmartUtil::tstring itemText ( buffer.begin(), buffer.begin() + count ); // now get text extents - SIZE textSize = - {0 - }; + SIZE textSize; + memset( & textSize, 0, sizeof( SIZE ) ); + HGDIOBJ oldFont = ::SelectObject( hdc, data->Font->getHandle() ); ::GetTextExtentPoint32( hdc, itemText.c_str(), ( int ) itemText.size(), & textSize ); ::SelectObject( hdc, oldFont ); @@ -307,9 +307,9 @@ if ( menu->drawSidebar ) { // get title text extents - SIZE textSize = - {0 - }; + SIZE textSize; + memset( & textSize, 0, sizeof( SIZE ) ); + ::GetTextExtentPoint32( hdc, menu->itsTitle.c_str(), ( int ) menu->itsTitle.size(), & textSize ); itemWidth += textSize.cy; @@ -425,9 +425,9 @@ titleFont = ::CreateFontIndirect( & lf ); // get title text size - SIZE textSize = - {0 - }; + SIZE textSize; + memset( & textSize, 0, sizeof( SIZE ) ); + HGDIOBJ oldFont = ::SelectObject( canvas.getDc(), titleFont ); ::GetTextExtentPoint32( canvas.getDc(), menu->itsTitle.c_str(), ( int ) menu->itsTitle.size(), & textSize ); ::SelectObject( canvas.getDc(), oldFont ); @@ -808,6 +808,39 @@ ); return handled; } + + static HRESULT dispatchPopup( private_::SignalContent & params ) + { + // get callback + typename WidgetType::voidPopupFunction func = + reinterpret_cast< typename WidgetType::voidPopupFunction >( params.Function ); + + StayAliveDeleter< WidgetType > deleter; + boost::shared_ptr< WidgetType > ptrThis( boost::polymorphic_cast< WidgetType * >( params.This ), deleter ); + + // call the callback + func + ( dynamic_cast< EventHandlerClass * >( params.This->getParent() ) + , ptrThis + ); + return 0; + } + + static HRESULT dispatchPopupThis( private_::SignalContent & params ) + { + // get method pointer + typename WidgetType::itsVoidPopupFunction func = + reinterpret_cast< typename WidgetType::itsVoidPopupFunction >( params.FunctionThis ); + + StayAliveDeleter< WidgetType > deleter; + boost::shared_ptr< WidgetType > ptrThis( boost::polymorphic_cast< WidgetType * >( params.This ), deleter ); + + // call method + ( ( * boost::polymorphic_cast< EventHandlerClass * >( params.This->getParent() ) ).*func ) + ( ptrThis + ); + return 0; + } }; // Menu Renderer static data members initialization @@ -965,6 +998,14 @@ /// Typedef of a static/global function taking a pointer to the original class, a pointer to the this Widget class and an unsigned int returning void typedef bool ( * boolMeasureItemFunction )( EventHandlerClass *, ObjectType, MEASUREITEMSTRUCT * ); + /// \ingroup eventsSignatures + /// \typedef Typedef of a member function to the original class taking pointer to the this Widget returning void + typedef void ( EventHandlerClass::* itsVoidPopupFunction )( ObjectType ); + + /// \ingroup eventsSignatures + /// Typedef of a static/global function taking a pointer to the original class, a pointer to the this Widget class returning void + typedef void ( * voidPopupFunction )( EventHandlerClass *, ObjectType ); + // Overriden to set default drawing void create( bool isPopup = false ); @@ -993,6 +1034,12 @@ void onMeasureItem( boolMeasureItemFunction eventHandler ); void onMeasureItem( itsBoolMeasureItemFunction eventHandler ); + /// Sets the event handler for the Popup event + void onPopup( voidPopupFunction eventHandler ); + + /// Sets the event handler for the Popup event + void onPopup( itsVoidPopupFunction eventHandler ); + /// Appends a separator item to the menu /** A menu separator is basically just "air" between menu items.< br > * A separator cannot be "clicked" or "chosen". @@ -1019,6 +1066,14 @@ */ void removeItem( unsigned itemIndex ); + /// Remove all items from the menu + /** Will also delete any submenus. + */ + void removeAllItems(); + + /// Return the number of items in the menu + int getCount(); + /// Displays and handles a menu which can appear anywhere in the window. /** Typically called by a Right Mouse click. If both the x and the y coordinate * is - 1 ( default ) it'll show the context menu on the position the mouse was @@ -1085,6 +1140,12 @@ /// Returns true if item is checked bool isItemChecked( unsigned itemID ); + /// Returns true if menu is "system menu" (icon in top left of window) + bool isSystemMenu() + { + return isSysMenu; + } + /// Returns item text SmartUtil::tstring getItemText( unsigned itemID ); @@ -1668,6 +1729,48 @@ } template< class EventHandlerClass, class MessageMapPolicy > +void WidgetMenuExtended< EventHandlerClass, MessageMapPolicy >::onPopup( voidPopupFunction eventHandler ) +{ + // get pointer to this message map + MessageMapType * This = boost::polymorphic_cast< MessageMapType * >( this ); + + // setup slot + This->addNewSignal + ( typename MessageMapType::SignalTupleType + ( private_::SignalContent + ( Message( WM_INITMENUPOPUP ) + , reinterpret_cast< private_::SignalContent::voidFunctionTakingVoid >( eventHandler ) + , This + ) + , typename MessageMapType::SignalType + ( typename MessageMapType::SignalType::SlotType( & Dispatcher::dispatchPopup ) + ) + ) + ); +} + +template< class EventHandlerClass, class MessageMapPolicy > +void WidgetMenuExtended< EventHandlerClass, MessageMapPolicy >::onPopup( itsVoidPopupFunction eventHandler ) +{ + // get pointer to this message map + MessageMapType * This = boost::polymorphic_cast< MessageMapType * >( this ); + + // setup slot + This->addNewSignal + ( typename MessageMapType::SignalTupleType + ( private_::SignalContent + ( Message( WM_INITMENUPOPUP ) + , reinterpret_cast< itsVoidFunction >( eventHandler ) + , This + ) + , typename MessageMapType::SignalType + ( typename MessageMapType::SignalType::SlotType( & Dispatcher::dispatchPopupThis ) + ) + ) + ); +} + +template< class EventHandlerClass, class MessageMapPolicy > void WidgetMenuExtended< EventHandlerClass, MessageMapPolicy >::appendSeparatorItem() { // init structure for new item @@ -1751,21 +1854,26 @@ { size_t i = 0; private_::ItemDataWrapper * wrapper = 0; + int itemRemoved = -1; for ( i = 0; i < itsItemDataRef.size(); ++i ) { // get current data wrapper wrapper = itsItemDataRef[i]; - if ( wrapper->index == itemIndex ) // if found + if ( wrapper->index == int(itemIndex) ) // if found { + itemRemoved = int(i); delete wrapper; itsItemDataRef[i] = 0; } - else if ( wrapper->index > itemIndex ) + else if ( wrapper->index > int(itemIndex) ) --wrapper->index; // adjust succeeding item indices } + if( itemRemoved != -1 ) + itsItemDataRef.erase( itsItemDataRef.begin() + itemRemoved ); + if ( popup != NULL ) // remove sub menus if any { for ( i = 0; i < itsChildrenRef.size(); ++i ) @@ -1780,6 +1888,26 @@ } template< class EventHandlerClass, class MessageMapPolicy > +void WidgetMenuExtended< EventHandlerClass, MessageMapPolicy >::removeAllItems() +{ + //must be backwards, since bigger indexes change on remove + for( int i = this->getCount() - 1; i >= 0; i-- ) + { + this->removeItem( i ); + } +} + +template< class EventHandlerClass, class MessageMapPolicy > +int WidgetMenuExtended< EventHandlerClass, MessageMapPolicy >::getCount() +{ + HMENU handle = reinterpret_cast< HMENU >( this->Widget::itsHandle ); + int count = ::GetMenuItemCount( handle ); + if( count == -1 ) + throw xCeption( _T( "Couldn't get item count in getCount()" ) ); + return count; +} + +template< class EventHandlerClass, class MessageMapPolicy > void WidgetMenuExtended< EventHandlerClass, MessageMapPolicy >::appendItem( unsigned itemID, const SmartUtil::tstring & text, MenuItemDataPtr itemData, Index: WidgetMenu.h =================================================================== RCS file: /cvsroot/smartwin/SmartWin/include/smartwin/widgets/WidgetMenu.h,v retrieving revision 1.31 retrieving revision 1.32 diff -u -d -r1.31 -r1.32 --- WidgetMenu.h 8 Dec 2006 22:15:55 -0000 1.31 +++ WidgetMenu.h 9 Dec 2006 22:06:11 -0000 1.32 @@ -433,6 +433,9 @@ */ bool getItemEnabled( unsigned id ); + /// Return the number of items in the menu + int getCount(); + /// Displays and handles a menu which can appear anywhere in the window. /** Typically called by a Right Mouse click. If both the x and the y coordinates * are - 1 ( default ), it'll show the context menu at the mouse position when @@ -464,6 +467,11 @@ */ unsigned trackPopupMenu( EventHandlerClass * mainWindow, int x = - 1, int y = - 1, unsigned flags = 0 ); + bool isSystemMenu() + { + return isSysMenu; + } + // We CAN'T have this one protected since boost needs to be able to destroy // objects of this type // TODO: Fix...!! @@ -600,9 +608,9 @@ template< class EventHandlerClass, class MessageMapPolicy > SmartUtil::tstring WidgetMenu< EventHandlerClass, MessageMapPolicy >::getText( unsigned id ) { - MENUITEMINFO mi = - {0 - }; + MENUITEMINFO mi; + memset( & mi, 0, sizeof( MENUITEMINFO ) ); + mi.cbSize = sizeof( MENUITEMINFO ); mi.fMask = MIIM_TYPE; if ( ::GetMenuItemInfo( reinterpret_cast< HMENU >( this->Widget::itsHandle ), id, FALSE, & mi ) == 0 ) @@ -673,6 +681,16 @@ } template< class EventHandlerClass, class MessageMapPolicy > +int WidgetMenu< EventHandlerClass, MessageMapPolicy >::getCount() +{ + HMENU handle = reinterpret_cast< HMENU >( this->Widget::itsHandle ); + int count = ::GetMenuItemCount( handle ); + if( count == -1 ) + throw xCeption( _T( "Couldn't get item count in getCount()" ) ); + return count; +} + +template< class EventHandlerClass, class MessageMapPolicy > unsigned WidgetMenu< EventHandlerClass, MessageMapPolicy >:: trackPopupMenu( EventHandlerClass * mainWindow, int x, int y, unsigned flags ) { |