-
Notifications
You must be signed in to change notification settings - Fork 234
/
qapplication_mac.mm
3174 lines (2903 loc) · 125 KB
/
qapplication_mac.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
/****************************************************************************
**
** Copyright (c) 2007-2008, Apple, Inc.
**
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
**
** * Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer.
**
** * Redistributions in binary form must reproduce the above copyright notice,
** this list of conditions and the following disclaimer in the documentation
** and/or other materials provided with the distribution.
**
** * Neither the name of Apple, Inc. nor the names of its contributors
** may be used to endorse or promote products derived from this software
** without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
****************************************************************************/
#include <Cocoa/Cocoa.h>
#include "qapplication.h"
#include "qbitarray.h"
#include "qclipboard.h"
#include "qcursor.h"
#include "qdatastream.h"
#include "qdatetime.h"
#include "qdesktopwidget.h"
#include "qdockwidget.h"
#include "qevent.h"
#include "qhash.h"
#include "qlayout.h"
#include "qmenubar.h"
#include "qmessagebox.h"
#include "qmime.h"
#include "qpixmapcache.h"
#include "qpointer.h"
#include "qsessionmanager.h"
#include "qsettings.h"
#include "qsocketnotifier.h"
#include "qstyle.h"
#include "qstylefactory.h"
#include "qtextcodec.h"
#include "qtoolbar.h"
#include "qvariant.h"
#include "qwidget.h"
#include "qcolormap.h"
#include "qdir.h"
#include "qdebug.h"
#include "qtimer.h"
#include "qurl.h"
#include "private/qmacinputcontext_p.h"
#include "private/qpaintengine_mac_p.h"
#include "private/qcursor_p.h"
#include "private/qapplication_p.h"
#include "private/qcolor_p.h"
#include "private/qwidget_p.h"
#include "private/qkeymapper_p.h"
#include "private/qeventdispatcher_mac_p.h"
#include "private/qeventdispatcher_unix_p.h"
#include <private/qcocoamenuloader_mac_p.h>
#include <private/qcocoaapplication_mac_p.h>
#include <private/qcocoaapplicationdelegate_mac_p.h>
#include <private/qt_cocoa_helpers_mac_p.h>
#include <private/qcocoawindow_mac_p.h>
#include <private/qpixmap_mac_p.h>
#include <private/qdesktopwidget_mac_p.h>
#include <private/qeventdispatcher_mac_p.h>
#include <qvarlengtharray.h>
#ifndef QT_NO_ACCESSIBILITY
# include "qaccessible.h"
#endif
#ifndef QT_NO_THREAD
# include "qmutex.h"
#endif
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <sys/select.h>
/*****************************************************************************
QApplication debug facilities
*****************************************************************************/
//#define DEBUG_EVENTS //like EventDebug but more specific to Qt
//#define DEBUG_DROPPED_EVENTS
//#define DEBUG_MOUSE_MAPS
//#define DEBUG_MODAL_EVENTS
//#define DEBUG_PLATFORM_SETTINGS
#define QMAC_SPEAK_TO_ME
#ifdef QMAC_SPEAK_TO_ME
#include "qregexp.h"
#endif
#ifndef kThemeBrushAlternatePrimaryHighlightColor
#define kThemeBrushAlternatePrimaryHighlightColor -5
#endif
#define kCMDeviceUnregisteredNotification CFSTR("CMDeviceUnregisteredNotification")
#define kCMDefaultDeviceNotification CFSTR("CMDefaultDeviceNotification")
#define kCMDeviceProfilesNotification CFSTR("CMDeviceProfilesNotification")
#define kCMDefaultDeviceProfileNotification CFSTR("CMDefaultDeviceProfileNotification")
QT_BEGIN_NAMESPACE
//for qt_mac.h
QPaintDevice *qt_mac_safe_pdev = 0;
QList<QMacWindowChangeEvent*> *QMacWindowChangeEvent::change_events = 0;
QPointer<QWidget> topLevelAt_cache = 0;
/*****************************************************************************
Internal variables and functions
*****************************************************************************/
static struct {
bool use_qt_time_limit;
QPointer<QWidget> last_widget;
int last_x, last_y;
int last_modifiers, last_button;
EventTime last_time;
} qt_mac_dblclick = { false, 0, -1, -1, 0, 0, -2 };
static bool app_do_modal = false; // modal mode
extern QWidgetList *qt_modal_stack; // stack of modal widgets
extern bool qt_tab_all_widgets; // from qapplication.cpp
bool qt_mac_app_fullscreen = false;
bool qt_scrollbar_jump_to_pos = false;
static bool qt_mac_collapse_on_dblclick = true;
extern int qt_antialiasing_threshold; // from qapplication.cpp
QWidget * qt_button_down; // widget got last button-down
QPointer<QWidget> qt_last_mouse_receiver;
#ifndef QT_MAC_USE_COCOA
static bool qt_button_down_in_content; // whether the button_down was in the content area.
static bool qt_mac_previous_press_in_popup_mode = false;
static bool qt_mac_no_click_through_mode = false;
static int tablet_button_state = 0;
#endif
#if defined(QT_DEBUG)
static bool appNoGrab = false; // mouse/keyboard grabbing
#endif
#ifndef QT_MAC_USE_COCOA
static EventHandlerRef app_proc_handler = 0;
static EventHandlerUPP app_proc_handlerUPP = 0;
#endif
static AEEventHandlerUPP app_proc_ae_handlerUPP = NULL;
static EventHandlerRef tablet_proximity_handler = 0;
static EventHandlerUPP tablet_proximity_UPP = 0;
bool QApplicationPrivate::native_modal_dialog_active;
Q_GUI_EXPORT bool qt_applefontsmoothing_enabled;
/*****************************************************************************
External functions
*****************************************************************************/
extern void qt_mac_beep(); //qsound_mac.mm
extern Qt::KeyboardModifiers qt_mac_get_modifiers(int keys); //qkeymapper_mac.cpp
extern bool qt_mac_can_clickThrough(const QWidget *); //qwidget_mac.cpp
extern bool qt_mac_is_macdrawer(const QWidget *); //qwidget_mac.cpp
extern OSWindowRef qt_mac_window_for(const QWidget*); //qwidget_mac.cpp
extern QWidget *qt_mac_find_window(OSWindowRef); //qwidget_mac.cpp
extern void qt_mac_set_cursor(const QCursor *); //qcursor_mac.cpp
extern bool qt_mac_is_macsheet(const QWidget *); //qwidget_mac.cpp
extern void qt_mac_command_set_enabled(MenuRef, UInt32, bool); //qmenu_mac.cpp
extern bool qt_sendSpontaneousEvent(QObject *obj, QEvent *event); // qapplication.cpp
extern void qt_mac_update_cursor(); // qcursor_mac.mm
// Forward Decls
void onApplicationWindowChangedActivation( QWidget*widget, bool activated );
void onApplicationChangedActivation( bool activated );
void qt_mac_loadMenuNib(QT_MANGLE_NAMESPACE(QCocoaMenuLoader) *qtMenuLoader)
{
// Create qt_menu.nib dir in temp.
QDir temp = QDir::temp();
temp.mkdir("qt_menu.nib");
QString nibDir = temp.canonicalPath() + QLatin1String("/") + QLatin1String("qt_menu.nib/");
if (!QDir(nibDir).exists()) {
qWarning("qt_mac_loadMenuNib: could not create nib directory in temp");
return;
}
// Copy nib files from resources to temp.
QDir nibResource(":/trolltech/mac/qt_menu.nib/");
if (!nibResource.exists()) {
qWarning("qt_mac_loadMenuNib: could not load nib from resources");
return;
}
foreach (const QFileInfo &file, nibResource.entryInfoList())
QFile::copy(file.absoluteFilePath(), nibDir + QLatin1String("/") + file.fileName());
// Load and instantiate nib file from temp
NSURL *nibUrl = [NSURL fileURLWithPath : reinterpret_cast<const NSString *>(QCFString::toCFStringRef(nibDir))];
NSNib *nib = [[NSNib alloc] initWithContentsOfURL : nibUrl];
[nib autorelease];
if (!nib) {
qWarning("qt_mac_loadMenuNib: could not load nib from temp");
return;
}
bool ok = [nib instantiateNibWithOwner : qtMenuLoader topLevelObjects : nil];
if (!ok)
qWarning("qt_mac_loadMenuNib: could not instantiate nib");
}
static void qt_mac_read_fontsmoothing_settings()
{
qt_applefontsmoothing_enabled = true;
int w = 10, h = 10;
QImage image(w, h, QImage::Format_RGB32);
image.fill(0xffffffff);
QPainter p(&image);
p.drawText(0, h, "X\\");
p.end();
const int *bits = (const int *) ((const QImage &) image).bits();
int bpl = image.bytesPerLine() / 4;
for (int y=0; y<w; ++y) {
for (int x=0; x<h; ++x) {
int r = qRed(bits[x]);
int g = qGreen(bits[x]);
int b = qBlue(bits[x]);
if (r != g || r != b) {
qt_applefontsmoothing_enabled = true;
return;
}
}
bits += bpl;
}
qt_applefontsmoothing_enabled = false;
}
Q_GUI_EXPORT bool qt_mac_execute_apple_script(const char *script, long script_len, AEDesc *ret) {
OSStatus err;
AEDesc scriptTextDesc;
ComponentInstance theComponent = 0;
OSAID scriptID = kOSANullScript, resultID = kOSANullScript;
// set up locals to a known state
AECreateDesc(typeNull, 0, 0, &scriptTextDesc);
scriptID = kOSANullScript;
resultID = kOSANullScript;
// open the scripting component
theComponent = OpenDefaultComponent(kOSAComponentType, typeAppleScript);
if (!theComponent) {
err = paramErr;
goto bail;
}
// put the script text into an aedesc
err = AECreateDesc(typeUTF8Text, script, script_len, &scriptTextDesc);
if (err != noErr)
goto bail;
// compile the script
err = OSACompile(theComponent, &scriptTextDesc, kOSAModeNull, &scriptID);
if (err != noErr)
goto bail;
// run the script
err = OSAExecute(theComponent, scriptID, kOSANullScript, kOSAModeNull, &resultID);
// collect the results - if any
if (ret) {
AECreateDesc(typeNull, 0, 0, ret);
if (err == errOSAScriptError)
OSAScriptError(theComponent, kOSAErrorMessage, typeChar, ret);
else if (err == noErr && resultID != kOSANullScript)
OSADisplay(theComponent, resultID, typeChar, kOSAModeNull, ret);
}
bail:
AEDisposeDesc(&scriptTextDesc);
if (scriptID != kOSANullScript)
OSADispose(theComponent, scriptID);
if (resultID != kOSANullScript)
OSADispose(theComponent, resultID);
if (theComponent)
CloseComponent(theComponent);
return err == noErr;
}
Q_GUI_EXPORT bool qt_mac_execute_apple_script(const char *script, AEDesc *ret)
{
return qt_mac_execute_apple_script(script, qstrlen(script), ret);
}
Q_GUI_EXPORT bool qt_mac_execute_apple_script(const QString &script, AEDesc *ret)
{
const QByteArray l = script.toUtf8(); return qt_mac_execute_apple_script(l.constData(), l.size(), ret);
}
/* Resolution change magic */
void qt_mac_display_change_callbk(CGDirectDisplayID, CGDisplayChangeSummaryFlags flags, void *)
{
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
const bool resized = flags & kCGDisplayDesktopShapeChangedFlag;
#else
Q_UNUSED(flags);
const bool resized = true;
#endif
if (resized && qApp) {
if (QDesktopWidget *dw = qApp->desktop()) {
QResizeEvent *re = new QResizeEvent(dw->size(), dw->size());
QApplication::postEvent(dw, re);
QCoreGraphicsPaintEngine::cleanUpMacColorSpaces();
}
}
}
#ifdef DEBUG_PLATFORM_SETTINGS
static void qt_mac_debug_palette(const QPalette &pal, const QPalette &pal2, const QString &where)
{
const char *const groups[] = {"Active", "Disabled", "Inactive" };
const char *const roles[] = { "WindowText", "Button", "Light", "Midlight", "Dark", "Mid",
"Text", "BrightText", "ButtonText", "Base", "Window", "Shadow",
"Highlight", "HighlightedText", "Link", "LinkVisited" };
if (!where.isNull())
qDebug("qt-internal: %s", where.toLatin1().constData());
for(int grp = 0; grp < QPalette::NColorGroups; grp++) {
for(int role = 0; role < QPalette::NColorRoles; role++) {
QBrush b = pal.brush((QPalette::ColorGroup)grp, (QPalette::ColorRole)role);
QPixmap pm = b.texture();
qDebug(" %s::%s %d::%d::%d [%p]%s", groups[grp], roles[role], b.color().red(),
b.color().green(), b.color().blue(), pm.isNull() ? 0 : &pm,
pal2.brush((QPalette::ColorGroup)grp, (QPalette::ColorRole)role) != b ? " (*)" : "");
}
}
}
#else
#define qt_mac_debug_palette(x, y, z)
#endif
//raise a notification
#ifndef QT_MAC_USE_COCOA
static NMRec qt_mac_notification = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
#endif
void qt_mac_send_notification()
{
#ifndef QT_MAC_USE_COCOA
//send it
qt_mac_notification.nmMark = 1; //non-zero magic number
qt_mac_notification.qType = nmType;
NMInstall(&qt_mac_notification);
#else
QMacCocoaAutoReleasePool pool;
[[NSApplication sharedApplication] requestUserAttention:NSInformationalRequest];
#endif
}
void qt_mac_cancel_notification()
{
#ifndef QT_MAC_USE_COCOA
NMRemove(&qt_mac_notification);
#else
QMacCocoaAutoReleasePool pool;
[[NSApplication sharedApplication] cancelUserAttentionRequest:NSInformationalRequest];
#endif
}
#ifndef QT_MAC_USE_COCOA
//find widget (and part) at a given point
static short qt_mac_window_at(int x, int y, QWidget **w=0)
{
Point p;
p.h = x;
p.v = y;
OSWindowRef wp;
WindowPartCode wpc;
OSStatus err = FindWindowOfClass(&p, kAllWindowClasses, &wp, &wpc);
if(err != noErr) {
if(w)
(*w) = 0;
return wpc;
}
if(w) {
if(wp) {
*w = qt_mac_find_window(wp);
#if 0
if(!*w)
qWarning("QApplication: qt_mac_window_at: Couldn't find %d",(int)wp);
#endif
} else {
*w = 0;
}
}
return wpc;
}
#endif
void qt_mac_set_app_icon(const QPixmap &pixmap)
{
#ifndef QT_MAC_USE_COCOA
if(pixmap.isNull()) {
RestoreApplicationDockTileImage();
} else {
CGImageRef img = (CGImageRef)pixmap.macCGHandle();
SetApplicationDockTileImage(img);
CGImageRelease(img);
}
#else
QMacCocoaAutoReleasePool pool;
NSImage *image = NULL;
if (pixmap.isNull()) {
// Get Application icon from bundle
image = [[NSImage imageNamed:@"NSApplicationIcon"] retain]; // released below
} else {
image = static_cast<NSImage *>(qt_mac_create_nsimage(pixmap));
}
[NSApp setApplicationIconImage:image];
[image release];
#endif
}
Q_GUI_EXPORT void qt_mac_set_press_and_hold_context(bool b)
{
Q_UNUSED(b);
qWarning("qt_mac_set_press_and_hold_context: This functionality is no longer available");
}
bool qt_nograb() // application no-grab option
{
#if defined(QT_DEBUG)
return appNoGrab;
#else
return false;
#endif
}
void qt_mac_update_os_settings()
{
if (!qApp)
return;
if (!QApplication::startingUp()) {
static bool needToPolish = true;
if (needToPolish) {
QApplication::style()->polish(qApp);
needToPolish = false;
}
}
//focus mode
/* First worked as of 10.2.3 */
QSettings appleSettings(QLatin1String("apple.com"));
QVariant appleValue = appleSettings.value(QLatin1String("AppleKeyboardUIMode"), 0);
qt_tab_all_widgets = (appleValue.toInt() & 0x2);
//paging mode
/* First worked as of 10.2.3 */
appleValue = appleSettings.value(QLatin1String("AppleScrollerPagingBehavior"), false);
qt_scrollbar_jump_to_pos = appleValue.toBool();
//collapse
/* First worked as of 10.3.3 */
appleValue = appleSettings.value(QLatin1String("AppleMiniaturizeOnDoubleClick"), true);
qt_mac_collapse_on_dblclick = appleValue.toBool();
// Anti-aliasing threshold
appleValue = appleSettings.value(QLatin1String("AppleAntiAliasingThreshold"));
if (appleValue.isValid())
qt_antialiasing_threshold = appleValue.toInt();
#ifdef DEBUG_PLATFORM_SETTINGS
qDebug("qt_mac_update_os_settings *********************************************************************");
#endif
{ // setup the global palette
QColor qc;
(void) QApplication::style(); // trigger creation of application style and system palettes
QPalette pal = *QApplicationPrivate::sys_pal;
pal.setBrush( QPalette::Active, QPalette::Highlight, qcolorForTheme(kThemeBrushPrimaryHighlightColor) );
pal.setBrush( QPalette::Inactive, QPalette::Highlight, qcolorForTheme(kThemeBrushSecondaryHighlightColor) );
pal.setBrush( QPalette::Disabled, QPalette::Highlight, qcolorForTheme(kThemeBrushSecondaryHighlightColor) );
pal.setBrush( QPalette::Active, QPalette::Shadow, qcolorForTheme(kThemeBrushButtonActiveDarkShadow) );
pal.setBrush( QPalette::Inactive, QPalette::Shadow, qcolorForTheme(kThemeBrushButtonInactiveDarkShadow) );
pal.setBrush( QPalette::Disabled, QPalette::Shadow, qcolorForTheme(kThemeBrushButtonInactiveDarkShadow) );
qc = qcolorForThemeTextColor(kThemeTextColorDialogActive);
pal.setColor(QPalette::Active, QPalette::Text, qc);
pal.setColor(QPalette::Active, QPalette::WindowText, qc);
pal.setColor(QPalette::Active, QPalette::HighlightedText, qc);
qc = qcolorForThemeTextColor(kThemeTextColorDialogInactive);
pal.setColor(QPalette::Inactive, QPalette::Text, qc);
pal.setColor(QPalette::Inactive, QPalette::WindowText, qc);
pal.setColor(QPalette::Inactive, QPalette::HighlightedText, qc);
pal.setColor(QPalette::Disabled, QPalette::Text, qc);
pal.setColor(QPalette::Disabled, QPalette::WindowText, qc);
pal.setColor(QPalette::Disabled, QPalette::HighlightedText, qc);
pal.setBrush(QPalette::ToolTipBase, QColor(255, 255, 199));
if (!QApplicationPrivate::sys_pal || *QApplicationPrivate::sys_pal != pal) {
QApplicationPrivate::setSystemPalette(pal);
QApplication::setPalette(pal);
}
#ifdef DEBUG_PLATFORM_SETTINGS
qt_mac_debug_palette(pal, QApplication::palette(), "Global Palette");
#endif
}
QFont fnt = qfontForThemeFont(kThemeApplicationFont);
#ifdef DEBUG_PLATFORM_SETTINGS
qDebug("qt-internal: Font for Application [%s::%d::%d::%d]",
fnt.family().toLatin1().constData(), fnt.pointSize(), fnt.bold(), fnt.italic());
#endif
if (!QApplicationPrivate::sys_font || *QApplicationPrivate::sys_font != fnt)
QApplicationPrivate::setSystemFont(fnt);
{ //setup the fonts
struct FontMap {
FontMap(const char *qc, short fk) : qt_class(qc), font_key(fk) { }
const char *const qt_class;
short font_key;
} mac_widget_fonts[] = {
FontMap("QPushButton", kThemePushButtonFont),
FontMap("QListView", kThemeViewsFont),
FontMap("QListBox", kThemeViewsFont),
FontMap("QTitleBar", kThemeWindowTitleFont),
FontMap("QMenuBar", kThemeMenuTitleFont),
FontMap("QMenu", kThemeMenuItemFont),
FontMap("QComboMenuItem", kThemeSystemFont),
FontMap("QHeaderView", kThemeSmallSystemFont),
FontMap("Q3Header", kThemeSmallSystemFont),
FontMap("QTipLabel", kThemeSmallSystemFont),
FontMap("QLabel", kThemeSystemFont),
FontMap("QToolButton", kThemeSmallSystemFont),
FontMap("QMenuItem", kThemeMenuItemFont), // It doesn't exist, but its unique.
FontMap("QComboLineEdit", kThemeViewsFont), // It doesn't exist, but its unique.
FontMap("QSmallFont", kThemeSmallSystemFont), // It doesn't exist, but its unique.
FontMap("QMiniFont", kThemeMiniSystemFont), // It doesn't exist, but its unique.
FontMap(0, 0) };
for(int i = 0; mac_widget_fonts[i].qt_class; i++) {
QFont fnt = qfontForThemeFont(mac_widget_fonts[i].font_key);
bool set_font = true;
FontHash *hash = qt_app_fonts_hash();
if (!hash->isEmpty()) {
FontHash::const_iterator it
= hash->constFind(mac_widget_fonts[i].qt_class);
if (it != hash->constEnd())
set_font = (fnt != *it);
}
if (set_font) {
QApplication::setFont(fnt, mac_widget_fonts[i].qt_class);
#ifdef DEBUG_PLATFORM_SETTINGS
qDebug("qt-internal: Font for %s [%s::%d::%d::%d]", mac_widget_fonts[i].qt_class,
fnt.family().toLatin1().constData(), fnt.pointSize(), fnt.bold(), fnt.italic());
#endif
}
}
}
QApplicationPrivate::initializeWidgetPaletteHash();
#ifdef DEBUG_PLATFORM_SETTINGS
qDebug("qt_mac_update_os_settings END !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
#endif
}
void QApplicationPrivate::initializeWidgetPaletteHash()
{
{ //setup the palette
struct PaletteMap {
inline PaletteMap(const char *qc, ThemeBrush a, ThemeBrush i) :
qt_class(qc), active(a), inactive(i) { }
const char *const qt_class;
ThemeBrush active, inactive;
} mac_widget_colors[] = {
PaletteMap("QToolButton", kThemeTextColorBevelButtonActive, kThemeTextColorBevelButtonInactive),
PaletteMap("QAbstractButton", kThemeTextColorPushButtonActive, kThemeTextColorPushButtonInactive),
PaletteMap("QHeaderView", kThemeTextColorPushButtonActive, kThemeTextColorPushButtonInactive),
PaletteMap("Q3Header", kThemeTextColorPushButtonActive, kThemeTextColorPushButtonInactive),
PaletteMap("QComboBox", kThemeTextColorPopupButtonActive, kThemeTextColorPopupButtonInactive),
PaletteMap("QAbstractItemView", kThemeTextColorListView, kThemeTextColorDialogInactive),
PaletteMap("QMessageBoxLabel", kThemeTextColorAlertActive, kThemeTextColorAlertInactive),
PaletteMap("QTabBar", kThemeTextColorTabFrontActive, kThemeTextColorTabFrontInactive),
PaletteMap("QLabel", kThemeTextColorPlacardActive, kThemeTextColorPlacardInactive),
PaletteMap("QGroupBox", kThemeTextColorPlacardActive, kThemeTextColorPlacardInactive),
PaletteMap("QMenu", kThemeTextColorPopupLabelActive, kThemeTextColorPopupLabelInactive),
PaletteMap("QTextEdit", 0, 0),
PaletteMap("QTextControl", 0, 0),
PaletteMap("QLineEdit", 0, 0),
PaletteMap(0, 0, 0) };
QColor qc;
for(int i = 0; mac_widget_colors[i].qt_class; i++) {
QPalette pal;
if (mac_widget_colors[i].active != 0) {
qc = qcolorForThemeTextColor(mac_widget_colors[i].active);
pal.setColor(QPalette::Active, QPalette::Text, qc);
pal.setColor(QPalette::Active, QPalette::WindowText, qc);
pal.setColor(QPalette::Active, QPalette::HighlightedText, qc);
qc = qcolorForThemeTextColor(mac_widget_colors[i].inactive);
pal.setColor(QPalette::Inactive, QPalette::Text, qc);
pal.setColor(QPalette::Disabled, QPalette::Text, qc);
pal.setColor(QPalette::Inactive, QPalette::WindowText, qc);
pal.setColor(QPalette::Disabled, QPalette::WindowText, qc);
pal.setColor(QPalette::Inactive, QPalette::HighlightedText, qc);
pal.setColor(QPalette::Disabled, QPalette::HighlightedText, qc);
}
if (!strcmp(mac_widget_colors[i].qt_class, "QMenu")) {
qc = qcolorForThemeTextColor(kThemeTextColorMenuItemActive);
pal.setBrush(QPalette::ButtonText, qc);
qc = qcolorForThemeTextColor(kThemeTextColorMenuItemSelected);
pal.setBrush(QPalette::HighlightedText, qc);
qc = qcolorForThemeTextColor(kThemeTextColorMenuItemDisabled);
pal.setBrush(QPalette::Disabled, QPalette::Text, qc);
} else if (!strcmp(mac_widget_colors[i].qt_class, "QAbstractButton")
|| !strcmp(mac_widget_colors[i].qt_class, "QHeaderView")
|| !strcmp(mac_widget_colors[i].qt_class, "Q3Header")) { //special
pal.setColor(QPalette::Disabled, QPalette::ButtonText,
pal.color(QPalette::Disabled, QPalette::Text));
pal.setColor(QPalette::Inactive, QPalette::ButtonText,
pal.color(QPalette::Inactive, QPalette::Text));
pal.setColor(QPalette::Active, QPalette::ButtonText,
pal.color(QPalette::Active, QPalette::Text));
} else if (!strcmp(mac_widget_colors[i].qt_class, "QAbstractItemView")) {
pal.setBrush(QPalette::Active, QPalette::Highlight,
qcolorForTheme(kThemeBrushAlternatePrimaryHighlightColor));
qc = qcolorForThemeTextColor(kThemeTextColorMenuItemSelected);
pal.setBrush(QPalette::Active, QPalette::HighlightedText, qc);
#if 1
pal.setBrush(QPalette::Inactive, QPalette::Text,
pal.brush(QPalette::Active, QPalette::Text));
pal.setBrush(QPalette::Inactive, QPalette::HighlightedText,
pal.brush(QPalette::Active, QPalette::Text));
#endif
} else if (!strcmp(mac_widget_colors[i].qt_class, "QTextEdit")
|| !strcmp(mac_widget_colors[i].qt_class, "QTextControl")) {
pal.setBrush(QPalette::Inactive, QPalette::Text,
pal.brush(QPalette::Active, QPalette::Text));
pal.setBrush(QPalette::Inactive, QPalette::HighlightedText,
pal.brush(QPalette::Active, QPalette::Text));
} else if (!strcmp(mac_widget_colors[i].qt_class, "QLineEdit")) {
pal.setBrush(QPalette::Disabled, QPalette::Base,
pal.brush(QPalette::Active, QPalette::Base));
}
bool set_palette = true;
PaletteHash *phash = qt_app_palettes_hash();
if (!phash->isEmpty()) {
PaletteHash::const_iterator it
= phash->constFind(mac_widget_colors[i].qt_class);
if (it != phash->constEnd())
set_palette = (pal != *it);
}
if (set_palette) {
QApplication::setPalette(pal, mac_widget_colors[i].qt_class);
#ifdef DEBUG_PLATFORM_SETTINGS
qt_mac_debug_palette(pal, QApplication::palette(), QLatin1String("Palette for ") + QString::fromLatin1(mac_widget_colors[i].qt_class));
#endif
}
}
}
}
static void qt_mac_event_release(EventRef &event)
{
ReleaseEvent(event);
event = 0;
}
#ifndef QT_MAC_USE_COCOA
static void qt_mac_event_release(QWidget *w, EventRef &event)
{
if (event) {
QWidget *widget = 0;
if (GetEventParameter(event, kEventParamQWidget, typeQWidget, 0, sizeof(widget), 0, &widget) == noErr
&& w == widget) {
if (IsEventInQueue(GetMainEventQueue(), event))
RemoveEventFromQueue(GetMainEventQueue(), event);
qt_mac_event_release(event);
}
}
}
static bool qt_mac_event_remove(EventRef &event)
{
if (event) {
if (IsEventInQueue(GetMainEventQueue(), event))
RemoveEventFromQueue(GetMainEventQueue(), event);
qt_mac_event_release(event);
return true;
}
return false;
}
#endif
/* sheets */
#ifndef QT_MAC_USE_COCOA
static EventRef request_showsheet_pending = 0;
#endif
void qt_event_request_showsheet(QWidget *w)
{
Q_ASSERT(qt_mac_is_macsheet(w));
#ifdef QT_MAC_USE_COCOA
w->repaint();
[NSApp beginSheet:qt_mac_window_for(w) modalForWindow:qt_mac_window_for(w->parentWidget())
modalDelegate:nil didEndSelector:nil contextInfo:0];
#else
qt_mac_event_remove(request_showsheet_pending);
CreateEvent(0, kEventClassQt, kEventQtRequestShowSheet, GetCurrentEventTime(),
kEventAttributeUserEvent, &request_showsheet_pending);
SetEventParameter(request_showsheet_pending, kEventParamQWidget, typeQWidget, sizeof(w), &w);
PostEventToQueue(GetMainEventQueue(), request_showsheet_pending, kEventPriorityStandard);
#endif
}
static void qt_post_window_change_event(QWidget *widget)
{
qt_widget_private(widget)->needWindowChange = true;
QEvent *glWindowChangeEvent = new QEvent(QEvent::MacGLWindowChange);
QApplication::postEvent(widget, glWindowChangeEvent);
}
/*
Posts updates to all child and grandchild OpenGL widgets for the given widget.
*/
static void qt_mac_update_child_gl_widgets(QWidget *widget)
{
// Update all OpenGL child widgets for the given widget.
QList<QWidgetPrivate::GlWidgetInfo> &glWidgets = qt_widget_private(widget)->glWidgets;
QList<QWidgetPrivate::GlWidgetInfo>::iterator end = glWidgets.end();
QList<QWidgetPrivate::GlWidgetInfo>::iterator it = glWidgets.begin();
for (;it != end; ++it) {
qt_post_window_change_event(it->widget);
}
}
/*
Sends updates to all child and grandchild gl widgets that have updates pending.
*/
void qt_mac_send_posted_gl_updates(QWidget *widget)
{
QList<QWidgetPrivate::GlWidgetInfo> &glWidgets = qt_widget_private(widget)->glWidgets;
QList<QWidgetPrivate::GlWidgetInfo>::iterator end = glWidgets.end();
QList<QWidgetPrivate::GlWidgetInfo>::iterator it = glWidgets.begin();
for (;it != end; ++it) {
QWidget *glWidget = it->widget;
if (qt_widget_private(glWidget)->needWindowChange) {
QEvent glChangeEvent(QEvent::MacGLWindowChange);
QApplication::sendEvent(glWidget, &glChangeEvent);
}
}
}
/*
Posts updates to all OpenGL widgets within the window that the given widget intersects.
*/
static void qt_mac_update_intersected_gl_widgets(QWidget *widget)
{
#ifndef QT_MAC_USE_COCOA
QList<QWidgetPrivate::GlWidgetInfo> &glWidgets = qt_widget_private(widget->window())->glWidgets;
if (glWidgets.isEmpty())
return;
// Exit if the window has not been created yet (mapToGlobal/size will force create it)
if (widget->testAttribute(Qt::WA_WState_Created) == false || HIViewGetWindow(qt_mac_nativeview_for(widget)) == 0)
return;
const QRect globalWidgetRect = QRect(widget->mapToGlobal(QPoint(0, 0)), widget->size());
QList<QWidgetPrivate::GlWidgetInfo>::iterator end = glWidgets.end();
QList<QWidgetPrivate::GlWidgetInfo>::iterator it = glWidgets.begin();
for (;it != end; ++it){
QWidget *glWidget = it->widget;
const QRect globalGlWidgetRect = QRect(glWidget->mapToGlobal(QPoint(0, 0)), glWidget->size());
if (globalWidgetRect.intersects(globalGlWidgetRect)) {
qt_post_window_change_event(glWidget);
it->lastUpdateWidget = widget;
} else if (it->lastUpdateWidget == widget) {
// Update the gl wigets that the widget intersected the last time around,
// and that we are not intersecting now. This prevents paint errors when the
// intersecting widget leaves a gl widget.
qt_post_window_change_event(glWidget);
it->lastUpdateWidget = 0;
}
}
#else
Q_UNUSED(widget);
#endif
}
/*
Posts a kEventQtRequestWindowChange event to the main Carbon event queue.
*/
static EventRef request_window_change_pending = 0;
Q_GUI_EXPORT void qt_event_request_window_change()
{
if(request_window_change_pending)
return;
CreateEvent(0, kEventClassQt, kEventQtRequestWindowChange, GetCurrentEventTime(),
kEventAttributeUserEvent, &request_window_change_pending);
PostEventToQueue(GetMainEventQueue(), request_window_change_pending, kEventPriorityHigh);
}
/* window changing. This is a hack around Apple's missing functionality, pending the toolbox
team fix. --Sam */
Q_GUI_EXPORT void qt_event_request_window_change(QWidget *widget)
{
if (!widget)
return;
// Post a kEventQtRequestWindowChange event. This event is semi-public,
// don't remove this line!
qt_event_request_window_change();
// Post update request on gl widgets unconditionally.
if (qt_widget_private(widget)->isGLWidget == true) {
qt_post_window_change_event(widget);
return;
}
qt_mac_update_child_gl_widgets(widget);
qt_mac_update_intersected_gl_widgets(widget);
}
/* activation */
static struct {
QPointer<QWidget> widget;
EventRef event;
EventLoopTimerRef timer;
EventLoopTimerUPP timerUPP;
} request_activate_pending = { 0, 0, 0, 0 };
bool qt_event_remove_activate()
{
if (request_activate_pending.timer) {
RemoveEventLoopTimer(request_activate_pending.timer);
request_activate_pending.timer = 0;
}
if (request_activate_pending.event)
qt_mac_event_release(request_activate_pending.event);
return true;
}
void qt_event_activate_timer_callbk(EventLoopTimerRef r, void *)
{
EventLoopTimerRef otc = request_activate_pending.timer;
qt_event_remove_activate();
if (r == otc && !request_activate_pending.widget.isNull()) {
const QWidget *tlw = request_activate_pending.widget->window();
Qt::WindowType wt = tlw->windowType();
if (tlw->isVisible()
&& ((wt != Qt::Desktop && wt != Qt::Popup && wt != Qt::Tool) || tlw->isModal())) {
CreateEvent(0, kEventClassQt, kEventQtRequestActivate, GetCurrentEventTime(),
kEventAttributeUserEvent, &request_activate_pending.event);
PostEventToQueue(GetMainEventQueue(), request_activate_pending.event, kEventPriorityHigh);
}
}
}
void qt_event_request_activate(QWidget *w)
{
if (w == request_activate_pending.widget)
return;
/* We put these into a timer because due to order of events being sent we need to be sure this
comes from inside of the event loop */
qt_event_remove_activate();
if (!request_activate_pending.timerUPP)
request_activate_pending.timerUPP = NewEventLoopTimerUPP(qt_event_activate_timer_callbk);
request_activate_pending.widget = w;
InstallEventLoopTimer(GetMainEventLoop(), 0, 0, request_activate_pending.timerUPP, 0, &request_activate_pending.timer);
}
/* menubars */
#ifndef QT_MAC_USE_COCOA
static EventRef request_menubarupdate_pending = 0;
#endif
void qt_event_request_menubarupdate()
{
#ifndef QT_MAC_USE_COCOA
if (request_menubarupdate_pending) {
if (IsEventInQueue(GetMainEventQueue(), request_menubarupdate_pending))
return;
#ifdef DEBUG_DROPPED_EVENTS
qDebug("%s:%d Whoa, we dropped an event on the floor!", __FILE__, __LINE__);
#endif
}
CreateEvent(0, kEventClassQt, kEventQtRequestMenubarUpdate, GetCurrentEventTime(),
kEventAttributeUserEvent, &request_menubarupdate_pending);
PostEventToQueue(GetMainEventQueue(), request_menubarupdate_pending, kEventPriorityHigh);
#else
// Just call this. The request has the benefit that we don't call this multiple times, but
// we can optimize this.
QMenuBar::macUpdateMenuBar();
#endif
}
#ifndef QT_MAC_USE_COCOA
//context menu
static EventRef request_context_pending = 0;
static void qt_event_request_context(QWidget *w=0, EventRef *where=0)
{
if (!where)
where = &request_context_pending;
if (*where)
return;
CreateEvent(0, kEventClassQt, kEventQtRequestContext, GetCurrentEventTime(),
kEventAttributeUserEvent, where);
if (w)
SetEventParameter(*where, kEventParamQWidget, typeQWidget, sizeof(w), &w);
PostEventToQueue(GetMainEventQueue(), *where, kEventPriorityStandard);
}
#endif
void QApplicationPrivate::createEventDispatcher()
{
Q_Q(QApplication);
if (q->type() != QApplication::Tty)
eventDispatcher = new QEventDispatcherMac(q);
else
eventDispatcher = new QEventDispatcherUNIX(q);
}
/* clipboard */
void qt_event_send_clipboard_changed()
{
#ifndef QT_MAC_USE_COCOA
AppleEvent ae;
if (AECreateAppleEvent(kEventClassQt, typeAEClipboardChanged, 0, kAutoGenerateReturnID, kAnyTransactionID, &ae) != noErr)
qDebug("Can't happen!!");
AppleEvent reply;
AESend(&ae, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, 0, 0);
#endif
}
/* app menu */
static QMenu *qt_mac_dock_menu = 0;
Q_GUI_EXPORT void qt_mac_set_dock_menu(QMenu *menu)
{
qt_mac_dock_menu = menu;
#ifdef QT_MAC_USE_COCOA
[NSApp setDockMenu:menu->macMenu()];
#else
SetApplicationDockTileMenu(menu->macMenu());
#endif
}
/* events that hold pointers to widgets, must be cleaned up like this */