-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwindow_c.cpp
More file actions
726 lines (632 loc) · 18.5 KB
/
window_c.cpp
File metadata and controls
726 lines (632 loc) · 18.5 KB
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
#include "window_c.h"
#include <cstring>
#include <memory>
#include <mutex>
#include <unordered_map>
#include "../window.h"
#include "string_utils_c.h"
using namespace nativeapi;
// Internal registry to manage window lifetimes for C API
// This keeps shared_ptr alive while C code holds the handle
namespace {
std::mutex g_windows_mutex;
std::unordered_map<WindowId, std::shared_ptr<Window>> g_windows;
} // namespace
// Window creation and destruction
FFI_PLUGIN_EXPORT
native_window_t native_window_create(void) {
try {
// Create window with default settings
auto window = std::make_shared<Window>();
// Store in internal registry to keep it alive
{
std::lock_guard<std::mutex> lock(g_windows_mutex);
g_windows[window->GetId()] = window;
}
// Return raw pointer (internal registry holds the shared_ptr)
return static_cast<void*>(window.get());
} catch (...) {
return nullptr;
}
}
FFI_PLUGIN_EXPORT
native_window_t native_window_create_from_native(void* native_window) {
if (!native_window)
return nullptr;
try {
// Wrap existing native window
auto window = std::make_shared<Window>(native_window);
// Store in internal registry to keep it alive
{
std::lock_guard<std::mutex> lock(g_windows_mutex);
g_windows[window->GetId()] = window;
}
// Return raw pointer (internal registry holds the shared_ptr)
return static_cast<void*>(window.get());
} catch (...) {
return nullptr;
}
}
FFI_PLUGIN_EXPORT
void native_window_destroy(native_window_t window) {
if (!window)
return;
try {
auto* win = static_cast<nativeapi::Window*>(window);
WindowId window_id = win->GetId();
// Remove from internal registry (this will destroy it if no other references exist)
{
std::lock_guard<std::mutex> lock(g_windows_mutex);
g_windows.erase(window_id);
}
} catch (...) {
// Silently fail
}
}
// Window basic operations
FFI_PLUGIN_EXPORT
native_window_id_t native_window_get_id(native_window_t window) {
if (!window)
return -1;
auto* win = static_cast<nativeapi::Window*>(window);
return win->GetId();
}
FFI_PLUGIN_EXPORT
void native_window_focus(native_window_t window) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->Focus();
}
FFI_PLUGIN_EXPORT
void native_window_blur(native_window_t window) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->Blur();
}
FFI_PLUGIN_EXPORT
bool native_window_is_focused(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsFocused();
}
FFI_PLUGIN_EXPORT
void native_window_show(native_window_t window) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->Show();
}
FFI_PLUGIN_EXPORT
void native_window_show_inactive(native_window_t window) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->ShowInactive();
}
FFI_PLUGIN_EXPORT
void native_window_hide(native_window_t window) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->Hide();
}
FFI_PLUGIN_EXPORT
bool native_window_is_visible(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsVisible();
}
// Window state operations
FFI_PLUGIN_EXPORT
void native_window_maximize(native_window_t window) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->Maximize();
}
FFI_PLUGIN_EXPORT
void native_window_unmaximize(native_window_t window) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->Unmaximize();
}
FFI_PLUGIN_EXPORT
bool native_window_is_maximized(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsMaximized();
}
FFI_PLUGIN_EXPORT
void native_window_minimize(native_window_t window) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->Minimize();
}
FFI_PLUGIN_EXPORT
void native_window_restore(native_window_t window) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->Restore();
}
FFI_PLUGIN_EXPORT
bool native_window_is_minimized(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsMinimized();
}
FFI_PLUGIN_EXPORT
void native_window_set_fullscreen(native_window_t window, bool is_fullscreen) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetFullScreen(is_fullscreen);
}
FFI_PLUGIN_EXPORT
bool native_window_is_fullscreen(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsFullScreen();
}
// Window geometry operations
FFI_PLUGIN_EXPORT
void native_window_set_bounds(native_window_t window, native_rectangle_t bounds) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
Rectangle rect = {bounds.x, bounds.y, bounds.width, bounds.height};
win->SetBounds(rect);
}
FFI_PLUGIN_EXPORT
native_rectangle_t native_window_get_bounds(native_window_t window) {
native_rectangle_t result = {0.0, 0.0, 0.0, 0.0};
if (!window)
return result;
auto* win = static_cast<nativeapi::Window*>(window);
Rectangle bounds = win->GetBounds();
result.x = bounds.x;
result.y = bounds.y;
result.width = bounds.width;
result.height = bounds.height;
return result;
}
FFI_PLUGIN_EXPORT
void native_window_set_size(native_window_t window, double width, double height, bool animate) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
nativeapi::Size size = {width, height};
win->SetSize(size, animate);
}
FFI_PLUGIN_EXPORT
native_size_t native_window_get_size(native_window_t window) {
native_size_t result = {0.0, 0.0};
if (!window)
return result;
auto* win = static_cast<nativeapi::Window*>(window);
nativeapi::Size size = win->GetSize();
result.width = size.width;
result.height = size.height;
return result;
}
FFI_PLUGIN_EXPORT
void native_window_set_content_size(native_window_t window, double width, double height) {
if (!window)
return;
nativeapi::Size size = {width, height};
auto* win = static_cast<nativeapi::Window*>(window);
win->SetContentSize(size);
}
FFI_PLUGIN_EXPORT
native_size_t native_window_get_content_size(native_window_t window) {
native_size_t result = {0.0, 0.0};
if (!window)
return result;
auto* win = static_cast<nativeapi::Window*>(window);
nativeapi::Size size = win->GetContentSize();
result.width = size.width;
result.height = size.height;
return result;
}
FFI_PLUGIN_EXPORT
void native_window_set_content_bounds(native_window_t window, native_rectangle_t bounds) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
nativeapi::Rectangle rect = {bounds.x, bounds.y, bounds.width, bounds.height};
win->SetContentBounds(rect);
}
FFI_PLUGIN_EXPORT
native_rectangle_t native_window_get_content_bounds(native_window_t window) {
native_rectangle_t result = {0.0, 0.0, 0.0, 0.0};
if (!window)
return result;
auto* win = static_cast<nativeapi::Window*>(window);
nativeapi::Rectangle bounds = win->GetContentBounds();
result.x = bounds.x;
result.y = bounds.y;
result.width = bounds.width;
result.height = bounds.height;
return result;
}
FFI_PLUGIN_EXPORT
void native_window_set_minimum_size(native_window_t window, double width, double height) {
if (!window)
return;
nativeapi::Size size = {width, height};
auto* win = static_cast<nativeapi::Window*>(window);
win->SetMinimumSize(size);
}
FFI_PLUGIN_EXPORT
native_size_t native_window_get_minimum_size(native_window_t window) {
native_size_t result = {0.0, 0.0};
if (!window)
return result;
auto* win = static_cast<nativeapi::Window*>(window);
nativeapi::Size size = win->GetMinimumSize();
result.width = size.width;
result.height = size.height;
return result;
}
FFI_PLUGIN_EXPORT
void native_window_set_maximum_size(native_window_t window, double width, double height) {
if (!window)
return;
nativeapi::Size size = {width, height};
auto* win = static_cast<nativeapi::Window*>(window);
win->SetMaximumSize(size);
}
FFI_PLUGIN_EXPORT
native_size_t native_window_get_maximum_size(native_window_t window) {
native_size_t result = {0.0, 0.0};
if (!window)
return result;
auto* win = static_cast<nativeapi::Window*>(window);
nativeapi::Size size = win->GetMaximumSize();
result.width = size.width;
result.height = size.height;
return result;
}
FFI_PLUGIN_EXPORT
void native_window_set_position(native_window_t window, double x, double y) {
if (!window)
return;
nativeapi::Point point = {x, y};
auto* win = static_cast<nativeapi::Window*>(window);
win->SetPosition(point);
}
FFI_PLUGIN_EXPORT
native_point_t native_window_get_position(native_window_t window) {
native_point_t result = {0.0, 0.0};
if (!window)
return result;
auto* win = static_cast<nativeapi::Window*>(window);
nativeapi::Point point = win->GetPosition();
result.x = point.x;
result.y = point.y;
return result;
}
FFI_PLUGIN_EXPORT
void native_window_center(native_window_t window) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->Center();
}
// Window properties
FFI_PLUGIN_EXPORT
void native_window_set_resizable(native_window_t window, bool resizable) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetResizable(resizable);
}
FFI_PLUGIN_EXPORT
bool native_window_is_resizable(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsResizable();
}
FFI_PLUGIN_EXPORT
void native_window_set_movable(native_window_t window, bool movable) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetMovable(movable);
}
FFI_PLUGIN_EXPORT
bool native_window_is_movable(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsMovable();
}
FFI_PLUGIN_EXPORT
void native_window_set_minimizable(native_window_t window, bool minimizable) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetMinimizable(minimizable);
}
FFI_PLUGIN_EXPORT
bool native_window_is_minimizable(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsMinimizable();
}
FFI_PLUGIN_EXPORT
void native_window_set_maximizable(native_window_t window, bool maximizable) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetMaximizable(maximizable);
}
FFI_PLUGIN_EXPORT
bool native_window_is_maximizable(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsMaximizable();
}
FFI_PLUGIN_EXPORT
void native_window_set_fullscreenable(native_window_t window, bool fullscreenable) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetFullScreenable(fullscreenable);
}
FFI_PLUGIN_EXPORT
bool native_window_is_fullscreenable(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsFullScreenable();
}
FFI_PLUGIN_EXPORT
void native_window_set_closable(native_window_t window, bool closable) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetClosable(closable);
}
FFI_PLUGIN_EXPORT
bool native_window_is_closable(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsClosable();
}
FFI_PLUGIN_EXPORT
void native_window_set_window_control_buttons_visible(native_window_t window, bool visible) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetWindowControlButtonsVisible(visible);
}
FFI_PLUGIN_EXPORT
bool native_window_is_window_control_buttons_visible(native_window_t window) {
if (!window)
return true;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsWindowControlButtonsVisible();
}
FFI_PLUGIN_EXPORT
void native_window_set_always_on_top(native_window_t window, bool always_on_top) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetAlwaysOnTop(always_on_top);
}
FFI_PLUGIN_EXPORT
bool native_window_is_always_on_top(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsAlwaysOnTop();
}
FFI_PLUGIN_EXPORT
bool native_window_set_title(native_window_t window, const char* title) {
if (!window || !title)
return false;
try {
auto* win = static_cast<nativeapi::Window*>(window);
win->SetTitle(std::string(title));
return true;
} catch (...) {
return false;
}
}
FFI_PLUGIN_EXPORT
char* native_window_get_title(native_window_t window) {
if (!window)
return nullptr;
try {
auto* win = static_cast<nativeapi::Window*>(window);
std::string title = win->GetTitle();
return to_c_str(title);
} catch (...) {
return nullptr;
}
}
FFI_PLUGIN_EXPORT
void native_window_set_title_bar_style(native_window_t window, native_title_bar_style_t style) {
if (!window)
return;
try {
auto* win = static_cast<nativeapi::Window*>(window);
TitleBarStyle cpp_style =
(style == NATIVE_TITLE_BAR_STYLE_HIDDEN) ? TitleBarStyle::Hidden : TitleBarStyle::Normal;
win->SetTitleBarStyle(cpp_style);
} catch (...) {
// Silently fail
}
}
FFI_PLUGIN_EXPORT
native_title_bar_style_t native_window_get_title_bar_style(native_window_t window) {
if (!window)
return NATIVE_TITLE_BAR_STYLE_NORMAL;
try {
auto* win = static_cast<nativeapi::Window*>(window);
TitleBarStyle cpp_style = win->GetTitleBarStyle();
return (cpp_style == TitleBarStyle::Hidden) ? NATIVE_TITLE_BAR_STYLE_HIDDEN
: NATIVE_TITLE_BAR_STYLE_NORMAL;
} catch (...) {
return NATIVE_TITLE_BAR_STYLE_NORMAL;
}
}
FFI_PLUGIN_EXPORT
void native_window_set_has_shadow(native_window_t window, bool has_shadow) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetHasShadow(has_shadow);
}
FFI_PLUGIN_EXPORT
bool native_window_has_shadow(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->HasShadow();
}
FFI_PLUGIN_EXPORT
void native_window_set_opacity(native_window_t window, float opacity) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetOpacity(opacity);
}
FFI_PLUGIN_EXPORT
float native_window_get_opacity(native_window_t window) {
if (!window)
return 1.0f;
auto* win = static_cast<nativeapi::Window*>(window);
return win->GetOpacity();
}
FFI_PLUGIN_EXPORT
void native_window_set_visual_effect(native_window_t window, native_visual_effect_t effect) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetVisualEffect(static_cast<nativeapi::VisualEffect>(effect));
}
FFI_PLUGIN_EXPORT
native_visual_effect_t native_window_get_visual_effect(native_window_t window) {
if (!window)
return NATIVE_VISUAL_EFFECT_NONE;
auto* win = static_cast<nativeapi::Window*>(window);
return static_cast<native_visual_effect_t>(win->GetVisualEffect());
}
FFI_PLUGIN_EXPORT
void native_window_set_background_color(native_window_t window, native_color_t color) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
Color cpp_color = Color::FromRGBA(color.r, color.g, color.b, color.a);
win->SetBackgroundColor(cpp_color);
}
FFI_PLUGIN_EXPORT
native_color_t native_window_get_background_color(native_window_t window) {
native_color_t result = {255, 255, 255, 255}; // Default to white
if (!window)
return result;
auto* win = static_cast<nativeapi::Window*>(window);
Color cpp_color = win->GetBackgroundColor();
result.r = cpp_color.r;
result.g = cpp_color.g;
result.b = cpp_color.b;
result.a = cpp_color.a;
return result;
}
FFI_PLUGIN_EXPORT
void native_window_set_visible_on_all_workspaces(native_window_t window, bool visible) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetVisibleOnAllWorkspaces(visible);
}
FFI_PLUGIN_EXPORT
bool native_window_is_visible_on_all_workspaces(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsVisibleOnAllWorkspaces();
}
FFI_PLUGIN_EXPORT
void native_window_set_ignore_mouse_events(native_window_t window, bool ignore) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetIgnoreMouseEvents(ignore);
}
FFI_PLUGIN_EXPORT
bool native_window_is_ignore_mouse_events(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsIgnoreMouseEvents();
}
FFI_PLUGIN_EXPORT
void native_window_set_focusable(native_window_t window, bool focusable) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->SetFocusable(focusable);
}
FFI_PLUGIN_EXPORT
bool native_window_is_focusable(native_window_t window) {
if (!window)
return false;
auto* win = static_cast<nativeapi::Window*>(window);
return win->IsFocusable();
}
// Window interactions
FFI_PLUGIN_EXPORT
void native_window_start_dragging(native_window_t window) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->StartDragging();
}
FFI_PLUGIN_EXPORT
void native_window_start_resizing(native_window_t window) {
if (!window)
return;
auto* win = static_cast<nativeapi::Window*>(window);
win->StartResizing();
}
// Platform-specific functions
FFI_PLUGIN_EXPORT
void* native_window_get_native_object(native_window_t window) {
if (!window)
return nullptr;
auto* win = static_cast<nativeapi::Window*>(window);
return win->GetNativeObject();
}
// Memory management
FFI_PLUGIN_EXPORT
void native_window_free_string(char* str) {
free_c_str(str);
}
FFI_PLUGIN_EXPORT
void native_window_list_free(native_window_list_t* list) {
if (!list)
return;
if (list->windows) {
// Note: We don't delete the individual window handles here
// because they are managed by the window manager
delete[] list->windows;
}
list->windows = nullptr;
list->count = 0;
}