| 1 |
{%MainUnit ../castlewindow.pas} |
| 2 |
{ |
| 3 |
Copyright 2004-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" 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. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ CastleWindow backend based on GTK 2. |
| 18 |
|
| 19 |
TODO: |
| 20 |
- In OpenBackend implement MaxWidth/Height |
| 21 |
(Or maybe these properties should be removed? |
| 22 |
They are made for symmetry with MinWidth/Height. Are they really useful?) |
| 23 |
- Value of propery FullScreen could change at runtime. |
| 24 |
Observe window-state-event, change FullScreen at such times. |
| 25 |
This way I should be able to react to fullscreen changes |
| 26 |
forced by user (using window manager, not F11) really cleanly. |
| 27 |
} |
| 28 |
|
| 29 |
{$ifdef USE_EGL} |
| 30 |
{$I castlewindow_egl.inc} |
| 31 |
{$else} |
| 32 |
{$I castlewindow_glx.inc} |
| 33 |
{$endif} |
| 34 |
|
| 35 |
{$ifdef read_interface_uses} |
| 36 |
Glib2, Gdk2, Gtk2, CastleDynLib, |
| 37 |
{$ifdef CASTLE_WINDOW_GTK_WITH_XLIB} Gdk2X, X, Xlib, XUtil, {$endif} |
| 38 |
{$endif} |
| 39 |
|
| 40 |
{$ifdef read_implementation_uses} |
| 41 |
CastleInternalWindowModes, CastleFindFiles, CastleInternalFileMonitor, |
| 42 |
{$endif} |
| 43 |
|
| 44 |
{$ifdef read_interface_types} |
| 45 |
{ @exclude } |
| 46 |
{ For now I use GtkDrawingArea when CASTLE_WINDOW_GTK_2. |
| 47 |
But, really, GLAreaGtk could be any gtk widget with CASTLE_WINDOW_GTK_2. } |
| 48 |
PGtkGLArea = PGtkWidget; |
| 49 |
{$endif read_interface_types} |
| 50 |
|
| 51 |
{$ifdef read_window_interface} |
| 52 |
private |
| 53 |
WindowGtk: PGtkWindow; |
| 54 |
GLAreaGtk: PGtkGLArea; |
| 55 |
|
| 56 |
{ These things are initialized only when (MainMenu <> nil) and (not Closed). } |
| 57 |
window_vbox: PGtkVBox; |
| 58 |
window_accel_group: PGtkAccelGroup; |
| 59 |
|
| 60 |
{ Run ADialog as a modal window. |
| 61 |
ADialog will be made modal (and "transient" for us in the GtkWindow variant). |
| 62 |
|
| 63 |
Then we will wait (while in TModeGLFrozenImage) for dialog to finish. |
| 64 |
PGtkDialog version just returns gint response, like gtk_dialog_run |
| 65 |
--- possible values like gtk_dialog_run, depending on dialog buttons. |
| 66 |
PGtkWindow version returns @true if closed by Ok button, |
| 67 |
otherwise (when closed by Cancel button, or "delete" event) returns @false. |
| 68 |
|
| 69 |
Overloaded version with PGtkDialog is a close analogy of |
| 70 |
gtk_dialog_run. In fact, it was written looking at implementation |
| 71 |
of gtk_dialog_run (http://git.gnome.org/browse/gtk+/tree/gtk/gtkdialog.c?h=gtk-2-18), |
| 72 |
although it's also very similar (even shares some internal helper data) |
| 73 |
with the GtkWindow variant. |
| 74 |
|
| 75 |
Never use GTK's gtk_dialog_run! (Reason: We have to run event loop by our |
| 76 |
Application.ProcessMessage, not by GTK's g_main_loop_run, |
| 77 |
to properly run Application.ProcessUpdates, to properly redraw / |
| 78 |
call Update etc.) |
| 79 |
|
| 80 |
@groupBegin } |
| 81 |
function GtkDialogRun(ADialog: PGtkWindow; |
| 82 |
ok_button, cancel_button: PGtkWidget): boolean; |
| 83 |
function GtkDialogRun(ADialog: PGtkDialog): gint; |
| 84 |
{ @groupEnd } |
| 85 |
procedure GetInitialCursorPos; |
| 86 |
{$ifdef CASTLE_WINDOW_GTK_WITH_XLIB} |
| 87 |
{ GLAreaGtk X11 identifier. |
| 88 |
TODO: Name is confusing, this is about GLAreaGtk, not from WindowGtk. |
| 89 |
It is used by castlewindow_glx.inc functions. } |
| 90 |
function WindowXID: TWindow; |
| 91 |
{$endif} |
| 92 |
private |
| 93 |
FBorderWidth: Cardinal; |
| 94 |
|
| 95 |
{ If > 0, you cannot recreate menu. } |
| 96 |
MenuRecreateForbidden: Integer; |
| 97 |
|
| 98 |
procedure UpdateCursor; |
| 99 |
public |
| 100 |
{ Change this only when Closed. |
| 101 |
This is the width of border of main GtkWindow that will be created |
| 102 |
in Open, set with gtk_container_set_border_width. } |
| 103 |
property BorderWidth: Cardinal |
| 104 |
read FBorderWidth write FBorderWidth default 0; |
| 105 |
protected |
| 106 |
{$endif read_window_interface} |
| 107 |
|
| 108 |
{$ifdef read_application_interface} |
| 109 |
private |
| 110 |
{ Find in OpenWindows window with WindowGtk = given seekWindowGtk. |
| 111 |
Return nil if no such window is found. } |
| 112 |
function TryFindWindowGtk(seekWindowGtk: PGtkWindow): TCastleWindow; |
| 113 |
{ Find in OpenWindows window with GLAreaGtk = given seekGLAreaGtk. |
| 114 |
Return nil if no such window is found. } |
| 115 |
function TryFindGLAreaGtk(seekGLAreaGtk: PGtkGLArea): TCastleWindow; |
| 116 |
|
| 117 |
{ Same as TryFindWindowGtk, but exception EInternalError when |
| 118 |
no such window is found. } |
| 119 |
function FindWindowGtk(seekWindowGtk: PGtkWindow): TCastleWindow; |
| 120 |
{ Same as TryFindGLAreaGtk, but exception EInternalError when |
| 121 |
no such window is found. } |
| 122 |
function FindGLAreaGtk(seekGLAreaGtk: PGtkGLArea): TCastleWindow; |
| 123 |
|
| 124 |
{ This must be constantly called. |
| 125 |
We use it for two tasks currently: |
| 126 |
1. calling Update and times events of Application and all our windows |
| 127 |
2. taking care of redraw } |
| 128 |
procedure ProcessUpdates; |
| 129 |
private |
| 130 |
{ Used by SetCursor, each item may be nil if not initialized. } |
| 131 |
Cursors: array [TMouseCursor] of PGdkCursor; |
| 132 |
|
| 133 |
FXDisplayName: String; |
| 134 |
|
| 135 |
InitializeXDisplayDone: boolean; |
| 136 |
|
| 137 |
XDisplayGdk: PGdkDisplay; |
| 138 |
XScreenGdk: PGdkScreen; |
| 139 |
|
| 140 |
{ Initialize X display, calling gtk_init, gtk_gl_init, calculating |
| 141 |
XDisplayGdk and XScreenGdk. |
| 142 |
|
| 143 |
Call this only once you're sure that XDisplayName is set to |
| 144 |
a value desired by used (so, after parsing command-line parameters). |
| 145 |
|
| 146 |
Also, call this only when necessary, when you need to open a window |
| 147 |
(or get the screen parameters, like Application.ScreenWidth / ScreenHeight). |
| 148 |
This way, our GUI tools can perform a subset of their functionality |
| 149 |
(e.g. respond to --help or --version) even in a headless mode |
| 150 |
(without X server running). } |
| 151 |
procedure InitializeXDisplay; |
| 152 |
|
| 153 |
{$ifdef CASTLE_WINDOW_GTK_WITH_XLIB} |
| 154 |
{ Used by castlewindow_glx.inc functions, used by XF86VMODE functions. } |
| 155 |
function XDisplay: Xlib.PDisplay; |
| 156 |
function XScreen: Integer; |
| 157 |
{$endif} |
| 158 |
public |
| 159 |
{ Set XDisplay name, this will be used for all TCastleWindow |
| 160 |
that will be subsequently initialized by TCastleWindow.Open. |
| 161 |
|
| 162 |
Note that this is exposed by GTK even for non-XWindows platforms, |
| 163 |
but I don't know what it does there. } |
| 164 |
property XDisplayName: String read FXDisplayName write FXDisplayName; |
| 165 |
{$endif read_application_interface} |
| 166 |
|
| 167 |
{$ifdef read_implementation} |
| 168 |
|
| 169 |
{ various helpers ------------------------------------------------------------ } |
| 170 |
|
| 171 |
{$I castlewindow_gtk_menu.inc} |
| 172 |
|
| 173 |
{ This is mispelled in FPC gtk bindings, see |
| 174 |
[http://www.freepascal.org/bugs/showrec.php3?ID=5112] } |
| 175 |
function gdk_display_open(display_name:Pgchar):PGdkDisplay; |
| 176 |
cdecl; external gdklib; |
| 177 |
|
| 178 |
{ Some functions missing from FPC gtk bindings } |
| 179 |
function gdk_cursor_new_for_display(Display: PGdkDisplay; |
| 180 |
cursor_type: TGdkCursorType): PGdkCursor; |
| 181 |
cdecl; external gdklib name 'gdk_cursor_new_for_display'; |
| 182 |
function gdk_display_get_default: PGdkDisplay; |
| 183 |
cdecl; external gdklib name 'gdk_display_get_default'; |
| 184 |
function gdk_screen_get_primary_monitor(Screen: PGdkScreen): GInt; |
| 185 |
cdecl; external gdklib name 'gdk_screen_get_primary_monitor'; |
| 186 |
function gdk_screen_get_resolution(screen: PGdkScreen): gdouble; |
| 187 |
cdecl; external gdklib; |
| 188 |
|
| 189 |
{ Converts GDK keysym (from gdkkeysyms.pp, or gdk/gdkkeysym.h) to |
| 190 |
keyXxx constant (from Keys unit). Returns keyNone if no conversion is possible |
| 191 |
(gdk keysym has no corresponding TKey value). } |
| 192 |
function GdkKeysymToKey(GdkKeysym: guint): TKey; |
| 193 |
begin |
| 194 |
case GdkKeysym of |
| 195 |
GDK_KEY_BackSpace: Result := keyBackSpace; |
| 196 |
GDK_KEY_Tab: Result := keyTab; |
| 197 |
GDK_KEY_Return: Result := keyEnter; |
| 198 |
|
| 199 |
GDK_KEY_Escape: Result := keyEscape; |
| 200 |
GDK_KEY_Space: Result := keySpace; |
| 201 |
GDK_KEY_Page_Up: Result := keyPageUp; |
| 202 |
GDK_KEY_Page_Down: Result := keyPageDown; |
| 203 |
GDK_KEY_End: Result := keyEnd; |
| 204 |
GDK_KEY_Home: Result := keyHome; |
| 205 |
GDK_KEY_Left: Result := keyArrowLeft; |
| 206 |
GDK_KEY_Up: Result := keyArrowUp; |
| 207 |
GDK_KEY_Right: Result := keyArrowRight; |
| 208 |
GDK_KEY_Down: Result := keyArrowDown; |
| 209 |
GDK_KEY_Insert: Result := keyInsert; |
| 210 |
GDK_KEY_Delete: Result := keyDelete; |
| 211 |
GDK_KEY_BracketLeft: Result := keyLeftBracket; |
| 212 |
GDK_KEY_BracketRight: Result := keyRightBracket; |
| 213 |
|
| 214 |
GDK_KEY_KP_Add: Result := keyNumpadPlus; |
| 215 |
GDK_KEY_KP_Subtract: Result := keyNumpadMinus; |
| 216 |
|
| 217 |
GDK_KEY_0..GDK_KEY_9: Result := TKey(Ord(key0) + GdkKeysym - GDK_KEY_0); |
| 218 |
|
| 219 |
GDK_KEY_Capital_A..GDK_KEY_Capital_Z: Result := TKey(Ord(keyA) + GdkKeysym - GDK_KEY_Capital_A); |
| 220 |
GDK_KEY_A ..GDK_KEY_Z : Result := TKey(Ord(keyA) + GdkKeysym - GDK_KEY_A); |
| 221 |
|
| 222 |
GDK_KEY_F1..GDK_KEY_F12: Result := TKey(Ord(keyF1) + GdkKeysym - GDK_KEY_F1); |
| 223 |
|
| 224 |
GDK_KEY_Comma: Result := keyComma; |
| 225 |
GDK_KEY_Period: Result := keyPeriod; |
| 226 |
GDK_KEY_Print: Result := keyPrintScreen; |
| 227 |
GDK_KEY_Caps_Lock: Result := keyCapsLock; |
| 228 |
GDK_KEY_Scroll_Lock: Result := keyScrollLock; |
| 229 |
GDK_KEY_Num_Lock: Result := keyNumLock; |
| 230 |
GDK_KEY_Pause: Result := keyPause; |
| 231 |
|
| 232 |
GDK_KEY_KP_0: Result := keyNumpad0; |
| 233 |
GDK_KEY_KP_1: Result := keyNumpad1; |
| 234 |
GDK_KEY_KP_2: Result := keyNumpad2; |
| 235 |
GDK_KEY_KP_3: Result := keyNumpad3; |
| 236 |
GDK_KEY_KP_4: Result := keyNumpad4; |
| 237 |
GDK_KEY_KP_5: Result := keyNumpad5; |
| 238 |
GDK_KEY_KP_6: Result := keyNumpad6; |
| 239 |
GDK_KEY_KP_7: Result := keyNumpad7; |
| 240 |
GDK_KEY_KP_8: Result := keyNumpad8; |
| 241 |
GDK_KEY_KP_9: Result := keyNumpad9; |
| 242 |
GDK_KEY_KP_End: Result := keyNumpadEnd; |
| 243 |
GDK_KEY_KP_Down: Result := keyNumpadDown; |
| 244 |
GDK_KEY_KP_Next: Result := keyNumpadPageDown; |
| 245 |
GDK_KEY_KP_Left: Result := keyNumpadLeft; |
| 246 |
GDK_KEY_KP_Begin: Result := keyNumpadBegin; |
| 247 |
GDK_KEY_KP_Right: Result := keyNumpadRight; |
| 248 |
GDK_KEY_KP_Home: Result := keyNumpadHome; |
| 249 |
GDK_KEY_KP_Up: Result := keyNumpadUp; |
| 250 |
GDK_KEY_KP_Prior: Result := keyNumpadPageUp; |
| 251 |
GDK_KEY_KP_Insert: Result := keyNumpadInsert; |
| 252 |
GDK_KEY_KP_Delete: Result := keyNumpadDelete; |
| 253 |
GDK_KEY_KP_Enter: Result := keyNumpadEnter; |
| 254 |
GDK_KEY_KP_Multiply: Result := keyNumpadMultiply; |
| 255 |
GDK_KEY_KP_Divide: Result := keyNumpadDivide; |
| 256 |
GDK_KEY_Apostrophe: Result := keyApostrophe; |
| 257 |
GDK_KEY_Semicolon: Result := keySemicolon; |
| 258 |
GDK_KEY_Slash: Result := keySlash; |
| 259 |
GDK_KEY_Grave: Result := keyBackQuote; |
| 260 |
GDK_KEY_Minus: Result := keyMinus; |
| 261 |
GDK_KEY_Plus: Result := keyPlus; |
| 262 |
GDK_KEY_Equal: Result := keyEqual; |
| 263 |
GDK_KEY_BackSlash: Result := keyBackSlash; |
| 264 |
|
| 265 |
else Result := keyNone; |
| 266 |
end; |
| 267 |
end; |
| 268 |
|
| 269 |
{ Converts GDK mouse button number (from GdkEventButton.button) |
| 270 |
to TCastleMouseButton. Returns false (and does not change mb) if such |
| 271 |
button number cannot be represented as TCastleMouseButton. } |
| 272 |
function ButtonNumberToMouseButton(bn: guint; out mb: TCastleMouseButton): boolean; |
| 273 |
begin |
| 274 |
{ No need to implement this using dirty typecasts, it's simple enough |
| 275 |
to do it type-safe. } |
| 276 |
case bn of |
| 277 |
1: mb := buttonLeft; |
| 278 |
2: mb := buttonMiddle; |
| 279 |
3: mb := buttonRight; |
| 280 |
8: mb := buttonExtra1; |
| 281 |
9: mb := buttonExtra2; |
| 282 |
else Exit(false); |
| 283 |
end; |
| 284 |
Result := true; |
| 285 |
end; |
| 286 |
|
| 287 |
{ callbacks for gtk ----------------------------------------------------- |
| 288 |
|
| 289 |
All gtk callback names are named signal_xxx where xxx is the gtk signal name. |
| 290 |
So all events have names like signal_yyy_event. |
| 291 |
|
| 292 |
Callbacks that expect as 1st parameter GLAreaGtk of some OpenWindows window |
| 293 |
have 1st parameter AGLAreaGtk: PGtkAreaGtk. |
| 294 |
Similarly, callbackas that expect as 1st parameter WindowGtk of some |
| 295 |
OpenWindows window have 1st parameter AWindowGtk: PGtkWindow. |
| 296 |
|
| 297 |
Note that we capture and handle exceptions here, we don't depend on |
| 298 |
Application.DoRun to catch exceptions from our ProcessMessage. |
| 299 |
This avoids problems with GTK: we *cannot* let any exception go out |
| 300 |
from the GTK callback, it causes further SEGFAULTs, |
| 301 |
probably because GTK is simply not prepared for a callback to raise exception. |
| 302 |
A simple test is to remove these try..except clauses, and instead do |
| 303 |
|
| 304 |
try |
| 305 |
gtk_main_iteration_do(WaitForMessage); |
| 306 |
except |
| 307 |
on E: TObject do |
| 308 |
HandleException(E); |
| 309 |
end; |
| 310 |
|
| 311 |
instead of direct |
| 312 |
|
| 313 |
gtk_main_iteration_do(WaitForMessage); |
| 314 |
|
| 315 |
in ProcessMessage. The result? At the end of HandleException, you get |
| 316 |
SEGFAULT at the end of TCastleWindow.MessageOK... |
| 317 |
(debugger doesn't show sensible stuff, it's like GtkDialogRun is Ok, |
| 318 |
and then any instruction just crashes.) |
| 319 |
I found no other explanation other than GTK memory got corrupted. |
| 320 |
Catching exception inside the callback, and handling it here, works OK. |
| 321 |
} |
| 322 |
|
| 323 |
{$define SIGNAL_GENERAL_BEGIN:= |
| 324 |
try} |
| 325 |
{$define SIGNAL_GENERAL_END:= |
| 326 |
except |
| 327 |
on E: TObject do |
| 328 |
// sender is just Application, like TCustomApplication.Run does |
| 329 |
Application.HandleException(Application); |
| 330 |
end} |
| 331 |
|
| 332 |
{$define SIGNAL_GLAREA_BEGIN:= |
| 333 |
begin |
| 334 |
with Application.FindGLAreaGtk(AGLAreaGtk) do |
| 335 |
begin |
| 336 |
SIGNAL_GENERAL_BEGIN} |
| 337 |
{$define SIGNAL_GLAREA_END:= |
| 338 |
SIGNAL_GENERAL_END; |
| 339 |
end; |
| 340 |
end} |
| 341 |
|
| 342 |
{$define SIGNAL_WINDOW_BEGIN:= |
| 343 |
begin |
| 344 |
with Application.FindWindowGtk(AWindowGtk) do |
| 345 |
begin |
| 346 |
SIGNAL_GENERAL_BEGIN} |
| 347 |
{$define SIGNAL_WINDOW_END:= |
| 348 |
SIGNAL_GENERAL_END; |
| 349 |
end; |
| 350 |
end} |
| 351 |
|
| 352 |
function signal_expose_event(AGLAreaGtk: PGtkGLArea; Event: PGdkEventExpose; |
| 353 |
data: gpointer): gboolean; cdecl; |
| 354 |
SIGNAL_GLAREA_BEGIN |
| 355 |
{ About Event^.Count: |
| 356 |
|
| 357 |
Event^.Count should be >= 0. |
| 358 |
|
| 359 |
If it's > 0 it means that more EXPOSE events were present in event queue |
| 360 |
when this expose event was sent. |
| 361 |
|
| 362 |
So (since we are here not redrawing a portion of our window, instead |
| 363 |
we're redrawing the whole window) one can think that we can safely ignore |
| 364 |
this expose event when Event^.Count > 0. |
| 365 |
We will redraw ourselves next time, when the event queue will not contain |
| 366 |
any more expose events. |
| 367 |
|
| 368 |
But: when AutoRedisplay := true (or a similiar situation, |
| 369 |
we're constantly doing Invalidate) it is not so safe to do this |
| 370 |
because it is possible then that EVERY event will have Event^.Count > 0. |
| 371 |
I.e. we may not process expose events fast enough, and there always |
| 372 |
will be some expose event in queue when another expose event arrives, |
| 373 |
so always Event^.Count > 0. |
| 374 |
|
| 375 |
Actually, GDK does some exposure events compression itself. |
| 376 |
I don't know what exactly this means. But I guess that |
| 377 |
stupid Invalidate calls will be ignored, so new expose events will be |
| 378 |
added to queue only when no other expose events exist there. |
| 379 |
So every event will have Event^.Count = 0. But this STILL means that |
| 380 |
checking here is (Event^.Count > 0) is useless ! |
| 381 |
|
| 382 |
Test below confirms that: |
| 383 |
if Event.Count > 0 then |
| 384 |
Writeln('Have Expose event with Event.Count = ', Event.Count); |
| 385 |
} |
| 386 |
|
| 387 |
Invalidate; |
| 388 |
|
| 389 |
Result := gtrue; |
| 390 |
SIGNAL_GLAREA_END; |
| 391 |
|
| 392 |
function signal_window_configure_event(AWindowGtk: PGtkWindow; |
| 393 |
event: PGdkEventConfigure; data: gpointer): gboolean; cdecl; |
| 394 |
{ This signal is registered for WindowGtk, not GLAreaGtk. |
| 395 |
That's because we want to update here Left/Top. |
| 396 |
And position of GLAreaGtk is constant, only WindowGtk position changes. } |
| 397 |
var |
| 398 |
LeftGtk, TopGtk: gint; |
| 399 |
SIGNAL_WINDOW_BEGIN |
| 400 |
{ I could ask here for GLAreaGtk global position. |
| 401 |
But as of now, Left and Top are coordinates of whole WindowGtk |
| 402 |
(with WindowManager decorations, that's how |
| 403 |
gdk_window_get_root_origin works). |
| 404 |
Reasons -- the same as in castlewindow_xlib.inc. } |
| 405 |
gdk_window_get_root_origin(GTK_WIDGET(WindowGtk)^.window, @LeftGtk, @TopGtk); |
| 406 |
FLeft := LeftGtk; |
| 407 |
FTop := TopGtk; |
| 408 |
{ for the sake of gtk, I can't tell that I "handled" this in any way } |
| 409 |
Result := gfalse; |
| 410 |
SIGNAL_WINDOW_END; |
| 411 |
|
| 412 |
function signal_glarea_configure_event(AGLAreaGtk: PGtkGLArea; |
| 413 |
event: PGdkEventConfigure; data: gpointer): gboolean; cdecl; |
| 414 |
{ We could probably do this in signal_window_configure_event, |
| 415 |
since GLAreaGtk size changes only when WindowGtk size changes. |
| 416 |
But I was simply unable to find a function in gtk that does it in a clean |
| 417 |
way (getting GTK_WIDGET(GLAreaGtk).Allocation.Width does not work as it |
| 418 |
should). This signal ensures that Event.Width/Height are exactly what |
| 419 |
I want. |
| 420 |
|
| 421 |
Moreover, this way ensures that after only changing the window position |
| 422 |
no DoResize will be called. And that's good -- there's no point in calling |
| 423 |
DoResize when only Left/Top changed. } |
| 424 |
SIGNAL_GLAREA_BEGIN |
| 425 |
DoResize(Event^.Width, Event^.Height, false); |
| 426 |
{ for the sake of gtk, I can't tell that I "handled" this in any way } |
| 427 |
Result := gfalse; |
| 428 |
SIGNAL_GLAREA_END; |
| 429 |
|
| 430 |
function signal_delete_event(AWindowGtk: PGtkWindow; Event: PGdkEventKey; |
| 431 |
data: gpointer): gboolean; cdecl; |
| 432 |
SIGNAL_WINDOW_BEGIN |
| 433 |
DoCloseQuery; |
| 434 |
Result := gtrue; |
| 435 |
SIGNAL_WINDOW_END; |
| 436 |
|
| 437 |
function signal_key_press_event(AWindowGtk: PGtkWindow; Event: PGdkEventKey; |
| 438 |
data: gpointer): gboolean; cdecl; |
| 439 |
var |
| 440 |
Key: TKey; |
| 441 |
KeyString: String; |
| 442 |
SIGNAL_WINDOW_BEGIN |
| 443 |
case Event^.KeyVal of |
| 444 |
GDK_KEY_Shift_L: SetPrivateModifiersDown(mkShift, false, true); |
| 445 |
GDK_KEY_Shift_R: SetPrivateModifiersDown(mkShift, true, true); |
| 446 |
GDK_KEY_Control_L: SetPrivateModifiersDown(mkCtrl, false, true); |
| 447 |
GDK_KEY_Control_R: SetPrivateModifiersDown(mkCtrl, true, true); |
| 448 |
GDK_KEY_Alt_L: SetPrivateModifiersDown(mkAlt, false, true); |
| 449 |
GDK_KEY_Alt_R: SetPrivateModifiersDown(mkAlt, true, true); |
| 450 |
else |
| 451 |
begin |
| 452 |
Key := GdkKeysymToKey(Event^.KeyVal); |
| 453 |
KeyString := Event^._string; |
| 454 |
|
| 455 |
if (Key <> keyNone) or (KeyString <> '') then |
| 456 |
begin |
| 457 |
{ Fix some cases when KeyString should better correspond to Key. } |
| 458 |
if KeyString = '' then |
| 459 |
case Key of |
| 460 |
{$ifdef MSWINDOWS} |
| 461 |
{ It seems that GTK 1.3 for Windows cannot translate GDK_KEY_Escape and |
| 462 |
GDK_KEY_Return to standard chars (#13 and #27). So I'm fixing it here. } |
| 463 |
keyEscape : KeyString := CharEscape; |
| 464 |
keyEnter : KeyString := CharEnter; |
| 465 |
{$endif} |
| 466 |
{ It seems that GTK 2 doesn't translate backspace and tab to |
| 467 |
appropriate chars. So I'm fixing it here. } |
| 468 |
keyTab : KeyString := CharTab; |
| 469 |
keyBackSpace: KeyString := CharBackSpace; |
| 470 |
keyDelete : KeyString := CharDelete; |
| 471 |
else ; |
| 472 |
end; |
| 473 |
|
| 474 |
DoKeyDown(Key, KeyString); |
| 475 |
end; |
| 476 |
end; |
| 477 |
end; |
| 478 |
|
| 479 |
{ It would be nice to say here Result := true if key was handled |
| 480 |
in DoKeyDown. But I don't have any indicator whether key was handled |
| 481 |
in EventPress or OnPress (because such thing would complicate |
| 482 |
implementation of OnPress callback in every program using |
| 483 |
CastleWindow). So I must assume that either (1) always or (2) never |
| 484 |
when I called DoKeyDown key is handled. |
| 485 |
But (1) is actually not acceptable, because it disallows user |
| 486 |
to activate menu using F10 key. So I must set Result := false here. |
| 487 |
|
| 488 |
I'm also setting Result := false in signal_key_release_event, |
| 489 |
to be consequent. |
| 490 |
} |
| 491 |
Result := gfalse; |
| 492 |
SIGNAL_WINDOW_END; |
| 493 |
|
| 494 |
function signal_key_release_event(AWindowGtk: PGtkWindow; Event: PGdkEventKey; |
| 495 |
data: gpointer): gboolean; cdecl; |
| 496 |
var |
| 497 |
Key: TKey; |
| 498 |
SIGNAL_WINDOW_BEGIN |
| 499 |
case Event^.KeyVal of |
| 500 |
GDK_KEY_Shift_L: SetPrivateModifiersDown(mkShift, false, false); |
| 501 |
GDK_KEY_Shift_R: SetPrivateModifiersDown(mkShift, true, false); |
| 502 |
GDK_KEY_Control_L: SetPrivateModifiersDown(mkCtrl, false, false); |
| 503 |
GDK_KEY_Control_R: SetPrivateModifiersDown(mkCtrl, true, false); |
| 504 |
GDK_KEY_Alt_L: SetPrivateModifiersDown(mkAlt, false, false); |
| 505 |
GDK_KEY_Alt_R: SetPrivateModifiersDown(mkAlt, true, false); |
| 506 |
else |
| 507 |
begin |
| 508 |
Key := GdkKeysymToKey(Event^.KeyVal); |
| 509 |
if Key <> keyNone then DoKeyUp(Key); |
| 510 |
end; |
| 511 |
end; |
| 512 |
|
| 513 |
{ Why false, not true ? See comments at the end of |
| 514 |
signal_key_press_event. } |
| 515 |
Result := gfalse; |
| 516 |
SIGNAL_WINDOW_END; |
| 517 |
|
| 518 |
function signal_button_press_event(AGLAreaGtk: PGtkGLArea; Event: PGdkEventButton; |
| 519 |
data: gpointer): gboolean; cdecl; |
| 520 |
var mb: TCastleMouseButton; |
| 521 |
SIGNAL_GLAREA_BEGIN |
| 522 |
Result := (Event^._type = GDK_BUTTON_PRESS) and |
| 523 |
ButtonNumberToMouseButton(Event^.button, mb); |
| 524 |
if Result then |
| 525 |
DoMouseDown(LeftTopToCastle(Event^.x, Event^.y), mb, 0); |
| 526 |
SIGNAL_GLAREA_END; |
| 527 |
|
| 528 |
function signal_button_release_event(AGLAreaGtk: PGtkGLArea; Event: PGdkEventButton; |
| 529 |
data: gpointer): gboolean; cdecl; |
| 530 |
var mb: TCastleMouseButton; |
| 531 |
SIGNAL_GLAREA_BEGIN |
| 532 |
Result := (Event^._type = GDK_BUTTON_RELEASE) and |
| 533 |
ButtonNumberToMouseButton(Event^.button, mb); |
| 534 |
if Result then |
| 535 |
DoMouseUp(LeftTopToCastle(Event^.x, Event^.y), mb, 0); |
| 536 |
SIGNAL_GLAREA_END; |
| 537 |
|
| 538 |
function signal_motion_notify_event(AGLAreaGtk: PGtkGLArea; Event: PGdkEventMotion; |
| 539 |
data: gpointer): gboolean; cdecl; |
| 540 |
SIGNAL_GLAREA_BEGIN |
| 541 |
DoMotion(InputMotion(MousePosition, |
| 542 |
LeftTopToCastle(Event^.x, Event^.y), MousePressed, 0)); |
| 543 |
Result := gtrue; |
| 544 |
SIGNAL_GLAREA_END; |
| 545 |
|
| 546 |
function signal_scroll_event(AGLAreaGtk: PGtkGLArea; Event: PGdkEventScroll; |
| 547 |
data: gpointer): gboolean; cdecl; |
| 548 |
SIGNAL_GLAREA_BEGIN |
| 549 |
case Event^.Direction of |
| 550 |
GDK_SCROLL_UP: DoMouseWheel( 1, true); |
| 551 |
GDK_SCROLL_DOWN: DoMouseWheel(-1, true); |
| 552 |
GDK_SCROLL_LEFT: DoMouseWheel( 1, false); |
| 553 |
GDK_SCROLL_RIGHT:DoMouseWheel(-1, false); |
| 554 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 555 |
else WritelnWarning('GTK', 'Invalid GdkEventScroll.direction'); |
| 556 |
{$endif} |
| 557 |
end; |
| 558 |
Result := gtrue; |
| 559 |
SIGNAL_GLAREA_END; |
| 560 |
|
| 561 |
procedure signal_menu_item_activate(AMenuItemGtk: PGtkMenuItem; |
| 562 |
data: gpointer); cdecl; |
| 563 |
var |
| 564 |
MenuItem: TMenuItem; |
| 565 |
Window: TCastleWindow; |
| 566 |
begin |
| 567 |
SIGNAL_GENERAL_BEGIN |
| 568 |
MenuItem := MenuItemFromSmallId( PtrInt(Data) ); |
| 569 |
Window := TCastleWindow( gtk_object_get_user_data(GTK_OBJECT(AMenuItemGtk)) ); |
| 570 |
|
| 571 |
if MenuItem is TMenuItemChecked then |
| 572 |
begin |
| 573 |
{ GTK checked menu items do always something like our AutoCheckedToggle. |
| 574 |
This assert confirms that: } |
| 575 |
|
| 576 |
if not (MenuItem is TMenuItemRadio) then |
| 577 |
Assert( gtk_check_menu_item_get_active(PGtkCheckMenuItem(AMenuItemGtk)) <> |
| 578 |
TMenuItemChecked(MenuItem).Checked); |
| 579 |
|
| 580 |
{ I don't want this (because, first of all, not always my |
| 581 |
TMenuItemChecked will have AutoCheckedToggle = true. |
| 582 |
Second, when not MenuActive, I do not want such behavior, even for |
| 583 |
AutoCheckedToggle = true.) |
| 584 |
|
| 585 |
So below I'm simply returning such GTK menu item to it's previous state. |
| 586 |
|
| 587 |
Note that we CAN'T do here simple |
| 588 |
1. gtk_check_menu_item_set_active(PGtkCheckMenuItem(AMenuItemGtk), |
| 589 |
not (gtk_check_menu_item_get_active(AMenuItemGtk))); |
| 590 |
or (equivalent) |
| 591 |
2. gtk_check_menu_item_set_active(PGtkCheckMenuItem(AMenuItemGtk), |
| 592 |
TMenuItemChecked(MenuItem).Checked); |
| 593 |
because gtk_check_menu_item_set_active |
| 594 |
would call THIS SIGNAL ONCE AGAIN when we change active state ! |
| 595 |
This means that inside gtk_check_menu_item_set_active this |
| 596 |
procedure would be called once again (if we were using 1., |
| 597 |
this would already be a bug, because this would lead to recursive |
| 598 |
call. If we were using 2., the bug will be |
| 599 |
because we would call Window.DoMenuClick(MenuItem) TWICE. |
| 600 |
|
| 601 |
So I must temporary block this signal before doing |
| 602 |
gtk_check_menu_item_set_active. } |
| 603 |
MenuItemBlock(AMenuItemGtk, MenuItem); |
| 604 |
try |
| 605 |
gtk_check_menu_item_set_active(PGtkCheckMenuItem(AMenuItemGtk), |
| 606 |
TMenuItemChecked(MenuItem).Checked); |
| 607 |
finally |
| 608 |
MenuItemUnblock(AMenuItemGtk, MenuItem); |
| 609 |
end; |
| 610 |
end; |
| 611 |
|
| 612 |
Window.DoMenuClick(MenuItem); |
| 613 |
SIGNAL_GENERAL_END; |
| 614 |
end; |
| 615 |
|
| 616 |
function signal_window_focus_out_event(AWindowGtk: PGtkWindow; |
| 617 |
Event: PGdkEventfocus; data: gpointer): gboolean; cdecl; |
| 618 |
{ This is called when user switches to another window. } |
| 619 |
SIGNAL_WINDOW_BEGIN |
| 620 |
ReleaseAllKeysAndMouse; |
| 621 |
{ for the sake of gtk, I can't tell that I "handled" this in any way } |
| 622 |
FFocused := false; |
| 623 |
Result := gfalse; |
| 624 |
SIGNAL_WINDOW_END; |
| 625 |
|
| 626 |
function signal_window_focus_in_event(AWindowGtk: PGtkWindow; |
| 627 |
Event: PGdkEventfocus; data: gpointer): gboolean; cdecl; |
| 628 |
SIGNAL_WINDOW_BEGIN |
| 629 |
{ Reset FMousePosition, since mouse position is unknown now, |
| 630 |
and do not activate mouse look at next frame. |
| 631 |
Note: resetting with FMousePosition := Vector2(-1, -1); |
| 632 |
is not enough, testcase: wrong 1st movement in Unholy after alt+tab to fight. } |
| 633 |
GetInitialCursorPos; |
| 634 |
Container.MouseLookIgnoreNextMotion; |
| 635 |
FileMonitor.CheckChanges; |
| 636 |
FFocused := true; |
| 637 |
Result := gfalse; |
| 638 |
SIGNAL_WINDOW_END; |
| 639 |
|
| 640 |
{ TCastleWindow ------------------------------------------------------------ } |
| 641 |
|
| 642 |
procedure TCastleWindow.SetCursor(const Value: TMouseCursor); |
| 643 |
begin |
| 644 |
if FCursor <> Value then |
| 645 |
begin |
| 646 |
FCursor := Value; |
| 647 |
if not Closed then |
| 648 |
UpdateCursor; |
| 649 |
end; |
| 650 |
end; |
| 651 |
|
| 652 |
procedure TCastleWindow.UpdateCursor; |
| 653 |
|
| 654 |
function CreateNoneCursor: PGdkCursor; |
| 655 |
const |
| 656 |
Bits: array[0..0] of Guchar = (0); |
| 657 |
Color: TGdkColor = (Pixel: 0; Red: 0; Green: 0; Blue: 0); |
| 658 |
var |
| 659 |
Pixmap: PGdkPixmap; |
| 660 |
begin |
| 661 |
{ Based on [http://mail.gnome.org/archives/gtk-app-devel-list/2005-January/msg00370.html] } |
| 662 |
Pixmap := gdk_bitmap_create_from_data(nil, @Bits, 1, 1); |
| 663 |
try |
| 664 |
Result := gdk_cursor_new_from_pixmap(Pixmap, Pixmap, @Color, @Color, 0, 0); |
| 665 |
finally gdk_pixmap_unref(Pixmap); end; |
| 666 |
end; |
| 667 |
|
| 668 |
const |
| 669 |
{ GDK cursors. |
| 670 |
See https://developer.gnome.org/gdk3/stable/gdk3-Cursors.html for the list. } |
| 671 |
GdkCursorTypeFromCastle: array [mcStandard .. High(TMouseCursor)] of |
| 672 |
TGdkCursorType = |
| 673 |
( |
| 674 |
GDK_LEFT_PTR, //< mcStandard. |
| 675 |
// Note that GDK_ARROW seems standard under XWindows |
| 676 |
// (it is a right-top arrow), |
| 677 |
// but actually in modern applications it is more standard |
| 678 |
// to use left-top arrow in GDK_LEFT_PTR. |
| 679 |
GDK_WATCH, // mcWait |
| 680 |
GDK_XTERM, // mcText |
| 681 |
GDK_HAND2, // mcHand |
| 682 |
GDK_SB_V_DOUBLE_ARROW, // mcResizeVertical |
| 683 |
GDK_SB_H_DOUBLE_ARROW, // mcResizeHorizontal |
| 684 |
GDK_TOP_LEFT_CORNER, // mcResizeTopLeft |
| 685 |
GDK_TOP_SIDE, // mcResizeTop |
| 686 |
GDK_TOP_RIGHT_CORNER, // mcResizeTopRight |
| 687 |
GDK_LEFT_SIDE, // mcResizeLeft |
| 688 |
GDK_RIGHT_SIDE, // mcResizeRight |
| 689 |
GDK_BOTTOM_LEFT_CORNER, // mcResizeBottomLeft |
| 690 |
GDK_BOTTOM_SIDE, // mcResizeBottom |
| 691 |
GDK_BOTTOM_RIGHT_CORNER // mcResizeBottomRight |
| 692 |
); |
| 693 |
begin |
| 694 |
if Application.Cursors[InternalCursor] = nil then |
| 695 |
begin |
| 696 |
{ initialize Application.Cursors[Cursor] } |
| 697 |
case InternalCursor of |
| 698 |
mcDefault: |
| 699 |
{ don't do anything, Application.Cursors[mcDefault] should be nil, |
| 700 |
gdk_window_set_cursor will understand nil as "use parent" |
| 701 |
and that's exactly what we want for mcDefault. }; |
| 702 |
mcNone, mcForceNone: Application.Cursors[InternalCursor] := CreateNoneCursor; |
| 703 |
else |
| 704 |
Application.Cursors[InternalCursor] := gdk_cursor_new_for_display( |
| 705 |
Application.XDisplayGdk, GdkCursorTypeFromCastle[InternalCursor]); |
| 706 |
end; |
| 707 |
end; |
| 708 |
|
| 709 |
gdk_window_set_cursor(GTK_WIDGET(GLAreaGtk)^.Window, Application.Cursors[InternalCursor]); |
| 710 |
end; |
| 711 |
|
| 712 |
{$ifdef CASTLE_WINDOW_GTK_WITH_XLIB} |
| 713 |
// Since GDK >= 2.2 |
| 714 |
function gdk_x11_screen_lookup_visual(Screen: PGdkScreen; XVisualid: TVisualID): PGdkVisual; |
| 715 |
cdecl; external gdklib; |
| 716 |
{$else} |
| 717 |
// Since GDK >= 2.8 |
| 718 |
procedure gdk_display_warp_pointer(Display: PGdkDisplay; |
| 719 |
Screen: PGdkScreen; X, Y: GInt); cdecl; external gdklib; |
| 720 |
// Since GTK >= 2.18 |
| 721 |
procedure gdk_window_get_root_coords(Window: PGdkWindow; |
| 722 |
X, Y: GInt; RootX, RootY: PGInt); cdecl; external gtklib; |
| 723 |
{$endif} |
| 724 |
|
| 725 |
{$ifdef CASTLE_WINDOW_GTK_WITH_XLIB} |
| 726 |
function TCastleWindow.WindowXID: TWindow; |
| 727 |
begin |
| 728 |
Result := gdk_x11_drawable_get_xid(GTK_WIDGET(GLAreaGtk)^.Window); |
| 729 |
end; |
| 730 |
{$endif} |
| 731 |
|
| 732 |
procedure TCastleWindow.SetMousePosition(const Value: TVector2); |
| 733 |
var |
| 734 |
P: TVector2Integer; |
| 735 |
{$ifndef CASTLE_WINDOW_GTK_WITH_XLIB} |
| 736 |
RootX, RootY: GInt; |
| 737 |
{$endif} |
| 738 |
begin |
| 739 |
if not Closed then |
| 740 |
begin |
| 741 |
{ Do not set Mouse.CursorPos to the same value, to make sure we don't cause |
| 742 |
unnecessary OnMotion on some systems while actual MousePosition didn't change. } |
| 743 |
if TVector2.PerfectlyEquals(Value, FMousePosition) then Exit; |
| 744 |
|
| 745 |
P := CastleToLeftTopInt(Value); |
| 746 |
|
| 747 |
{$ifdef CASTLE_WINDOW_GTK_WITH_XLIB} |
| 748 |
{ With older GDK/GTK, you had to use XWarpPointer to position mouse. |
| 749 |
We still use it, for now, for GTK under Xlib --- this way we work |
| 750 |
even with ancient GTK versions. } |
| 751 |
XWarpPointer( |
| 752 |
gdk_x11_drawable_get_xdisplay(GTK_WIDGET(GLAreaGtk)^.Window), |
| 753 |
X.None, |
| 754 |
WindowXID, |
| 755 |
0, 0, 0, 0, P.X, P.Y); |
| 756 |
{$else} |
| 757 |
gdk_window_get_root_coords(GTK_WIDGET(GLAreaGtk)^.Window, |
| 758 |
P.X, P.Y, @RootX, @RootY); |
| 759 |
gdk_display_warp_pointer(Application.XDisplayGdk, |
| 760 |
Application.XScreenGdk, RootX, RootY); |
| 761 |
{$endif} |
| 762 |
end; |
| 763 |
end; |
| 764 |
|
| 765 |
procedure TCastleWindow.SetCaption(const Part: TCaptionPart; const Value: String); |
| 766 |
begin |
| 767 |
FCaption[Part] := Value; |
| 768 |
if not Closed then |
| 769 |
gtk_window_set_title(GTK_WINDOW(WindowGtk), PChar(GetWholeCaption)); |
| 770 |
end; |
| 771 |
|
| 772 |
procedure TCastleWindow.GetInitialCursorPos; |
| 773 |
var |
| 774 |
X, Y: GInt; |
| 775 |
begin |
| 776 |
gdk_window_get_pointer(GTK_WIDGET(GLAreaGtk)^.Window, @X, @Y, nil); |
| 777 |
FMousePosition := LeftTopToCastle(X, Y); |
| 778 |
end; |
| 779 |
|
| 780 |
procedure TCastleWindow.OpenBackend; |
| 781 |
|
| 782 |
{ Notes about FullScreen implementation for GTK_2: |
| 783 |
|
| 784 |
I'm doing fullscreen using gtk_window_fullscreen, |
| 785 |
this tells window manager that I want to be fullscreen |
| 786 |
using XAtom _NET_WM_STATE_FULLSCREEN. |
| 787 |
This avoids using override_redirect (so gtk menu is usable, |
| 788 |
and user can use window-manager key shortcuts like Alt+Tab |
| 789 |
with our fullscreen window) and at the same time window is on top |
| 790 |
(it should not be covered by things like gnome-panel). |
| 791 |
} |
| 792 |
|
| 793 |
const |
| 794 |
VBoxSpacing = 2; |
| 795 |
|
| 796 |
procedure set_default_glarea_size(Width, Height: Integer); |
| 797 |
{ This requires MainMenu.Handle to be initialized (only if MainMenu <> nil, |
| 798 |
of course ) } |
| 799 |
var |
| 800 |
WindowWidth, WindowHeight: Integer; |
| 801 |
WindowReq: TGtkRequisition; |
| 802 |
begin |
| 803 |
{ Implementation of this is really non-elegant. |
| 804 |
But I do not know how to implement this in GTK+ (even if I would use |
| 805 |
GTK+ 2.x). I would like something like |
| 806 |
gtk_widget_set_default_size(GLAreaGtk, Width, Height) |
| 807 |
but there is no gtk_widget_set_default_size function, there is only |
| 808 |
gtk_window_set_default_size. I do NOT want to set minimal area of a widget, |
| 809 |
I want default size (that could be later made smaller by a user), |
| 810 |
and I do not want to give non-standard configuration values to |
| 811 |
gtk_window_set_policy. So I do the only solution that I know of: |
| 812 |
I'm simply doing gtk_window_set_default_size, enlarging my Height |
| 813 |
by menu height and VBoxSpacing. } |
| 814 |
WindowWidth := Width; |
| 815 |
WindowHeight := Height; |
| 816 |
if (MainMenu <> nil) and MainMenuVisible then |
| 817 |
begin |
| 818 |
gtk_widget_size_request(GTK_WIDGET(MainMenu.Handle), @WindowReq); |
| 819 |
WindowHeight += WindowReq.Height + VBoxSpacing; |
| 820 |
end; |
| 821 |
gtk_window_set_default_size(WindowGtk, WindowWidth, WindowHeight); |
| 822 |
end; |
| 823 |
|
| 824 |
procedure ContextNotPossible; |
| 825 |
begin |
| 826 |
raise EGLContextNotPossible.CreateFmt( |
| 827 |
'Cannot initialize OpenGL context with requested attributes (%s)', |
| 828 |
[ RequestedBufferAttributes ]); |
| 829 |
end; |
| 830 |
|
| 831 |
procedure InitializeDpi; |
| 832 |
var |
| 833 |
ScreenWidthMM: Single; |
| 834 |
NewDpi: Single; |
| 835 |
begin |
| 836 |
NewDpi := gdk_screen_get_resolution(Application.XScreenGdk); |
| 837 |
if NewDpi <= 0 then |
| 838 |
begin |
| 839 |
ScreenWidthMM := gdk_screen_width_mm(); |
| 840 |
if ScreenWidthMM <= 0 then |
| 841 |
begin |
| 842 |
{ Lazarus LCL also secures from this case, with comment: |
| 843 |
"some TV-out screens don't know there size" (GetScreenWidthMM). } |
| 844 |
NewDpi := DefaultDpi; |
| 845 |
end else |
| 846 |
NewDpi := gdk_screen_width() / (ScreenWidthMM / 25.4); |
| 847 |
end; |
| 848 |
Container.Dpi := NewDpi; |
| 849 |
// Too verbose by default, TCastleContainer.InternalCalculateUIScale will log it if UIScaling is usDpiScale. |
| 850 |
// WritelnLog('GTK', 'Dots (pixels) per inch is %f (%f * default %f)', |
| 851 |
// [NewDpi, NewDpi / DefaultDpi, DefaultDpi]); |
| 852 |
end; |
| 853 |
|
| 854 |
{$ifndef USE_EGL} |
| 855 |
var |
| 856 |
GdkVisual: PGdkVisual; |
| 857 |
GdkColormap: PGdkColormap; |
| 858 |
{$endif} |
| 859 |
begin |
| 860 |
Application.InitializeXDisplay; |
| 861 |
|
| 862 |
WindowGtk := PGtkWindow(gtk_window_new(GTK_WINDOW_TOPLEVEL)); |
| 863 |
|
| 864 |
if (GtkIconName <> '') and |
| 865 |
{ gtk_window_set_icon_name exists since GTK 2.6. |
| 866 |
(Although I officially require GTK 2.6, this function is really |
| 867 |
not critical, so we check it dynamically.) } |
| 868 |
Assigned(@gtk_window_set_icon_name) then |
| 869 |
gtk_window_set_icon_name(WindowGtk, PCharOrNil(GtkIconName)); |
| 870 |
|
| 871 |
gtk_window_set_title(GTK_WINDOW(WindowGtk), PChar(GetWholeCaption)); |
| 872 |
gtk_container_set_border_width(GTK_CONTAINER(WindowGtk), BorderWidth); |
| 873 |
|
| 874 |
{$ifndef USE_EGL} |
| 875 |
{ Create XVisual } |
| 876 |
ContextCreateEarly; |
| 877 |
|
| 878 |
{ Create GdkColormap using XVisual } |
| 879 |
GdkVisual := gdk_x11_screen_lookup_visual(Application.XScreenGdk, XVisual^.VisualId); |
| 880 |
GdkColormap := gdk_colormap_new(GdkVisual, gfalse); |
| 881 |
gtk_widget_push_colormap(GdkColormap); |
| 882 |
{$endif} |
| 883 |
|
| 884 |
{ Create now GLAreaGtk, which will use GdkColormap } |
| 885 |
GLAreaGtk := gtk_drawing_area_new; |
| 886 |
gtk_widget_set_double_buffered(GLAreaGtk, gfalse); // following Lazarus GTK OpenGL, not sure if needed |
| 887 |
|
| 888 |
// Cleanup after gtk_widget_push_colormap called above |
| 889 |
gtk_widget_pop_colormap(); |
| 890 |
|
| 891 |
{$ifndef USE_EGL} |
| 892 |
{ Note that we don't have, and also don't need, WindowXID now. |
| 893 |
|
| 894 |
That's because widget is not realized, and calling WindowXID |
| 895 |
would just cause warning |
| 896 |
"... Gdk-WARNING **: ....: /build/gtk+2.0-ABXlu1/gtk+2.0-2.24.32/gdk/x11/gdkdrawable-x11.c:952 drawable is not a pixmap or window" |
| 897 |
|
| 898 |
Luckily, TGLContextGlx doesn't require WindowXID. |
| 899 |
And not defined USE_EGL -> implies we use TGLContextGlx now, |
| 900 |
as GTK backend supports only glX or EGL. |
| 901 |
} |
| 902 |
ContextCreate; |
| 903 |
{$endif} |
| 904 |
|
| 905 |
gtk_widget_show(GTK_WIDGET(GLAreaGtk)); |
| 906 |
|
| 907 |
{ connect signal handlers to GLAreaGtk } |
| 908 |
{ What events to catch ? It must cover all signal_yyy_event functions that we |
| 909 |
will connect. This must be called before X Window is created. } |
| 910 |
gtk_widget_set_events(GTK_WIDGET(GLAreaGtk), |
| 911 |
GDK_EXPOSURE_MASK {for expose_event} or |
| 912 |
GDK_BUTTON_PRESS_MASK {for button_press_event} or |
| 913 |
GDK_BUTTON_RELEASE_MASK {for button_release_event} or |
| 914 |
GDK_POINTER_MOTION_MASK {for motion_notify_event} |
| 915 |
{ we don't have to tell here that we want configure_event, |
| 916 |
it is always called }); |
| 917 |
|
| 918 |
gtk_signal_connect(GTK_OBJECT(GLAreaGtk), 'configure_event', |
| 919 |
GTK_SIGNAL_FUNC(@signal_glarea_configure_event), nil); |
| 920 |
gtk_signal_connect(GTK_OBJECT(GLAreaGtk), 'expose_event', |
| 921 |
GTK_SIGNAL_FUNC(@signal_expose_event), nil); |
| 922 |
gtk_signal_connect(GTK_OBJECT(GLAreaGtk), 'button_press_event', |
| 923 |
GTK_SIGNAL_FUNC(@signal_button_press_event), nil); |
| 924 |
gtk_signal_connect(GTK_OBJECT(GLAreaGtk), 'button_release_event', |
| 925 |
GTK_SIGNAL_FUNC(@signal_button_release_event), nil); |
| 926 |
gtk_signal_connect(GTK_OBJECT(GLAreaGtk), 'motion_notify_event', |
| 927 |
GTK_SIGNAL_FUNC(@signal_motion_notify_event), nil); |
| 928 |
gtk_signal_connect(GTK_OBJECT(GLAreaGtk), 'scroll_event', |
| 929 |
GTK_SIGNAL_FUNC(@signal_scroll_event), nil); |
| 930 |
|
| 931 |
{ connect signal handlers to WindowGtk } |
| 932 |
gtk_widget_set_events(GTK_WIDGET(WindowGtk), |
| 933 |
GDK_KEY_PRESS_MASK {for key_press} or |
| 934 |
GDK_KEY_RELEASE_MASK {for key_release} |
| 935 |
{ we don't have to tell here that we want configure_event, |
| 936 |
it is always called }); |
| 937 |
|
| 938 |
gtk_signal_connect(GTK_OBJECT(WindowGtk), 'configure_event', |
| 939 |
GTK_SIGNAL_FUNC(@signal_window_configure_event), nil); |
| 940 |
gtk_signal_connect (GTK_OBJECT(WindowGtk), 'delete_event', |
| 941 |
GTK_SIGNAL_FUNC(@signal_delete_event), nil); |
| 942 |
gtk_signal_connect(GTK_OBJECT(WindowGtk), 'key_press_event', |
| 943 |
GTK_SIGNAL_FUNC(@signal_key_press_event), nil); |
| 944 |
gtk_signal_connect(GTK_OBJECT(WindowGtk), 'key_release_event', |
| 945 |
GTK_SIGNAL_FUNC(@signal_key_release_event), nil); |
| 946 |
gtk_signal_connect(GTK_OBJECT(WindowGtk), 'focus_out_event', |
| 947 |
GTK_SIGNAL_FUNC(@signal_window_focus_out_event), nil); |
| 948 |
gtk_signal_connect(GTK_OBJECT(WindowGtk), 'focus_in_event', |
| 949 |
GTK_SIGNAL_FUNC(@signal_window_focus_in_event), nil); |
| 950 |
|
| 951 |
{ Add us to OpenWindows windows to ensure that all callbacks that are |
| 952 |
eventually called by procedures below will be able to use |
| 953 |
Application.FindGLAreaGtk and Application.FindWindowGtk. } |
| 954 |
Application.OpenWindowsAdd(Self); |
| 955 |
|
| 956 |
{ setup window position from Left, Top } |
| 957 |
gtk_widget_set_uposition(GTK_WIDGET(WindowGtk), Left, Top); |
| 958 |
|
| 959 |
if (MainMenu <> nil) and MainMenuVisible then |
| 960 |
begin |
| 961 |
window_accel_group := gtk_accel_group_new; |
| 962 |
gtk_window_add_accel_group(GTK_WINDOW(WindowGtk), window_accel_group); |
| 963 |
|
| 964 |
window_vbox := PGtkVBox(gtk_vbox_new(gfalse, VBoxSpacing)); |
| 965 |
gtk_widget_show(GTK_WIDGET(window_vbox)); |
| 966 |
MenuInitialize; |
| 967 |
gtk_box_pack_end_defaults(PGtkBox(window_vbox), GTK_WIDGET(GLAreaGtk)); |
| 968 |
gtk_container_add(GTK_CONTAINER(WindowGtk), GTK_WIDGET(window_vbox)); |
| 969 |
end else |
| 970 |
gtk_container_add(GTK_CONTAINER(WindowGtk), GTK_WIDGET(GLAreaGtk)); |
| 971 |
|
| 972 |
{ setup window/glarea size and resize policy from |
| 973 |
MinWidth, MinHeight, Width, Height, ResizeAllowed } |
| 974 |
if FullScreen then |
| 975 |
begin |
| 976 |
{ Applying FullScreen changes to the Left / Top / Width / Height. |
| 977 |
Call DoResize (not perfect, since it doesn't account for menu bar |
| 978 |
height, but better than nothing), in case GTK will not send |
| 979 |
us proper resize event (but it will send up proper resize event, |
| 980 |
tests show). } |
| 981 |
FLeft := 0; |
| 982 |
FTop := 0; |
| 983 |
DoResize(Application.ScreenWidth, Application.ScreenHeight, false); |
| 984 |
|
| 985 |
{ When in FullScreen we set whole window size, not just GLArea size, to screen size. |
| 986 |
This is the ultimate sense of FullScreen, |
| 987 |
so there is no discussion, no matter what value has ResizeAllowed. |
| 988 |
So I'm doing gtk_widget_set_usize(WindowGtk, ...) and |
| 989 |
gtk_window_set_default_size |
| 990 |
instead of |
| 991 |
gtk_widget_set_usize(GLAreaGtk, ...) and |
| 992 |
set_default_glarea_size |
| 993 |
} |
| 994 |
gtk_widget_set_usize(GTK_WIDGET(WindowGtk), MinWidth, MinHeight); |
| 995 |
gtk_window_set_default_size(WindowGtk, Application.ScreenWidth, Application.ScreenHeight); |
| 996 |
|
| 997 |
gtk_window_fullscreen(WindowGtk); |
| 998 |
end else |
| 999 |
begin |
| 1000 |
if ResizeAllowed <> raAllowed then |
| 1001 |
gtk_widget_set_usize(GTK_WIDGET(GLAreaGtk), Width, Height) |
| 1002 |
else |
| 1003 |
gtk_widget_set_usize(GTK_WIDGET(GLAreaGtk), MinWidth, MinHeight); |
| 1004 |
set_default_glarea_size(Width, Height); |
| 1005 |
gtk_window_set_resizable(WindowGtk, ResizeAllowed = raAllowed); |
| 1006 |
end; |
| 1007 |
|
| 1008 |
{ Honour display (XScreenGdk is default screen on chosen display) |
| 1009 |
chosen by user. } |
| 1010 |
gtk_window_set_screen(WindowGtk, Application.XScreenGdk); |
| 1011 |
|
| 1012 |
if Visible then |
| 1013 |
begin |
| 1014 |
{ Show WindowGtk, it will also make visible all other widgets. |
| 1015 |
(Their "gtk_widget_show" calls only scheduled them to be shown |
| 1016 |
when this top-level window is shown.) |
| 1017 |
|
| 1018 |
gtk_widget_show also realizes (allocates X resources) the widget |
| 1019 |
and all it's children before returning (but does not necessarily |
| 1020 |
map the Gdk window *now*, one can use gtk_widget_show_now for this; |
| 1021 |
but I don't need it) } |
| 1022 |
gtk_widget_show(GTK_WIDGET(WindowGtk)); |
| 1023 |
end else |
| 1024 |
begin |
| 1025 |
{ We want to realize all our GTK resources now, to finish |
| 1026 |
our initialization. So we can call gtk_widget_realize. |
| 1027 |
This automatically realizes parents of passed object, so if I pass here |
| 1028 |
GLAreaGtk, then also WindowGtk will get realized, and that's all I need. |
| 1029 |
|
| 1030 |
See [http://library.gnome.org/devel/gtk/stable/GtkWidget.html#gtk-widget-realize] |
| 1031 |
and [http://developer.gnome.org/doc/GGAD/faqs.html] |
| 1032 |
("When do I need to call gtk_widget_realize() vs. gtk_widget_show()?" |
| 1033 |
question). } |
| 1034 |
gtk_widget_realize(GTK_WIDGET(GLAreaGtk)); |
| 1035 |
end; |
| 1036 |
|
| 1037 |
{$ifdef USE_EGL} |
| 1038 |
{ In case of EGL we really need WindowXID. |
| 1039 |
So we cannot use previous workaround, instead we wait for a good time |
| 1040 |
to get WindowXID, when widget is realized. |
| 1041 |
TODO: Should we just do it with glX version too? } |
| 1042 |
{ TODO: WindowXID is only with X, so for now CASTLE_WINDOW_GTK_2 |
| 1043 |
cannot work with non-Unix (like Windows). } |
| 1044 |
ContextCreate(WindowXID); |
| 1045 |
{$endif} |
| 1046 |
|
| 1047 |
{ From this point I require that WindowGtk is realized (has associated |
| 1048 |
GdkWindow, i.e. I can use GTK_WIDGET(WindowGtk).Window). |
| 1049 |
It also means that I can do MakeCurrent (and call OpenGL commands) |
| 1050 |
from now. In particular, we have valid gl context when we leave OpenBackend. } |
| 1051 |
if (GTK_REALIZED and GTK_WIDGET_FLAGS( GTK_WIDGET(WindowGtk) )) = 0 then |
| 1052 |
raise EInternalError.Create('CastleWindow.OpenBackend: GdkWindow not realized yet'); |
| 1053 |
|
| 1054 |
GetInitialCursorPos; |
| 1055 |
UpdateCursor; |
| 1056 |
InitializeDpi; |
| 1057 |
end; |
| 1058 |
|
| 1059 |
procedure TCastleWindow.CloseBackend; |
| 1060 |
begin |
| 1061 |
{ Do not unref here GLAreaGtk. |
| 1062 |
GLAreaGtk is placed inside WindowGtk when we do |
| 1063 |
gtk_container_add(WindowGtk, GLAreaGtk). During gtk_container_add |
| 1064 |
WindowGtk calls sink on GLAreaGtk, so GLAreaGtk has reference 1 and it |
| 1065 |
means that it is referenced from WindowGtk. So WindowGtk is responsible |
| 1066 |
for calling unref on GLAreaGtk. } |
| 1067 |
GLAreaGtk := nil; |
| 1068 |
|
| 1069 |
if (MainMenu <> nil) and MainMenuVisible then |
| 1070 |
MenuFinalize; |
| 1071 |
|
| 1072 |
window_vbox := nil; |
| 1073 |
|
| 1074 |
if window_accel_group <> nil then |
| 1075 |
begin |
| 1076 |
g_object_unref(G_OBJECT(window_accel_group)); |
| 1077 |
window_accel_group := nil; |
| 1078 |
end; |
| 1079 |
|
| 1080 |
{ Note: we could use |
| 1081 |
gtk_quit_add_destroy(1, GTK_OBJECT(WindowGtk)); |
| 1082 |
in OpenBackend instead of doing gtk_widget_unref below. |
| 1083 |
But doing gtk_widget_unref below is a cleaner way (we're freeing WindowGtk |
| 1084 |
as soon as it can be freed). } |
| 1085 |
if WindowGtk <> nil then |
| 1086 |
begin |
| 1087 |
if Visible then |
| 1088 |
gtk_widget_hide(GTK_WIDGET(WindowGtk)); |
| 1089 |
|
| 1090 |
ContextDestroy; |
| 1091 |
|
| 1092 |
{ Doing here |
| 1093 |
gtk_widget_unref(GTK_WIDGET(WindowGtk)); |
| 1094 |
instead of gtk_widget_destroy causes |
| 1095 |
Gtk-CRITICAL **: file gtkobject.c: line 1179 (gtk_object_unref): |
| 1096 |
assertion `object->ref_count > 0' failed. |
| 1097 |
|
| 1098 |
If I understand some comments (e.g. at GtkWidget.gtk_widget_destroy) |
| 1099 |
properly GTK+ holds a reference to toplevel windows. Again, if I understand |
| 1100 |
it properly, this means that my program is NOT treated as the owner |
| 1101 |
of a reference to WindowGtk. So my program should not do unref on |
| 1102 |
WindowGtk. Instead my program should call gtk_widget_destroy and |
| 1103 |
gtk_widget_destroy will automatically do unref to release the |
| 1104 |
reference GTK+ has to toplevel WindowGtk window (and gtk_widget_destroy |
| 1105 |
will also tell GTK+ to remove WindowGtk from it's internal list of |
| 1106 |
toplevel windows and do some other needed by GTK+ things). } |
| 1107 |
gtk_widget_destroy(GTK_WIDGET(WindowGtk)); |
| 1108 |
WindowGtk := nil; |
| 1109 |
|
| 1110 |
{$ifndef USE_EGL} |
| 1111 |
ContextDestroyLate; |
| 1112 |
{$endif} |
| 1113 |
end; |
| 1114 |
end; |
| 1115 |
|
| 1116 |
procedure TCastleWindow.CreateBackend; |
| 1117 |
begin |
| 1118 |
{ We don't do here anything, and thus it doesn't require calling |
| 1119 |
Application.InitializeXDisplay. This way, merely calling |
| 1120 |
TCastleWindow.Create doesn't require X server runnning. } |
| 1121 |
end; |
| 1122 |
|
| 1123 |
{ TCastleWindow.*Dialog --------------------------------------------------- } |
| 1124 |
|
| 1125 |
type |
| 1126 |
{ Data type for GtkDialogRun (both variants). |
| 1127 |
PDialogData will be passed as a 2nd argument to signals |
| 1128 |
(and 3rd argument to events) associated with dialog boxes. } |
| 1129 |
TDialogData = record |
| 1130 |
Answered: boolean; { = false } |
| 1131 |
Answer: gint; |
| 1132 |
end; |
| 1133 |
PDialogData = ^TDialogData; |
| 1134 |
|
| 1135 |
procedure signal_dialog_ok_clicked(AWidget: PGtkWidget; Data: PDialogData); cdecl; |
| 1136 |
begin |
| 1137 |
Data^.Answered := true; |
| 1138 |
Data^.Answer := GTK_RESPONSE_ACCEPT; |
| 1139 |
end; |
| 1140 |
|
| 1141 |
procedure signal_dialog_cancel_clicked(AWidget: PGtkWidget; |
| 1142 |
Data: PDialogData); cdecl; |
| 1143 |
begin |
| 1144 |
Data^.Answered := true; |
| 1145 |
Data^.Answer := GTK_RESPONSE_REJECT; |
| 1146 |
end; |
| 1147 |
|
| 1148 |
function signal_dialog_delete_event(AWidget: PGtkWidget; Event: PGdkEventAny; |
| 1149 |
Data: PDialogData): gboolean; cdecl; |
| 1150 |
begin |
| 1151 |
Data^.Answered := true; |
| 1152 |
Data^.Answer := GTK_RESPONSE_DELETE_EVENT; |
| 1153 |
{ don't allow to call gtk_widget_destroy, we will call it ourselves |
| 1154 |
in BackendFileDialog/ColorDialog. } |
| 1155 |
Result := gtrue; |
| 1156 |
end; |
| 1157 |
|
| 1158 |
procedure signal_dialog_unmap(ADialog: PGtkDialog; Data: PDialogData); cdecl; |
| 1159 |
begin |
| 1160 |
Data^.Answered := true; |
| 1161 |
Data^.Answer := GTK_RESPONSE_NONE; |
| 1162 |
end; |
| 1163 |
|
| 1164 |
procedure signal_dialog_response(ADialog: PGtkDialog; ResponseId: GInt; |
| 1165 |
Data: PDialogData); cdecl; |
| 1166 |
begin |
| 1167 |
Data^.Answered := true; |
| 1168 |
Data^.Answer := ResponseId; |
| 1169 |
end; |
| 1170 |
|
| 1171 |
function TCastleWindow.GtkDialogRun(ADialog: PGtkWindow; |
| 1172 |
ok_button, cancel_button: PGtkWidget): boolean; |
| 1173 |
var |
| 1174 |
Data: TDialogData; |
| 1175 |
Mode: TGLModeFrozenScreen; |
| 1176 |
begin |
| 1177 |
gtk_signal_connect(GTK_OBJECT(ok_button), |
| 1178 |
'clicked', GTK_SIGNAL_FUNC(@signal_dialog_ok_clicked), @Data); |
| 1179 |
gtk_signal_connect(GTK_OBJECT(cancel_button), |
| 1180 |
'clicked', GTK_SIGNAL_FUNC(@signal_dialog_cancel_clicked), @Data); |
| 1181 |
gtk_signal_connect(GTK_OBJECT(ADialog), |
| 1182 |
'delete_event', GTK_SIGNAL_FUNC(@signal_dialog_delete_event), @Data); |
| 1183 |
|
| 1184 |
gtk_window_set_modal(ADialog, gtrue); |
| 1185 |
gtk_window_set_transient_for(ADialog, WindowGtk); |
| 1186 |
|
| 1187 |
{ prepare Data } |
| 1188 |
Data.Answered := false; |
| 1189 |
|
| 1190 |
Mode := TGLModeFrozenScreen.Create(Self); |
| 1191 |
try |
| 1192 |
gtk_widget_show(GTK_WIDGET(ADialog)); |
| 1193 |
while not Data.Answered do Application.ProcessMessage(true, true); |
| 1194 |
finally Mode.Free end; |
| 1195 |
|
| 1196 |
Result := Data.Answer = GTK_RESPONSE_ACCEPT; |
| 1197 |
end; |
| 1198 |
|
| 1199 |
function TCastleWindow.GtkDialogRun(ADialog: PGtkDialog): gint; |
| 1200 |
var |
| 1201 |
Data: TDialogData; |
| 1202 |
Mode: TGLModeFrozenScreen; |
| 1203 |
SigHandle1, SigHandle2, SigHandle3: GULong; |
| 1204 |
begin |
| 1205 |
SigHandle1 := gtk_signal_connect(GTK_OBJECT(ADialog), |
| 1206 |
'response', GTK_SIGNAL_FUNC(@signal_dialog_response), @Data); |
| 1207 |
SigHandle2 := gtk_signal_connect(GTK_OBJECT(ADialog), |
| 1208 |
'unmap', GTK_SIGNAL_FUNC(@signal_dialog_unmap), @Data); |
| 1209 |
SigHandle3 := gtk_signal_connect(GTK_OBJECT(ADialog), |
| 1210 |
'delete_event',GTK_SIGNAL_FUNC(@signal_dialog_delete_event), @Data); |
| 1211 |
|
| 1212 |
{ gtk_dialog_run contains here also safeguards against destroying |
| 1213 |
the dialog when it runs, by using g_object_ref/unref around and |
| 1214 |
registering signal on destroy of ADialog. We do not need this, |
| 1215 |
nothing can destroy the dialog AFAIK. } |
| 1216 |
|
| 1217 |
gtk_window_set_modal(GTK_WINDOW(ADialog), gtrue); |
| 1218 |
|
| 1219 |
Data.Answered := false; |
| 1220 |
|
| 1221 |
Mode := TGLModeFrozenScreen.Create(Self); |
| 1222 |
try |
| 1223 |
gtk_widget_show(GTK_WIDGET(ADialog)); |
| 1224 |
while not Data.Answered do Application.ProcessMessage(true, true); |
| 1225 |
finally Mode.Free end; |
| 1226 |
|
| 1227 |
{ Disconnect these signal handlers. gtk_dialog_run also does this. |
| 1228 |
|
| 1229 |
Otherwise under x86_64/Linux we get segfaults at gtk_widget_destroy |
| 1230 |
(usually done by the caller of GtkDialogRun). |
| 1231 |
Seen both with FPC 2.2.4 and 2.4.0, probably FPC version doesn't matter. |
| 1232 |
These are probably caused by gtk_widget_destroy calling some signal |
| 1233 |
(probably 'unmap') when destroying, that gets invalid @Data pointer |
| 1234 |
and messes with memory. } |
| 1235 |
gtk_signal_disconnect(GTK_OBJECT(ADialog), SigHandle1); |
| 1236 |
gtk_signal_disconnect(GTK_OBJECT(ADialog), SigHandle2); |
| 1237 |
gtk_signal_disconnect(GTK_OBJECT(ADialog), SigHandle3); |
| 1238 |
|
| 1239 |
Result := Data.Answer; |
| 1240 |
end; |
| 1241 |
|
| 1242 |
function GtkVersionAtLeast(const Major, Minor, Micro: Cardinal): boolean; |
| 1243 |
begin |
| 1244 |
Result := |
| 1245 |
(gtk_major_version > Major) or |
| 1246 |
((gtk_major_version = Major) and |
| 1247 |
(gtk_minor_version > Minor) or |
| 1248 |
((gtk_minor_version = Minor) and |
| 1249 |
(gtk_micro_version >= Micro) )); |
| 1250 |
end; |
| 1251 |
|
| 1252 |
function TCastleWindow.BackendFileDialog(const Title: String; var FileName: String; |
| 1253 |
OpenDialog: boolean; FileFilters: TFileFilterList): boolean; |
| 1254 |
|
| 1255 |
{ FileChooser is available only in GTK >= 2.4. |
| 1256 |
We assume that everyone with GTK 2 has also newer GTK 2, >= 2.4. |
| 1257 |
So we just always use GtkFileChooser, old implementation using |
| 1258 |
GtkFileSelection is removed. } |
| 1259 |
|
| 1260 |
var |
| 1261 |
HasDefaultFilter: boolean; |
| 1262 |
|
| 1263 |
procedure DialogAddFilters(Dialog: PGtkFileChooserDialog; |
| 1264 |
FileFilters: TFileFilterList); |
| 1265 |
var |
| 1266 |
I, J: Integer; |
| 1267 |
GtkFilter: PGtkFileFilter; |
| 1268 |
Filter: TFileFilter; |
| 1269 |
begin |
| 1270 |
for I := 0 to FileFilters.Count - 1 do |
| 1271 |
begin |
| 1272 |
GtkFilter := gtk_file_filter_new(); |
| 1273 |
Filter := FileFilters[I]; |
| 1274 |
|
| 1275 |
gtk_file_filter_set_name(GtkFilter, PChar(Filter.Name)); |
| 1276 |
for J := 0 to Filter.Patterns.Count - 1 do |
| 1277 |
gtk_file_filter_add_pattern(GtkFilter, PChar(Filter.Patterns[J])); |
| 1278 |
gtk_file_chooser_add_filter(Dialog, GtkFilter); |
| 1279 |
|
| 1280 |
if I = FileFilters.DefaultFilter then |
| 1281 |
begin |
| 1282 |
gtk_file_chooser_set_filter(Dialog, GtkFilter); |
| 1283 |
HasDefaultFilter := true; |
| 1284 |
end; |
| 1285 |
end; |
| 1286 |
end; |
| 1287 |
|
| 1288 |
var |
| 1289 |
Action: TGtkFileChooserAction; |
| 1290 |
OkButtonText: PChar; |
| 1291 |
Dialog: PGtkFileChooserDialog; |
| 1292 |
CFileName: PChar; |
| 1293 |
ExpandedFileName, Dir: String; |
| 1294 |
begin |
| 1295 |
if OpenDialog then |
| 1296 |
begin |
| 1297 |
Action := GTK_FILE_CHOOSER_ACTION_OPEN; |
| 1298 |
OkButtonText := PChar(GTK_STOCK_OPEN); |
| 1299 |
end else |
| 1300 |
begin |
| 1301 |
Action := GTK_FILE_CHOOSER_ACTION_SAVE; |
| 1302 |
OkButtonText := PChar(GTK_STOCK_SAVE); |
| 1303 |
end; |
| 1304 |
|
| 1305 |
Dialog := PGtkFileChooserDialog(gtk_file_chooser_dialog_new( |
| 1306 |
PChar(Title), WindowGtk, Action, |
| 1307 |
PChar(GTK_STOCK_CANCEL), [ GTK_RESPONSE_CANCEL, |
| 1308 |
OkButtonText, GTK_RESPONSE_ACCEPT, |
| 1309 |
nil ])); |
| 1310 |
|
| 1311 |
HasDefaultFilter := false; |
| 1312 |
|
| 1313 |
if FileFilters <> nil then |
| 1314 |
DialogAddFilters(Dialog, FileFilters); |
| 1315 |
|
| 1316 |
if not OpenDialog then |
| 1317 |
begin |
| 1318 |
{ The overwrite_confirmation stuff is only in GTK >= 2.8. |
| 1319 |
For example fink (stable) has only GTK 2.6 for now, |
| 1320 |
so we use them only if they can be loaded from GTK lib. } |
| 1321 |
if Assigned(@gtk_file_chooser_set_do_overwrite_confirmation) then |
| 1322 |
gtk_file_chooser_set_do_overwrite_confirmation(Dialog, gtrue); |
| 1323 |
end; |
| 1324 |
|
| 1325 |
{ We don't want to simply call gtk_file_chooser_set_filename, |
| 1326 |
as it doesn't behave like we want in all cases. |
| 1327 |
|
| 1328 |
gtk_file_chooser_set_filename changes to the dir with file. |
| 1329 |
If the file exists, _set_filename will select it. |
| 1330 |
_set_filename will do nothing if file is not on the list, |
| 1331 |
so in this case it's better to _set_current_name. } |
| 1332 |
|
| 1333 |
if FileName = '' then |
| 1334 |
begin |
| 1335 |
{ We explicitly allow FileName = '' in the interface of BackendFileDialog, |
| 1336 |
it's equal to FileName = GetCurrentDir (with PathDelim at the end) } |
| 1337 |
gtk_file_chooser_set_current_folder(Dialog, PChar(GetCurrentDir)); |
| 1338 |
end else |
| 1339 |
begin |
| 1340 |
{ GTK likes to get full pathnames, otherwise things like |
| 1341 |
|
| 1342 |
(bump_mapping:5494): libgnomevfs-CRITICAL **: |
| 1343 |
gnome_vfs_get_uri_from_local_path: |
| 1344 |
assertion `g_path_is_absolute (local_full_path)' failed |
| 1345 |
|
| 1346 |
happen. } |
| 1347 |
|
| 1348 |
ExpandedFileName := ExpandFileName(FileName); |
| 1349 |
|
| 1350 |
{ Test in one go whether file exists, and is a regular file or dir. } |
| 1351 |
if FileExists(ExpandedFileName) then |
| 1352 |
begin |
| 1353 |
if DirectoryExists(ExpandedFileName) then |
| 1354 |
gtk_file_chooser_set_current_folder(Dialog, PChar(ExpandedFileName)) else |
| 1355 |
begin |
| 1356 |
if HasDefaultFilter and not GtkVersionAtLeast(2, 24, 4) then |
| 1357 |
begin |
| 1358 |
{ Unfortunately, the default filter will be ignored after |
| 1359 |
gtk_file_chooser_set_filename |
| 1360 |
(even if your FileName matches with the default filter). |
| 1361 |
See [http://www.mail-archive.com/[email protected]/msg29044.html]. |
| 1362 |
This makes ugly dialog (with by default all files shown, and no |
| 1363 |
filter selected in combo box), so avoid it: |
| 1364 |
workaround is to set only directory. |
| 1365 |
|
| 1366 |
Note that adding gtk_file_chooser_set_current_name |
| 1367 |
with ExtractFileName(ExpandedFileName) |
| 1368 |
(this would be a little nicer workaround) |
| 1369 |
after setting dir has no effect, no idea why. |
| 1370 |
|
| 1371 |
This problem is present in GTK 2.18 (Debian lenny), |
| 1372 |
not present in GTK 2.24.4 (Debian wheezy on 2011-07, also Ubuntu 11.4). } |
| 1373 |
ExpandedFileName := ExtractFilePath(ExpandedFileName); |
| 1374 |
gtk_file_chooser_set_current_folder(Dialog, PChar(ExpandedFileName)); |
| 1375 |
end else |
| 1376 |
gtk_file_chooser_set_filename(Dialog, PChar(ExpandedFileName)); |
| 1377 |
end; |
| 1378 |
end else |
| 1379 |
begin |
| 1380 |
Dir := ExtractFileDir(ExpandedFileName); |
| 1381 |
if Dir = '' then |
| 1382 |
Dir := GetCurrentDir; |
| 1383 |
gtk_file_chooser_set_current_folder(Dialog, PChar(Dir)); |
| 1384 |
|
| 1385 |
{ GTK 2 makes warnings |
| 1386 |
|
| 1387 |
(bezier_curves:32196): Gtk-CRITICAL **: |
| 1388 |
gtk_file_chooser_default_set_current_name: |
| 1389 |
assertion `impl->action == GTK_FILE_CHOOSER_ACTION_SAVE || |
| 1390 |
impl->action == GTK_FILE_CHOOSER_ACTION_CREATE_FOLDER' failed |
| 1391 |
|
| 1392 |
when trying to set non-existing base filename for an "open" dialog. |
| 1393 |
That's Ok, I mean non-existing base filename actually doesn't make sense |
| 1394 |
for open dialog... |
| 1395 |
So we don't do it, simply ignoring ExtractFileName(FileName). } |
| 1396 |
if not OpenDialog then |
| 1397 |
begin |
| 1398 |
gtk_file_chooser_set_current_name(Dialog, |
| 1399 |
PChar(ExtractFileName(ExpandedFileName))); |
| 1400 |
end; |
| 1401 |
end; |
| 1402 |
end; |
| 1403 |
|
| 1404 |
{ The Dialog is set to modal, it's shown, |
| 1405 |
buttons cause exit with proper response. } |
| 1406 |
Result := GtkDialogRun(GTK_DIALOG(Dialog)) = GTK_RESPONSE_ACCEPT; |
| 1407 |
|
| 1408 |
if Result then |
| 1409 |
begin |
| 1410 |
CFileName := gtk_file_chooser_get_filename(Dialog); |
| 1411 |
{ This copies string contents, so we can later free CFileName pointer. } |
| 1412 |
FileName := CFileName; |
| 1413 |
g_free(CFileName); |
| 1414 |
end; |
| 1415 |
|
| 1416 |
gtk_widget_destroy(GTK_WIDGET(Dialog)); |
| 1417 |
end; |
| 1418 |
|
| 1419 |
function TCastleWindow.ColorDialog(var Color: TCastleColor): Boolean; |
| 1420 |
var |
| 1421 |
Dialog: PGtkColorSelectionDialog; |
| 1422 |
GtkColor: array [0..3] of GDouble; |
| 1423 |
i: Integer; |
| 1424 |
begin |
| 1425 |
Dialog := PGtkColorSelectionDialog(gtk_color_selection_dialog_new('Choose color')); |
| 1426 |
try |
| 1427 |
for i := 0 to 3 do GtkColor[i] := Color[i]; |
| 1428 |
gtk_color_selection_set_color(PGtkColorSelection(Dialog^.colorsel), @GtkColor); |
| 1429 |
|
| 1430 |
Result := GtkDialogRun(PGtkWindow(Dialog), |
| 1431 |
Dialog^.ok_button, Dialog^.cancel_button); |
| 1432 |
|
| 1433 |
if Result then |
| 1434 |
begin |
| 1435 |
gtk_color_selection_get_color(PGtkColorSelection(Dialog^.colorsel), @GtkColor); |
| 1436 |
{ Note: Do not touch Color.Data[3], leave it unchanged. Reason: |
| 1437 |
While https://developer-old.gnome.org/gtk2/2.24/GtkColorSelection.html#gtk-color-selection-set-color |
| 1438 |
says it wants a pointer to 4 components, including alpha, but actually alpha treatment is useless: |
| 1439 |
- user cannot edit alpha |
| 1440 |
- on return, it seems always 65535.0 (why this, max of Word? it's in Double...) |
| 1441 |
} |
| 1442 |
for i := 0 to 2 do |
| 1443 |
Color.Data[i] := GtkColor[i]; |
| 1444 |
end; |
| 1445 |
finally gtk_widget_destroy(GTK_WIDGET(Dialog)); end; |
| 1446 |
end; |
| 1447 |
|
| 1448 |
{ We would like to map to TGtkMessageType, |
| 1449 |
but value GTK_MESSAGE_OTHER is missing from FPC bindings |
| 1450 |
and it prevents compilation in FPC >= 2.7.1. } |
| 1451 |
|
| 1452 |
const |
| 1453 |
MessageTypeToGtk: array [TWindowMessageType] of Integer = |
| 1454 |
( Ord(GTK_MESSAGE_INFO), |
| 1455 |
Ord(GTK_MESSAGE_WARNING), |
| 1456 |
Ord(GTK_MESSAGE_QUESTION), |
| 1457 |
Ord(GTK_MESSAGE_ERROR), |
| 1458 |
{ GTK_MESSAGE_OTHER, missing from FPC bindings } 4 ); |
| 1459 |
|
| 1460 |
procedure TCastleWindow.MessageOK(const S: String; const MessageType: TWindowMessageType); |
| 1461 |
var |
| 1462 |
Dialog: PGtkMessageDialog; |
| 1463 |
SNew: String; |
| 1464 |
begin |
| 1465 |
SNew := SUnformattable(S); // gtk_message_dialog_new formats argument using printf, prevent it |
| 1466 |
Dialog := PGtkMessageDialog(gtk_message_dialog_new(WindowGtk, 0, |
| 1467 |
TGtkMessageType(MessageTypeToGtk[MessageType]), GTK_BUTTONS_OK, PChar(SNew))); |
| 1468 |
try |
| 1469 |
GtkDialogRun(GTK_DIALOG(Dialog)); |
| 1470 |
finally gtk_widget_destroy(GTK_WIDGET(Dialog)); end; |
| 1471 |
end; |
| 1472 |
|
| 1473 |
function TCastleWindow.MessageYesNo(const S: String; const MessageType: TWindowMessageType): boolean; |
| 1474 |
var |
| 1475 |
Dialog: PGtkMessageDialog; |
| 1476 |
SNew: String; |
| 1477 |
begin |
| 1478 |
SNew := SUnformattable(S); // gtk_message_dialog_new formats argument using printf, prevent it |
| 1479 |
Dialog := PGtkMessageDialog(gtk_message_dialog_new(WindowGtk, 0, |
| 1480 |
TGtkMessageType(MessageTypeToGtk[MessageType]), GTK_BUTTONS_YES_NO, PChar(SNew))); |
| 1481 |
try |
| 1482 |
Result := GtkDialogRun(GTK_DIALOG(Dialog)) = GTK_RESPONSE_YES; |
| 1483 |
finally gtk_widget_destroy(GTK_WIDGET(Dialog)); end; |
| 1484 |
end; |
| 1485 |
|
| 1486 |
procedure TCastleWindow.UpdateFullScreenBackend; |
| 1487 |
begin |
| 1488 |
if FFullScreenBackend <> FFullScreenWanted then |
| 1489 |
begin |
| 1490 |
FFullScreenBackend := FFullScreenWanted; |
| 1491 |
if not Closed then |
| 1492 |
begin |
| 1493 |
if FFullScreenBackend then |
| 1494 |
gtk_window_fullscreen(WindowGtk) |
| 1495 |
else |
| 1496 |
gtk_window_unfullscreen(WindowGtk); |
| 1497 |
end; |
| 1498 |
end; |
| 1499 |
end; |
| 1500 |
|
| 1501 |
procedure TCastleWindow.BackendInsideUpdate; |
| 1502 |
begin |
| 1503 |
end; |
| 1504 |
|
| 1505 |
{ TGTKClipboard ----------------------------------------------------------- } |
| 1506 |
|
| 1507 |
{ TODO: Shouldn't we convert here UTF8<->system, like in LCL backend? |
| 1508 |
GTK always uses UTF8, which may not be our system encoding. |
| 1509 |
|
| 1510 |
TODO: gtk_clipboard_wait_for_text runs the main loop. |
| 1511 |
We don't really want it (it would require blocking the main UI usually, |
| 1512 |
which makes the whole asynchronous fun useless --- if we want to block, |
| 1513 |
then why not just wait for the clipboard, instead of running app loop?). |
| 1514 |
|
| 1515 |
Hm, although LCL also does here waiting by Application.ProcessMessages |
| 1516 |
inside lcl/interfaces/gtk/gtkproc.inc inside WaitForClipboardAnswer. |
| 1517 |
} |
| 1518 |
|
| 1519 |
type |
| 1520 |
TGTKClipboard = class(TCastleClipboard) |
| 1521 |
protected |
| 1522 |
function GetAsText: String; override; |
| 1523 |
procedure SetAsText(const Value: String); override; |
| 1524 |
end; |
| 1525 |
|
| 1526 |
function TGTKClipboard.GetAsText: String; |
| 1527 |
var |
| 1528 |
C: PGtkClipboard; |
| 1529 |
begin |
| 1530 |
C := gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
| 1531 |
Result := gtk_clipboard_wait_for_text(C); |
| 1532 |
end; |
| 1533 |
|
| 1534 |
procedure TGTKClipboard.SetAsText(const Value: String); |
| 1535 |
var |
| 1536 |
C: PGtkClipboard; |
| 1537 |
begin |
| 1538 |
C := gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
| 1539 |
gtk_clipboard_set_text(C, PChar(Value), Length(Value)); |
| 1540 |
end; |
| 1541 |
|
| 1542 |
{ TCastleApplication ------------------------------------------------------------- } |
| 1543 |
|
| 1544 |
procedure TCastleApplication.ProcessUpdates; |
| 1545 |
begin |
| 1546 |
{ Render the invalidated windows. |
| 1547 |
And call Update and Timer events of Application and all windows. |
| 1548 |
|
| 1549 |
Note: Right now we do not use normal GTK mechanism for redraws |
| 1550 |
(that is, gtk_widget_queue_draw for posting and handling signal "expose" |
| 1551 |
to call DoRender). In the past, this caused problems, and in particular |
| 1552 |
failed totally when in newer GTK/glib expose signal couldn't be |
| 1553 |
delivered when we were already inside one expose signal handler. |
| 1554 |
|
| 1555 |
Also, we use our own timer mechanism, without depending on GTK timers. |
| 1556 |
This makes our code consistent across platforms. } |
| 1557 |
|
| 1558 |
UpdateAndRenderEverything; |
| 1559 |
end; |
| 1560 |
|
| 1561 |
function TCastleApplication.TryFindWindowGtk(seekWindowGtk: PGtkWindow): TCastleWindow; |
| 1562 |
var |
| 1563 |
i: Integer; |
| 1564 |
begin |
| 1565 |
for i := 0 to OpenWindowsCount - 1 do |
| 1566 |
if OpenWindows[i].WindowGtk = seekWindowGtk then |
| 1567 |
Exit(OpenWindows[i]); |
| 1568 |
Result := nil; |
| 1569 |
end; |
| 1570 |
|
| 1571 |
function TCastleApplication.TryFindGLAreaGtk(seekGLAreaGtk: PGtkGLArea): TCastleWindow; |
| 1572 |
var |
| 1573 |
i: Integer; |
| 1574 |
begin |
| 1575 |
for i := 0 to OpenWindowsCount-1 do |
| 1576 |
if OpenWindows[i].GLAreaGtk = seekGLAreaGtk then |
| 1577 |
Exit(OpenWindows[i]); |
| 1578 |
Result := nil; |
| 1579 |
end; |
| 1580 |
|
| 1581 |
function TCastleApplication.FindWindowGtk(seekWindowGtk: PGtkWindow): TCastleWindow; |
| 1582 |
begin |
| 1583 |
Result := TryFindWindowGtk(seekWindowGtk); |
| 1584 |
if Result = nil then raise EInternalError.Create( |
| 1585 |
'No open window with such WindowGtk found'); |
| 1586 |
end; |
| 1587 |
|
| 1588 |
function TCastleApplication.FindGLAreaGtk(seekGLAreaGtk: PGtkGLArea): TCastleWindow; |
| 1589 |
begin |
| 1590 |
Result := TryFindGLAreaGtk(seekGLAreaGtk); |
| 1591 |
if Result = nil then raise EInternalError.Create( |
| 1592 |
'No open window with such GLAreaGtk found'); |
| 1593 |
end; |
| 1594 |
|
| 1595 |
procedure TCastleApplication.Run; |
| 1596 |
begin |
| 1597 |
if OpenWindowsCount = 0 then Exit; |
| 1598 |
inherited Run; // calls DoRun, surrounded by HandleException, in a loop |
| 1599 |
|
| 1600 |
{ Do not replace this with gtk_main(). |
| 1601 |
We need to use our ProcessMessage to call our ProcessUpdates. } |
| 1602 |
end; |
| 1603 |
|
| 1604 |
function TCastleApplication.ProcessAllMessages: boolean; |
| 1605 |
begin |
| 1606 |
{ Basically, call gtk_main_iteration (equivalent to |
| 1607 |
gtk_main_iteration_do with parameter blocking = always true), |
| 1608 |
only while gtk_events_pending (so we know it will not actually block). |
| 1609 |
|
| 1610 |
Moreover, call ProcessUpdates in the middle. |
| 1611 |
|
| 1612 |
Summing it up, just call ProcessMessage(true) while gtk_events_pending. } |
| 1613 |
|
| 1614 |
Result := not Terminated; |
| 1615 |
|
| 1616 |
while Result and (gtk_events_pending() <> 0) do |
| 1617 |
begin |
| 1618 |
{ Since we know that gtk_events_pending() <> 0, the 1st parameter WaitForMessage |
| 1619 |
below for ProcessMessage doesn't matter: we know we will never wait for |
| 1620 |
message, since we just made sure that we have some message. |
| 1621 |
Only 2nd parameter WaitToLimitFPS matters. } |
| 1622 |
Result := ProcessMessage(false, false); |
| 1623 |
end; |
| 1624 |
|
| 1625 |
if Result then |
| 1626 |
begin |
| 1627 |
{ We know now gtk_events_pending() = 0. So call ProcessUpdates now |
| 1628 |
(otherwise ProcessAllMessages would never do ProcessUpdates, |
| 1629 |
since we call ProcessMessage only when gtk_events_pending() <> 0, |
| 1630 |
which then avoids ProcessUpdates.) } |
| 1631 |
ProcessUpdates; |
| 1632 |
Result := not Terminated; |
| 1633 |
end; |
| 1634 |
end; |
| 1635 |
|
| 1636 |
function TCastleApplication.ProcessMessage(WaitForMessage, WaitToLimitFPS: boolean): boolean; |
| 1637 |
var |
| 1638 |
WasAnyMessage: boolean; |
| 1639 |
begin |
| 1640 |
(* Note that gtk_main_iteration_do result is not useful to us. |
| 1641 |
That is, we can't just call here |
| 1642 |
|
| 1643 |
Result := not GBoolToPas(gtk_main_iteration_do(WaitForMessage)) |
| 1644 |
|
| 1645 |
because gtk_main_iteration_do always returns true if the program |
| 1646 |
doesn't use gtk_main call at all (i.e. does it's whole loop manually, |
| 1647 |
by calling Application.ProcessMessage). |
| 1648 |
|
| 1649 |
Confirmed by a look at gtk source code |
| 1650 |
(http://git.gnome.org/browse/gtk+/tree/gtk/gtkmain.c?h=gtk-2-18), |
| 1651 |
gtk_main_iteration_do ends like this: |
| 1652 |
|
| 1653 |
if (main_loops) |
| 1654 |
return !g_main_is_running (main_loops->data); |
| 1655 |
else |
| 1656 |
return TRUE; |
| 1657 |
|
| 1658 |
and note that main_loops is set only by gtk_main. |
| 1659 |
So a program that doesn't call gtk_main at all has |
| 1660 |
main_loops = nil always. |
| 1661 |
|
| 1662 |
This means that I should trace the Terminated value myself, |
| 1663 |
just like I do for X11 or WinAPI CastleWindow implementations. |
| 1664 |
And the result of gtk_main_iteration_do call should be ignored. *) |
| 1665 |
|
| 1666 |
if Terminated then Exit(false); |
| 1667 |
|
| 1668 |
{ Only allow yourself a ProcessUpdates call when no events are pending. |
| 1669 |
This is done regardless of WaitForMessage mechanism. |
| 1670 |
|
| 1671 |
This follows castlewindow_winsystem.inc approach, and works very good |
| 1672 |
to prevent doing Update / Render when we're clogged with events |
| 1673 |
(typically happens when walking with mouse look, then we're clogged |
| 1674 |
with mouse move events). } |
| 1675 |
if gtk_events_pending() = 0 then |
| 1676 |
begin |
| 1677 |
ProcessUpdates; |
| 1678 |
if Terminated then Exit(false); |
| 1679 |
end; |
| 1680 |
|
| 1681 |
{ Once we had below condition "and (not Terminated)", |
| 1682 |
but it's pointless, we know Terminated is false now. } |
| 1683 |
WaitForMessage := WaitForMessage and Application.AllowSuspendForInput; |
| 1684 |
if WaitForMessage then |
| 1685 |
MarkSleeping; |
| 1686 |
|
| 1687 |
{ we could probably not call gtk_main_iteration_do when WasAnyMessage = false, |
| 1688 |
and simplify this code a little. But let's keep it this way, to be similar |
| 1689 |
to castlewindow_winsystem.inc, that should do the same approach. } |
| 1690 |
WasAnyMessage := (gtk_events_pending() <> 0) or WaitForMessage; |
| 1691 |
|
| 1692 |
gtk_main_iteration_do(WaitForMessage); |
| 1693 |
Result := not Terminated; |
| 1694 |
|
| 1695 |
if (not WasAnyMessage) and |
| 1696 |
(not Terminated) and |
| 1697 |
(not WaitForMessage) and |
| 1698 |
WaitToLimitFPS then |
| 1699 |
DoLimitFPS; |
| 1700 |
end; |
| 1701 |
|
| 1702 |
procedure TCastleApplication.BackendTerminate; |
| 1703 |
begin |
| 1704 |
{ TODO: This implementation (doing gtk_main_quit) |
| 1705 |
is now likely unnecessary. |
| 1706 |
|
| 1707 |
We call gtk_main_quit only to exit from gtk_main --- but we do not use |
| 1708 |
gtk_main anymore to implement our loop. |
| 1709 |
We implement loop simply by calling ProcessMessage. } |
| 1710 |
|
| 1711 |
if gtk_main_level() > 0 then |
| 1712 |
gtk_main_quit(); |
| 1713 |
end; |
| 1714 |
|
| 1715 |
procedure TCastleApplication.CreateBackend; |
| 1716 |
begin |
| 1717 |
inherited Initialize; // set Terminated := false |
| 1718 |
|
| 1719 |
RegisterClipboard(TGTKClipboard.Create); |
| 1720 |
end; |
| 1721 |
|
| 1722 |
procedure TCastleApplication.DestroyBackend; |
| 1723 |
var |
| 1724 |
C: TMouseCursor; |
| 1725 |
begin |
| 1726 |
{ unref cursors allocated in Cursors array } |
| 1727 |
for C := Low(C) to High(C) do |
| 1728 |
if Cursors[C] <> nil then |
| 1729 |
begin |
| 1730 |
gdk_cursor_unref(Cursors[C]); |
| 1731 |
Cursors[C] := nil; |
| 1732 |
end; |
| 1733 |
end; |
| 1734 |
|
| 1735 |
{ When CASTLE_WINDOW_USE_XF86VMODE, then castlewindow_xf86vmode.inc |
| 1736 |
will provide implementation for ScreenWidth and ScreenHeight. } |
| 1737 |
{$ifndef CASTLE_WINDOW_USE_XF86VMODE} |
| 1738 |
function TCastleApplication.ScreenWidth: integer; |
| 1739 |
var |
| 1740 |
Monitor: GInt; |
| 1741 |
Rect: TGdkRectangle; |
| 1742 |
begin |
| 1743 |
InitializeXDisplay; |
| 1744 |
//Result := gdk_screen_get_width(XScreenGdk); |
| 1745 |
// Below is better for dual monitors. |
| 1746 |
// TODO: add to docs that Left/Top are relative to global coords, not this monitor |
| 1747 |
// TODO: this is actually still bad, different window may be on different monitors with different resolutions |
| 1748 |
Monitor := gdk_screen_get_primary_monitor(XScreenGdk); |
| 1749 |
gdk_screen_get_monitor_geometry(XScreenGdk, Monitor, @Rect); |
| 1750 |
Result := Rect.Width; |
| 1751 |
end; |
| 1752 |
|
| 1753 |
function TCastleApplication.ScreenHeight: integer; |
| 1754 |
var |
| 1755 |
Monitor: GInt; |
| 1756 |
Rect: TGdkRectangle; |
| 1757 |
begin |
| 1758 |
InitializeXDisplay; |
| 1759 |
//Result := gdk_screen_get_height(XScreenGdk); |
| 1760 |
// Below is better for dual monitors. |
| 1761 |
// TODO: add to docs that Left/Top are relative to global coords, not this monitor |
| 1762 |
// TODO: this is actually still bad, different window may be on different monitors with different resolutions |
| 1763 |
Monitor := gdk_screen_get_primary_monitor(XScreenGdk); |
| 1764 |
gdk_screen_get_monitor_geometry(XScreenGdk, Monitor, @Rect); |
| 1765 |
Result := Rect.Height; |
| 1766 |
end; |
| 1767 |
{$endif CASTLE_WINDOW_USE_XF86VMODE} |
| 1768 |
|
| 1769 |
function TCastleApplication.BackendName: String; |
| 1770 |
begin |
| 1771 |
Result := 'GTK+'; |
| 1772 |
end; |
| 1773 |
|
| 1774 |
{$ifdef CASTLE_WINDOW_GTK_WITH_XLIB} |
| 1775 |
function TCastleApplication.XDisplay: Xlib.PDisplay; |
| 1776 |
begin |
| 1777 |
InitializeXDisplay; |
| 1778 |
Result := gdk_x11_display_get_xdisplay(XDisplayGdk); |
| 1779 |
end; |
| 1780 |
|
| 1781 |
function TCastleApplication.XScreen: Integer; |
| 1782 |
begin |
| 1783 |
InitializeXDisplay; |
| 1784 |
Result := gdk_x11_screen_get_screen_number(XScreenGdk); |
| 1785 |
end; |
| 1786 |
{$endif} |
| 1787 |
|
| 1788 |
procedure TCastleApplication.InitializeXDisplay; |
| 1789 |
|
| 1790 |
{$ifdef DARWIN} |
| 1791 |
{ Replacement for gtk_init on Darwin, to try harder to attach to a running |
| 1792 |
X server on macOS. } |
| 1793 |
procedure DarwinGtkInit; |
| 1794 |
const |
| 1795 |
AltXDisplayName = ':0'; |
| 1796 |
var |
| 1797 |
AltDisplay: PGdkDisplay; |
| 1798 |
begin |
| 1799 |
if not gtk_init_check(@argc, @argv) then |
| 1800 |
begin |
| 1801 |
AltDisplay := gdk_display_open(PChar(AltXDisplayName)); |
| 1802 |
|
| 1803 |
if AltDisplay <> nil then |
| 1804 |
begin |
| 1805 |
Writeln(ErrOutput, 'Opening the default X display failed, retrying with "', |
| 1806 |
AltXDisplayName, '" to try to attach to running X server on macOS.'); |
| 1807 |
gdk_display_manager_set_default_display(gdk_display_manager_get(), AltDisplay); |
| 1808 |
gtk_init(@argc, @argv); |
| 1809 |
|
| 1810 |
{ if success, update XDisplayName to the one actually used } |
| 1811 |
XDisplayName := AltXDisplayName; |
| 1812 |
end else |
| 1813 |
begin |
| 1814 |
Writeln(ErrOutput, 'Opening X display failed (tried both the default, and "', AltXDisplayName, '"). Start X server first.'); |
| 1815 |
Halt(1); |
| 1816 |
end; |
| 1817 |
end; |
| 1818 |
end; |
| 1819 |
{$endif} |
| 1820 |
|
| 1821 |
begin |
| 1822 |
if not InitializeXDisplayDone then |
| 1823 |
begin |
| 1824 |
{ TODO: Old comment suggested that GTK initializes X display at gtk_init, |
| 1825 |
parsing --display on it's own. Confirm? |
| 1826 |
It should work anyway. } |
| 1827 |
|
| 1828 |
{$ifdef DARWIN} |
| 1829 |
DarwinGtkInit; |
| 1830 |
{$else} |
| 1831 |
gtk_init(@argc, @argv); |
| 1832 |
{$endif} |
| 1833 |
|
| 1834 |
if XDisplayName <> '' then |
| 1835 |
XDisplayGdk := gdk_display_open(PChar(XDisplayName)) |
| 1836 |
else |
| 1837 |
XDisplayGdk := gdk_display_get_default(); |
| 1838 |
|
| 1839 |
XScreenGdk := gdk_display_get_default_screen(XDisplayGdk); |
| 1840 |
|
| 1841 |
InitializeXDisplayDone := true; |
| 1842 |
end; |
| 1843 |
end; |
| 1844 |
|
| 1845 |
{ TWindowContainer ----------------------------------------------------------- } |
| 1846 |
|
| 1847 |
function TWindowContainer.SettingMousePositionCausesMotion: Boolean; |
| 1848 |
begin |
| 1849 |
{ Confirmed using window_events example that it is "true" for GTK |
| 1850 |
(press "5", OnMotion is generated). } |
| 1851 |
Result := true; |
| 1852 |
end; |
| 1853 |
|
| 1854 |
{$endif read_implementation} |