libyui  3.9.3
YDialogHelpers.cc
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YDialogHelpers.cc
20 
21  Author: Jiri Srain <jsrain@suse.cz>
22 
23 /-*/
24 
25 
26 #include <sys/types.h>
27 #include <dirent.h>
28 
29 #define YUILogComponent "ui"
30 #include "YUILog.h"
31 
32 #include "YDialog.h"
33 #include "YEvent.h"
34 #include "YPushButton.h"
35 #include "YButtonBox.h"
36 
37 #include "YUI.h"
38 #include "YApplication.h"
39 #include "YWidgetFactory.h"
40 #include "YOptionalWidgetFactory.h"
41 #include "YLayoutBox.h"
42 #include "YRichText.h"
43 #include "YAlignment.h"
44 #include "YUIException.h"
45 #include "YEventFilter.h"
46 #include "YWidgetID.h"
47 #include "YDumbTab.h"
48 
49 
50 using std::string;
51 using std::map;
52 using std::vector;
53 
54 
55 void
56 YDialog::showText( const string & text, bool useRichText )
57 {
58 
59  // set help text dialog size to 80% of topmost dialog, respectively 45x15 (default)
60 
61  unsigned int dialogWidth = 45;
62  unsigned int dialogHeight = 15;
63 
64  if ( ! _dialogStack.empty() )
65  {
66  YDialog * dialog = _dialogStack.top();
67  dialogWidth = (unsigned int) ( (float) dialog->preferredWidth() * 0.8 );
68  dialogHeight = (unsigned int) ( (float) dialog->preferredHeight() * 0.8 );
69  }
70 
71  // limit dialog to a reasonable size
72  if ( dialogWidth > 80 || dialogHeight > 25 )
73  {
74  dialogWidth = 80;
75  dialogHeight = 25;
76  }
77 
78  try
79  {
80  YDialog * dialog = YUI::widgetFactory()->createPopupDialog();
81  YAlignment * minSize = YUI::widgetFactory()->createMinSize( dialog, dialogWidth, dialogHeight );
82  YLayoutBox * vbox = YUI::widgetFactory()->createVBox( minSize );
83  YUI::widgetFactory()->createRichText( vbox, text, ! useRichText );
84  YButtonBox * buttonBox = YUI::widgetFactory()->createButtonBox( vbox );
85  YPushButton * okButton = YUI::widgetFactory()->createPushButton( buttonBox, "&OK" );
86  okButton->setRole( YOKButton );
87  okButton->setDefaultButton();
88 
89  dialog->waitForEvent();
90  dialog->destroy();
91  }
92  catch ( const YUIException & exception )
93  {
94  // Don't let the application die just because help couldn't be displayed.
95 
96  YUI_CAUGHT( exception );
97  }
98 }
99 
100 
101 bool
103 {
104  string helpText;
105 
106  while ( widget )
107  {
108  if ( ! widget->helpText().empty() )
109  {
110  yuiDebug() << "Found help text for " << widget << endl;
111  helpText = widget->helpText();
112  }
113 
114  widget = widget->parent();
115  }
116 
117  if ( ! helpText.empty() )
118  {
119  yuiMilestone() << "Showing help text" << endl;
120  showText( helpText, true );
121 
122  yuiMilestone() << "Help dialog closed" << endl;
123  }
124  else // No help text
125  {
126  yuiWarning() << "No help text" << endl;
127  }
128 
129  return ! helpText.empty();
130 }
131 
132 
133 bool
135 {
136  yuiMilestone() <<"Showing Release Notes" << endl;
137 
138  // set help text dialog size to 80% of topmost dialog, respectively 45x15 (default)
139 
140  unsigned int dialogWidth = 45;
141  unsigned int dialogHeight = 15;
142 
143  if ( ! _dialogStack.empty() )
144  {
145  YDialog * dialog = _dialogStack.top();
146  dialogWidth = (unsigned int) ( (float) dialog->preferredWidth() * 0.8 );
147  dialogHeight = (unsigned int) ( (float) dialog->preferredHeight() * 0.8 );
148  }
149 
150  // limit dialog to a reasonable size
151  if ( dialogWidth > 80 || dialogHeight > 25 )
152  {
153  dialogWidth = 80;
154  dialogHeight = 25;
155  }
156 
157  try
158  {
159  map<string,string> relnotes = YUI::application()->releaseNotes();
160 
161  if ( relnotes.size() == 0)
162  {
163  return false;
164  }
165 
166  vector<string> keys;
167 
168  for ( map<string,string>::const_iterator it = relnotes.begin(); it != relnotes.end(); ++it )
169  {
170  keys.push_back( it->first );
171  }
172 
173  YDialog * dialog = YUI::widgetFactory()->createPopupDialog();
174  YAlignment * minSize = YUI::widgetFactory()->createMinSize( dialog, dialogWidth, dialogHeight );
175  YLayoutBox * vbox = YUI::widgetFactory()->createVBox( minSize );
176  YDumbTab * rnTab = 0;
177  YRichText * richtext = 0;
178  bool usePlainTextMode = YUI::app()->isTextMode();
179 
180  // both Qt and NCurses do support DumbTab
181  if ( relnotes.size() > 1 && YUI::optionalWidgetFactory()->hasDumbTab() )
182  {
183  rnTab = YUI::optionalWidgetFactory()->createDumbTab( vbox );
184  int index = 0;
185 
186  for ( map<string,string>::const_iterator it = relnotes.begin(); it != relnotes.end(); it++ )
187  {
188  YItem * item = new YItem( (*it).first );
189  item->setIndex( index++ );
190  rnTab->addItem( item );
191  }
192 
193  richtext = YUI::widgetFactory()->createRichText( rnTab,
194  (*(relnotes.begin())).second,
195  usePlainTextMode );
196  }
197  else
198  {
199  richtext = YUI::widgetFactory()->createRichText( vbox,
200  (*(relnotes.begin())).second,
201  usePlainTextMode );
202  }
203 
204  YButtonBox * buttonBox = YUI::widgetFactory()->createButtonBox( vbox );
205  YPushButton * okButton = YUI::widgetFactory()->createPushButton( buttonBox, "&OK" );
206  okButton->setRole( YOKButton );
207  okButton->setDefaultButton();
208 
209  while ( true )
210  {
211  YEvent * event = dialog->waitForEvent();
212 
213  if ( event )
214  {
215  if ( event->eventType() == YEvent::MenuEvent && event->item())
216  {
217  YItem * item = dynamic_cast<YItem *>( event->item() );
218  richtext->setValue( relnotes[ keys[ item->index() ] ] );
219  }
220  else if ( event->eventType() == YEvent::CancelEvent || // window manager "close window" button
221  event->widget() == okButton )
222  {
223  break;
224  }
225  }
226  }
227 
228  dialog->destroy();
229  }
230  catch ( const YUIException & exception )
231  {
232  // Don't let the application die just because the release notes couldn't be displayed.
233 
234  YUI_CAUGHT( exception );
235  }
236 
237  return true;
238 }
static bool showHelpText(YWidget *widget)
Show the help text for the specified widget.
static YWidgetFactory * widgetFactory()
Return the widget factory that provides all the createXY() methods for standard (mandatory, i.e.
Definition: YUI.cc:132
A vertical or horizontal stacking of widgets, implementing HBox and VBox.
Definition: YLayoutBox.h:37
DumbTab: A very simple tab widget that can display and switch between a number of tabs...
Definition: YDumbTab.h:40
void setIndex(int index)
Set this item&#39;s index.
Definition: YItem.h:133
static YApplication * application()
Aliases for YUI::app()
Definition: YUI.h:112
virtual void setDefaultButton(bool def=true)
Make this button the default button.
Definition: YPushButton.cc:98
Abstract base class for events to be returned upon UI::UserInput() and related functions.
Definition: YEvent.h:43
YWidget * parent() const
Return this widget&#39;s parent or 0 if it doesn&#39;t have a parent.
Definition: YWidget.cc:271
virtual void setValue(const std::string &newValue)
Change the text content of the RichText widget.
Definition: YRichText.cc:74
int index() const
Return the index of this item (as set with setIndex() ).
Definition: YItem.h:138
std::string helpText() const
Return the help text for this widget.
Definition: YWidget.cc:342
virtual int preferredWidth()
Preferred width of the widget.
A push button; may have an icon, and a F-key shortcut.
Definition: YPushButton.h:37
virtual void addItem(YItem *item)
Add an item (a tab page).
Definition: YDumbTab.cc:68
virtual void setRole(YButtonRole role)
Set a predefined role for this button.
Definition: YPushButton.cc:154
Implementation of all the alignment widgets:
Definition: YAlignment.h:41
static std::stack< YDialog * > _dialogStack
Stack holding all currently existing dialogs.
Definition: YDialog.h:415
Simple item class for SelectionBox, ComboBox, MultiSelectionBox etc.
Definition: YItem.h:49
static YOptionalWidgetFactory * optionalWidgetFactory()
Return the widget factory that provides all the createXY() methods for optional ("special") widgets a...
Definition: YUI.cc:147
static bool showRelNotesText()
Show the release notes.
static void showText(const std::string &text, bool richText=false)
Show the specified text in a pop-up dialog with a local event loop.
Text formatted with simple HTML-like tags, with "links" generating events.
Definition: YRichText.h:40
static YApplication * app()
Return the global YApplication object.
Definition: YUI.cc:162
A window in the desktop environment.
Definition: YDialog.h:47
Container widget for dialog buttons that abstracts the button order depending on the desktop environm...
Definition: YButtonBox.h:148
Abstract base class of all UI widgets.
Definition: YWidget.h:54
Base class for UI Exceptions.
Definition: YUIException.h:297
bool destroy(bool doThrow=true)
Close and delete this dialog (and all its children) if it is the topmost dialog.
Definition: YDialog.cc:234
YEvent * waitForEvent(int timeout_millisec=0)
Wait for a user event.
Definition: YDialog.cc:361
std::map< std::string, std::string > releaseNotes() const
Get the current release notes map.
virtual int preferredHeight()
Preferred height of the widget.