From 230c90774f48fd3ba9a9f1c3af5354fbad94843f Mon Sep 17 00:00:00 2001 From: Julian Smart Date: Sat, 15 Apr 2006 17:40:13 +0000 Subject: [PATCH] Easier platform testing class and function git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@38746 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/defs.h | 18 +++++ include/wx/utils.h | 70 +++++++++++++++++++ samples/dialogs/dialogs.cpp | 13 +++- src/common/utilscmn.cpp | 134 ++++++++++++++++++++++++++++++++++++ 4 files changed, 233 insertions(+), 2 deletions(-) diff --git a/include/wx/defs.h b/include/wx/defs.h index ea4f1efbdc..8bfbee8cc2 100644 --- a/include/wx/defs.h +++ b/include/wx/defs.h @@ -701,6 +701,24 @@ enum wxDOS /* wxBase under MS-DOS */ }; +/* Friendlier platform names */ +enum +{ + wxMotif = wxMOTIF_X, + wxMac = wxMAC, + wxMSW = wxWINDOWS, + wxWinCE = wxWINDOWS_CE, + wxWinPocketPC = wxWINDOWS_POCKETPC, + wxWinSmartPhone = wxWINDOWS_SMARTPHONE, + wxWin95= wxWIN95, + wxUnix = wxUNIX, /* wxBase under Unix */ + wxPalmOS = wxPALMOS, /* PalmOS */ + wxOS2 = wxOS2_PM, + + wxMGL = 100, + wxCocoa +}; + /* ---------------------------------------------------------------------------- */ /* standard wxWidgets types */ /* ---------------------------------------------------------------------------- */ diff --git a/include/wx/utils.h b/include/wx/utils.h index e599e210cb..9923ea9159 100644 --- a/include/wx/utils.h +++ b/include/wx/utils.h @@ -21,6 +21,7 @@ #include "wx/filefn.h" class WXDLLIMPEXP_BASE wxArrayString; +class WXDLLIMPEXP_BASE wxArrayInt; // need this for wxGetDiskSpace() as we can't, unfortunately, forward declare // wxLongLong @@ -100,6 +101,75 @@ WXDLLIMPEXP_BASE const wxChar *wxGetInstallPrefix(); // Return path to wxWin data (/usr/share/wx/%{version}) (Unices) WXDLLIMPEXP_BASE wxString wxGetDataDir(); +/* + * Class to make it easier to specify platform-dependent values + * + * Examples: + * long val = wxPlatform(3).Is(wxMac, 1).Is(wxGTK, 2).Is(stPDA, 5); + * wxString strVal = wxPlatform(wxT("Hello")).Is(wxMac, wxT("Mac")).Is(wxMSW, wxT("MSW")); + * + * A custom platform symbol: + * + * #define stPDA 100 + * #ifdef __WXWINCE__ + * wxPlatform::AddPlatform(stPDA); + * #endif + * + * long windowStyle = wxCAPTION | (long) wxPlatform().IsNot(stPDA, wxRESIZE_BORDER); + * + */ + +class WXDLLIMPEXP_BASE wxPlatform +{ +public: + wxPlatform() { m_longValue = 0; m_doubleValue = 0.0; } + + // Specify an optional default value + wxPlatform(long defValue) { m_longValue = defValue; m_doubleValue = 0.0; } + wxPlatform(const wxString& defValue) { m_stringValue = defValue; m_longValue = 0; m_doubleValue = 0.0; } + wxPlatform(double defValue) { m_longValue = 0; m_doubleValue = defValue; } + + wxPlatform& Is(int platform, long value); + wxPlatform& IsNot(int platform, long value); + + wxPlatform& Is(int platform, int value) { return Is(platform, (long) value); } + wxPlatform& IsNot(int platform, int value) { return IsNot(platform, (long) value); } + + wxPlatform& Is(int platform, const wxString& value); + wxPlatform& IsNot(int platform, const wxString& value); + + wxPlatform& Is(int platform, double value); + wxPlatform& IsNot(int platform, double value); + + // This should be specified first to set the default value, or simply + // pass the value to the constructor + wxPlatform& Default(long value); + wxPlatform& Default(const wxString& value); + wxPlatform& Default(double value); + + long GetInteger() const { return m_longValue; } + const wxString& GetString() const { return m_stringValue; } + double GetDouble() const { return m_doubleValue; } + + operator int() const { return (int) GetInteger(); } + operator long() const { return GetInteger(); } + operator double() const { return GetDouble(); } + operator const wxString() const { return GetString(); } + operator const wxChar*() const { return (const wxChar*) GetString(); } + + static void AddPlatform(int platform); + static bool PlatformIs(int platform); + static void ClearPlatforms(); + +private: + long m_longValue; + double m_doubleValue; + wxString m_stringValue; + static wxArrayInt* sm_customPlatforms; +}; + +/// Function for testing current platform +inline bool wxPlatformIs(int platform) { return wxPlatform::PlatformIs(platform); } #if wxUSE_GUI diff --git a/samples/dialogs/dialogs.cpp b/samples/dialogs/dialogs.cpp index 14bfa35549..f0cc421f00 100644 --- a/samples/dialogs/dialogs.cpp +++ b/samples/dialogs/dialogs.cpp @@ -369,9 +369,14 @@ bool MyApp::OnInit() wxMenu *sheet_menu = new wxMenu; sheet_menu->Append(DIALOGS_PROPERTY_SHEET, _T("&Standard property sheet\tShift-Ctrl-P")); sheet_menu->Append(DIALOGS_PROPERTY_SHEET_TOOLBOOK, _T("&Toolbook sheet\tShift-Ctrl-T")); + + if (wxPlatformIs(wxMac)) + sheet_menu->Append(DIALOGS_PROPERTY_SHEET_BUTTONTOOLBOOK, _T("Button &Toolbook sheet\tShift-Ctrl-U")); +/* #ifdef __WXMAC__ sheet_menu->Append(DIALOGS_PROPERTY_SHEET_BUTTONTOOLBOOK, _T("Button &Toolbook sheet\tShift-Ctrl-U")); #endif +*/ file_menu->Append(wxID_ANY, _T("&Property sheets"), sheet_menu); #endif // USE_SETTINGS_DIALOG @@ -1455,18 +1460,22 @@ SettingsDialog::SettingsDialog(wxWindow* win, int dialogType) m_imageList = NULL; Create(win, wxID_ANY, _("Preferences"), wxDefaultPosition, wxDefaultSize, - wxDEFAULT_DIALOG_STYLE + wxDEFAULT_DIALOG_STYLE| (int)wxPlatform().IsNot(wxWinCE, resizeBorder) +/* #ifndef __WXWINCE__ |resizeBorder #endif +*/ ); // If using a toolbook, also follow Mac style and don't create buttons if (!useToolBook) - CreateButtons(wxOK|wxCANCEL + CreateButtons(wxOK|wxCANCEL| (int)wxPlatform().IsNot(wxWinPocketPC, wxHELP) +/* #ifndef __POCKETPC__ |wxHELP #endif +*/ ); wxBookCtrlBase* notebook = GetBookCtrl(); diff --git a/src/common/utilscmn.cpp b/src/common/utilscmn.cpp index fdda0694d9..b79b0847e1 100644 --- a/src/common/utilscmn.cpp +++ b/src/common/utilscmn.cpp @@ -303,6 +303,140 @@ int wxGetOsVersion(int *verMaj, int *verMin) return info.os; } +/* + * Class to make it easier to specify platform-dependent values + */ + +wxArrayInt* wxPlatform::sm_customPlatforms = NULL; + +wxPlatform& wxPlatform::Is(int platform, long value) +{ + if (wxPlatformIs(platform)) + m_longValue = value; + return *this; +} + +wxPlatform& wxPlatform::IsNot(int platform, long value) +{ + if (!wxPlatformIs(platform)) + m_longValue = value; + return *this; +} + +wxPlatform& wxPlatform::Is(int platform, double value) +{ + if (wxPlatformIs(platform)) + m_doubleValue = value; + return *this; +} + +wxPlatform& wxPlatform::IsNot(int platform, double value) +{ + if (!wxPlatformIs(platform)) + m_doubleValue = value; + return *this; +} + +wxPlatform& wxPlatform::Is(int platform, const wxString& value) +{ + if (wxPlatformIs(platform)) + m_stringValue = value; + return *this; +} + +wxPlatform& wxPlatform::IsNot(int platform, const wxString& value) +{ + if (!wxPlatformIs(platform)) + m_stringValue = value; + return *this; +} + +wxPlatform& wxPlatform::Default(long value) +{ + m_longValue = value; + return *this; +} + +wxPlatform& wxPlatform::Default(double value) +{ + m_doubleValue = value; + return *this; +} + +wxPlatform& wxPlatform::Default(const wxString& value) +{ + m_stringValue = value; + return *this; +} + +void wxPlatform::AddPlatform(int platform) +{ + if (!sm_customPlatforms) + sm_customPlatforms = new wxArrayInt; + sm_customPlatforms->Add(platform); +} + +void wxPlatform::ClearPlatforms() +{ + delete sm_customPlatforms; + sm_customPlatforms = NULL; +} + +/// Function for testing current platform + +bool wxPlatform::PlatformIs(int platform) +{ +#ifdef __WXMSW__ + if (platform == wxMSW) + return true; +#endif +#ifdef __WXWINCE__ + if (platform == wxWinCE) + return true; +#endif +#if defined(__WXWINCE__) && defined(__POCKETPC__) + if (platform == wxWinPocketPC) + return true; +#endif +#if defined(__WXWINCE__) && defined(__SMARTPHONE__) + if (platform == wxWinSmartphone) + return true; +#endif +#ifdef __WXGTK__ + if (platform == wxGTK) + return true; +#endif +#ifdef __WXMAC__ + if (platform == wxMac) + return true; +#endif +#ifdef __WXX11__ + if (platform == wxX11) + return true; +#endif +#ifdef __UNIX__ + if (platform == wxUnix) + return true; +#endif +#ifdef __WXMGL__ + if (platform == wxMGL) + return true; +#endif +#ifdef __WXOS2__ + if (platform == wxOS2) + return true; +#endif +#ifdef __WXCOCA__ + if (platform == wxCocoa) + return true; +#endif + + if (sm_customPlatforms && sm_customPlatforms->Index(platform) != wxNOT_FOUND) + return true; + + return false; +} + // ---------------------------------------------------------------------------- // network and user id functions // ----------------------------------------------------------------------------