]> git.saurik.com Git - wxWidgets.git/blob - src/common/appcmn.cpp
added support for ellipsization and markup in wxStaticText (modified patch 1629946)
[wxWidgets.git] / src / common / appcmn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/appcmn.cpp
3 // Purpose: wxAppConsole and wxAppBase methods common to all platforms
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 18.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #if defined(__BORLANDC__)
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/app.h"
29 #include "wx/window.h"
30 #include "wx/bitmap.h"
31 #include "wx/log.h"
32 #include "wx/msgdlg.h"
33 #include "wx/confbase.h"
34 #include "wx/utils.h"
35 #endif
36
37 #include "wx/apptrait.h"
38 #include "wx/cmdline.h"
39 #include "wx/evtloop.h"
40 #include "wx/msgout.h"
41 #include "wx/thread.h"
42 #include "wx/vidmode.h"
43 #include "wx/ptr_scpd.h"
44
45 #ifdef __WXDEBUG__
46 #if wxUSE_STACKWALKER
47 #include "wx/stackwalk.h"
48 #endif // wxUSE_STACKWALKER
49 #endif // __WXDEBUG__
50
51 #if defined(__WXMSW__)
52 #include "wx/msw/private.h" // includes windows.h for LOGFONT
53 #endif
54
55 #if wxUSE_FONTMAP
56 #include "wx/fontmap.h"
57 #endif // wxUSE_FONTMAP
58
59 // DLL options compatibility check:
60 #include "wx/build.h"
61 WX_CHECK_BUILD_OPTIONS("wxCore")
62
63 WXDLLIMPEXP_DATA_CORE(wxList) wxPendingDelete;
64
65 // ----------------------------------------------------------------------------
66 // wxEventLoopPtr
67 // ----------------------------------------------------------------------------
68
69 // this defines wxEventLoopPtr
70 wxDEFINE_TIED_SCOPED_PTR_TYPE(wxEventLoop)
71
72 // ============================================================================
73 // wxAppBase implementation
74 // ============================================================================
75
76 // ----------------------------------------------------------------------------
77 // initialization
78 // ----------------------------------------------------------------------------
79
80 wxAppBase::wxAppBase()
81 {
82 m_topWindow = (wxWindow *)NULL;
83
84 m_useBestVisual = false;
85 m_forceTrueColour = false;
86
87 m_isActive = true;
88
89 m_mainLoop = NULL;
90
91 // We don't want to exit the app if the user code shows a dialog from its
92 // OnInit() -- but this is what would happen if we set m_exitOnFrameDelete
93 // to Yes initially as this dialog would be the last top level window.
94 // OTOH, if we set it to No initially we'll have to overwrite it with Yes
95 // when we enter our OnRun() because we do want the default behaviour from
96 // then on. But this would be a problem if the user code calls
97 // SetExitOnFrameDelete(false) from OnInit().
98 //
99 // So we use the special "Later" value which is such that
100 // GetExitOnFrameDelete() returns false for it but which we know we can
101 // safely (i.e. without losing the effect of the users SetExitOnFrameDelete
102 // call) overwrite in OnRun()
103 m_exitOnFrameDelete = Later;
104 }
105
106 bool wxAppBase::Initialize(int& argcOrig, wxChar **argvOrig)
107 {
108 if ( !wxAppConsole::Initialize(argcOrig, argvOrig) )
109 return false;
110
111 #if wxUSE_THREADS
112 wxPendingEventsLocker = new wxCriticalSection;
113 #endif
114
115 wxInitializeStockLists();
116
117 wxBitmap::InitStandardHandlers();
118
119 return true;
120 }
121
122 // ----------------------------------------------------------------------------
123 // cleanup
124 // ----------------------------------------------------------------------------
125
126 wxAppBase::~wxAppBase()
127 {
128 // this destructor is required for Darwin
129 }
130
131 void wxAppBase::CleanUp()
132 {
133 // clean up all the pending objects
134 DeletePendingObjects();
135
136 // and any remaining TLWs (they remove themselves from wxTopLevelWindows
137 // when destroyed, so iterate until none are left)
138 while ( !wxTopLevelWindows.empty() )
139 {
140 // do not use Destroy() here as it only puts the TLW in pending list
141 // but we want to delete them now
142 delete wxTopLevelWindows.GetFirst()->GetData();
143 }
144
145 // undo everything we did in Initialize() above
146 wxBitmap::CleanUpHandlers();
147
148 wxStockGDI::DeleteAll();
149
150 wxDeleteStockLists();
151
152 delete wxTheColourDatabase;
153 wxTheColourDatabase = NULL;
154
155 delete wxPendingEvents;
156 wxPendingEvents = NULL;
157
158 #if wxUSE_THREADS
159 delete wxPendingEventsLocker;
160 wxPendingEventsLocker = NULL;
161
162 #if wxUSE_VALIDATORS
163 // If we don't do the following, we get an apparent memory leak.
164 ((wxEvtHandler&) wxDefaultValidator).ClearEventLocker();
165 #endif // wxUSE_VALIDATORS
166 #endif // wxUSE_THREADS
167 }
168
169 // ----------------------------------------------------------------------------
170 // various accessors
171 // ----------------------------------------------------------------------------
172
173 wxWindow* wxAppBase::GetTopWindow() const
174 {
175 wxWindow* window = m_topWindow;
176 if (window == NULL && wxTopLevelWindows.GetCount() > 0)
177 window = wxTopLevelWindows.GetFirst()->GetData();
178 return window;
179 }
180
181 wxVideoMode wxAppBase::GetDisplayMode() const
182 {
183 return wxVideoMode();
184 }
185
186 wxLayoutDirection wxAppBase::GetLayoutDirection() const
187 {
188 #if wxUSE_INTL
189 const wxLocale *const locale = wxGetLocale();
190 if ( locale )
191 {
192 const wxLanguageInfo *const
193 info = wxLocale::GetLanguageInfo(locale->GetLanguage());
194
195 if ( info )
196 return info->LayoutDirection;
197 }
198 #endif // wxUSE_INTL
199
200 // we don't know
201 return wxLayout_Default;
202 }
203
204 #if wxUSE_CMDLINE_PARSER
205
206 // ----------------------------------------------------------------------------
207 // GUI-specific command line options handling
208 // ----------------------------------------------------------------------------
209
210 #define OPTION_THEME _T("theme")
211 #define OPTION_MODE _T("mode")
212
213 void wxAppBase::OnInitCmdLine(wxCmdLineParser& parser)
214 {
215 // first add the standard non GUI options
216 wxAppConsole::OnInitCmdLine(parser);
217
218 // the standard command line options
219 static const wxCmdLineEntryDesc cmdLineGUIDesc[] =
220 {
221 #ifdef __WXUNIVERSAL__
222 {
223 wxCMD_LINE_OPTION,
224 wxEmptyString,
225 OPTION_THEME,
226 gettext_noop("specify the theme to use"),
227 wxCMD_LINE_VAL_STRING,
228 0x0
229 },
230 #endif // __WXUNIVERSAL__
231
232 #if defined(__WXMGL__)
233 // VS: this is not specific to wxMGL, all fullscreen (framebuffer) ports
234 // should provide this option. That's why it is in common/appcmn.cpp
235 // and not mgl/app.cpp
236 {
237 wxCMD_LINE_OPTION,
238 wxEmptyString,
239 OPTION_MODE,
240 gettext_noop("specify display mode to use (e.g. 640x480-16)"),
241 wxCMD_LINE_VAL_STRING,
242 0x0
243 },
244 #endif // __WXMGL__
245
246 // terminator
247 {
248 wxCMD_LINE_NONE,
249 wxEmptyString,
250 wxEmptyString,
251 wxEmptyString,
252 wxCMD_LINE_VAL_NONE,
253 0x0
254 }
255 };
256
257 parser.SetDesc(cmdLineGUIDesc);
258 }
259
260 bool wxAppBase::OnCmdLineParsed(wxCmdLineParser& parser)
261 {
262 #ifdef __WXUNIVERSAL__
263 wxString themeName;
264 if ( parser.Found(OPTION_THEME, &themeName) )
265 {
266 wxTheme *theme = wxTheme::Create(themeName);
267 if ( !theme )
268 {
269 wxLogError(_("Unsupported theme '%s'."), themeName.c_str());
270 return false;
271 }
272
273 // Delete the defaultly created theme and set the new theme.
274 delete wxTheme::Get();
275 wxTheme::Set(theme);
276 }
277 #endif // __WXUNIVERSAL__
278
279 #if defined(__WXMGL__)
280 wxString modeDesc;
281 if ( parser.Found(OPTION_MODE, &modeDesc) )
282 {
283 unsigned w, h, bpp;
284 if ( wxSscanf(modeDesc.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3 )
285 {
286 wxLogError(_("Invalid display mode specification '%s'."), modeDesc.c_str());
287 return false;
288 }
289
290 if ( !SetDisplayMode(wxVideoMode(w, h, bpp)) )
291 return false;
292 }
293 #endif // __WXMGL__
294
295 return wxAppConsole::OnCmdLineParsed(parser);
296 }
297
298 #endif // wxUSE_CMDLINE_PARSER
299
300 // ----------------------------------------------------------------------------
301 // main event loop implementation
302 // ----------------------------------------------------------------------------
303
304 int wxAppBase::MainLoop()
305 {
306 wxEventLoopTiedPtr mainLoop(&m_mainLoop, new wxEventLoop);
307
308 return m_mainLoop->Run();
309 }
310
311 void wxAppBase::ExitMainLoop()
312 {
313 // we should exit from the main event loop, not just any currently active
314 // (e.g. modal dialog) event loop
315 if ( m_mainLoop && m_mainLoop->IsRunning() )
316 {
317 m_mainLoop->Exit(0);
318 }
319 }
320
321 bool wxAppBase::Pending()
322 {
323 // use the currently active message loop here, not m_mainLoop, because if
324 // we're showing a modal dialog (with its own event loop) currently the
325 // main event loop is not running anyhow
326 wxEventLoop * const loop = wxEventLoop::GetActive();
327
328 return loop && loop->Pending();
329 }
330
331 bool wxAppBase::Dispatch()
332 {
333 // see comment in Pending()
334 wxEventLoop * const loop = wxEventLoop::GetActive();
335
336 return loop && loop->Dispatch();
337 }
338
339 // ----------------------------------------------------------------------------
340 // OnXXX() hooks
341 // ----------------------------------------------------------------------------
342
343 bool wxAppBase::OnInitGui()
344 {
345 #ifdef __WXUNIVERSAL__
346 if ( !wxTheme::Get() && !wxTheme::CreateDefault() )
347 return false;
348 #endif // __WXUNIVERSAL__
349
350 return true;
351 }
352
353 int wxAppBase::OnRun()
354 {
355 // see the comment in ctor: if the initial value hasn't been changed, use
356 // the default Yes from now on
357 if ( m_exitOnFrameDelete == Later )
358 {
359 m_exitOnFrameDelete = Yes;
360 }
361 //else: it has been changed, assume the user knows what he is doing
362
363 return MainLoop();
364 }
365
366 int wxAppBase::OnExit()
367 {
368 #ifdef __WXUNIVERSAL__
369 delete wxTheme::Set(NULL);
370 #endif // __WXUNIVERSAL__
371
372 return wxAppConsole::OnExit();
373 }
374
375 void wxAppBase::Exit()
376 {
377 ExitMainLoop();
378 }
379
380 wxAppTraits *wxAppBase::CreateTraits()
381 {
382 return new wxGUIAppTraits;
383 }
384
385 // ----------------------------------------------------------------------------
386 // misc
387 // ----------------------------------------------------------------------------
388
389 void wxAppBase::SetActive(bool active, wxWindow * WXUNUSED(lastFocus))
390 {
391 if ( active == m_isActive )
392 return;
393
394 m_isActive = active;
395
396 wxActivateEvent event(wxEVT_ACTIVATE_APP, active);
397 event.SetEventObject(this);
398
399 (void)ProcessEvent(event);
400 }
401
402 // ----------------------------------------------------------------------------
403 // idle handling
404 // ----------------------------------------------------------------------------
405
406 void wxAppBase::DeletePendingObjects()
407 {
408 wxList::compatibility_iterator node = wxPendingDelete.GetFirst();
409 while (node)
410 {
411 wxObject *obj = node->GetData();
412
413 // remove it from the list first so that if we get back here somehow
414 // during the object deletion (e.g. wxYield called from its dtor) we
415 // wouldn't try to delete it the second time
416 if ( wxPendingDelete.Member(obj) )
417 wxPendingDelete.Erase(node);
418
419 delete obj;
420
421 // Deleting one object may have deleted other pending
422 // objects, so start from beginning of list again.
423 node = wxPendingDelete.GetFirst();
424 }
425 }
426
427 // Returns true if more time is needed.
428 bool wxAppBase::ProcessIdle()
429 {
430 wxIdleEvent event;
431 bool needMore = false;
432 wxWindowList::compatibility_iterator node = wxTopLevelWindows.GetFirst();
433 while (node)
434 {
435 wxWindow* win = node->GetData();
436 if (SendIdleEvents(win, event))
437 needMore = true;
438 node = node->GetNext();
439 }
440
441 event.SetEventObject(this);
442 (void) ProcessEvent(event);
443 if (event.MoreRequested())
444 needMore = true;
445
446 wxUpdateUIEvent::ResetUpdateTime();
447
448 return needMore;
449 }
450
451 // Send idle event to window and all subwindows
452 bool wxAppBase::SendIdleEvents(wxWindow* win, wxIdleEvent& event)
453 {
454 bool needMore = false;
455
456 win->OnInternalIdle();
457
458 if (wxIdleEvent::CanSend(win))
459 {
460 event.SetEventObject(win);
461 win->GetEventHandler()->ProcessEvent(event);
462
463 if (event.MoreRequested())
464 needMore = true;
465 }
466 wxWindowList::compatibility_iterator node = win->GetChildren().GetFirst();
467 while ( node )
468 {
469 wxWindow *child = node->GetData();
470 if (SendIdleEvents(child, event))
471 needMore = true;
472
473 node = node->GetNext();
474 }
475
476 return needMore;
477 }
478
479 void wxAppBase::OnIdle(wxIdleEvent& WXUNUSED(event))
480 {
481 // If there are pending events, we must process them: pending events
482 // are either events to the threads other than main or events posted
483 // with wxPostEvent() functions
484 // GRG: I have moved this here so that all pending events are processed
485 // before starting to delete any objects. This behaves better (in
486 // particular, wrt wxPostEvent) and is coherent with wxGTK's current
487 // behaviour. Changed Feb/2000 before 2.1.14
488 ProcessPendingEvents();
489
490 // 'Garbage' collection of windows deleted with Close().
491 DeletePendingObjects();
492
493 #if wxUSE_LOG
494 // flush the logged messages if any
495 wxLog::FlushActive();
496 #endif // wxUSE_LOG
497
498 }
499
500 // ----------------------------------------------------------------------------
501 // exceptions support
502 // ----------------------------------------------------------------------------
503
504 #if wxUSE_EXCEPTIONS
505
506 bool wxAppBase::OnExceptionInMainLoop()
507 {
508 throw;
509
510 // some compilers are too stupid to know that we never return after throw
511 #if defined(__DMC__) || (defined(_MSC_VER) && _MSC_VER < 1200)
512 return false;
513 #endif
514 }
515
516 #endif // wxUSE_EXCEPTIONS
517
518 // ----------------------------------------------------------------------------
519 // wxGUIAppTraitsBase
520 // ----------------------------------------------------------------------------
521
522 #if wxUSE_LOG
523
524 wxLog *wxGUIAppTraitsBase::CreateLogTarget()
525 {
526 #if wxUSE_LOGGUI
527 return new wxLogGui;
528 #else
529 // we must have something!
530 return new wxLogStderr;
531 #endif
532 }
533
534 #endif // wxUSE_LOG
535
536 wxMessageOutput *wxGUIAppTraitsBase::CreateMessageOutput()
537 {
538 // The standard way of printing help on command line arguments (app --help)
539 // is (according to common practice):
540 // - console apps: to stderr (on any platform)
541 // - GUI apps: stderr on Unix platforms (!)
542 // message box under Windows and others
543 #ifdef __UNIX__
544 return new wxMessageOutputStderr;
545 #else // !__UNIX__
546 // wxMessageOutputMessageBox doesn't work under Motif
547 #ifdef __WXMOTIF__
548 return new wxMessageOutputLog;
549 #else
550 return new wxMessageOutputMessageBox;
551 #endif
552 #endif // __UNIX__/!__UNIX__
553 }
554
555 #if wxUSE_FONTMAP
556
557 wxFontMapper *wxGUIAppTraitsBase::CreateFontMapper()
558 {
559 return new wxFontMapper;
560 }
561
562 #endif // wxUSE_FONTMAP
563
564 wxRendererNative *wxGUIAppTraitsBase::CreateRenderer()
565 {
566 // use the default native renderer by default
567 return NULL;
568 }
569
570 #ifdef __WXDEBUG__
571
572 bool wxGUIAppTraitsBase::ShowAssertDialog(const wxString& msg)
573 {
574 #if defined(__WXMSW__) || !wxUSE_MSGDLG
575 // under MSW we prefer to use the base class version using ::MessageBox()
576 // even if wxMessageBox() is available because it has less chances to
577 // double fault our app than our wxMessageBox()
578 return wxAppTraitsBase::ShowAssertDialog(msg);
579 #else // wxUSE_MSGDLG
580 wxString msgDlg = msg;
581
582 #if wxUSE_STACKWALKER
583 // on Unix stack frame generation may take some time, depending on the
584 // size of the executable mainly... warn the user that we are working
585 wxFprintf(stderr, wxT("[Debug] Generating a stack trace... please wait"));
586 fflush(stderr);
587
588 const wxString stackTrace = GetAssertStackTrace();
589 if ( !stackTrace.empty() )
590 msgDlg << _T("\n\nCall stack:\n") << stackTrace;
591 #endif // wxUSE_STACKWALKER
592
593 // this message is intentionally not translated -- it is for
594 // developpers only
595 msgDlg += wxT("\nDo you want to stop the program?\n")
596 wxT("You can also choose [Cancel] to suppress ")
597 wxT("further warnings.");
598
599 switch ( wxMessageBox(msgDlg, wxT("wxWidgets Debug Alert"),
600 wxYES_NO | wxCANCEL | wxICON_STOP ) )
601 {
602 case wxYES:
603 wxTrap();
604 break;
605
606 case wxCANCEL:
607 // no more asserts
608 return true;
609
610 //case wxNO: nothing to do
611 }
612
613 return false;
614 #endif // !wxUSE_MSGDLG/wxUSE_MSGDLG
615 }
616
617 #endif // __WXDEBUG__
618
619 bool wxGUIAppTraitsBase::HasStderr()
620 {
621 // we consider that under Unix stderr always goes somewhere, even if the
622 // user doesn't always see it under GUI desktops
623 #ifdef __UNIX__
624 return true;
625 #else
626 return false;
627 #endif
628 }
629
630 void wxGUIAppTraitsBase::ScheduleForDestroy(wxObject *object)
631 {
632 if ( !wxPendingDelete.Member(object) )
633 wxPendingDelete.Append(object);
634 }
635
636 void wxGUIAppTraitsBase::RemoveFromPendingDelete(wxObject *object)
637 {
638 wxPendingDelete.DeleteObject(object);
639 }
640
641 #if wxUSE_SOCKETS
642
643 #if defined(__WINDOWS__)
644 #include "wx/msw/gsockmsw.h"
645 #elif defined(__UNIX__) || defined(__DARWIN__) || defined(__OS2__)
646 #include "wx/unix/gsockunx.h"
647 #elif defined(__WXMAC__)
648 #include <MacHeaders.c>
649 #define OTUNIXERRORS 1
650 #include <OpenTransport.h>
651 #include <OpenTransportProviders.h>
652 #include <OpenTptInternet.h>
653
654 #include "wx/mac/gsockmac.h"
655 #else
656 #error "Must include correct GSocket header here"
657 #endif
658
659 GSocketGUIFunctionsTable* wxGUIAppTraitsBase::GetSocketGUIFunctionsTable()
660 {
661 #if defined(__WXMAC__) && !defined(__DARWIN__)
662 // NB: wxMac CFM does not have any GUI-specific functions in gsocket.c and
663 // so it doesn't need this table at all
664 return NULL;
665 #else // !__WXMAC__ || __DARWIN__
666 static GSocketGUIFunctionsTableConcrete table;
667 return &table;
668 #endif // !__WXMAC__ || __DARWIN__
669 }
670
671 #endif