1/* 2 * Copyright (C) 2009 Kevin Ollivier All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26#include "config.h" 27 28#include "scrollbar_render.h" 29 30#include <wx/defs.h> 31#include <wx/dc.h> 32#include <wx/dcgraph.h> 33#include <wx/renderer.h> 34#include <wx/settings.h> 35#include <wx/window.h> 36 37#include <gtk/gtk.h> 38 39#if wxCHECK_VERSION(2, 9, 0) 40 #include <wx/gtk/dc.h> 41#else 42 #include "wx/gtk/win_gtk.h" 43#endif 44 45int wxStyleForPart(wxScrollbarPart part, wxScrollbarPart focusPart, wxScrollbarPart hoverPart, int flags) 46{ 47 int style = 0; 48 if (flags == wxCONTROL_DISABLED) 49 style = wxCONTROL_DISABLED; 50 else if (part == focusPart) 51 style = wxCONTROL_PRESSED; 52 else if (part == hoverPart) 53 style = wxCONTROL_CURRENT; 54 55 return style; 56} 57 58GtkWidget* GetButtonWidget() 59{ 60 static GtkWidget *s_button = NULL; 61 static GtkWidget *s_window = NULL; 62 63 if ( !s_button ) 64 { 65 s_window = gtk_window_new( GTK_WINDOW_POPUP ); 66 gtk_widget_realize( s_window ); 67 s_button = gtk_button_new(); 68 gtk_container_add( GTK_CONTAINER(s_window), s_button ); 69 gtk_widget_realize( s_button ); 70 } 71 72 return s_button; 73} 74 75 76GdkWindow* wxGetGdkWindowForDC(wxWindow* win, wxDC& dc) 77{ 78 GdkWindow* gdk_window = NULL; 79#if wxCHECK_VERSION(2, 9, 0) 80 if ( dc.IsKindOf( CLASSINFO(wxGCDC) ) ) 81 gdk_window = win->GTKGetDrawingWindow(); 82 else 83 { 84 wxGTKDCImpl *impl = wxDynamicCast(dc.GetImpl(), wxGTKDCImpl); 85 if ( impl ) 86 gdk_window = impl->GetGDKWindow(); 87 } 88#else // wx < 2.9 89 // The way to get a GdkWindow* from a wxWindow is to use 90 // GTK_PIZZA(win->m_wxwindow)->bin_window, but this approach 91 // won't work when drawing to a wxMemoryDC as it has its own 92 // GdkWindow* for its bitmap. wxWindowDC's GetGDKWindow() was 93 // designed to create a solution for all DCs, but we can't 94 // implement it with wxGCDC since it doesn't retain its wxWindow. 95 // So, to work around this, we use GetGDKWindow whenever possible 96 // and use bin_window for wxGCDC. 97#if wxUSE_GRAPHICS_CONTEXT 98 if ( dc.IsKindOf( CLASSINFO(wxGCDC) ) ) 99 gdk_window = GTK_PIZZA(win->m_wxwindow)->bin_window; 100 else 101#endif 102 gdk_window = dc.GetGDKWindow(); 103 wxASSERT_MSG( gdk_window, 104 wxT("cannot use wxRendererNative on wxDC of this type") ); 105#endif // wx 2.9/2.8 106 107 return gdk_window; 108} 109 110void wxRenderer_DrawScrollbar(wxWindow* window, wxDC& dc, const wxRect& rect, wxOrientation orient, 111 int current, wxScrollbarPart focusPart, wxScrollbarPart hoverPart, int max, int step, int flags) 112{ 113 bool horiz = orient == wxHORIZONTAL; 114 wxColour scrollbar_colour = wxSystemSettings::GetColour(wxSYS_COLOUR_SCROLLBAR); 115 dc.SetBrush(wxBrush(scrollbar_colour)); 116 dc.SetPen(wxPen(wxSystemSettings::GetColour(wxSYS_COLOUR_BTNSHADOW))); 117 dc.DrawRectangle(rect.x, rect.y, rect.width, rect.height); 118 119 // when going from Cairo -> Gdk, any Cairo context transformations are lost 120 // so we need to alter the coordinates to reflect their transformed point. 121 double xtrans = 0; 122 double ytrans = 0; 123 124 wxGCDC* gcdc = wxDynamicCast(&dc, wxGCDC); 125 wxGraphicsContext* gc = gcdc->GetGraphicsContext(); 126 gc->GetTransform().TransformPoint(&xtrans, &ytrans); 127 128 wxRendererNative& renderer = wxRendererNative::Get(); 129 int x = rect.x + (int)xtrans; 130 int y = rect.y + (int)ytrans; 131 132 int buttonLength = 16; 133 134 renderer.DrawPushButton(window, dc, wxRect(x,y,buttonLength,buttonLength), wxStyleForPart(wxSCROLLPART_BACKBTNSTART, focusPart, hoverPart, flags)); 135 136 GtkWidget* button = GetButtonWidget(); 137 GdkWindow* gdk_window = wxGetGdkWindowForDC(window, dc); 138 139 GtkArrowType arrowType = GTK_ARROW_UP; 140 if (horiz) 141 arrowType = GTK_ARROW_LEFT; 142 143 gtk_paint_arrow( button->style, gdk_window, GTK_STATE_NORMAL, GTK_SHADOW_OUT, NULL, button, "arrow", arrowType, false, x + 4, y + 4, 8, 8); 144 145 wxRect buttonRect = rect; 146 int physicalLength = horiz ? rect.width : rect.height; 147 physicalLength -= buttonLength*2; 148 int thumbStart = 0; 149 int thumbLength = 0; 150 calcThumbStartAndLength(physicalLength, max, current, step, &thumbStart, &thumbLength); 151 152 if (horiz) { 153 buttonRect.x = x + thumbStart + buttonLength; 154 buttonRect.y = y; 155 buttonRect.width = thumbLength; 156 } else { 157 buttonRect.x = x; 158 buttonRect.y = y + thumbStart + buttonLength; 159 buttonRect.height = thumbLength; 160 } 161 162 renderer.DrawPushButton(window, dc, buttonRect, wxStyleForPart(wxSCROLLPART_THUMB, focusPart, hoverPart, flags)); 163 164 if (horiz) 165 x += rect.width - buttonLength; 166 else 167 y += rect.height - buttonLength; 168 169 renderer.DrawPushButton(window, dc, wxRect(x,y,buttonLength,buttonLength), wxStyleForPart(wxSCROLLPART_FWDBTNEND, focusPart, hoverPart, flags)); 170 171 if (horiz) 172 arrowType = GTK_ARROW_RIGHT; 173 else 174 arrowType = GTK_ARROW_DOWN; 175 176 gtk_paint_arrow( button->style, gdk_window, GTK_STATE_NORMAL, GTK_SHADOW_OUT, NULL, button, "arrow", arrowType, false, x + 4, y + 4, 8, 8); 177} 178