X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e933b5bc365e0907a4f0fd15b4c49e4c96c2b59f..0d1903dbda864780eec30efdc4e91776bdbfd21b:/src/motif/utils.cpp diff --git a/src/motif/utils.cpp b/src/motif/utils.cpp index 7f0a201f83..4700c3c329 100644 --- a/src/motif/utils.cpp +++ b/src/motif/utils.cpp @@ -4,7 +4,6 @@ // Author: Julian Smart // Modified by: // Created: 17/09/98 -// RCS-ID: $Id$ // Copyright: (c) Julian Smart // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -20,17 +19,18 @@ // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" -#ifdef __VMS -#define XtDisplay XTDISPLAY +#include "wx/utils.h" + +#ifndef WX_PRECOMP + #include "wx/app.h" + #include "wx/dcmemory.h" + #include "wx/bitmap.h" #endif -#include "wx/setup.h" -#include "wx/utils.h" #include "wx/apptrait.h" -#include "wx/app.h" -#include "wx/dcmemory.h" -#include "wx/bitmap.h" #include "wx/evtloop.h" +#include "wx/private/eventloopsourcesmanager.h" +#include "wx/motif/private/timer.h" #include @@ -42,38 +42,17 @@ #pragma message disable nosimpint #endif -#include "wx/unix/execute.h" - #include #include #include "wx/motif/private.h" -#if wxUSE_RESOURCES -#include "X11/Xresource.h" -#endif - #include "X11/Xutil.h" #ifdef __VMS__ #pragma message enable nosimpint #endif -// ---------------------------------------------------------------------------- -// private functions -// ---------------------------------------------------------------------------- - -// Yuck this is really BOTH site and platform dependent -// so we should use some other strategy! -#ifdef sun - #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults" -#else - #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults" -#endif - -#if wxUSE_RESOURCES -static char *GetIniFile (char *dest, const char *filename); -#endif // ============================================================================ // implementation @@ -98,414 +77,125 @@ void wxFlushEvents(WXDisplay* wxdisplay) } } -// ---------------------------------------------------------------------------- -// wxExecute stuff -// ---------------------------------------------------------------------------- +#if wxUSE_EVENTLOOP_SOURCE -static void xt_notify_end_process(XtPointer data, int *WXUNUSED(fid), - XtInputId *id) +extern "C" { - wxEndProcessData *proc_data = (wxEndProcessData *)data; - wxHandleProcessTermination(proc_data); - - // VZ: I think they should be the same... - wxASSERT( (int)*id == proc_data->tag ); - - XtRemoveInput(*id); -} - -int wxAddProcessCallback(wxEndProcessData *proc_data, int fd) +static +void +wxMotifInputHandler(XtPointer data, + int* WXUNUSED(fd), + XtInputId* WXUNUSED(inputId)) { - XtInputId id = XtAppAddInput((XtAppContext) wxTheApp->GetAppContext(), - fd, - (XtPointer *) XtInputReadMask, - (XtInputCallbackProc) xt_notify_end_process, - (XtPointer) proc_data); + wxEventLoopSourceHandler * const + handler = static_cast(data); - return (int)id; + handler->OnReadWaiting(); } -// ---------------------------------------------------------------------------- -// misc -// ---------------------------------------------------------------------------- - -// Emit a beeeeeep -#ifndef __EMX__ -// on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp) -void wxBell() -{ - // Use current setting for the bell - XBell (wxGlobalDisplay(), 0); } -#endif -wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo() +// This class exists just to call XtRemoveInput() in its dtor, the real work of +// dispatching events on the file descriptor to the handler is done by +// wxMotifInputHandler callback above. +class wxMotifEventLoopSource : public wxEventLoopSource { - static wxToolkitInfo info; - - info.shortName = _T("motif"); - info.name = _T("wxMotif"); -#ifdef __WXUNIVERSAL__ - info.shortName << _T("univ"); - info.name << _T("/wxUniversal"); -#endif - // FIXME TODO - // This code is WRONG!! Does NOT return the - // Motif version of the libs but the X protocol - // version! - Display *display = wxGlobalDisplay(); - if (display) +public: + wxMotifEventLoopSource(XtInputId inputId, + wxEventLoopSourceHandler *handler, + int flags) + : wxEventLoopSource(handler, flags), + m_inputId(inputId) { - info.versionMajor = ProtocolVersion (display); - info.versionMinor = ProtocolRevision (display); } - info.os = wxMOTIF_X; - return info; -} -// ---------------------------------------------------------------------------- -// Reading and writing resources (eg WIN.INI, .Xdefaults) -// ---------------------------------------------------------------------------- - -#if wxUSE_RESOURCES - -// Read $HOME for what it says is home, if not -// read $USER or $LOGNAME for user name else determine -// the Real User, then determine the Real home dir. -static char * GetIniFile (char *dest, const char *filename) -{ - char *home = NULL; - if (filename && wxIsAbsolutePath(filename)) + virtual ~wxMotifEventLoopSource() { - strcpy(dest, filename); - } - else if ((home = wxGetUserHome()) != NULL) - { - strcpy(dest, home); - if (dest[strlen(dest) - 1] != '/') - strcat (dest, "/"); - if (filename == NULL) - { - if ((filename = getenv ("XENVIRONMENT")) == NULL) - filename = ".Xdefaults"; - } - else if (*filename != '.') - strcat (dest, "."); - strcat (dest, filename); - } else - { - dest[0] = '\0'; - } - return dest; -} - -static char *GetResourcePath(char *buf, const char *name, bool create = false) -{ - if (create && wxFileExists (name) ) { - strcpy(buf, name); - return buf; // Exists so ... - } - - if (*name == '/') - strcpy(buf, name); - else { - // Put in standard place for resource files if not absolute - strcpy (buf, DEFAULT_XRESOURCE_DIR); - strcat (buf, "/"); - strcat (buf, wxFileNameFromPath (name).c_str()); + XtRemoveInput(m_inputId); } - if (create) { - // Touch the file to create it - FILE *fd = fopen (buf, "w"); - if (fd) fclose (fd); - } - return buf; -} - -/* -* We have a cache for writing different resource files, -* which will only get flushed when we call wxFlushResources(). -* Build up a list of resource databases waiting to be written. -* -*/ +private: + const XtInputId m_inputId; -wxList wxResourceCache (wxKEY_STRING); + wxDECLARE_NO_COPY_CLASS(wxMotifEventLoopSource); +}; -void -wxFlushResources (void) +class wxMotifEventLoopSourcesManager : public wxEventLoopSourcesManagerBase { - char nameBuffer[512]; - - wxNode *node = wxResourceCache.First (); - while (node) +public: + wxEventLoopSource * + AddSourceForFD(int fd, wxEventLoopSourceHandler* handler, int flags) { - const char *file = node->GetKeyString(); - // If file doesn't exist, create it first. - (void)GetResourcePath(nameBuffer, file, true); - - XrmDatabase database = (XrmDatabase) node->Data (); - XrmPutFileDatabase (database, nameBuffer); - XrmDestroyDatabase (database); - wxNode *next = node->Next (); - delete node; - node = next; - } -} - -static XrmDatabase wxResourceDatabase = 0; - -void wxXMergeDatabases (wxApp * theApp, Display * display); - -bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file) -{ - char buffer[500]; - - (void) GetIniFile (buffer, file); + wxCHECK_MSG( wxTheApp, NULL, "Must create wxTheApp first" ); + + // The XtInputXXXMask values cannot be combined (hence "Mask" is a + // complete misnomer), and supporting those would make the code more + // complicated and we don't need them for now. + wxCHECK_MSG( !(flags & (wxEVENT_SOURCE_OUTPUT | + wxEVENT_SOURCE_EXCEPTION)), + NULL, + "Monitoring FDs for output/errors not supported" ); + + wxCHECK_MSG( flags & wxEVENT_SOURCE_INPUT, + NULL, + "Should be monitoring for input" ); + + XtInputId inputId = XtAppAddInput + ( + (XtAppContext) wxTheApp->GetAppContext(), + fd, + (XtPointer) XtInputReadMask, + wxMotifInputHandler, + handler + ); + if ( inputId < 0 ) + return 0; - XrmDatabase database; - wxNode *node = wxResourceCache.Find (buffer); - if (node) - database = (XrmDatabase) node->Data (); - else - { - database = XrmGetFileDatabase (buffer); - wxResourceCache.Append (buffer, (wxObject *) database); + return new wxMotifEventLoopSource(inputId, handler, flags); } +}; - char resName[300]; - strcpy (resName, section.c_str()); - strcat (resName, "."); - strcat (resName, entry.c_str()); - - XrmPutStringResource (&database, resName, value); - return true; -} - -bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file) +wxEventLoopSourcesManagerBase* wxGUIAppTraits::GetEventLoopSourcesManager() { - char buf[50]; - sprintf(buf, "%.4f", value); - return wxWriteResource(section, entry, buf, file); -} + static wxMotifEventLoopSourcesManager s_eventLoopSourcesManager; -bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file) -{ - char buf[50]; - sprintf(buf, "%ld", value); - return wxWriteResource(section, entry, buf, file); -} - -bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file) -{ - char buf[50]; - sprintf(buf, "%d", value); - return wxWriteResource(section, entry, buf, file); + return &s_eventLoopSourcesManager; } -bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file) -{ - if (!wxResourceDatabase) - { - Display *display = wxGlobalDisplay(); - wxXMergeDatabases (wxTheApp, display); - } - - XrmDatabase database; +#endif // wxUSE_EVENTLOOP_SOURCE - if (!file.empty()) - { - char buffer[500]; - - // Is this right? Trying to get it to look in the user's - // home directory instead of current directory -- JACS - (void) GetIniFile (buffer, file); - - wxNode *node = wxResourceCache.Find (buffer); - if (node) - database = (XrmDatabase) node->Data (); - else - { - database = XrmGetFileDatabase (buffer); - wxResourceCache.Append (buffer, (wxObject *) database); - } - } - else - database = wxResourceDatabase; - - XrmValue xvalue; - char *str_type[20]; - char buf[150]; - strcpy (buf, section); - strcat (buf, "."); - strcat (buf, entry); - - Bool success = XrmGetResource (database, buf, "*", str_type, - &xvalue); - // Try different combinations of upper/lower case, just in case... - if (!success) - { - buf[0] = (isupper (buf[0]) ? tolower (buf[0]) : toupper (buf[0])); - success = XrmGetResource (database, buf, "*", str_type, - &xvalue); - } - if (success) - { - if (*value) - delete[] *value; - - *value = new char[xvalue.size + 1]; - strncpy (*value, xvalue.addr, (int) xvalue.size); - return true; - } - return false; -} +// ---------------------------------------------------------------------------- +// misc +// ---------------------------------------------------------------------------- -bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file) +// Emit a beeeeeep +void wxBell() { - char *s = NULL; - bool succ = wxGetResource(section, entry, (char **)&s, file); - if (succ) - { - *value = (float)strtod(s, NULL); - delete[] s; - return true; - } - else return false; + // Use current setting for the bell + XBell (wxGlobalDisplay(), 0); } -bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file) +wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const { - char *s = NULL; - bool succ = wxGetResource(section, entry, (char **)&s, file); - if (succ) - { - *value = strtol(s, NULL, 10); - delete[] s; - return true; - } - else return false; -} + // XmVERSION and XmREVISION are defined in Xm/Xm.h + if ( verMaj ) + *verMaj = XmVERSION; + if ( verMin ) + *verMin = XmREVISION; -bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file) -{ - char *s = NULL; - bool succ = wxGetResource(section, entry, (char **)&s, file); - if (succ) - { - // Handle True, False here - // True, Yes, Enables, Set or Activated - if (*s == 'T' || *s == 'Y' || *s == 'E' || *s == 'S' || *s == 'A') - *value = true; - // False, No, Disabled, Reset, Cleared, Deactivated - else if (*s == 'F' || *s == 'N' || *s == 'D' || *s == 'R' || *s == 'C') - *value = false; - // Handle as Integer - else - *value = (int) strtol (s, NULL, 10); - delete[] s; - return true; - } - else - return false; + return wxPORT_MOTIF; } -void wxXMergeDatabases (wxApp * theApp, Display * display) +wxEventLoopBase* wxGUIAppTraits::CreateEventLoop() { - XrmDatabase homeDB, serverDB, applicationDB; - char filenamebuf[1024]; - - char *filename = &filenamebuf[0]; - char *environment; - wxString classname = theApp->GetClassName(); - char name[256]; - (void) strcpy (name, "/usr/lib/X11/app-defaults/"); - (void) strcat (name, classname.c_str()); - - /* Get application defaults file, if any */ - applicationDB = XrmGetFileDatabase (name); - (void) XrmMergeDatabases (applicationDB, &wxResourceDatabase); - - /* Merge server defaults, created by xrdb, loaded as a property of the root - * window when the server initializes and loaded into the display - * structure on XOpenDisplay; - * if not defined, use .Xdefaults - */ - - if (XResourceManagerString (display) != NULL) - { - serverDB = XrmGetStringDatabase (XResourceManagerString (display)); - } - else - { - (void) GetIniFile (filename, NULL); - serverDB = XrmGetFileDatabase (filename); - } - XrmMergeDatabases (serverDB, &wxResourceDatabase); - - /* Open XENVIRONMENT file, or if not defined, the .Xdefaults, - * and merge into existing database - */ - - if ((environment = getenv ("XENVIRONMENT")) == NULL) - { - size_t len; - environment = GetIniFile (filename, NULL); - len = strlen (environment); - wxString hostname = wxGetHostName(); - if ( !hostname.empty() ) - strncat(environment, hostname, 1024 - len); - } - homeDB = XrmGetFileDatabase (environment); - XrmMergeDatabases (homeDB, &wxResourceDatabase); + return new wxEventLoop; } -#if 0 - -/* -* Not yet used but may be useful. -* -*/ -void -wxSetDefaultResources (const Widget w, const char **resourceSpec, const char *name) +wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer* timer) { - int i; - Display *dpy = XtDisplay (w); // Retrieve the display pointer - - XrmDatabase rdb = NULL; // A resource data base - - // Create an empty resource database - rdb = XrmGetStringDatabase (""); - - // Add the Component resources, prepending the name of the component - - i = 0; - while (resourceSpec[i] != NULL) - { - char buf[1000]; - - sprintf (buf, "*%s%s", name, resourceSpec[i++]); - XrmPutLineResource (&rdb, buf); - } - - // Merge them into the Xt database, with lowest precendence - - if (rdb) - { -#if (XlibSpecificationRelease>=5) - XrmDatabase db = XtDatabase (dpy); - XrmCombineDatabase (rdb, &db, False); -#else - XrmMergeDatabases (dpy->db, &rdb); - dpy->db = rdb; -#endif - } + return new wxMotifTimerImpl(timer); } -#endif -// 0 - -#endif // wxUSE_RESOURCES // ---------------------------------------------------------------------------- // display info @@ -566,18 +256,6 @@ void wxDisplaySizeMM(int *width, int *height) *height = DisplayHeightMM(dpy, DefaultScreen (dpy)); } -void wxClientDisplayRect(int *x, int *y, int *width, int *height) -{ - // This is supposed to return desktop dimensions minus any window - // manager panels, menus, taskbars, etc. If there is a way to do that - // for this platform please fix this function, otherwise it defaults - // to the entire desktop. - if (x) *x = 0; - if (y) *y = 0; - wxDisplaySize(width, height); -} - - // Configurable display in wxX11 and wxMotif static WXDisplay *gs_currentDisplay = NULL; static wxString gs_displayName; @@ -747,12 +425,11 @@ void wxAllocColor(Display *d,Colormap cmp,XColor *xc) { if (!XAllocColor(d,cmp,xc)) { - // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n"; + // cout << "wxAllocColor : Warning : cannot allocate color, attempt find nearest !\n"; wxAllocNearestColor(d,cmp,xc); } } -#ifdef __WXDEBUG__ wxString wxGetXEventName(XEvent& event) { #if wxUSE_NANOX @@ -778,7 +455,6 @@ wxString wxGetXEventName(XEvent& event) return str; #endif } -#endif // ---------------------------------------------------------------------------- // accelerators @@ -896,6 +572,9 @@ XmString wxFindAcceleratorText (const char *s) // Change a widget's foreground and background colours. void wxDoChangeForegroundColour(WXWidget widget, wxColour& foregroundColour) { + if (!foregroundColour.IsOk()) + return; + // When should we specify the foreground, if it's calculated // by wxComputeColours? // Solution: say we start with the default (computed) foreground colour. @@ -910,10 +589,13 @@ void wxDoChangeForegroundColour(WXWidget widget, wxColour& foregroundColour) NULL); } -void wxDoChangeBackgroundColour(WXWidget widget, wxColour& backgroundColour, bool changeArmColour) +void wxDoChangeBackgroundColour(WXWidget widget, const wxColour& backgroundColour, bool changeArmColour) { + if (!backgroundColour.IsOk()) + return; + wxComputeColours (XtDisplay((Widget) widget), & backgroundColour, - (wxColour*) NULL); + NULL); XtVaSetValues ((Widget) widget, XmNbackground, g_itemColors[wxBACK_INDEX].pixel, @@ -956,11 +638,6 @@ wxString wxXmStringToString( const XmString& xmString ) return wxEmptyString; } -XmString wxStringToXmString( const wxString& str ) -{ - return XmStringCreateLtoR((char *)str.c_str(), XmSTRING_DEFAULT_CHARSET); -} - XmString wxStringToXmString( const char* str ) { return XmStringCreateLtoR((char *)str, XmSTRING_DEFAULT_CHARSET); @@ -980,7 +657,7 @@ wxBitmap wxCreateMaskedBitmap(const wxBitmap& bitmap, const wxColour& colour) wxMemoryDC destDC; wxMemoryDC srcDC; - srcDC.SelectObject(bitmap); + srcDC.SelectObjectAsSource(bitmap); destDC.SelectObject(newBitmap); wxBrush brush(colour, wxSOLID); @@ -1011,7 +688,7 @@ WXWidget wxCreateBorderWidget( WXWidget parent, long style ) NULL ); } - else if (style & wxSUNKEN_BORDER) + else if ((style & wxSUNKEN_BORDER) || (style & wxBORDER_THEME)) { borderWidget = XtVaCreateManagedWidget (