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>
@@ -56,18 +60,26 @@ public List<SVGItem> Items
5660 get { return m_items ; }
5761 }
5862
63+ private bool _isShown ;
64+ private TextBoxTraceListener _listener ;
65+
5966 public MainWindow ( )
6067 {
6168 InitializeComponent ( ) ;
6269
6370 this . Loaded += OnMainWindowLoaded ;
71+ this . Closing += OnMainWindowClosing ;
6472 }
6573
66- private void OnMainWindowLoaded ( object sender , RoutedEventArgs e )
74+ protected override void OnContentRendered ( EventArgs e )
6775 {
68- txtLogger . Document . Blocks . Clear ( ) ;
69- txtLogger . SetValue ( Block . LineHeightProperty , 1.0 ) ;
70- Trace . Listeners . Add ( new TextBoxTraceListener ( txtLogger ) ) ;
76+ base . OnContentRendered ( e ) ;
77+
78+ if ( _isShown )
79+ {
80+ return ;
81+ }
82+ _isShown = true ;
7183
7284 // default path
7385 string appPath = Process . GetCurrentProcess ( ) . MainModule . FileName ;
@@ -85,14 +97,50 @@ private void OnMainWindowLoaded(object sender, RoutedEventArgs e)
8597 tabPages . SelectedIndex = 0 ;
8698 }
8799
100+ private void OnMainWindowLoaded ( object sender , RoutedEventArgs e )
101+ {
102+ txtLogger . Document . Blocks . Clear ( ) ;
103+ txtLogger . SetValue ( Block . LineHeightProperty , 1.0 ) ;
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+ }
122+ }
123+
88124 void ListFiles ( )
89125 {
90126 string path = this . SvgPath ;
91127 if ( Directory . Exists ( path ) == false )
92128 {
93129 return ;
94130 }
131+
132+ if ( m_items . Count != 0 )
133+ {
134+ m_items . Clear ( ) ;
135+ m_filelist . Items . Refresh ( ) ;
136+ }
137+
95138 string [ ] files = Directory . GetFiles ( path , "*.svg" ) ;
139+ if ( files == null || files . Length == 0 )
140+ {
141+ return ;
142+ }
143+
96144 foreach ( string file in files )
97145 m_items . Add ( new SVGItem ( file ) ) ;
98146
@@ -119,7 +167,15 @@ private void OnTabControlSelectionChanged(object sender, SelectionChangedEventAr
119167
120168 private void OnBrowseButtonClick ( object sender , RoutedEventArgs e )
121169 {
122- 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+ }
123179 }
124180 }
125181
@@ -165,7 +221,7 @@ void EnsureLoaded()
165221 return ;
166222 }
167223 _isLogged = true ;
168- Trace . WriteLine ( "Exception Loading: " + this . FullPath ) ;
224+ Trace . TraceError ( "Exception Loading: " + this . FullPath ) ;
169225 Trace . WriteLine ( ex . ToString ( ) ) ;
170226 Trace . WriteLine ( string . Empty ) ;
171227 }
@@ -174,11 +230,22 @@ void EnsureLoaded()
174230
175231 public class TextBoxTraceListener : TraceListener
176232 {
233+ private delegate void AppendTextDelegate ( string message ) ;
234+
235+ private const string AppTitle = "ClipArtViewer" ;
236+ private const string AppName = "ClipArtViewer.exe" ;
237+
177238 private RichTextBox _textBox ;
239+ private NotificationManager _notifyIcon ;
240+
241+ private AppendTextDelegate _appendText ;
178242
179243 public TextBoxTraceListener ( RichTextBox textBox )
180244 {
181- _textBox = textBox ;
245+ _textBox = textBox ;
246+ _notifyIcon = new NotificationManager ( ) ;
247+
248+ _appendText = new AppendTextDelegate ( AppendText ) ;
182249 }
183250
184251 public override void Write ( string message )
@@ -189,27 +256,72 @@ public override void Write(string message)
189256 }
190257 else
191258 {
192- _textBox . Dispatcher . Invoke ( new AppendTextDelegate ( AppendText ) , message ) ;
259+ _textBox . Dispatcher . Invoke ( _appendText , message ) ;
193260 }
194261 }
195262
196263 public override void WriteLine ( string message )
197264 {
198265 if ( _textBox . Dispatcher . CheckAccess ( ) )
199266 {
200- AppendText ( message ) ;
267+ AppendText ( message + Environment . NewLine ) ;
201268 }
202269 else
203270 {
204- _textBox . Dispatcher . Invoke ( new AppendTextDelegate ( AppendText ) , message ) ;
271+ _textBox . Dispatcher . Invoke ( _appendText , message + Environment . NewLine ) ;
205272 }
206273 }
207274
208- private delegate void AppendTextDelegate ( string message ) ;
209-
210275 private void AppendText ( string message )
211276 {
212- _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 ) ;
213325 }
214326 }
215327}
0 commit comments