appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::WinButtonMenu Class Reference

Window button menu base class. More...

#include <WinMenu.h>

Inheritance diagram for rnr::WinButtonMenu:
rnr::WinGtkButtonMenu

Public Types

typedef std::map< int, WinButton * > MapBttns_T
 map of added menu buttons
 

Public Member Functions

 WinButtonMenu (const std::string &strIconPath=WinIconDirDft)
 WinButtonMenu initialization constructor. More...
 
virtual ~WinButtonMenu ()
 WinButtonMenu destructor.
 
void appendIconDir (const std::string &strIconDir)
 Append directory to icon search path. More...
 
void prependIconDir (const std::string &strIconDir)
 Prepend directory to icon search path. More...
 
void setIconPath (const std::string &strIconPath)
 Set icon search path. More...
 
std::string getIconPath ()
 Get icon search path. More...
 
virtual bool addImageButton (int nEvent, WidgetState eInitState, AlignOp eAlign, const std::string &strAltText, const std::string &strIconNormal, const std::string &strToolTipNormal="", const std::string &strIconActive="", const std::string &strToolTipActive="")
 Add button with image to menu. More...
 
virtual bool addLabelButton (int nEvent, WidgetState eInitState, AlignOp eAlign, const std::string &strLabelNormal, const std::string &strToolTipNormal="", const std::string &strLabelActive="", const std::string &strToolTipActive="")
 Add button with text label to menu. More...
 
virtual void bind (Win *pWin)
 Bind the button menu to the given window. More...
 
virtual void unbind ()
 Unbind the menu from the currently bound window.
 
int getCurrentEvent ()
 Get the current button menu event. More...
 
int setCurrentEvent (int nNewEvent)
 Set the current button menu event. More...
 
int getPreviousEvent ()
 Get the previous menu event. More...
 
size_t getNumMenuButtons ()
 Get the current number of menu items;. More...
 
WidgetState getButtonState (int nEvent)
 Get the given button state. More...
 
void setButtonState (int nEvent, WidgetState eNewState)
 Set the given button state. More...
 
void setButtonStateList (const std::string &strEvent,...)
 Set the states of the given buttons. More...
 
void resetAllButtonStates ()
 Reset all menu buttons states to their initial states.
 

Protected Member Functions

std::string makeIconPath (const std::string &strIconFile)
 Make icon image file path. More...
 
void changeButtonState (WinButton *pButton, WidgetState eNewState)
 Change the state and gui look of the given button. More...
 

Static Protected Member Functions

static void onMenuButtonClick (int nEvent, void *user_data)
 Menu button press callback. More...
 

Protected Attributes

Winm_pWin
 menus are bound to this window
 
std::string m_strIconPath
 icon directories search path
 
int m_nCurrentEvent
 current (user) menu event
 
int m_nPreviousEvent
 previous (user) menu event
 
MapBttns_T m_mapButtons
 menu button map
 

Detailed Description

Window button menu base class.

Buttons with icons or text are placed on the left or right side of the application window. Each icon is associated with an application defined event.

Definition at line 232 of file WinMenu.h.

Constructor & Destructor Documentation

rnr::WinButtonMenu::WinButtonMenu ( const std::string &  strIconPath = WinIconDirDft)
inline

WinButtonMenu initialization constructor.

Parameters
strIconPathIcon path of search directories.

Definition at line 243 of file WinMenu.h.

References rnr::UIEventNone.

244  {
245  m_pWin = NULL;
246  m_strIconPath = strIconPath;
249  }
Win * m_pWin
menus are bound to this window
Definition: WinMenu.h:482
no action
Definition: Win.h:115
int m_nCurrentEvent
current (user) menu event
Definition: WinMenu.h:484
std::string m_strIconPath
icon directories search path
Definition: WinMenu.h:483
int m_nPreviousEvent
previous (user) menu event
Definition: WinMenu.h:485

Member Function Documentation

virtual bool rnr::WinButtonMenu::addImageButton ( int  nEvent,
WidgetState  eInitState,
AlignOp  eAlign,
const std::string &  strAltText,
const std::string &  strIconNormal,
const std::string &  strToolTipNormal = "",
const std::string &  strIconActive = "",
const std::string &  strToolTipActive = "" 
)
inlinevirtual

Add button with image to menu.

Note
Buttons are added to window only at bind() time.
Parameters
nEventUnique event (and id) associated with button press.
eInitStateButton widget initial state. See GUI Widget State.
eAlignButton alignment. See rnmpwin_align.
strAltTextButton alternative text string.
strIconNormalButton icon image file name for normal state.
strToolTipNormalButton tooltip for normal state.
strIconActiveButton icon image file name for active state. Empty for no icon.
strToolTipActiveButton tooltip for active state.
Returns
Returns true on success, false otherwise.

Reimplemented in rnr::WinGtkButtonMenu.

Definition at line 329 of file WinMenu.h.

337  {
338  return false;
339  }
virtual bool rnr::WinButtonMenu::addLabelButton ( int  nEvent,
WidgetState  eInitState,
AlignOp  eAlign,
const std::string &  strLabelNormal,
const std::string &  strToolTipNormal = "",
const std::string &  strLabelActive = "",
const std::string &  strToolTipActive = "" 
)
inlinevirtual

Add button with text label to menu.

Note
Buttons are added to window only at bind() time.
Parameters
nEventUnique event (and id) associated with button press.
eInitStateButton widget initial state. See GUI Widget State.
eAlignButton alignment. See rnmpwin_align.
strLabelNormalButton text label for normal state.
strToolTipNormalButton tooltip for normal state.
strLabelActiveButton text label for active state. Empty for no label.
strToolTipActiveButton tooltip for active state.
Returns
Returns true on success, false otherwise.

Reimplemented in rnr::WinGtkButtonMenu.

Definition at line 359 of file WinMenu.h.

366  {
367  return false;
368  }
void rnr::WinButtonMenu::appendIconDir ( const std::string &  strIconDir)
inline

Append directory to icon search path.

Parameters
strIconDirDirectory holding icons.

Definition at line 261 of file WinMenu.h.

262  {
263  if( m_strIconPath.empty() )
264  {
265  m_strIconPath = strIconDir;
266  }
267  else
268  {
269  m_strIconPath = m_strIconPath + PATH_SEP_STR + strIconDir;
270  }
271  }
std::string m_strIconPath
icon directories search path
Definition: WinMenu.h:483
void WinButtonMenu::bind ( Win pWin)
virtual

Bind the button menu to the given window.

Any previously bound menu is unbound. This menu is reset to its initial state. The menu buttons are added to the window and displayed.

Parameters
pWinPointer to binding window.

Definition at line 167 of file WinMenu.cxx.

References rnr::WinButton::getCurrentImageWidget(), rnr::WinButton::getCurrentLabel(), rnr::WinButton::getCurrentToolTip(), rnr::WinButton::m_eAlign, rnr::WinButton::m_eType, rnr::WinButton::m_nEvent, rnr::WinButton::m_strAltText, rnr::WinButtonTypeImage, and rnr::WinButtonTypeLabel.

168 {
169  MapBttns_T::iterator iter;
170  WinButton *pButton;
171 
172  if( pWin == NULL )
173  {
174  LOGERROR("Binding window is NULL.");
175  return;
176  }
177 
178  // unbind any previous menu
179  if( m_pWin != NULL )
180  {
181  unbind();
182  }
183 
184  m_pWin = pWin;
185 
186  // add menu items to bound window
187  for(iter=m_mapButtons.begin(); iter!=m_mapButtons.end(); ++iter)
188  {
189  pButton = iter->second;
190 
191  switch( pButton->m_eType )
192  {
193  case WinButtonTypeImage:
194  m_pWin->addImageButton(pButton->m_nEvent,
195  pButton->m_eAlign,
196  pButton->getCurrentImageWidget(),
197  pButton->m_strAltText,
198  pButton->getCurrentToolTip());
199  break;
200  case WinButtonTypeLabel:
201  m_pWin->addLabelButton(pButton->m_nEvent,
202  pButton->m_eAlign,
203  pButton->getCurrentLabel(),
204  pButton->getCurrentToolTip());
205  break;
206  default:
207  break;
208  }
209  }
210 
211  // reset menu item states
213 
214  // register menu button press callback
216 }
void registerButtonClickCallback(BttnCbFunc_T funcCb, void *param)
Register application button click event callback.
Definition: Win.h:636
WinButtonType m_eType
button type
Definition: WinMenu.h:201
Win * m_pWin
menus are bound to this window
Definition: WinMenu.h:482
MapBttns_T m_mapButtons
menu button map
Definition: WinMenu.h:486
std::string getCurrentLabel()
Get the text label associated with the current button state.
Definition: WinMenu.cxx:110
AlignOp m_eAlign
left or right alignment
Definition: WinMenu.h:205
std::string m_strAltText
alternate text for icon(s)
Definition: WinMenu.h:206
button has text label(s)
Definition: WinMenu.h:84
void resetAllButtonStates()
Reset all menu buttons states to their initial states.
Definition: WinMenu.cxx:265
int m_nEvent
button push "mouse click" event
Definition: WinMenu.h:202
virtual bool addLabelButton(int nBttnId, AlignOp eAlign, const std::string &strLabel, const std::string &strToolTip="")=0
Add a button with a label to window button menu.
std::string getCurrentToolTip()
Get the tooltip associated with the current button state.
Definition: WinMenu.cxx:126
button has image(s)
Definition: WinMenu.h:83
virtual void unbind()
Unbind the menu from the currently bound window.
Definition: WinMenu.cxx:218
Window button base class.
Definition: WinMenu.h:94
static void onMenuButtonClick(int nEvent, void *user_data)
Menu button press callback.
Definition: WinMenu.cxx:398
virtual bool addImageButton(int nBttnId, AlignOp eAlign, const std::string &strIconPath, const std::string &strAltText, const std::string &strToolTip="")=0
Add a button with an image to window button menu.
void * getCurrentImageWidget()
Get the icon image widget associated with the current button state.
Definition: WinMenu.cxx:94
void WinButtonMenu::changeButtonState ( WinButton pButton,
WidgetState  eNewState 
)
protected

Change the state and gui look of the given button.

Parameters
pButtonPointer to menu button instance.
eNewStateNew button state. See rnmpwin_widgetstate

Definition at line 338 of file WinMenu.cxx.

References rnr::WinButton::getActiveImageWidget(), rnr::WinButton::getNormalImageWidget(), rnr::WinButton::m_eState, rnr::WinButton::m_eType, rnr::WinButton::m_nEvent, rnr::WinButton::m_strTagActive, rnr::WinButton::m_strTagNormal, rnr::WinButton::m_strToolTipActive, rnr::WinButton::m_strToolTipNormal, rnr::WidgetStateActive, rnr::WinButtonTypeImage, and rnr::WinButtonTypeLabel.

Referenced by onMenuButtonClick().

339 {
340  WidgetState eCurState;
341 
342  eCurState = pButton->m_eState;
343  pButton->m_eState = eNewState;
344 
345  if( m_pWin != NULL )
346  {
347  switch( pButton->m_eType )
348  {
349  case WinButtonTypeImage:
350  if( pButton->getActiveImageWidget() != NULL )
351  {
352  // normal,disabled --> active
353  if( (eCurState != WidgetStateActive) &&
354  (eNewState == WidgetStateActive) )
355  {
357  pButton->getActiveImageWidget(),
358  pButton->m_strToolTipActive);
359  }
360  // active --> normal,disabled
361  else if( (eCurState == WidgetStateActive) &&
362  (eNewState != WidgetStateActive) )
363  {
365  pButton->getNormalImageWidget(),
366  pButton->m_strToolTipNormal);
367  }
368  }
369  break;
370  case WinButtonTypeLabel:
371  if( !pButton->m_strTagActive.empty() )
372  {
373  // normal,disabled --> active
374  if( (eCurState != WidgetStateActive) &&
375  (eNewState == WidgetStateActive) )
376  {
378  pButton->m_strTagActive,
379  pButton->m_strToolTipActive);
380  }
381  // active --> normal,disabled
382  else if( (eCurState == WidgetStateActive) &&
383  (eNewState != WidgetStateActive) )
384  {
386  pButton->m_strTagNormal,
387  pButton->m_strToolTipNormal);
388  }
389  }
390  break;
391  }
392 
393  // show gui widget state
394  m_pWin->showButtonState(pButton->m_nEvent, eNewState);
395  }
396 }
WidgetState
Definition: Win.h:129
WinButtonType m_eType
button type
Definition: WinMenu.h:201
Win * m_pWin
menus are bound to this window
Definition: WinMenu.h:482
virtual void * getActiveImageWidget()
Get the active icon image widget.
Definition: WinMenu.h:169
virtual void * getNormalImageWidget()
Get the normal icon image widget.
Definition: WinMenu.h:158
virtual bool replaceButtonImage(int nBttnId, const std::string &strIconPath, const std::string &strToolTip="")=0
Replace existing button image with new image.
WidgetState m_eState
current button widget state
Definition: WinMenu.h:204
button has text label(s)
Definition: WinMenu.h:84
int m_nEvent
button push "mouse click" event
Definition: WinMenu.h:202
std::string m_strTagNormal
button icon for normal state
Definition: WinMenu.h:207
button has image(s)
Definition: WinMenu.h:83
std::string m_strToolTipActive
button tooltip for active state
Definition: WinMenu.h:210
std::string m_strToolTipNormal
button tooltip for normal state
Definition: WinMenu.h:208
virtual bool showButtonState(int nBttnId, WidgetState eBttnState)=0
Show a button&#39;s widget state.
selected
Definition: Win.h:132
virtual bool replaceButtonLabel(int nBttnId, const std::string &strLabel, const std::string &strToolTip="")=0
Replace existing button label with new label.
std::string m_strTagActive
button icon for active state
Definition: WinMenu.h:209
WidgetState rnr::WinButtonMenu::getButtonState ( int  nEvent)
inline

Get the given button state.

Parameters
nEventMenu-wide unique event button identifier.
Returns
Current button state. See rnmpwin_widgetstate

Definition at line 439 of file WinMenu.h.

References rnr::WidgetStateNormal.

440  {
442 
443  if( m_mapButtons.find(nEvent) != m_mapButtons.end() )
444  {
445  eState = m_mapButtons[nEvent]->m_eState;
446  }
447  return eState;
448  }
WidgetState
Definition: Win.h:129
MapBttns_T m_mapButtons
menu button map
Definition: WinMenu.h:486
normal - enabled but not selected
Definition: Win.h:131
int rnr::WinButtonMenu::getCurrentEvent ( )
inline

Get the current button menu event.

Returns
Current event.

Definition at line 390 of file WinMenu.h.

391  {
392  return m_nCurrentEvent;
393  }
int m_nCurrentEvent
current (user) menu event
Definition: WinMenu.h:484
std::string rnr::WinButtonMenu::getIconPath ( )
inline

Get icon search path.

Returns
Icon path string.

Definition at line 305 of file WinMenu.h.

306  {
307  return m_strIconPath;
308  }
std::string m_strIconPath
icon directories search path
Definition: WinMenu.h:483
size_t rnr::WinButtonMenu::getNumMenuButtons ( )
inline

Get the current number of menu items;.

Returns
Number of items.

Definition at line 427 of file WinMenu.h.

428  {
429  return m_mapButtons.size();
430  }
MapBttns_T m_mapButtons
menu button map
Definition: WinMenu.h:486
int rnr::WinButtonMenu::getPreviousEvent ( )
inline

Get the previous menu event.

Returns
Current action.

Definition at line 417 of file WinMenu.h.

418  {
419  return m_nPreviousEvent;
420  }
int m_nPreviousEvent
previous (user) menu event
Definition: WinMenu.h:485
string WinButtonMenu::makeIconPath ( const std::string &  strIconFile)
protected

Make icon image file path.

The built file path is check of existence.

Parameters
strIconFileIcon file absolute or relative file name.
Returns
Returns string containing icon path. Empty if no file specified or found.

Definition at line 275 of file WinMenu.cxx.

276 {
277  string strPath; // working path of search directories
278  string strIconFqName; // icon fully-qualified file name
279  size_t pos1; // substring starting position 1
280  size_t pos2; // substring ending position 2
281 
282  //
283  // No file name. Note this is not an error.
284  //
285  if( strIconFile.empty() )
286  {
287  return strIconFqName;
288  }
289 
290  //
291  // Icon file name is an absolute path.
292  //
293  if( strIconFile[0] == DIR_SEP_CHAR )
294  {
295  strIconFqName = strIconFile;
296  if( access(strIconFqName.c_str(), F_OK|R_OK) != 0 )
297  {
298  LOGWARN("%s: %s(errno=%d)",
299  strIconFqName.c_str(), strerror(errno), errno);
300  strIconFqName.clear();
301  }
302  return strIconFqName;
303  }
304 
305  //
306  // Search for file.
307  //
308  if( m_strIconPath.empty() )
309  {
310  strPath = ".";
311  }
312  else
313  {
314  strPath = m_strIconPath + PATH_SEP_STR + ".";
315  }
316 
317  for(pos1=0, pos2=strPath.find(PATH_SEP_STR, pos1);
318  pos1 != strPath.npos;
319  pos1=pos2+1, pos2=strPath.find(PATH_SEP_STR, pos1))
320  {
321  strIconFqName = strPath.substr(pos1, pos2) + DIR_SEP_STR + strIconFile;
322  if( access(strIconFqName.c_str(), F_OK|R_OK) == 0 )
323  {
324  return strIconFqName;
325  }
326  }
327 
328  //
329  // Failed to find icon file.
330  //
331  LOGWARN("%s: Icon not found in %s.", strIconFile.c_str(), strPath.c_str());
332 
333  strIconFqName.clear();
334 
335  return strIconFqName;
336 }
std::string m_strIconPath
icon directories search path
Definition: WinMenu.h:483
void WinButtonMenu::onMenuButtonClick ( int  nEvent,
void *  user_data 
)
staticprotected

Menu button press callback.

Parameters
nEventButton event (and button id).
user_dataUser-supplied data (this).

Definition at line 398 of file WinMenu.cxx.

References changeButtonState(), m_mapButtons, setCurrentEvent(), rnr::WidgetStateActive, and rnr::WidgetStateNormal.

399 {
400  WinButtonMenu *pMenu = (WinButtonMenu *)user_data;
401  MapBttns_T::iterator pos;
402  WidgetState eState;
403 
404  pMenu->setCurrentEvent(nEvent);
405 
406  if( (pos = pMenu->m_mapButtons.find(nEvent)) != pMenu->m_mapButtons.end() )
407  {
408  eState = pos->second->m_eState;
409 
410  // toggle state
411  if( eState == WidgetStateNormal )
412  {
413  pMenu->changeButtonState(pos->second, WidgetStateActive);
414  }
415  else if( eState == WidgetStateActive )
416  {
417  pMenu->changeButtonState(pos->second, WidgetStateNormal);
418  }
419  }
420 }
WidgetState
Definition: Win.h:129
Window button menu base class.
Definition: WinMenu.h:232
MapBttns_T m_mapButtons
menu button map
Definition: WinMenu.h:486
normal - enabled but not selected
Definition: Win.h:131
int setCurrentEvent(int nNewEvent)
Set the current button menu event.
Definition: WinMenu.h:402
void changeButtonState(WinButton *pButton, WidgetState eNewState)
Change the state and gui look of the given button.
Definition: WinMenu.cxx:338
selected
Definition: Win.h:132
void rnr::WinButtonMenu::prependIconDir ( const std::string &  strIconDir)
inline

Prepend directory to icon search path.

Parameters
strIconDirDirectory holding icons.

Definition at line 278 of file WinMenu.h.

279  {
280  if( m_strIconPath.empty() )
281  {
282  m_strIconPath = strIconDir;
283  }
284  else
285  {
286  m_strIconPath = strIconDir + PATH_SEP_STR + m_strIconPath;
287  }
288  }
std::string m_strIconPath
icon directories search path
Definition: WinMenu.h:483
void rnr::WinButtonMenu::setButtonState ( int  nEvent,
WidgetState  eNewState 
)
inline

Set the given button state.

Parameters
nEventMenu-wide unique event button identifier.
eNewStateNew button state. See rnmpwin_widgetstate

Definition at line 456 of file WinMenu.h.

457  {
458  MapBttns_T::iterator pos;
459 
460  if( (pos = m_mapButtons.find(nEvent)) != m_mapButtons.end() )
461  {
462  changeButtonState(pos->second, eNewState);
463  }
464  }
MapBttns_T m_mapButtons
menu button map
Definition: WinMenu.h:486
void changeButtonState(WinButton *pButton, WidgetState eNewState)
Change the state and gui look of the given button.
Definition: WinMenu.cxx:338
void WinButtonMenu::setButtonStateList ( const std::string &  strEvent,
  ... 
)

Set the states of the given buttons.

Parameters
strEventEvent reason string (used for logging only).
...Sequence of event,state pairs. The sequence is terminated by the UIEventNone special 'no' action event.

Definition at line 229 of file WinMenu.cxx.

References rnr::UIEventNone.

230 {
231  va_list ap;
232  size_t i;
233  int nEvent;
234  WidgetState eNewState;
235  MapBttns_T::iterator pos;
236 
237  LOGDIAG3("'%s' bulk menu state changes.", strEvent.c_str());
238 
239  va_start(ap, strEvent);
240 
241  //
242  // Loop through button map, but limit to number of buttons.
243  //
244  for(i=0; i<m_mapButtons.size(); ++i)
245  {
246  nEvent = va_arg(ap, int);
247 
248  // stop criteria
249  if( nEvent == UIEventNone )
250  {
251  break;
252  }
253 
254  eNewState = (WidgetState)va_arg(ap, int);
255 
256  if( (pos = m_mapButtons.find(nEvent)) != m_mapButtons.end() )
257  {
258  changeButtonState(pos->second, eNewState);
259  }
260  }
261 
262  va_end(ap);
263 }
WidgetState
Definition: Win.h:129
MapBttns_T m_mapButtons
menu button map
Definition: WinMenu.h:486
no action
Definition: Win.h:115
void changeButtonState(WinButton *pButton, WidgetState eNewState)
Change the state and gui look of the given button.
Definition: WinMenu.cxx:338
int rnr::WinButtonMenu::setCurrentEvent ( int  nNewEvent)
inline

Set the current button menu event.

Parameters
nNewEventNew event unique identifier.
Returns
New current event.

Definition at line 402 of file WinMenu.h.

Referenced by onMenuButtonClick().

403  {
404  if( nNewEvent != m_nCurrentEvent )
405  {
407  }
408  m_nCurrentEvent = nNewEvent;
409  return m_nCurrentEvent;
410  }
int m_nCurrentEvent
current (user) menu event
Definition: WinMenu.h:484
int m_nPreviousEvent
previous (user) menu event
Definition: WinMenu.h:485
void rnr::WinButtonMenu::setIconPath ( const std::string &  strIconPath)
inline

Set icon search path.

Parameters
strIconPathIcon path of search directories.

Definition at line 295 of file WinMenu.h.

296  {
297  m_strIconPath = strIconPath;
298  }
std::string m_strIconPath
icon directories search path
Definition: WinMenu.h:483

The documentation for this class was generated from the following files: