| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/gtk/renderer.cpp |
| 3 | // Purpose: implementation of wxRendererNative for wxGTK |
| 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> |
| 9 | // License: wxWindows licence |
| 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 | |
| 27 | #include "wx/renderer.h" |
| 28 | |
| 29 | #ifndef WX_PRECOMP |
| 30 | #include "wx/window.h" |
| 31 | #include "wx/dcclient.h" |
| 32 | #include "wx/settings.h" |
| 33 | #endif |
| 34 | |
| 35 | #include <gtk/gtk.h> |
| 36 | #include "wx/gtk/win_gtk.h" |
| 37 | |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | // wxRendererGTK: our wxRendererNative implementation |
| 40 | // ---------------------------------------------------------------------------- |
| 41 | |
| 42 | class WXDLLEXPORT wxRendererGTK : public wxDelegateRendererNative |
| 43 | { |
| 44 | public: |
| 45 | // draw the header control button (used by wxListCtrl) |
| 46 | virtual void DrawHeaderButton(wxWindow *win, |
| 47 | wxDC& dc, |
| 48 | const wxRect& rect, |
| 49 | int flags = 0, |
| 50 | wxHeaderSortIconType sortArrow = wxHDR_SORT_ICON_NONE, |
| 51 | wxHeaderButtonParams* params = NULL); |
| 52 | |
| 53 | // draw the expanded/collapsed icon for a tree control item |
| 54 | virtual void DrawTreeItemButton(wxWindow *win, |
| 55 | wxDC& dc, |
| 56 | const wxRect& rect, |
| 57 | int flags = 0); |
| 58 | |
| 59 | virtual void DrawSplitterBorder(wxWindow *win, |
| 60 | wxDC& dc, |
| 61 | const wxRect& rect, |
| 62 | int flags = 0); |
| 63 | virtual void DrawSplitterSash(wxWindow *win, |
| 64 | wxDC& dc, |
| 65 | const wxSize& size, |
| 66 | wxCoord position, |
| 67 | wxOrientation orient, |
| 68 | int flags = 0); |
| 69 | |
| 70 | virtual void DrawComboBoxDropButton(wxWindow *win, |
| 71 | wxDC& dc, |
| 72 | const wxRect& rect, |
| 73 | int flags = 0); |
| 74 | |
| 75 | virtual void DrawDropArrow(wxWindow *win, |
| 76 | wxDC& dc, |
| 77 | const wxRect& rect, |
| 78 | int flags = 0); |
| 79 | |
| 80 | virtual void DrawCheckBox(wxWindow *win, |
| 81 | wxDC& dc, |
| 82 | const wxRect& rect, |
| 83 | int flags = 0); |
| 84 | |
| 85 | virtual void DrawPushButton(wxWindow *win, |
| 86 | wxDC& dc, |
| 87 | const wxRect& rect, |
| 88 | int flags = 0); |
| 89 | |
| 90 | virtual void DrawItemSelectionRect(wxWindow *win, |
| 91 | wxDC& dc, |
| 92 | const wxRect& rect, |
| 93 | int flags = 0); |
| 94 | |
| 95 | virtual wxSplitterRenderParams GetSplitterParams(const wxWindow *win); |
| 96 | |
| 97 | private: |
| 98 | // FIXME: shouldn't we destroy these windows somewhere? |
| 99 | |
| 100 | // used by DrawHeaderButton and DrawPushButton |
| 101 | static GtkWidget *GetButtonWidget(); |
| 102 | |
| 103 | // used by DrawTreeItemButton() |
| 104 | static GtkWidget *GetTreeWidget(); |
| 105 | |
| 106 | // used by DrawCheckBox() |
| 107 | static GtkWidget *GetCheckButtonWidget(); |
| 108 | }; |
| 109 | |
| 110 | // ============================================================================ |
| 111 | // implementation |
| 112 | // ============================================================================ |
| 113 | |
| 114 | /* static */ |
| 115 | wxRendererNative& wxRendererNative::GetDefault() |
| 116 | { |
| 117 | static wxRendererGTK s_rendererGTK; |
| 118 | |
| 119 | return s_rendererGTK; |
| 120 | } |
| 121 | |
| 122 | // ---------------------------------------------------------------------------- |
| 123 | // helper functions |
| 124 | // ---------------------------------------------------------------------------- |
| 125 | |
| 126 | GtkWidget * |
| 127 | wxRendererGTK::GetButtonWidget() |
| 128 | { |
| 129 | static GtkWidget *s_button = NULL; |
| 130 | static GtkWidget *s_window = NULL; |
| 131 | |
| 132 | if ( !s_button ) |
| 133 | { |
| 134 | s_window = gtk_window_new( GTK_WINDOW_POPUP ); |
| 135 | gtk_widget_realize( s_window ); |
| 136 | s_button = gtk_button_new(); |
| 137 | gtk_container_add( GTK_CONTAINER(s_window), s_button ); |
| 138 | gtk_widget_realize( s_button ); |
| 139 | } |
| 140 | |
| 141 | return s_button; |
| 142 | } |
| 143 | |
| 144 | GtkWidget * |
| 145 | wxRendererGTK::GetCheckButtonWidget() |
| 146 | { |
| 147 | static GtkWidget *s_button = NULL; |
| 148 | static GtkWidget *s_window = NULL; |
| 149 | |
| 150 | if ( !s_button ) |
| 151 | { |
| 152 | s_window = gtk_window_new( GTK_WINDOW_POPUP ); |
| 153 | gtk_widget_realize( s_window ); |
| 154 | s_button = gtk_check_button_new(); |
| 155 | gtk_container_add( GTK_CONTAINER(s_window), s_button ); |
| 156 | gtk_widget_realize( s_button ); |
| 157 | } |
| 158 | |
| 159 | return s_button; |
| 160 | } |
| 161 | |
| 162 | GtkWidget * |
| 163 | wxRendererGTK::GetTreeWidget() |
| 164 | { |
| 165 | static GtkWidget *s_tree = NULL; |
| 166 | static GtkWidget *s_window = NULL; |
| 167 | |
| 168 | if ( !s_tree ) |
| 169 | { |
| 170 | s_tree = gtk_tree_view_new(); |
| 171 | s_window = gtk_window_new( GTK_WINDOW_POPUP ); |
| 172 | gtk_widget_realize( s_window ); |
| 173 | gtk_container_add( GTK_CONTAINER(s_window), s_tree ); |
| 174 | gtk_widget_realize( s_tree ); |
| 175 | } |
| 176 | |
| 177 | return s_tree; |
| 178 | } |
| 179 | |
| 180 | // ---------------------------------------------------------------------------- |
| 181 | // list/tree controls drawing |
| 182 | // ---------------------------------------------------------------------------- |
| 183 | |
| 184 | void |
| 185 | wxRendererGTK::DrawHeaderButton(wxWindow *win, |
| 186 | wxDC& dc, |
| 187 | const wxRect& rect, |
| 188 | int flags, |
| 189 | wxHeaderSortIconType sortArrow, |
| 190 | wxHeaderButtonParams* params) |
| 191 | { |
| 192 | |
| 193 | GtkWidget *button = GetButtonWidget(); |
| 194 | |
| 195 | GdkWindow* gdk_window = dc.GetGDKWindow(); |
| 196 | wxASSERT_MSG( gdk_window, |
| 197 | wxT("cannot use wxRendererNative on wxDC of this type") ); |
| 198 | |
| 199 | int x_diff = 0; |
| 200 | if (win->GetLayoutDirection() == wxLayout_RightToLeft) |
| 201 | x_diff = rect.width; |
| 202 | |
| 203 | gtk_paint_box |
| 204 | ( |
| 205 | button->style, |
| 206 | gdk_window, |
| 207 | flags & wxCONTROL_DISABLED ? GTK_STATE_INSENSITIVE : GTK_STATE_NORMAL, |
| 208 | GTK_SHADOW_OUT, |
| 209 | NULL, |
| 210 | button, |
| 211 | "button", |
| 212 | dc.LogicalToDeviceX(rect.x) - x_diff, rect.y, rect.width, rect.height |
| 213 | ); |
| 214 | |
| 215 | DrawHeaderButtonContents(win, dc, rect, flags, sortArrow, params); |
| 216 | } |
| 217 | |
| 218 | // draw a ">" or "v" button |
| 219 | void |
| 220 | wxRendererGTK::DrawTreeItemButton(wxWindow* win, |
| 221 | wxDC& dc, const wxRect& rect, int flags) |
| 222 | { |
| 223 | GtkWidget *tree = GetTreeWidget(); |
| 224 | |
| 225 | GdkWindow* gdk_window = dc.GetGDKWindow(); |
| 226 | wxASSERT_MSG( gdk_window, |
| 227 | wxT("cannot use wxRendererNative on wxDC of this type") ); |
| 228 | |
| 229 | GtkStateType state; |
| 230 | if ( flags & wxCONTROL_CURRENT ) |
| 231 | state = GTK_STATE_PRELIGHT; |
| 232 | else |
| 233 | state = GTK_STATE_NORMAL; |
| 234 | |
| 235 | int x_diff = 0; |
| 236 | if (win->GetLayoutDirection() == wxLayout_RightToLeft) |
| 237 | x_diff = rect.width; |
| 238 | |
| 239 | // VZ: I don't know how to get the size of the expander so as to centre it |
| 240 | // in the given rectangle, +2/3 below is just what looks good here... |
| 241 | gtk_paint_expander |
| 242 | ( |
| 243 | tree->style, |
| 244 | gdk_window, |
| 245 | state, |
| 246 | NULL, |
| 247 | tree, |
| 248 | "treeview", |
| 249 | dc.LogicalToDeviceX(rect.x) + 6 - x_diff, |
| 250 | dc.LogicalToDeviceY(rect.y) + 3, |
| 251 | flags & wxCONTROL_EXPANDED ? GTK_EXPANDER_EXPANDED |
| 252 | : GTK_EXPANDER_COLLAPSED |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | // ---------------------------------------------------------------------------- |
| 258 | // splitter sash drawing |
| 259 | // ---------------------------------------------------------------------------- |
| 260 | |
| 261 | static int GetGtkSplitterFullSize() |
| 262 | { |
| 263 | static GtkWidget *s_paned = NULL; |
| 264 | if (s_paned == NULL) |
| 265 | s_paned = gtk_vpaned_new(); |
| 266 | |
| 267 | gint handle_size; |
| 268 | gtk_widget_style_get (s_paned, "handle_size", &handle_size, NULL); |
| 269 | |
| 270 | return handle_size; |
| 271 | } |
| 272 | |
| 273 | wxSplitterRenderParams |
| 274 | wxRendererGTK::GetSplitterParams(const wxWindow *WXUNUSED(win)) |
| 275 | { |
| 276 | // we don't draw any border, hence 0 for the second field |
| 277 | return wxSplitterRenderParams |
| 278 | ( |
| 279 | GetGtkSplitterFullSize(), |
| 280 | 0, |
| 281 | true // hot sensitive |
| 282 | ); |
| 283 | } |
| 284 | |
| 285 | void |
| 286 | wxRendererGTK::DrawSplitterBorder(wxWindow * WXUNUSED(win), |
| 287 | wxDC& WXUNUSED(dc), |
| 288 | const wxRect& WXUNUSED(rect), |
| 289 | int WXUNUSED(flags)) |
| 290 | { |
| 291 | // nothing to do |
| 292 | } |
| 293 | |
| 294 | void |
| 295 | wxRendererGTK::DrawSplitterSash(wxWindow *win, |
| 296 | wxDC& dc, |
| 297 | const wxSize& size, |
| 298 | wxCoord position, |
| 299 | wxOrientation orient, |
| 300 | int flags) |
| 301 | { |
| 302 | if ( !win->m_wxwindow->window ) |
| 303 | { |
| 304 | // window not realized yet |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | GdkWindow* gdk_window = dc.GetGDKWindow(); |
| 309 | wxASSERT_MSG( gdk_window, |
| 310 | wxT("cannot use wxRendererNative on wxDC of this type") ); |
| 311 | |
| 312 | wxCoord full_size = GetGtkSplitterFullSize(); |
| 313 | |
| 314 | // are we drawing vertical or horizontal splitter? |
| 315 | const bool isVert = orient == wxVERTICAL; |
| 316 | |
| 317 | GdkRectangle rect; |
| 318 | |
| 319 | if ( isVert ) |
| 320 | { |
| 321 | int h = win->GetClientSize().GetHeight(); |
| 322 | |
| 323 | rect.x = position; |
| 324 | rect.y = 0; |
| 325 | rect.width = full_size; |
| 326 | rect.height = h; |
| 327 | } |
| 328 | else // horz |
| 329 | { |
| 330 | int w = win->GetClientSize().GetWidth(); |
| 331 | |
| 332 | rect.x = 0; |
| 333 | rect.y = position; |
| 334 | rect.height = full_size; |
| 335 | rect.width = w; |
| 336 | } |
| 337 | |
| 338 | int x_diff = 0; |
| 339 | if (win->GetLayoutDirection() == wxLayout_RightToLeft) |
| 340 | x_diff = rect.width; |
| 341 | |
| 342 | gtk_paint_handle |
| 343 | ( |
| 344 | win->m_wxwindow->style, |
| 345 | gdk_window, |
| 346 | flags & wxCONTROL_CURRENT ? GTK_STATE_PRELIGHT : GTK_STATE_NORMAL, |
| 347 | GTK_SHADOW_NONE, |
| 348 | NULL /* no clipping */, |
| 349 | win->m_wxwindow, |
| 350 | "paned", |
| 351 | dc.LogicalToDeviceX(rect.x) - x_diff, |
| 352 | dc.LogicalToDeviceY(rect.y), |
| 353 | rect.width, |
| 354 | rect.height, |
| 355 | isVert ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL |
| 356 | ); |
| 357 | } |
| 358 | |
| 359 | void |
| 360 | wxRendererGTK::DrawDropArrow(wxWindow *WXUNUSED(win), |
| 361 | wxDC& dc, |
| 362 | const wxRect& rect, |
| 363 | int flags) |
| 364 | { |
| 365 | GtkWidget *button = GetButtonWidget(); |
| 366 | |
| 367 | // If we give GTK_PIZZA(win->m_wxwindow)->bin_window as |
| 368 | // a window for gtk_paint_xxx function, then it won't |
| 369 | // work for wxMemoryDC. So that is why we assume wxDC |
| 370 | // is wxWindowDC (wxClientDC, wxMemoryDC and wxPaintDC |
| 371 | // are derived from it) and use its m_window. |
| 372 | GdkWindow* gdk_window = dc.GetGDKWindow(); |
| 373 | wxASSERT_MSG( gdk_window, |
| 374 | wxT("cannot use wxRendererNative on wxDC of this type") ); |
| 375 | |
| 376 | // draw arrow so that there is even space horizontally |
| 377 | // on both sides |
| 378 | int arrowX = rect.width/4 + 1; |
| 379 | int arrowWidth = rect.width - (arrowX*2); |
| 380 | |
| 381 | // scale arrow's height accoording to the width |
| 382 | int arrowHeight = rect.width/3; |
| 383 | int arrowY = (rect.height-arrowHeight)/2 + |
| 384 | ((rect.height-arrowHeight) & 1); |
| 385 | |
| 386 | GtkStateType state; |
| 387 | |
| 388 | if ( flags & wxCONTROL_PRESSED ) |
| 389 | state = GTK_STATE_ACTIVE; |
| 390 | else if ( flags & wxCONTROL_DISABLED ) |
| 391 | state = GTK_STATE_INSENSITIVE; |
| 392 | else if ( flags & wxCONTROL_CURRENT ) |
| 393 | state = GTK_STATE_PRELIGHT; |
| 394 | else |
| 395 | state = GTK_STATE_NORMAL; |
| 396 | |
| 397 | // draw arrow on button |
| 398 | gtk_paint_arrow |
| 399 | ( |
| 400 | button->style, |
| 401 | gdk_window, |
| 402 | state, |
| 403 | flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT, |
| 404 | NULL, |
| 405 | button, |
| 406 | "arrow", |
| 407 | GTK_ARROW_DOWN, |
| 408 | FALSE, |
| 409 | rect.x + arrowX, |
| 410 | rect.y + arrowY, |
| 411 | arrowWidth, |
| 412 | arrowHeight |
| 413 | ); |
| 414 | } |
| 415 | |
| 416 | void |
| 417 | wxRendererGTK::DrawComboBoxDropButton(wxWindow *win, |
| 418 | wxDC& dc, |
| 419 | const wxRect& rect, |
| 420 | int flags) |
| 421 | { |
| 422 | DrawPushButton(win,dc,rect,flags); |
| 423 | DrawDropArrow(win,dc,rect); |
| 424 | } |
| 425 | |
| 426 | void |
| 427 | wxRendererGTK::DrawCheckBox(wxWindow *WXUNUSED(win), |
| 428 | wxDC& dc, |
| 429 | const wxRect& rect, |
| 430 | int flags ) |
| 431 | { |
| 432 | GtkWidget *button = GetCheckButtonWidget(); |
| 433 | |
| 434 | // for reason why we do this, see DrawDropArrow |
| 435 | GdkWindow* gdk_window = dc.GetGDKWindow(); |
| 436 | wxASSERT_MSG( gdk_window, |
| 437 | wxT("cannot use wxRendererNative on wxDC of this type") ); |
| 438 | |
| 439 | GtkStateType state; |
| 440 | |
| 441 | if ( flags & wxCONTROL_PRESSED ) |
| 442 | state = GTK_STATE_ACTIVE; |
| 443 | else if ( flags & wxCONTROL_DISABLED ) |
| 444 | state = GTK_STATE_INSENSITIVE; |
| 445 | else if ( flags & wxCONTROL_CURRENT ) |
| 446 | state = GTK_STATE_PRELIGHT; |
| 447 | else |
| 448 | state = GTK_STATE_NORMAL; |
| 449 | |
| 450 | gtk_paint_check |
| 451 | ( |
| 452 | button->style, |
| 453 | gdk_window, |
| 454 | state, |
| 455 | flags & wxCONTROL_CHECKED ? GTK_SHADOW_IN : GTK_SHADOW_OUT, |
| 456 | NULL, |
| 457 | button, |
| 458 | "cellcheck", |
| 459 | dc.LogicalToDeviceX(rect.x)+2, |
| 460 | dc.LogicalToDeviceY(rect.y)+3, |
| 461 | 13, 13 |
| 462 | ); |
| 463 | } |
| 464 | |
| 465 | void |
| 466 | wxRendererGTK::DrawPushButton(wxWindow *WXUNUSED(win), |
| 467 | wxDC& dc, |
| 468 | const wxRect& rect, |
| 469 | int flags) |
| 470 | { |
| 471 | GtkWidget *button = GetButtonWidget(); |
| 472 | |
| 473 | // for reason why we do this, see DrawDropArrow |
| 474 | GdkWindow* gdk_window = dc.GetGDKWindow(); |
| 475 | wxASSERT_MSG( gdk_window, |
| 476 | wxT("cannot use wxRendererNative on wxDC of this type") ); |
| 477 | |
| 478 | // draw button |
| 479 | GtkStateType state; |
| 480 | |
| 481 | if ( flags & wxCONTROL_PRESSED ) |
| 482 | state = GTK_STATE_ACTIVE; |
| 483 | else if ( flags & wxCONTROL_DISABLED ) |
| 484 | state = GTK_STATE_INSENSITIVE; |
| 485 | else if ( flags & wxCONTROL_CURRENT ) |
| 486 | state = GTK_STATE_PRELIGHT; |
| 487 | else |
| 488 | state = GTK_STATE_NORMAL; |
| 489 | |
| 490 | gtk_paint_box |
| 491 | ( |
| 492 | button->style, |
| 493 | gdk_window, |
| 494 | state, |
| 495 | flags & wxCONTROL_PRESSED ? GTK_SHADOW_IN : GTK_SHADOW_OUT, |
| 496 | NULL, |
| 497 | button, |
| 498 | "button", |
| 499 | rect.x, rect.y, rect.width, rect.height |
| 500 | ); |
| 501 | } |
| 502 | |
| 503 | void |
| 504 | wxRendererGTK::DrawItemSelectionRect(wxWindow *win, |
| 505 | wxDC& dc, |
| 506 | const wxRect& rect, |
| 507 | int flags ) |
| 508 | { |
| 509 | GdkWindow* gdk_window = dc.GetGDKWindow(); |
| 510 | wxASSERT_MSG( gdk_window, |
| 511 | wxT("cannot use wxRendererNative on wxDC of this type") ); |
| 512 | |
| 513 | GtkStateType state; |
| 514 | if (flags & wxCONTROL_SELECTED) |
| 515 | { |
| 516 | if (flags & wxCONTROL_FOCUSED) |
| 517 | state = GTK_STATE_SELECTED; |
| 518 | else |
| 519 | state = GTK_STATE_INSENSITIVE; |
| 520 | |
| 521 | gtk_paint_flat_box( win->m_wxwindow->style, |
| 522 | gdk_window, |
| 523 | state, |
| 524 | GTK_SHADOW_NONE, |
| 525 | NULL, |
| 526 | win->m_wxwindow, |
| 527 | "treeview", |
| 528 | dc.LogicalToDeviceX(rect.x), |
| 529 | dc.LogicalToDeviceY(rect.y), |
| 530 | rect.width, |
| 531 | rect.height ); |
| 532 | } |
| 533 | |
| 534 | if (flags & wxCONTROL_CURRENT) |
| 535 | { |
| 536 | dc.SetPen( *wxBLACK_PEN ); |
| 537 | dc.SetBrush( *wxTRANSPARENT_BRUSH ); |
| 538 | dc.DrawRectangle( rect ); |
| 539 | } |
| 540 | } |