| 1 |
This documents some history about GTK CastleWindow backend. |
| 2 |
My initial approach was to use GTK/glib's mechanism for stuff like |
| 3 |
PostRedisplay and Update, more precisely: |
| 4 |
|
| 5 |
- PostRedisplay was doing |
| 6 |
if not Closed then gtk_widget_queue_draw(GTK_WIDGET(GLAreaGtk)); |
| 7 |
|
| 8 |
- FlushRedisplay was doing |
| 9 |
gdk_window_process_updates(GTK_WIDGET(WindowGtk)^.Window, g_true); |
| 10 |
|
| 11 |
- signal_expose_event was calling DoDraw. |
| 12 |
|
| 13 |
- GtkIdle (now called Application.ProcessUpdates) |
| 14 |
was registered as GTK's idle function, |
| 15 |
by gtk_idle_add_priority. GtkIdle was responsible for doing our |
| 16 |
idle / timer events (Application.OnIdle/Timer, TCastleWindow.OnIdle/Timer). |
| 17 |
|
| 18 |
At the beginning, it was registered with priority |
| 19 |
GTK_PRIORITY_REDRAW, then G_PRIORITY_HIGH_IDLE. |
| 20 |
|
| 21 |
But, multiple reasons later, after many code rearrangements, |
| 22 |
I dropped these ideas. Now I just implement redrawing and handling idle |
| 23 |
by my own methods, called outside of GTK loop processing functions. |
| 24 |
|
| 25 |
Some reasoning: |
| 26 |
|
| 27 |
------------------------------------------------------------------------------ |
| 28 |
|
| 29 |
About idle priority: |
| 30 |
|
| 31 |
We would like to set it to GTK_PRIORITY_REDRAW (this is |
| 32 |
G_PRIORITY_HIGH_IDLE + 20, smaller than G_PRIORITY_DEFAULT_IDLE, |
| 33 |
which means it's more important). That's because our idle events |
| 34 |
(Application.OnIdle, TCastleWindow.OnIdle etc.) |
| 35 |
must be constantly called, even when AutoRedisplay = true |
| 36 |
(i.e. when we always have some redraw pending), and with |
| 37 |
~approximately the same frequency as draw events. |
| 38 |
|
| 39 |
The default priority of gtk_idle_add, which is G_PRIORITY_DEFAULT_IDLE, |
| 40 |
would make our idle events (Application.OnIdle etc.) never called |
| 41 |
when AutoRedisplay = true. |
| 42 |
|
| 43 |
Unfortunately, with GTK 2 (GTK 1 didn't have this problem), |
| 44 |
GTK_PRIORITY_REDRAW causes a problem: after switching to alternative menu, |
| 45 |
key shortcuts to menu items do not work. |
| 46 |
I don't know what's the exact reason of this, I guess that I'm just |
| 47 |
blocking some other internal gtk idles and so I'm causing some |
| 48 |
mysterious problems. |
| 49 |
|
| 50 |
Simple test case with some comments is in |
| 51 |
../../../examples/window/window_menu.lpr |
| 52 |
(and also gtk/tests/test_changing_menu/test_changing_menu.lpr |
| 53 |
on michalis.ii SVN, temporarily private (too obscure...)). |
| 54 |
|
| 55 |
G_PRIORITY_DEFAULT_IDLE doesn't have this problem, but it's leaving |
| 56 |
us with the first problem: Application.OnIdle will not be called |
| 57 |
continuously if we redraw continuously (like when AutoRedisplay = true). |
| 58 |
- Old solution: call WindowsIdleAndTimer manually |
| 59 |
after calling DoDraw in signal_expose_event. |
| 60 |
- Later solution: we just do not depend on GTK's expose events |
| 61 |
and gtk_widget_queue_draw for the job. We track RedisplayPosted |
| 62 |
ourselves, and realize it from our GtkIdle. |
| 63 |
|
| 64 |
------------------------------------------------------------------------------ |
| 65 |
|
| 66 |
Sometimes we had to temporarily uninstall GtkIdle from GTK. |
| 67 |
|
| 68 |
Reasoning in TGLApplication.ProcessAllMessages: |
| 69 |
|
| 70 |
GtkIdleInstalled must be temporarily turned off. |
| 71 |
That's because when GtkIdleInstalled = true gtk_events_pending() |
| 72 |
always returns true (because it thinks that it should call GtkIdle |
| 73 |
in a loop). |
| 74 |
|
| 75 |
Reasoning in TCastleWindow.FileDialog when using GtkFileChooser: |
| 76 |
|
| 77 |
Temporarily set GtkIdleInstalled to false. |
| 78 |
Reason? Just like for ProcessAllMessages: when |
| 79 |
GtkIdleInstalled = true then gtk_events_pending() |
| 80 |
always returns true (because it thinks that it should call GtkIdle |
| 81 |
in a loop). |
| 82 |
|
| 83 |
In this case, gtk_events_pending may be called inside gtk_dialog_run. |
| 84 |
This seems to be the case for GTK starting from 2.14: on Ubuntu 8.10 |
| 85 |
open dialog hangs strangely (you cannot see the entries in open dialog, |
| 86 |
and clicking on "file system" hangs etc.) without this |
| 87 |
GtkIdleInstalled := false fix. |
| 88 |
|
| 89 |
This was done by this code: |
| 90 |
|
| 91 |
GtkIdleHandle: guint; |
| 92 |
FGtkIdleInstalled: boolean; |
| 93 |
procedure SetGtkIdleInstalled(value: boolean); |
| 94 |
{ This controls whether GtkIdle is installed (i.e. registered to gtk). |
| 95 |
It is initialized to true in CreateImplDepend, finalized to false |
| 96 |
in DestroyImplDepend and for the most part stays as true. |
| 97 |
BUT it must be temporarily disabled in ProcessAllMesages. } |
| 98 |
property GtkIdleInstalled: boolean |
| 99 |
read FGtkIdleInstalled write SetGtkIdleInstalled; |
| 100 |
|
| 101 |
procedure TGLApplication.SetGtkIdleInstalled(value: boolean); |
| 102 |
begin |
| 103 |
if FGtkIdleInstalled <> value then |
| 104 |
begin |
| 105 |
FGtkIdleInstalled := value; |
| 106 |
if FGtkIdleInstalled then |
| 107 |
begin |
| 108 |
GtkIdleHandle := gtk_idle_add_priority(G_PRIORITY_DEFAULT_IDLE, |
| 109 |
@GtkIdle, nil); |
| 110 |
end else |
| 111 |
begin |
| 112 |
gtk_idle_remove(GtkIdleHandle); |
| 113 |
end; |
| 114 |
end; |
| 115 |
end; |
| 116 |
|
| 117 |
------------------------------------------------------------------------------ |
| 118 |
|
| 119 |
About using signal_expose_event: |
| 120 |
|
| 121 |
Looks like we cannot call here DoDraw, or any other callback. |
| 122 |
Reason: with newer GTK / glib (Debian gtk 2.18.3-1 and glib 2.22.3-1) |
| 123 |
signal "expose" is never delivered when another "expose" signal |
| 124 |
already works. |
| 125 |
|
| 126 |
(More info about this GTK change: |
| 127 |
|
| 128 |
I didn't found exact evidence for this, |
| 129 |
I suspect it's related to |
| 130 |
http://library.gnome.org/devel/gtk/2.18/gtk-migrating-ClientSideWindows.html, |
| 131 |
there's a text |
| 132 |
|
| 133 |
"One change that can cause problems for some applications is that |
| 134 |
GDK is more aggressive about optimizing away expose events. |
| 135 |
Code that does more than just repainting exposed areas in |
| 136 |
response to expose events may be affected by this. |
| 137 |
" |
| 138 |
|
| 139 |
If interested in digging more, probably looking at usage of |
| 140 |
G_SIGNAL_NO_RECURSE (for GSignalFlags) in newer GTK src could |
| 141 |
get something. |
| 142 |
) |
| 143 |
|
| 144 |
Which means that any event that could cause recursive |
| 145 |
event loop will not work correctly, i.e. will not get expose events |
| 146 |
from GTK. For example, calling GLWinMessages.MessageOk from OnDraw |
| 147 |
will fail badly --- during modal message window will not receive |
| 148 |
expose events when window is moved, resized etc. |
| 149 |
|
| 150 |
So just PostRedisplay. Nearest GtkIdle will handle redraw. |
| 151 |
|
| 152 |
This also means that we do whole PostRedisplay tracking ourselves, |
| 153 |
by RedisplayPosted boolean. No point in using gtk_widget_queue_draw / |
| 154 |
gdk_window_process_updates if they don't behave like we expect. |
| 155 |
|
| 156 |
------------------------------------------------------------------------------ |
| 157 |
|
| 158 |
Eventually, the final problem that broke the straw: it seems |
| 159 |
that expose signal cannot be generated when we're inside GTK's registered |
| 160 |
idle event. |
| 161 |
|
| 162 |
This concerns even the expose events that should be passed |
| 163 |
to us from obscuring + showing the window again. Strangely, expose |
| 164 |
after resizing the window still gets generated. But generally they don't work... |
| 165 |
|
| 166 |
This means that making MessageOk from OnDraw, or OnIdle, still doesn't |
| 167 |
work as expected... |
| 168 |
|
| 169 |
Bottom line: it's not reliable to call any TCastleWindow events (that could |
| 170 |
always call MessageOk and enter loop that will require more expose events |
| 171 |
to run properly) from GTK's signal expose *or* GTK's registered idle. |
| 172 |
|
| 173 |
------------------------------------------------------------------------------ |
| 174 |
|
| 175 |
Also, using GTK's signal expose was forcing us to use elaborate |
| 176 |
AutoRedisplayAddToList hack. Reasoning and code below: |
| 177 |
|
| 178 |
DoDraw took care of AutoRedisplay: |
| 179 |
if AutoRedisplay then |
| 180 |
begin |
| 181 |
if Application.AutoRedisplayAddToList > 0 then |
| 182 |
|
| 183 |
make sure that Application.AutoRedisplayList contains Self |
| 184 |
(i.e. add Self to Application.AutoRedisplayList, unless |
| 185 |
Application.AutoRedisplayList already contains Self) |
| 186 |
|
| 187 |
else |
| 188 |
PostRedisplay; |
| 189 |
|
| 190 |
So specific CastleWindow implementations need not to worry about |
| 191 |
AutoRedisplay. They only have to implement PostRedisplay. |
| 192 |
|
| 193 |
Also, some implementations (currently this concerns gtk |
| 194 |
implementations) should disallow doing PostRedisplay caused |
| 195 |
by AutoRedisplay = true when they are inside ProcessAllMessages |
| 196 |
(since it can, in some situations, as with GTK bindings, |
| 197 |
mean that ProcessAllMessages would hang forever, since |
| 198 |
there would be always pending message to redisplay the window). |
| 199 |
Such implementations want to do |
| 200 |
|
| 201 |
if AutoRedisplayAddToList = 0 then AutoRedisplayList.Clear; |
| 202 |
Inc(AutoRedisplayAddToList); |
| 203 |
|
| 204 |
at the beginning and then |
| 205 |
|
| 206 |
Dec(AutoRedisplayAddToList); |
| 207 |
if AutoRedisplayAddToList = 0 then AutoRedisplayList.PostRedisplay; |
| 208 |
|
| 209 |
at the end, see castlewindow_gtk.inc ProcessAllMessages implementation |
| 210 |
for example (with proper try...finally clause around). |
| 211 |
Also such backends should do |
| 212 |
AutoRedisplayList := TCastleWindowsList.Create; |
| 213 |
and |
| 214 |
FreeAndNil(AutoRedisplayList); |
| 215 |
at appropriate places (e.g. in |
| 216 |
TGLApplication.Create/DestroyImplDependent) |
| 217 |
and |
| 218 |
Application.AutoRedisplayList.Delete(Self); |
| 219 |
in TCastleWindow.CloseImplDepend (to make sure that destroyed windows |
| 220 |
are not a memebers of AutoRedisplayList). |
| 221 |
|
| 222 |
Note that AutoRedisplayAddToList is an integer (not a simple boolean) |
| 223 |
to be safe in case someone calls ProcessAllMessages recursively |
| 224 |
(e.g. call ProcessAllMessages that calls OnMouseDown that calls |
| 225 |
ProcessAllMessages inside). } |