OpenASIP  2.0
MessageDialog.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file MessageDialog.cc
26  *
27  * Implementation of MessageDialog class.
28  *
29  * @author Veli-Pekka Jääskeläinen (vjaaskel-no.spam-cs.tut.fi) 2005
30  * @note rating: red
31  */
32 
33 
34 #include <wx/tokenzr.h>
35 #include "MessageDialog.hh"
36 #include "WxConversion.hh"
37 
38 
39 // Maximum number of charactes per line.
40 const unsigned int MAX_LINE_WIDTH = 100;
41 // Maximum number of lines.
42 const int MAX_LINES = 20;
43 
44 
45 /**
46  * Constructor.
47  */
49  wxWindow* parent,
50  const wxString& title,
51  const wxString& message,
52  long style):
53  wxMessageDialog(
54  parent,
55  suppressLines(wordWrap(message, MAX_LINE_WIDTH), MAX_LINES),
56  title, style) {
57 
58 }
59 
60 
61 
62 /**
63  * Destructor.
64  */
66 }
67 
68 
69 /**
70  * Wraps lines longer than given width to multiple lines. Existing line
71  * breaks aren't modified, but are taken to account when determining line
72  * lengths. Lines are wrapped at word boundaries only, unless a word is
73  * longer than the given line width.
74  *
75  * @param string String to wrap.
76  * @param lineWidth Maximum line width.
77  * @return Word wrapped string.
78  */
79 wxString
80 MessageDialog::wordWrap(const wxString& string, unsigned int lineWidth) {
81 
82  wxString wrapped;
83  wxStringTokenizer lines(string, _T("\n"), wxTOKEN_RET_EMPTY);
84 
85  while (lines.HasMoreTokens()) {
86  // Handle each existing line of the string separately.
87  wxString line = lines.GetNextToken();
88  wxStringTokenizer words(line, _T(" \t"), wxTOKEN_RET_DELIMS);
89  wxString wrappedLine;
90  int length = 0;
91  while (words.HasMoreTokens()) {
92  // Handle lines word by word.
93  wxString word = words.GetNextToken();
94  if (length + word.length() < lineWidth) {
95  // Word fits to the current line.
96  wrappedLine.Append(word);
97  length = length + word.length();
98  } else if (word.length() < lineWidth) {
99  // Word doesn't fit to the current line, but isn't longer
100  // than the maximum line width.
101  wrappedLine.Append(_(" \n"));
102  wrappedLine.Append(word);
103  length = word.length();
104  } else {
105  // Word is longer than the maximum line width.
106  unsigned int i = 0;
107  for (; i < word.length(); i = i + (lineWidth - 1)) {
108  wrappedLine.Append(_T(" \n"));
109  wrappedLine.Append(word.Mid(i, lineWidth - 1));
110  length = word.length() - i;
111  }
112  }
113  }
114  wrappedLine.Append(_T("\n"));
115  wrapped.Append(wrappedLine);
116  }
117 
118  return wrapped;
119 }
120 
121 /**
122  * Truncates a string to given number of lines. If lines are suppressed,
123  * text '[n lines suppressed.]' is appended to the truncated
124  * string, where n is the number of lines suppressd.
125  *
126  * @param string String to truncate.
127  * @param maxLines Maximum number of lines.
128  * @return Truncated string.
129  */
130 wxString
131 MessageDialog::suppressLines(const wxString& string, int maxLines) {
132 
133  int lineCount = 0;
134  wxString truncated;
135 
136  wxStringTokenizer lines(
137  string, _T("\n"), wxTOKEN_RET_EMPTY|wxTOKEN_RET_DELIMS);
138 
139  while (lineCount < maxLines && lines.HasMoreTokens()) {
140  // Get maximum number of lines to the result string.
141  lineCount++;
142  truncated.Append(lines.GetNextToken());
143  }
144 
145  if (!lines.HasMoreTokens()) {
146  // No lines suppressed.
147  return truncated;
148  }
149 
150  int suppressed = 0;
151  while (lines.HasMoreTokens()) {
152  // Count suppressed lines.
153  lines.GetNextToken();
154  suppressed++;
155  }
156 
157  truncated.Append(_T("\n [ "));
158  truncated.Append(WxConversion::toWxString(suppressed));
159  truncated.Append(_T(" lines suppressed.]"));
160  return truncated;
161 }
WxConversion::toWxString
static wxString toWxString(const std::string &source)
MessageDialog.hh
MessageDialog::~MessageDialog
virtual ~MessageDialog()
Definition: MessageDialog.cc:65
MAX_LINES
const int MAX_LINES
Definition: MessageDialog.cc:42
MAX_LINE_WIDTH
const unsigned int MAX_LINE_WIDTH
Definition: MessageDialog.cc:40
MessageDialog::MessageDialog
MessageDialog(wxWindow *parent, const wxString &title, const wxString &message, long style)
Definition: MessageDialog.cc:48
MessageDialog::wordWrap
wxString wordWrap(const wxString &string, unsigned int lineWidth)
Definition: MessageDialog.cc:80
WxConversion.hh
MessageDialog::suppressLines
wxString suppressLines(const wxString &string, int maxLines)
Definition: MessageDialog.cc:131