Re: Consensus on getter conventions?



> > Note, that gtk+ constantly has more than 1 in/out parameter in
> > many functions that cause bindings problems.
> 
> Yes. Fortunately, most of those functions are rather simple - they
> check each out-parameter for NULL first, and if that's the case they
> don't do anything with them. So I make multiple methods for these
> functions - e.g. gtk_ctree_get_node_info becomes (something like)
> 
>   String getNodeText(CTreeNode node)
>   int getNodeSpacing(CTreeNode node, int column)
>   GdkPixmap getNodePixmapClosed(CTreeNode node)
>   GdkPixmap getNodeMaskClosed(CTreeNode node)
>   GdkPixmap getNodePixmapOpened(CTreeNode node)
>   GdkPixmap getNodeMaskOpened(CTreeNode node)
>   boolean isNodeExpanded(CTreeNode node)
>   boolean isNodeLeaf(CTreeNode node)
> 
> Unfortunately the success/failure int return value of
> gtk_ctree_get_node_info has to be ignored (at least not delivered to
> the user).
> 
> Another problem with these multiple-return functions is that the
> corresponding set-method (in this case gtk_ctree_set_node_info)
> cannot be used to set only one value, so in Java I get:
> 
>   void setNodeInfo(CTreeNode, String text, int spacing,
>                    GdkPixmap pixmapClosed, GdkBitmap maskClosed,
>                    GdkPixmap pixmapOpened, GdkBitmap maskOpened,
>                    boolean leaf, boolean expanded)
> 
> instead of (nicer) setNodeText, setNodeSpacing, ...

Darn I completely forgot to mention that beef.  Thanks for
bringing it up.

There is however, a problem with placing multiple sets and gets
for submembers of a list (clist) in that the specifier
of the list item is by index.  Thus if you need to set multiple
values you need to scroll though a doubly linked list many times.

The way to avoid this is to give an access to the bindings
which takes a pointer to the submember node.  Then we can call
the setNode* functions without facing O(n) for each set.  

The current way this is handled in C++ is with functions like this.

---------------------------------------------------------------------
void Cell::set_pixmap(const Gdk_Pixmap& pixmap,const Gdk_Bitmap& mask=0)
  {
    GtkCell *cell=get_cell();
    if (!cell) return;
    string s;
    switch (cell->type)
      {
        case GTK_CELL_EMPTY:
        case GTK_CELL_PIXMAP:
          gtk_clist_set_pixmap(parent_,get_row_num(),column_,
            pixmap.gdkobj(), mask.gdkobj());
          break;
        case GTK_CELL_TEXT:
          s=cell->u.text;
          if (s=="")  // junk text should be removed
        case GTK_CELL_TEXT:
          s=cell->u.text;
          if (s=="")  // junk text should be removed
            gtk_clist_set_pixmap(parent_,get_row_num(),column_,
              pixmap.gdkobj(), mask.gdkobj());
          else
            gtk_clist_set_pixtext(parent_,get_row_num(),column_,s.c_str(),
              5, pixmap.gdkobj(), mask.gdkobj());
          break;
        case GTK_CELL_PIXTEXT:
          s=cell->u.pt.text;
          gtk_clist_set_pixtext(parent_,get_row_num(),column_,s.c_str(),
            cell->u.pt.spacing, pixmap.gdkobj(), mask.gdkobj());
          break;
        default:
          break;
      }
  }
---------------------------------------------------------------------

Note that in C++ setting the pixmap does not remove the text or
other charactoristics.  (You would need to clear the cell fist
if you want that.  This is much better for
bindings as you can set multiple properties of a cell separately
in different locations in the code.   It also reduces the number
of interface functions with many arguments as you don't need a 
separate set_pixmap, set_text, set_pixtext.   The replacements
are then clear, set_text, set_pixmap, set_spacing, which can
be used together in any order.

Just another place where gtk+ is nasty on bindings.

--Karl






[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]