appkit  1.5.1
RoadNarrows Robotics Application Kit
WinMenu.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Link: https://github.com/roadnarrows-robotics/rnr-sdk
6 //
7 // Library: librnr_win
8 //
9 // File: WinMenu.h
10 //
11 /*! \file
12  *
13  * $LastChangedDate: 2013-05-08 08:22:06 -0600 (Wed, 08 May 2013) $
14  * $Rev: 2920 $
15  *
16  * \brief RoadNarrows Robotics base window button menu interface.
17  *
18  * \author Robin Knight (robin.knight@roadnarrows.com)
19  * \author Daniel Packard (daniel@roadnarrows.com)
20  *
21  * \par Copyright
22  * \h_copy 2011-2017. RoadNarrows LLC.\n
23  * http://www.roadnarrows.com\n
24  * All Rights Reserved
25  */
26 /*
27  * @EulaBegin@
28  *
29  * Permission is hereby granted, without written agreement and without
30  * license or royalty fees, to use, copy, modify, and distribute this
31  * software and its documentation for any purpose, provided that
32  * (1) The above copyright notice and the following two paragraphs
33  * appear in all copies of the source code and (2) redistributions
34  * including binaries reproduces these notices in the supporting
35  * documentation. Substantial modifications to this software may be
36  * copyrighted by their authors and need not follow the licensing terms
37  * described here, provided that the new terms are clearly indicated in
38  * all files where they apply.
39  *
40  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
41  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
42  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
43  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
44  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
45  * THE POSSIBILITY OF SUCH DAMAGE.
46  *
47  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
48  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
49  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
50  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
51  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
52  *
53  * @EulaEnd@
54  */
55 ////////////////////////////////////////////////////////////////////////////////
56 
57 #ifndef _RNR_WIN_BUTTON_MENU_H
58 #define _RNR_WIN_BUTTON_MENU_H
59 
60 #include <string>
61 #include <map>
62 
63 #include "rnr/rnrconfig.h"
64 
65 #include "rnr/appkit/Win.h"
66 
67 /*!
68  * \brief RoadNarrows Robotics
69  */
70 namespace rnr
71 {
72 #if defined(ARCH_overo)
73  const char* const WinIconDirDft = "/usr/local/share/appkit/images/icons";
74 #else
75  const char* const WinIconDirDft = "/prj/share/appkit/images/icons";
76 #endif // ARCH_overo
77 
78  /*!
79  * \brief Window button types.
80  */
82  {
83  WinButtonTypeImage, ///< button has image(s)
84  WinButtonTypeLabel ///< button has text label(s)
85  };
86 
87  //...........................................................................
88  // Class WinButton
89  //...........................................................................
90 
91  /*!
92  * \brief Window button base class.
93  */
94  class WinButton
95  {
96  public:
97 
98  /*!
99  * \brief WinButton initialization constructor.
100  *
101  * \param eType Button type.
102  * \param nEvent Event associated with button press (and id).
103  * \param eInitState Button widget initial state.
104  * See \ref rnrwin_widgetstate.
105  * \param eAlign Button alignment. See \ref rnmpwin_align.
106  * \param strAltText Button alternative text string.
107  * \param strTagNormal Button visual tag for normal state.
108  * Either icon path or text string label.
109  * \param strToolTipNormal Button tooltip for normal state.
110  * \param strTagActive Button visual tag for active state.
111  * Either icon path or text string label.
112  * Empty for no tag.
113  * \param strToolTipActive Button tooltip for active state.
114  */
116  int nEvent,
117  WidgetState eInitState,
118  AlignOp eAlign,
119  const std::string &strAltText,
120  const std::string &strTagNormal,
121  const std::string &strToolTipNormal = "",
122  const std::string &strTagActive = "",
123  const std::string &strToolTipActive = "") :
124  m_eType(eType),
125  m_nEvent(nEvent),
126  m_eInitState(eInitState),
127  m_eState(eInitState),
128  m_eAlign(eAlign),
129  m_strAltText(strAltText),
130  m_strTagNormal(strTagNormal),
131  m_strToolTipNormal(strToolTipNormal),
132  m_strTagActive(strTagActive),
133  m_strToolTipActive(strToolTipActive)
134  {
135  }
136 
137  /*!
138  * \brief WinButton copy constructor.
139  *
140  * \param src Source window button.
141  */
142  WinButton(const WinButton &src)
143  {
144  copy(&src);
145  }
146 
147  /*!
148  * \brief Destructor.
149  */
150  virtual ~WinButton() { }
151 
152  /*!
153  * \brief Get the normal icon image widget.
154  *
155  * \return Returns (possibly NULL) pointer to image widget
156  * (gui toolkit specific).
157  */
158  virtual void *getNormalImageWidget()
159  {
160  return NULL;
161  }
162 
163  /*!
164  * \brief Get the active icon image widget.
165  *
166  * \return Returns (possibly NULL) pointer to image widget
167  * (gui toolkit specific).
168  */
169  virtual void *getActiveImageWidget()
170  {
171  return NULL;
172  }
173 
174  /*!
175  * \brief Get the icon image widget associated with the current button
176  * state.
177  *
178  * \return
179  * Returns pointer to image widget (gui toolkit specific) if found.\n
180  * Otherwise returns NULL.
181  */
182  void *getCurrentImageWidget();
183 
184  /*!
185  * \brief Get the text label associated with the current button state.
186  *
187  * \return Returns (possibly empty) text string.
188  */
189  std::string getCurrentLabel();
190 
191  /*!
192  * \brief Get the tooltip associated with the current button state.
193  *
194  * \return Returns (possibly empty) text string.
195  */
196  std::string getCurrentToolTip();
197 
198  friend class WinButtonMenu;
199 
200  protected:
201  WinButtonType m_eType; ///< button type
202  int m_nEvent; ///< button push "mouse click" event
203  WidgetState m_eInitState; ///< initial button widget state
204  WidgetState m_eState; ///< current button widget state
205  AlignOp m_eAlign; ///< left or right alignment
206  std::string m_strAltText; ///< alternate text for icon(s)
207  std::string m_strTagNormal; ///< button icon for normal state
208  std::string m_strToolTipNormal; ///< button tooltip for normal state
209  std::string m_strTagActive; ///< button icon for active state
210  std::string m_strToolTipActive; ///< button tooltip for active state
211 
212  /*!
213  * \brief Deep copy of source to this.
214  *
215  * \param pSrc Pointer to source button.
216  */
217  void copy(const WinButton *pSrc);
218  };
219 
220 
221  //...........................................................................
222  // Class WinButtonMenu
223  //...........................................................................
224 
225  /*!
226  * \brief Window button menu base class.
227  *
228  * Buttons with icons or text are placed on the left or right side of the
229  * application window. Each icon is associated with an application defined
230  * event.
231  */
233  {
234  public:
235  typedef std::map<int, WinButton*> MapBttns_T;
236  ///< map of added menu buttons
237 
238  /*!
239  * \brief WinButtonMenu initialization constructor.
240  *
241  * \param strIconPath Icon path of search directories.
242  */
243  WinButtonMenu(const std::string &strIconPath=WinIconDirDft)
244  {
245  m_pWin = NULL;
246  m_strIconPath = strIconPath;
247  m_nCurrentEvent = UIEventNone;
248  m_nPreviousEvent = UIEventNone;
249  }
250 
251  /*!
252  * \brief WinButtonMenu destructor.
253  */
254  virtual ~WinButtonMenu();
255 
256  /*!
257  * \brief Append directory to icon search path.
258  *
259  * \param strIconDir Directory holding icons.
260  */
261  void appendIconDir(const std::string &strIconDir)
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  }
272 
273  /*!
274  * \brief Prepend directory to icon search path.
275  *
276  * \param strIconDir Directory holding icons.
277  */
278  void prependIconDir(const std::string &strIconDir)
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  }
289 
290  /*!
291  * \brief Set icon search path.
292  *
293  * \param strIconPath Icon path of search directories.
294  */
295  void setIconPath(const std::string &strIconPath)
296  {
297  m_strIconPath = strIconPath;
298  }
299 
300  /*!
301  * \brief Get icon search path.
302  *
303  * \return Icon path string.
304  */
305  std::string getIconPath()
306  {
307  return m_strIconPath;
308  }
309 
310  /*!
311  * \brief Add button with image to menu.
312  *
313  * \note Buttons are added to window only at bind() time.
314  *
315  * \param nEvent Unique event (and id) associated with button
316  * press.
317  * \param eInitState Button widget initial state.
318  * See \ref rnrwin_widgetstate.
319  * \param eAlign Button alignment. See \ref rnmpwin_align.
320  * \param strAltText Button alternative text string.
321  * \param strIconNormal Button icon image file name for normal state.
322  * \param strToolTipNormal Button tooltip for normal state.
323  * \param strIconActive Button icon image file name for active state.
324  * Empty for no icon.
325  * \param strToolTipActive Button tooltip for active state.
326  *
327  * \return Returns true on success, false otherwise.
328  */
329  virtual bool addImageButton(int nEvent,
330  WidgetState eInitState,
331  AlignOp eAlign,
332  const std::string &strAltText,
333  const std::string &strIconNormal,
334  const std::string &strToolTipNormal = "",
335  const std::string &strIconActive = "",
336  const std::string &strToolTipActive = "")
337  {
338  return false;
339  }
340 
341  /*!
342  * \brief Add button with text label to menu.
343  *
344  * \note Buttons are added to window only at bind() time.
345  *
346  * \param nEvent Unique event (and id) associated with button
347  * press.
348  * \param eInitState Button widget initial state.
349  * See \ref rnrwin_widgetstate.
350  * \param eAlign Button alignment. See \ref rnmpwin_align.
351  * \param strLabelNormal Button text label for normal state.
352  * \param strToolTipNormal Button tooltip for normal state.
353  * \param strLabelActive Button text label for active state.
354  * Empty for no label.
355  * \param strToolTipActive Button tooltip for active state.
356  *
357  * \return Returns true on success, false otherwise.
358  */
359  virtual bool addLabelButton(int nEvent,
360  WidgetState eInitState,
361  AlignOp eAlign,
362  const std::string &strLabelNormal,
363  const std::string &strToolTipNormal = "",
364  const std::string &strLabelActive = "",
365  const std::string &strToolTipActive = "")
366  {
367  return false;
368  }
369 
370  /*!
371  * \brief Bind the button menu to the given window.
372  *
373  * Any previously bound menu is unbound. This menu is reset to its initial
374  * state. The menu buttons are added to the window and displayed.
375  *
376  * \param pWin Pointer to binding window.
377  */
378  virtual void bind(Win *pWin);
379 
380  /*!
381  * \brief Unbind the menu from the currently bound window.
382  */
383  virtual void unbind();
384 
385  /*!
386  * \brief Get the current button menu event.
387  *
388  * \return Current event.
389  */
391  {
392  return m_nCurrentEvent;
393  }
394 
395  /*!
396  * \brief Set the current button menu event.
397  *
398  * \param nNewEvent New event unique identifier.
399  *
400  * \return New current event.
401  */
402  int setCurrentEvent(int nNewEvent)
403  {
404  if( nNewEvent != m_nCurrentEvent )
405  {
406  m_nPreviousEvent = m_nCurrentEvent;
407  }
408  m_nCurrentEvent = nNewEvent;
409  return m_nCurrentEvent;
410  }
411 
412  /*!
413  * \brief Get the previous menu event.
414  *
415  * \return Current action.
416  */
418  {
419  return m_nPreviousEvent;
420  }
421 
422  /*!
423  * \brief Get the current number of menu items;
424  *
425  * \return Number of items.
426  */
428  {
429  return m_mapButtons.size();
430  }
431 
432  /*!
433  * \brief Get the given button state.
434  *
435  * \param nEvent Menu-wide unique event button identifier.
436  *
437  * \return Current button state. See \ref rnmpwin_widgetstate
438  */
440  {
442 
443  if( m_mapButtons.find(nEvent) != m_mapButtons.end() )
444  {
445  eState = m_mapButtons[nEvent]->m_eState;
446  }
447  return eState;
448  }
449 
450  /*!
451  * \brief Set the given button state.
452  *
453  * \param nEvent Menu-wide unique event button identifier.
454  * \param eNewState New button state. See \ref rnmpwin_widgetstate
455  */
456  void setButtonState(int nEvent, WidgetState eNewState)
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  }
465 
466  /*!
467  * \brief Set the states of the given buttons.
468  *
469  * \param strEvent Event reason string (used for logging only).
470  * \param ... Sequence of event,state pairs. The sequence is
471  * terminated by the UIEventNone special 'no' action
472  * event.
473  */
474  void setButtonStateList(const std::string &strEvent, ...);
475 
476  /*!
477  * \brief Reset all menu buttons states to their initial states.
478  */
479  void resetAllButtonStates();
480 
481  protected:
482  Win *m_pWin; ///< menus are bound to this window
483  std::string m_strIconPath; ///< icon directories search path
484  int m_nCurrentEvent; ///< current (user) menu event
485  int m_nPreviousEvent; ///< previous (user) menu event
486  MapBttns_T m_mapButtons; ///< menu button map
487 
488  /*!
489  * \brief Make icon image file path.
490  *
491  * The built file path is check of existence.
492  *
493  * \param strIconFile Icon file absolute or relative file name.
494  *
495  * \return Returns string containing icon path. Empty if no file specified
496  * or found.
497  */
498  std::string makeIconPath(const std::string &strIconFile);
499 
500  /*!
501  * \brief Change the state and gui look of the given button.
502  *
503  * \param pButton Pointer to menu button instance.
504  * \param eNewState New button state. See \ref rnmpwin_widgetstate
505  */
506  void changeButtonState(WinButton *pButton, WidgetState eNewState);
507 
508  /*!
509  * \brief Menu button press callback.
510  *
511  * \param nEvent Button event (and button id).
512  * \param user_data User-supplied data (this).
513  */
514  static void onMenuButtonClick(int nEvent, void *user_data);
515  };
516 
517 } // namespace
518 
519 
520 #endif // _RNR_WIN_BUTTON_MENU_H
WidgetState
Definition: Win.h:129
int getPreviousEvent()
Get the previous menu event.
Definition: WinMenu.h:417
WinButtonType m_eType
button type
Definition: WinMenu.h:201
WidgetState m_eInitState
initial button widget state
Definition: WinMenu.h:203
virtual ~WinButton()
Destructor.
Definition: WinMenu.h:150
Win * m_pWin
menus are bound to this window
Definition: WinMenu.h:482
WinButtonType
Window button types.
Definition: WinMenu.h:81
void copy(const WinButton *pSrc)
Deep copy of source to this.
Definition: WinMenu.cxx:80
Window button menu base class.
Definition: WinMenu.h:232
WinButtonMenu(const std::string &strIconPath=WinIconDirDft)
WinButtonMenu initialization constructor.
Definition: WinMenu.h:243
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
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
AlignOp m_eAlign
left or right alignment
Definition: WinMenu.h:205
std::string m_strAltText
alternate text for icon(s)
Definition: WinMenu.h:206
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.
Definition: WinMenu.h:359
void prependIconDir(const std::string &strIconDir)
Prepend directory to icon search path.
Definition: WinMenu.h:278
WidgetState m_eState
current button widget state
Definition: WinMenu.h:204
int getCurrentEvent()
Get the current button menu event.
Definition: WinMenu.h:390
void setButtonState(int nEvent, WidgetState eNewState)
Set the given button state.
Definition: WinMenu.h:456
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.
Definition: WinMenu.h:329
RoadNarrows Robotics Win abstract base class interface.
WinButton(WinButtonType eType, int nEvent, WidgetState eInitState, AlignOp eAlign, const std::string &strAltText, const std::string &strTagNormal, const std::string &strToolTipNormal="", const std::string &strTagActive="", const std::string &strToolTipActive="")
WinButton initialization constructor.
Definition: WinMenu.h:115
button has text label(s)
Definition: WinMenu.h:84
no action
Definition: Win.h:115
int m_nEvent
button push "mouse click" event
Definition: WinMenu.h:202
std::string getIconPath()
Get icon search path.
Definition: WinMenu.h:305
AlignOp
Definition: Win.h:81
std::string m_strTagNormal
button icon for normal state
Definition: WinMenu.h:207
std::string getCurrentToolTip()
Get the tooltip associated with the current button state.
Definition: WinMenu.cxx:126
normal - enabled but not selected
Definition: Win.h:131
button has image(s)
Definition: WinMenu.h:83
std::string m_strToolTipActive
button tooltip for active state
Definition: WinMenu.h:210
int setCurrentEvent(int nNewEvent)
Set the current button menu event.
Definition: WinMenu.h:402
std::string m_strToolTipNormal
button tooltip for normal state
Definition: WinMenu.h:208
WinButton(const WinButton &src)
WinButton copy constructor.
Definition: WinMenu.h:142
RNR Win window abstract base class.
Definition: Win.h:211
Window button base class.
Definition: WinMenu.h:94
std::map< int, WinButton * > MapBttns_T
map of added menu buttons
Definition: WinMenu.h:235
size_t getNumMenuButtons()
Get the current number of menu items;.
Definition: WinMenu.h:427
int m_nCurrentEvent
current (user) menu event
Definition: WinMenu.h:484
void setIconPath(const std::string &strIconPath)
Set icon search path.
Definition: WinMenu.h:295
WidgetState getButtonState(int nEvent)
Get the given button state.
Definition: WinMenu.h:439
void * getCurrentImageWidget()
Get the icon image widget associated with the current button state.
Definition: WinMenu.cxx:94
void appendIconDir(const std::string &strIconDir)
Append directory to icon search path.
Definition: WinMenu.h:261
std::string m_strIconPath
icon directories search path
Definition: WinMenu.h:483
RoadNarrows Robotics.
Definition: Camera.h:74
int m_nPreviousEvent
previous (user) menu event
Definition: WinMenu.h:485
std::string m_strTagActive
button icon for active state
Definition: WinMenu.h:209