libyui-mga  1.0.7
YMGAMsgBox.cc
1 /*
2  Copyright 2014 by Angelo Naselli
3 
4  This library is free software; you can redistribute it and/or modify
5  it under the terms of the GNU Lesser General Public License as
6  published by the Free Software Foundation; either version 2.1 of the
7  License, or (at your option) version 3.0 of the License. This library
8  is distributed in the hope that it will be useful, but WITHOUT ANY
9  WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
11  License for more details. You should have received a copy of the GNU
12  Lesser General Public License along with this library; if not, write
13  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
14  Floor, Boston, MA 02110-1301 USA
15 */
16 
17 /*-/
18 
19  File: YMGAMessageBox.cc
20 
21  Author: Angelo Naselli <anaselli@linux.it>
22 
23 /-*/
24 
25 #define YUILogComponent "mga-ui"
26 #include "YUILog.h"
27 
28 #include <yui/YUI.h>
29 #include <yui/YTypes.h>
30 #include <yui/YApplication.h>
31 #include <yui/YWidgetFactory.h>
32 #include <yui/YDialog.h>
33 #include <yui/YLayoutBox.h>
34 #include <yui/YAlignment.h>
35 #include <yui/YPushButton.h>
36 #include <yui/YEvent.h>
37 #include <yui/YRichText.h>
38 #include <YButtonBox.h>
39 
40 #include <boost/algorithm/string/trim.hpp>
41 #include <vector>
42 
43 #include "YMGAMsgBox.h"
44 
45 using std::endl;
46 
48 public:
49  YMGAMessageBoxPrivate() : title(), text(),
50  icon(), useRichText(false),
51  minWidth(40), minHeight(15),
52  buttons(YMGAMessageBox::B_ONE),
54  default_button(YMGAMessageBox::B_ONE) {}
55 
56  virtual ~YMGAMessageBoxPrivate(){}
57 
58  std::string title;
59  std::string text;
60  std::string icon;
61  bool useRichText;
62  YLayoutSize_t minWidth;
63  YLayoutSize_t minHeight;
66  YMGAMessageBox::DLG_BUTTON default_button;
67  std::vector<std::string> label;
68 };
69 
71  YMGAMessageBox::DLG_MODE dlg_mode) :
72  priv ( new YMGAMessageBoxPrivate())
73 {
74  YUI_CHECK_NEW ( priv );
75 
76  priv->buttons = b_num;
77  priv->mode = dlg_mode;
78  priv->label.resize((b_num==B_TWO ? 2 : 1));
79 }
80 
81 
82 
84 {
85  delete priv;
86 }
87 
88 
89 void YMGAMessageBox::setIcon(const std::string& icon)
90 {
91  priv->icon = icon;
92  boost::algorithm::trim(priv->icon);
93 }
94 
95 void YMGAMessageBox::setTitle(const std::string& title)
96 {
97  priv->title = title;
98 }
99 
100 void YMGAMessageBox::setText(const std::string& text, bool useRichText)
101 {
102  priv->text = text;
103  priv->useRichText = useRichText;
104 }
105 
106 void YMGAMessageBox::setMinSize(YLayoutSize_t minWidth, YLayoutSize_t minHeight)
107 {
108  // limit dialog to a reasonable size
109  if ( minWidth > 80 || minHeight > 25 )
110  {
111  minWidth = 80;
112  minHeight = 25;
113  }
114  priv->minHeight = minHeight;
115  priv->minWidth = minWidth;
116 }
117 
118 void YMGAMessageBox::setButtonLabel(const std::string& label, DLG_BUTTON button)
119 {
120  if ( priv->buttons != B_TWO && button == B_TWO)
121  YUI_THROW( YUIException( "Only one button configured" ) );
122 
123  priv->label[button] = label;
124 }
125 
127 {
128  if ( priv->buttons != B_TWO && button == B_TWO)
129  YUI_THROW( YUIException( "Only one button configured" ) );
130 
131  priv->default_button = button;
132 }
133 
134 
136 {
137  DLG_BUTTON btn = priv->default_button;
138 
139  std::string oldTitle = YUI::app()->applicationTitle();
140  YUI::app()->setApplicationTitle(priv->title);
141  if(priv->icon.length())
142  YUI::app()->setApplicationIcon(priv->icon);
143 
144  YDialogColorMode colorMode;
145  if (priv->mode == D_INFO) {
146  colorMode = YDialogInfoColor;
147  }
148  else if (priv->mode == D_WARNING) {
149  colorMode = YDialogWarnColor;
150  }
151  else { //D_NORMAL
152  colorMode = YDialogNormalColor;
153  }
154 
155  YDialog *pDialog = YUI::widgetFactory()->createPopupDialog(colorMode);
156  YAlignment* minSize = YUI::widgetFactory()->createMinSize( pDialog, priv->minWidth, priv->minHeight );
157  auto vbox = YUI::widgetFactory()->createVBox( minSize );
158 
159  auto midhbox = YUI::widgetFactory()->createHBox(vbox);
160  // app description
161  auto toprightvbox = YUI::widgetFactory()->createVBox(midhbox);
162  toprightvbox->setWeight(YD_HORIZ, 5);
163  YUI::widgetFactory()->createSpacing(toprightvbox,YD_HORIZ,false,5.0);
164  YUI::widgetFactory()->createRichText(toprightvbox, priv->text, !priv->useRichText);
165  YUI::widgetFactory()->createSpacing(toprightvbox,YD_HORIZ,false,5.0);
166 
167  // info button, if information are defined
168  auto bottomhbox = YUI::widgetFactory()->createHBox(vbox);
169  YPushButton *pB1=nullptr, *pB2=nullptr;
170  if (priv->buttons == B_ONE) {
171  pB1 = YUI::widgetFactory()->createPushButton(bottomhbox, priv->label[B_ONE]);
172  }
173  else { // B_TWO
174  auto alignRight = YUI::widgetFactory()->createRight(bottomhbox);
175  auto buttonBox = YUI::widgetFactory()->createHBox( alignRight );
176  pB1 = YUI::widgetFactory()->createPushButton(buttonBox, priv->label[B_ONE]);
177  pB2 = YUI::widgetFactory()->createPushButton(buttonBox, priv->label[B_TWO]);
178  }
179  if (priv->default_button == B_TWO)
180  pDialog->setDefaultButton(pB2);
181  else
182  pDialog->setDefaultButton(pB1);
183 
184  while(true)
185  {
186  YEvent* event = pDialog->waitForEvent();
187  if(event)
188  {
189  // window manager "close window" button
190  if ( event->eventType() == YEvent::CancelEvent) {
191  break; // leave event loop -> return default_button
192  }
193  else if( event->widget() == pB1 ) {
194  btn = B_ONE;
195  break;
196  }
197  else if (pB2 != nullptr && event->widget() == pB2) {
198  btn = B_TWO;
199  break;
200  }
201  }
202  }
203 
204  pDialog->destroy();
205  YUI::app()->setApplicationTitle(oldTitle);
206 
207  return btn;
208 }
YMGAMessageBox(DLG_BUTTON b_num=B_ONE, DLG_MODE dlg_mode=D_NORMAL)
The constructor.
Definition: YMGAMsgBox.cc:70
DLG_BUTTON show()
it shows the message box dialog using data set by user.
Definition: YMGAMsgBox.cc:135
void setButtonLabel(const std::string &label, DLG_BUTTON button=B_ONE)
sets the message box button name (empty string is assigned by default)
Definition: YMGAMsgBox.cc:118
two buttons dialog, or button two pressed
Definition: YMGAMsgBox.h:41
Info dialog.
Definition: YMGAMsgBox.h:48
One button dialog, or button one pressed.
Definition: YMGAMsgBox.h:39
Normal dialog.
Definition: YMGAMsgBox.h:46
void setDefaultButton(DLG_BUTTON button=B_ONE)
sets the message box default button
Definition: YMGAMsgBox.cc:126
void setText(const std::string &text, bool useRichText=false)
sets the message box text information
Definition: YMGAMsgBox.cc:100
Warning dialog.
Definition: YMGAMsgBox.h:50
void setMinSize(YLayoutSize_t minWidth, YLayoutSize_t minHeight)
sets the dilaog box minimum size according to YWidgetFactory::createMinSize()
Definition: YMGAMsgBox.cc:106
void setTitle(const std::string &title)
sets the message box title
Definition: YMGAMsgBox.cc:95
virtual ~YMGAMessageBox()
Destructor.
Definition: YMGAMsgBox.cc:83
void setIcon(const std::string &icon)
sets the message box icon (full path)
Definition: YMGAMsgBox.cc:89