Changeset 239 for trunk/src


Ignore:
Timestamp:
Oct 17, 2009, 1:26:01 AM (16 years ago)
Author:
Dmitry A. Kuminov
Message:

gui: Use fonts and colors from the system palette for Qt widgets by default to make them look more native.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/gui/kernel/qapplication_pm.cpp

    r225 r239  
    148148};
    149149
     150static QRgb qt_sysclr2qrgb(LONG sysClr)
     151{
     152    // QRgb has the same RGB format (0xaarrggbb) as OS/2 uses (ignoring the
     153    // highest alpha byte) so we just cast the OS/2 LONG RGB value to qRgb
     154    // which is an unsigned int actually.
     155    return ((QRgb)WinQuerySysColor(HWND_DESKTOP, sysClr, 0)) & RGB_MASK;
     156}
     157
     158static QFont qt_sysfont2qfont(const PSZ scope)
     159{
     160    // note: 10.System Proportional is de-facto the default font selected into
     161    // the presentation space
     162
     163    static const PSZ app = "PM_SystemFonts";
     164    static const PSZ def = "10.System Proportional";
     165
     166    ULONG keyLen = 0;
     167    QFont f("System Proportional", 10);
     168
     169    if (PrfQueryProfileSize(HINI_USERPROFILE, app, scope, &keyLen) && keyLen) {
     170        keyLen++; // reserve space for the dot
     171        char *buf = new char[keyLen];
     172        ULONG realLen = PrfQueryProfileString(HINI_USERPROFILE, app, scope, def,
     173                                              buf, keyLen);
     174        realLen--; // excude terminating NULL
     175
     176        // parse the font definition
     177        int height = 0;
     178        char *dot = strchr(buf, '.'), *dot2 = 0;
     179        if (dot) {
     180            *dot = 0;
     181            height = strtoul(buf, NULL, 10);
     182            dot2 = strchr(++ dot, '.');
     183            if (dot2) {
     184                // process simulated styles
     185                buf[realLen] = '.';
     186                buf[realLen+1] = 0;
     187                strupr( dot2 );
     188                // @todo currently, simulated bold and italic font styles are not
     189                // supported by Qt/OS2 because Qt doesn't support style simulation
     190                // explicitly. the code below is commented out to prevent selecting
     191                // true fonts when simulated ones are actually requested.
     192                // if (strstr(dot2, ".BOLD.")) f.setBold(true);
     193                // if (strstr(dot2, ".ITALIC.")) f.setItalic(true);
     194                if (strstr(dot2, ".UNDERSCORE.")) f.setUnderline(true);
     195                if (strstr(dot2, ".UNDERLINED.")) f.setUnderline(true);
     196                if (strstr(dot2, ".STRIKEOUT.")) f.setStrikeOut(true);
     197                *dot2 = 0;
     198            }
     199            // query non-simulated styles
     200            FONTMETRICS fm;
     201            LONG cnt = 1; // use the first match
     202            GpiQueryFonts(qt_display_ps(), QF_PUBLIC, dot, &cnt, sizeof(FONTMETRICS), &fm);
     203            if (cnt) {
     204                if (fm.fsSelection & FM_SEL_ITALIC) f.setItalic(true);
     205                if (fm.fsType & FM_TYPE_FIXED) f.setFixedPitch(true);
     206                USHORT weight = fm.usWeightClass;
     207                USHORT width = fm.usWidthClass;
     208                if (weight < 4) f.setWeight( QFont::Light );
     209                else if (weight < 6) f.setWeight(QFont::Normal);
     210                else if (weight < 7) f.setWeight(QFont::DemiBold);
     211                else if (weight < 8) f.setWeight(QFont::Bold);
     212                else f.setWeight(QFont::Black);
     213                switch (width) {
     214                    case 1: f.setStretch(QFont::UltraCondensed); break;
     215                    case 2: f.setStretch(QFont::ExtraCondensed); break;
     216                    case 3: f.setStretch(QFont::Condensed); break;
     217                    case 4: f.setStretch(QFont::SemiCondensed); break;
     218                    case 5: f.setStretch(QFont::Unstretched); break;
     219                    case 6: f.setStretch(QFont::SemiExpanded); break;
     220                    case 7: f.setStretch(QFont::Expanded); break;
     221                    case 8: f.setStretch(QFont::ExtraExpanded); break;
     222                    case 9: f.setStretch(QFont::UltraExpanded); break;
     223                    default: f.setStretch(QFont::Unstretched); break;
     224                }
     225                f.setFamily(fm.szFamilyname);
     226                f.setPointSize(height);
     227            }
     228        }
     229        delete[] buf;
     230    }
     231    return f;
     232}
     233
    150234static void qt_set_pm_resources()
    151235{
    152     // @todo later: take colors, fonts, etc. from the system theme
     236    QFont menuFont = qt_sysfont2qfont("Menus");
     237    QFont iconFont = qt_sysfont2qfont("IconText");
     238    QFont titleFont = qt_sysfont2qfont("WindowTitles");
     239
     240    QApplication::setFont(menuFont, "QMenu");
     241    QApplication::setFont(menuFont, "QMenuBar");
     242    QApplication::setFont(titleFont, "Q3TitleBar");
     243    QApplication::setFont(titleFont, "QWorkspaceTitleBar");
     244    QApplication::setFont(iconFont, "QAbstractItemView");
     245    QApplication::setFont(iconFont, "QDockWidgetTitle");
     246
     247    QPalette pal;
     248
     249    pal.setColor(QPalette::WindowText,
     250                 QColor(qt_sysclr2qrgb(SYSCLR_WINDOWTEXT)));
     251    pal.setColor(QPalette::Window,
     252                 QColor(qt_sysclr2qrgb(SYSCLR_DIALOGBACKGROUND)));
     253    pal.setColor(QPalette::ButtonText,
     254                 QColor(qt_sysclr2qrgb(SYSCLR_MENUTEXT)));
     255    pal.setColor(QPalette::Button,
     256                 QColor(qt_sysclr2qrgb(SYSCLR_BUTTONMIDDLE)));
     257    pal.setColor(QPalette::Light,
     258                 QColor(qt_sysclr2qrgb(SYSCLR_BUTTONLIGHT)));
     259    pal.setColor(QPalette::Dark,
     260                 QColor(qt_sysclr2qrgb(SYSCLR_BUTTONDARK)));
     261    pal.setColor(QPalette::Midlight,
     262                 QColor((pal.light().color().red()   + pal.button().color().red())   / 2,
     263                        (pal.light().color().green() + pal.button().color().green()) / 2,
     264                        (pal.light().color().blue()  + pal.button().color().blue())  / 2));
     265    pal.setColor(QPalette::Mid,
     266                 QColor((pal.dark().color().red()    + pal.button().color().red())   / 2,
     267                        (pal.dark().color().green()  + pal.button().color().green()) / 2,
     268                        (pal.dark().color().blue()   + pal.button().color().blue())  / 2));
     269    pal.setColor(QPalette::Shadow, // note: SYSCLR_SHADOW often = SYSCLR_BUTTONDARK
     270                 QColor(qt_sysclr2qrgb(SYSCLR_BUTTONDEFAULT)));
     271    pal.setColor(QPalette::Text,
     272                 QColor(qt_sysclr2qrgb(SYSCLR_WINDOWTEXT)));
     273    pal.setColor(QPalette::Base,
     274                 QColor(qt_sysclr2qrgb(SYSCLR_ENTRYFIELD)));
     275    pal.setColor(QPalette::BrightText,
     276                 QColor(qt_sysclr2qrgb(SYSCLR_BUTTONLIGHT)));
     277    pal.setColor(QPalette::Highlight,
     278                 QColor(qt_sysclr2qrgb(SYSCLR_HILITEBACKGROUND)));
     279    pal.setColor(QPalette::HighlightedText,
     280                 QColor(qt_sysclr2qrgb(SYSCLR_HILITEFOREGROUND)));
     281    // these colors are not present in the PM system palette
     282    pal.setColor(QPalette::Link, Qt::blue);
     283    pal.setColor(QPalette::LinkVisited, Qt::magenta);
     284
     285    // disabled colors
     286    // note: it should be SYSCLR_MENUDISABLEDTEXT but many styles use etched
     287    // appearance for disabled elements (in combination with QPalette::Light)
     288    // which gives weakly readable text. Make it somewhat darker.
     289    pal.setColor(QPalette::Disabled, QPalette::WindowText,
     290                  QColor(qt_sysclr2qrgb(SYSCLR_BUTTONDARK)));
     291    pal.setColor(QPalette::Disabled, QPalette::ButtonText,
     292                  QColor(qt_sysclr2qrgb(SYSCLR_BUTTONDARK)));
     293    pal.setColor(QPalette::Disabled, QPalette::Text,
     294                  QColor(qt_sysclr2qrgb(SYSCLR_BUTTONDARK)));
     295
     296    QApplicationPrivate::setSystemPalette(pal);
     297
     298    // special palete: menus
     299    QPalette spal = pal;
     300    spal.setColor(QPalette::Window,
     301                  QColor(qt_sysclr2qrgb(SYSCLR_MENU)));
     302    spal.setColor(QPalette::WindowText,
     303                  QColor(qt_sysclr2qrgb(SYSCLR_MENUTEXT)));
     304    spal.setColor(QPalette::Highlight,
     305                  QColor(qt_sysclr2qrgb( SYSCLR_MENUHILITEBGND)));
     306    spal.setColor(QPalette::HighlightedText,
     307                  QColor(qt_sysclr2qrgb(SYSCLR_MENUHILITE)));
     308
     309    QApplication::setPalette(spal, "QMenu");
     310    QApplication::setPalette(spal, "QMenuBar");
     311
     312    // special palete: static widget text
     313    spal = pal;
     314    QColor staticTextCol(qt_sysclr2qrgb( SYSCLR_WINDOWSTATICTEXT));
     315    spal.setColor(QPalette::WindowText, staticTextCol);
     316    spal.setColor(QPalette::Text, staticTextCol);
     317
     318    QApplication::setPalette(spal, "QLabel");
     319    QApplication::setPalette(spal, "QGroupBox");
    153320};
    154321
     
    194361    qApp->setObjectName(priv->appName());
    195362
    196     // default font (HELV.PFB series should always be present)
    197     QApplicationPrivate::setSystemFont(QFont(QLatin1String("Helvetica"), 10));
     363    // @todo search for QTPM_USE_WINDOWFONT in Qt3 for OS/2 sources for a
     364    // discussion on whether to use PM_Fonts/DefaultFont or WindowText as the
     365    // default one. So far, the latter is used.
     366    QApplicationPrivate::setSystemFont(qt_sysfont2qfont("WindowText"));
    198367
    199368    // QFont::locale_init();  ### Uncomment when it does something on OS/2
     
    20742243        USHORT vk = SHORT2FROMMP(qmsg.mp2);
    20752244        str += QString().
    2076             sprintf(" rep %02d scan %02X ch %04X (%ls) vk %04X",
     2245            sprintf(" rep %02d scan %02X ch %04X (%s) vk %04X",
    20772246                    repeat, scan, ch, (ch > 32 && ch < 254) ?
    2078                     QString::fromLocal8Bit((char *)&ch, 1).utf16() :
    2079                     QString(QChar(' ')).utf16(), vk);
     2247                    qPrintable(QString::fromLocal8Bit((char *)&ch, 1)) :
     2248                    qPrintable(QString(QChar(' '))), vk);
    20802249        QString flstr;
    20812250        myDefFlagEx(fl, KC_CHAR, flstr, "CHAR");
Note: See TracChangeset for help on using the changeset viewer.