/
simwin.cc
1697 lines (1443 loc) · 47.4 KB
/
simwin.cc
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1997 - 2003 Hansjörg Malthaner
*
* This file is part of the Simutrans project under the artistic license.
* (see license.txt)
*/
/* Subfenster fuer Sim
* keine Klasse, da die funktionen von C-Code aus aufgerufen werden koennen
*
* Die Funktionen implementieren ein 'Object' Windowmanager
* Es gibt nur diesen einen Windowmanager
*
* 17.11.97, Hj. Malthaner
*
* Fenster jetzt typisiert
* 21.06.98, Hj. Malthaner
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "simcolor.h"
#include "simevent.h"
#include "simgraph.h"
#include "simmenu.h"
#include "simskin.h"
#include "simsys.h"
#include "simticker.h"
#include "simwin.h"
#include "simhalt.h"
#include "simworld.h"
#include "dataobj/translator.h"
#include "dataobj/umgebung.h"
#include "dataobj/loadsave.h"
#include "besch/skin_besch.h"
#include "dings/zeiger.h"
#include "gui/map_frame.h"
#include "gui/help_frame.h"
#include "gui/messagebox.h"
#include "gui/gui_frame.h"
#include "player/simplay.h"
#include "tpl/vector_tpl.h"
#include "utils/simstring.h"
#include "utils/cbuffer_t.h"
// needed to restore/save them
#include "gui/werkzeug_waehler.h"
#include "gui/player_frame_t.h"
#include "gui/money_frame.h"
#include "gui/halt_detail.h"
#include "gui/halt_info.h"
#include "gui/convoi_detail_t.h"
#include "gui/convoi_info_t.h"
#include "gui/fahrplan_gui.h"
#include "gui/line_management_gui.h"
#include "gui/schedule_list.h"
#define dragger_size 12
// (Mathew Hounsell)
// I added a button to the map window to fix it's size to the best one.
// This struct is the flow back to the object of the refactoring.
class simwin_gadget_flags_t
{
public:
simwin_gadget_flags_t( void ) : title(true), close( false ), help( false ), prev( false ), size( false ), next( false ), sticky( false ), gotopos( false ) { }
bool title:1;
bool close:1;
bool help:1;
bool prev:1;
bool size:1;
bool next:1;
bool sticky:1;
bool gotopos:1;
};
class simwin_t
{
public:
koord pos; // Fensterposition
uint32 dauer; // Wie lange soll das Fenster angezeigt werden ?
uint8 wt; // the flags for the window type
long magic_number; // either magic number or this pointer (which is unique too)
gui_frame_t *gui;
bool closing;
bool sticky; // true if window is sticky
bool rollup;
simwin_gadget_flags_t flags; // (Mathew Hounsell) See Above.
simwin_t() : flags() {}
bool operator== (const simwin_t &) const;
};
bool simwin_t::operator== (const simwin_t &other) const { return gui == other.gui; }
// true , if windows need to be redraw "dirty" (including title)
static bool windows_dirty = false;
#define MAX_WIN (64)
static vector_tpl<simwin_t> wins(MAX_WIN);
static vector_tpl<simwin_t> kill_list(MAX_WIN);
static karte_t* wl = NULL; // Zeiger auf aktuelle Welt, wird in win_set_welt gesetzt
static int top_win(int win, bool keep_state );
static void display_win(int win);
// Hajo: tooltip data
static int tooltip_xpos = 0;
static int tooltip_ypos = 0;
static const char * tooltip_text = 0;
static const char * static_tooltip_text = 0;
// Knightly : For timed tooltip with initial delay and finite visible duration.
// Valid owners are required for timing. Invalid (NULL) owners disable timing.
static const void * tooltip_owner = 0; // owner of the registered tooltip
static const void * tooltip_group = 0; // group to which the owner belongs
static unsigned long tooltip_register_time = 0; // time at which a tooltip is initially registered
static bool show_ticker=0;
/* Hajo: if we are inside the event handler,
* the window handler has gui pointer as value,
* to defer destruction if this window
*/
static void *inside_event_handling = NULL;
/* only this gui element can set a tooltip
*/
static void *tooltip_element = NULL;
static void destroy_framed_win(simwin_t *win);
//=========================================================================
// Helper Functions
#define REVERSE_GADGETS (!umgebung_t::window_buttons_right)
// (Mathew Hounsell) A "Gadget Box" is a windows control button.
enum simwin_gadget_et { GADGET_CLOSE, GADGET_HELP, GADGET_SIZE, GADGET_PREV, GADGET_NEXT, GADGET_GOTOPOS=36, GADGET_STICKY=21, GADGET_STICKY_PUSHED, COUNT_GADGET=255 };
/**
* Display a window gadget
* @author Hj. Malthaner
*/
static int display_gadget_box(simwin_gadget_et const code,
int const x, int const y,
int const color,
bool const pushed)
{
display_vline_wh_clip(x, y, 16, color+1, false);
display_vline_wh_clip(x+15, y+1, 14, COL_BLACK, false);
display_vline_wh_clip(x+16, y+1, 14, color+1, false);
if(pushed) {
display_fillbox_wh_clip(x+1, y+1, 14, 14, color+1, false);
}
image_id img = IMG_LEER;
if( skinverwaltung_t::window_skin ) {
// "x", "?", "=", "«", "»"
img = skinverwaltung_t::window_skin->get_bild_nr(code+1);
}
if( img != IMG_LEER ) {
display_color_img(img, x, y, 0, false, false);
}
else {
const char *gadget_text = "#";
static const char *gadget_texts[5]={ "X", "?", "=", "<", ">" };
if( code <= GADGET_NEXT ) {
gadget_text = gadget_texts[code];
}
else if( code == GADGET_GOTOPOS ) {
gadget_text = "*";
}
else if( code == GADGET_STICKY ) {
gadget_text = "s";
}
else if( code == GADGET_STICKY_PUSHED ) {
gadget_text = "S";
}
display_proportional( x+4, y+4, gadget_text, ALIGN_LEFT, COL_BLACK, false );
}
// Hajo: return width of gadget
return 16;
}
//-------------------------------------------------------------------------
// (Mathew Hounsell) Created
static int display_gadget_boxes(
simwin_gadget_flags_t const * const flags,
int const x, int const y,
int const color,
bool const close_pushed,
bool const sticky_pushed
) {
int width = 0;
const int w=(REVERSE_GADGETS?16:-16);
// Only the close and sticky gadget can be pushed.
if( flags->close ) {
display_gadget_box( GADGET_CLOSE, x +w*width, y, color, close_pushed );
width ++;
}
if( flags->size ) {
display_gadget_box( GADGET_SIZE, x + w*width, y, color, false );
width++;
}
if( flags->help ) {
display_gadget_box( GADGET_HELP, x + w*width, y, color, false );
width++;
}
if( flags->prev ) {
display_gadget_box( GADGET_PREV, x + w*width, y, color, false );
width++;
}
if( flags->next ) {
display_gadget_box( GADGET_NEXT, x + w*width, y, color, false );
width++;
}
if( flags->gotopos ) {
display_gadget_box( GADGET_GOTOPOS, x + w*width, y, color, false );
width++;
}
if( flags->sticky ) {
display_gadget_box( sticky_pushed ? GADGET_STICKY_PUSHED : GADGET_STICKY, x + w*width, y, color, sticky_pushed );
width++;
}
return abs( w*width );
}
static simwin_gadget_et decode_gadget_boxes(
simwin_gadget_flags_t const * const flags,
int const x,
int const px
) {
int offset = px-x;
const int w=(REVERSE_GADGETS?-16:16);
//DBG_MESSAGE("simwin_gadget_et decode_gadget_boxes()","offset=%i, w=%i",offset, w );
// Only the close gadget can be pushed.
if( flags->close ) {
if( offset >= 0 && offset<16 ) {
//DBG_MESSAGE("simwin_gadget_et decode_gadget_boxes()","close" );
return GADGET_CLOSE;
}
offset += w;
}
if( flags->size ) {
if( offset >= 0 && offset<16 ) {
//DBG_MESSAGE("simwin_gadget_et decode_gadget_boxes()","size" );
return GADGET_SIZE;
}
offset += w;
}
if( flags->help ) {
if( offset >= 0 && offset<16 ) {
//DBG_MESSAGE("simwin_gadget_et decode_gadget_boxes()","help" );
return GADGET_HELP;
}
offset += w;
}
if( flags->prev ) {
if( offset >= 0 && offset<16 ) {
return GADGET_PREV;
}
offset += w;
}
if( flags->next ) {
if( offset >= 0 && offset<16 ) {
return GADGET_NEXT;
}
offset += w;
}
if( flags->gotopos ) {
if( offset >= 0 && offset<16 ) {
return GADGET_GOTOPOS;
}
offset += w;
}
if( flags->sticky ) {
if( offset >= 0 && offset<16 ) {
return GADGET_STICKY;
}
offset += w;
}
return COUNT_GADGET;
}
//-------------------------------------------------------------------------
// (Mathew Hounsell) Refactored
static void win_draw_window_title(const koord pos, const koord gr,
const PLAYER_COLOR_VAL titel_farbe,
const char * const text,
const PLAYER_COLOR_VAL text_farbe,
const koord3d welt_pos,
const bool closing,
const bool sticky,
simwin_gadget_flags_t &flags )
{
PUSH_CLIP(pos.x, pos.y, gr.x, gr.y);
display_fillbox_wh_clip(pos.x, pos.y, gr.x, 1, titel_farbe+1, false);
display_fillbox_wh_clip(pos.x, pos.y+1, gr.x, 14, titel_farbe, false);
display_fillbox_wh_clip(pos.x, pos.y+15, gr.x, 1, COL_BLACK, false);
display_vline_wh_clip(pos.x+gr.x-1, pos.y, 15, COL_BLACK, false);
// Draw the gadgets and then move left and draw text.
flags.gotopos = (welt_pos != koord3d::invalid);
int width = display_gadget_boxes( &flags, pos.x+(REVERSE_GADGETS?0:gr.x-20), pos.y, titel_farbe, closing, sticky );
int titlewidth = display_proportional_clip( pos.x + (REVERSE_GADGETS?width+4:4), pos.y+(16-LINEASCENT)/2, text, ALIGN_LEFT, text_farbe, false );
if( flags.gotopos ) {
display_proportional_clip( pos.x + (REVERSE_GADGETS?width+4:4)+titlewidth+8, pos.y+(16-LINEASCENT)/2, welt_pos.get_2d().get_fullstr(), ALIGN_LEFT, text_farbe, false );
}
POP_CLIP();
}
//-------------------------------------------------------------------------
/**
* Draw dragger widget
* @author Hj. Malthaner
*/
static void win_draw_window_dragger(koord pos, koord gr)
{
pos += gr;
if( skinverwaltung_t::window_skin && skinverwaltung_t::window_skin->get_bild_nr(36)!=IMG_LEER ) {
const bild_besch_t *dragger = skinverwaltung_t::window_skin->get_bild(36);
display_color_img( dragger->get_nummer(), pos.x-dragger->get_pic()->w, pos.y-dragger->get_pic()->h, 0, false, false);
}
else {
for( int x=0; x<dragger_size; x++ ) {
display_fillbox_wh( pos.x-x, pos.y-dragger_size+x, x, 1, (x & 1) ? COL_BLACK : MN_GREY4, true);
}
}
}
//=========================================================================
// returns the window (if open) otherwise zero
gui_frame_t *win_get_magic(long magic)
{
if(magic!=-1 && magic!=0) {
// es kann nur ein fenster fuer jede pos. magic number geben
FOR(vector_tpl<simwin_t>, const& i, wins) {
if (i.magic_number == magic) {
// if 'special' magic number, return it
return i.gui;
}
}
}
return NULL;
}
/**
* Returns top window
* @author prissi
*/
gui_frame_t *win_get_top()
{
return wins.empty() ? 0 : wins.back().gui;
}
/**
* returns the focused component of the top window
* @author Knightly
*/
gui_komponente_t *win_get_focus()
{
return wins.empty() ? 0 : wins.back().gui->get_focus();
}
int win_get_open_count()
{
return wins.get_count();
}
// brings a window to front, if open
bool top_win( const gui_frame_t *gui, bool keep_rollup )
{
for( uint i=0; i<wins.get_count()-1; i++ ) {
if(wins[i].gui==gui) {
top_win(i,keep_rollup);
return true;
}
}
// not open
return false;
}
/**
* Checks if a window is a top level window
* @author Hj. Malthaner
*/
bool win_is_top(const gui_frame_t *ig)
{
return !wins.empty() && wins.back().gui == ig;
}
// window functions
// save/restore all dialogues
void rdwr_all_win(loadsave_t *file)
{
if( file->get_version()>102003 ) {
if( file->is_saving() ) {
FOR(vector_tpl<simwin_t>, & i, wins) {
uint32 id = i.gui->get_rdwr_id();
if( id!=magic_reserved ) {
file->rdwr_long( id );
i.pos.rdwr(file);
file->rdwr_byte(i.wt);
file->rdwr_bool(i.sticky);
file->rdwr_bool(i.rollup);
i.gui->rdwr(file);
}
}
uint32 end = magic_none;
file->rdwr_long( end );
}
else {
// restore windows
while(1) {
uint32 id;
file->rdwr_long(id);
// create the matching
gui_frame_t *w = NULL;
switch(id) {
// end of dialogues
case magic_none: return;
// actual dialogues to restore
case magic_convoi_info: w = new convoi_info_t(wl); break;
case magic_convoi_detail: w = new convoi_detail_t(wl); break;
case magic_halt_info: w = new halt_info_t(wl); break;
case magic_halt_detail: w = new halt_detail_t(wl); break;
case magic_reliefmap: w = new map_frame_t(wl); break;
case magic_ki_kontroll_t: w = new ki_kontroll_t(wl); break;
case magic_schedule_rdwr_dummy: w = new fahrplan_gui_t(wl); break;
case magic_line_schedule_rdwr_dummy: w = new line_management_gui_t(wl); break;
default:
if( id>=magic_finances_t && id<magic_finances_t+MAX_PLAYER_COUNT ) {
w = new money_frame_t( wl->get_spieler(id-magic_finances_t) );
}
else if( id>=magic_line_management_t && id<magic_line_management_t+MAX_PLAYER_COUNT ) {
w = new schedule_list_gui_t( wl->get_spieler(id-magic_line_management_t) );
}
else if( id>=magic_toolbar && id<magic_toolbar+256 ) {
werkzeug_t::toolbar_tool[id-magic_toolbar]->update(wl,wl->get_active_player());
w = werkzeug_t::toolbar_tool[id-magic_toolbar]->get_werkzeug_waehler();
}
else {
dbg->fatal( "rdwr_all_win()", "No idea how to restore magic $%Xlu", id );
}
}
/* sequece is now the same for all dialogues
* restore coordinates
* create window
* read state
* restore content
* restore state - gui_frame_t::rdwr() might create its own window ->> want to restore state to that window
*/
koord p;
p.rdwr(file);
uint8 win_type;
file->rdwr_byte( win_type );
create_win( p.x, p.y, w, (wintype)win_type, id );
bool sticky, rollup;
file->rdwr_bool( sticky );
file->rdwr_bool( rollup );
w->rdwr( file );
wins.back().sticky = sticky;
wins.back().rollup = rollup;
}
}
}
}
int create_win(gui_frame_t* const gui, wintype const wt, long const magic)
{
return create_win( -1, -1, gui, wt, magic);
}
int create_win(int x, int y, gui_frame_t* const gui, wintype const wt, long const magic)
{
assert(gui!=NULL && magic!=0);
if( magic!=magic_none && win_get_magic(magic) ) {
top_win( win_get_magic(magic) );
return -1;
}
/* if there are too many handles (likely in large games)
* we search for any error/news message at the bottom of the stack and delete it
* => newer information might be more important ...
*/
if( wins.get_count()==MAX_WIN ) {
// we try to remove one of the lowest news windows (magic_none)
for( uint i=0; i<MAX_WIN; i++ ) {
if( wins[i].magic_number == magic_none && dynamic_cast<news_window *>(wins[i].gui)!=NULL ) {
destroy_win( wins[i].gui );
break;
}
}
}
if( wins.get_count() < MAX_WIN ) {
if (!wins.empty()) {
// mark old title dirty
mark_rect_dirty_wc( wins.back().pos.x, wins.back().pos.y, wins.back().pos.x+wins.back().gui->get_fenstergroesse().x, wins.back().pos.y+16 );
}
wins.append( simwin_t() );
simwin_t& win = wins.back();
// (Mathew Hounsell) Make Sure Closes Aren't Forgotten.
// Must Reset as the entries and thus flags are reused
win.flags.close = true;
win.flags.title = gui->has_title();
win.flags.help = ( gui->get_hilfe_datei() != NULL );
win.flags.prev = gui->has_prev();
win.flags.next = gui->has_next();
win.flags.size = gui->has_min_sizer();
win.flags.sticky = gui->has_sticky();
win.gui = gui;
// take care of time delete windows ...
win.wt = wt & w_time_delete ? w_info : wt;
win.dauer = (wt&w_time_delete) ? MESG_WAIT : -1;
win.magic_number = magic;
win.closing = false;
win.rollup = false;
win.sticky = false;
// Hajo: Notify window to be shown
assert(gui);
event_t ev;
ev.ev_class = INFOWIN;
ev.ev_code = WIN_OPEN;
ev.mx = 0;
ev.my = 0;
ev.cx = 0;
ev.cy = 0;
ev.button_state = 0;
void *old = inside_event_handling;
inside_event_handling = gui;
gui->infowin_event(&ev);
inside_event_handling = old;
koord gr = gui->get_fenstergroesse();
if(x == -1) {
// try to keep the toolbar below all other toolbars
y = 32;
if(wt & w_no_overlap) {
for( uint32 i=0; i<wins.get_count()-1; i++ ) {
if(wins[i].wt & w_no_overlap) {
if(wins[i].pos.y>=y) {
sint16 lower_y = wins[i].pos.y + wins[i].gui->get_fenstergroesse().y;
if(lower_y >= y) {
y = lower_y;
}
}
}
}
// right aligned
// x = max( 0, display_get_width()-gr.x );
// but we go for left
x = 0;
y = min( y, display_get_height()-gr.y );
}
else {
x = min(get_maus_x() - gr.x/2, display_get_width()-gr.x);
y = min(get_maus_y() - gr.y-32, display_get_height()-gr.y);
}
}
if(x<0) {
x = 0;
}
if(y<32) {
y = 32;
}
win.pos = koord(x,y);
mark_rect_dirty_wc( x, y, x+gr.x, y+gr.y );
return wins.get_count();
}
else {
return -1;
}
}
/* sometimes a window cannot destroyed while it is still handled;
* in those cases it will added to kill list and it is only destructed
* by this function
*/
static void process_kill_list()
{
FOR(vector_tpl<simwin_t>, & i, kill_list) {
wins.remove(i);
destroy_framed_win(&i);
}
kill_list.clear();
}
/**
* Destroy a framed window
* @author Hj. Malthaner
*/
static void destroy_framed_win(simwin_t *wins)
{
// mark dirty
koord gr = wins->gui->get_fenstergroesse();
mark_rect_dirty_wc( wins->pos.x, wins->pos.y, wins->pos.x+gr.x, wins->pos.y+gr.y );
if(wins->gui) {
event_t ev;
ev.ev_class = INFOWIN;
ev.ev_code = WIN_CLOSE;
ev.mx = 0;
ev.my = 0;
ev.cx = 0;
ev.cy = 0;
ev.button_state = 0;
void *old = inside_event_handling;
inside_event_handling = wins->gui;
wins->gui->infowin_event(&ev);
inside_event_handling = old;
}
if( (wins->wt&w_do_not_delete)==0 ) {
// remove from kill list first
// otherwise delete will be called again on that window
for( uint j = 0; j < kill_list.get_count(); j++ ) {
if( kill_list[j].gui == wins->gui ) {
kill_list.remove_at(j);
break;
}
}
delete wins->gui;
}
windows_dirty = true;
}
void destroy_win(const long magic)
{
const gui_frame_t *gui = win_get_magic(magic);
if(gui) {
destroy_win( gui );
}
}
void destroy_win(const gui_frame_t *gui)
{
for( uint i=0; i<wins.get_count(); i++ ) {
if(wins[i].gui == gui) {
if(inside_event_handling==wins[i].gui) {
kill_list.append_unique(wins[i]);
}
else {
simwin_t win = wins[i];
wins.remove_at(i);
destroy_framed_win(&win);
}
break;
}
}
}
void destroy_all_win(bool destroy_sticky)
{
for ( int curWin=0 ; curWin < (int)wins.get_count() ; curWin++ ) {
if( destroy_sticky || !wins[curWin].sticky ) {
if( inside_event_handling==wins[curWin].gui ) {
// only add this, if not already added
kill_list.append_unique(wins[curWin]);
}
else {
destroy_framed_win(&wins[curWin]);
}
// compact the window list
wins.remove_at(curWin);
curWin--;
}
}
}
int top_win(int win, bool keep_state )
{
if( (uint32)win==wins.get_count()-1 ) {
return win;
} // already topped
// mark old title dirty
mark_rect_dirty_wc( wins.back().pos.x, wins.back().pos.y, wins.back().pos.x+wins.back().gui->get_fenstergroesse().x, wins.back().pos.y+16 );
simwin_t tmp = wins[win];
wins.remove_at(win);
if( !keep_state ) {
tmp.rollup = false; // make visible when topping
}
wins.append(tmp);
// mark new dirty
koord gr = wins.back().gui->get_fenstergroesse();
mark_rect_dirty_wc( wins.back().pos.x, wins.back().pos.y, wins.back().pos.x+gr.x, wins.back().pos.y+gr.y );
event_t ev;
ev.ev_class = INFOWIN;
ev.ev_code = WIN_TOP;
ev.mx = 0;
ev.my = 0;
ev.cx = 0;
ev.cy = 0;
ev.button_state = 0;
void *old = inside_event_handling;
inside_event_handling = tmp.gui;
tmp.gui->infowin_event(&ev);
inside_event_handling = old;
return wins.get_count()-1;
}
void display_win(int win)
{
// ok, now process it
gui_frame_t *komp = wins[win].gui;
koord gr = komp->get_fenstergroesse();
koord pos = wins[win].pos;
PLAYER_COLOR_VAL title_color = (komp->get_titelcolor()&0xF8)+umgebung_t::front_window_bar_color;
PLAYER_COLOR_VAL text_color = +umgebung_t::front_window_text_color;
if( (unsigned)win!=wins.get_count()-1 ) {
// not top => maximum brightness
title_color = (title_color&0xF8)+umgebung_t::bottom_window_bar_color;
text_color = umgebung_t::bottom_window_text_color;
}
bool need_dragger = komp->get_resizemode() != gui_frame_t::no_resize;
// %HACK (Mathew Hounsell) So draw will know if gadget is needed.
wins[win].flags.help = ( komp->get_hilfe_datei() != NULL );
if( wins[win].flags.title ) {
win_draw_window_title(wins[win].pos,
gr,
title_color,
komp->get_name(),
text_color,
komp->get_weltpos(),
wins[win].closing,
wins[win].sticky,
wins[win].flags );
}
// mark top window, if requested
if(umgebung_t::window_frame_active && (unsigned)win==wins.get_count()-1) {
const int y_off = wins[win].flags.title ? 0 : 16;
if(!wins[win].rollup) {
display_ddd_box( wins[win].pos.x-1, wins[win].pos.y-1 + y_off, gr.x+2, gr.y+2 - y_off, title_color, title_color+1 );
}
else {
display_ddd_box( wins[win].pos.x-1, wins[win].pos.y-1 + y_off, gr.x+2, 18 - y_off, title_color, title_color+1 );
}
}
if(!wins[win].rollup) {
komp->zeichnen(wins[win].pos, gr);
// dragger zeichnen
if(need_dragger) {
win_draw_window_dragger( pos, gr);
}
}
}
void display_all_win()
{
// first: empty kill list
process_kill_list();
// check which window can set tooltip
const sint16 x = get_maus_x();
const sint16 y = get_maus_y();
tooltip_element = NULL;
for( uint32 i = wins.get_count(); i-- != 0; ) {
if( (!wins[i].rollup && wins[i].gui->getroffen(x-wins[i].pos.x,y-wins[i].pos.y)) ||
(wins[i].rollup && x>=wins[i].pos.x && x<wins[i].pos.x+wins[i].gui->get_fenstergroesse().x && y>=wins[i].pos.y && y<wins[i].pos.y+16)
) {
// tooltips are only allowed for this window
tooltip_element = wins[i].gui;
break;
}
}
// then display windows
for( uint i=0; i<wins.get_count(); i++ ) {
void *old_gui = inside_event_handling;
inside_event_handling = wins[i].gui;
display_win(i);
inside_event_handling = old_gui;
}
}
void win_rotate90( sint16 new_ysize )
{
FOR(vector_tpl<simwin_t>, const& i, wins) {
i.gui->map_rotate90(new_ysize);
}
}
static void remove_old_win()
{
// alte fenster entfernen, falls dauer abgelaufen
for( int i=wins.get_count()-1; i>=0; i=min(i,(int)wins.get_count())-1 ) {
if(wins[i].dauer > 0) {
wins[i].dauer --;
if(wins[i].dauer == 0) {
destroy_win( wins[i].gui );
}
}
}
}
static inline void snap_check_distance( sint16 *r, const sint16 a, const sint16 b )
{
if( abs(a-b)<=umgebung_t::window_snap_distance ) {
*r = a;
}
}
void snap_check_win( const int win, koord *r, const koord from_pos, const koord from_gr, const koord to_pos, const koord to_gr )
{
bool resize;
if( from_gr==to_gr && from_pos!=to_pos ) { // check if we're moving
resize = false;
}
else if( from_gr!=to_gr && from_pos==from_pos ) { // or resizing the window
resize = true;
}
else {
return; // or nothing to do.
}
const int wins_count = wins.get_count();
for( int i=0; i<=wins_count; i++ ) {
if( i==win ) {
// Don't snap to self
continue;
}
koord other_gr, other_pos;
if( i==wins_count ) {
// Allow snap to screen edge
other_pos.x = 0;
other_pos.y = 32;
other_gr.x = display_get_width();
other_gr.y = display_get_height()-16-32;
if( show_ticker ) {
other_gr.y -= 16;
}
}
else {
// Snap to other window
other_gr = wins[i].gui->get_fenstergroesse();
other_pos = wins[i].pos;
if( wins[i].rollup ) {
other_gr.y = 18;
}
}
// my bottom below other top and my top above other bottom ---- in same vertical band
if( from_pos.y+from_gr.y>=other_pos.y && from_pos.y<=other_pos.y+other_gr.y ) {
if( resize ) {
// other right side and my new right side within snap
snap_check_distance( &r->x, other_pos.x+other_gr.x-from_pos.x, to_gr.x ); // snap right - align right sides
}
else {
// other right side and my new right side within snap
snap_check_distance( &r->x, other_pos.x+other_gr.x-from_gr.x, to_pos.x ); // snap right - align right sides
// other left side and my new left side within snap
snap_check_distance( &r->x, other_pos.x, to_pos.x ); // snap left - align left sides
}
}
// my new bottom below other top and my new top above other bottom ---- in same vertical band
if( resize ) {
if( from_pos.y+to_gr.y>other_pos.y && from_pos.y<other_pos.y+other_gr.y ) {
// other left side and my new right side within snap
snap_check_distance( &r->x, other_pos.x-from_pos.x, to_gr.x ); // snap right - align my right to other left
}
}
else {
if( to_pos.y+from_gr.y>other_pos.y && to_pos.y<other_pos.y+other_gr.y ) {
// other left side and my new right side within snap
snap_check_distance( &r->x, other_pos.x-from_gr.x, to_pos.x ); // snap right - align my right to other left
// other right side and my new left within snap
snap_check_distance( &r->x, other_pos.x+other_gr.x, to_pos.x ); // snap left - align my left to other right
}
}
// my right side right of other left side and my left side left of other right side ---- in same horizontal band
if( from_pos.x+from_gr.x>=other_pos.x && from_pos.x<=other_pos.x+other_gr.x ) {
if( resize ) {
// other bottom and my new bottom within snap
snap_check_distance( &r->y, other_pos.y+other_gr.y-from_pos.y, to_gr.y ); // snap down - align bottoms
}
else {
// other bottom and my new bottom within snap
snap_check_distance( &r->y, other_pos.y+other_gr.y-from_gr.y, to_pos.y ); // snap down - align bottoms
// other top and my new top within snap
snap_check_distance( &r->y, other_pos.y, to_pos.y ); // snap up - align tops
}
}
// my new right side right of other left side and my new left side left of other right side ---- in same horizontal band
if ( resize ) {
if( from_pos.x+to_gr.x>other_pos.x && from_pos.x<other_pos.x+other_gr.x ) {
// other top and my new bottom within snap
snap_check_distance( &r->y, other_pos.y-from_pos.y, to_gr.y ); // snap down - align my bottom to other top
}
}
else {
if( to_pos.x+from_gr.x>other_pos.x && to_pos.x<other_pos.x+other_gr.x ) {
// other top and my new bottom within snap
snap_check_distance( &r->y, other_pos.y-from_gr.y, to_pos.y ); // snap down - align my bottom to other top
// other bottom and my new top within snap
snap_check_distance( &r->y, other_pos.y+other_gr.y, to_pos.y ); // snap up - align my top to other bottom
}
}
}
}
void move_win(int win, event_t *ev)
{
const koord mouse_from( ev->cx, ev->cy );
const koord mouse_to( ev->mx, ev->my );
const koord from_pos = wins[win].pos;
koord from_gr = wins[win].gui->get_fenstergroesse();
if( wins[win].rollup ) {
from_gr.y = 18;