appkit  1.5.1
RoadNarrows Robotics Application Kit
WinGtkMenu.cxx
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Application Tool Kit
4 //
5 // Library: librnr_wingtk
6 //
7 // File: WinGtkMenu.cxx
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2013-05-03 07:45:13 -0600 (Fri, 03 May 2013) $
12  * $Rev: 2904 $
13  *
14  * \brief RoadNarrows Robotics GTK derived window button menu implementation.
15  *
16  * \author Robin Knight (robin.knight@roadnarrows.com)
17  * \author Daniel Packard (daniel@roadnarrows.com)
18  *
19  * \par Copyright
20  * \h_copy 2011-2017. RoadNarrows LLC.\n
21  * http://www.roadnarrows.com\n
22  * All Rights Reserved
23  */
24 /*
25  * @EulaBegin@
26  *
27  * Permission is hereby granted, without written agreement and without
28  * license or royalty fees, to use, copy, modify, and distribute this
29  * software and its documentation for any purpose, provided that
30  * (1) The above copyright notice and the following two paragraphs
31  * appear in all copies of the source code and (2) redistributions
32  * including binaries reproduces these notices in the supporting
33  * documentation. Substantial modifications to this software may be
34  * copyrighted by their authors and need not follow the licensing terms
35  * described here, provided that the new terms are clearly indicated in
36  * all files where they apply.
37  *
38  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
39  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
40  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
41  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
42  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
43  * THE POSSIBILITY OF SUCH DAMAGE.
44  *
45  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
46  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
47  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
48  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
49  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
50  *
51  * @EulaEnd@
52  */
53 ////////////////////////////////////////////////////////////////////////////////
54 
55 #include <sys/types.h>
56 #include <limits.h>
57 #include <libgen.h>
58 #include <unistd.h>
59 #include <stdarg.h>
60 
61 #include <string>
62 
63 #include "rnr/rnrconfig.h"
64 #include "rnr/log.h"
65 
66 #include "rnr/appkit/Win.h"
67 #include "rnr/appkit/WinMenu.h"
68 #include "rnr/appkit/WinGtk.h"
69 #include "rnr/appkit/WinGtkMenu.h"
70 
71 
72 using namespace std;
73 using namespace rnr;
74 
75 
76 //.............................................................................
77 // Class WinGtkButton
78 //.............................................................................
79 
80 WinGtkButton::WinGtkButton(WinButtonType eType,
81  int nEvent,
82  WidgetState eInitState,
83  AlignOp eAlign,
84  const string &strAltText,
85  const string &strTagNormal,
86  const string &strToolTipNormal,
87  const string &strTagActive,
88  const string &strToolTipActive) :
89  WinButton(eType, nEvent, eInitState, eAlign, strAltText,
90  strTagNormal, strToolTipNormal,
91  strTagActive, strToolTipActive)
92 {
93  if( eType == WinButtonTypeImage )
94  {
95  m_wImgNormal = loadIcon(strTagNormal);
96  m_wImgActive = loadIcon(strTagActive);
97  }
98  else
99  {
100  m_wImgNormal = NULL;
101  m_wImgActive = NULL;
102  }
103 }
104 
106 {
107  if( m_eType == WinButtonTypeImage )
108  {
111  }
112  else
113  {
114  m_wImgNormal = NULL;
115  m_wImgActive = NULL;
116  }
117 }
118 
120 {
121  if( m_wImgNormal != NULL )
122  {
123  gtk_widget_destroy(m_wImgNormal);
124  }
125 
126  if( m_wImgActive != NULL )
127  {
128  gtk_widget_destroy(m_wImgActive);
129  }
130 }
131 
132 GtkWidget *WinGtkButton::loadIcon(const string &strIconPath)
133 {
134  GtkWidget *wBttnImg;
135 
136  if( access(strIconPath.c_str(), F_OK|R_OK) != 0 )
137  {
138  LOGSYSERROR("%s: %s(errno=%d)", strIconPath.c_str());
139  return NULL;
140  }
141 
142  // never returns null - will return "broken" icon instead
143  wBttnImg = gtk_image_new_from_file(strIconPath.c_str());
144 
145  // menu item owns this widget
146  gtk_widget_ref(wBttnImg);
147 
148  return wBttnImg;
149 }
150 
151 
152 //.............................................................................
153 // Class WinGtkButtonMenu
154 //.............................................................................
155 
157  WidgetState eInitState,
158  AlignOp eAlign,
159  const string &strAltText,
160  const string &strIconNormal,
161  const string &strToolTipNormal,
162  const string &strIconActive,
163  const string &strToolTipActive)
164 {
165  // button associated with the given event already exists
166  if( m_mapButtons.find(nEvent) != m_mapButtons.end() )
167  {
168  return false;
169  }
170 
171  string strIconPathNormal = makeIconPath(strIconNormal);
172  string strIconPathActive = makeIconPath(strIconActive);
173 
174  // add image button
175  if( !strIconPathNormal.empty() )
176  {
177  m_mapButtons[nEvent] = new WinGtkButton(WinButtonTypeImage,
178  nEvent,
179  eInitState,
180  eAlign,
181  strAltText,
182  strIconPathNormal,
183  strToolTipNormal,
184  strIconPathActive,
185  strToolTipActive);
186 
187  }
188 
189  // no image, make an label button instead
190  else
191  {
192  m_mapButtons[nEvent] = new WinGtkButton(WinButtonTypeLabel,
193  nEvent,
194  eInitState,
195  eAlign,
196  "",
197  strAltText,
198  strToolTipNormal);
199  }
200 
201  return true;
202 }
203 
205  WidgetState eInitState,
206  AlignOp eAlign,
207  const string &strLabelNormal,
208  const string &strToolTipNormal,
209  const string &strLabelActive,
210  const string &strToolTipActive)
211 {
212  // button associated with the given event already exists
213  if( m_mapButtons.find(nEvent) != m_mapButtons.end() )
214  {
215  return false;
216  }
217 
218  // add label button
219  m_mapButtons[nEvent] = new WinGtkButton(WinButtonTypeLabel,
220  nEvent,
221  eInitState,
222  eAlign,
223  "",
224  strLabelNormal,
225  strToolTipNormal,
226  strLabelActive,
227  strToolTipNormal);
228 
229  return true;
230 }
WidgetState
Definition: Win.h:129
WinButtonType m_eType
button type
Definition: WinMenu.h:201
WinButtonType
Window button types.
Definition: WinMenu.h:81
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: WinGtkMenu.cxx:204
RoadNarrows Robotics GTK derived window button menu interface.
RoadNarrows Robotics GTK derived WinGtk window class interface.
GtkWidget * m_wImgActive
active state menu button icon
Definition: WinGtkMenu.h:151
RoadNarrows Robotics Win abstract base class interface.
WinGtkButton(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="")
WinGtkButton initialization constructor.
RoadNarrows Robotics base window button menu interface.
button has text label(s)
Definition: WinMenu.h:84
virtual ~WinGtkButton()
Destructor.
Definition: WinGtkMenu.cxx:119
AlignOp
Definition: Win.h:81
std::string m_strTagNormal
button icon for normal state
Definition: WinMenu.h:207
button has image(s)
Definition: WinMenu.h:83
GtkWidget * loadIcon(const std::string &strIconPath)
Load icon image from file.
Definition: WinGtkMenu.cxx:132
GtkWidget * m_wImgNormal
normal state menu button icon
Definition: WinGtkMenu.h:150
Window button base class.
Definition: WinMenu.h:94
GTK window derived button class.
Definition: WinGtkMenu.h:82
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: WinGtkMenu.cxx:156
RoadNarrows Robotics.
Definition: Camera.h:74
std::string m_strTagActive
button icon for active state
Definition: WinMenu.h:209