Re: Model-View-Control in GTK+



2009/2/26 Vlad Volodin <vest 84 gmail com>:
Well, I want to make the followed: I want associate the data and the widget.
If I change the data state with function similar to
obj_change(MyObject* obj, int row, int col, State state);
I want the widget being redrawed. And vice-versa, if I click on it, I want
the data being changed.

I think, I have to use signals, but I'm not sure how. Once (from my data to
the widget) or Twice (data-widget, widget-data)?

The simplest solution is to have a single "changed" signal on the
model. The views connect to the "changed" signals on all the models
they display, and when they see one, they update.

A typical interaction might be:

* user drags something in the view
* the event handler in the view calls model_set_value (newvalue)
* the model updates it's state and emits "changed"
* the view sees the "changed" signal and refreshes its widgets

The trick is that the view's event handler never does any drawing, it
just updates the model.

A useful further improvement is to put the "changed" handling into an
idle task. So when a model emits changed, the view, instead of
refreshing, adds itself to a list of dirty views (or, if it's already
marked dirty, does nothing).

Now have a callback so that every time your app becomes idle, it takes
a dirty view off the list and refreshes it.

You can also break the view and the controller into two separate
objects, but you probably need to have a very complex object before
that becomes important.

I have an extra thing in my app to handle nested objects. If a model
adds or removes a child, my views automatically get extra child views
added. Again, not so important for most apps I guess.

John



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