forked from gphilippot/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MacMisc.pb
306 lines (237 loc) · 10.3 KB
/
MacMisc.pb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
;--------------------------------------------------------------------------------------------
; Copyright (c) Fantaisie Software. All rights reserved.
; Dual licensed under the GPL and Fantaisie Software licenses.
; See LICENSE and LICENSE-FANTAISIE in the project root for license information.
;--------------------------------------------------------------------------------------------
; OS X specific file:
CompilerIf #CompileMacCocoa
; Global AlreadyRunning
; handler for the "open document" event
;
; Problem: We must install this handler inside the OSStartupCode() to catch the event
; when double-clicking on a file caused the IDE to load. (if we set the handler later, a WaitWindowEvent call will eat the event)
; But we cannot actually load the file before the IDE is ready. So this variable checks this (set in PureBasic.pb)
;
Global ReadyForDocumentEvent = 0
ProcedureC OpenDocument(*FilesUTF8)
; Files are separated by a tab
;
Files$ = PeekS(*FilesUTF8, -1, #PB_UTF8)
Repeat
File$ = StringField(Files$, k+1, #TAB$) ; index is 1 based
If File$ <> ""
If FileSize(File$) >= 0 ; no directories here...
Debug "passed file: "+File$
; Use the queue, as loading a document from this callback crash the IDE (probably because we do GUI operations in startup callback
; It will be loaded in the main loop
LastElement(OpenFilesCommandLine())
AddElement(OpenFilesCommandLine()) ; will cause this file to be loaded when the IDE is ready for it
OpenFilesCommandLine() = File$
EndIf
k+1
EndIf
Until File$ = ""
EndProcedure
Procedure OSStartupCode()
; Set the default Path Values
;
Home$ = GetEnvironmentVariable("HOME")
If Right(Home$, 1) <> #Separator
Home$ + #Separator
EndIf
If PureBasicPath$ = "" ; Only change if not set by commandline
CompilerIf #SpiderBasic
PureBasicPath$ = GetEnvironmentVariable("SPIDERBASIC_HOME")
CompilerElse
PureBasicPath$ = GetEnvironmentVariable("PUREBASIC_HOME")
CompilerEndIf
CompilerIf Defined(FredLocalCompile, #PB_Constant) ; Fred config
CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
CompilerIf #SpiderBasic
PureBasicPath$ = "C:\PureBasic\Svn\"+#SVNVersion+"\Build\SpiderBasic_x64\"
CompilerElse
PureBasicPath$ = "C:\PureBasic\Svn\"+#SVNVersion+"\Build\PureBasic_x64\"
CompilerEndIf
CompilerElse
CompilerIf #SpiderBasic
PureBasicPath$ = "/Users/fred/svn/"+#SVNVersion+"/Build/SpiderBasic"
CompilerElse
PureBasicPath$ = "C:\PureBasic\Svn\"+#SVNVersion+"\Build\PureBasic_x86\"
CompilerEndIf
CompilerEndIf
CompilerEndIf
If PureBasicPath$ <> ""
If Right(PureBasicPath$, 1) <> #Separator
PureBasicPath$ + #Separator
EndIf
Else
; If the PUREBASIC_HOME isn't set, then it's probably launched from the Finder so gets the bundle path
;
Bundle = CFBundleGetMainBundle_()
If Bundle
BundleURL = CFBundleCopyBundleURL_(Bundle)
If BundleURL
*Buffer = AllocateMemory(256)
CFURLGetFileSystemRepresentation_(BundleURL, #True, *Buffer, 255) ; it returns a byte buffer (UTF-8)
BundlePath$ = PeekS(*Buffer, -1, #PB_UTF8) ; It returns something like: /tmp/PureBasic.app, so gets the path only
FreeMemory(*Buffer)
CompilerIf #SpiderBasic
; If SpiderBasic home is bundled inside the .app, uses this path.
; Note: it needs to be in the resources directory, or the app signing will fail
;
If FileSize(BundlePath$ + "/Contents/Resources/compilers") = -2
PureBasicPath$ = BundlePath$ + "/Contents/Resources/"
Else
PureBasicPath$ = GetPathPart(BundlePath$) ; It returns something like: /tmp/PureBasic.app, so gets the path only
EndIf
CompilerElse
; If PureBasic home is bundled inside the .app, uses this path.
; Note: it needs to be in the resources directory, or the app signing will fail
;
If FileSize(BundlePath$ + "/Contents/Resources/compilers") = -2
PureBasicPath$ = BundlePath$ + "/Contents/Resources/"
Else
PureBasicPath$ = GetPathPart(BundlePath$) ; It returns something like: /tmp/PureBasic.app, so gets the path only
EndIf
CompilerEndIf
EndIf
EndIf
EndIf
EndIf
TempPath$ = "/tmp/"
If PreferencesFile$ = "" ; Only change if not set by commandline
PreferencesFile$ = PureBasicConfigPath() + #PreferenceFileName$
EndIf
If AddToolsFile$ = "" ; Only change if not set by commandline
AddToolsFile$ = PureBasicConfigPath() + "tools.prefs"
EndIf
If TemplatesFile$ = "" ; Only change if not set by commandline
TemplatesFile$ = PureBasicConfigPath() + "templates.prefs"
EndIf
If HistoryDatabaseFile$ = "" ; Only change if not set by commandline
HistoryDatabaseFile$ = PureBasicConfigPath() + "history.db"
EndIf
If SourcePathSet = 0 ; Only change if not set by commandline
; It's important to point on the examples when the path is not set as when your run PureBasic for the first
; time, you want to open examples to test them. This value can be changed in the prefs and won't be
; used if another pref is found
;
SourcePath$ = PureBasicPath$ + "Examples/"
EndIf
CompilerIf #SpiderBasic
RunOnceFile$ = TempPath$ + ".sbrunonce.txt"
CompilerElse
RunOnceFile$ = TempPath$ + ".pbrunonce.txt"
CompilerEndIf
;Debug "PureBasicPath: "+PureBasicPath$
;Debug "Preferences: "+ PreferencesFile$
;Debug "Tools settings: "+AddToolsFile$
; #GDK_SB_H_DOUBLE_ARROW = 108
; #GDK_SB_V_DOUBLE_ARROW = 116
; SplitterCursor = gdk_cursor_new_(#GDK_SB_H_DOUBLE_ARROW)
; SplitterCursor2 = gdk_cursor_new_(#GDK_SB_V_DOUBLE_ARROW)
ButtonBackgroundColor = GetButtonBackgroundColor()
; Install the "Open Document" event handler
; Do not do this in OSStartupCode so we only receive this event when we wre ready
; to actually load a file...
;
PB_Gadget_SetOpenFinderFiles(@OpenDocument())
ProcedureReturn 1
EndProcedure
Procedure OSEndCode()
;If AlreadyRunning = 0
; DeleteFile(TempPath$ + ".purebasic.running")
;EndIf
EndProcedure
; determine things like StatusBarHeight once after the GUI is created.
Procedure GetWindowMetrics()
StatusbarHeight = StatusBarHeight(#STATUSBAR)
ToolbarHeight = 0 ; Toolbar is not calculated in client area on OS X
ToolbarTopOffset = 2
MenuHeight = MenuHeight()
EndProcedure
Procedure LoadEditorFonts()
; The font still needs to be loaded for the Preferences display
If LoadFont(#FONT_Editor, EditorFontName$, EditorFontSize)
EditorFontID = FontID(#FONT_Editor)
EndIf
EditorBoldFontName$ = EditorFontName$ ; for scintilla
EndProcedure
Procedure UpdateToolbarView()
PB_Gadget_ShowToolBar(ToolBarID(#TOOLBAR), ShowMainToolbar)
EndProcedure
Procedure RunOnce_Startup(InitialSourceLine)
ProcedureReturn #False ; do not quit the IDE, as RunOnce is not implemented
EndProcedure
Procedure RunOnce_UpdateSetting()
; If Editor_RunOnce
; If CreateFile(#FILE_RunOnce, TempPath$ + ".purebasic.running")
; WriteString(#FILE_RunOnce, Str(getpid_()))
; CloseFile(#FILE_RunOnce)
; EndIf
; Else
; DeleteFile(TempPath$ + ".purebasic.running")
; EndIf
EndProcedure
; Dead session detection
Procedure Session_IsRunning(OSSessionID$)
; Only one instance can run on OSX, so any instance that is not the current one must be dead
ProcedureReturn #False
EndProcedure
Procedure.s Session_Start()
; no need to track any information
ProcedureReturn ""
EndProcedure
Procedure Session_End(OSSessionID$)
EndProcedure
; Handler to redirect all key input back to Scintilla
; Just forwards all messages
;
ProcedureC RawKeyEventHandler(*NextHandler, *event, *handlerdata)
Debug "."
; Call down the handler chain to see if this key will be handled (shortcut etc)
; If not, dispatch this event to the ScintillaGadget. Works quite well
;
If CallNextEventHandler_(*NextHandler, *event) <> #noErr
SendEventToEventTarget_(*event, GetControlEventTarget_(GadgetID(*ActiveSource\EditorGadget)))
EndIf
ProcedureReturn #noErr
EndProcedure
Procedure AutoComplete_SetupRawKeyHandler()
; We use the other way around on OS X Cocoa (accelerators on the main window)
EndProcedure
Procedure AutoComplete_SetFocusCallback()
; nothing to do here
EndProcedure
Procedure AutoComplete_AdjustWindowSize(MaxWidth, MaxHeight)
; TODO: works without this as well, but would be nice to have
EndProcedure
Procedure ToolsPanel_ApplyColors(Gadget)
If Gadget = propgrid
grid_SetGadgetColor(Gadget, #Grid_Color_Background, ToolsPanelBackColor)
grid_SetDefaultStyle(Gadget,P_FontGrid, #P_FontGridSize, ToolsPanelFrontColor)
Else
If ToolsPanelFontID <> #PB_Default
SetGadgetFont(Gadget, ToolsPanelFontID)
EndIf
If ToolsPanelUseColors
SetGadgetColor(Gadget, #PB_Gadget_FrontColor, ToolsPanelFrontColor)
SetGadgetColor(Gadget, #PB_Gadget_BackColor, ToolsPanelBackColor)
;SetGadgetColor(Gadget, #PB_Gadget_LineColor, ToolsPanelFrontColor) ; just set linecolor too. will be ignored if unsupported
EndIf
EndIf
EndProcedure
; Procedure CPUMonitor_Init()
;
; IsCPUMonitorInitialized = 0
;
; EndProcedure
;
; ; This procedure must do the following:
; ; - update the #GADGET_CPU_List values for each process
; ; - update the CPUUsage value in the RunningDebuggers() list (for all processes)
; ; - fill the pointers to the general values (all % CPU usage)
; ;
; Procedure GetCPUInfo(*Free.LONG, *Used.LONG)
; EndProcedure
CompilerEndIf