How to "extend" a widget?



Hi there,

I am new to GTK (and C, actually), and one of the things I was looking
into doing was "extending" an existing widget. By that I mean, I don't
want to write a custom widget from scratch, nor a "composite widget"
(which, if I got it right, means putting 2 or more widgets together
into one, to be quick).

What I was looking into would be more taking an existing widget, and
modifying it a little, as in changing its behavior on some aspect, or
adding a feature, something like that.

My question is: what would be the best/standard/recommanded way to do
such a thing?


For instance, looking at GtkCalendar I figured I wanted to:
- add a way to set the first day of the week, in code
- have the current day (i.e. today) to be highlighted somehow, e.g.
different background color

So I looked at the code of GtkCalendar, and figured both of those
would be quite easy to do. The former is just changing week_start in
the private structure (GtkCalendarPrivate), the later can be just
adding a CSS class when drawing that day.

But I'm not sure how to do that, when writing my own widget. Because
if the former remains easy, I'm not sure accessing the private struct
is a good thing. Like, could it change in a GTK update and therefore
"broke" my code?

Similarily, to do the later I need to replace GtkCalendar's draw
function with my own, that calls the original one and then repaints
the current day (if needed). But to do so, I need to copy/paste a few
private functions from gtkcalendar.c so that, again, doesn't feel too
right.

But the only other way I can think of right now, would be to write my
own calendar widget from scratch (or, based on GtkCalendar's source
code). Which I don't feel sounds like a good idea, because I only want
to do 2 minor changes to the existing one; Because when doing so, a
few things need to be done, renaming functions and such, plus dealing
with things that are GTK-internals and not available from outside; But
also because I like the idea to have a widget that can be casted as a
GtkCalendar and use as such for any and all "standard" calendar
functions, while if writing my own (thus from GtkWidget) none of that
would be possible, which seems to go against the whole idea...

Besides, while I'm using GtkCalendar for now, I can only imagine how
bad/complicated things would get once i'll move on to GtkTreeView...

Anyways, the code I wrote for this is here: http://pastebin.com/ubG6g3QP

As I said, I'm not sure this is the right way to do such a thing, so I
would appreciate any help/information on how one would do this
properly.

Thank you,
-jacky



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