]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/renderer.cpp
don't overwrite the existing local file if we failed to open it but it does exist
[wxWidgets.git] / src / gtk1 / renderer.cpp
CommitLineData
9c7f49f5 1///////////////////////////////////////////////////////////////////////////////
cdccdfab 2// Name: src/gtk1/renderer.cpp
38c4cb6a 3// Purpose: implementation of wxRendererNative for wxGTK
9c7f49f5
VZ
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 20.07.2003
7// RCS-ID: $Id$
8// Copyright: (c) 2003 Vadim Zeitlin <vadim@wxwindows.org>
65571936 9// License: wxWindows licence
9c7f49f5
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// for compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
e1bf3ad3 27#include "wx/renderer.h"
cdccdfab
WS
28
29#ifndef WX_PRECOMP
30 #include "wx/window.h"
da80ae71 31 #include "wx/dc.h"
ed4b0fdc 32 #include "wx/dcclient.h"
cdccdfab
WS
33#endif
34
9c7f49f5 35#include <gtk/gtk.h>
3cbab641 36#include "wx/gtk1/win_gtk.h"
9c7f49f5 37
91af0895
WS
38// RR: After a correction to the orientation of the sash
39// this doesn't seem to be required anymore and it
40// seems to confuse some themes so USE_ERASE_RECT=0
41#define USE_ERASE_RECT 0
42
9c7f49f5 43// ----------------------------------------------------------------------------
38c4cb6a 44// wxRendererGTK: our wxRendererNative implementation
9c7f49f5
VZ
45// ----------------------------------------------------------------------------
46
38c4cb6a 47class WXDLLEXPORT wxRendererGTK : public wxDelegateRendererNative
9c7f49f5
VZ
48{
49public:
50 // draw the header control button (used by wxListCtrl)
51 virtual void DrawHeaderButton(wxWindow *win,
52 wxDC& dc,
53 const wxRect& rect,
54 int flags = 0);
55
d16cf3cd
VZ
56 virtual void DrawSplitterBorder(wxWindow *win,
57 wxDC& dc,
af99040c
VZ
58 const wxRect& rect,
59 int flags = 0);
95155e75
VZ
60 virtual void DrawSplitterSash(wxWindow *win,
61 wxDC& dc,
62 const wxSize& size,
d16cf3cd 63 wxCoord position,
af99040c
VZ
64 wxOrientation orient,
65 int flags = 0);
d16cf3cd 66
38511687
VZ
67 virtual void DrawComboBoxDropButton(wxWindow *win,
68 wxDC& dc,
69 const wxRect& rect,
70 int flags = 0);
71
4c85ab75
VZ
72 virtual void DrawDropArrow(wxWindow *win,
73 wxDC& dc,
74 const wxRect& rect,
75 int flags = 0);
76
af99040c 77 virtual wxSplitterRenderParams GetSplitterParams(const wxWindow *win);
e1befae3
VZ
78
79private:
bc13e772
VZ
80 // FIXME: shouldn't we destroy these windows somewhere?
81
e1befae3 82 // used by DrawHeaderButton and DrawComboBoxDropButton
bc13e772 83 static GtkWidget *GetButtonWidget();
9c7f49f5
VZ
84};
85
86// ============================================================================
87// implementation
88// ============================================================================
89
90/* static */
f0244295 91wxRendererNative& wxRendererNative::GetDefault()
9c7f49f5
VZ
92{
93 static wxRendererGTK s_rendererGTK;
94
95 return s_rendererGTK;
96}
97
a4622f29 98// ----------------------------------------------------------------------------
bc13e772 99// helper functions
a4622f29
VZ
100// ----------------------------------------------------------------------------
101
bc13e772
VZ
102GtkWidget *
103wxRendererGTK::GetButtonWidget()
104{
105 static GtkWidget *s_button = NULL;
106 static GtkWidget *s_window = NULL;
a4622f29 107
bc13e772
VZ
108 if ( !s_button )
109 {
110 s_window = gtk_window_new( GTK_WINDOW_POPUP );
111 gtk_widget_realize( s_window );
112 s_button = gtk_button_new();
113 gtk_container_add( GTK_CONTAINER(s_window), s_button );
114 gtk_widget_realize( s_button );
115 }
116
117 return s_button;
118}
119
d16cf3cd
VZ
120// ----------------------------------------------------------------------------
121// list/tree controls drawing
122// ----------------------------------------------------------------------------
123
9c7f49f5
VZ
124void
125wxRendererGTK::DrawHeaderButton(wxWindow *win,
126 wxDC& dc,
127 const wxRect& rect,
128 int flags)
129{
9b311923 130
bc13e772 131 GtkWidget *button = GetButtonWidget();
9b311923 132
9c7f49f5
VZ
133 gtk_paint_box
134 (
bc13e772 135 button->style,
a4622f29
VZ
136 // FIXME: I suppose GTK_PIZZA(win->m_wxwindow)->bin_window doesn't work with wxMemoryDC.
137 // Maybe use code similar as in DrawComboBoxDropButton below?
9c7f49f5
VZ
138 GTK_PIZZA(win->m_wxwindow)->bin_window,
139 flags & wxCONTROL_DISABLED ? GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL,
140 GTK_SHADOW_OUT,
38511687 141 NULL,
bc13e772 142 button,
9b311923
RR
143 "button",
144 dc.XLOG2DEV(rect.x) -1, rect.y -1, rect.width +2, rect.height +2
9c7f49f5
VZ
145 );
146}
147
d16cf3cd
VZ
148// ----------------------------------------------------------------------------
149// splitter sash drawing
150// ----------------------------------------------------------------------------
151
3cbab641
MR
152// the full sash width (should be even)
153static const wxCoord SASH_SIZE = 8;
af99040c 154
3cbab641
MR
155// margin around the sash
156static const wxCoord SASH_MARGIN = 2;
d16cf3cd 157
38418827
RR
158static int GetGtkSplitterFullSize()
159{
38418827 160 return SASH_SIZE + SASH_MARGIN;
38418827
RR
161}
162
af99040c 163wxSplitterRenderParams
38418827 164wxRendererGTK::GetSplitterParams(const wxWindow *WXUNUSED(win))
d16cf3cd 165{
af99040c
VZ
166 // we don't draw any border, hence 0 for the second field
167 return wxSplitterRenderParams
168 (
38418827 169 GetGtkSplitterFullSize(),
af99040c 170 0,
af99040c 171 false // not
af99040c 172 );
d16cf3cd
VZ
173}
174
175void
176wxRendererGTK::DrawSplitterBorder(wxWindow * WXUNUSED(win),
177 wxDC& WXUNUSED(dc),
af99040c
VZ
178 const wxRect& WXUNUSED(rect),
179 int WXUNUSED(flags))
d16cf3cd
VZ
180{
181 // nothing to do
182}
95155e75 183
95155e75
VZ
184void
185wxRendererGTK::DrawSplitterSash(wxWindow *win,
186 wxDC& dc,
187 const wxSize& size,
d16cf3cd 188 wxCoord position,
af99040c 189 wxOrientation orient,
3cbab641 190 int WXUNUSED(flags))
95155e75
VZ
191{
192 if ( !win->m_wxwindow->window )
193 {
0100b858 194 // window not realized yet
95155e75
VZ
195 return;
196 }
91af0895 197
38418827 198 wxCoord full_size = GetGtkSplitterFullSize();
95155e75 199
d16cf3cd
VZ
200 // are we drawing vertical or horizontal splitter?
201 const bool isVert = orient == wxVERTICAL;
202
d16cf3cd 203 GdkRectangle rect;
91af0895 204#if USE_ERASE_RECT
35468934 205 GdkRectangle erase_rect;
91af0895
WS
206#endif
207
d16cf3cd
VZ
208 if ( isVert )
209 {
0100b858 210 int h = win->GetClientSize().GetHeight();
e1befae3 211
d16cf3cd 212 rect.x = position;
0100b858 213 rect.y = 0;
38418827 214 rect.width = full_size;
0100b858 215 rect.height = h;
e1befae3 216
91af0895 217#if USE_ERASE_RECT
35468934
RR
218 erase_rect.x = position;
219 erase_rect.y = 0;
38418827 220 erase_rect.width = full_size;
35468934 221 erase_rect.height = h;
91af0895 222#endif
d16cf3cd
VZ
223 }
224 else // horz
225 {
0100b858 226 int w = win->GetClientSize().GetWidth();
e1befae3 227
0100b858 228 rect.x = 0;
d16cf3cd 229 rect.y = position;
38418827 230 rect.height = full_size;
0100b858 231 rect.width = w;
e1befae3 232
91af0895 233#if USE_ERASE_RECT
35468934
RR
234 erase_rect.y = position;
235 erase_rect.x = 0;
38418827 236 erase_rect.height = full_size;
35468934 237 erase_rect.width = w;
91af0895 238#endif
d16cf3cd
VZ
239 }
240
91af0895 241#if USE_ERASE_RECT
ab5ea030
RR
242 // we must erase everything first, otherwise the garbage
243 // from the old sash is left when dragging it
35468934
RR
244 gtk_paint_flat_box
245 (
246 win->m_wxwindow->style,
247 GTK_PIZZA(win->m_wxwindow)->bin_window,
248 GTK_STATE_NORMAL,
249 GTK_SHADOW_NONE,
250 NULL,
251 win->m_wxwindow,
ab5ea030 252 (char *)"viewportbin", // const_cast
35468934
RR
253 erase_rect.x,
254 erase_rect.y,
255 erase_rect.width,
256 erase_rect.height
257 );
ab5ea030 258#endif
35468934 259
d16cf3cd
VZ
260
261 // leave some margin before sash itself
262 position += SASH_MARGIN / 2;
263
264 // and finally draw it using GTK paint functions
265 typedef void (*GtkPaintLineFunc)(GtkStyle *, GdkWindow *,
266 GtkStateType,
267 GdkRectangle *, GtkWidget *,
ef9bfb71 268 gchar *,
ef9bfb71 269 gint, gint, gint);
d16cf3cd
VZ
270
271 GtkPaintLineFunc func = isVert ? gtk_paint_vline : gtk_paint_hline;
272
273 (*func)
95155e75
VZ
274 (
275 win->m_wxwindow->style,
d16cf3cd 276 GTK_PIZZA(win->m_wxwindow)->bin_window,
95155e75 277 GTK_STATE_NORMAL,
d16cf3cd 278 NULL,
95155e75 279 win->m_wxwindow,
d16cf3cd
VZ
280 (char *)"paned", // const_cast
281 0, isVert ? size.y : size.x, position + SASH_SIZE / 2 - 1
95155e75
VZ
282 );
283
284 gtk_paint_box
285 (
286 win->m_wxwindow->style,
d16cf3cd 287 GTK_PIZZA(win->m_wxwindow)->bin_window,
95155e75
VZ
288 GTK_STATE_NORMAL,
289 GTK_SHADOW_OUT,
d16cf3cd 290 (GdkRectangle*) NULL,
95155e75
VZ
291 win->m_wxwindow,
292 (char *)"paned", // const_cast
d16cf3cd
VZ
293 isVert ? position : size.x - 2*SASH_SIZE,
294 isVert ? size.y - 2*SASH_SIZE : position,
295 SASH_SIZE, SASH_SIZE
95155e75
VZ
296 );
297}
298
4c85ab75
VZ
299void
300wxRendererGTK::DrawDropArrow(wxWindow *win,
301 wxDC& dc,
302 const wxRect& rect,
303 int flags)
38511687 304{
bc13e772 305 GtkWidget *button = GetButtonWidget();
38511687 306
4c85ab75
VZ
307 // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as
308 // a window for gtk_paint_xxx function, then it won't
309 // work for wxMemoryDC. So that is why we assume wxDC
310 // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC
311 // are derived from it) and use its m_window.
2e992e06
VZ
312 GdkWindow* gdk_window = dc.GetGDKWindow();
313 wxASSERT_MSG( gdk_window,
314 wxT("cannot use wxRendererNative on wxDC of this type") );
a4622f29 315
4c85ab75
VZ
316 // draw arrow so that there is even space horizontally
317 // on both sides
318 int arrowX = rect.width/4 + 1;
319 int arrowWidth = rect.width - (arrowX*2);
320
321 // scale arrow's height accoording to the width
322 int arrowHeight = rect.width/3;
323 int arrowY = (rect.height-arrowHeight)/2 +
324 ((rect.height-arrowHeight) & 1);
325
e1befae3 326 GtkStateType state;
a4622f29 327
3203621a
JS
328 if ( flags & wxCONTROL_PRESSED )
329 state = GTK_STATE_ACTIVE;
a4622f29
VZ
330 else if ( flags & wxCONTROL_DISABLED )
331 state = GTK_STATE_INSENSITIVE;
3203621a
JS
332 else if ( flags & wxCONTROL_CURRENT )
333 state = GTK_STATE_PRELIGHT;
e1befae3
VZ
334 else
335 state = GTK_STATE_NORMAL;
a4622f29 336
a4622f29 337 // draw arrow on button
a4622f29
VZ
338 gtk_paint_arrow
339 (
bc13e772 340 button->style,
2e992e06 341 gdk_window,
a4622f29 342 state,
e1befae3 343 flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
a4622f29 344 NULL,
bc13e772 345 button,
a4622f29
VZ
346 "arrow",
347 GTK_ARROW_DOWN,
a8ac548e 348 FALSE,
4c85ab75
VZ
349 rect.x + arrowX,
350 rect.y + arrowY,
351 arrowWidth,
352 arrowHeight
a4622f29 353 );
38511687
VZ
354}
355
4c85ab75
VZ
356void
357wxRendererGTK::DrawComboBoxDropButton(wxWindow *win,
358 wxDC& dc,
359 const wxRect& rect,
360 int flags)
361{
362 GtkWidget *button = GetButtonWidget();
363
364 // for reason why we do this, see DrawDropArrow
2e992e06
VZ
365 GdkWindow* gdk_window = dc.GetGDKWindow();
366 wxASSERT_MSG( gdk_window,
367 wxT("cannot use wxRendererNative on wxDC of this type") );
4c85ab75
VZ
368
369 // draw button
370 GtkStateType state;
371
3203621a
JS
372 if ( flags & wxCONTROL_PRESSED )
373 state = GTK_STATE_ACTIVE;
4c85ab75
VZ
374 else if ( flags & wxCONTROL_DISABLED )
375 state = GTK_STATE_INSENSITIVE;
3203621a
JS
376 else if ( flags & wxCONTROL_CURRENT )
377 state = GTK_STATE_PRELIGHT;
4c85ab75
VZ
378 else
379 state = GTK_STATE_NORMAL;
380
381 gtk_paint_box
382 (
383 button->style,
2e992e06 384 gdk_window,
4c85ab75
VZ
385 state,
386 flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT,
387 NULL,
388 button,
389 "button",
390 rect.x, rect.y, rect.width, rect.height
391 );
392
393 // draw arrow on button
394 DrawDropArrow(win,dc,rect,flags);
395
396}