Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AtkCocoaHelper.cs « MonoDevelop.Components.AtkCocoaHelper « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a6a7b0038be7b059ecad74635d0fe2fe7d20842c (plain)
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
//
// AtkCocoaHelper.cs
//
// Author:
//       Iain Holmes <iain@xamarin.com>
//
// Copyright (c) 2016 Xamarin, Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

#if MAC
using AppKit;
using CoreGraphics;
using Foundation;
using ObjCRuntime;
#endif

namespace MonoDevelop.Components.AtkCocoaHelper
{
	public static class AtkCocoaExtensions
	{
		public static void SetCommonAttributes (this Atk.Object o, string name, string label, string help)
		{
			if (!string.IsNullOrEmpty (name)) {
				o.Name = name;
			}
			if (!string.IsNullOrEmpty (help)) {
				o.Description = help;
			}
			if (!string.IsNullOrEmpty (label)) {
				o.SetLabel (label);
			}
		}

		public static void SetCommonAttributes (this Xwt.Accessibility.Accessible o, string name, string label, string help)
		{
			if (!string.IsNullOrEmpty (name)) {
				o.Identifier = name;
			}

			if (!string.IsNullOrEmpty (label)) {
				o.Label = label;
			}

			if (!string.IsNullOrEmpty (help)) {
				o.Description = help;
			}
		}

		public static void SetCommonAccessibilityAttributes (this Xwt.Widget w, string name, Xwt.Widget label, string help)
		{
			w.Accessible.SetCommonAttributes (name, null, help);
			if (label != null) {
				// FIXME Add relationship to Xwt
			}
		}

		public static void SetCommonAccessibilityAttributes (this Xwt.Widget w, string name, string label, string help)
		{
			w.Accessible.SetCommonAttributes (name, label, help);
		}

		public static void SetCommonAccessibilityAttributes (this Gtk.Widget w, string name, string label, string help)
		{
			var accessible = w.Accessible;
			accessible.SetCommonAttributes (name, label, help);
		}

		public static void SetCommonAccessibilityAttributes (this Gtk.Widget w, string name, Gtk.Widget label, string help)
		{
			var accessible = w.Accessible;
			accessible.SetCommonAttributes (name, null, help);

			if (label != null) {
				w.SetAccessibilityLabelRelationship (label);
			}
		}

		public static void SetAccessibilityLabelRelationship (this Gtk.Widget w, Gtk.Widget label)
		{
			w.Accessible.SetTitleUIElement (label.Accessible);
			label.Accessible.SetTitleFor (w.Accessible);
		}
	}

	// AtkCocoaHelper wraps NSAccessibilityElement to set NSAccessibility properties that aren't supported by Atk
	public static class AtkCocoa
	{
		public enum Actions
		{
			AXCancel,
			AXConfirm,
			AXDecrement,
			AXDelete,
			AXIncrement,
			AXPick,
			AXPress,
			AXRaise,
			AXShowAlternateUI,
			AXShowDefaultUI,
			AXShowMenu
		};

		public enum Roles
		{
			AXButton,
			AXCell,
			AXColumn,
			AXGroup,
			AXGrowArea,
			AXImage,
			AXLink,
			AXList,
			AXMenuButton,
			AXPopUpButton,
			AXRadioButton,
			AXRow,
			AXRuler,
			AXSplitGroup,
			AXSplitter,
			AXStaticText,
			AXTabGroup,
			AXTextArea
		};

		public enum SubRoles
		{
			AXCloseButton,
		};

		public struct Range
		{
			public int Location { get; set; }
			public int Length { get; set; }
		}
	}

	public class ActionDelegate
	{
		HashSet<AtkCocoa.Actions> actions = new HashSet<AtkCocoa.Actions> ();

		Atk.Object owner;
		internal Atk.Object Owner {
			set {
				owner = value;

				if (owner.GetType () == typeof (Atk.NoOpObject)) {
					return;
				}

				HandleSignalAttachment (owner, (signal, handler) => signal.AddDelegate (handler));
			}
		}

		void HandleSignalAttachment (Atk.Object owner, Action<GLib.Signal, EventHandler<GLib.SignalArgs>> action)
		{
			var signal = GLib.Signal.Lookup (owner, "request-actions", typeof (GLib.SignalArgs));
			action (signal, new EventHandler<GLib.SignalArgs> (RequestActionsHandler));

			signal = GLib.Signal.Lookup (owner, "perform-cancel", typeof (GLib.SignalArgs));
			action (signal, new EventHandler<GLib.SignalArgs> (PerformCancelHandler));
			signal = GLib.Signal.Lookup (owner, "perform-confirm", typeof (GLib.SignalArgs));
			action (signal, new EventHandler<GLib.SignalArgs> (PerformConfirmHandler));
			signal = GLib.Signal.Lookup (owner, "perform-decrement", typeof (GLib.SignalArgs));
			action (signal, new EventHandler<GLib.SignalArgs> (PerformDecrementHandler));
			signal = GLib.Signal.Lookup (owner, "perform-delete", typeof (GLib.SignalArgs));
			action (signal, new EventHandler<GLib.SignalArgs> (PerformDeleteHandler));
			signal = GLib.Signal.Lookup (owner, "perform-increment", typeof (GLib.SignalArgs));
			action (signal, new EventHandler<GLib.SignalArgs> (PerformIncrementHandler));
			signal = GLib.Signal.Lookup (owner, "perform-pick", typeof (GLib.SignalArgs));
			action (signal, new EventHandler<GLib.SignalArgs> (PerformPickHandler));
			signal = GLib.Signal.Lookup (owner, "perform-press", typeof (GLib.SignalArgs));
			action (signal, new EventHandler<GLib.SignalArgs> (PerformPressHandler));
			signal = GLib.Signal.Lookup (owner, "perform-raise", typeof (GLib.SignalArgs));
			action (signal, new EventHandler<GLib.SignalArgs> (PerformRaiseHandler));
			signal = GLib.Signal.Lookup (owner, "perform-show-alternate-ui", typeof (GLib.SignalArgs));
			action (signal, new EventHandler<GLib.SignalArgs> (PerformShowAlternateUIHandler));
			signal = GLib.Signal.Lookup (owner, "perform-show-default-ui", typeof (GLib.SignalArgs));
			action (signal, new EventHandler<GLib.SignalArgs> (PerformShowDefaultUIHandler));
			signal = GLib.Signal.Lookup (owner, "perform-show-menu", typeof (GLib.SignalArgs));
			action (signal, new EventHandler<GLib.SignalArgs> (PerformShowMenuHandler));
		}

		public ActionDelegate (Gtk.Widget widget)
		{
			widget.Destroyed += WidgetDestroyed;
			Owner = widget.Accessible;
		}

		void WidgetDestroyed (object sender, EventArgs e)
		{
			FreeActions ();

			HandleSignalAttachment (owner, (signal, handler) => signal.RemoveDelegate (handler));
			owner = null;
		}

		// Because the allocated memory is passed to unmanaged code where it cannot be freed
		// we need to keep track of it until the object is finalized, or the actions need to be calculated again
		IntPtr allocatedActionPtr;
		IntPtr [] allocatedActionStrings;

		void FreeActions ()
		{
			if (allocatedActionStrings != null) {
				foreach (var ptr in allocatedActionStrings) {
					Marshal.FreeHGlobal (ptr);
				}
				allocatedActionStrings = null;
			}

			if (allocatedActionPtr != IntPtr.Zero) {
				Marshal.FreeHGlobal (allocatedActionPtr);
				allocatedActionPtr = IntPtr.Zero;
			}
		}

		void RegenerateActions ()
		{
			FreeActions ();

			// +1 so we can add a NULL to terminate the array
			int actionCount = actions.Count + 1;
			IntPtr intPtr = Marshal.AllocHGlobal (actionCount * Marshal.SizeOf<IntPtr> ());
			IntPtr [] actionsPtr = new IntPtr [actionCount];

			int i = 0;
			foreach (var action in actions) {
				actionsPtr [i] = Marshal.StringToHGlobalAnsi (action.ToString ());
				i++;
			}

			// Terminator
			actionsPtr [i] = IntPtr.Zero;

			Marshal.Copy (actionsPtr, 0, intPtr, actionCount);

			allocatedActionStrings = actionsPtr;
			allocatedActionPtr = intPtr;
		}

		void AddAction (AtkCocoa.Actions action)
		{
			if (owner.GetType () == typeof (Atk.NoOpObject)) {
				return;
			}

			actions.Add (action);
			RegenerateActions ();
		}

		void RemoveAction (AtkCocoa.Actions action)
		{
			if (owner.GetType () == typeof (Atk.NoOpObject)) {
				return;
			}

			actions.Remove (action);
			RegenerateActions ();
		}

		void RequestActionsHandler (object sender, GLib.SignalArgs args)
		{
			args.RetVal = allocatedActionPtr;
		}

		void PerformCancelHandler (object sender, GLib.SignalArgs args)
		{
			performCancel?.Invoke (this, args);
		}

		void PerformConfirmHandler (object sender, GLib.SignalArgs args)
		{
			performConfirm?.Invoke (this, args);
		}

		void PerformDecrementHandler (object sender, GLib.SignalArgs args)
		{
			performDecrement?.Invoke (this, args);
		}

		void PerformDeleteHandler (object sender, GLib.SignalArgs args)
		{
			performDelete?.Invoke (this, args);
		}

		void PerformIncrementHandler (object sender, GLib.SignalArgs args)
		{
			performIncrement?.Invoke (this, args);
		}

		void PerformPickHandler (object sender, GLib.SignalArgs args)
		{
			performPick?.Invoke (this, args);
		}

		void PerformPressHandler (object sender, GLib.SignalArgs args)
		{
			performPress?.Invoke (this, args);
		}

		void PerformRaiseHandler (object sender, GLib.SignalArgs args)
		{
			performRaise?.Invoke (this, args);
		}

		void PerformShowAlternateUIHandler (object sender, GLib.SignalArgs args)
		{
			performShowAlternateUI?.Invoke (this, args);
		}

		void PerformShowDefaultUIHandler (object sender, GLib.SignalArgs args)
		{
			performShowDefaultUI?.Invoke (this, args);
		}

		void PerformShowMenuHandler (object sender, GLib.SignalArgs args)
		{
			performShowMenu?.Invoke (this, args);
		}

		event EventHandler performCancel;
		public event EventHandler PerformCancel {
			add {
				if (performCancel == null) {
					AddAction (AtkCocoa.Actions.AXCancel);
				}
				performCancel += value;
			}
			remove {
				performCancel -= value;
				if (performCancel == null) {
					RemoveAction (AtkCocoa.Actions.AXCancel);
				}
			}
		}

		event EventHandler performConfirm;
		public event EventHandler PerformConfirm {
			add {
				if (performConfirm == null) {
					AddAction (AtkCocoa.Actions.AXConfirm);
				}
				performConfirm += value;
			}
			remove {
				performConfirm -= value;
				if (performConfirm == null) {
					RemoveAction (AtkCocoa.Actions.AXConfirm);
				}
			}
		}
		event EventHandler performDecrement;
		public event EventHandler PerformDecrement {
			add {
				if (performDecrement == null) {
					AddAction (AtkCocoa.Actions.AXDecrement);
				}
				performDecrement += value;
			}
			remove {
				performDecrement -= value;
				if (performDecrement == null) {
					RemoveAction (AtkCocoa.Actions.AXDecrement);
				}
			}
		}
		event EventHandler performDelete;
		public event EventHandler PerformDelete {
			add {
				if (performDelete == null) {
					AddAction (AtkCocoa.Actions.AXDelete);
				}
				performDelete += value;
			}
			remove {
				performDelete -= value;
				if (performDelete == null) {
					RemoveAction (AtkCocoa.Actions.AXDelete);
				}
			}
		}
		event EventHandler performIncrement;
		public event EventHandler PerformIncrement {
			add {
				if (performIncrement == null) {
					AddAction (AtkCocoa.Actions.AXIncrement);
				}
				performIncrement += value;
			}
			remove {
				performIncrement -= value;
				if (performIncrement == null) {
					RemoveAction (AtkCocoa.Actions.AXIncrement);
				}
			}
		}
		event EventHandler performPick;
		public event EventHandler PerformPick {
			add {
				if (performPick == null) {
					AddAction (AtkCocoa.Actions.AXPick);
				}
				performPick += value;
			}
			remove {
				performPick -= value;
				if (performPick == null) {
					RemoveAction (AtkCocoa.Actions.AXPick);
				}
			}
		}
		event EventHandler performPress;
		public event EventHandler PerformPress {
			add {
				if (performPress == null) {
					AddAction (AtkCocoa.Actions.AXPress);
				}
				performPress += value;
			}
			remove {
				performPress -= value;
				if (performPress == null) {
					RemoveAction (AtkCocoa.Actions.AXPress);
				}
			}
		}
		event EventHandler performRaise;
		public event EventHandler PerformRaise {
			add {
				if (performRaise == null) {
					AddAction (AtkCocoa.Actions.AXRaise);
				}
				performRaise += value;
			}
			remove {
				performRaise -= value;
				if (performRaise == null) {
					RemoveAction (AtkCocoa.Actions.AXRaise);
				}
			}
		}
		event EventHandler performShowAlternateUI;
		public event EventHandler PerformShowAlternateUI {
			add {
				if (performShowAlternateUI == null) {
					AddAction (AtkCocoa.Actions.AXShowAlternateUI);
				}
				performShowAlternateUI += value;
			}
			remove {
				performShowAlternateUI -= value;
				if (performShowAlternateUI == null) {
					RemoveAction (AtkCocoa.Actions.AXShowAlternateUI);
				}
			}
		}
		event EventHandler performShowDefaultUI;
		public event EventHandler PerformShowDefaultUI {
			add {
				if (performShowDefaultUI == null) {
					AddAction (AtkCocoa.Actions.AXShowDefaultUI);
				}
				performShowDefaultUI += value;
			}
			remove {
				performShowDefaultUI -= value;
				if (performShowDefaultUI == null) {
					RemoveAction (AtkCocoa.Actions.AXShowDefaultUI);
				}
			}
		}
		event EventHandler performShowMenu;
		public event EventHandler PerformShowMenu {
			add {
				if (performShowMenu == null) {
					AddAction (AtkCocoa.Actions.AXShowMenu);
				}
				performShowMenu += value;
			}
			remove {
				performShowMenu -= value;
				if (performShowMenu == null) {
					RemoveAction (AtkCocoa.Actions.AXShowMenu);
				}
			}
		}
	}

	// On anything other than Mac this is just a dummy class to prevent needing to have #ifdefs all over the main code
	public interface IAccessibilityElementProxy
	{
		event EventHandler PerformCancel;
		event EventHandler PerformConfirm;
		event EventHandler PerformDecrement;
		event EventHandler PerformDelete;
		event EventHandler PerformIncrement;
		event EventHandler PerformPick;
		event EventHandler PerformPress;
		event EventHandler PerformRaise;
		event EventHandler PerformShowAlternateUI;
		event EventHandler PerformShowDefaultUI;
		event EventHandler PerformShowMenu;

		Gtk.Widget GtkParent { get; set; }
		Gdk.Rectangle FrameInGtkParent { get; set; }
		Gdk.Rectangle FrameInParent { get; set; }

		void AddAccessibleChild (IAccessibilityElementProxy child);
		void SetRole (string role, string description = null);
		void SetRole (AtkCocoa.Roles role, string description = null);

		string Value { get; set; }
		string Title { get; set; }
		string Label { get; set; }
		string Identifier { get; set; }
		string Help { get; set; }
		bool Hidden { get; set; }

		// For Navigable Static Text
		Func<string> Contents { set; }
		Func<int> NumberOfCharacters { set; }
		Func<int> InsertionPointLineNumber { set; }
		Func<AtkCocoa.Range, Gdk.Rectangle> FrameForRange { set; }
		Func<int, int> LineForIndex { set; }
		Func<int, AtkCocoa.Range> RangeForLine { set; }
		Func<AtkCocoa.Range, string> StringForRange { set; }
		Func<int, AtkCocoa.Range> RangeForIndex { set; }
		Func<int, AtkCocoa.Range> StyleRangeForIndex { set; }
		Func<Gdk.Point, AtkCocoa.Range> RangeForPosition { set; }
	}
}