1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gtk.CellRenderer; 26 27 private import cairo.Context; 28 private import gdk.Event; 29 private import glib.Str; 30 private import gobject.ObjectG; 31 private import gobject.Signals; 32 private import gtk.CellEditable; 33 private import gtk.CellEditableIF; 34 private import gtk.Requisition; 35 private import gtk.Widget; 36 private import gtkc.gtk; 37 public import gtkc.gtktypes; 38 private import std.algorithm; 39 40 41 /** 42 * The #GtkCellRenderer is a base class of a set of objects used for 43 * rendering a cell to a #cairo_t. These objects are used primarily by 44 * the #GtkTreeView widget, though they aren’t tied to them in any 45 * specific way. It is worth noting that #GtkCellRenderer is not a 46 * #GtkWidget and cannot be treated as such. 47 * 48 * The primary use of a #GtkCellRenderer is for drawing a certain graphical 49 * elements on a #cairo_t. Typically, one cell renderer is used to 50 * draw many cells on the screen. To this extent, it isn’t expected that a 51 * CellRenderer keep any permanent state around. Instead, any state is set 52 * just prior to use using #GObjects property system. Then, the 53 * cell is measured using gtk_cell_renderer_get_size(). Finally, the cell 54 * is rendered in the correct location using gtk_cell_renderer_render(). 55 * 56 * There are a number of rules that must be followed when writing a new 57 * #GtkCellRenderer. First and foremost, it’s important that a certain set 58 * of properties will always yield a cell renderer of the same size, 59 * barring a #GtkStyle change. The #GtkCellRenderer also has a number of 60 * generic properties that are expected to be honored by all children. 61 * 62 * Beyond merely rendering a cell, cell renderers can optionally 63 * provide active user interface elements. A cell renderer can be 64 * “activatable” like #GtkCellRendererToggle, 65 * which toggles when it gets activated by a mouse click, or it can be 66 * “editable” like #GtkCellRendererText, which 67 * allows the user to edit the text using a #GtkEntry. 68 * To make a cell renderer activatable or editable, you have to 69 * implement the #GtkCellRendererClass.activate or 70 * #GtkCellRendererClass.start_editing virtual functions, respectively. 71 * 72 * Many properties of #GtkCellRenderer and its subclasses have a 73 * corresponding “set” property, e.g. “cell-background-set” corresponds 74 * to “cell-background”. These “set” properties reflect whether a property 75 * has been set or not. You should not set them independently. 76 */ 77 public class CellRenderer : ObjectG 78 { 79 /** the main Gtk struct */ 80 protected GtkCellRenderer* gtkCellRenderer; 81 82 /** Get the main Gtk struct */ 83 public GtkCellRenderer* getCellRendererStruct(bool transferOwnership = false) 84 { 85 if (transferOwnership) 86 ownedRef = false; 87 return gtkCellRenderer; 88 } 89 90 /** the main Gtk struct as a void* */ 91 protected override void* getStruct() 92 { 93 return cast(void*)gtkCellRenderer; 94 } 95 96 protected override void setStruct(GObject* obj) 97 { 98 gtkCellRenderer = cast(GtkCellRenderer*)obj; 99 super.setStruct(obj); 100 } 101 102 /** 103 * Sets our main struct and passes it to the parent class. 104 */ 105 public this (GtkCellRenderer* gtkCellRenderer, bool ownedRef = false) 106 { 107 this.gtkCellRenderer = gtkCellRenderer; 108 super(cast(GObject*)gtkCellRenderer, ownedRef); 109 } 110 111 112 /** */ 113 public static GType getType() 114 { 115 return gtk_cell_renderer_get_type(); 116 } 117 118 /** 119 * Passes an activate event to the cell renderer for possible processing. 120 * Some cell renderers may use events; for example, #GtkCellRendererToggle 121 * toggles when it gets a mouse click. 122 * 123 * Params: 124 * event = a #GdkEvent 125 * widget = widget that received the event 126 * path = widget-dependent string representation of the event location; 127 * e.g. for #GtkTreeView, a string representation of #GtkTreePath 128 * backgroundArea = background area as passed to gtk_cell_renderer_render() 129 * cellArea = cell area as passed to gtk_cell_renderer_render() 130 * flags = render flags 131 * 132 * Returns: %TRUE if the event was consumed/handled 133 */ 134 public bool activate(Event event, Widget widget, string path, GdkRectangle* backgroundArea, GdkRectangle* cellArea, GtkCellRendererState flags) 135 { 136 return gtk_cell_renderer_activate(gtkCellRenderer, (event is null) ? null : event.getEventStruct(), (widget is null) ? null : widget.getWidgetStruct(), Str.toStringz(path), backgroundArea, cellArea, flags) != 0; 137 } 138 139 /** 140 * Gets the aligned area used by @cell inside @cell_area. Used for finding 141 * the appropriate edit and focus rectangle. 142 * 143 * Params: 144 * widget = the #GtkWidget this cell will be rendering to 145 * flags = render flags 146 * cellArea = cell area which would be passed to gtk_cell_renderer_render() 147 * alignedArea = the return location for the space inside @cell_area 148 * that would acually be used to render. 149 * 150 * Since: 3.0 151 */ 152 public void getAlignedArea(Widget widget, GtkCellRendererState flags, GdkRectangle* cellArea, out GdkRectangle alignedArea) 153 { 154 gtk_cell_renderer_get_aligned_area(gtkCellRenderer, (widget is null) ? null : widget.getWidgetStruct(), flags, cellArea, &alignedArea); 155 } 156 157 /** 158 * Fills in @xalign and @yalign with the appropriate values of @cell. 159 * 160 * Params: 161 * xalign = location to fill in with the x alignment of the cell, or %NULL 162 * yalign = location to fill in with the y alignment of the cell, or %NULL 163 * 164 * Since: 2.18 165 */ 166 public void getAlignment(out float xalign, out float yalign) 167 { 168 gtk_cell_renderer_get_alignment(gtkCellRenderer, &xalign, &yalign); 169 } 170 171 /** 172 * Fills in @width and @height with the appropriate size of @cell. 173 * 174 * Params: 175 * width = location to fill in with the fixed width of the cell, or %NULL 176 * height = location to fill in with the fixed height of the cell, or %NULL 177 */ 178 public void getFixedSize(out int width, out int height) 179 { 180 gtk_cell_renderer_get_fixed_size(gtkCellRenderer, &width, &height); 181 } 182 183 /** 184 * Fills in @xpad and @ypad with the appropriate values of @cell. 185 * 186 * Params: 187 * xpad = location to fill in with the x padding of the cell, or %NULL 188 * ypad = location to fill in with the y padding of the cell, or %NULL 189 * 190 * Since: 2.18 191 */ 192 public void getPadding(out int xpad, out int ypad) 193 { 194 gtk_cell_renderer_get_padding(gtkCellRenderer, &xpad, &ypad); 195 } 196 197 /** 198 * Retreives a renderer’s natural size when rendered to @widget. 199 * 200 * Params: 201 * widget = the #GtkWidget this cell will be rendering to 202 * minimumSize = location to store the minimum size, or %NULL 203 * naturalSize = location to store the natural size, or %NULL 204 * 205 * Since: 3.0 206 */ 207 public void getPreferredHeight(Widget widget, out int minimumSize, out int naturalSize) 208 { 209 gtk_cell_renderer_get_preferred_height(gtkCellRenderer, (widget is null) ? null : widget.getWidgetStruct(), &minimumSize, &naturalSize); 210 } 211 212 /** 213 * Retreives a cell renderers’s minimum and natural height if it were rendered to 214 * @widget with the specified @width. 215 * 216 * Params: 217 * widget = the #GtkWidget this cell will be rendering to 218 * width = the size which is available for allocation 219 * minimumHeight = location for storing the minimum size, or %NULL 220 * naturalHeight = location for storing the preferred size, or %NULL 221 * 222 * Since: 3.0 223 */ 224 public void getPreferredHeightForWidth(Widget widget, int width, out int minimumHeight, out int naturalHeight) 225 { 226 gtk_cell_renderer_get_preferred_height_for_width(gtkCellRenderer, (widget is null) ? null : widget.getWidgetStruct(), width, &minimumHeight, &naturalHeight); 227 } 228 229 /** 230 * Retrieves the minimum and natural size of a cell taking 231 * into account the widget’s preference for height-for-width management. 232 * 233 * Params: 234 * widget = the #GtkWidget this cell will be rendering to 235 * minimumSize = location for storing the minimum size, or %NULL 236 * naturalSize = location for storing the natural size, or %NULL 237 * 238 * Since: 3.0 239 */ 240 public void getPreferredSize(Widget widget, out Requisition minimumSize, out Requisition naturalSize) 241 { 242 GtkRequisition* outminimumSize = gMalloc!GtkRequisition(); 243 GtkRequisition* outnaturalSize = gMalloc!GtkRequisition(); 244 245 gtk_cell_renderer_get_preferred_size(gtkCellRenderer, (widget is null) ? null : widget.getWidgetStruct(), outminimumSize, outnaturalSize); 246 247 minimumSize = ObjectG.getDObject!(Requisition)(outminimumSize, true); 248 naturalSize = ObjectG.getDObject!(Requisition)(outnaturalSize, true); 249 } 250 251 /** 252 * Retreives a renderer’s natural size when rendered to @widget. 253 * 254 * Params: 255 * widget = the #GtkWidget this cell will be rendering to 256 * minimumSize = location to store the minimum size, or %NULL 257 * naturalSize = location to store the natural size, or %NULL 258 * 259 * Since: 3.0 260 */ 261 public void getPreferredWidth(Widget widget, out int minimumSize, out int naturalSize) 262 { 263 gtk_cell_renderer_get_preferred_width(gtkCellRenderer, (widget is null) ? null : widget.getWidgetStruct(), &minimumSize, &naturalSize); 264 } 265 266 /** 267 * Retreives a cell renderers’s minimum and natural width if it were rendered to 268 * @widget with the specified @height. 269 * 270 * Params: 271 * widget = the #GtkWidget this cell will be rendering to 272 * height = the size which is available for allocation 273 * minimumWidth = location for storing the minimum size, or %NULL 274 * naturalWidth = location for storing the preferred size, or %NULL 275 * 276 * Since: 3.0 277 */ 278 public void getPreferredWidthForHeight(Widget widget, int height, out int minimumWidth, out int naturalWidth) 279 { 280 gtk_cell_renderer_get_preferred_width_for_height(gtkCellRenderer, (widget is null) ? null : widget.getWidgetStruct(), height, &minimumWidth, &naturalWidth); 281 } 282 283 /** 284 * Gets whether the cell renderer prefers a height-for-width layout 285 * or a width-for-height layout. 286 * 287 * Returns: The #GtkSizeRequestMode preferred by this renderer. 288 * 289 * Since: 3.0 290 */ 291 public GtkSizeRequestMode getRequestMode() 292 { 293 return gtk_cell_renderer_get_request_mode(gtkCellRenderer); 294 } 295 296 /** 297 * Returns the cell renderer’s sensitivity. 298 * 299 * Returns: %TRUE if the cell renderer is sensitive 300 * 301 * Since: 2.18 302 */ 303 public bool getSensitive() 304 { 305 return gtk_cell_renderer_get_sensitive(gtkCellRenderer) != 0; 306 } 307 308 /** 309 * Obtains the width and height needed to render the cell. Used by view 310 * widgets to determine the appropriate size for the cell_area passed to 311 * gtk_cell_renderer_render(). If @cell_area is not %NULL, fills in the 312 * x and y offsets (if set) of the cell relative to this location. 313 * 314 * Please note that the values set in @width and @height, as well as those 315 * in @x_offset and @y_offset are inclusive of the xpad and ypad properties. 316 * 317 * Deprecated: Use gtk_cell_renderer_get_preferred_size() instead. 318 * 319 * Params: 320 * widget = the widget the renderer is rendering to 321 * cellArea = The area a cell will be allocated, or %NULL 322 * xOffset = location to return x offset of cell relative to @cell_area, or %NULL 323 * yOffset = location to return y offset of cell relative to @cell_area, or %NULL 324 * width = location to return width needed to render a cell, or %NULL 325 * height = location to return height needed to render a cell, or %NULL 326 */ 327 public void getSize(Widget widget, GdkRectangle* cellArea, out int xOffset, out int yOffset, out int width, out int height) 328 { 329 gtk_cell_renderer_get_size(gtkCellRenderer, (widget is null) ? null : widget.getWidgetStruct(), cellArea, &xOffset, &yOffset, &width, &height); 330 } 331 332 /** 333 * Translates the cell renderer state to #GtkStateFlags, 334 * based on the cell renderer and widget sensitivity, and 335 * the given #GtkCellRendererState. 336 * 337 * Params: 338 * widget = a #GtkWidget, or %NULL 339 * cellState = cell renderer state 340 * 341 * Returns: the widget state flags applying to @cell 342 * 343 * Since: 3.0 344 */ 345 public GtkStateFlags getState(Widget widget, GtkCellRendererState cellState) 346 { 347 return gtk_cell_renderer_get_state(gtkCellRenderer, (widget is null) ? null : widget.getWidgetStruct(), cellState); 348 } 349 350 /** 351 * Returns the cell renderer’s visibility. 352 * 353 * Returns: %TRUE if the cell renderer is visible 354 * 355 * Since: 2.18 356 */ 357 public bool getVisible() 358 { 359 return gtk_cell_renderer_get_visible(gtkCellRenderer) != 0; 360 } 361 362 /** 363 * Checks whether the cell renderer can do something when activated. 364 * 365 * Returns: %TRUE if the cell renderer can do anything when activated 366 * 367 * Since: 3.0 368 */ 369 public bool isActivatable() 370 { 371 return gtk_cell_renderer_is_activatable(gtkCellRenderer) != 0; 372 } 373 374 /** 375 * Invokes the virtual render function of the #GtkCellRenderer. The three 376 * passed-in rectangles are areas in @cr. Most renderers will draw within 377 * @cell_area; the xalign, yalign, xpad, and ypad fields of the #GtkCellRenderer 378 * should be honored with respect to @cell_area. @background_area includes the 379 * blank space around the cell, and also the area containing the tree expander; 380 * so the @background_area rectangles for all cells tile to cover the entire 381 * @window. 382 * 383 * Params: 384 * cr = a cairo context to draw to 385 * widget = the widget owning @window 386 * backgroundArea = entire cell area (including tree expanders and maybe 387 * padding on the sides) 388 * cellArea = area normally rendered by a cell renderer 389 * flags = flags that affect rendering 390 */ 391 public void render(Context cr, Widget widget, GdkRectangle* backgroundArea, GdkRectangle* cellArea, GtkCellRendererState flags) 392 { 393 gtk_cell_renderer_render(gtkCellRenderer, (cr is null) ? null : cr.getContextStruct(), (widget is null) ? null : widget.getWidgetStruct(), backgroundArea, cellArea, flags); 394 } 395 396 /** 397 * Sets the renderer’s alignment within its available space. 398 * 399 * Params: 400 * xalign = the x alignment of the cell renderer 401 * yalign = the y alignment of the cell renderer 402 * 403 * Since: 2.18 404 */ 405 public void setAlignment(float xalign, float yalign) 406 { 407 gtk_cell_renderer_set_alignment(gtkCellRenderer, xalign, yalign); 408 } 409 410 /** 411 * Sets the renderer size to be explicit, independent of the properties set. 412 * 413 * Params: 414 * width = the width of the cell renderer, or -1 415 * height = the height of the cell renderer, or -1 416 */ 417 public void setFixedSize(int width, int height) 418 { 419 gtk_cell_renderer_set_fixed_size(gtkCellRenderer, width, height); 420 } 421 422 /** 423 * Sets the renderer’s padding. 424 * 425 * Params: 426 * xpad = the x padding of the cell renderer 427 * ypad = the y padding of the cell renderer 428 * 429 * Since: 2.18 430 */ 431 public void setPadding(int xpad, int ypad) 432 { 433 gtk_cell_renderer_set_padding(gtkCellRenderer, xpad, ypad); 434 } 435 436 /** 437 * Sets the cell renderer’s sensitivity. 438 * 439 * Params: 440 * sensitive = the sensitivity of the cell 441 * 442 * Since: 2.18 443 */ 444 public void setSensitive(bool sensitive) 445 { 446 gtk_cell_renderer_set_sensitive(gtkCellRenderer, sensitive); 447 } 448 449 /** 450 * Sets the cell renderer’s visibility. 451 * 452 * Params: 453 * visible = the visibility of the cell 454 * 455 * Since: 2.18 456 */ 457 public void setVisible(bool visible) 458 { 459 gtk_cell_renderer_set_visible(gtkCellRenderer, visible); 460 } 461 462 /** 463 * Passes an activate event to the cell renderer for possible processing. 464 * 465 * Params: 466 * event = a #GdkEvent 467 * widget = widget that received the event 468 * path = widget-dependent string representation of the event location; 469 * e.g. for #GtkTreeView, a string representation of #GtkTreePath 470 * backgroundArea = background area as passed to gtk_cell_renderer_render() 471 * cellArea = cell area as passed to gtk_cell_renderer_render() 472 * flags = render flags 473 * 474 * Returns: A new #GtkCellEditable, or %NULL 475 */ 476 public CellEditableIF startEditing(Event event, Widget widget, string path, GdkRectangle* backgroundArea, GdkRectangle* cellArea, GtkCellRendererState flags) 477 { 478 auto p = gtk_cell_renderer_start_editing(gtkCellRenderer, (event is null) ? null : event.getEventStruct(), (widget is null) ? null : widget.getWidgetStruct(), Str.toStringz(path), backgroundArea, cellArea, flags); 479 480 if(p is null) 481 { 482 return null; 483 } 484 485 return ObjectG.getDObject!(CellEditable, CellEditableIF)(cast(GtkCellEditable*) p); 486 } 487 488 /** 489 * Informs the cell renderer that the editing is stopped. 490 * If @canceled is %TRUE, the cell renderer will emit the 491 * #GtkCellRenderer::editing-canceled signal. 492 * 493 * This function should be called by cell renderer implementations 494 * in response to the #GtkCellEditable::editing-done signal of 495 * #GtkCellEditable. 496 * 497 * Params: 498 * canceled = %TRUE if the editing has been canceled 499 * 500 * Since: 2.6 501 */ 502 public void stopEditing(bool canceled) 503 { 504 gtk_cell_renderer_stop_editing(gtkCellRenderer, canceled); 505 } 506 507 protected class OnEditingCanceledDelegateWrapper 508 { 509 static OnEditingCanceledDelegateWrapper[] listeners; 510 void delegate(CellRenderer) dlg; 511 gulong handlerId; 512 513 this(void delegate(CellRenderer) dlg) 514 { 515 this.dlg = dlg; 516 this.listeners ~= this; 517 } 518 519 void remove(OnEditingCanceledDelegateWrapper source) 520 { 521 foreach(index, wrapper; listeners) 522 { 523 if (wrapper.handlerId == source.handlerId) 524 { 525 listeners[index] = null; 526 listeners = std.algorithm.remove(listeners, index); 527 break; 528 } 529 } 530 } 531 } 532 533 /** 534 * This signal gets emitted when the user cancels the process of editing a 535 * cell. For example, an editable cell renderer could be written to cancel 536 * editing when the user presses Escape. 537 * 538 * See also: gtk_cell_renderer_stop_editing(). 539 * 540 * Since: 2.4 541 */ 542 gulong addOnEditingCanceled(void delegate(CellRenderer) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 543 { 544 auto wrapper = new OnEditingCanceledDelegateWrapper(dlg); 545 wrapper.handlerId = Signals.connectData( 546 this, 547 "editing-canceled", 548 cast(GCallback)&callBackEditingCanceled, 549 cast(void*)wrapper, 550 cast(GClosureNotify)&callBackEditingCanceledDestroy, 551 connectFlags); 552 return wrapper.handlerId; 553 } 554 555 extern(C) static void callBackEditingCanceled(GtkCellRenderer* cellrendererStruct, OnEditingCanceledDelegateWrapper wrapper) 556 { 557 wrapper.dlg(wrapper.outer); 558 } 559 560 extern(C) static void callBackEditingCanceledDestroy(OnEditingCanceledDelegateWrapper wrapper, GClosure* closure) 561 { 562 wrapper.remove(wrapper); 563 } 564 565 protected class OnEditingStartedDelegateWrapper 566 { 567 static OnEditingStartedDelegateWrapper[] listeners; 568 void delegate(CellEditableIF, string, CellRenderer) dlg; 569 gulong handlerId; 570 571 this(void delegate(CellEditableIF, string, CellRenderer) dlg) 572 { 573 this.dlg = dlg; 574 this.listeners ~= this; 575 } 576 577 void remove(OnEditingStartedDelegateWrapper source) 578 { 579 foreach(index, wrapper; listeners) 580 { 581 if (wrapper.handlerId == source.handlerId) 582 { 583 listeners[index] = null; 584 listeners = std.algorithm.remove(listeners, index); 585 break; 586 } 587 } 588 } 589 } 590 591 /** 592 * This signal gets emitted when a cell starts to be edited. 593 * The intended use of this signal is to do special setup 594 * on @editable, e.g. adding a #GtkEntryCompletion or setting 595 * up additional columns in a #GtkComboBox. 596 * 597 * Note that GTK+ doesn't guarantee that cell renderers will 598 * continue to use the same kind of widget for editing in future 599 * releases, therefore you should check the type of @editable 600 * before doing any specific setup, as in the following example: 601 * |[<!-- language="C" --> 602 * static void 603 * text_editing_started (GtkCellRenderer *cell, 604 * GtkCellEditable *editable, 605 * const gchar *path, 606 * gpointer data) 607 * { 608 * if (GTK_IS_ENTRY (editable)) 609 * { 610 * GtkEntry *entry = GTK_ENTRY (editable); 611 * 612 * // ... create a GtkEntryCompletion 613 * 614 * gtk_entry_set_completion (entry, completion); 615 * } 616 * } 617 * ]| 618 * 619 * Params: 620 * editable = the #GtkCellEditable 621 * path = the path identifying the edited cell 622 * 623 * Since: 2.6 624 */ 625 gulong addOnEditingStarted(void delegate(CellEditableIF, string, CellRenderer) dlg, ConnectFlags connectFlags=cast(ConnectFlags)0) 626 { 627 auto wrapper = new OnEditingStartedDelegateWrapper(dlg); 628 wrapper.handlerId = Signals.connectData( 629 this, 630 "editing-started", 631 cast(GCallback)&callBackEditingStarted, 632 cast(void*)wrapper, 633 cast(GClosureNotify)&callBackEditingStartedDestroy, 634 connectFlags); 635 return wrapper.handlerId; 636 } 637 638 extern(C) static void callBackEditingStarted(GtkCellRenderer* cellrendererStruct, GtkCellEditable* editable, char* path, OnEditingStartedDelegateWrapper wrapper) 639 { 640 wrapper.dlg(ObjectG.getDObject!(CellEditable, CellEditableIF)(editable), Str.toString(path), wrapper.outer); 641 } 642 643 extern(C) static void callBackEditingStartedDestroy(OnEditingStartedDelegateWrapper wrapper, GClosure* closure) 644 { 645 wrapper.remove(wrapper); 646 } 647 }