]> git.saurik.com Git - wxWidgets.git/blob - src/gtk1/app.cpp
documented that wxHSCROLL disables wrapping; added comment to the source
[wxWidgets.git] / src / gtk1 / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "app.h"
12 #endif
13
14 #ifdef __VMS
15 // vms_jackets.h should for proper working be included before anything else
16 # include <vms_jackets.h>
17 #undef ConnectionNumber
18 #endif
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #include "wx/app.h"
24 #include "wx/gdicmn.h"
25 #include "wx/utils.h"
26 #include "wx/intl.h"
27 #include "wx/log.h"
28 #include "wx/memory.h"
29 #include "wx/font.h"
30 #include "wx/settings.h"
31 #include "wx/dialog.h"
32 #include "wx/msgdlg.h"
33 #include "wx/file.h"
34 #include "wx/filename.h"
35 #include "wx/module.h"
36 #include "wx/image.h"
37
38 #ifdef __WXUNIVERSAL__
39 #include "wx/univ/theme.h"
40 #include "wx/univ/renderer.h"
41 #endif
42
43 #if wxUSE_THREADS
44 #include "wx/thread.h"
45 #endif
46
47 #include <unistd.h>
48
49 #ifdef HAVE_POLL
50 #if defined(__VMS)
51 #include <poll.h>
52 #else
53 // bug in the OpenBSD headers: at least in 3.1 there is no extern "C"
54 // in neither poll.h nor sys/poll.h which results in link errors later
55 #ifdef __OPENBSD__
56 extern "C"
57 {
58 #endif
59
60 #include <sys/poll.h>
61
62 #ifdef __OPENBSD__
63 };
64 #endif
65 #endif // platform
66 #else // !HAVE_POLL
67 // we implement poll() ourselves using select() which is supposed exist in
68 // all modern Unices
69 #include <sys/types.h>
70 #include <sys/time.h>
71 #include <unistd.h>
72 #endif // HAVE_POLL/!HAVE_POLL
73
74 #include "wx/gtk/win_gtk.h"
75
76 #include <gtk/gtk.h>
77
78
79 //-----------------------------------------------------------------------------
80 // global data
81 //-----------------------------------------------------------------------------
82
83 bool g_mainThreadLocked = FALSE;
84 gint g_pendingTag = 0;
85
86 static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
87
88 //-----------------------------------------------------------------------------
89 // idle system
90 //-----------------------------------------------------------------------------
91
92 extern bool g_isIdle;
93
94 void wxapp_install_idle_handler();
95
96 //-----------------------------------------------------------------------------
97 // wxYield
98 //-----------------------------------------------------------------------------
99
100 // not static because used by textctrl.cpp
101 //
102 // MT-FIXME
103 bool wxIsInsideYield = FALSE;
104
105 bool wxApp::Yield(bool onlyIfNeeded)
106 {
107 if ( wxIsInsideYield )
108 {
109 if ( !onlyIfNeeded )
110 {
111 wxFAIL_MSG( wxT("wxYield called recursively" ) );
112 }
113
114 return FALSE;
115 }
116
117 #if wxUSE_THREADS
118 if ( !wxThread::IsMain() )
119 {
120 // can't call gtk_main_iteration() from other threads like this
121 return TRUE;
122 }
123 #endif // wxUSE_THREADS
124
125 wxIsInsideYield = TRUE;
126
127 if (!g_isIdle)
128 {
129 // We need to remove idle callbacks or the loop will
130 // never finish.
131 gtk_idle_remove( m_idleTag );
132 m_idleTag = 0;
133 g_isIdle = TRUE;
134 }
135
136 // disable log flushing from here because a call to wxYield() shouldn't
137 // normally result in message boxes popping up &c
138 wxLog::Suspend();
139
140 while (gtk_events_pending())
141 gtk_main_iteration();
142
143 // It's necessary to call ProcessIdle() to update the frames sizes which
144 // might have been changed (it also will update other things set from
145 // OnUpdateUI() which is a nice (and desired) side effect). But we
146 // call ProcessIdle() only once since this is not meant for longish
147 // background jobs (controlled by wxIdleEvent::RequestMore() and the
148 // return value of Processidle().
149 ProcessIdle();
150
151 // let the logs be flashed again
152 wxLog::Resume();
153
154 wxIsInsideYield = FALSE;
155
156 return TRUE;
157 }
158
159 //-----------------------------------------------------------------------------
160 // wxWakeUpIdle
161 //-----------------------------------------------------------------------------
162
163 void wxApp::WakeUpIdle()
164 {
165 #if wxUSE_THREADS
166 if (!wxThread::IsMain())
167 wxMutexGuiEnter();
168 #endif
169
170 if (g_isIdle)
171 wxapp_install_idle_handler();
172
173 #if wxUSE_THREADS
174 if (!wxThread::IsMain())
175 wxMutexGuiLeave();
176 #endif
177 }
178
179 //-----------------------------------------------------------------------------
180 // local functions
181 //-----------------------------------------------------------------------------
182
183 // the callback functions must be extern "C" to comply with GTK+ declarations
184 extern "C"
185 {
186
187 static gint wxapp_pending_callback( gpointer WXUNUSED(data) )
188 {
189 if (!wxTheApp) return TRUE;
190
191 // When getting called from GDK's time-out handler
192 // we are no longer within GDK's grab on the GUI
193 // thread so we must lock it here ourselves.
194 gdk_threads_enter();
195
196 // Sent idle event to all who request them.
197 wxTheApp->ProcessPendingEvents();
198
199 g_pendingTag = 0;
200
201 // Flush the logged messages if any.
202 #if wxUSE_LOG
203 wxLog::FlushActive();
204 #endif // wxUSE_LOG
205
206 // Release lock again
207 gdk_threads_leave();
208
209 // Return FALSE to indicate that no more idle events are
210 // to be sent (single shot instead of continuous stream)
211 return FALSE;
212 }
213
214 static gint wxapp_idle_callback( gpointer WXUNUSED(data) )
215 {
216 if (!wxTheApp)
217 return TRUE;
218
219 #ifdef __WXDEBUG__
220 // don't generate the idle events while the assert modal dialog is shown,
221 // this completely confuses the apps which don't expect to be reentered
222 // from some safely-looking functions
223 if ( wxTheApp->IsInAssert() )
224 {
225 // But repaint the assertion message if necessary
226 if (wxTopLevelWindows.GetCount() > 0)
227 {
228 wxWindow* win = (wxWindow*) wxTopLevelWindows.GetLast()->GetData();
229 #ifdef __WXGTK20__
230 if (win->IsKindOf(CLASSINFO(wxMessageDialog)))
231 #else
232 if (win->IsKindOf(CLASSINFO(wxGenericMessageDialog)))
233 #endif
234 win->OnInternalIdle();
235 }
236 return TRUE;
237 }
238 #endif // __WXDEBUG__
239
240 // When getting called from GDK's time-out handler
241 // we are no longer within GDK's grab on the GUI
242 // thread so we must lock it here ourselves.
243 gdk_threads_enter();
244
245 // Indicate that we are now in idle mode and event handlers
246 // will have to reinstall the idle handler again.
247 g_isIdle = TRUE;
248 wxTheApp->m_idleTag = 0;
249
250 // Send idle event to all who request them as long as
251 // no events have popped up in the event queue.
252 while (wxTheApp->ProcessIdle() && (gtk_events_pending() == 0))
253 ;
254
255 // Release lock again
256 gdk_threads_leave();
257
258 // Return FALSE to indicate that no more idle events are
259 // to be sent (single shot instead of continuous stream).
260 return FALSE;
261 }
262
263 #if wxUSE_THREADS
264
265 #ifdef HAVE_POLL
266 #define wxPoll poll
267 #define wxPollFd pollfd
268 #else // !HAVE_POLL
269
270 typedef GPollFD wxPollFd;
271
272 int wxPoll(wxPollFd *ufds, unsigned int nfds, int timeout)
273 {
274 // convert timeout from ms to struct timeval (s/us)
275 timeval tv_timeout;
276 tv_timeout.tv_sec = timeout/1000;
277 tv_timeout.tv_usec = (timeout%1000)*1000;
278
279 // remember the highest fd used here
280 int fdMax = -1;
281
282 // and fill the sets for select()
283 fd_set readfds;
284 fd_set writefds;
285 fd_set exceptfds;
286 FD_ZERO(&readfds);
287 FD_ZERO(&writefds);
288 FD_ZERO(&exceptfds);
289
290 unsigned int i;
291 for ( i = 0; i < nfds; i++ )
292 {
293 wxASSERT_MSG( ufds[i].fd < FD_SETSIZE, _T("fd out of range") );
294
295 if ( ufds[i].events & G_IO_IN )
296 FD_SET(ufds[i].fd, &readfds);
297
298 if ( ufds[i].events & G_IO_PRI )
299 FD_SET(ufds[i].fd, &exceptfds);
300
301 if ( ufds[i].events & G_IO_OUT )
302 FD_SET(ufds[i].fd, &writefds);
303
304 if ( ufds[i].fd > fdMax )
305 fdMax = ufds[i].fd;
306 }
307
308 fdMax++;
309 int res = select(fdMax, &readfds, &writefds, &exceptfds, &tv_timeout);
310
311 // translate the results back
312 for ( i = 0; i < nfds; i++ )
313 {
314 ufds[i].revents = 0;
315
316 if ( FD_ISSET(ufds[i].fd, &readfds ) )
317 ufds[i].revents |= G_IO_IN;
318
319 if ( FD_ISSET(ufds[i].fd, &exceptfds ) )
320 ufds[i].revents |= G_IO_PRI;
321
322 if ( FD_ISSET(ufds[i].fd, &writefds ) )
323 ufds[i].revents |= G_IO_OUT;
324 }
325
326 return res;
327 }
328
329 #endif // HAVE_POLL/!HAVE_POLL
330
331 static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
332 {
333 gdk_threads_enter();
334
335 wxMutexGuiLeave();
336 g_mainThreadLocked = TRUE;
337
338 // we rely on the fact that glib GPollFD struct is really just pollfd but
339 // I wonder how wise is this in the long term (VZ)
340 gint res = wxPoll( (wxPollFd *) ufds, nfds, timeout );
341
342 wxMutexGuiEnter();
343 g_mainThreadLocked = FALSE;
344
345 gdk_threads_leave();
346
347 return res;
348 }
349
350 #endif // wxUSE_THREADS
351
352 } // extern "C"
353
354 void wxapp_install_idle_handler()
355 {
356 // GD: this assert is raised when using the thread sample (which works)
357 // so the test is probably not so easy. Can widget callbacks be
358 // triggered from child threads and, if so, for which widgets?
359 // wxASSERT_MSG( wxThread::IsMain() || gs_WakeUpIdle, wxT("attempt to install idle handler from widget callback in child thread (should be exclusively from wxWakeUpIdle)") );
360
361 wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") );
362
363 g_isIdle = FALSE;
364
365 if (g_pendingTag == 0)
366 g_pendingTag = gtk_idle_add_priority( 900, wxapp_pending_callback, (gpointer) NULL );
367
368 // This routine gets called by all event handlers
369 // indicating that the idle is over. It may also
370 // get called from other thread for sending events
371 // to the main thread (and processing these in
372 // idle time). Very low priority.
373 wxTheApp->m_idleTag = gtk_idle_add_priority( 1000, wxapp_idle_callback, (gpointer) NULL );
374 }
375
376 //-----------------------------------------------------------------------------
377 // Access to the root window global
378 //-----------------------------------------------------------------------------
379
380 GtkWidget* wxGetRootWindow()
381 {
382 if (gs_RootWindow == NULL)
383 {
384 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
385 gtk_widget_realize( gs_RootWindow );
386 }
387 return gs_RootWindow;
388 }
389
390 //-----------------------------------------------------------------------------
391 // wxApp
392 //-----------------------------------------------------------------------------
393
394 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
395
396 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
397 EVT_IDLE(wxAppBase::OnIdle)
398 END_EVENT_TABLE()
399
400 wxApp::wxApp()
401 {
402 m_initialized = FALSE;
403 #ifdef __WXDEBUG__
404 m_isInAssert = FALSE;
405 #endif // __WXDEBUG__
406
407 m_idleTag = 0;
408 wxapp_install_idle_handler();
409
410 #if wxUSE_THREADS
411 g_main_set_poll_func( wxapp_poll_func );
412 #endif
413
414 m_colorCube = (unsigned char*) NULL;
415
416 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
417 m_glVisualInfo = (void *) NULL;
418 }
419
420 wxApp::~wxApp()
421 {
422 if (m_idleTag) gtk_idle_remove( m_idleTag );
423
424 if (m_colorCube) free(m_colorCube);
425 }
426
427 bool wxApp::OnInitGui()
428 {
429 if ( !wxAppBase::OnInitGui() )
430 return FALSE;
431
432 GdkVisual *visual = gdk_visual_get_system();
433
434 // if this is a wxGLApp (derived from wxApp), and we've already
435 // chosen a specific visual, then derive the GdkVisual from that
436 if (m_glVisualInfo != NULL)
437 {
438 #ifdef __WXGTK20__
439 // seems gtk_widget_set_default_visual no longer exists?
440 GdkVisual* vis = gtk_widget_get_default_visual();
441 #else
442 GdkVisual* vis = gdkx_visual_get(
443 ((XVisualInfo *) m_glVisualInfo) ->visualid );
444 gtk_widget_set_default_visual( vis );
445 #endif
446
447 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
448 gtk_widget_set_default_colormap( colormap );
449
450 visual = vis;
451 }
452
453 // On some machines, the default visual is just 256 colours, so
454 // we make sure we get the best. This can sometimes be wasteful.
455
456 else
457 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual))
458 {
459 #ifdef __WXGTK20__
460 /* seems gtk_widget_set_default_visual no longer exists? */
461 GdkVisual* vis = gtk_widget_get_default_visual();
462 #else
463 GdkVisual* vis = gdk_visual_get_best();
464 gtk_widget_set_default_visual( vis );
465 #endif
466
467 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
468 gtk_widget_set_default_colormap( colormap );
469
470 visual = vis;
471 }
472
473 // Nothing to do for 15, 16, 24, 32 bit displays
474 if (visual->depth > 8) return TRUE;
475
476 // initialize color cube for 8-bit color reduction dithering
477
478 GdkColormap *cmap = gtk_widget_get_default_colormap();
479
480 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
481
482 for (int r = 0; r < 32; r++)
483 {
484 for (int g = 0; g < 32; g++)
485 {
486 for (int b = 0; b < 32; b++)
487 {
488 int rr = (r << 3) | (r >> 2);
489 int gg = (g << 3) | (g >> 2);
490 int bb = (b << 3) | (b >> 2);
491
492 int index = -1;
493
494 GdkColor *colors = cmap->colors;
495 if (colors)
496 {
497 int max = 3 * 65536;
498
499 for (int i = 0; i < cmap->size; i++)
500 {
501 int rdiff = ((rr << 8) - colors[i].red);
502 int gdiff = ((gg << 8) - colors[i].green);
503 int bdiff = ((bb << 8) - colors[i].blue);
504 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
505 if (sum < max)
506 {
507 index = i; max = sum;
508 }
509 }
510 }
511 else
512 {
513 // assume 8-bit true or static colors. this really exists
514 GdkVisual* vis = gdk_colormap_get_visual( cmap );
515 index = (r >> (5 - vis->red_prec)) << vis->red_shift;
516 index |= (g >> (5 - vis->green_prec)) << vis->green_shift;
517 index |= (b >> (5 - vis->blue_prec)) << vis->blue_shift;
518 }
519 m_colorCube[ (r*1024) + (g*32) + b ] = index;
520 }
521 }
522 }
523
524 return TRUE;
525 }
526
527 GdkVisual *wxApp::GetGdkVisual()
528 {
529 GdkVisual *visual = NULL;
530
531 if (m_glVisualInfo)
532 visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid );
533 else
534 visual = gdk_window_get_visual( wxGetRootWindow()->window );
535
536 wxASSERT( visual );
537
538 return visual;
539 }
540
541 int wxApp::MainLoop()
542 {
543 gtk_main();
544 return 0;
545 }
546
547 void wxApp::Exit()
548 {
549 // VZ: no idea why is it different from ExitMainLoop() but this is what
550 // wxExit() used to do
551 gtk_main_quit();
552 }
553
554 void wxApp::ExitMainLoop()
555 {
556 if (gtk_main_level() > 0)
557 gtk_main_quit();
558 }
559
560 bool wxApp::Initialized()
561 {
562 return m_initialized;
563 }
564
565 bool wxApp::Pending()
566 {
567 return (gtk_events_pending() > 0);
568 }
569
570 void wxApp::Dispatch()
571 {
572 gtk_main_iteration();
573 }
574
575 bool wxApp::Initialize(int& argc, wxChar **argv)
576 {
577 #if wxUSE_THREADS
578 // GTK 1.2 up to version 1.2.3 has broken threads
579 if ((gtk_major_version == 1) &&
580 (gtk_minor_version == 2) &&
581 (gtk_micro_version < 4))
582 {
583 printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" );
584 }
585 else
586 {
587 g_thread_init(NULL);
588 }
589 #endif // wxUSE_THREADS
590
591 gtk_set_locale();
592
593 // We should have the wxUSE_WCHAR_T test on the _outside_
594 #if wxUSE_WCHAR_T
595 #if defined(__WXGTK20__)
596 // gtk+ 2.0 supports Unicode through UTF-8 strings
597 wxConvCurrent = &wxConvUTF8;
598 #else // GTK 1.x
599 if (!wxOKlibc())
600 wxConvCurrent = &wxConvLocal;
601 #endif
602 #else // !wxUSE_WCHAR_T
603 if (!wxOKlibc())
604 wxConvCurrent = (wxMBConv*) NULL;
605 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
606
607 #if wxUSE_UNICODE
608 // gtk_init() wants UTF-8, not wchar_t, so convert
609 int i;
610 char **argvGTK = new char *[argc + 1];
611 for ( i = 0; i < argc; i++ )
612 {
613 argvGTK[i] = wxStrdupA(wxConvUTF8.cWX2MB(argv[i]));
614 }
615
616 argvGTK[argc] = NULL;
617
618 int argcGTK = argc;
619 gtk_init( &argcGTK, &argvGTK );
620
621 if ( argcGTK != argc )
622 {
623 // we have to drop the parameters which were consumed by GTK+
624 for ( i = 0; i < argcGTK; i++ )
625 {
626 while ( strcmp(wxConvUTF8.cWX2MB(argv[i]), argvGTK[i]) != 0 )
627 {
628 memmove(argv + i, argv + i + 1, argc - i);
629 }
630 }
631
632 argc = argcGTK;
633 }
634 //else: gtk_init() didn't modify our parameters
635
636 // free our copy
637 for ( i = 0; i < argcGTK; i++ )
638 {
639 free(argvGTK[i]);
640 }
641
642 delete [] argvGTK;
643 #else // !wxUSE_UNICODE
644 // gtk_init() shouldn't actually change argv itself (just its contents) so
645 // it's ok to pass pointer to it
646 gtk_init( &argc, &argv );
647 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
648
649 // we can not enter threads before gtk_init is done
650 gdk_threads_enter();
651
652 if ( !wxAppBase::Initialize(argc, argv) )
653 {
654 gdk_threads_leave();
655
656 return false;
657 }
658
659 wxSetDetectableAutoRepeat( TRUE );
660
661 #if wxUSE_INTL
662 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
663 #endif
664
665 wxGetRootWindow();
666
667 return true;
668 }
669
670 void wxApp::CleanUp()
671 {
672 gdk_threads_leave();
673
674 wxAppBase::CleanUp();
675 }
676
677 #ifdef __WXDEBUG__
678
679 void wxApp::OnAssert(const wxChar *file, int line, const wxChar* cond, const wxChar *msg)
680 {
681 m_isInAssert = TRUE;
682
683 wxAppBase::OnAssert(file, line, cond, msg);
684
685 m_isInAssert = FALSE;
686 }
687
688 #endif // __WXDEBUG__
689