Debian Codegraph @

src/base/castleutils_read_write.inc

raw
1 {%MainUnit castleutils.pas}
2 {
3 Copyright 2002-2021 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 {$ifdef read_interface}
18
19 { Write using a dialog box or console.
20
21 If we are a Windows GUI program (not IsConsole) then we use
22 native Windows dialog boxes.
23 Otherwise (a console is available, which is always true on non-Windows)
24 we output the message using simple Writeln (to standard output for
25 InfoWrite, or ErrOutput for ErrorWrite and WarningWrite).
26
27 @groupBegin }
28 procedure ErrorWrite(const s: string); overload;
29 procedure WarningWrite(const s: string); overload;
30 procedure InfoWrite(const s: string); overload;
31
32 procedure ErrorWrite(const s: string; const args: array of const); overload;
33 procedure WarningWrite(const s: string; const args: array of const); overload;
34 procedure InfoWrite(const s: string; const args: array of const); overload;
35 { @groupEnd }
36
37 { Output messages, using console or dialog box.
38
39 If we're not on Windows or IsConsole, then we simply output Messages
40 using Writeln.
41
42 If we're on Windows and not IsConsole, then every Messages is displayed
43 in a separate dialog box. Dialog box uses our InfoBox routine,
44 with Messages[I] being message content and title being
45 @code(Format(TitleFormat, [I + 1, Messages.Count])).
46
47 This is good for outputting a lot of information.
48
49 @deprecated Deprecated. This just looks ugly in GUI version.
50 It's better to present long information using only a console (just use Writeln),
51 or only a full-featured GUI (like Lazarus LCL or our CastleUIControls). }
52 procedure InfoWriteParts(const TitleFormat: string; const Messages: array of string); deprecated;
53
54 {$endif read_interface}
55
56 {$ifdef read_implementation}
57
58 { Notes for Windows: even when IsConsole = false, we can have
59 GetStdHandle(STD_ERROR_HANDLE) and/or GetStdHandle(STD_OUTPUT_HANDLE)
60 available. User can always run GUI program and redirect our
61 stdin/out/err and then GUI app will have some of stdin, stdout, stderr
62 available. In other words, GUI app *may* have stdin/out/err,
63 but it doesn't have to. Depends on how the user run the app.
64 But XxxWrite functions will always for GUI app (that have
65 IsConsole = false) use XxxBox functions, never Writeln,
66 even when stdin/out/err will be available.
67 This ensures that program always behaves in the same way.
68 }
69
70 {$ifdef MSWINDOWS}
71 { Simple standard Windows message boxes.
72 If Parent is 0 (default), the box will be a modal box for the whole process
73 (all our windows), since we use MB_TASKMODAL.
74
75 Internal. Instead of these it's much better to use:
76 - TCastleWindow.MessageOK, TCastleWindow.MessageYesNo if you use
77 CastleWindow.
78 - or LCL dialog routines (like ShowMessage and everything in Dialogs unit)
79 if you use LCL.
80
81 @groupBegin }
82 procedure WindowsErrorBox (const Text: String; const Caption: String = 'Error' ; const Parent: HWND = 0); forward;
83 procedure WindowsInfoBox (const Text: String; const Caption: String = 'Information'; const Parent: HWND = 0); forward;
84 procedure WindowsWarningBox(const Text: String; const Caption: String = 'Warning' ; const Parent: HWND = 0); forward;
85 { @groupEnd }
86
87 procedure WindowsErrorBox(const Text: String; const Caption: String; const Parent: HWND);
88 begin
89 MessageBox(Parent, PChar(Text), PChar(Caption), MB_OK or MB_ICONERROR or MB_TASKMODAL);
90 end;
91
92 procedure WindowsInfoBox(const Text: String; const Caption: String; const Parent: HWND);
93 begin
94 MessageBox(Parent, PChar(Text), PChar(Caption), MB_OK or MB_ICONINFORMATION or MB_TASKMODAL);
95 end;
96
97 procedure WindowsWarningBox(const Text: String; const Caption: String; const Parent: HWND);
98 begin
99 MessageBox(Parent, PChar(Text), PChar(Caption), MB_OK or MB_ICONWARNING or MB_TASKMODAL);
100 end;
101
102 {$endif}
103
104 procedure ErrorWrite(const s: string);
105 begin
106 {$ifdef MSWINDOWS} if not IsConsole then WindowsErrorBox(s) else {$endif}
107 Writeln(ErrOutput, s);
108 end;
109
110 procedure WarningWrite(const s: string);
111 begin
112 {$ifdef MSWINDOWS} if not IsConsole then WindowsWarningBox(s) else {$endif}
113 Writeln(ErrOutput, s);
114 end;
115
116 procedure InfoWrite(const s: string);
117 begin
118 {$ifdef MSWINDOWS} if not IsConsole then WindowsInfoBox(s) else {$endif}
119 Writeln(s);
120 end;
121
122 procedure ErrorWrite(const s: string; const args: array of const);
123 begin ErrorWrite(format(s, args)) end;
124 procedure WarningWrite(const s: string; const args: array of const);
125 begin WarningWrite(format(s, args)) end;
126 procedure InfoWrite(const s: string; const args: array of const);
127 begin InfoWrite(format(s, args)) end;
128
129 procedure InfoWriteParts(const TitleFormat: string; const Messages: array of string);
130 var i: Integer;
131 begin
132 {$ifdef MSWINDOWS}
133 if not IsConsole then
134 begin
135 for i := 0 to High(Messages) do
136 WindowsInfoBox(Messages[i], Format(TitleFormat, [i+1, High(Messages)+1]));
137 end else
138 {$endif}
139 begin
140 for i := 0 to High(Messages) do Writeln(Messages[i]);
141 end;
142 end;
143
144 {$endif read_implementation}