88using System . Windows . Media ;
99using System . Windows . Controls ;
1010using System . Windows . Documents ;
11+ using System . Windows . Interop ;
1112
1213using SVGImage . SVG ;
1314
15+ using ShellFileDialogs ;
16+ using Notification . Wpf ;
17+
1418namespace ClipArtViewer
1519{
1620 /// <summary>
@@ -57,12 +61,14 @@ public List<SVGItem> Items
5761 }
5862
5963 private bool _isShown ;
64+ private TextBoxTraceListener _listener ;
6065
6166 public MainWindow ( )
6267 {
6368 InitializeComponent ( ) ;
6469
6570 this . Loaded += OnMainWindowLoaded ;
71+ this . Closing += OnMainWindowClosing ;
6672 }
6773
6874 protected override void OnContentRendered ( EventArgs e )
@@ -95,7 +101,24 @@ private void OnMainWindowLoaded(object sender, RoutedEventArgs e)
95101 {
96102 txtLogger . Document . Blocks . Clear ( ) ;
97103 txtLogger . SetValue ( Block . LineHeightProperty , 1.0 ) ;
98- Trace . Listeners . Add ( new TextBoxTraceListener ( txtLogger ) ) ;
104+
105+ if ( _listener == null )
106+ {
107+ _listener = new TextBoxTraceListener ( txtLogger ) ;
108+ Trace . Listeners . Add ( _listener ) ;
109+ }
110+
111+ Trace . WriteLine ( "" ) ;
112+ }
113+
114+ private void OnMainWindowClosing ( object sender , System . ComponentModel . CancelEventArgs e )
115+ {
116+ if ( _listener != null )
117+ {
118+ Trace . Listeners . Remove ( _listener ) ;
119+ _listener . Dispose ( ) ;
120+ _listener = null ;
121+ }
99122 }
100123
101124 void ListFiles ( )
@@ -105,7 +128,19 @@ void ListFiles()
105128 {
106129 return ;
107130 }
131+
132+ if ( m_items . Count != 0 )
133+ {
134+ m_items . Clear ( ) ;
135+ m_filelist . Items . Refresh ( ) ;
136+ }
137+
108138 string [ ] files = Directory . GetFiles ( path , "*.svg" ) ;
139+ if ( files == null || files . Length == 0 )
140+ {
141+ return ;
142+ }
143+
109144 foreach ( string file in files )
110145 m_items . Add ( new SVGItem ( file ) ) ;
111146
@@ -132,7 +167,15 @@ private void OnTabControlSelectionChanged(object sender, SelectionChangedEventAr
132167
133168 private void OnBrowseButtonClick ( object sender , RoutedEventArgs e )
134169 {
135- MessageBox . Show ( "Not implemented yet!" , "ClipArtViewer" , MessageBoxButton . OK , MessageBoxImage . Information ) ;
170+ var sampleDir = Path . GetFullPath ( SamplesDir ) ;
171+ IntPtr windowHandle = new WindowInteropHelper ( this ) . Handle ;
172+ string selectedDirectory = FolderBrowserDialog . ShowDialog ( windowHandle ,
173+ "Select the location of the SVG files" , Path . GetDirectoryName ( sampleDir ) ) ;
174+ if ( selectedDirectory != null )
175+ {
176+ this . SvgPath = selectedDirectory ;
177+ ListFiles ( ) ;
178+ }
136179 }
137180 }
138181
@@ -178,7 +221,7 @@ void EnsureLoaded()
178221 return ;
179222 }
180223 _isLogged = true ;
181- Trace . WriteLine ( "Exception Loading: " + this . FullPath ) ;
224+ Trace . TraceError ( "Exception Loading: " + this . FullPath ) ;
182225 Trace . WriteLine ( ex . ToString ( ) ) ;
183226 Trace . WriteLine ( string . Empty ) ;
184227 }
@@ -187,11 +230,22 @@ void EnsureLoaded()
187230
188231 public class TextBoxTraceListener : TraceListener
189232 {
233+ private delegate void AppendTextDelegate ( string message ) ;
234+
235+ private const string AppTitle = "ClipArtViewer" ;
236+ private const string AppName = "ClipArtViewer.exe" ;
237+
190238 private RichTextBox _textBox ;
239+ private NotificationManager _notifyIcon ;
240+
241+ private AppendTextDelegate _appendText ;
191242
192243 public TextBoxTraceListener ( RichTextBox textBox )
193244 {
194- _textBox = textBox ;
245+ _textBox = textBox ;
246+ _notifyIcon = new NotificationManager ( ) ;
247+
248+ _appendText = new AppendTextDelegate ( AppendText ) ;
195249 }
196250
197251 public override void Write ( string message )
@@ -202,27 +256,72 @@ public override void Write(string message)
202256 }
203257 else
204258 {
205- _textBox . Dispatcher . Invoke ( new AppendTextDelegate ( AppendText ) , message ) ;
259+ _textBox . Dispatcher . Invoke ( _appendText , message ) ;
206260 }
207261 }
208262
209263 public override void WriteLine ( string message )
210264 {
211265 if ( _textBox . Dispatcher . CheckAccess ( ) )
212266 {
213- AppendText ( message ) ;
267+ AppendText ( message + Environment . NewLine ) ;
214268 }
215269 else
216270 {
217- _textBox . Dispatcher . Invoke ( new AppendTextDelegate ( AppendText ) , message ) ;
271+ _textBox . Dispatcher . Invoke ( _appendText , message + Environment . NewLine ) ;
218272 }
219273 }
220274
221- private delegate void AppendTextDelegate ( string message ) ;
222-
223275 private void AppendText ( string message )
224276 {
225- _textBox . AppendText ( message + Environment . NewLine ) ;
277+ if ( _textBox == null || message == null )
278+ {
279+ return ;
280+ }
281+
282+ if ( message . StartsWith ( AppName , StringComparison . OrdinalIgnoreCase ) )
283+ {
284+ message = message . Remove ( 0 , AppName . Length + 1 ) ;
285+ }
286+
287+ if ( message . StartsWith ( "Error" , StringComparison . OrdinalIgnoreCase ) )
288+ {
289+ _textBox . AppendText ( message ) ;
290+ if ( _notifyIcon != null )
291+ {
292+ _notifyIcon . Show ( AppTitle , "There is an error, see the Error Logging page for more information." ,
293+ NotificationType . Error , "" , TimeSpan . FromSeconds ( 5 ) ) ;
294+ }
295+ }
296+ else if ( message . StartsWith ( "Warn" , StringComparison . OrdinalIgnoreCase ) )
297+ {
298+ _textBox . AppendText ( message ) ;
299+ if ( _notifyIcon != null )
300+ {
301+ _notifyIcon . Show ( AppTitle , "There is a warning, see the Error Logging page for more information." ,
302+ NotificationType . Warning , "" , TimeSpan . FromSeconds ( 5 ) ) ;
303+ }
304+ }
305+ else
306+ {
307+ _textBox . AppendText ( message ) ;
308+ }
309+
310+ _textBox . ScrollToEnd ( ) ;
311+ }
312+
313+ protected override void Dispose ( bool disposing )
314+ {
315+ if ( _notifyIcon != null )
316+ {
317+ //_notifyIcon.Visible = false;
318+ _notifyIcon . Close ( ) ;
319+ }
320+
321+ _textBox = null ;
322+ _notifyIcon = null ;
323+
324+ base . Dispose ( disposing ) ;
226325 }
227326 }
228327}
0 commit comments