forked from fantaisie-software/purebasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMacExtensions.pb
355 lines (281 loc) · 11.5 KB
/
MacExtensions.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
; --------------------------------------------------------------------------------------------
; 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.
; --------------------------------------------------------------------------------------------
; this file should contain all OS X specific extension
; functions that are "self contained" ie, do not need anything of the
; IDE, so they can be reused by the debugger and such
;
CompilerIf #CompileMacCocoa
ImportC ""
PB_Gadget_CenterEditorGadget(GadgetID.i)
PB_Gadget_ShowToolBar(ToolBarID.i, State.l)
PB_Gadget_SelectAllComboBoxText(*ComboBox)
PB_Gadget_SetOpenFinderFiles(*Callback)
PB_Gadget_AddFullScreenButton(*WindowID)
PB_Gadget_AbsoluteGadgetY(*Gadget)
; PB_Gadget_GetProperty(Gadget, Property)
; PB_SelectElement(*List, Index) ; we need to use this on the ListIcon gadgets internal linkedlist, so we can't use the regular ListIndex()
EndImport
; For the tab panel
;
#kThemeBrushAlertBackgroundActive = 3
#noErr = 0
; the HIThemeBrushCreateCGColor() is 10.4+ only, but the x86 version is at least 10.4 anyway
ImportC ""
HIThemeBrushCreateCGColor(Brush, *Color)
CGColorGetComponents(Color)
CGColorGetNumberOfComponents(Color)
CGColorRelease(Color)
EndImport
CompilerIf Defined(PB_Gadget, #PB_Structure) = 0
Structure PB_Gadget
*Gadget
*Container
; Definition is incomplete, but more is not needed
EndStructure
CompilerEndIf
Procedure ShowWindowMaximized(Window)
SetWindowState(Window, #PB_Window_Maximize)
HideWindow(Window, 0)
EndProcedure
Procedure IsWindowMaximized(Window)
If GetWindowState(Window) & #PB_Window_Maximize
ProcedureReturn 1
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure IsWindowMinimized(Window)
If GetWindowState(Window) & #PB_Window_Minimize
ProcedureReturn 1
Else
ProcedureReturn 0
EndIf
EndProcedure
Procedure SetWindowForeground(Window)
SetActiveWindow(Window) ; This is correct as SetActiveWindow() call SelectWindow_()
EndProcedure
; set window to the foreground without giving it the focus (and without a focus event!)
;
Procedure SetWindowForeground_NoActivate(Window)
Debug "TODO SetWindowForeground_NoActivate()"
;BringToFront_(WindowID(Window))
EndProcedure
Procedure SetWindowStayOnTop(Window, StayOnTop)
StickyWindow(Window, StayOnTop)
EndProcedure
Procedure GetPanelWidth(Gadget)
ProcedureReturn GetGadgetAttribute(Gadget, #PB_Panel_ItemWidth)
EndProcedure
Procedure GetPanelHeight(Gadget)
ProcedureReturn GetGadgetAttribute(Gadget, #PB_Panel_ItemHeight)
EndProcedure
Procedure SelectComboBoxText(Gadget)
PB_Gadget_SelectAllComboBoxText(GadgetID(Gadget))
EndProcedure
Procedure RedrawGadget(Gadget)
; Not needed on Cocoa
EndProcedure
Procedure GetButtonBackgroundColor()
ProcedureReturn $FFFFFF ; TODO
EndProcedure
Procedure StartFlickerFix(Window)
EndProcedure
Procedure StopFlickerFix(Window, DoRedraw)
EndProcedure
Procedure StartGadgetFlickerFix(Gadget)
EndProcedure
Procedure StopGadgetFlickerFix(Gadget)
EndProcedure
Procedure ZeroMemory(*Buffer, Size)
memset_(*Buffer, 0, Size)
EndProcedure
Structure PB_ListIconElement
*Text
Image.i
State.l
UserData.i
EndStructure
Structure PB_TreeElement
*Text
Level.l
Image.i
Expanded.l
Checked.l
UserData.i
EndStructure
#ListProperty = AsciiConst('L', 'I', 'S', 'T')
#PureImageColumnID = AsciiConst('P', 'U', 'R', 'I')
#kDataBrowserNoItem = 0
#kDataBrowserItemNoProperty = 0
Procedure.s GetExplorerName()
ProcedureReturn "Finder"
EndProcedure
Procedure ShowExplorerDirectory(Directory$)
RunProgram("open", Chr(34)+Directory$+Chr(34), "")
EndProcedure
Procedure ShowExplorerFile(File$)
RunProgram("open", "-R " + #DQUOTE$ + File$ + #DQUOTE$, "")
EndProcedure
; Carbon event modifiers
;
#activeFlagBit = 0
#btnStateBit = 7
#cmdKeyBit = 8
#shiftKeyBit = 9
#alphaLockBit = 10
#optionKeyBit = 11
#controlKeyBit = 12
#rightShiftKeyBit = 13
#rightOptionKeyBit = 14
#rightControlKeyBit = 15
#activeFlag = 1 << #activeFlagBit
#btnState = 1 << #btnStateBit
#cmdKey = 1 << #cmdKeyBit
#shiftKey = 1 << #shiftKeyBit
#alphaLock = 1 << #alphaLockBit
#optionKey = 1 << #optionKeyBit
#controlKey = 1 << #controlKeyBit
#rightShiftKey = 1 << #rightShiftKeyBit
#rightOptionKey = 1 << #rightOptionKeyBit
#rightControlKey = 1 << #rightControlKeyBit
Procedure ModifierKeyPressed(Key)
Select Key
Case #PB_Shortcut_Command: mod = #cmdKey
Case #PB_Shortcut_Control: mod = #controlKey
Case #PB_Shortcut_Shift: mod = #shiftKey
Case #PB_Shortcut_Alt: mod = #optionKey
EndSelect
If GetCurrentEventKeyModifiers_() & mod
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure OpenWebBrowser(Url$)
RunProgram("open", Url$, "")
EndProcedure
Procedure GetCocoaColor(NSColorName.s)
Protected.CGFloat r, g, b, a
Protected NSColor, NSColorSpace
; There is no controlAccentColor on macOS < 10.14
If NSColorName = "controlAccentColor" And OSVersion() < #PB_OS_MacOSX_10_14
ProcedureReturn $D5ABAD
EndIf
; There are no system colors on macOS < 10.10
If Left(NSColorName, 6) = "system" And OSVersion() < #PB_OS_MacOSX_10_10
NSColorName = LCase(Mid(NSColorName, 7, 1)) + Mid(NSColorName, 8)
EndIf
NSColorSpace = CocoaMessage(0, 0, "NSColorSpace deviceRGBColorSpace")
NSColor = CocoaMessage(0, CocoaMessage(0, 0, "NSColor " + NSColorName), "colorUsingColorSpace:", NSColorSpace)
If NSColor
CocoaMessage(@r, NSColor, "redComponent")
CocoaMessage(@g, NSColor, "greenComponent")
CocoaMessage(@b, NSColor, "blueComponent")
CocoaMessage(@a, NSColor, "alphaComponent")
ProcedureReturn RGBA(r * 255.0, g * 255.0, b * 255.0, a * 255.0)
EndIf
EndProcedure
; Ignore the apparence support if we are compiling the standalone debugger
CompilerIf Defined(PUREBASIC_IDE, #PB_Constant)
; Appearance support
Procedure UpdateAppearance()
Protected NSApp, NSAppearance, NSAppearanceAqua, NSEffectiveAppearance, NSAppearanceSave
Protected Update, OldFaceColor, Item, ItemCount
NSApp = CocoaMessage(0, 0, "NSApplication sharedApplication")
NSAppearance = CocoaMessage(0, NSApp, "appearance")
If Not DisplayDarkMode And NSAppearance = 0
NSAppearanceAqua = CocoaMessage(0, 0, "NSAppearance appearanceNamed:$", @"NSAppearanceNameAqua")
CocoaMessage(0, NSApp, "setAppearance:", NSAppearanceAqua)
Update = #True
ElseIf DisplayDarkMode And NSAppearance <> 0
NSAppearanceAqua = #nil
CocoaMessage(0, NSApp, "setAppearance:", #nil)
Update = #True
EndIf
If Update
With TabBarGadgetInclude
OldFaceColor = \FaceColor & $FFFFFF
; Save current appearance
NSAppearanceSave = CocoaMessage(0, 0, "NSAppearance currentAppearance")
NSEffectiveAppearance = CocoaMessage(0, NSApp, "effectiveAppearance")
CocoaMessage(0, 0, "NSAppearance setCurrentAppearance:", NSEffectiveAppearance)
\BorderColor = GetCocoaColor("systemGrayColor")
\TabBarColor = GetCocoaColor("windowBackgroundColor")
\TextColor = GetCocoaColor("windowFrameTextColor")
\FaceColor = GetCocoaColor("windowBackgroundColor")
; Restore current appearance
CocoaMessage(0, 0, "NSAppearance setCurrentAppearance:", NSAppearanceSave)
; Update tabbar item with default colors
ItemCount = CountTabBarGadgetItems(#GADGET_FilesPanel) - 1
For Item = 0 To ItemCount
If GetTabBarGadgetItemColor(#GADGET_FilesPanel, Item, #PB_Gadget_BackColor) = OldFaceColor
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Item, #PB_Gadget_FrontColor, \TextColor)
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Item, #PB_Gadget_BackColor, \FaceColor)
EndIf
Next
EndWith
EndIf
EndProcedure
EnumerationBinary
#NSKeyValueObservingOptionNew
#NSKeyValueObservingOptionOld
EndEnumeration
ProcedureC KVO_Appearance(obj, sel, keyPath, object, change, context)
Select PeekS(CocoaMessage(0, keyPath, "UTF8String"), -1, #PB_UTF8)
Case "effectiveAppearance"
If DisplayDarkMode
With TabBarGadgetInclude
OldFaceColor = \FaceColor & $FFFFFF
; Save current appearance
NSAppearanceSave = CocoaMessage(0, 0, "NSAppearance currentAppearance")
NSEffectiveAppearance = CocoaMessage(0, NSApp, "effectiveAppearance")
CocoaMessage(0, 0, "NSAppearance setCurrentAppearance:", NSEffectiveAppearance)
\BorderColor = GetCocoaColor("systemGrayColor")
\TabBarColor = GetCocoaColor("windowBackgroundColor")
\TextColor = GetCocoaColor("windowFrameTextColor")
\FaceColor = GetCocoaColor("windowBackgroundColor")
; Restore current appearance
CocoaMessage(0, 0, "NSAppearance setCurrentAppearance:", NSAppearanceSave)
; Update tabbar item with default colors
ItemCount = CountTabBarGadgetItems(#GADGET_FilesPanel) - 1
For Item = 0 To ItemCount
If GetTabBarGadgetItemColor(#GADGET_FilesPanel, Item, #PB_Gadget_BackColor) = OldFaceColor
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Item, #PB_Gadget_FrontColor, \TextColor)
SetTabBarGadgetItemColor(#GADGET_FilesPanel, Item, #PB_Gadget_BackColor, \FaceColor)
EndIf
Next
EndWith
EndIf
EndSelect
EndProcedure
Procedure InitAppearanceObserver()
Protected NSApp, KVO_Class, KVO_Object
; Create Key-Value Observer class (PB_KVO_Appearance)
KVO_Class = objc_allocateClassPair_(objc_getClass_("NSObject"), "PB_KVO_Appearance", 0)
class_addMethod_(KVO_Class, sel_registerName_("observeValueForKeyPath:ofObject:change:context:"), @KVO_Appearance(), "v@:@@@^v")
objc_registerClassPair_(KVO_Class)
; Create PB_KVO class instance (KVO)
KVO_Object = CocoaMessage(0, 0, "PB_KVO_Appearance new")
; Add observer
NSApp = CocoaMessage(0, 0, "NSApplication sharedApplication")
CocoaMessage(0, NSApp, "addObserver:", KVO_Object, "forKeyPath:$", @"effectiveAppearance", "options:", #NSKeyValueObservingOptionNew, "context:", #nil)
EndProcedure
CompilerEndIf
Procedure GetListViewScroll(Gadget)
Protected *GadgetStruct.PB_Gadget, *ClipView, Bounds.CGRect
*GadgetStruct = IsGadget(Gadget)
*ClipView = CocoaMessage(0, *GadgetStruct\Container, "contentView")
CocoaMessage(@Bounds, *ClipView, "bounds")
ProcedureReturn Bounds\origin\y
EndProcedure
Procedure SetListViewScroll(Gadget, Position)
Protected *GadgetStruct.PB_Gadget, *DocumentView, TargetPoint.CGPoint
TargetPoint\y = Position
*GadgetStruct = IsGadget(Gadget)
*DocumentView = CocoaMessage(0, *GadgetStruct\Container, "documentView")
CocoaMessage(0, *DocumentView, "scrollPoint:@", @TargetPoint)
EndProcedure
CompilerEndIf