VirtualBox

Ignore:
Timestamp:
Mar 29, 2007 2:15:54 PM (18 years ago)
Author:
vboxsync
Message:

FE/Qt: Attempted to fix a bunch of bugs in VBoxGlobal::getOpenFileName().

Location:
trunk/src/VBox/Frontends/VirtualBox
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/include/VBoxGlobal.h

    r1790 r1804  
    338338    bool startMachine (const QUuid &id);
    339339
    340     QString getOpenFileName (const QString &, const QString &, QWidget*, const char*,
    341                              const QString &, QString *defaultFilter = 0,
    342                              bool resolveSymLinks = true);
    343 
    344340    void startEnumeratingMedia();
    345341
     
    399395
    400396    static QString highlight (const QString &aStr, bool aToolTip = false);
     397
     398    static QString getOpenFileName (const QString &, const QString &, QWidget*,
     399                                    const char*, const QString &,
     400                                    QString *defaultFilter = 0,
     401                                    bool resolveSymLinks = true);
    401402
    402403signals:
  • trunk/src/VBox/Frontends/VirtualBox/src/VBoxGlobal.cpp

    r1790 r1804  
    291291#endif
    292292
     293// Helpers for VBoxGlobal::getOpenFileName()
     294/////////////////////////////////////////////////////////////////////////////
     295
    293296#if defined Q_WS_WIN
    294 /*
    295  * These definitions are used for Win32 API native dialog displaying.
    296  */
     297
    297298extern void qt_enter_modal (QWidget*);
    298299extern void qt_leave_modal (QWidget*);
    299 const char qt_file_dialog_filter_reg_exp[] = "([a-zA-Z0-9 ]*)\\(([a-zA-Z0-9_.*? +;#\\[\\]]*)\\)$";
    300 
    301 /*
    302  * This static function made for convenience using QFileDialog filter
    303  * format with our own Win32 native dialog. It composes QStringList of
    304  * filters for Win32 API.
    305  */
    306 static QStringList makeFiltersList (const QString &aFilter)
    307 {
    308     if (aFilter.isEmpty())
    309         return QStringList();
    310 
    311     int i = aFilter.find (";;", 0);
    312     QString sep (";;");
    313     if (i == -1)
    314     {
    315         if (aFilter.find ("\n", 0) != -1)
    316         {
    317             sep = "\n";
    318             i = aFilter.find (sep, 0);
    319         }
    320     }
    321 
    322     return QStringList::split (sep, aFilter);
    323 }
    324 
    325 /*
    326  * This static function made for convenience using QFileDialog filter
    327  * format with our own Win32 native dialog. It converts filter from
    328  * QFileDialog format into Win32 API format.
    329  */
     300
    330301static QString extractFilter (const QString &aRawFilter)
    331302{
     303    static const char qt_file_dialog_filter_reg_exp[] =
     304        "([a-zA-Z0-9 ]*)\\(([a-zA-Z0-9_.*? +;#\\[\\]]*)\\)$";
     305
    332306    QString result = aRawFilter;
    333307    QRegExp r (QString::fromLatin1 (qt_file_dialog_filter_reg_exp));
     
    338312}
    339313
    340 /*
    341  * This static function made for convenience using QFileDialog filter
    342  * format with our own Win32 native dialog. This is wrapper used for
    343  * all QFileDialog filter converting into Win32 API format.
     314/**
     315 * Converts QFileDialog filter list to Win32 API filter list.
    344316 */
    345317static QString winFilter (const QString &aFilter)
    346318{
    347     QStringList filterLst = makeFiltersList (aFilter);
     319    QStringList filterLst;
     320
     321    if (!aFilter.isEmpty())
     322    {
     323        int i = aFilter.find (";;", 0);
     324        QString sep (";;");
     325        if (i == -1)
     326        {
     327            if (aFilter.find ("\n", 0) != -1)
     328            {
     329                sep = "\n";
     330                i = aFilter.find (sep, 0);
     331            }
     332        }
     333   
     334        filterLst = QStringList::split (sep, aFilter);
     335    }
     336
    348337    QStringList::Iterator it = filterLst.begin();
    349338    QString winfilters;
     
    360349
    361350/*
    362  * This Win32 API callback function made for control using native
    363  * Win32 API dialog. It handles the situation of switching this dialog
    364  * from one file filter mode to another.
     351 * Callback function to control the native Win32 API file dialog
    365352 */
    366353UINT CALLBACK OFNHookProc (HWND aHdlg, UINT aUiMsg, WPARAM aWParam, LPARAM aLParam)
     
    371358        if (notif->hdr.code == CDN_TYPECHANGE)
    372359        {
    373             /* locating native dialog controls */
     360            /* locate native dialog controls */
    374361            HWND parent = GetParent (aHdlg);
    375362            HWND button = GetDlgItem (parent, IDOK);
     
    390377            free (buffer);
    391378
    392             /* making request for focus moving to filter selector combo-box */
     379            /* make request for focus moving to filter selector combo-box */
    393380            HWND curFocus = GetFocus();
    394381            PostMessage (curFocus, WM_KILLFOCUS, (WPARAM)selector, 0);
     
    401388}
    402389
    403 /*
    404  * This static function made for convenience using native Win32 API dialog.
    405  * This is function is used for composing settings for Win32 API native dialog.
    406  */
    407 static OPENFILENAME* makeOFN (QWidget* parent,
    408                               const QString& initialSelection,
    409                               const QString& initialDirectory,
    410                               const QString& title,
    411                               const QString& filters,
    412                               QFileDialog::Mode mode)
    413 {
    414     parent = parent ? parent->topLevelWidget() : qApp->mainWidget();
    415 
    416     static QString tFilters = filters;
    417     QString tInitDir = QDir::convertSeparators (initialDirectory);
    418     QString initSel = QDir::convertSeparators (initialSelection);
    419     int maxLen = 1023;
    420     TCHAR *tInitSel = new TCHAR [maxLen+1];
    421     if (initSel.length() > 0 && initSel.length() <= (uint)maxLen)
    422         memcpy (tInitSel, initSel.ucs2(), (initSel.length()+1)*sizeof (QChar));
    423     else
    424         tInitSel[0] = 0;
    425 
    426     OPENFILENAME* ofn = new OPENFILENAME;
    427     memset (ofn, 0, sizeof (OPENFILENAME));
    428 
    429     ofn->lStructSize    = sizeof(OPENFILENAME);
    430     ofn->hwndOwner  = parent ? parent->winId() : 0;
    431     ofn->lpstrFilter    = (TCHAR *)tFilters.ucs2();
    432     ofn->lpstrFile  = tInitSel;
    433     ofn->nMaxFile   = maxLen;
    434     ofn->lpstrInitialDir = (TCHAR *)tInitDir.ucs2();
    435     ofn->lpstrTitle = (TCHAR *)title.ucs2();
    436     ofn->Flags = (OFN_NOCHANGEDIR | OFN_HIDEREADONLY |
    437                   OFN_EXPLORER | OFN_ENABLEHOOK |
    438                   OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST);
    439     ofn->lpfnHook = OFNHookProc;
    440     return ofn;
    441 }
    442 
    443 /*
    444  * This static function made for convenience using native Win32 API dialog.
    445  * This is function is used for cleaning settings for Win32 API native dialog.
    446  */
    447 static void cleanUpOFN (OPENFILENAME** ofn)
    448 {
    449     delete (*ofn)->lpstrFile;
    450     delete *ofn;
    451     *ofn = 0;
    452 }
    453 #endif
     390#endif /* Q_WS_WIN */
    454391
    455392
     
    663600
    664601    return *console_wnd;
    665 }
    666 
    667 QString VBoxGlobal::getOpenFileName (const QString &startWith,
    668                                                   const QString &filter,
    669                                                   QWidget       *parent,
    670                                                   const char    *name,
    671                                                   const QString &caption,
    672                                                   QString       *selectedFilter,
    673                                                   bool           resolveSymlinks )
    674 {
    675 #if defined Q_WS_WIN
    676     QString result;
    677     QFileInfo fi (startWith);
    678     QString workingDirectory = fi.exists() ? fi.dirPath (TRUE) : QDir::homeDirPath();
    679     QString initialSelection = fi.isFile() ? fi.fileName() : QString::null;
    680     QString title = caption.isNull() ? tr ("Select file to open") : caption;
    681 
    682     if (parent)
    683     {
    684         QEvent e (QEvent::WindowBlocked);
    685         QApplication::sendEvent (parent, &e);
    686         qt_enter_modal (parent);
    687     }
    688 
    689     OPENFILENAME* ofn = makeOFN (parent, initialSelection,
    690                                  workingDirectory, title,
    691                                  winFilter (filter), QFileDialog::ExistingFile);
    692     if (GetOpenFileName (ofn))
    693     {
    694         result = QString::fromUcs2 ((ushort*)ofn->lpstrFile);
    695     }
    696     cleanUpOFN (&ofn);
    697 
    698     if (parent)
    699     {
    700         qt_leave_modal (parent);
    701         QEvent e (QEvent::WindowUnblocked);
    702         QApplication::sendEvent (parent, &e);
    703     }
    704 
    705     // qt_win_eatMouseMove();
    706     MSG msg = {0, 0, 0, 0, 0, 0, 0};
    707     while (PeekMessage (&msg, 0, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE));
    708     if (msg.message == WM_MOUSEMOVE)
    709         PostMessage (msg.hwnd, msg.message, 0, msg.lParam);
    710 
    711     return result.isEmpty() ? result : QFileInfo (result).absFilePath();
    712 #else
    713     return QFileDialog::getOpenFileName (startWith, filter, parent, name,
    714                                          caption, selectedFilter, resolveSymlinks );
    715 #endif
    716602}
    717603
     
    22242110}
    22252111
    2226 /* static */
    22272112/**
    22282113 *  Reformats the input string @a aStr so that:
     
    22392124 *  is added. Also, new line chars are replaced with <br> instead of <p>.
    22402125 */
     2126/* static */
    22412127QString VBoxGlobal::highlight (const QString &aStr, bool aToolTip /* = false */)
    22422128{
     
    22752161
    22762162    return text;
     2163}
     2164
     2165/**
     2166 *  Reimplementation of QFileDialog::getOpenFileName() that removes some
     2167 *  oddities and limitations.
     2168 *
     2169 *  On Win32, this funciton makes sure a file filter is applied automatically
     2170 *  right after it is selected from the drop-down list, to conform to common
     2171 *  experience in other applications.
     2172 *
     2173 *  On all other platforms, this function is equivalent to
     2174 *  QFileDialog::getOpenFileName().
     2175 */
     2176/* static */
     2177QString VBoxGlobal::getOpenFileName (const QString &startWith,
     2178                                     const QString &filters,
     2179                                     QWidget       *parent,
     2180                                     const char    *name,
     2181                                     const QString &caption,
     2182                                     QString       *selectedFilter,
     2183                                     bool           resolveSymlinks)
     2184{
     2185    LogTraceMsg (("filters=%s\n", filters.local8Bit().data()));
     2186#if defined Q_WS_WIN
     2187
     2188    QString result;
     2189
     2190    QString workDir;
     2191    QString initSel;
     2192    QFileInfo fi (startWith);
     2193
     2194    if (fi.isDir())
     2195        workDir = startWith;
     2196    else
     2197    {
     2198        workDir = fi.dirPath (true);
     2199        initSel = fi.fileName();
     2200    }
     2201
     2202    workDir = QDir::convertSeparators (workDir);
     2203    if (!workDir.endsWith ("\\"))
     2204        workDir += "\\";
     2205
     2206    QString title = caption.isNull() ? tr ("Select file to open") : caption;
     2207
     2208    if (parent)
     2209    {
     2210        QEvent e (QEvent::WindowBlocked);
     2211        QApplication::sendEvent (parent, &e);
     2212        qt_enter_modal (parent);
     2213    }
     2214
     2215    parent = parent ? parent->topLevelWidget() : qApp->mainWidget();
     2216
     2217    QString winFilters = winFilter (filters);
     2218    AssertCompile (sizeof (TCHAR) == sizeof (QChar));
     2219    TCHAR buf [1024];
     2220    if (initSel.length() > 0 && initSel.length() < sizeof (buf))
     2221        memcpy (buf, initSel.ucs2(), (initSel.length() + 1) * sizeof (TCHAR));
     2222    else
     2223        buf [0] = 0;
     2224
     2225    OPENFILENAME ofn;
     2226    memset (&ofn, 0, sizeof (OPENFILENAME));
     2227
     2228    ofn.lStructSize = sizeof (OPENFILENAME);
     2229    ofn.hwndOwner = parent ? parent->winId() : 0;
     2230    ofn.lpstrFilter = (TCHAR *) winFilters.ucs2();
     2231    ofn.lpstrFile = buf;
     2232    ofn.nMaxFile = sizeof (buf) - 1;
     2233    ofn.lpstrInitialDir = (TCHAR *) workDir.ucs2();
     2234    ofn.lpstrTitle = (TCHAR *) title.ucs2();
     2235    ofn.Flags = (OFN_NOCHANGEDIR | OFN_HIDEREADONLY |
     2236                  OFN_EXPLORER | OFN_ENABLEHOOK |
     2237                  OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST);
     2238    ofn.lpfnHook = OFNHookProc;
     2239
     2240    if (GetOpenFileName (&ofn))
     2241    {
     2242        result = QString::fromUcs2 ((ushort *) ofn.lpstrFile);
     2243    }
     2244
     2245    if (parent)
     2246    {
     2247        qt_leave_modal (parent);
     2248        QEvent e (QEvent::WindowUnblocked);
     2249        QApplication::sendEvent (parent, &e);
     2250    }
     2251
     2252    // qt_win_eatMouseMove();
     2253    MSG msg = {0, 0, 0, 0, 0, 0, 0};
     2254    while (PeekMessage (&msg, 0, WM_MOUSEMOVE, WM_MOUSEMOVE, PM_REMOVE));
     2255    if (msg.message == WM_MOUSEMOVE)
     2256        PostMessage (msg.hwnd, msg.message, 0, msg.lParam);
     2257
     2258    return result.isEmpty() ? result : QFileInfo (result).absFilePath();
     2259
     2260#else
     2261
     2262    return QFileDialog::getOpenFileName (startWith, filter, parent, name,
     2263                                         caption, selectedFilter, resolveSymlinks );
     2264
     2265#endif
    22772266}
    22782267
  • trunk/src/VBox/Frontends/VirtualBox/ui/VBoxDiskImageManagerDlg.ui.h

    r1790 r1804  
    17911791    QString dir;
    17921792    if (item && item->getStatus() == VBoxMedia::Ok)
    1793         dir = item->getPath().stripWhiteSpace();
     1793        dir = QFileInfo (item->getPath().stripWhiteSpace()).dirPath (true);
    17941794
    17951795    if (!dir)
    17961796        if (currentList == hdsView)
    17971797            dir = vbox.GetSystemProperties().GetDefaultVDIFolder();
     1798
    17981799    if (!dir || !QFileInfo (dir).exists())
    17991800        dir = vbox.GetHomeFolder();
     
    18291830    }
    18301831
    1831     QString src = vboxGlobal().getOpenFileName (dir, filter, this,
    1832                                                 "AddDiskImageDialog", title);
     1832    QString src = VBoxGlobal::getOpenFileName (dir, filter, this,
     1833                                               "AddDiskImageDialog", title);
    18331834    src =  QDir::convertSeparators (src);
    18341835
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette