blib/lib/Games/Axmud/Obj/Workspace.pm untested

filter: Statementstmt 0.0% 0 / 676 Branchbran 0.0% 0 / 400 Conditioncond 0.0% 0 / 402 MC/DCmcdc 0.0% 0 / 224 Subroutinesub 0.0% 0 / 41 Podpod 0.0% 0 / 38 totaltotal 0.0% 0 / 1781 CC 201 SCAR 62.2
CC
201
Cov
0%
createGridWin
1980.0
createSimpleGridWin
600.0
getWinmap
306.0
CRAP 503.2
1 # Copyright (C) 2011-2024 A S Lewis
2 #
3 # This program is free software: you can redistribute it and/or modify it under the terms of the GNU
4 # General Public License as published by the Free Software Foundation, either version 3 of the
5 # License, or (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License along with this program. If not,
12 # see <http://www.gnu.org/licenses/>.
13 #
14 #
15 # Games::Axmud::Obj::Workspace
16 # The workspace object. Arranges windows on a workspace grid on a single workspace
17  
18 { package Games::Axmud::Obj::Workspace;
19  
20 use strict;
21 use warnings;
22 # use diagnostics;
23  
24 use Glib qw(TRUE FALSE);
25  
26 our @ISA = qw(Games::Axmud);
27  
28 ##################
29 # Constructors
30  
31 sub new {
32  
33 # Called by GA::Obj::Desktop->add_workspace()
34 #
35 # Expected arguments
36 # $number - Unique number for this workspace object
37 # $systemNum - The corresponding system workspace number (0 if not known)
38 #
39 # Return values
40 # 'undef' on improper arguments
41 # Blessed reference to the newly-created object on success
42  
43 my ($class, $number, $systemNum, $check) = @_;
44  
45 # Check for improper arguments
46 if (! defined $class || ! defined $number || ! defined $systemNum || defined $check) {
47  
48 return $axmud::CLIENT->writeImproper($class . '->new', @_);
49 }
50  
51 # Setup
52 my $self = {
53 _objName => 'workspace_' . $number,
54 _objClass => $class,
55 _parentFile => undef, # No parent file object
56 _parentWorld => undef, # No parent file object
57 _privFlag => TRUE, # All IVs are private
58  
59 # IVs
60 # ---
61  
62 # Unique number for this workspace object (across all sessions)
63 number => $number,
64 # The corresponding system workspace number (0 if not known)
65 systemNum => $systemNum,
66  
67 # The current size of the workspace, set by $self->start
68 currentWidth => undef,
69 currentHeight => undef,
70 # The size of any panels detected by the call to $self->findPanelSize, which tries to
71 # detect panel sizes for this workspace. If detection fails, it uses values specified
72 # by IVs in GA::Client instead
73 panelLeftSize => undef,
74 panelRightSize => undef,
75 panelTopSize => undef,
76 panelBottomSize => undef,
77 # The size of window controls detected by the call to $self->findWinControlSize, which
78 # creates test windows to determine the sizes. If the test fails, Axmud uses values
79 # specified by GA::Client instead
80 controlsLeftSize => undef,
81 controlsRightSize => undef,
82 controlsTopSize => undef,
83 controlsBottomSize => undef,
84  
85 # Registry hash of workspace grid objects that have been created on this workspace (a
86 # subset of GA::Obj::Desktop->gridHash). Hash in the form
87 # $gridHash{number} = blessed_reference_to_workspace_grid_object
88 gridHash => {},
89 # In some cases workspace grids are not enabled on this workspace, in which case this
90 # flag is set by $self->start
91 # TRUE - workspace grids are enabled on this workspace (the default setting)
92 # FALSE - workspace grids are disabled on this workspace (and this is the only
93 # workspace used by Axmud - see comments in GA::Obj::Desktop->new).
94 # Axmud doesn't change the position of any windows, which are left
95 # where the system's window manager puts them. Window controls are
96 # compulsory
97 gridEnableFlag => TRUE,
98 # The name of the default zonemap to use for this workspace (which initially depends on
99 # GA::Client->initWorkspaceHash). Set only while $self->gridEnableFlag remains set
100 # to TRUE
101 defaultZonemap => undef,
102 };
103  
104 # Bless the object into existence
105 bless $self, $class;
106  
107 return $self;
108 }
109  
110 ##################
111 # Methods
112  
113 sub start {
114  
115 # Called by GA::Obj::Desktop->start for the default (first) workspace and by
116 # GA::Obj::Desktop->useWorkspace thereafter
117 # Finds the actual size of the available workspace available to Axmud (the total workspace,
118 # minus any panels/taskbars), if possible. If not possible, artificially sets the size
119 # of the available workspace according to various IVs
120 #
121 # Expected arguments
122 # $zonemap - The default zonemap to use for this workspace
123 #
124 # Return values
125 # 'undef' on improper arguments
126 # 1 otherwise
127  
128 my ($self, $zonemap, $check) = @_;
129  
130 # Local variables
131 my ($screen, $msg);
132  
133 # Check for improper arguments
134 if (! defined $zonemap || defined $check) {
135  
136 return $axmud::CLIENT->writeImproper($self->_objClass . '->start', @_);
137 }
138  
139 if (! $axmud::CLIENT->desktopObj->gridPermitFlag) {
140  
141 # Workspace grids are disabled in general
142 $self->ivPoke('gridEnableFlag', FALSE);
143  
144 } else {
145  
146 # Test whether the workspace is too small
147 # NB Due to technical limitations, we assume that the values returned by this test
148 # apply to all workspaces, on systems where multiple workspaces are available
149 $screen = Gtk3::Gdk::Screen::get_default();
150 $self->ivPoke('currentWidth', $screen->get_width());
151 $self->ivPoke('currentHeight', $screen->get_height());
152  
153 if (
154 $self->currentWidth < $axmud::CLIENT->constWorkspaceMinWidth
155 || $self->currentHeight < $axmud::CLIENT->constWorkspaceMinHeight
156 ) {
157 # Test failed
158 $self->ivPoke('gridEnableFlag', FALSE);
159  
160 $axmud::CLIENT->writeWarning(
161 'Workspace ' . $self->number . ' smaller than minimum - disabling workspace'
162 . ' grids',
163 $self->_objClass . '->start',
164 );
165 }
166 }
167  
168 if ($self->gridEnableFlag && $self->currentWidth && $self->currentHeight) {
169  
170 # Set the default zonemap for this workspace
171 $self->ivPoke('defaultZonemap', $zonemap);
172  
173 # Test whether the workspace is too big and, if so, reduce the size of the available
174 # workspace used by workspace grids
175 if (
176 $self->currentWidth > $axmud::CLIENT->constWorkspaceMaxWidth
177 && $self->currentHeight > $axmud::CLIENT->constWorkspaceMaxHeight
178 ) {
179 $self->ivPoke('currentWidth', $axmud::CLIENT->constWorkspaceMaxWidth);
180 $self->ivPoke('currentHeight', $axmud::CLIENT->constWorkspaceMaxHeight);
181 $msg = 'Desktop width and height';
182  
183 } elsif ($self->currentWidth > $axmud::CLIENT->constWorkspaceMaxWidth) {
184  
185 $self->ivPoke('currentWidth', $axmud::CLIENT->constWorkspaceMaxWidth);
186 $msg = 'Desktop width';
187  
188 } elsif ($self->currentHeight > $axmud::CLIENT->constWorkspaceMaxHeight) {
189  
190 $self->ivPoke('currentHeight', $axmud::CLIENT->constWorkspaceMaxHeight);
191 $msg = 'Desktop height';
192 }
193  
194 if ($msg) {
195  
196 $axmud::CLIENT->writeWarning(
197 $msg . ' exceeds maximum, reducing desktop size to '
198 . $self->currentWidth . 'x' . $self->currentHeight,
199 $self->_objClass . '->setup',
200 );
201 }
202  
203 # Find the size of panels (taskbars), if possible
204 $self->findPanelSize();
205 }
206  
207 # Find the size of the window controls on this workspace, by creating two test windows in
208 # opposite corners and checking their actual size and position with what was expected
209 $self->findWinControlSize();
210  
211 # If workspace grids are enabled, set up workspace grids for each existing session (but
212 # don't set up any workspace grids if there are no sessions running yet; each new session
213 # creates its own workspace grids)
214 if ($axmud::CLIENT->sessionHash) {
215  
216 if (! $axmud::CLIENT->shareMainWinFlag) {
217  
218 # Create a shared workspace grid
219 $self->addWorkspaceGrid();
220  
221 } else {
222  
223 # Create a workspace grid for each existing session
224 foreach my $session ($axmud::CLIENT->listSessions()) {
225  
226 $self->addWorkspaceGrid($session);
227 }
228 }
229 }
230  
231 # Operation complete
232 return 1;
233 }
234  
235 sub stop {
236  
237 # Called by GA::Obj::Desktop->del_workspace and (for the default workspace) by
238 # GA::Obj::Desktop->stop
239 # Shuts down any workspace grid objects for this workspace (which closes its windows)
240 #
241 # Expected arguments
242 # (none besides $self)
243 #
244 # Return values
245 # 'undef' on improper arguments
246 # 1 otherwise
247  
248 my ($self, $check) = @_;
249  
250 # Local variables
251 my ($count, $msg);
252  
253 # Check for improper arguments
254 if (defined $check) {
255  
256 return $axmud::CLIENT->writeImproper($self->_objClass . '->stop', @_);
257 }
258  
259 # Close down workspace grid objects
260 foreach my $gridObj (sort {$a->number <=> $b->number} ($self->ivValues('gridHash'))) {
261  
262 $self->removeWorkspaceGrid($gridObj);
263 }
264  
265 # Check there are no workspace grids left (for error-detection purposes)
266 $count = $self->ivPairs('gridHash');
267 if ($count) {
268  
269 if ($count == 1) {
270 $msg = 'There was 1';
271 } else {
272 $msg = 'There were ' . $count;
273 }
274  
275 $msg .= ' un-closed workspace grids when the parent workspace closed';
276  
277 $axmud::CLIENT->writeWarning($msg, $self->_objClass . '->stop');
278 }
279  
280 # In certain (rare) circumstances (such as when Axmud starts in blind mode, and the user
281 # manually closes the dialogue window created by GA::Client->connectBlind), the spare
282 # 'main' window will still exist. If it does, close it now to prevent an error
283 if (
284 $axmud::CLIENT->mainWin
285 && $axmud::CLIENT->mainWin->owner eq $axmud::CLIENT
286 && $axmud::CLIENT->mainWin->workspaceObj eq $self
287 ) {
288 $axmud::CLIENT->mainWin->winDestroy();
289 }
290  
291 return 1;
292 }
293  
294 # General functions
295  
296 sub addWorkspaceGrid {
297  
298 # Called by $self->start or GA::Session->setMainWin
299 # Adds a workspace grid object to this workspace, and sets it up (if allowed)
300 #
301 # Expected arguments
302 # (none besides $self)
303 #
304 # Optional arguments
305 # $session - (GA::Client->shareMainWinFlag = TRUE) The GA::Session object which
306 # controls this workspace grid
307 # - (GA::Client->shareMainWinFlag = FALSE) 'undef' (the grid is shared between
308 # all sessions)
309 # $zonemap - The zonemap to use to set up the workspace grid object. If 'undef',
310 # $self->defaultZonemap is used
311 #
312 # Return values
313 # 'undef' on improper arguments, if workspace grids can't be created in general or if
314 # the operation fails
315 # Otherwise returns workspace grid object added
316  
317 my ($self, $session, $zonemap, $check) = @_;
318  
319 # Local variables
320 my ($gridObj, $result);
321  
322 # Check for improper arguments
323 if (defined $check) {
324  
325 return $axmud::CLIENT->writeImproper($self->_objClass . '->addWorkspaceGrid', @_);
326 }
327  
328 if (
329 # Workspace grids are disactivated generally (at the user's request)
330 ! $axmud::CLIENT->activateGridFlag
331 # Axmud cannot create workspace grids, because the desktop is too small, etc
332 || ! $axmud::CLIENT->desktopObj->gridPermitFlag
333 # # Workspace grids are disabled on this workspace
334 || ! $self->gridEnableFlag
335 ) {
336 # Workspace grids are disabled on this workspace
337 return undef;
338 }
339  
340 # Add the workspace grid object
341 $gridObj = $axmud::CLIENT->desktopObj->add_grid($self, $session);
342 if (! $gridObj) {
343  
344 # Operation failed
345 return undef;
346  
347 } else {
348  
349 # Also update our own IV
350 $self->ivAdd('gridHash', $gridObj->number, $gridObj);
351 }
352  
353 # Set up the new workspace grid object
354 if ($zonemap) {
355 $result = $gridObj->start($zonemap);
356 } else {
357 $result = $gridObj->start($self->defaultZonemap);
358 }
359  
360 if (! $result) {
361  
362 # Setup failed; discard this workspace grid
363 $axmud::CLIENT->desktopObj->del_grid($gridObj);
364 $self->ivDelete('gridHash', $gridObj->number);
365  
366 return undef;
367  
368 } else {
369  
370 return $gridObj;
371 }
372 }
373  
374 sub removeWorkspaceGrid {
375  
376 # Called by $self->stop, ->disableWorkspaceGrids and
377 # GA::Obj::Desktop->removeSessionWorkspaceGrids
378 # Removes a workspace grid object from this workspace (which closes all its windows), and
379 # informs GA::Obj::Desktop
380 #
381 # Expected arguments
382 # $gridObj - The GA::Obj::WorkspaceGrid to remove
383 #
384 # Optional arguments
385 # $session - If defined, that session's 'main' window is disengaged (removed from its
386 # workspace grid, but not destroyed)
387 #
388 # Return values
389 # 'undef' on improper arguments or if the operation fails
390 # 1 otherwise
391  
392 my ($self, $gridObj, $session, $check) = @_;
393  
394 # Check for improper arguments
395 if (defined $check) {
396  
397 return $axmud::CLIENT->writeImproper($self->_objClass . '->removeWorkspaceGrid', @_);
398 }
399  
400 # Shut down the workspace grid object
401 if (! $axmud::CLIENT->desktopObj->del_grid($gridObj, $session)) {
402  
403 return undef;
404  
405 } else {
406  
407 # Update our own IVs
408 $self->ivDelete('gridHash', $gridObj->number);
409  
410 return 1;
411 }
412 }
413  
414 sub enableWorkspaceGrids {
415  
416 # Called by GA::Obj::Desktop->activateWorkspaceGrids and GA::Cmd::ActivateGrid->do
417 # Enables workspace grids on this workspace. Creates workspace grids for every session (or a
418 # single workspace grid, if sessions don't share a 'main' window), places existing
419 # windows on the correct grid and updates IVs
420 # If grids are already enabled on this workspace, does nothing
421 #
422 # Expected arguments
423 # (none besides $self)
424 #
425 # Optional arguments
426 # $zonemap - If specified, the name of the zonemap (matches a key in
427 # GA::Client->zonemapHash) to be used as the default zonemap on this
428 # workspace (and which is applied to every new workspace grid)
429 #
430 # Return values
431 # 'undef' on improper arguments
432 # 1 otherwise
433  
434 my ($self, $zonemap, $check) = @_;
435  
436 # Local variables
437 my (
438 $zonemapObj,
439 @gridList, @winList,
440 );
441  
442 # Check for improper arguments
443 if (defined $check) {
444  
445 return $axmud::CLIENT->writeImproper($self->_objClass . '->enableWorkspaceGrids', @_);
446 }
447  
448 if (! $self->gridEnableFlag) {
449  
450 # Update IVs
451 $self->ivPoke('gridEnableFlag', TRUE);
452  
453 # If a zonemap was specified, it's the default zonemap for this workspace
454 if (defined $zonemap && $axmud::CLIENT->ivExists('zonemapHash', $zonemap)) {
455  
456 $self->ivPoke('defaultZonemap', $zonemap);
457 }
458  
459 # Get a sorted list of 'grid' window objects
460 @winList = sort {$a->number <=> $b->number}
461 ($axmud::CLIENT->desktopObj->ivValues('gridWinHash'));
462  
463 # Create workspace grids
464 if ($axmud::CLIENT->shareMainWinFlag) {
465  
466 # Create a grid for every session
467 foreach my $session ($axmud::CLIENT->listSessions()) {
468  
469 my $gridObj = $self->addWorkspaceGrid($session, $zonemap);
470 if ($gridObj) {
471  
472 push (@gridList, $gridObj);
473  
474 # All 'grid' windows on this workspace controlled by the session should be
475 # placed onto this grid. Start by updating the workspace object's IV
476 foreach my $winObj (@winList) {
477  
478 if (
479 $winObj->workspaceObj eq $self
480 && $winObj->session
481 && $winObj->session eq $session
482 ) {
483 $gridObj->add_gridWin($winObj);
484 }
485 }
486 }
487 }
488  
489 } else {
490  
491 # Create a single grid shared by all sessions
492 my $gridObj = $self->addWorkspaceGrid(undef, $zonemap);
493 if ($gridObj) {
494  
495 push (@gridList, $gridObj);
496  
497 # All 'grid' windows on this workspace controlled by the session should be
498 # placed onto this grid. Start by updating the workspace object's IV
499 foreach my $winObj (@winList) {
500  
501 if ($winObj->workspaceObj eq $self) {
502  
503 $gridObj->add_gridWin($winObj);
504 }
505 }
506 }
507 }
508  
509 # Get the default zonemap object for this workspace
510 $zonemapObj = $axmud::CLIENT->ivShow('zonemapHash', $self->defaultZonemap);
511  
512 # For any workspace grid object assigned a window, we can use the normal reset code to
513 # have the windows places on the grid, as if they had been opened there
514 foreach my $gridObj (@gridList) {
515  
516 if ($gridObj->gridWinHash) {
517  
518 $gridObj->applyZonemap($zonemapObj);
519 }
520 }
521 }
522  
523 return 1;
524 }
525  
526 sub disableWorkspaceGrids {
527  
528 # Called by GA::Obj::Desktop->disactivateWorkspaceGrids and GA::Cmd::DisactivateGrid->do
529 # Disables workspace grids on this workspace and updates IVs. If grids are already
530 # disabled on this workspace, does nothing
531 #
532 # Expected arguments
533 # (none besides $self)
534 #
535 # Return values
536 # 'undef' on improper arguments
537 # 1 otherwise
538  
539 my ($self, $check) = @_;
540  
541 # Check for improper arguments
542 if (defined $check) {
543  
544 return $axmud::CLIENT->writeImproper($self->_objClass . '->disableWorkspaceGrids', @_);
545 }
546  
547 if ($self->gridEnableFlag) {
548  
549 foreach my $gridObj ($self->ivValues('gridHash')) {
550  
551 my @list;
552  
553 # Import the grid's windows and reset its IVs, so that the windows aren't closed
554 # when the grid object is removed
555 @list = $gridObj->ivValues('gridWinHash');
556 $gridObj->reset_gridWinHash();
557  
558 # Remove the workspace grid
559 $self->removeWorkspaceGrid($gridObj);
560  
561 # Update the window object's IVs
562 foreach my $winObj (@list) {
563  
564 $winObj->set_workspaceGridObj();
565 $winObj->set_areaObj();
566 }
567 }
568  
569 # Update IVs
570 $self->ivPoke('gridEnableFlag', FALSE);
571 }
572  
573 return 1;
574 }
575  
576 sub findWorkspaceGrid {
577  
578 # Can be called by anything
579 # If GA::Client->shareMainWinFlag is TRUE, each session controls a different workspace
580 # grid in this workspace. Finds the workspace grid object controlled by a specified
581 # session, and returns it
582 # If GA::Client->shareMainWinFlag is FALSE, all sessions share a single workspace grid on
583 # each workspace. Returns the shared workspace grid object
584 #
585 # Expected arguments
586 # $session - The GA::Session which controls a workspace grid
587 #
588 # Return values
589 # 'undef' on improper arguments or if there are no matching workspace grid objects
590 # Otherwise returns the first matching GA::Obj::WorkspaceGrid object
591  
592 my ($self, $session, $check) = @_;
593  
594 # Local variables
595 my @list;
596  
597 # Check for improper arguments
598 if (defined $check) {
599  
600 return $axmud::CLIENT->writeImproper($self->_objClass . '->findWorkspaceGrid', @_);
601 }
602  
603 # Sort workspace grid objects in the order in which they were created (there shouldn't be
604 # more than one workspace grid object controlled by a particular session, nor should
605 # there be more than one workspace grid object controlled by no sessions, but we'll
606 # return the earliest-created one anyway)
607 @list = sort {$a->number <=> $b->number} ($self->ivValues('gridHash'));
608  
609 if (! $axmud::CLIENT->shareMainWinFlag) {
610  
611 # All sessions share a workspace grid
612 return $list[0];
613  
614 } else {
615  
616 # Find a workspace grid object controlled a GA::Session, if one was specified
617 foreach my $obj (@list) {
618  
619 if (! defined $obj->owner || $obj->owner eq $session) {
620  
621 return $obj;
622 }
623 }
624 }
625  
626 # No matching workspace grid found
627 return undef;
628 }
629  
630 sub findPanelSize {
631  
632 # Called by $self->start or GA::Cmd::TestPanel->do
633 # Tries to find the sizes of any panels (taskbars) on this workspace. (It's rather unlikely
634 # that different workspaces will use different panel sizes, but Axmud checks panel sizes
635 # on every workspace it uses anyway)
636 # If the test succeeds, uses those sizes to reduce the size of the available workspace by
637 # setting $self->panelLeftSize, etc. (If GA::Client->customPanelLeftSize (etc) are set,
638 # those values take precedence over the test values)
639 # If the test fails and $self->panelLeftSize (etc) have never been set for this workspace,
640 # sets them using GA::Client->customPanelLeftSize or ->constPanelLeftSize (etc)
641 #
642 # Returns the actual sizes detected - not the new values of $self->panelLeftSize, etc
643 #
644 # Expected arguments
645 # (none besides $self)
646 #
647 # Optional arguments
648 # $timeout - A timeout for the test, in seconds. If not defined, a default timeout is
649 # used
650 #
651 # Return values
652 # An empty list on improper arguments or if the test fails
653 # Otherwise returns the detected sizes of panels/taskbars detected (which might not match
654 # the values stored in $self->panelLeftSize, etc), a list in the form
655 # (left, right, top, bottom)
656  
657 my ($self, $timeout, $check) = @_;
658  
659 # Local variables
660 my (
661 $xPos, $yPos, $width, $height, $left, $right, $top, $bottom,
662 @emptyList,
663 );
664  
665 # Check for improper arguments
666 if (defined $check) {
667  
668 $axmud::CLIENT->writeImproper($self->_objClass . '->findPanelSize', @_);
669 return @emptyList;
670 }
671  
672 # Try to detect panel sizes
673 ($xPos, $yPos, $width, $height) = $self->testPanelSize($timeout);
674 # If the test succeeded, perform some sanity checking (just in case)
675 if (defined $xPos && ($xPos < 0 || $yPos < 0 || $width < 0 || $height < 0)) {
676  
677 # Sanity check fails, therefore the test fails
678 $xPos = undef;
679 }
680  
681 # Respond to the test
682 if (! defined $xPos) {
683  
684 # Test failed
685  
686 # If panel sizes have never been set for this workspace, set them now
687 if (! defined $self->controlsLeftSize) {
688  
689 if (defined $axmud::CLIENT->customPanelLeftSize) {
690 $self->ivPoke('panelLeftSize', $axmud::CLIENT->customPanelLeftSize);
691 } else {
692 $self->ivPoke('panelLeftSize', $axmud::CLIENT->constPanelLeftSize);
693 }
694  
695 if (defined $axmud::CLIENT->customPanelRightSize) {
696 $self->ivPoke('panelRightSize', $axmud::CLIENT->customPanelRightSize);
697 } else {
698 $self->ivPoke('panelRightSize', $axmud::CLIENT->constPanelRightSize);
699 }
700  
701 if (defined $axmud::CLIENT->customPanelTopSize) {
702 $self->ivPoke('panelTopSize', $axmud::CLIENT->customPanelTopSize);
703 } else {
704 $self->ivPoke('panelTopSize', $axmud::CLIENT->constPanelTopSize);
705 }
706  
707 if (defined $axmud::CLIENT->customPanelBottomSize) {
708 $self->ivPoke('panelBottomSize', $axmud::CLIENT->customPanelBottomSize);
709 } else {
710 $self->ivPoke('panelBottomSize', $axmud::CLIENT->constPanelBottomSize);
711 }
712 }
713  
714 return @emptyList;
715  
716 } else {
717  
718 # Test successful. Work out panel sizes
719 $left = $xPos;
720 $right = $self->currentWidth - $width - $left;
721 $top = $yPos;
722 $bottom = $self->currentHeight - $height - $top;
723  
724 # Store the detected sizes unless GA::Client->customPanelLeftSize (etc) are set, as
725 # those values take precedence
726 if (defined $axmud::CLIENT->customPanelLeftSize) {
727 $self->ivPoke('panelLeftSize', $axmud::CLIENT->customPanelLeftSize);
728 } else {
729 $self->ivPoke('panelLeftSize', $left);
730 }
731  
732 if (defined $axmud::CLIENT->customPanelRightSize) {
733 $self->ivPoke('panelRightSize', $axmud::CLIENT->customPanelRightSize);
734 } else {
735 $self->ivPoke('panelRightSize', $right);
736 }
737  
738 if (defined $axmud::CLIENT->customPanelTopSize) {
739 $self->ivPoke('panelTopSize', $axmud::CLIENT->customPanelTopSize);
740 } else {
741 $self->ivPoke('panelTopSize', $top);
742 }
743  
744 if (defined $axmud::CLIENT->customPanelBottomSize) {
745 $self->ivPoke('panelBottomSize', $axmud::CLIENT->customPanelBottomSize);
746 } else {
747 $self->ivPoke('panelBottomSize', $bottom);
748 }
749  
750 # Return the results of the test (not the size of the available workspace)
751 return ($left, $right, $top, $bottom);
752 }
753 }
754  
755 sub testPanelSize {
756  
757 # Called by $self->findPanelSize
758 # Creates a maximised test window and returns its position and size
759 #
760 # Expected arguments
761 # (none besides $self)
762 #
763 # Optional arguments
764 # $timeout - A timeout, in seconds. If not defined, a default timeout is used
765 #
766 # Return values
767 # An empty list on improper arguments or if the test fails
768 # Otherwise returns the detected sizes of panels/taskbars detected, a list in the form
769 # (left, right, top, bottom)
770  
771 my ($self, $timeout, $check) = @_;
772  
773 # Local variables
774 my (
775 $testWinName, $initSize, $startTime, $checkTime, $regionX, $regionY, $regionWidth,
776 $regionHeight,
777 @emptyList,
778 );
779  
780 # Check for improper arguments
781 if (defined $check) {
782  
783 $axmud::CLIENT->writeImproper($self->_objClass . '->testPanelSize', @_);
784 return @emptyList;
785 }
786  
787 # Prepare the test window's title bar
788 $testWinName = $axmud::SCRIPT . ' panel test';
789 # The test window's size can be measured once it is no longer its original size
790 $initSize = 100;
791 # If no timeout was specified, use the default one
792 if (! defined $timeout) {
793  
794 $timeout = 1;
795 }
796  
797 # Create a test window with maximum opacity, so the user doesn't see it
798 my $testWin = Gtk3::Window->new('toplevel');
799 $testWin->set_title($testWinName);
800 $testWin->set_border_width(0);
801 $testWin->set_size_request($initSize, $initSize);
802 $testWin->set_decorated(FALSE);
803 $testWin->set_opacity(0);
804 $testWin->set_icon_list($axmud::CLIENT->desktopObj->{dialogueWinIconList});
805 $testWin->set_skip_taskbar_hint(TRUE);
806 $testWin->set_skip_pager_hint(TRUE);
807 $testWin->show_all();
808  
809 # If using X11::WMCtrl, we can move the test window to the correct workspace (if not, there
810 # is only one workspace in use, anyway)
811 if ($axmud::CLIENT->desktopObj->wmCtrlObj) {
812  
813 $axmud::CLIENT->desktopObj->wmCtrlObj->move_to($testWinName, $self->systemNum);
814 }
815  
816 $testWin->maximize();
817 $testWin->show_all();
818  
819 # Initialise the timeout (a time in seconds)
820 $startTime = $axmud::CLIENT->getTime();
821  
822 # The window will not become maximised immediately, so we keep looking on a loop until it
823 # does on until the timeout expires
824 do {
825  
826 $axmud::CLIENT->desktopObj->updateWidgets($self->_objClass . '->testWinControls');
827 ($regionX, $regionY, $regionWidth, $regionHeight)
828 = $self->getWinGeometry($testWin->get_window());
829  
830 $checkTime = $axmud::CLIENT->getTime();
831  
832 } until (
833 ($regionWidth != $initSize && $regionHeight != $initSize)
834 || $checkTime > ($startTime + $timeout)
835 );
836  
837 $testWin->destroy();
838  
839 if ($regionWidth == $initSize || $regionHeight == $initSize) {
840  
841 # Test failed
842 return @emptyList;
843  
844 } else {
845  
846 # Test successful
847 return ($regionX, $regionY, $regionWidth, $regionHeight);
848 }
849 }
850  
851 sub findWinControlSize {
852  
853 # Called by $self->start or GA::Cmd::TestWindowControls->do
854 # Tries to find the sizes of window controls, the edges around windows added by desktop's
855 # window manager. (It's extremely unlikely that different workspaces will use different
856 # window controls, but Axmud checks windows controls on every workspace it uses anyway)
857 # Create two test windows in opposite corners of the workspace and compare their actual size
858 # and position with what was expected
859 # If the test succeeds, sets $self->controlsLeftSize, etc. (If
860 # GA::Client->customControlsLeftSize (etc) are set, those values take precedence over
861 # the test values)
862 # If the test fails and $self->controlsLeftSize (etc) have never been set for this
863 # workspace, sets them using GA::Client->customControlsLeftSize or
864 # ->constControlsLeftSize (etc)
865 #
866 # Expected arguments
867 # (none besides $self)
868 #
869 # Return values
870 # An empty list on improper arguments or if either test fails
871 # Otherwise returns the detected sizes of window controls (which might not match the
872 # values stored in $self->controlsLeftSize, etc), a list in the form
873 # (left, right, top, bottom)
874  
875 my ($self, $check) = @_;
876  
877 # Local variables
878 my (
879 $left, $right, $top, $bottom,
880 @emptyList,
881 );
882  
883 # Check for improper arguments
884 if (defined $check) {
885  
886 $axmud::CLIENT->writeImproper($self->_objClass . '->findWinControlSize', @_);
887 return @emptyList;
888 }
889  
890 # Try to detect window controls sizes. Create first test window near the top-left corner of
891 # this workspace
892 ($left, $top) = $self->testWinControls(FALSE);
893 # Create second test window near the bottom-right corner
894 ($right, $bottom) = $self->testWinControls(TRUE);
895  
896 # If the test succeeded, perform some sanity checking (just in case)
897 if (
898 defined $left
899 && defined $right
900 && (
901 $left < 0 || $left > 200
902 || $top < 0 || $top > 200
903 || $right < 0 || $right > 200
904 || $bottom < 0 || $bottom > 200
905 )
906 ) {
907 # Sanity check fails, therefore the test fails
908 $left = undef;
909 }
910  
911 # Respond to the test
912 if (! defined $left || ! defined $right) {
913  
914 # Test failed
915  
916 # If window controls sizes have never been set for this workspace, set them now
917 if (! defined $self->controlsLeftSize) {
918  
919 if (defined $axmud::CLIENT->customControlsLeftSize) {
920 $self->ivPoke('controlsLeftSize', $axmud::CLIENT->customControlsLeftSize);
921 } else {
922 $self->ivPoke('controlsLeftSize', $axmud::CLIENT->constControlsLeftSize);
923 }
924  
925 if (defined $axmud::CLIENT->customControlsRightSize) {
926 $self->ivPoke('controlsRightSize', $axmud::CLIENT->customControlsRightSize);
927 } else {
928 $self->ivPoke('controlsRightSize', $axmud::CLIENT->constControlsRightSize);
929 }
930  
931 if (defined $axmud::CLIENT->customControlsTopSize) {
932 $self->ivPoke('controlsTopSize', $axmud::CLIENT->customControlsTopSize);
933 } else {
934 $self->ivPoke('controlsTopSize', $axmud::CLIENT->constControlsTopSize);
935 }
936  
937 if (defined $axmud::CLIENT->customControlsBottomSize) {
938 $self->ivPoke('controlsBottomSize', $axmud::CLIENT->customControlsBottomSize);
939 } else {
940 $self->ivPoke('controlsBottomSize', $axmud::CLIENT->constControlsBottomSize);
941 }
942 }
943  
944 return @emptyList;
945  
946 } else {
947  
948 # Test successful
949  
950 # DEBUG v2.0
951 # MS Windows adds extra pixels to the window width and height (for unknown reasons);
952 # compensate for that, if required
953 if ($^O eq 'MSWin32' && $axmud::CLIENT->mswinWinPosnTweakFlag) {
954  
955 # N.B. The $top value is OK
956 $left = 0;
957 $right = 0;
958 $bottom = 0;
959 }
960  
961 # Store the detected sizes unless GA::Client->customControlsLeftSize (etc) are set, as
962 # those values take precedence
963 if (defined $axmud::CLIENT->customControlsLeftSize) {
964 $self->ivPoke('controlsLeftSize', $axmud::CLIENT->customControlsLeftSize);
965 } else {
966 $self->ivPoke('controlsLeftSize', $left);
967 }
968  
969 if (defined $axmud::CLIENT->customControlsRightSize) {
970 $self->ivPoke('controlsRightSize', $axmud::CLIENT->customControlsRightSize);
971 } else {
972 $self->ivPoke('controlsRightSize', $right);
973 }
974  
975 if (defined $axmud::CLIENT->customControlsTopSize) {
976 $self->ivPoke('controlsTopSize', $axmud::CLIENT->customControlsTopSize);
977 } else {
978 $self->ivPoke('controlsTopSize', $top);
979 }
980  
981 if (defined $axmud::CLIENT->customControlsBottomSize) {
982 $self->ivPoke('controlsBottomSize', $axmud::CLIENT->customControlsBottomSize);
983 } else {
984 $self->ivPoke('controlsBottomSize', $bottom);
985 }
986  
987 # Return the results of the test (not the stored controls sizes)
988 return ($left, $right, $top, $bottom);
989 }
990 }
991  
992 sub testWinControls {
993  
994 # Called by $self->findWinControlSize
995 # Creates a test window in one of two opposite corners of this workspace and, by testing its
996 # actual coordinates against its expected coordinates, finds out the size of two of the
997 # window controls used by the current desktop theme
998 #
999 # Expected arguments
1000 # $gravityFlag - FALSE to use top-left corner, TRUE to use bottom-right corner
1001 #
1002 # Return values
1003 # Returns an empty list on improper arguments or if the window's position on the desktop
1004 # can't be found
1005 # Otherwise returns a list in the form
1006 # ($gravityFlag = FALSE) - The list (size_of_left_control, size_of_top_control)
1007 # ($gravityFlag = TRUE) - The list (size_of_bottom_control, size_of_right_control)
1008  
1009 my ($self, $gravityFlag, $check) = @_;
1010  
1011 # Local variables
1012 my (
1013 $testWinName, $testWinSize, $testWinDistance, $checkTime, $matchFlag, $regionX,
1014 $regionY, $regionWidth, $regionHeight, $clientX, $clientY,
1015 @emptyList, @nameList,
1016 );
1017  
1018 # Check for improper arguments
1019 if (! defined $gravityFlag || defined $check) {
1020  
1021 $axmud::CLIENT->writeImproper($self->_objClass . '->testWinControls', @_);
1022 return @emptyList;
1023 }
1024  
1025 # This test requires that window panels have been set in an earlier call to
1026 # $self->findPanelSize; if they haven't, then this test fails
1027 if (
1028 ! defined $self->panelLeftSize
1029 || ! defined $self->panelRightSize
1030 || ! defined $self->panelTopSize
1031 || ! defined $self->panelBottomSize
1032 ) {
1033 return @emptyList;
1034 }
1035  
1036 # Prepare the test window's title bar
1037 if (! $gravityFlag) {
1038 $testWinName = $axmud::SCRIPT . '_test_1';
1039 } else {
1040 $testWinName = $axmud::SCRIPT . '_test_2';
1041 }
1042  
1043 # Prepare the width and height of the test window in pixels
1044 $testWinSize = 100;
1045 # Set the distance from the corner in pixels
1046 $testWinDistance = 100;
1047  
1048 # Create the test window
1049 my $testWin = Gtk3::Window->new('toplevel');
1050 $testWin->set_title($testWinName);
1051 $testWin->set_border_width(0);
1052 $testWin->set_size_request($testWinSize, $testWinSize);
1053 $testWin->set_decorated(TRUE);
1054 $testWin->set_opacity(0);
1055 $testWin->set_skip_taskbar_hint(TRUE);
1056 $testWin->set_skip_pager_hint(TRUE);
1057  
1058 if (! $gravityFlag) {
1059  
1060 # Position the test window near the top-left corner
1061 $testWin->move(
1062 ($self->panelLeftSize + $testWinDistance),
1063 ($self->panelTopSize + $testWinDistance),
1064 );
1065  
1066 } else {
1067  
1068 # Position the test window near the bottom-right corner
1069 $testWin->move(
1070 ($self->currentWidth - $self->panelRightSize - $testWinDistance - $testWinSize),
1071 ($self->currentHeight - $self->panelBottomSize - $testWinDistance - $testWinSize),
1072 );
1073 }
1074  
1075 # Set the window's gravity
1076 if (! $gravityFlag) {
1077 $testWin->set_gravity('GDK_GRAVITY_NORTH_WEST');
1078 } else {
1079 $testWin->set_gravity('GDK_GRAVITY_SOUTH_EAST');
1080 }
1081  
1082 # Use standard 'dialogue' window icons
1083 $testWin->set_icon_list($axmud::CLIENT->desktopObj->{dialogueWinIconList});
1084  
1085 # Make the window visible, briefly
1086 $testWin->show_all();
1087 $axmud::CLIENT->desktopObj->updateWidgets($self->_objClass . '->testWinControls');
1088  
1089 # If using X11::WMCtrl, we can move the test window to the correct workspace (if not, there
1090 # is only one workspace in use, anyway)
1091 if ($axmud::CLIENT->desktopObj->wmCtrlObj) {
1092  
1093 # Very rarely, the call to X11::WMCtrl->get_windows() produces an unexplainable error.
1094 # This nudge seems to prevent it from happening
1095 $axmud::CLIENT->desktopObj->wmCtrlObj->get_window_manager();
1096  
1097 # In some cases, X11::WMCtrl doesn't know about the test window (despite the call to
1098 # ->updateWidgets just above). Wait for up to a second and, if X11::WMCtrl still
1099 # hasn't spotted the new window, regard the test as a failure
1100 $checkTime = $axmud::CLIENT->getTime() + 1;
1101 do {
1102  
1103 OUTER: foreach my $hashRef ($axmud::CLIENT->desktopObj->wmCtrlObj->get_windows()) {
1104  
1105 if ($$hashRef{'title'} eq $testWinName) {
1106  
1107 $matchFlag = TRUE;
1108 last OUTER;
1109 }
1110 }
1111  
1112 } until ($matchFlag || $axmud::CLIENT->getTime() > $checkTime);
1113  
1114 if (! $matchFlag) {
1115  
1116 # Test failed
1117 return @emptyList;
1118  
1119 } else {
1120  
1121 # Can move the window to its workspace without triggering an error (providing we do
1122 # a quick ->updateWidgets first)
1123 $axmud::CLIENT->desktopObj->updateWidgets($self->_objClass . '->testWinControls');
1124 $axmud::CLIENT->desktopObj->wmCtrlObj->move_to($testWinName, $self->systemNum);
1125 }
1126 }
1127  
1128 # Get the window's actual position on the desktop
1129 ($regionX, $regionY, $regionWidth, $regionHeight, $clientX, $clientY)
1130 = $self->getWinGeometry($testWin->get_window());
1131  
1132 # Destroy the test window, now that we have all the data we want
1133 $testWin->destroy();
1134  
1135 # Return the window controls sizes as a list
1136 if (! $gravityFlag) {
1137  
1138 # Return size of the left and top window controls
1139 return (
1140 ($clientX - ($self->panelLeftSize + $testWinDistance)),
1141 ($clientY - ($self->panelTopSize + $testWinDistance)),
1142 )
1143  
1144 } else {
1145  
1146 # Return size of the right and bottom window controls
1147 return (
1148 (
1149 $self->currentWidth - $self->panelRightSize - $testWinDistance - $testWinSize
1150 - $clientX
1151 ),
1152 (
1153 $self->currentHeight - $self->panelBottomSize - $testWinDistance - $testWinSize
1154 - $clientY
1155 ),
1156 )
1157 }
1158 }
1159  
1160 sub getWinGeometry {
1161  
1162 # Can be called by any function, e.g. by $self->testWinControls
1163 # Gets the actual size and position of a Gdk::Window
1164 #
1165 # Expected arguments
1166 # $gdkWin - The Gdk::Window to use
1167 #
1168 # Return values
1169 # An empty list on improper arguments or if the test fails
1170 # Otherwise a list in groups of 4, showing the size and position of the window region (the
1171 # window's visible size and position), and then the size and position of the window's
1172 # client area (i.e. not taking into account the window's title bar):
1173 # (
1174 # $regionX, $regionY, $regionWidth, $regionHeight,
1175 # $clientX, $clientY, $clientWidth, $clientHeight,
1176 # )
1177  
1178 my ($self, $gdkWin, $check) = @_;
1179  
1180 # Local variables
1181 my (
1182 $regionX, $regionY, $clientX, $clientY, $clientWidth, $clientHeight,
1183 @emptyList,
1184 );
1185  
1186 # Check for improper arguments
1187 if (! defined $gdkWin || defined $check) {
1188  
1189 $axmud::CLIENT->writeImproper($self->_objClass . '->getWinGeometry', @_);
1190 return @emptyList;
1191 }
1192  
1193 # Make sure all drawing operations are complete
1194 $axmud::CLIENT->desktopObj->updateWidgets($self->_objClass . '->getWinGeometry');
1195  
1196 # Get the position of the visible window
1197 ($regionX, $regionY) = $gdkWin->get_root_origin();
1198 # Get the position of the window's client area
1199 ($clientX, $clientY) = $gdkWin->get_origin();
1200 # Get the size of the window's client area; the size of the window region can be calculated
1201 # by adding the ttitle bar
1202 $clientWidth = $gdkWin->get_width();
1203 $clientHeight = $gdkWin->get_height();
1204  
1205 # Return the results
1206 return (
1207 $regionX,
1208 $regionY,
1209 $clientWidth + ($clientX - $regionX),
1210 $clientHeight + ($clientY - $regionY),
1211 $clientX,
1212 $clientY,
1213 $clientWidth,
1214 $clientHeight,
1215 );
1216 }
1217  
1218 sub matchWinList {
1219  
1220 # Called GA::GrabWindowCmd->do or any other code
1221 # Compares a list of patterns - e.g. ('Notepad', 'Firefox') - against a list of windows on
1222 # this workspace. Returns a list of internal IDs for the matching windows
1223 # The patterns are treated as regexes (so 'Notepad' and '^Note' are both acceptable). The
1224 # pattern match is case-insensitive
1225 #
1226 # NB This function does nothing if X11::WMCtrl is not available
1227 #
1228 # Expected arguments
1229 # $number - The number of windows to match. 1 - return only the first matching
1230 # window; 7 - return only the first 7 matching windows, 0 - return all
1231 # matching windows
1232 #
1233 # Optional arguments
1234 # @patternList - A list of patterns matching one or more window titles. If an empty
1235 # list, no matching occurs
1236 #
1237 # Return values
1238 # An empty list on improper arguments
1239 # Otherwise returns a list (which may be an empty list) in groups of 2 for each matching
1240 # window, in the form
1241 # (window_title, window_internal_id)
1242  
1243 my ($self, $number, @patternList) = @_;
1244  
1245 # Local variables
1246 my (
1247 $count,
1248 @emptyList, @returnList,
1249 );
1250  
1251 # Check for improper arguments
1252 if (! defined $number) {
1253  
1254 return $axmud::CLIENT->writeImproper($self->_objClass . '->matchWinList', @_);
1255 }
1256  
1257 # Can't do anything without X11::WMCtrl
1258 if (! $axmud::CLIENT->desktopObj->wmCtrlObj) {
1259  
1260 return @emptyList;
1261 }
1262  
1263 # Cycle through the list of windows on all workspaces, ignoring any windows that aren't on
1264 # this workspace
1265 $count = 0;
1266 OUTER: foreach my $hashRef ($axmud::CLIENT->desktopObj->wmCtrlObj->get_windows()) {
1267  
1268 my $title;
1269  
1270 if ($$hashRef{'workspace'} == $self->systemNum) {
1271  
1272 # X11::WMCtrl handles a string containing both the window's title and the hostname
1273 # of the X client drawing the window; however, when that hostname is 'N/A',
1274 # X11::WMCtrl is unable to remove it, so we'll have to do that ourselves
1275 $title = $$hashRef{'title'};
1276 $title =~ s/^\sN\/A\s//;
1277  
1278 INNER: foreach my $pattern (@patternList) {
1279  
1280 if ($title =~ m/$pattern/i) {
1281  
1282 # A matching window was found
1283 push (@returnList, $title, $$hashRef{'id'});
1284 $count++;
1285  
1286 # Do we have enough matching windows now?
1287 if ($number > 0 && $count >= $number) {
1288  
1289 # We have enough matching windows
1290 return @returnList;
1291  
1292 } else {
1293  
1294 # Look for the next matching window
1295 next OUTER;
1296 }
1297 }
1298 }
1299 }
1300 }
1301  
1302 # Operation complete
1303 return @returnList;
1304 }
1305  
1306 sub moveResizeWin {
1307  
1308 # Can be called by any function (but not used by $self->testWinControls, which calls
1309 # Gtk3::Window->move and ->resize directly)
1310 # Moves a window to a specific position on the workspace and resizes it
1311 # Calls to this function should usually be followed by a call to
1312 # GA::Obj::Zone->restackWin() and/or GA::Generic::Win->restoreFocus
1313 #
1314 # Expected arguments
1315 # $winObj - The window object (anything inheriting from GA::Generic::Win) whose
1316 # window should be moved and/or resized
1317 #
1318 # Optional arguments
1319 # $xPosPixels, $yPosPixels
1320 # - The new position of the window (both set to 'undef' if the position isn't
1321 # to be changed)
1322 # $widthPixels, $heightPixels
1323 # - The new size of the window (both set to 'undef' if the size isn't to be
1324 # changed)
1325 #
1326 # Notes
1327 # The following combinations of arguments lists are acceptable:
1328 # ($winObj, $xPosPixels, $yPosPixels, $widthPixels, $heightPixels)
1329 # ($winObj, $xPosPixels, $yPosPixels, undef, undef)
1330 # ($winObj, undef, undef, $widthPixels, $heightPixels)
1331 # ($winObj, undef, undef, undef, undef)
1332 # Any other combination, for example the following one, will cause an error
1333 # ($winObj, $xPosPixels, $yPosPixels, undef, $heightPixels)
1334 #
1335 # Return values
1336 # 'undef' on improper arguments or if the window can't be moved (because a non-external
1337 # window object doesn't have its ->winWidget set)
1338 # 1 otherwise
1339  
1340 my ($self, $winObj, $xPosPixels, $yPosPixels, $widthPixels, $heightPixels, $check) = @_;
1341  
1342 # Local variables
1343 my $title;
1344  
1345 # Check for improper arguments
1346 if (
1347 ! defined $winObj
1348 || (defined $xPosPixels && ! defined $yPosPixels)
1349 || (! defined $xPosPixels && defined $yPosPixels)
1350 || (defined $widthPixels && ! defined $heightPixels)
1351 || (! defined $widthPixels && defined $heightPixels)
1352 || defined $check
1353 ) {
1354 return $axmud::CLIENT->writeImproper($self->_objClass . '->moveResizeWin', @_);
1355 }
1356  
1357 if ($winObj->winType ne 'external') {
1358  
1359 # Move the window to the correct workspace, if possible
1360 if ($axmud::CLIENT->desktopObj->wmCtrlObj) {
1361  
1362 # Give the window a unique name, so WMCtrl can find it
1363 my $title = $winObj->winWidget->get_title();
1364 $winObj->winWidget->set_title($axmud::SCRIPT . int(rand(1_000_000_000)));
1365  
1366 # Move the window
1367 $axmud::CLIENT->desktopObj->wmCtrlObj->move_to(
1368 $winObj->winWidget->get_title(),
1369 $self->systemNum,
1370 );
1371  
1372 # Restore the original title
1373 $winObj->winWidget->set_title($title);
1374 }
1375  
1376 # Resize the window, if that was specified
1377 if (defined $widthPixels) {
1378  
1379 $winObj->winWidget->resize($widthPixels, $heightPixels);
1380 }
1381  
1382 # Move the window, if that was specified
1383 # $winObj->winWidget is occasionally set to 'undef' just before this line. It's a very
1384 # difficult error to reproduce, so I'm not sure what the cause is
1385 if ($winObj->winWidget && defined $xPosPixels) {
1386  
1387 # DEBUG v2.0
1388 # MS Windows adds extra pixels to the X position (for unknown reasons); compensate
1389 # for that, if required
1390 if ($^O eq 'MSWin32' && $axmud::CLIENT->mswinWinPosnTweakFlag) {
1391  
1392 $xPosPixels -= 7;
1393 }
1394  
1395 $winObj->winWidget->move($xPosPixels, $yPosPixels);
1396 }
1397  
1398 } else {
1399  
1400 # X11::WMCtrl expects -1 values, rather than undef
1401 if (! defined $xPosPixels) {
1402  
1403 $xPosPixels = -1;
1404 $yPosPixels = -1;
1405 }
1406  
1407 if (! defined $widthPixels) {
1408  
1409 $widthPixels = -1;
1410 $heightPixels = -1;
1411 }
1412  
1413 # Move the window to the correct workspace
1414 $axmud::CLIENT->desktopObj->wmCtrlObj->wmctrl(
1415 '-r',
1416 $winObj->internalID,
1417 '-t',
1418 $self->systemNum,
1419 '-i',
1420 );
1421  
1422 # Set the window's size and position
1423 $axmud::CLIENT->desktopObj->wmCtrlObj->wmctrl(
1424 '-r',
1425 $winObj->internalID,
1426 "-e 0,$xPosPixels,$yPosPixels,$widthPixels,$heightPixels",
1427 '-i',
1428 );
1429 }
1430  
1431 # Operation complete
1432 return 1;
1433 }
1434  
1435 # Window creation
1436  
1437 sub createGridWin {
1438  
1439 # Can be called by anything
1440 #
1441 # Creates a 'grid' window object. 'external' windows use GA::Win::External objects; other
1442 # kinds of 'grid' window use GA::Win::Internal. (This function isn't used to create 'free'
1443 # windows; call GA::Generic::Win->createFreeWin for that)
1444 # If the window itself doesn't exist, creates it using the specified size and coordinates,
1445 # in the specified layer, on the workspace $self->systemNum
1446 # If the window already exists, resizes and moves it using the specified size and
1447 # coordinates
1448 # If workspace grids are available and the window's specified position is occupied by
1449 # another window, moves the specified window to another position in the zone, if possible
1450 # If workspace grids aren't available, ignores the specified coordinates (if supplied), and
1451 # lets the desktop's window manager decide where to put the window
1452 #
1453 # NB If the calling function wants the window to appear on a particular workspace only, it
1454 # can call this function directly
1455 # If the calling function wants the window to appear on the first available workspace, it
1456 # can call GA::Obj::Desktop->listWorkspaces to get an ordered list of workspaces, with
1457 # the preferred workspace (if specified) first in the list. The calling function can then
1458 # call this function for each workspace on the list until a new window object is actually
1459 # created
1460 #
1461 # NB 'main' windows should only be created by code in GA::Client or GA::Session.
1462 # 'protocol' and 'external' windows should only be created by code in GA::Session
1463 # If you write your own plugins, don't use them to create your own 'main', 'protocol' or
1464 # 'external' windows. You can create as many 'map', 'fixed' or 'custom' windows as you
1465 # like, though
1466 #
1467 # Expected arguments
1468 # $winType - The window type; one of the 'grid' window types specified by
1469 # GA::Client->constGridWinTypeHash
1470 # $winName - A name for the window:
1471 # $winType $winName
1472 # -------- --------
1473 # main main
1474 # map map
1475 # protocol Any string chosen by the protocol code (default value is
1476 # 'protocol')
1477 # fixed Any string chosen by the controlling code (default value
1478 # is 'fixed')
1479 # custom Any string chosen by the controlling code. For task
1480 # windows, the name of the task (e.g. 'status_task',
1481 # for other windows, default value is 'custom'
1482 # external The 'external' window's name (e.g. 'Notepad')
1483 #
1484 # Optional arguments
1485 # $winTitle - The text to use in the window's title bar. If 'undef', a default
1486 # window title is used
1487 # $winmapName - The name of the GA::Obj::Winmap object that specifies the
1488 # Gtk3::Window's layout when it is first created. If 'undef', a
1489 # default winmap is used. Ignored for 'map', 'fixed' and 'external'
1490 # windows which should have no winmap
1491 # $packageName - 'main', 'protocol' and 'custom' windows are created via a call to
1492 # GA::Win::Internal->new(). 'external' windows are created via a
1493 # call to GA::Win::External->new(). However, the calling function
1494 # can specify its own $packageName, if required. It's expected that
1495 # the package should inherit from GA::Win::Internal or
1496 # GA::Win::External
1497 # - 'map' windows are created via a call to GA::Win::Map->new(). If the
1498 # calling function specifies its own $packageName, it's expected that
1499 # the package either inherits from GA::Win::Map, or provides a
1500 # similar range of functions for other parts of the Axmud code to call
1501 # - $packageName must be specified when creating a 'fixed' window. If not,
1502 # an error is produced
1503 # $winWidget - The Gtk3::Window, if it already exists and it is known (otherwise
1504 # set to 'undef')
1505 # $internalID - For 'external' windows, the window internal ID, provided by
1506 # X11::WMCtrl ('undef' for other types of window)
1507 # $owner - The owner, if known. Can be any blessed reference, typically it's an
1508 # GA::Session or a task (inheriting from GA::Generic::Task). The owner
1509 # must have a ->del_winObj function, which is called when this window
1510 # closes
1511 # $session - The owner's session. If $owner is a GA::Session, that session. If
1512 # it's something else (like a task), the task's session. If $owner is
1513 # 'undef', so is $session. If $owner is GA::Client, then $session
1514 # should be 'undef'
1515 # $workspaceGrid - The number of the workspace grid to use (matches a key in
1516 # $self->gridHash). Should be set when GA::CLIENT->shareMainWinFlag
1517 # is TRUE. If 'undef' (when GA::Client->shareMainWinFlag = FALSE, or
1518 # if the calling function forgot to specify a workspace), uses the
1519 # workspace grid with the lowest number, on the assumption that it's
1520 # a single workspace grid shared by all sessions
1521 # $zone - Within the workspace grid, the zone number to use (matches a key in
1522 # GA::Obj::WorkspaceGrid->zoneHash). If 'undef' (or if
1523 # $workspaceGrid is 'undef'), this function will choose the zone. If
1524 # set, and the specified $zone is reserved for other windows, this
1525 # function will choose a different zone. Ignored if workspace grids
1526 # are not available
1527 # $layer - Which layer the window should be put in. If 'undef' and zones are
1528 # enabled, the default layer is used. Ignored if workspace grids are
1529 # not available
1530 # $xPosPixels, $yPosPixels
1531 # - The desired position of the top left-hand corner of the window in
1532 # pixels, with (0, 0) representing the top left-hand pixel on the
1533 # workspace. (If either or both is 'undef', this function will decide
1534 # the position. Ignored if $zone is specified)
1535 # $widthPixels, $heightPixels
1536 # - The desired size of the window, in pixels. (If either or both is
1537 # 'undef', this function will decide the size of the window. Ignored
1538 # if $zone is specified)
1539 # $beforeListRef, $afterListRef
1540 # - References to a list of functions in the window object. Any functions
1541 # in the first list are called just after the Gtk3::Window is created.
1542 # Any functions in the second list are called just after the
1543 # window is made visible. These functions should be used to set up
1544 # additional ->signal_connects for this type of window, if they are
1545 # required
1546 #
1547 # Return values
1548 # 'undef' on improper arguments, if the window object can't be created, or if the window
1549 # itself can't be created (when it doesn't already exist)
1550 # Blessed reference to the newly-created window object on success
1551  
1552 my (
1553 $self, $winType, $winName, $winTitle, $winmapName, $packageName, $winWidget,
1554 $internalID, $owner, $session, $workspaceGrid, $zone, $layer, $xPosPixels, $yPosPixels,
1555 $widthPixels, $heightPixels, $beforeListRef, $afterListRef, $check,
1556 ) = @_;
1557  
1558 # Local variables
1559 my (
1560 $complainant, $pluginName, $pluginObj, $zoneSpecifiedFlag, $workspaceGridObj, $zoneObj,
1561 $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $areaObj, $winmapObj, $winObj,
1562 @geometryList,
1563 );
1564  
1565 # Check for improper arguments
1566 if (! defined $winType || ! defined $winName || defined $check) {
1567  
1568 return $axmud::CLIENT->writeImproper($self->_objClass . '->createGridWin', @_);
1569 }
1570  
1571 # Any error messages (besides improper arguments errors) should be written via the
1572 # GA::Session, if there is one, or the GA::Client, if not
1573 if ($session) {
1574 $complainant = $session;
1575 } else {
1576 $complainant = $axmud::CLIENT;
1577 }
1578  
1579 # Check $winType is valid
1580 if (! $axmud::CLIENT->ivExists('constGridWinTypeHash', $winType)) {
1581  
1582 return $complainant->writeError(
1583 'Invalid \'grid\' window type \'' . $winType . '\'',
1584 $self->_objClass . '->createGridWin',
1585 );
1586  
1587 # Check that $owner and $session are (in combination) valid values
1588 } elsif (
1589 (defined $owner && ! defined $session && $owner->_objClass ne 'Games::Axmud::Client')
1590 || (defined $session && ! defined $owner)
1591 ) {
1592 return $complainant->writeError(
1593 'Invalid owner/session arguments',
1594 $self->_objClass . '->createGridWin',
1595 );
1596  
1597 # No session may open a second 'main' window
1598 } elsif ($winType eq 'main' && $session && $session->mainWin) {
1599  
1600 return $complainant->writeError(
1601 'Cannot create second \'main\' window for session #' . $session->number,
1602 $self->_objClass . '->createGridWin',
1603 );
1604  
1605  
1606 # For 'fixed' windows, we must have received a package name
1607 } elsif ($winType eq 'fixed' && ! $packageName) {
1608  
1609 return $complainant->writeError(
1610 'Cannot create \'fixed\' windows without a specified package name',
1611 $self->_objClass . '->createGridWin',
1612 );
1613 }
1614  
1615 # If a package name was specified and it was added by a plugin, check that the plugin is
1616 # enabled
1617 if ($packageName) {
1618  
1619 $pluginName = $axmud::CLIENT->ivShow('pluginGridWinHash', $packageName);
1620 if ($pluginName) {
1621  
1622 $pluginObj = $axmud::CLIENT->ivShow('pluginHash', $pluginName);
1623 if ($pluginObj && ! $pluginObj->enabledFlag) {
1624  
1625 return $complainant->writeError(
1626 'Cannot create the window because the \'' . $pluginName . '\' plugin is'
1627 . ' disabled',
1628 $self->_objClass . '->createGridWin',
1629 );
1630 }
1631 }
1632 }
1633  
1634 # Workspace grids and zones are disabled for this workspace. Axmud doesn't change the
1635 # position of any windows. Windows are left where the system's window manager puts them.
1636 # Window controls are compulsory
1637 if (! $self->gridEnableFlag || ! $axmud::CLIENT->activateGridFlag) {
1638  
1639 return $self->createSimpleGridWin(
1640 $winType, $winName, $winTitle, $winmapName, $packageName, $winWidget, $internalID,
1641 $owner, $session, $xPosPixels, $yPosPixels, $widthPixels, $heightPixels,
1642 $beforeListRef, $afterListRef,
1643 );
1644 }
1645  
1646 # Workspace grids and zones are enabled for this workspace
1647  
1648 # We need to remember if $zone was specified when this function was called, or not, because
1649 # the way that $widthPixels/$heightPixels and $xPosPixels/$yPosPixels are used depends on
1650 # it
1651 $zoneSpecifiedFlag = FALSE;
1652 if (defined $workspaceGrid && defined $zone) {
1653  
1654 $zoneSpecifiedFlag = TRUE;
1655  
1656 # Check the workspace grid and zone exist; if either do not, reset the variables so
1657 # that the next block of code can choose its own zone
1658 $workspaceGridObj = $self->ivShow('gridHash', $workspaceGrid);
1659 if ($workspaceGridObj) {
1660  
1661 $zoneObj = $workspaceGridObj->ivShow('zoneHash', $zone);
1662 }
1663  
1664 if (! $zoneObj) {
1665  
1666 # The specified zone number, $zone, doesn't exist
1667 $complainant->writeWarning(
1668 'The specified zone #' . $zone . ' in workspace grid #' . $workspaceGrid
1669 . ' doesn\'t exist',
1670 $self->_objClass . '->createGridWin',
1671 );
1672 }
1673  
1674 # Tidy up
1675 if (! $zoneObj) {
1676  
1677 $zone = undef;
1678 }
1679  
1680 if (! $workspaceGridObj) {
1681  
1682 $workspaceGrid = undef;
1683 }
1684  
1685 } elsif (defined $xPosPixels && defined $yPosPixels) {
1686  
1687 # Either the workspace grid or the zone weren't specified, but we can use
1688 # $xPosPixels / $yPosPixels to choose them
1689 if (defined $workspaceGrid) {
1690  
1691 # Use $xPosPixels/$yPosPixels to choose a zone on the specified workspace
1692 $workspaceGridObj = $self->ivShow('gridHash', $workspaceGrid);
1693  
1694 } else {
1695  
1696 # Use $xPosPixels/$yPosPixels to choose a zone in the correct workspace grid (the
1697 # one controlled by $session)
1698 $workspaceGridObj = $self->findWorkspaceGrid($session);
1699 if ($workspaceGridObj) {
1700  
1701 $workspaceGrid = $workspaceGridObj->number;
1702 }
1703 }
1704  
1705 if ($workspaceGridObj) {
1706  
1707 $zoneObj = $workspaceGridObj->findZone($xPosPixels, $yPosPixels);
1708 if ($zoneObj) {
1709  
1710 $zone = $zoneObj->number;
1711  
1712 # If this zone is reserved for certain types of windows, not including this kind
1713 # of window, don't use the specified zone
1714 if (! $zoneObj->checkWinAllowed($winType, $winName, $session)) {
1715  
1716 # Window not allowed in this zone
1717 $complainant->writeWarning(
1718 '\'' . $winType . '\' windows aren\'t allowed in zone #' . $zone,
1719 $self->_objClass . '->createGridWin',
1720 );
1721  
1722 $zoneObj = undef;
1723 }
1724 }
1725 }
1726  
1727 # Tidy up
1728 if (! $zoneObj) {
1729  
1730 $zone = undef;
1731 }
1732  
1733 if (! $workspaceGridObj) {
1734  
1735 $workspaceGrid = undef;
1736 }
1737 }
1738  
1739 # Now, if the zone still hasn't been set, decide which zone to use
1740 if (! defined $zone) {
1741  
1742 # If no workspace grid is yet specified, choose one (the one controlled by $session)
1743 if (! defined $workspaceGridObj) {
1744  
1745 $workspaceGridObj = $self->findWorkspaceGrid($session);
1746 if (! $workspaceGridObj) {
1747  
1748 return $complainant->writeWarning(
1749 'Cannot find a workspace grid onto which window \'' . $winName
1750 . '\' can be placed',
1751 $self->_objClass . '->createGridWin',
1752 );
1753  
1754 } else {
1755  
1756 $workspaceGrid = $workspaceGridObj->number;
1757 }
1758 }
1759  
1760 # Check that the workspace grid actually contains some zones
1761 if (! $workspaceGridObj->zoneHash) {
1762  
1763 return $complainant->writeWarning(
1764 'There are no zones on the workspace grid in which to put the window \''
1765 . $winName . '\', window was not allocated to a zone',
1766 $self->_objClass . '->createGridWin',
1767 );
1768  
1769 } else {
1770  
1771 $zoneObj = $self->chooseZone(
1772 $workspaceGridObj,
1773 $winType,
1774 $winName,
1775 $winWidget,
1776 $internalID,
1777 $owner,
1778 $session,
1779 );
1780  
1781 if (! $zoneObj) {
1782  
1783 # All available zones are full. Error message has been displayed
1784 return undef;
1785  
1786 } else {
1787  
1788 $zone = $zoneObj->number;
1789 }
1790 }
1791 }
1792  
1793 # If $zone wasn't specified when this function was called and one (but not both) of
1794 # $widthPixels / $heightPixels weren't specified, use default values for the width and
1795 # height
1796 # Exception: if this window is a 'main' window and it's the first window ever created,
1797 # assume that the Axmud is still setting up. If there is more than one zone, set the
1798 # 'main' window to fill the zone. If there is only one zone, use the default 'main' window
1799 # size
1800 ($widthPixels, $heightPixels) = $self->chooseWinSize(
1801 $winType,
1802 $workspaceGridObj,
1803 $zoneObj,
1804 $zoneSpecifiedFlag,
1805 $widthPixels,
1806 $heightPixels,
1807 );
1808  
1809 # Choose the exact size and position of the window inside its zone, taking into account such
1810 # factors as other windows in the zone
1811 (
1812 $layer, $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $xPosPixels, $yPosPixels,
1813 $widthPixels, $heightPixels,
1814 ) = $self->chooseWinPosn(
1815 $complainant,
1816 $winType,
1817 $zoneSpecifiedFlag,
1818 $workspaceGridObj,
1819 $widthPixels,
1820 $heightPixels,
1821 $zoneObj,
1822 $layer,
1823 $xPosPixels,
1824 $yPosPixels,
1825 );
1826  
1827 if (! defined $layer) {
1828  
1829 # Zone is full
1830 return undef;
1831 }
1832  
1833 # Create a GA::Obj::Area within the zone (which handles the new window, and is the same
1834 # size as it)
1835 $areaObj = $zoneObj->addArea(
1836 $layer,
1837 $xPosBlocks,
1838 $yPosBlocks,
1839 $widthBlocks,
1840 $heightBlocks,
1841 $xPosPixels,
1842 $yPosPixels,
1843 $widthPixels,
1844 $heightPixels,
1845 $session,
1846 );
1847  
1848 if (! $areaObj) {
1849  
1850 # Checks failed
1851 return undef;
1852 }
1853  
1854 # Convert a winmap name to a winmap object. If no winmap was specified, use a default one
1855 $winmapObj = $self->getWinmap($winType, $winmapName, $zoneObj);
1856 # We need to specify a winmap name or 'undef' in the call to ->new
1857 if ($winmapObj) {
1858 $winmapName = $winmapObj->name;
1859 } else {
1860 $winmapName = undef;
1861 }
1862  
1863 # Create a new window object
1864 if (! defined $packageName) {
1865  
1866 if ($winType eq 'external') {
1867  
1868 $packageName = 'Games::Axmud::Win::External';
1869  
1870 } elsif ($winType eq 'map') {
1871  
1872 $packageName = 'Games::Axmud::Win::Map';
1873  
1874 } else {
1875  
1876 # $winType is 'main', 'protocol', 'custom'
1877 $packageName = 'Games::Axmud::Win::Internal';
1878 }
1879 }
1880  
1881 $winObj = $packageName->new(
1882 $axmud::CLIENT->desktopObj->gridWinCount,
1883 $winType,
1884 $winName,
1885 $self,
1886 $owner,
1887 $session,
1888 $workspaceGridObj,
1889 $areaObj,
1890 $winmapName,
1891 );
1892  
1893 # (The check for ->winType makes sure that plugins don't create, for example, a 'free'
1894 # window and then try to add it via a call to GA::Client->createGridWin)
1895 if (! $winObj || $winObj->winCategory ne 'grid' || $winObj->winType ne $winType) {
1896  
1897 # Something or other failed. Update the zone
1898 $zoneObj->removeArea($areaObj);
1899  
1900 return undef;
1901 }
1902  
1903 # Update the GA::Obj::Desktop object's registry of all 'grid' windows
1904 $axmud::CLIENT->desktopObj->add_gridWin($winObj);
1905 # Tell the workspace grid object and area object they have received a new window
1906 $workspaceGridObj->add_gridWin($winObj);
1907 $areaObj->set_win($winObj);
1908  
1909 # Move the window to its correct workspace, size and position
1910 if ($winType ne 'external') {
1911  
1912 # Create a new Gtk3::Window widget at the specified size and position (but don't make
1913 # it visible yet)
1914 if (! $winObj->winSetup($winTitle, $beforeListRef)) {
1915  
1916 # Something or other failed. Update the zone
1917 $zoneObj->removeArea($areaObj);
1918  
1919 return undef;
1920 }
1921  
1922 # Make the window actually visible, if it's not already visible
1923 if (! $winObj->enabledFlag) {
1924  
1925 $winObj->winEnable($afterListRef);
1926 }
1927  
1928 # Move the window to its correct workspace, size and position
1929 $self->moveResizeWin(
1930 $winObj,
1931 $xPosPixels,
1932 $yPosPixels,
1933 $widthPixels,
1934 $heightPixels,
1935 );
1936  
1937 # # For 'grid' windows, $winObj->winEnable created the window in a minimised state so it
1938 # # didn't appear to jump around on the desktop. Unminimise it now
1939 # $winObj->unminimise();
1940  
1941 # Update the GA::Client's hash of stored window positions (if required)
1942 if ($winObj->winWidget) {
1943  
1944 $axmud::CLIENT->add_storeGridPosn(
1945 $winObj,
1946 $winObj->winWidget->get_position(),
1947 $winObj->winWidget->get_size(),
1948 );
1949 }
1950  
1951 } else {
1952  
1953 # Update the 'external' window object's IVs
1954 if (! $winObj->winSetup($internalID)) {
1955  
1956 # Something or other failed. Update the zone
1957 $zoneObj->removeArea($areaObj);
1958  
1959 return undef;
1960 }
1961  
1962 $self->moveResizeWin(
1963 $winObj,
1964 ($xPosPixels - $self->controlsLeftSize),
1965 ($yPosPixels - $self->controlsTopSize),
1966 $widthPixels,
1967 $heightPixels,
1968 );
1969  
1970 # Make the window actually visible, if it's not already visible
1971 if (! $winObj->enabledFlag) {
1972  
1973 $winObj->winEnable($afterListRef);
1974 }
1975  
1976 # If this 'external' window has already been grabbed to the workspace grid with
1977 # ';grabwindow' and then removed from it with ';banishwindow', it will be minimised.
1978 # In any case, if the window is minimised, it should be unminimised before being
1979 # restacked
1980 $winObj->unminimise();
1981 }
1982  
1983 # The workspace grid's new current layer is the layer in which this window will be
1984 # placed (so that it's visible to the user immediately)
1985 $workspaceGridObj->set_currentLayer($layer);
1986  
1987 # Make sure all windows on the workspace grid are stacked correctly, so that windows in
1988 # lower layers are beneath windows in higher layers (but windows in a layer higher than
1989 # the workspace grid's current layer are minimised)
1990 $zoneObj->restackWin();
1991  
1992 # GA::Obj::Zone->adjustSingleWin is called above to fill in small gaps in the zone. Now,
1993 # if the zone is wide enough (and its orientation is 'horizontal') or high enough (and its
1994 # orientation is 'vertical') for only one area of the zone's default size, then consider
1995 # expanding all of the areas in the zone to fill gaps that are bigger than a gridblock
1996 # or two, but still not big enough to hold another area
1997 # If it would be better to reduce all the area sizes in order to make room for another
1998 # area, some time in the future, do that instead. Leave enough room for an area of the
1999 # default size
2000 # If the global flag isn't set allowing these adjustments, or if the zone has a maximum of 1
2001 # area, don't make any adjustments
2002 if (
2003 $axmud::CLIENT->gridAdjustmentFlag
2004 && $zoneObj->areaMax != 1
2005 && (
2006 # Windows are stacked horizontally, and there's only room for one (default size)
2007 # window in each row
2008 (
2009 $zoneObj->orientation eq 'horizontal'
2010 && $zoneObj->heightBlocks >= $zoneObj->defaultAreaHeight
2011 && $zoneObj->heightBlocks < ($zoneObj->defaultAreaHeight * 2)
2012 # Windows are stacked vertically, and there's only room for one (default size)
2013 # window in each column
2014 ) || (
2015 $zoneObj->orientation eq 'vertical'
2016 && $zoneObj->widthBlocks >= $zoneObj->defaultAreaWidth
2017 && $zoneObj->widthBlocks < ($zoneObj->defaultAreaWidth * 2)
2018 )
2019 )
2020 ) {
2021 $zoneObj->adjustMultipleWin($layer);
2022 }
2023  
2024 # If a GA::Session was specified, restore focus to its 'main' window
2025 if ($session && $session->mainWin) {
2026  
2027 $session->mainWin->restoreFocus();
2028 }
2029  
2030 # Return the blessed reference of the new window object to show success
2031 return $winObj;
2032 }
2033  
2034 sub createSimpleGridWin {
2035  
2036 # Called by $self->createGridWin if workspace grids aren't available for this workspace,
2037 # when we don't have to bother with trying to make the window fit in a particular position
2038 # on the workspace
2039 # (When workspace grids are available, $self->createGridWin does the job itself)
2040 #
2041 # Also called by GA::Obj::Desktop->start to create the spare 'main' window when Axmud first
2042 # runs (and before the Connections window opens)
2043 #
2044 # Expected arguments
2045 # $winType - The window type; one of the 'grid' window types specified by
2046 # GA::Client->constGridWinTypeHash
2047 # $winName - A name for the window:
2048 # $winType $winName
2049 # -------- --------
2050 # main main
2051 # map map
2052 # protocol Any string chosen by the protocol code (default value is
2053 # 'protocol')
2054 # fixed Any string chosen by the controlling code (default value
2055 # is 'fixed')
2056 # custom Any string chosen by the controlling code. For task
2057 # windows, the name of the task (e.g. 'status_task',
2058 # for other windows, default value is 'custom'
2059 # external The 'external' window's name (e.g. 'Notepad')
2060 #
2061 # Optional arguments
2062 # $winTitle - The text to use in the window's title bar. If 'undef', a default
2063 # window title is used
2064 # $winmapName - The name of the GA::Obj::Winmap object that specifies the
2065 # Gtk3::Window's layout when it is first created. If 'undef', a
2066 # default winmap is used. Ignored for 'map', 'fixed' and 'external'
2067 # windows which should have no winmap
2068 # $packageName - 'main', 'protocol' and 'custom' windows are created via a call to
2069 # GA::Win::Internal->new(). 'external' windows are created via a
2070 # call to GA::Win::External->new(). However, the calling function
2071 # can specify its own $packageName, if required. It's expected that
2072 # the package should inherit from GA::Win::Internal or
2073 # GA::Win::External
2074 # - 'map' windows are created via a call to GA::Win::Map->new(). If the
2075 # calling function specifies its own $packageName, it's expected that
2076 # the package either inherits from GA::Win::Map, or provides a
2077 # similar range of functions for other parts of the Axmud code to call
2078 # - $packageName must be specified when creating a 'fixed' window. If not,
2079 # an error is produced
2080 # $winWidget - The Gtk3::Window, if it already exists and it is known (otherwise
2081 # set to 'undef')
2082 # $internalID - For 'external' windows, the window internal ID, provided by
2083 # X11::WMCtrl ('undef' for other types of window)
2084 # $owner - The owner, if known. Can be any blessed reference, typically it's an
2085 # GA::Session or a task (inheriting from GA::Generic::Task). The owner
2086 # must have a ->del_winObj function, which is called when this window
2087 # closes
2088 # $session - The owner's session. If $owner is a GA::Session, that session. If
2089 # it's something else (like a task), the task's session. If $owner is
2090 # 'undef', so is $session. If $owner is GA::Client, then $session
2091 # should be 'undef'
2092 # $xPosPixels, $yPosPixels
2093 # - The desired position of the top left-hand corner of the window in
2094 # pixels, with (0, 0) representing the top left-hand pixel on the
2095 # workspace. (If either or both is 'undef', this function will decide
2096 # the position. Ignored if $zone is specified)
2097 # $widthPixels, $heightPixels
2098 # - The desired size of the window, in pixels. (If either or both is
2099 # 'undef', this function will decide the size of the window)
2100 # $beforeListRef, $afterListRef
2101 # - References to a list of functions in the window object. Any functions
2102 # in the first list are called just after the Gtk3::Window is created.
2103 # Any functions in the second list are called just after the
2104 # window is made visible. These functions should be used to set up
2105 # additional ->signal_connects for this type of window, if they are
2106 # required
2107 #
2108 # Return values
2109 # 'undef' on improper arguments, if the window object can't be created, or if the window
2110 # itself can't be created (when it doesn't already exist)
2111 # Blessed reference to the newly-created window object on success
2112  
2113 my (
2114 $self, $winType, $winName, $winTitle, $winmapName, $packageName, $winWidget,
2115 $internalID, $owner, $session, $xPosPixels, $yPosPixels, $widthPixels, $heightPixels,
2116 $beforeListRef, $afterListRef, $check,
2117 ) = @_;
2118  
2119 # Local variables
2120 my (
2121 $listRef, $winmapObj, $winObj,
2122 @geometryList,
2123 );
2124  
2125 # Check for improper arguments
2126 if (! defined $winType || ! defined $winName || defined $check) {
2127  
2128 return $axmud::CLIENT->writeImproper($self->_objClass . '->createSimpleGridWin', @_);
2129 }
2130  
2131 # If the GA::Client IV stores a position/size for a window with this $winName, we might be
2132 # able to use it (but never for 'external' windows)
2133 $listRef = $axmud::CLIENT->ivShow('storeGridPosnHash', $winName);
2134 if ($winType ne 'external' && defined $listRef) {
2135  
2136 # $listRef is in the form (x y wid hei)
2137 if (
2138 ! defined $xPosPixels
2139 && ! defined $yPosPixels
2140 && defined $$listRef[0]
2141 && defined $$listRef[1]
2142 ) {
2143 $xPosPixels = $$listRef[0];
2144 $yPosPixels = $$listRef[1];
2145 }
2146  
2147 if (
2148 ! defined $widthPixels
2149 && ! defined $heightPixels
2150 && defined $$listRef[2]
2151 && defined $$listRef[3]
2152 ) {
2153 $widthPixels = $$listRef[2];
2154 $heightPixels = $$listRef[3];
2155 }
2156 }
2157  
2158 # If no size is still not specified, use a default size (and let the system's window manager
2159 # set its own position)
2160 if (! defined $widthPixels || ! defined $heightPixels) {
2161  
2162 if ($winType eq 'main') {
2163  
2164 $widthPixels = $axmud::CLIENT->customMainWinWidth;
2165 $heightPixels = $axmud::CLIENT->customMainWinHeight;
2166  
2167 } else {
2168  
2169 $widthPixels = $axmud::CLIENT->customGridWinWidth;
2170 $heightPixels = $axmud::CLIENT->customGridWinHeight;
2171 }
2172 }
2173  
2174 # Convert a winmap name to a winmap object. If no winmap was specified, use a default one
2175 $winmapObj = $self->getWinmap($winType, $winmapName);
2176 if (! defined $winmapObj) {
2177  
2178 # No winmap name specified, or winmap doesn't exist, so use a default winmap
2179 if ($winType eq 'main') {
2180  
2181 if ($self->gridEnableFlag && $axmud::CLIENT->activateGridFlag) {
2182  
2183 $winmapObj = $axmud::CLIENT->ivShow(
2184 'winmapHash',
2185 $axmud::CLIENT->defaultEnabledWinmap,
2186 );
2187  
2188 } else {
2189  
2190 $winmapObj = $axmud::CLIENT->ivShow(
2191 'winmapHash',
2192 $axmud::CLIENT->defaultDisabledWinmap,
2193 );
2194 }
2195  
2196 } elsif ($winType ne 'external') {
2197  
2198 $winmapObj
2199 = $axmud::CLIENT->ivShow('winmapHash', $axmud::CLIENT->defaultInternalWinmap);
2200 }
2201 }
2202  
2203 # We need to specify a winmap name or 'undef' in the call to ->new
2204 if ($winmapObj) {
2205 $winmapName = $winmapObj->name;
2206 } else {
2207 $winmapName = undef;
2208 }
2209  
2210 # Create a new window object
2211 if (! defined $packageName) {
2212  
2213 if ($winType eq 'external') {
2214  
2215 $packageName = 'Games::Axmud::Win::External';
2216  
2217 } elsif ($winType eq 'map') {
2218  
2219 $packageName = 'Games::Axmud::Win::Map';
2220  
2221 } else {
2222  
2223 # $winType is 'main', 'protocol', 'custom'
2224 $packageName = 'Games::Axmud::Win::Internal';
2225 }
2226 }
2227  
2228 $winObj = $packageName->new(
2229 $axmud::CLIENT->desktopObj->gridWinCount,
2230 $winType,
2231 $winName,
2232 $self,
2233 $owner,
2234 $session,
2235 undef,
2236 undef,
2237 $winmapName,
2238 );
2239  
2240 # (The check for ->winType makes sure that plugins don't create, for example, a 'free'
2241 # window and then try to add it via a call to GA::Client->createGridWin)
2242 if (! $winObj || $winObj->winCategory ne 'grid' || $winObj->winType ne $winType) {
2243  
2244 # Something or other failed
2245 return undef;
2246 }
2247  
2248 # Update the GA::Obj::Desktop object's registry of all 'grid' windows
2249 $axmud::CLIENT->desktopObj->add_gridWin($winObj);
2250  
2251 # Move the window to its correct workspace, size and position
2252 if ($winType ne 'external') {
2253  
2254 # Create a new Gtk3::Window widget (but don't make it visible yet)
2255 if (! $winObj->winSetup($winTitle, $beforeListRef)) {
2256  
2257 # Something or other failed
2258 return undef;
2259 }
2260  
2261 # If a size and/or position were specified, move the window
2262 if (
2263 (defined $xPosPixels && defined $yPosPixels)
2264 || (defined $widthPixels && defined $heightPixels)
2265 ) {
2266 $self->moveResizeWin(
2267 $winObj,
2268 $xPosPixels,
2269 $yPosPixels,
2270 $widthPixels,
2271 $heightPixels,
2272 );
2273 }
2274  
2275 # Make the window actually visible, if it's not already visible
2276 if (! $winObj->enabledFlag) {
2277  
2278 $winObj->winEnable($afterListRef);
2279 }
2280  
2281 # Update the GA::Client's hash of stored window positions (if required)
2282 if ($winObj->winWidget) {
2283  
2284 $axmud::CLIENT->add_storeGridPosn(
2285 $winObj,
2286 $winObj->winWidget->get_position(),
2287 $winObj->winWidget->get_size(),
2288 );
2289 }
2290  
2291 } else {
2292  
2293 # Update the 'external' window object's IVs
2294 if (! $winObj->winSetup($internalID)) {
2295  
2296 # Something or other failed
2297 return undef;
2298 }
2299  
2300 # If a size and/or position were specified, move the window
2301 if (
2302 (defined $xPosPixels && defined $yPosPixels)
2303 || (defined $widthPixels && defined $heightPixels)
2304 ) {
2305 $self->moveResizeWin(
2306 $winObj,
2307 ($xPosPixels - $self->controlsLeftSize),
2308 ($yPosPixels - $self->controlsTopSize),
2309 $widthPixels,
2310 $heightPixels,
2311 );
2312 }
2313  
2314 # Make the window actually visible, if it's not already visible
2315 if (! $winObj->enabledFlag) {
2316  
2317 $winObj->winEnable($afterListRef);
2318 }
2319  
2320 # If this 'external' window has already been grabbed to the workspace grid with
2321 # ';grabwindow' and then removed from it with ';banishwindow', it will be minimised.
2322 # In any case, if the window is minimised, it should be unminimised before being
2323 # restacked
2324 $axmud::CLIENT->desktopObj->wmCtrlObj->unminimize($internalID);
2325 }
2326  
2327 # Windows created via this function always have the focus (unlike those created by calls to
2328 # $self->createGridWin, in which the 'main' windows is always given focus)
2329 $winObj->restoreFocus();
2330  
2331 # Return the blessed reference of the new window object to show success
2332 return $winObj;
2333 }
2334  
2335 sub chooseZone {
2336  
2337 # Called by $self->createGridWin, when that function is asked to create a window in an
2338 # unspecified zone
2339 # Also called by GA::Session->setMainWin when there are no sessions, to move the spare
2340 # 'main' window into the first available zone
2341 #
2342 # Decides which zone in the specified workspace grid to place the window. If it's not
2343 # possible to place the window in any of them, returns 'undef'
2344 #
2345 # Expected arguments
2346 # $workspaceGridObj
2347 # - The GA::Obj::WorkspaceGrid object which will receive the new window
2348 # $winType - The window type; one of the 'grid' window types specified by
2349 # GA::Client->constGridWinTypeHash
2350 # $winName - The window name
2351 # $winType $winName
2352 # -------- --------
2353 # main main
2354 # map map
2355 # protocol Any string chosen by the protocol code (default value is
2356 # 'protocol')
2357 # fixed Any string chosen by the controlling code (default value
2358 # is 'fixed')
2359 # custom Any string chosen by the controlling code. For task
2360 # windows, the name of the task (e.g. 'status_task',
2361 # for other windows, default value is 'custom'
2362 # external The 'external' window's name (e.g. 'Notepad')
2363 #
2364 # Optional arguments
2365 # $winWidget - The Gtk3::Window, if it already exists and it is known (otherwise set
2366 # to 'undef')
2367 # $internalID - For 'external' windows, the window internal ID, provided by
2368 # X11::WMCtrl ('undef' for other types of window)
2369 # $owner - The owner, if known. Can be any blessed reference, typically it's an
2370 # GA::Session or a task (inheriting from GA::Generic::Task)
2371 # $session - The owner's session. If $owner is a GA::Session, that session. If
2372 # it's something else (like a task), the task's session. If $owner is
2373 # 'undef', so is $session
2374 #
2375 # Return values
2376 # 'undef' on improper arguments or if no zone is available for this window
2377 # Otherwise, returns the chosen GA::Obj::Zone
2378  
2379 my (
2380 $self, $workspaceGridObj, $winType, $winName, $winWidget, $internalID, $owner, $session,
2381 $check,
2382 ) = @_;
2383  
2384 # Local variables
2385 my @list;
2386  
2387 # Check for improper arguments
2388 if (
2389 ! defined $workspaceGridObj || ! defined $winType || ! defined $winName
2390 || defined $check
2391 ) {
2392 return $axmud::CLIENT->writeImproper($self->_objClass . '->chooseZone', @_);
2393 }
2394  
2395 # Choose zones in the following order:
2396 # 1. If it's a zone reserved for this window or for this window's session, choose it
2397 # 2. If it's an unreserved/unowned zone and the current layer isn't full, use it
2398 # 3. If it's an unreserved/unowned zone and any of its layers aren't full, use it
2399 @list = sort {$a->number <=> $b->number} ($workspaceGridObj->ivValues('zoneHash'));
2400  
2401 # 1. If it's a zone reserved for this window or for this window's session, choose it
2402 foreach my $zoneObj (@list) {
2403  
2404 my $areaCount;
2405  
2406 if ($zoneObj->reservedFlag || $zoneObj->owner) {
2407  
2408 # Is this window allowed in this zone, and if the zone has a maximum number of
2409 # areas, is there room for another area in the zone? (A GA::Obj::Area handles
2410 # the part of the zone's internal grid occupied by a single window)
2411 if ($zoneObj->checkWinAllowed($winType, $winName, $session)) {
2412  
2413 # The window is allowed in this zone, but is the zone already full?
2414 $areaCount = $zoneObj->ivPairs('areaHash');
2415 if (
2416 ($zoneObj->areaMax && $areaCount < $zoneObj->areaMax)
2417 || (! $zoneObj->areaMax)
2418 ) {
2419 # The window is allowed in this zone
2420 return $zoneObj;
2421 }
2422 }
2423 }
2424 }
2425  
2426 # 2. If it's an unreserved zone and the current layer isn't full, use it
2427 foreach my $zoneObj (@list) {
2428  
2429 my $areaCount;
2430  
2431 if (
2432 ! $zoneObj->reservedFlag
2433 && ! $zoneObj->owner
2434 && ($zoneObj->visibleAreaMax || ! $zoneObj->multipleLayerFlag)
2435 ) {
2436 # This is a zone with a maximum number of areas specified per layer. Is there room
2437 # for another area in the current layer?
2438 if ($zoneObj->checkWinAllowed($winType, $winName, $session)) {
2439  
2440 # Count the number of areas in the current layer
2441 $areaCount = 0;
2442 foreach my $areaObj ($zoneObj->ivValues('areaHash')) {
2443  
2444 if ($areaObj->layer == $workspaceGridObj->currentLayer) {
2445  
2446 $areaCount++;
2447 }
2448 }
2449  
2450 if (
2451 ($zoneObj->visibleAreaMax && $areaCount < $zoneObj->visibleAreaMax)
2452 || (! $zoneObj->multipleLayerFlag && $areaCount < $zoneObj->areaMax)
2453 ) {
2454 # Window allowed; use this zone
2455 return $zoneObj;
2456 }
2457 }
2458 }
2459 }
2460  
2461 # 3. If it's an unreserved zone and any of its layers aren't full, use it
2462 foreach my $zoneObj (@list) {
2463  
2464 my $areaCount;
2465  
2466 if (! $zoneObj->reservedFlag && ! $zoneObj->owner) {
2467  
2468 # This is a non-reserved zone. If the zone has a maximum number of areas, is there
2469 # room for another area in the zone?
2470 $areaCount = $zoneObj->ivPairs('areaHash');
2471 if (
2472 ($zoneObj->areaMax && $areaCount < $zoneObj->areaMax)
2473 || (! $zoneObj->areaMax)
2474 ) {
2475 # Window allowed; use this zone
2476 return $zoneObj;
2477 }
2478 }
2479 }
2480  
2481 # No zone found
2482 return undef;
2483 }
2484  
2485 sub chooseWinSize {
2486  
2487 # Called by $self->createGridWin after choosing the zone into which a window will be placed,
2488 # returns the size the window should have
2489 # Also called by GA::Obj::WorkspaceGrid->applyZonemap
2490 #
2491 # Expected arguments
2492 # $winType - The window type; one of the 'grid' window types specified by
2493 # GA::Client->constGridWinTypeHash
2494 # $workspaceGridObj
2495 # - The GA::Obj::WorkspaceGrid object which will receive the new window
2496 # $zoneObj - Blessed reference to the GA::Obj::Zone object, handling the zone in which
2497 # the window will be placed
2498 #
2499 # Optional arguments
2500 # $zoneSpecifiedFlag
2501 # - Set to TRUE if the function that called $self->createGridWin specified a
2502 # zone to use, otherwise set to FALSE. Set to 'undef' when called by
2503 # any other function
2504 # $widthPixels, $heightPixels
2505 # - The size of the window specified by the function that called
2506 # $self->createGridWin. Both set to 'undef' if no size was specified
2507 #
2508 # Return values
2509 # An empty list on improper arguments
2510 # Otherwise, returns the list ($widthPixels, $heightPixels)
2511  
2512 my (
2513 $self, $winType, $workspaceGridObj, $zoneObj, $zoneSpecifiedFlag, $widthPixels,
2514 $heightPixels, $check,
2515 ) = @_;
2516  
2517 # Local variables
2518 my (
2519 $blockSize,
2520 @emptyList,
2521 );
2522  
2523 # Check for improper arguments
2524 if (
2525 ! defined $winType || ! defined $workspaceGridObj || ! defined $zoneObj
2526 || defined $check
2527 ) {
2528 $axmud::CLIENT->writeImproper($self->_objClass . '->chooseWinSize', @_);
2529 return @emptyList;
2530 }
2531  
2532 # Import the standard gridblock size (in pixels) for convenience
2533 $blockSize = $axmud::CLIENT->gridBlockSize;
2534  
2535 # On the original call to $self->createGridWin, if a zone number wasn't specified and one
2536 # (but not both) of $widthPixels / $heightPixels weren't specified, use default values for
2537 # the width and height
2538 # Exception: if this window is a 'main' window and it's the first window ever created,
2539 # assume that the Axmud is still setting up. If there is more than one zone, set the
2540 # 'main' window to fill the zone. If there is only one zone, use the default 'main' window
2541 # size
2542 if (
2543 ! $zoneSpecifiedFlag
2544 && (
2545 (defined $widthPixels && ! defined $heightPixels)
2546 || (defined $heightPixels && ! defined $widthPixels)
2547 )
2548 ) {
2549 if ($winType eq 'main' && ! $axmud::CLIENT->desktopObj->gridWinHash) {
2550  
2551 # It's the 'main' window, which is almost always in zone 1
2552 if ($workspaceGridObj->zoneCount == 1) {
2553  
2554 $widthPixels = $axmud::CLIENT->customMainWinWidth;
2555 $heightPixels = $axmud::CLIENT->customMainWinHeight;
2556  
2557 } else {
2558  
2559 $widthPixels = ($zoneObj->widthBlocks * $blockSize);
2560 $heightPixels = ($zoneObj->heightBlocks * $blockSize);
2561 }
2562  
2563 } else {
2564  
2565 if (! defined $widthPixels) {
2566  
2567 $widthPixels = $axmud::CLIENT->customGridWinWidth;
2568  
2569 } elsif (! defined $heightPixels) {
2570  
2571 $heightPixels = $axmud::CLIENT->customGridWinHeight;
2572 }
2573 }
2574  
2575 # In a positioning free-for-all (zonemap 'single'), let a 'main' window use its own default
2576 # size
2577 } elsif ($winType eq 'main' && $workspaceGridObj->zonemap eq 'single') {
2578  
2579 $widthPixels = $axmud::CLIENT->customMainWinWidth;
2580 $heightPixels = $axmud::CLIENT->customMainWinHeight;
2581  
2582 # Most of the time, use the zone's default width and height if those defaults are specified.
2583 # Take into account any adjustment to the zone's default width and height that
2584 # $zoneObj->adjustMultipleWin might have made
2585 # If the zone's default values aren't specified, use the global default values
2586 } else {
2587  
2588 if (! defined $widthPixels) {
2589  
2590 if ($zoneObj->defaultAreaWidth) {
2591  
2592 $widthPixels
2593 = ($zoneObj->defaultAreaWidth + $zoneObj->widthAdjustBlocks) * $blockSize;
2594  
2595 } else {
2596  
2597 $widthPixels = $axmud::CLIENT->customGridWinWidth;
2598 }
2599 }
2600  
2601 if (! defined $heightPixels) {
2602  
2603 if ($zoneObj->defaultAreaHeight) {
2604  
2605 $heightPixels
2606 = ($zoneObj->defaultAreaHeight + $zoneObj->heightAdjustBlocks) * $blockSize;
2607  
2608 } else {
2609  
2610 $heightPixels = $axmud::CLIENT->customGridWinHeight;
2611 }
2612 }
2613 }
2614  
2615 return ($widthPixels, $heightPixels);
2616 }
2617  
2618 sub chooseWinPosn {
2619  
2620 # Called by $self->createGridWin after the window has been given a zone and a provisional
2621 # size
2622 # Sets the exact position of the window on the workspace, taking into account several
2623 # different factors
2624 #
2625 # Expected arguments
2626 # $complainant - The object to which error messages should be sent: either the
2627 # controlling GA::Session (if one was specified), or GA::Client
2628 # (if not)
2629 # $winType - The window type; one of the 'grid' window types specified by
2630 # GA::Client->constGridWinTypeHash
2631 # $zoneSpecifiedFlag
2632 # - Set to TRUE if the function that called $self->createGridWin specified
2633 # a zone to use, otherwise set to FALSE
2634 # $workspaceGridObj
2635 # - The GA::Obj::WorkspaceGrid object which will receive the new window
2636 # $widthPixels, $heightPixels
2637 # - The provisional size of the window (which may be changed by this
2638 # function)
2639 #
2640 # Optional arguments
2641 # $zoneObj - The GA::Obj::Zone, if already known (otherwise 'undef')
2642 # $layer - The layer in which to place the window specified by the function which
2643 # called $self->createGridWin. If no size was specified, set to
2644 # 'undef'
2645 # $xPosPixels, $yPosPixels
2646 # - The size of the window specified by the function which called
2647 # $self->createGridWin. If no size was specified, both set to 'undef'
2648 #
2649 # Return values
2650 # An empty list of improper arguments or if there is no room in this zone for the window
2651 # Otherwise, returns the window's size, position and layer in a list:
2652 # ($layer, $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $xPosPixels,
2653 # $yPosPixels, $widthPixels, $heightPixels)
2654  
2655 my (
2656 $self, $complainant, $winType, $zoneSpecifiedFlag, $workspaceGridObj, $widthPixels,
2657 $heightPixels, $zoneObj, $layer, $xPosPixels, $yPosPixels, $check
2658 ) = @_;
2659  
2660 # Local variables
2661 my (
2662 $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $successFlag,
2663 @emptyList,
2664 );
2665  
2666 # Check for improper arguments
2667 if (
2668 ! defined $complainant || ! defined $winType || ! defined $zoneSpecifiedFlag
2669 || ! defined $workspaceGridObj || ! defined $widthPixels || ! defined $heightPixels
2670 || defined $check
2671 ) {
2672 $axmud::CLIENT->writeImproper($self->_objClass . '->chooseWinPosn', @_);
2673 return @emptyList;
2674 }
2675  
2676 # If $zone was specified when this function was called, and either or both or $xPosPixels /
2677 # $yPosPixels were specified, this function must ignore them
2678 if ($zoneSpecifiedFlag) {
2679  
2680 $xPosPixels = undef;
2681 $yPosPixels = undef;
2682 }
2683  
2684 # Before we decide where exactly in the zone to put the window, we give it a provisional
2685 # position in one of the zone's corners (by default the top-left corner). If that region
2686 # of the zone happens to be free, the window will be put there permanently; otherwise it
2687 # will be moved to some other free part of the zone later in this function
2688 # However, if either or both of $xPosPixels / $yPosPixels are still specified, we use one
2689 # or both of those values as the provisional position instead
2690 ($widthBlocks, $heightBlocks, $xPosBlocks, $yPosBlocks)
2691 = $zoneObj->findProvWinPosn($widthPixels, $heightPixels, $xPosPixels, $yPosPixels);
2692  
2693 # If $layer wasn't specified, use the default layer provisionally
2694 if (! defined $layer) {
2695  
2696 $layer = $workspaceGridObj->defaultLayer;
2697  
2698 # Otherwise, check that the specified layer is valid (and use the default one if
2699 # not)
2700 } elsif (! $axmud::CLIENT->intCheck($layer, 0, ($workspaceGridObj->maxLayers - 1))) {
2701  
2702 $complainant->writeWarning(
2703 'The specified layer #' . $layer . ' is not valid, using the default layer #'
2704 . $workspaceGridObj->defaultLayer,
2705 $self->_objClass . '->chooseWinPosn',
2706 );
2707  
2708 $layer = $workspaceGridObj->defaultLayer;
2709 }
2710  
2711 # Place the window at some position within the zone (hopefully at the provisional one, on
2712 # the default layer)
2713 ($successFlag, $layer, $xPosBlocks, $yPosBlocks)
2714 = $zoneObj->placeWin($layer, $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks);
2715  
2716 if (! $successFlag) {
2717  
2718 # There is no room for the window in this zone
2719 $complainant->writeError(
2720 'Couldn\'t find room for the \'' . $winType . '\' window anywhere in zone #'
2721 . $zoneObj->number,
2722 $self->_objClass . '->chooseWinPosn',
2723 );
2724  
2725 return @emptyList;
2726 }
2727  
2728 # If the window position on the grid puts it rather close to the edge (or edges) of the
2729 # zone, and if the gaps between the window and the zone's edge are empty, adjust the size
2730 # of the window to fill the gap (this prevents small areas of the zone from always being
2731 # empty: makes the workspace look nice). If the maximum allowable gap size is 0, don't
2732 # fill gaps at all
2733 if ($axmud::CLIENT->gridGapMaxSize) {
2734  
2735 ($xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks)
2736 = $zoneObj->adjustSingleWin(
2737 $layer,
2738 $xPosBlocks,
2739 $yPosBlocks,
2740 $widthBlocks,
2741 $heightBlocks,
2742 );
2743 }
2744  
2745 # Now that we have the window's final size and position in the zone, reset the variables
2746 # that show the window's size and position, in pixels, on the workspace
2747 ($xPosPixels, $yPosPixels) = $zoneObj->getInternalGridPosn($xPosBlocks, $yPosBlocks);
2748  
2749 $widthPixels = $widthBlocks * $axmud::CLIENT->gridBlockSize;
2750 $heightPixels = $heightBlocks * $axmud::CLIENT->gridBlockSize;
2751  
2752 # If the desktop theme uses window controls, we have to take them into account, changing the
2753 # size of the window accordingly
2754 ($xPosPixels, $yPosPixels, $widthPixels, $heightPixels)
2755 = $workspaceGridObj->fineTuneWinSize(
2756 $winType,
2757 $xPosPixels,
2758 $yPosPixels,
2759 $widthPixels,
2760 $heightPixels
2761 );
2762  
2763 return (
2764 $layer, $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $xPosPixels, $yPosPixels,
2765 $widthPixels, $heightPixels,
2766 );
2767 }
2768  
2769 sub getWinmap {
2770  
2771 # Called by $self->createGridWin or $self->createSimpleGridWin
2772 # If a winmap name was specified as an argument to those functions, selects the
2773 # corresponding winmap object (if it exists)
2774 # Otherwise selects a default winmap object, or 'undef' for 'map', 'fixed' and 'external'
2775 # windows (which don't use winmaps)
2776 # 'main' windows must contain at least one winzone which creates a pane object
2777 # (GA::Table::Pane). For 'main' windows, if the selected winmap doesn't contain a pane
2778 # object, a standard winmap is used instead
2779 #
2780 # Expected arguments
2781 # $winType - The window type; one of the 'grid' window types specified by
2782 # GA::Client->constGridWinTypeHash
2783 #
2784 # Optional arguments
2785 # $winmapName - The winmap name specified as an argument in the calls to
2786 # $self->createGridWin or $self->createSimpleGridWin (may be 'undef')
2787 # $zoneObj - When called by $self->createGridWin, the GA::Obj::Zone into which the
2788 # window will be moved. When called by $self->createSimpleGridWin,
2789 # 'undef'
2790 #
2791 # Return values
2792 # 'undef' on improper arguments or if no winmap object is created
2793 # Otherwise, returns a winmap object (GA::Obj::Winmap)
2794  
2795 my ($self, $winType, $winmapName, $zoneObj, $check) = @_;
2796  
2797 # Local variables
2798 my ($winmapObj, $flag);
2799  
2800 # Check for improper arguments
2801 if (defined $check) {
2802  
2803 return $axmud::CLIENT->writeImproper($self->_objClass . '->getWinmap', @_);
2804 }
2805  
2806 # Convert a winmap name to a winmap object. If no winmap was specified, use a default one
2807 if (! defined $winType || defined $winmapName) {
2808  
2809 $winmapObj = $axmud::CLIENT->ivShow('winmapHash', $winmapName);
2810 }
2811  
2812 if (! $winmapObj) {
2813  
2814 # No winmap name specified, or winmap doesn't exist, so use a default winmap
2815 if ($winType eq 'main') {
2816  
2817 if ($self->gridEnableFlag && $axmud::CLIENT->activateGridFlag) {
2818  
2819 if ($zoneObj && defined $zoneObj->defaultEnabledWinmap) {
2820  
2821 $winmapObj
2822 = $axmud::CLIENT->ivShow('winmapHash', $zoneObj->defaultEnabledWinmap);
2823 }
2824  
2825 if (! $winmapObj) {
2826  
2827 $winmapObj = $axmud::CLIENT->ivShow(
2828 'winmapHash',
2829 $axmud::CLIENT->defaultEnabledWinmap,
2830 );
2831 }
2832  
2833 } else {
2834  
2835 if ($zoneObj && defined $zoneObj->defaultDisabledWinmap) {
2836  
2837 $winmapObj
2838 = $axmud::CLIENT->ivShow('winmapHash', $zoneObj->defaultDisabledWinmap);
2839 }
2840  
2841 if (! $winmapObj) {
2842  
2843 $winmapObj = $axmud::CLIENT->ivShow(
2844 'winmapHash',
2845 $axmud::CLIENT->defaultDisabledWinmap,
2846 );
2847 }
2848 }
2849  
2850 } elsif ($winType eq 'protocol' || $winType eq 'custom') {
2851  
2852 if ($zoneObj && defined $zoneObj->defaultInternalWinmap) {
2853  
2854 $winmapObj
2855 = $axmud::CLIENT->ivShow('winmapHash', $zoneObj->defaultInternalWinmap);
2856 }
2857  
2858 if (! $winmapObj) {
2859  
2860 $winmapObj = $axmud::CLIENT->ivShow(
2861 'winmapHash',
2862 $axmud::CLIENT->defaultInternalWinmap,
2863 );
2864 }
2865 }
2866 }
2867  
2868 # For 'main' windows, check we're going to get at least one pane object
2869 # (Exception: 'main_wait' deliberately contains no pane objects)
2870 if (
2871 $winType eq 'main'
2872 && (! $winmapName || $winmapName ne 'main_wait')
2873 ) {
2874 OUTER: foreach my $winzoneObj ($winmapObj->ivValues('zoneHash')) {
2875  
2876 if ($winzoneObj->packageName eq 'Games::Axmud::Table::Pane') {
2877  
2878 $flag = TRUE;
2879 last OUTER;
2880 }
2881 }
2882  
2883 if (! $flag) {
2884  
2885 # Use a standard winmap instead
2886 if ($self->gridEnableFlag && $axmud::CLIENT->activateGridFlag) {
2887  
2888 $winmapObj = $axmud::CLIENT->ivShow(
2889 'winmapHash',
2890 $axmud::CLIENT->constDefaultEnabledWinmap,
2891 );
2892  
2893 } else {
2894  
2895 $winmapObj = $axmud::CLIENT->ivShow(
2896 'winmapHash',
2897 $axmud::CLIENT->constDefaultDisabledWinmap,
2898 );
2899 }
2900 }
2901 }
2902  
2903 # Operation complete
2904 return $winmapObj;
2905 }
2906  
2907 ##################
2908 # Accessors - set
2909  
2910 sub set_defaultZonemap {
2911  
2912 # Called by GA::Cmd::ResetGrid->do, GA::Obj::File->extractData and
2913 # GA::WizWin::Setup->saveChanges
2914  
2915 my ($self, $obj, $check) = @_;
2916  
2917 # Check for improper arguments
2918 if (! defined $obj || defined $check) {
2919  
2920 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_defaultZonemap', @_);
2921 }
2922  
2923 # Temporary zonemaps (created by MXP) can't be used as a workspace's default zonemap
2924 if ($obj->tempFlag) {
2925  
2926 return undef;
2927  
2928 } else {
2929  
2930 $self->ivPoke('defaultZonemap', $obj->name);
2931 }
2932  
2933 return 1;
2934 }
2935  
2936 sub reset_defaultZonemap {
2937  
2938 # Called by GA::WizWin::Setup->saveChanges
2939  
2940 my ($self, $check) = @_;
2941  
2942 # Check for improper arguments
2943 if (defined $check) {
2944  
2945 return $axmud::CLIENT->writeImproper($self->_objClass . '->reset_defaultZonemap', @_);
2946 }
2947  
2948 $self->ivPoke('defaultZonemap', undef);
2949  
2950 return 1;
2951 }
2952  
2953 ##################
2954 # Accessors - get
2955  
2956 sub number
2957 { $_[0]->{number} }
2958 sub systemNum
2959 { $_[0]->{systemNum} }
2960  
2961 sub currentWidth
2962 { $_[0]->{currentWidth} }
2963 sub currentHeight
2964 { $_[0]->{currentHeight} }
2965 sub panelLeftSize
2966 { $_[0]->{panelLeftSize} }
2967 sub panelRightSize
2968 { $_[0]->{panelRightSize} }
2969 sub panelTopSize
2970 { $_[0]->{panelTopSize} }
2971 sub panelBottomSize
2972 { $_[0]->{panelBottomSize} }
2973 sub controlsLeftSize
2974 { $_[0]->{controlsLeftSize} }
2975 sub controlsRightSize
2976 { $_[0]->{controlsRightSize} }
2977 sub controlsTopSize
2978 { $_[0]->{controlsTopSize} }
2979 sub controlsBottomSize
2980 { $_[0]->{controlsBottomSize} }
2981  
2982 sub gridHash
2983 { my $self = shift; return %{$self->{gridHash}}; }
2984 sub gridEnableFlag
2985 { $_[0]->{gridEnableFlag} }
2986 sub defaultZonemap
2987 { $_[0]->{defaultZonemap} }
2988 }
2989  
2990 # Package must return a true value
2991 1