+ int direction, ascent, descent;
+ XCharStruct overall;
+ XTextExtents ((XFontStruct*) pFontStruct, "x", 1, &direction, &ascent,
+ &descent, &overall);
+
+ // return (overall.ascent + overall.descent);
+ return (ascent + descent);
+}
+
+int wxWindow::GetCharWidth() const
+{
+ wxCHECK_MSG( m_font.Ok(), 0, "valid window font needed" );
+
+ WXFontStructPtr pFontStruct = m_font.GetFontStruct(1.0, GetXDisplay());
+
+ int direction, ascent, descent;
+ XCharStruct overall;
+ XTextExtents ((XFontStruct*) pFontStruct, "x", 1, &direction, &ascent,
+ &descent, &overall);
+
+ return overall.width;
+}
+
+void wxWindow::GetTextExtent(const wxString& string,
+ int *x, int *y,
+ int *descent, int *externalLeading,
+ const wxFont *theFont) const
+{
+ wxFont *fontToUse = (wxFont *)theFont;
+ if (!fontToUse)
+ fontToUse = (wxFont *) & m_font;
+
+ wxCHECK_RET( fontToUse->Ok(), "valid window font needed" );
+
+ WXFontStructPtr pFontStruct = theFont->GetFontStruct(1.0, GetXDisplay());
+
+ int direction, ascent, descent2;
+ XCharStruct overall;
+ int slen = string.Len();
+
+#if 0
+ if (use16)
+ XTextExtents16((XFontStruct*) pFontStruct, (XChar2b *) (char*) (const char*) string, slen, &direction,
+ &ascent, &descent2, &overall);
+#endif
+
+ XTextExtents((XFontStruct*) pFontStruct, string, slen,
+ &direction, &ascent, &descent2, &overall);
+
+ if ( x )
+ *x = (overall.width);
+ if ( y )
+ *y = (ascent + descent2);
+ if (descent)
+ *descent = descent2;
+ if (externalLeading)
+ *externalLeading = 0;
+
+}
+
+// ----------------------------------------------------------------------------
+// painting
+// ----------------------------------------------------------------------------
+
+void wxWindow::Refresh(bool eraseBack, const wxRect *rect)
+{
+ m_needsRefresh = TRUE;
+ Display *display = XtDisplay((Widget) GetMainWidget());
+ Window thisWindow = XtWindow((Widget) GetMainWidget());
+
+ XExposeEvent dummyEvent;
+ int width, height;
+ GetSize(&width, &height);
+
+ dummyEvent.type = Expose;
+ dummyEvent.display = display;
+ dummyEvent.send_event = True;
+ dummyEvent.window = thisWindow;
+ if (rect)
+ {
+ dummyEvent.x = rect->x;
+ dummyEvent.y = rect->y;
+ dummyEvent.width = rect->width;
+ dummyEvent.height = rect->height;
+ }
+ else
+ {
+ dummyEvent.x = 0;
+ dummyEvent.y = 0;
+ dummyEvent.width = width;
+ dummyEvent.height = height;
+ }
+ dummyEvent.count = 0;
+
+ if (eraseBack)
+ {
+ wxClientDC dc(this);
+ wxBrush backgroundBrush(GetBackgroundColour(), wxSOLID);
+ dc.SetBackground(backgroundBrush);
+ if (rect)
+ dc.Clear(*rect);
+ else
+ dc.Clear();
+ }
+
+ XSendEvent(display, thisWindow, False, ExposureMask, (XEvent *)&dummyEvent);
+}
+
+void wxWindow::Clear()
+{
+ wxClientDC dc(this);
+ wxBrush brush(GetBackgroundColour(), wxSOLID);
+ dc.SetBackground(brush);
+ dc.Clear();
+}
+
+void wxWindow::ClearUpdateRects()
+{
+ wxRectList::Node* node = m_updateRects.GetFirst();
+ while (node)
+ {
+ wxRect* rect = node->GetData();
+ delete rect;
+ node = node->GetNext();
+ }
+
+ m_updateRects.Clear();
+}
+
+void wxWindow::DoPaint()
+{
+ //TODO : make a temporary gc so we can do the XCopyArea below
+ if (m_backingPixmap && !m_needsRefresh)
+ {
+ wxPaintDC dc(this);
+
+ GC tempGC = (GC) dc.GetBackingGC();
+
+ Widget widget = (Widget) GetMainWidget();
+
+ int scrollPosX = 0;
+ int scrollPosY = 0;
+
+ // We have to test whether it's a wxScrolledWindow (hack!) because
+ // otherwise we don't know how many pixels have been scrolled. We might
+ // solve this in the future by defining virtual wxWindow functions to get
+ // the scroll position in pixels. Or, each kind of scrolled window has to
+ // implement backing stores itself, using generic wxWindows code.
+ wxScrolledWindow* scrolledWindow = wxDynamicCast(this, wxScrolledWindow);
+ if ( scrolledWindow )
+ {
+ int x, y;
+ scrolledWindow->CalcScrolledPosition(0, 0, &x, &y);
+
+ scrollPosX = - x;
+ scrollPosY = - y;
+ }
+
+ // TODO: This could be optimized further by only copying the areas in the
+ // current update region.
+
+ // Only blit the part visible in the client area. The backing pixmap
+ // always starts at 0, 0 but we may be looking at only a portion of it.
+ wxSize clientArea = GetClientSize();
+ int toBlitX = m_pixmapWidth - scrollPosX;
+ int toBlitY = m_pixmapHeight - scrollPosY;
+
+ // Copy whichever is samller, the amount of pixmap we have to copy,
+ // or the size of the client area.
+ toBlitX = wxMin(toBlitX, clientArea.x);
+ toBlitY = wxMin(toBlitY, clientArea.y);
+
+ // Make sure we're not negative
+ toBlitX = wxMax(0, toBlitX);
+ toBlitY = wxMax(0, toBlitY);
+
+ XCopyArea
+ (
+ XtDisplay(widget),
+ (Pixmap) m_backingPixmap,
+ XtWindow (widget),
+ tempGC,
+ scrollPosX, scrollPosY, // Start at the scroll position
+ toBlitX, toBlitY, // How much of the pixmap to copy
+ 0, 0 // Destination
+ );
+ }
+ else
+ {
+ // Set an erase event first
+ wxEraseEvent eraseEvent(GetId());
+ eraseEvent.SetEventObject(this);
+ GetEventHandler()->ProcessEvent(eraseEvent);
+
+ wxPaintEvent event(GetId());
+ event.SetEventObject(this);
+ GetEventHandler()->ProcessEvent(event);
+
+ m_needsRefresh = FALSE;
+ }
+}
+
+// ----------------------------------------------------------------------------
+// event handlers
+// ----------------------------------------------------------------------------
+
+// Responds to colour changes: passes event on to children.
+void wxWindow::OnSysColourChanged(wxSysColourChangedEvent& event)
+{
+ wxWindowList::Node *node = GetChildren().GetFirst();
+ while ( node )
+ {
+ // Only propagate to non-top-level windows
+ wxWindow *win = node->GetData();
+ if ( win->GetParent() )
+ {
+ wxSysColourChangedEvent event2;
+ event.m_eventObject = win;
+ win->GetEventHandler()->ProcessEvent(event2);
+ }
+
+ node = node->GetNext();
+ }
+}
+
+void wxWindow::OnIdle(wxIdleEvent& WXUNUSED(event))
+{
+ // This calls the UI-update mechanism (querying windows for
+ // menu/toolbar/control state information)
+ UpdateWindowUI();
+}
+
+// ----------------------------------------------------------------------------
+// accelerators
+// ----------------------------------------------------------------------------
+
+bool wxWindow::ProcessAccelerator(wxKeyEvent& event)
+{
+ if (!m_acceleratorTable.Ok())
+ return FALSE;
+
+ int count = m_acceleratorTable.GetCount();
+ wxAcceleratorEntry* entries = m_acceleratorTable.GetEntries();
+ int i;
+ for (i = 0; i < count; i++)
+ {
+ wxAcceleratorEntry* entry = & (entries[i]);
+ if (entry->MatchesEvent(event))
+ {
+ // Bingo, we have a match. Now find a control that matches the
+ // entry command id.
+
+ // Need to go up to the top of the window hierarchy, since it might
+ // be e.g. a menu item
+ wxWindow* parent = this;
+ while ( parent && !parent->IsTopLevel() )
+ parent = parent->GetParent();
+
+ if (!parent)
+ return FALSE;
+
+ wxFrame* frame = wxDynamicCast(parent, wxFrame);
+ if ( frame )
+ {
+ // Try for a menu command
+ if (frame->GetMenuBar())
+ {
+ wxMenuItem* item = frame->GetMenuBar()->FindItem(entry->GetCommand());
+ if (item)
+ {
+ wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, entry->GetCommand());
+ commandEvent.SetEventObject(frame);
+
+ // If ProcessEvent returns TRUE (it was handled), then
+ // the calling code will skip the event handling.
+ return frame->GetEventHandler()->ProcessEvent(commandEvent);
+ }
+ }
+ }
+
+ // Find a child matching the command id
+ wxWindow* child = parent->FindWindow(entry->GetCommand());
+
+ // No such child
+ if (!child)
+ return FALSE;
+
+ // Now we process those kinds of windows that we can.
+ // For now, only buttons.
+ if ( wxDynamicCast(child, wxButton) )
+ {
+ wxCommandEvent commandEvent (wxEVT_COMMAND_BUTTON_CLICKED, child->GetId());
+ commandEvent.SetEventObject(child);
+ return child->GetEventHandler()->ProcessEvent(commandEvent);
+ }
+
+ return FALSE;
+ } // matches event
+ }// for
+
+ // We didn't match the key event against an accelerator.
+ return FALSE;
+}
+
+// ============================================================================
+// Motif-specific stuff from here on
+// ============================================================================
+
+// ----------------------------------------------------------------------------
+// function which maintain the global hash table mapping Widgets to wxWindows
+// ----------------------------------------------------------------------------
+
+bool wxAddWindowToTable(Widget w, wxWindow *win)
+{
+ wxWindow *oldItem = NULL;
+ if ((oldItem = (wxWindow *)wxWidgetHashTable->Get ((long) w)))
+ {
+ wxLogDebug("Widget table clash: new widget is %ld, %s",
+ (long)w, win->GetClassInfo()->GetClassName());
+ return FALSE;
+ }
+
+ wxWidgetHashTable->Put((long) w, win);
+
+ wxLogTrace("widget", "Widget 0x%08x <-> window %p (%s)",
+ w, win, win->GetClassInfo()->GetClassName());
+
+ return TRUE;
+}
+
+wxWindow *wxGetWindowFromTable(Widget w)
+{
+ return (wxWindow *)wxWidgetHashTable->Get((long) w);
+}
+
+void wxDeleteWindowFromTable(Widget w)
+{
+ wxWidgetHashTable->Delete((long)w);
+}
+
+// ----------------------------------------------------------------------------
+// add/remove window from the table
+// ----------------------------------------------------------------------------
+
+// Add to hash table, add event handler
+bool wxWindow::AttachWidget (wxWindow* WXUNUSED(parent), WXWidget mainWidget,
+ WXWidget formWidget, int x, int y, int width, int height)
+{
+ wxAddWindowToTable((Widget) mainWidget, this);
+ if (CanAddEventHandler())
+ {
+ XtAddEventHandler((Widget) mainWidget,
+ ButtonPressMask | ButtonReleaseMask | PointerMotionMask, // | KeyPressMask,
+ False,
+ wxPanelItemEventHandler,
+ (XtPointer) this);
+ }
+
+ if (!formWidget)
+ {
+ XtTranslations ptr;
+ XtOverrideTranslations ((Widget) mainWidget,
+ ptr = XtParseTranslationTable ("<Configure>: resize()"));
+ XtFree ((char *) ptr);
+ }
+
+ // Some widgets have a parent form widget, e.g. wxRadioBox
+ if (formWidget)
+ {
+ if (!wxAddWindowToTable((Widget) formWidget, this))
+ return FALSE;
+
+ XtTranslations ptr;
+ XtOverrideTranslations ((Widget) formWidget,
+ ptr = XtParseTranslationTable ("<Configure>: resize()"));
+ XtFree ((char *) ptr);
+ }
+
+ if (x == -1)
+ x = 0;
+ if (y == -1)
+ y = 0;
+ SetSize (x, y, width, height);
+
+ return TRUE;
+}
+
+// Remove event handler, remove from hash table
+bool wxWindow::DetachWidget(WXWidget widget)
+{
+ if (CanAddEventHandler())
+ {
+ XtRemoveEventHandler((Widget) widget,
+ ButtonPressMask | ButtonReleaseMask | PointerMotionMask, // | KeyPressMask,
+ False,
+ wxPanelItemEventHandler,
+ (XtPointer)this);
+ }
+
+ wxDeleteWindowFromTable((Widget) widget);
+ return TRUE;
+}
+
+// ----------------------------------------------------------------------------
+// Motif-specific accessors
+// ----------------------------------------------------------------------------
+
+// Get the underlying X window
+WXWindow wxWindow::GetXWindow() const
+{
+ Widget wMain = (Widget)GetMainWidget();
+ if ( wMain )
+ return (WXWindow) XtWindow(wMain);
+ else
+ return (WXWindow) 0;
+}