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

filter: Statementstmt 0.0% 0 / 673 Branchbran 0.0% 0 / 396 Conditioncond 0.0% 0 / 396 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 42 Podpod 0.0% 0 / 38 totaltotal 0.0% 0 / 1545 CC 199 SCAR 62.2
CC
199
Cov
0%
createGridWin
1980.0
createSimpleGridWin
600.0
getWinmap
306.0
CRAP 501.9
1 # Copyright (C) 2011-2022 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 # Store the detected sizes unless GA::Client->customControlsLeftSize (etc) are set, as
951 # those values take precedence
952 if (defined $axmud::CLIENT->customControlsLeftSize) {
953 $self->ivPoke('controlsLeftSize', $axmud::CLIENT->customControlsLeftSize);
954 } else {
955 $self->ivPoke('controlsLeftSize', $left);
956 }
957  
958 if (defined $axmud::CLIENT->customControlsRightSize) {
959 $self->ivPoke('controlsRightSize', $axmud::CLIENT->customControlsRightSize);
960 } else {
961 $self->ivPoke('controlsRightSize', $right);
962 }
963  
964 if (defined $axmud::CLIENT->customControlsTopSize) {
965 $self->ivPoke('controlsTopSize', $axmud::CLIENT->customControlsTopSize);
966 } else {
967 $self->ivPoke('controlsTopSize', $top);
968 }
969  
970 if (defined $axmud::CLIENT->customControlsBottomSize) {
971 $self->ivPoke('controlsBottomSize', $axmud::CLIENT->customControlsBottomSize);
972 } else {
973 $self->ivPoke('controlsBottomSize', $bottom);
974 }
975  
976 # Return the results of the test (not the stored controls sizes)
977 return ($left, $right, $top, $bottom);
978 }
979 }
980  
981 sub testWinControls {
982  
983 # Called by $self->findWinControlSize
984 # Creates a test window in one of two opposite corners of this workspace and, by testing its
985 # actual coordinates against its expected coordinates, finds out the size of two of the
986 # window controls used by the current desktop theme
987 #
988 # Expected arguments
989 # $gravityFlag - FALSE to use top-left corner, TRUE to use bottom-right corner
990 #
991 # Return values
992 # Returns an empty list on improper arguments or if the window's position on the desktop
993 # can't be found
994 # Otherwise returns a list in the form
995 # ($gravityFlag = FALSE) - The list (size_of_left_control, size_of_top_control)
996 # ($gravityFlag = TRUE) - The list (size_of_bottom_control, size_of_right_control)
997  
998 my ($self, $gravityFlag, $check) = @_;
999  
1000 # Local variables
1001 my (
1002 $testWinName, $testWinSize, $testWinDistance, $checkTime, $matchFlag, $regionX,
1003 $regionY, $regionWidth, $regionHeight, $clientX, $clientY,
1004 @emptyList, @nameList,
1005 );
1006  
1007 # Check for improper arguments
1008 if (! defined $gravityFlag || defined $check) {
1009  
1010 $axmud::CLIENT->writeImproper($self->_objClass . '->testWinControls', @_);
1011 return @emptyList;
1012 }
1013  
1014 # This test requires that window panels have been set in an earlier call to
1015 # $self->findPanelSize; if they haven't, then this test fails
1016 if (
1017 ! defined $self->panelLeftSize
1018 || ! defined $self->panelRightSize
1019 || ! defined $self->panelTopSize
1020 || ! defined $self->panelBottomSize
1021 ) {
1022 return @emptyList;
1023 }
1024  
1025 # Prepare the test window's title bar
1026 if (! $gravityFlag) {
1027 $testWinName = $axmud::SCRIPT . '_test_1';
1028 } else {
1029 $testWinName = $axmud::SCRIPT . '_test_2';
1030 }
1031  
1032 # Prepare the width and height of the test window in pixels
1033 $testWinSize = 100;
1034 # Set the distance from the corner in pixels
1035 $testWinDistance = 100;
1036  
1037 # Create the test window
1038 my $testWin = Gtk3::Window->new('toplevel');
1039 $testWin->set_title($testWinName);
1040 $testWin->set_border_width(0);
1041 $testWin->set_size_request($testWinSize, $testWinSize);
1042 $testWin->set_decorated(TRUE);
1043 $testWin->set_opacity(0);
1044 $testWin->set_skip_taskbar_hint(TRUE);
1045 $testWin->set_skip_pager_hint(TRUE);
1046  
1047 if (! $gravityFlag) {
1048  
1049 # Position the test window near the top-left corner
1050 $testWin->move(
1051 ($self->panelLeftSize + $testWinDistance),
1052 ($self->panelTopSize + $testWinDistance),
1053 );
1054  
1055 } else {
1056  
1057 # Position the test window near the bottom-right corner
1058 $testWin->move(
1059 ($self->currentWidth - $self->panelRightSize - $testWinDistance - $testWinSize),
1060 ($self->currentHeight - $self->panelBottomSize - $testWinDistance - $testWinSize),
1061 );
1062 }
1063  
1064 # Set the window's gravity
1065 if (! $gravityFlag) {
1066 $testWin->set_gravity('GDK_GRAVITY_NORTH_WEST');
1067 } else {
1068 $testWin->set_gravity('GDK_GRAVITY_SOUTH_EAST');
1069 }
1070  
1071 # Use standard 'dialogue' window icons
1072 $testWin->set_icon_list($axmud::CLIENT->desktopObj->{dialogueWinIconList});
1073  
1074 # Make the window visible, briefly
1075 $testWin->show_all();
1076 $axmud::CLIENT->desktopObj->updateWidgets($self->_objClass . '->testWinControls');
1077  
1078 # If using X11::WMCtrl, we can move the test window to the correct workspace (if not, there
1079 # is only one workspace in use, anyway)
1080 if ($axmud::CLIENT->desktopObj->wmCtrlObj) {
1081  
1082 # Very rarely, the call to X11::WMCtrl->get_windows() produces an unexplainable error.
1083 # This nudge seems to prevent it from happening
1084 $axmud::CLIENT->desktopObj->wmCtrlObj->get_window_manager();
1085  
1086 # In some cases, X11::WMCtrl doesn't know about the test window (despite the call to
1087 # ->updateWidgets just above). Wait for up to a second and, if X11::WMCtrl still
1088 # hasn't spotted the new window, regard the test as a failure
1089 $checkTime = $axmud::CLIENT->getTime() + 1;
1090 do {
1091  
1092 OUTER: foreach my $hashRef ($axmud::CLIENT->desktopObj->wmCtrlObj->get_windows()) {
1093  
1094 if ($$hashRef{'title'} eq $testWinName) {
1095  
1096 $matchFlag = TRUE;
1097 last OUTER;
1098 }
1099 }
1100  
1101 } until ($matchFlag || $axmud::CLIENT->getTime() > $checkTime);
1102  
1103 if (! $matchFlag) {
1104  
1105 # Test failed
1106 return @emptyList;
1107  
1108 } else {
1109  
1110 # Can move the window to its workspace without triggering an error (providing we do
1111 # a quick ->updateWidgets first)
1112 $axmud::CLIENT->desktopObj->updateWidgets($self->_objClass . '->testWinControls');
1113 $axmud::CLIENT->desktopObj->wmCtrlObj->move_to($testWinName, $self->systemNum);
1114 }
1115 }
1116  
1117 # Get the window's actual position on the desktop
1118 ($regionX, $regionY, $regionWidth, $regionHeight, $clientX, $clientY)
1119 = $self->getWinGeometry($testWin->get_window());
1120  
1121 # Destroy the test window, now that we have all the data we want
1122 $testWin->destroy();
1123  
1124 # Return the window controls sizes as a list
1125 if (! $gravityFlag) {
1126  
1127 # Return size of the left and top window controls
1128 return (
1129 ($clientX - ($self->panelLeftSize + $testWinDistance)),
1130 ($clientY - ($self->panelTopSize + $testWinDistance)),
1131 )
1132  
1133 } else {
1134  
1135 # Return size of the right and bottom window controls
1136 return (
1137 (
1138 $self->currentWidth - $self->panelRightSize - $testWinDistance - $testWinSize
1139 - $clientX
1140 ),
1141 (
1142 $self->currentHeight - $self->panelBottomSize - $testWinDistance - $testWinSize
1143 - $clientY
1144 ),
1145 )
1146 }
1147 }
1148  
1149 sub getWinGeometry {
1150  
1151 # Can be called by any function, e.g. by $self->testWinControls
1152 # Gets the actual size and position of a Gdk::Window
1153 #
1154 # Expected arguments
1155 # $gdkWin - The Gdk::Window to use
1156 #
1157 # Return values
1158 # An empty list on improper arguments or if the test fails
1159 # Otherwise a list in groups of 4, showing the size and position of the window region (the
1160 # window's visible size and position), and then the size and position of the window's
1161 # client area (i.e. not taking into account the window's title bar):
1162 # (
1163 # $regionX, $regionY, $regionWidth, $regionHeight,
1164 # $clientX, $clientY, $clientWidth, $clientHeight,
1165 # )
1166  
1167 my ($self, $gdkWin, $check) = @_;
1168  
1169 # Local variables
1170 my (
1171 $regionX, $regionY, $clientX, $clientY, $clientWidth, $clientHeight,
1172 @emptyList,
1173 );
1174  
1175 # Check for improper arguments
1176 if (! defined $gdkWin || defined $check) {
1177  
1178 $axmud::CLIENT->writeImproper($self->_objClass . '->getWinGeometry', @_);
1179 return @emptyList;
1180 }
1181  
1182 # Make sure all drawing operations are complete
1183 $axmud::CLIENT->desktopObj->updateWidgets($self->_objClass . '->getWinGeometry');
1184  
1185 # Get the position of the visible window
1186 ($regionX, $regionY) = $gdkWin->get_root_origin();
1187 # Get the position of the window's client area
1188 ($clientX, $clientY) = $gdkWin->get_origin();
1189 # Get the size of the window's client area; the size of the window region can be calculated
1190 # by adding the ttitle bar
1191 $clientWidth = $gdkWin->get_width();
1192 $clientHeight = $gdkWin->get_height();
1193  
1194 # Return the results
1195 return (
1196 $regionX,
1197 $regionY,
1198 $clientWidth + ($clientX - $regionX),
1199 $clientHeight + ($clientY - $regionY),
1200 $clientX,
1201 $clientY,
1202 $clientWidth,
1203 $clientHeight,
1204 );
1205 }
1206  
1207 sub matchWinList {
1208  
1209 # Called GA::GrabWindowCmd->do or any other code
1210 # Compares a list of patterns - e.g. ('Notepad', 'Firefox') - against a list of windows on
1211 # this workspace. Returns a list of internal IDs for the matching windows
1212 # The patterns are treated as regexes (so 'Notepad' and '^Note' are both acceptable). The
1213 # pattern match is case-insensitive
1214 #
1215 # NB This function does nothing if X11::WMCtrl is not available
1216 #
1217 # Expected arguments
1218 # $number - The number of windows to match. 1 - return only the first matching
1219 # window; 7 - return only the first 7 matching windows, 0 - return all
1220 # matching windows
1221 #
1222 # Optional arguments
1223 # @patternList - A list of patterns matching one or more window titles. If an empty
1224 # list, no matching occurs
1225 #
1226 # Return values
1227 # An empty list on improper arguments
1228 # Otherwise returns a list (which may be an empty list) in groups of 2 for each matching
1229 # window, in the form
1230 # (window_title, window_internal_id)
1231  
1232 my ($self, $number, @patternList) = @_;
1233  
1234 # Local variables
1235 my (
1236 $count,
1237 @emptyList, @returnList,
1238 );
1239  
1240 # Check for improper arguments
1241 if (! defined $number) {
1242  
1243 return $axmud::CLIENT->writeImproper($self->_objClass . '->matchWinList', @_);
1244 }
1245  
1246 # Can't do anything without X11::WMCtrl
1247 if (! $axmud::CLIENT->desktopObj->wmCtrlObj) {
1248  
1249 return @emptyList;
1250 }
1251  
1252 # Cycle through the list of windows on all workspaces, ignoring any windows that aren't on
1253 # this workspace
1254 $count = 0;
1255 OUTER: foreach my $hashRef ($axmud::CLIENT->desktopObj->wmCtrlObj->get_windows()) {
1256  
1257 my $title;
1258  
1259 if ($$hashRef{'workspace'} == $self->systemNum) {
1260  
1261 # X11::WMCtrl handles a string containing both the window's title and the hostname
1262 # of the X client drawing the window; however, when that hostname is 'N/A',
1263 # X11::WMCtrl is unable to remove it, so we'll have to do that ourselves
1264 $title = $$hashRef{'title'};
1265 $title =~ s/^\sN\/A\s//;
1266  
1267 INNER: foreach my $pattern (@patternList) {
1268  
1269 if ($title =~ m/$pattern/i) {
1270  
1271 # A matching window was found
1272 push (@returnList, $title, $$hashRef{'id'});
1273 $count++;
1274  
1275 # Do we have enough matching windows now?
1276 if ($number > 0 && $count >= $number) {
1277  
1278 # We have enough matching windows
1279 return @returnList;
1280  
1281 } else {
1282  
1283 # Look for the next matching window
1284 next OUTER;
1285 }
1286 }
1287 }
1288 }
1289 }
1290  
1291 # Operation complete
1292 return @returnList;
1293 }
1294  
1295 sub moveResizeWin {
1296  
1297 # Can be called by any function (but not used by $self->testWinControls, which calls
1298 # Gtk3::Window->move and ->resize directly)
1299 # Moves a window to a specific position on the workspace and resizes it
1300 # Calls to this function should usually be followed by a call to
1301 # GA::Obj::Zone->restackWin() and/or GA::Generic::Win->restoreFocus
1302 #
1303 # Expected arguments
1304 # $winObj - The window object (anything inheriting from GA::Generic::Win) whose
1305 # window should be moved and/or resized
1306 #
1307 # Optional arguments
1308 # $xPosPixels, $yPosPixels
1309 # - The new position of the window (both set to 'undef' if the position isn't
1310 # to be changed)
1311 # $widthPixels, $heightPixels
1312 # - The new size of the window (both set to 'undef' if the size isn't to be
1313 # changed)
1314 #
1315 # Notes
1316 # The following combinations of arguments lists are acceptable:
1317 # ($winObj, $xPosPixels, $yPosPixels, $widthPixels, $heightPixels)
1318 # ($winObj, $xPosPixels, $yPosPixels, undef, undef)
1319 # ($winObj, undef, undef, $widthPixels, $heightPixels)
1320 # ($winObj, undef, undef, undef, undef)
1321 # Any other combination, for example the following one, will cause an error
1322 # ($winObj, $xPosPixels, $yPosPixels, undef, $heightPixels)
1323 #
1324 # Return values
1325 # 'undef' on improper arguments or if the window can't be moved (because a non-external
1326 # window object doesn't have its ->winWidget set)
1327 # 1 otherwise
1328  
1329 my ($self, $winObj, $xPosPixels, $yPosPixels, $widthPixels, $heightPixels, $check) = @_;
1330  
1331 # Local variables
1332 my $title;
1333  
1334 # Check for improper arguments
1335 if (
1336 ! defined $winObj
1337 || (defined $xPosPixels && ! defined $yPosPixels)
1338 || (! defined $xPosPixels && defined $yPosPixels)
1339 || (defined $widthPixels && ! defined $heightPixels)
1340 || (! defined $widthPixels && defined $heightPixels)
1341 || defined $check
1342 ) {
1343 return $axmud::CLIENT->writeImproper($self->_objClass . '->moveResizeWin', @_);
1344 }
1345  
1346 if ($winObj->winType ne 'external') {
1347  
1348 # Move the window to the correct workspace, if possible
1349 if ($axmud::CLIENT->desktopObj->wmCtrlObj) {
1350  
1351 # Give the window a unique name, so WMCtrl can find it
1352 my $title = $winObj->winWidget->get_title();
1353 $winObj->winWidget->set_title($axmud::SCRIPT . int(rand(1_000_000_000)));
1354  
1355 # Move the window
1356 $axmud::CLIENT->desktopObj->wmCtrlObj->move_to(
1357 $winObj->winWidget->get_title(),
1358 $self->systemNum,
1359 );
1360  
1361 # Restore the original title
1362 $winObj->winWidget->set_title($title);
1363 }
1364  
1365 # Resize the window, if that was specified
1366 if (defined $widthPixels) {
1367  
1368 $winObj->winWidget->resize($widthPixels, $heightPixels);
1369 }
1370  
1371 # Move the window, if that was specified
1372 # $winObj->winWidget is occasionally set to 'undef' just before this line. It's a very
1373 # difficult error to reproduce, so I'm not sure what the cause is
1374 if ($winObj->winWidget && defined $xPosPixels) {
1375  
1376 $winObj->winWidget->move($xPosPixels, $yPosPixels);
1377 }
1378  
1379 } else {
1380  
1381 # X11::WMCtrl expects -1 values, rather than undef
1382 if (! defined $xPosPixels) {
1383  
1384 $xPosPixels = -1;
1385 $yPosPixels = -1;
1386 }
1387  
1388 if (! defined $widthPixels) {
1389  
1390 $widthPixels = -1;
1391 $heightPixels = -1;
1392 }
1393  
1394 # Move the window to the correct workspace
1395 $axmud::CLIENT->desktopObj->wmCtrlObj->wmctrl(
1396 '-r',
1397 $winObj->internalID,
1398 '-t',
1399 $self->systemNum,
1400 '-i',
1401 );
1402  
1403 # Set the window's size and position
1404 $axmud::CLIENT->desktopObj->wmCtrlObj->wmctrl(
1405 '-r',
1406 $winObj->internalID,
1407 "-e 0,$xPosPixels,$yPosPixels,$widthPixels,$heightPixels",
1408 '-i',
1409 );
1410 }
1411  
1412 # Operation complete
1413 return 1;
1414 }
1415  
1416 # Window creation
1417  
1418 sub createGridWin {
1419  
1420 # Can be called by anything
1421 #
1422 # Creates a 'grid' window object. 'external' windows use GA::Win::External objects; other
1423 # kinds of 'grid' window use GA::Win::Internal. (This function isn't used to create 'free'
1424 # windows; call GA::Generic::Win->createFreeWin for that)
1425 # If the window itself doesn't exist, creates it using the specified size and coordinates,
1426 # in the specified layer, on the workspace $self->systemNum
1427 # If the window already exists, resizes and moves it using the specified size and
1428 # coordinates
1429 # If workspace grids are available and the window's specified position is occupied by
1430 # another window, moves the specified window to another position in the zone, if possible
1431 # If workspace grids aren't available, ignores the specified coordinates (if supplied), and
1432 # lets the desktop's window manager decide where to put the window
1433 #
1434 # NB If the calling function wants the window to appear on a particular workspace only, it
1435 # can call this function directly
1436 # If the calling function wants the window to appear on the first available workspace, it
1437 # can call GA::Obj::Desktop->listWorkspaces to get an ordered list of workspaces, with
1438 # the preferred workspace (if specified) first in the list. The calling function can then
1439 # call this function for each workspace on the list until a new window object is actually
1440 # created
1441 #
1442 # NB 'main' windows should only be created by code in GA::Client or GA::Session.
1443 # 'protocol' and 'external' windows should only be created by code in GA::Session
1444 # If you write your own plugins, don't use them to create your own 'main', 'protocol' or
1445 # 'external' windows. You can create as many 'map', 'fixed' or 'custom' windows as you
1446 # like, though
1447 #
1448 # Expected arguments
1449 # $winType - The window type; one of the 'grid' window types specified by
1450 # GA::Client->constGridWinTypeHash
1451 # $winName - A name for the window:
1452 # $winType $winName
1453 # -------- --------
1454 # main main
1455 # map map
1456 # protocol Any string chosen by the protocol code (default value is
1457 # 'protocol')
1458 # fixed Any string chosen by the controlling code (default value
1459 # is 'fixed')
1460 # custom Any string chosen by the controlling code. For task
1461 # windows, the name of the task (e.g. 'status_task',
1462 # for other windows, default value is 'custom'
1463 # external The 'external' window's name (e.g. 'Notepad')
1464 #
1465 # Optional arguments
1466 # $winTitle - The text to use in the window's title bar. If 'undef', a default
1467 # window title is used
1468 # $winmapName - The name of the GA::Obj::Winmap object that specifies the
1469 # Gtk3::Window's layout when it is first created. If 'undef', a
1470 # default winmap is used. Ignored for 'map', 'fixed' and 'external'
1471 # windows which should have no winmap
1472 # $packageName - 'main', 'protocol' and 'custom' windows are created via a call to
1473 # GA::Win::Internal->new(). 'external' windows are created via a
1474 # call to GA::Win::External->new(). However, the calling function
1475 # can specify its own $packageName, if required. It's expected that
1476 # the package should inherit from GA::Win::Internal or
1477 # GA::Win::External
1478 # - 'map' windows are created via a call to GA::Win::Map->new(). If the
1479 # calling function specifies its own $packageName, it's expected that
1480 # the package either inherits from GA::Win::Map, or provides a
1481 # similar range of functions for other parts of the Axmud code to call
1482 # - $packageName must be specified when creating a 'fixed' window. If not,
1483 # an error is produced
1484 # $winWidget - The Gtk3::Window, if it already exists and it is known (otherwise
1485 # set to 'undef')
1486 # $internalID - For 'external' windows, the window internal ID, provided by
1487 # X11::WMCtrl ('undef' for other types of window)
1488 # $owner - The owner, if known. Can be any blessed reference, typically it's an
1489 # GA::Session or a task (inheriting from GA::Generic::Task). The owner
1490 # must have a ->del_winObj function, which is called when this window
1491 # closes
1492 # $session - The owner's session. If $owner is a GA::Session, that session. If
1493 # it's something else (like a task), the task's session. If $owner is
1494 # 'undef', so is $session. If $owner is GA::Client, then $session
1495 # should be 'undef'
1496 # $workspaceGrid - The number of the workspace grid to use (matches a key in
1497 # $self->gridHash). Should be set when GA::CLIENT->shareMainWinFlag
1498 # is TRUE. If 'undef' (when GA::Client->shareMainWinFlag = FALSE, or
1499 # if the calling function forgot to specify a workspace), uses the
1500 # workspace grid with the lowest number, on the assumption that it's
1501 # a single workspace grid shared by all sessions
1502 # $zone - Within the workspace grid, the zone number to use (matches a key in
1503 # GA::Obj::WorkspaceGrid->zoneHash). If 'undef' (or if
1504 # $workspaceGrid is 'undef'), this function will choose the zone. If
1505 # set, and the specified $zone is reserved for other windows, this
1506 # function will choose a different zone. Ignored if workspace grids
1507 # are not available
1508 # $layer - Which layer the window should be put in. If 'undef' and zones are
1509 # enabled, the default layer is used. Ignored if workspace grids are
1510 # not available
1511 # $xPosPixels, $yPosPixels
1512 # - The desired position of the top left-hand corner of the window in
1513 # pixels, with (0, 0) representing the top left-hand pixel on the
1514 # workspace. (If either or both is 'undef', this function will decide
1515 # the position. Ignored if $zone is specified)
1516 # $widthPixels, $heightPixels
1517 # - The desired size of the window, in pixels. (If either or both is
1518 # 'undef', this function will decide the size of the window. Ignored
1519 # if $zone is specified)
1520 # $beforeListRef, $afterListRef
1521 # - References to a list of functions in the window object. Any functions
1522 # in the first list are called just after the Gtk3::Window is created.
1523 # Any functions in the second list are called just after the
1524 # window is made visible. These functions should be used to set up
1525 # additional ->signal_connects for this type of window, if they are
1526 # required
1527 #
1528 # Return values
1529 # 'undef' on improper arguments, if the window object can't be created, or if the window
1530 # itself can't be created (when it doesn't already exist)
1531 # Blessed reference to the newly-created window object on success
1532  
1533 my (
1534 $self, $winType, $winName, $winTitle, $winmapName, $packageName, $winWidget,
1535 $internalID, $owner, $session, $workspaceGrid, $zone, $layer, $xPosPixels, $yPosPixels,
1536 $widthPixels, $heightPixels, $beforeListRef, $afterListRef, $check,
1537 ) = @_;
1538  
1539 # Local variables
1540 my (
1541 $complainant, $pluginName, $pluginObj, $zoneSpecifiedFlag, $workspaceGridObj, $zoneObj,
1542 $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $areaObj, $winmapObj, $winObj,
1543 @geometryList,
1544 );
1545  
1546 # Check for improper arguments
1547 if (! defined $winType || ! defined $winName || defined $check) {
1548  
1549 return $axmud::CLIENT->writeImproper($self->_objClass . '->createGridWin', @_);
1550 }
1551  
1552 # Any error messages (besides improper arguments errors) should be written via the
1553 # GA::Session, if there is one, or the GA::Client, if not
1554 if ($session) {
1555 $complainant = $session;
1556 } else {
1557 $complainant = $axmud::CLIENT;
1558 }
1559  
1560 # Check $winType is valid
1561 if (! $axmud::CLIENT->ivExists('constGridWinTypeHash', $winType)) {
1562  
1563 return $complainant->writeError(
1564 'Invalid \'grid\' window type \'' . $winType . '\'',
1565 $self->_objClass . '->createGridWin',
1566 );
1567  
1568 # Check that $owner and $session are (in combination) valid values
1569 } elsif (
1570 (defined $owner && ! defined $session && $owner->_objClass ne 'Games::Axmud::Client')
1571 || (defined $session && ! defined $owner)
1572 ) {
1573 return $complainant->writeError(
1574 'Invalid owner/session arguments',
1575 $self->_objClass . '->createGridWin',
1576 );
1577  
1578 # No session may open a second 'main' window
1579 } elsif ($winType eq 'main' && $session && $session->mainWin) {
1580  
1581 return $complainant->writeError(
1582 'Cannot create second \'main\' window for session #' . $session->number,
1583 $self->_objClass . '->createGridWin',
1584 );
1585  
1586  
1587 # For 'fixed' windows, we must have received a package name
1588 } elsif ($winType eq 'fixed' && ! $packageName) {
1589  
1590 return $complainant->writeError(
1591 'Cannot create \'fixed\' windows without a specified package name',
1592 $self->_objClass . '->createGridWin',
1593 );
1594 }
1595  
1596 # If a package name was specified and it was added by a plugin, check that the plugin is
1597 # enabled
1598 if ($packageName) {
1599  
1600 $pluginName = $axmud::CLIENT->ivShow('pluginGridWinHash', $packageName);
1601 if ($pluginName) {
1602  
1603 $pluginObj = $axmud::CLIENT->ivShow('pluginHash', $pluginName);
1604 if ($pluginObj && ! $pluginObj->enabledFlag) {
1605  
1606 return $complainant->writeError(
1607 'Cannot create the window because the \'' . $pluginName . '\' plugin is'
1608 . ' disabled',
1609 $self->_objClass . '->createGridWin',
1610 );
1611 }
1612 }
1613 }
1614  
1615 # Workspace grids and zones are disabled for this workspace. Axmud doesn't change the
1616 # position of any windows. Windows are left where the system's window manager puts them.
1617 # Window controls are compulsory
1618 if (! $self->gridEnableFlag || ! $axmud::CLIENT->activateGridFlag) {
1619  
1620 return $self->createSimpleGridWin(
1621 $winType, $winName, $winTitle, $winmapName, $packageName, $winWidget, $internalID,
1622 $owner, $session, $xPosPixels, $yPosPixels, $widthPixels, $heightPixels,
1623 $beforeListRef, $afterListRef,
1624 );
1625 }
1626  
1627 # Workspace grids and zones are enabled for this workspace
1628  
1629 # We need to remember if $zone was specified when this function was called, or not, because
1630 # the way that $widthPixels/$heightPixels and $xPosPixels/$yPosPixels are used depends on
1631 # it
1632 $zoneSpecifiedFlag = FALSE;
1633 if (defined $workspaceGrid && defined $zone) {
1634  
1635 $zoneSpecifiedFlag = TRUE;
1636  
1637 # Check the workspace grid and zone exist; if either do not, reset the variables so
1638 # that the next block of code can choose its own zone
1639 $workspaceGridObj = $self->ivShow('gridHash', $workspaceGrid);
1640 if ($workspaceGridObj) {
1641  
1642 $zoneObj = $workspaceGridObj->ivShow('zoneHash', $zone);
1643 }
1644  
1645 if (! $zoneObj) {
1646  
1647 # The specified zone number, $zone, doesn't exist
1648 $complainant->writeWarning(
1649 'The specified zone #' . $zone . ' in workspace grid #' . $workspaceGrid
1650 . ' doesn\'t exist',
1651 $self->_objClass . '->createGridWin',
1652 );
1653 }
1654  
1655 # Tidy up
1656 if (! $zoneObj) {
1657  
1658 $zone = undef;
1659 }
1660  
1661 if (! $workspaceGridObj) {
1662  
1663 $workspaceGrid = undef;
1664 }
1665  
1666 } elsif (defined $xPosPixels && defined $yPosPixels) {
1667  
1668 # Either the workspace grid or the zone weren't specified, but we can use
1669 # $xPosPixels / $yPosPixels to choose them
1670 if (defined $workspaceGrid) {
1671  
1672 # Use $xPosPixels/$yPosPixels to choose a zone on the specified workspace
1673 $workspaceGridObj = $self->ivShow('gridHash', $workspaceGrid);
1674  
1675 } else {
1676  
1677 # Use $xPosPixels/$yPosPixels to choose a zone in the correct workspace grid (the
1678 # one controlled by $session)
1679 $workspaceGridObj = $self->findWorkspaceGrid($session);
1680 if ($workspaceGridObj) {
1681  
1682 $workspaceGrid = $workspaceGridObj->number;
1683 }
1684 }
1685  
1686 if ($workspaceGridObj) {
1687  
1688 $zoneObj = $workspaceGridObj->findZone($xPosPixels, $yPosPixels);
1689 if ($zoneObj) {
1690  
1691 $zone = $zoneObj->number;
1692  
1693 # If this zone is reserved for certain types of windows, not including this kind
1694 # of window, don't use the specified zone
1695 if (! $zoneObj->checkWinAllowed($winType, $winName, $session)) {
1696  
1697 # Window not allowed in this zone
1698 $complainant->writeWarning(
1699 '\'' . $winType . '\' windows aren\'t allowed in zone #' . $zone,
1700 $self->_objClass . '->createGridWin',
1701 );
1702  
1703 $zoneObj = undef;
1704 }
1705 }
1706 }
1707  
1708 # Tidy up
1709 if (! $zoneObj) {
1710  
1711 $zone = undef;
1712 }
1713  
1714 if (! $workspaceGridObj) {
1715  
1716 $workspaceGrid = undef;
1717 }
1718 }
1719  
1720 # Now, if the zone still hasn't been set, decide which zone to use
1721 if (! defined $zone) {
1722  
1723 # If no workspace grid is yet specified, choose one (the one controlled by $session)
1724 if (! defined $workspaceGridObj) {
1725  
1726 $workspaceGridObj = $self->findWorkspaceGrid($session);
1727 if (! $workspaceGridObj) {
1728  
1729 return $complainant->writeWarning(
1730 'Cannot find a workspace grid onto which window \'' . $winName
1731 . '\' can be placed',
1732 $self->_objClass . '->createGridWin',
1733 );
1734  
1735 } else {
1736  
1737 $workspaceGrid = $workspaceGridObj->number;
1738 }
1739 }
1740  
1741 # Check that the workspace grid actually contains some zones
1742 if (! $workspaceGridObj->zoneHash) {
1743  
1744 return $complainant->writeWarning(
1745 'There are no zones on the workspace grid in which to put the window \''
1746 . $winName . '\', window was not allocated to a zone',
1747 $self->_objClass . '->createGridWin',
1748 );
1749  
1750 } else {
1751  
1752 $zoneObj = $self->chooseZone(
1753 $workspaceGridObj,
1754 $winType,
1755 $winName,
1756 $winWidget,
1757 $internalID,
1758 $owner,
1759 $session,
1760 );
1761  
1762 if (! $zoneObj) {
1763  
1764 # All available zones are full. Error message has been displayed
1765 return undef;
1766  
1767 } else {
1768  
1769 $zone = $zoneObj->number;
1770 }
1771 }
1772 }
1773  
1774 # If $zone wasn't specified when this function was called and one (but not both) of
1775 # $widthPixels / $heightPixels weren't specified, use default values for the width and
1776 # height
1777 # Exception: if this window is a 'main' window and it's the first window ever created,
1778 # assume that the Axmud is still setting up. If there is more than one zone, set the
1779 # 'main' window to fill the zone. If there is only one zone, use the default 'main' window
1780 # size
1781 ($widthPixels, $heightPixels) = $self->chooseWinSize(
1782 $winType,
1783 $workspaceGridObj,
1784 $zoneObj,
1785 $zoneSpecifiedFlag,
1786 $widthPixels,
1787 $heightPixels,
1788 );
1789  
1790 # Choose the exact size and position of the window inside its zone, taking into account such
1791 # factors as other windows in the zone
1792 (
1793 $layer, $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $xPosPixels, $yPosPixels,
1794 $widthPixels, $heightPixels,
1795 ) = $self->chooseWinPosn(
1796 $complainant,
1797 $winType,
1798 $zoneSpecifiedFlag,
1799 $workspaceGridObj,
1800 $widthPixels,
1801 $heightPixels,
1802 $zoneObj,
1803 $layer,
1804 $xPosPixels,
1805 $yPosPixels,
1806 );
1807  
1808 if (! defined $layer) {
1809  
1810 # Zone is full
1811 return undef;
1812 }
1813  
1814 # Create a GA::Obj::Area within the zone (which handles the new window, and is the same
1815 # size as it)
1816 $areaObj = $zoneObj->addArea(
1817 $layer,
1818 $xPosBlocks,
1819 $yPosBlocks,
1820 $widthBlocks,
1821 $heightBlocks,
1822 $xPosPixels,
1823 $yPosPixels,
1824 $widthPixels,
1825 $heightPixels,
1826 $session,
1827 );
1828  
1829 if (! $areaObj) {
1830  
1831 # Checks failed
1832 return undef;
1833 }
1834  
1835 # Convert a winmap name to a winmap object. If no winmap was specified, use a default one
1836 $winmapObj = $self->getWinmap($winType, $winmapName, $zoneObj);
1837 # We need to specify a winmap name or 'undef' in the call to ->new
1838 if ($winmapObj) {
1839 $winmapName = $winmapObj->name;
1840 } else {
1841 $winmapName = undef;
1842 }
1843  
1844 # Create a new window object
1845 if (! defined $packageName) {
1846  
1847 if ($winType eq 'external') {
1848  
1849 $packageName = 'Games::Axmud::Win::External';
1850  
1851 } elsif ($winType eq 'map') {
1852  
1853 $packageName = 'Games::Axmud::Win::Map';
1854  
1855 } else {
1856  
1857 # $winType is 'main', 'protocol', 'custom'
1858 $packageName = 'Games::Axmud::Win::Internal';
1859 }
1860 }
1861  
1862 $winObj = $packageName->new(
1863 $axmud::CLIENT->desktopObj->gridWinCount,
1864 $winType,
1865 $winName,
1866 $self,
1867 $owner,
1868 $session,
1869 $workspaceGridObj,
1870 $areaObj,
1871 $winmapName,
1872 );
1873  
1874 # (The check for ->winType makes sure that plugins don't create, for example, a 'free'
1875 # window and then try to add it via a call to GA::Client->createGridWin)
1876 if (! $winObj || $winObj->winCategory ne 'grid' || $winObj->winType ne $winType) {
1877  
1878 # Something or other failed. Update the zone
1879 $zoneObj->removeArea($areaObj);
1880  
1881 return undef;
1882 }
1883  
1884 # Update the GA::Obj::Desktop object's registry of all 'grid' windows
1885 $axmud::CLIENT->desktopObj->add_gridWin($winObj);
1886 # Tell the workspace grid object and area object they have received a new window
1887 $workspaceGridObj->add_gridWin($winObj);
1888 $areaObj->set_win($winObj);
1889  
1890 # Move the window to its correct workspace, size and position
1891 if ($winType ne 'external') {
1892  
1893 # Create a new Gtk3::Window widget at the specified size and position (but don't make
1894 # it visible yet)
1895 if (! $winObj->winSetup($winTitle, $beforeListRef)) {
1896  
1897 # Something or other failed. Update the zone
1898 $zoneObj->removeArea($areaObj);
1899  
1900 return undef;
1901 }
1902  
1903 # Make the window actually visible, if it's not already visible
1904 if (! $winObj->enabledFlag) {
1905  
1906 $winObj->winEnable($afterListRef);
1907 }
1908  
1909 # Move the window to its correct workspace, size and position
1910 $self->moveResizeWin(
1911 $winObj,
1912 $xPosPixels,
1913 $yPosPixels,
1914 $widthPixels,
1915 $heightPixels,
1916 );
1917  
1918 # # For 'grid' windows, $winObj->winEnable created the window in a minimised state so it
1919 # # didn't appear to jump around on the desktop. Unminimise it now
1920 # $winObj->unminimise();
1921  
1922 # Update the GA::Client's hash of stored window positions (if required)
1923 if ($winObj->winWidget) {
1924  
1925 $axmud::CLIENT->add_storeGridPosn(
1926 $winObj,
1927 $winObj->winWidget->get_position(),
1928 $winObj->winWidget->get_size(),
1929 );
1930 }
1931  
1932 } else {
1933  
1934 # Update the 'external' window object's IVs
1935 if (! $winObj->winSetup($internalID)) {
1936  
1937 # Something or other failed. Update the zone
1938 $zoneObj->removeArea($areaObj);
1939  
1940 return undef;
1941 }
1942  
1943 $self->moveResizeWin(
1944 $winObj,
1945 ($xPosPixels - $self->controlsLeftSize),
1946 ($yPosPixels - $self->controlsTopSize),
1947 $widthPixels,
1948 $heightPixels,
1949 );
1950  
1951 # Make the window actually visible, if it's not already visible
1952 if (! $winObj->enabledFlag) {
1953  
1954 $winObj->winEnable($afterListRef);
1955 }
1956  
1957 # If this 'external' window has already been grabbed to the workspace grid with
1958 # ';grabwindow' and then removed from it with ';banishwindow', it will be minimised.
1959 # In any case, if the window is minimised, it should be unminimised before being
1960 # restacked
1961 $winObj->unminimise();
1962 }
1963  
1964 # The workspace grid's new current layer is the layer in which this window will be
1965 # placed (so that it's visible to the user immediately)
1966 $workspaceGridObj->set_currentLayer($layer);
1967  
1968 # Make sure all windows on the workspace grid are stacked correctly, so that windows in
1969 # lower layers are beneath windows in higher layers (but windows in a layer higher than
1970 # the workspace grid's current layer are minimised)
1971 $zoneObj->restackWin();
1972  
1973 # GA::Obj::Zone->adjustSingleWin is called above to fill in small gaps in the zone. Now,
1974 # if the zone is wide enough (and its orientation is 'horizontal') or high enough (and its
1975 # orientation is 'vertical') for only one area of the zone's default size, then consider
1976 # expanding all of the areas in the zone to fill gaps that are bigger than a gridblock
1977 # or two, but still not big enough to hold another area
1978 # If it would be better to reduce all the area sizes in order to make room for another
1979 # area, some time in the future, do that instead. Leave enough room for an area of the
1980 # default size
1981 # If the global flag isn't set allowing these adjustments, or if the zone has a maximum of 1
1982 # area, don't make any adjustments
1983 if (
1984 $axmud::CLIENT->gridAdjustmentFlag
1985 && $zoneObj->areaMax != 1
1986 && (
1987 # Windows are stacked horizontally, and there's only room for one (default size)
1988 # window in each row
1989 (
1990 $zoneObj->orientation eq 'horizontal'
1991 && $zoneObj->heightBlocks >= $zoneObj->defaultAreaHeight
1992 && $zoneObj->heightBlocks < ($zoneObj->defaultAreaHeight * 2)
1993 # Windows are stacked vertically, and there's only room for one (default size)
1994 # window in each column
1995 ) || (
1996 $zoneObj->orientation eq 'vertical'
1997 && $zoneObj->widthBlocks >= $zoneObj->defaultAreaWidth
1998 && $zoneObj->widthBlocks < ($zoneObj->defaultAreaWidth * 2)
1999 )
2000 )
2001 ) {
2002 $zoneObj->adjustMultipleWin($layer);
2003 }
2004  
2005 # If a GA::Session was specified, restore focus to its 'main' window
2006 if ($session && $session->mainWin) {
2007  
2008 $session->mainWin->restoreFocus();
2009 }
2010  
2011 # Return the blessed reference of the new window object to show success
2012 return $winObj;
2013 }
2014  
2015 sub createSimpleGridWin {
2016  
2017 # Called by $self->createGridWin if workspace grids aren't available for this workspace,
2018 # when we don't have to bother with trying to make the window fit in a particular position
2019 # on the workspace
2020 # (When workspace grids are available, $self->createGridWin does the job itself)
2021 #
2022 # Also called by GA::Obj::Desktop->start to create the spare 'main' window when Axmud first
2023 # runs (and before the Connections window opens)
2024 #
2025 # Expected arguments
2026 # $winType - The window type; one of the 'grid' window types specified by
2027 # GA::Client->constGridWinTypeHash
2028 # $winName - A name for the window:
2029 # $winType $winName
2030 # -------- --------
2031 # main main
2032 # map map
2033 # protocol Any string chosen by the protocol code (default value is
2034 # 'protocol')
2035 # fixed Any string chosen by the controlling code (default value
2036 # is 'fixed')
2037 # custom Any string chosen by the controlling code. For task
2038 # windows, the name of the task (e.g. 'status_task',
2039 # for other windows, default value is 'custom'
2040 # external The 'external' window's name (e.g. 'Notepad')
2041 #
2042 # Optional arguments
2043 # $winTitle - The text to use in the window's title bar. If 'undef', a default
2044 # window title is used
2045 # $winmapName - The name of the GA::Obj::Winmap object that specifies the
2046 # Gtk3::Window's layout when it is first created. If 'undef', a
2047 # default winmap is used. Ignored for 'map', 'fixed' and 'external'
2048 # windows which should have no winmap
2049 # $packageName - 'main', 'protocol' and 'custom' windows are created via a call to
2050 # GA::Win::Internal->new(). 'external' windows are created via a
2051 # call to GA::Win::External->new(). However, the calling function
2052 # can specify its own $packageName, if required. It's expected that
2053 # the package should inherit from GA::Win::Internal or
2054 # GA::Win::External
2055 # - 'map' windows are created via a call to GA::Win::Map->new(). If the
2056 # calling function specifies its own $packageName, it's expected that
2057 # the package either inherits from GA::Win::Map, or provides a
2058 # similar range of functions for other parts of the Axmud code to call
2059 # - $packageName must be specified when creating a 'fixed' window. If not,
2060 # an error is produced
2061 # $winWidget - The Gtk3::Window, if it already exists and it is known (otherwise
2062 # set to 'undef')
2063 # $internalID - For 'external' windows, the window internal ID, provided by
2064 # X11::WMCtrl ('undef' for other types of window)
2065 # $owner - The owner, if known. Can be any blessed reference, typically it's an
2066 # GA::Session or a task (inheriting from GA::Generic::Task). The owner
2067 # must have a ->del_winObj function, which is called when this window
2068 # closes
2069 # $session - The owner's session. If $owner is a GA::Session, that session. If
2070 # it's something else (like a task), the task's session. If $owner is
2071 # 'undef', so is $session. If $owner is GA::Client, then $session
2072 # should be 'undef'
2073 # $xPosPixels, $yPosPixels
2074 # - The desired position of the top left-hand corner of the window in
2075 # pixels, with (0, 0) representing the top left-hand pixel on the
2076 # workspace. (If either or both is 'undef', this function will decide
2077 # the position. Ignored if $zone is specified)
2078 # $widthPixels, $heightPixels
2079 # - The desired size of the window, in pixels. (If either or both is
2080 # 'undef', this function will decide the size of the window)
2081 # $beforeListRef, $afterListRef
2082 # - References to a list of functions in the window object. Any functions
2083 # in the first list are called just after the Gtk3::Window is created.
2084 # Any functions in the second list are called just after the
2085 # window is made visible. These functions should be used to set up
2086 # additional ->signal_connects for this type of window, if they are
2087 # required
2088 #
2089 # Return values
2090 # 'undef' on improper arguments, if the window object can't be created, or if the window
2091 # itself can't be created (when it doesn't already exist)
2092 # Blessed reference to the newly-created window object on success
2093  
2094 my (
2095 $self, $winType, $winName, $winTitle, $winmapName, $packageName, $winWidget,
2096 $internalID, $owner, $session, $xPosPixels, $yPosPixels, $widthPixels, $heightPixels,
2097 $beforeListRef, $afterListRef, $check,
2098 ) = @_;
2099  
2100 # Local variables
2101 my (
2102 $listRef, $winmapObj, $winObj,
2103 @geometryList,
2104 );
2105  
2106 # Check for improper arguments
2107 if (! defined $winType || ! defined $winName || defined $check) {
2108  
2109 return $axmud::CLIENT->writeImproper($self->_objClass . '->createSimpleGridWin', @_);
2110 }
2111  
2112 # If the GA::Client IV stores a position/size for a window with this $winName, we might be
2113 # able to use it (but never for 'external' windows)
2114 $listRef = $axmud::CLIENT->ivShow('storeGridPosnHash', $winName);
2115 if ($winType ne 'external' && defined $listRef) {
2116  
2117 # $listRef is in the form (x y wid hei)
2118 if (
2119 ! defined $xPosPixels
2120 && ! defined $yPosPixels
2121 && defined $$listRef[0]
2122 && defined $$listRef[1]
2123 ) {
2124 $xPosPixels = $$listRef[0];
2125 $yPosPixels = $$listRef[1];
2126 }
2127  
2128 if (
2129 ! defined $widthPixels
2130 && ! defined $heightPixels
2131 && defined $$listRef[2]
2132 && defined $$listRef[3]
2133 ) {
2134 $widthPixels = $$listRef[2];
2135 $heightPixels = $$listRef[3];
2136 }
2137 }
2138  
2139 # If no size is still not specified, use a default size (and let the system's window manager
2140 # set its own position)
2141 if (! defined $widthPixels || ! defined $heightPixels) {
2142  
2143 if ($winType eq 'main') {
2144  
2145 $widthPixels = $axmud::CLIENT->customMainWinWidth;
2146 $heightPixels = $axmud::CLIENT->customMainWinHeight;
2147  
2148 } else {
2149  
2150 $widthPixels = $axmud::CLIENT->customGridWinWidth;
2151 $heightPixels = $axmud::CLIENT->customGridWinHeight;
2152 }
2153 }
2154  
2155 # Convert a winmap name to a winmap object. If no winmap was specified, use a default one
2156 $winmapObj = $self->getWinmap($winType, $winmapName);
2157 if (! defined $winmapObj) {
2158  
2159 # No winmap name specified, or winmap doesn't exist, so use a default winmap
2160 if ($winType eq 'main') {
2161  
2162 if ($self->gridEnableFlag && $axmud::CLIENT->activateGridFlag) {
2163  
2164 $winmapObj = $axmud::CLIENT->ivShow(
2165 'winmapHash',
2166 $axmud::CLIENT->defaultEnabledWinmap,
2167 );
2168  
2169 } else {
2170  
2171 $winmapObj = $axmud::CLIENT->ivShow(
2172 'winmapHash',
2173 $axmud::CLIENT->defaultDisabledWinmap,
2174 );
2175 }
2176  
2177 } elsif ($winType ne 'external') {
2178  
2179 $winmapObj
2180 = $axmud::CLIENT->ivShow('winmapHash', $axmud::CLIENT->defaultInternalWinmap);
2181 }
2182 }
2183  
2184 # We need to specify a winmap name or 'undef' in the call to ->new
2185 if ($winmapObj) {
2186 $winmapName = $winmapObj->name;
2187 } else {
2188 $winmapName = undef;
2189 }
2190  
2191 # Create a new window object
2192 if (! defined $packageName) {
2193  
2194 if ($winType eq 'external') {
2195  
2196 $packageName = 'Games::Axmud::Win::External';
2197  
2198 } elsif ($winType eq 'map') {
2199  
2200 $packageName = 'Games::Axmud::Win::Map';
2201  
2202 } else {
2203  
2204 # $winType is 'main', 'protocol', 'custom'
2205 $packageName = 'Games::Axmud::Win::Internal';
2206 }
2207 }
2208  
2209 $winObj = $packageName->new(
2210 $axmud::CLIENT->desktopObj->gridWinCount,
2211 $winType,
2212 $winName,
2213 $self,
2214 $owner,
2215 $session,
2216 undef,
2217 undef,
2218 $winmapName,
2219 );
2220  
2221 # (The check for ->winType makes sure that plugins don't create, for example, a 'free'
2222 # window and then try to add it via a call to GA::Client->createGridWin)
2223 if (! $winObj || $winObj->winCategory ne 'grid' || $winObj->winType ne $winType) {
2224  
2225 # Something or other failed
2226 return undef;
2227 }
2228  
2229 # Update the GA::Obj::Desktop object's registry of all 'grid' windows
2230 $axmud::CLIENT->desktopObj->add_gridWin($winObj);
2231  
2232 # Move the window to its correct workspace, size and position
2233 if ($winType ne 'external') {
2234  
2235 # Create a new Gtk3::Window widget (but don't make it visible yet)
2236 if (! $winObj->winSetup($winTitle, $beforeListRef)) {
2237  
2238 # Something or other failed
2239 return undef;
2240 }
2241  
2242 # If a size and/or position were specified, move the window
2243 if (
2244 (defined $xPosPixels && defined $yPosPixels)
2245 || (defined $widthPixels && defined $heightPixels)
2246 ) {
2247 $self->moveResizeWin(
2248 $winObj,
2249 $xPosPixels,
2250 $yPosPixels,
2251 $widthPixels,
2252 $heightPixels,
2253 );
2254 }
2255  
2256 # Make the window actually visible, if it's not already visible
2257 if (! $winObj->enabledFlag) {
2258  
2259 $winObj->winEnable($afterListRef);
2260 }
2261  
2262 # Update the GA::Client's hash of stored window positions (if required)
2263 if ($winObj->winWidget) {
2264  
2265 $axmud::CLIENT->add_storeGridPosn(
2266 $winObj,
2267 $winObj->winWidget->get_position(),
2268 $winObj->winWidget->get_size(),
2269 );
2270 }
2271  
2272 } else {
2273  
2274 # Update the 'external' window object's IVs
2275 if (! $winObj->winSetup($internalID)) {
2276  
2277 # Something or other failed
2278 return undef;
2279 }
2280  
2281 # If a size and/or position were specified, move the window
2282 if (
2283 (defined $xPosPixels && defined $yPosPixels)
2284 || (defined $widthPixels && defined $heightPixels)
2285 ) {
2286 $self->moveResizeWin(
2287 $winObj,
2288 ($xPosPixels - $self->controlsLeftSize),
2289 ($yPosPixels - $self->controlsTopSize),
2290 $widthPixels,
2291 $heightPixels,
2292 );
2293 }
2294  
2295 # Make the window actually visible, if it's not already visible
2296 if (! $winObj->enabledFlag) {
2297  
2298 $winObj->winEnable($afterListRef);
2299 }
2300  
2301 # If this 'external' window has already been grabbed to the workspace grid with
2302 # ';grabwindow' and then removed from it with ';banishwindow', it will be minimised.
2303 # In any case, if the window is minimised, it should be unminimised before being
2304 # restacked
2305 $axmud::CLIENT->desktopObj->wmCtrlObj->unminimize($internalID);
2306 }
2307  
2308 # Windows created via this function always have the focus (unlike those created by calls to
2309 # $self->createGridWin, in which the 'main' windows is always given focus)
2310 $winObj->restoreFocus();
2311  
2312 # Return the blessed reference of the new window object to show success
2313 return $winObj;
2314 }
2315  
2316 sub chooseZone {
2317  
2318 # Called by $self->createGridWin, when that function is asked to create a window in an
2319 # unspecified zone
2320 # Also called by GA::Session->setMainWin when there are no sessions, to move the spare
2321 # 'main' window into the first available zone
2322 #
2323 # Decides which zone in the specified workspace grid to place the window. If it's not
2324 # possible to place the window in any of them, returns 'undef'
2325 #
2326 # Expected arguments
2327 # $workspaceGridObj
2328 # - The GA::Obj::WorkspaceGrid object which will receive the new window
2329 # $winType - The window type; one of the 'grid' window types specified by
2330 # GA::Client->constGridWinTypeHash
2331 # $winName - The window name
2332 # $winType $winName
2333 # -------- --------
2334 # main main
2335 # map map
2336 # protocol Any string chosen by the protocol code (default value is
2337 # 'protocol')
2338 # fixed Any string chosen by the controlling code (default value
2339 # is 'fixed')
2340 # custom Any string chosen by the controlling code. For task
2341 # windows, the name of the task (e.g. 'status_task',
2342 # for other windows, default value is 'custom'
2343 # external The 'external' window's name (e.g. 'Notepad')
2344 #
2345 # Optional arguments
2346 # $winWidget - The Gtk3::Window, if it already exists and it is known (otherwise set
2347 # to 'undef')
2348 # $internalID - For 'external' windows, the window internal ID, provided by
2349 # X11::WMCtrl ('undef' for other types of window)
2350 # $owner - The owner, if known. Can be any blessed reference, typically it's an
2351 # GA::Session or a task (inheriting from GA::Generic::Task)
2352 # $session - The owner's session. If $owner is a GA::Session, that session. If
2353 # it's something else (like a task), the task's session. If $owner is
2354 # 'undef', so is $session
2355 #
2356 # Return values
2357 # 'undef' on improper arguments or if no zone is available for this window
2358 # Otherwise, returns the chosen GA::Obj::Zone
2359  
2360 my (
2361 $self, $workspaceGridObj, $winType, $winName, $winWidget, $internalID, $owner, $session,
2362 $check,
2363 ) = @_;
2364  
2365 # Local variables
2366 my @list;
2367  
2368 # Check for improper arguments
2369 if (
2370 ! defined $workspaceGridObj || ! defined $winType || ! defined $winName
2371 || defined $check
2372 ) {
2373 return $axmud::CLIENT->writeImproper($self->_objClass . '->chooseZone', @_);
2374 }
2375  
2376 # Choose zones in the following order:
2377 # 1. If it's a zone reserved for this window or for this window's session, choose it
2378 # 2. If it's an unreserved/unowned zone and the current layer isn't full, use it
2379 # 3. If it's an unreserved/unowned zone and any of its layers aren't full, use it
2380 @list = sort {$a->number <=> $b->number} ($workspaceGridObj->ivValues('zoneHash'));
2381  
2382 # 1. If it's a zone reserved for this window or for this window's session, choose it
2383 foreach my $zoneObj (@list) {
2384  
2385 my $areaCount;
2386  
2387 if ($zoneObj->reservedFlag || $zoneObj->owner) {
2388  
2389 # Is this window allowed in this zone, and if the zone has a maximum number of
2390 # areas, is there room for another area in the zone? (A GA::Obj::Area handles
2391 # the part of the zone's internal grid occupied by a single window)
2392 if ($zoneObj->checkWinAllowed($winType, $winName, $session)) {
2393  
2394 # The window is allowed in this zone, but is the zone already full?
2395 $areaCount = $zoneObj->ivPairs('areaHash');
2396 if (
2397 ($zoneObj->areaMax && $areaCount < $zoneObj->areaMax)
2398 || (! $zoneObj->areaMax)
2399 ) {
2400 # The window is allowed in this zone
2401 return $zoneObj;
2402 }
2403 }
2404 }
2405 }
2406  
2407 # 2. If it's an unreserved zone and the current layer isn't full, use it
2408 foreach my $zoneObj (@list) {
2409  
2410 my $areaCount;
2411  
2412 if (
2413 ! $zoneObj->reservedFlag
2414 && ! $zoneObj->owner
2415 && ($zoneObj->visibleAreaMax || ! $zoneObj->multipleLayerFlag)
2416 ) {
2417 # This is a zone with a maximum number of areas specified per layer. Is there room
2418 # for another area in the current layer?
2419 if ($zoneObj->checkWinAllowed($winType, $winName, $session)) {
2420  
2421 # Count the number of areas in the current layer
2422 $areaCount = 0;
2423 foreach my $areaObj ($zoneObj->ivValues('areaHash')) {
2424  
2425 if ($areaObj->layer == $workspaceGridObj->currentLayer) {
2426  
2427 $areaCount++;
2428 }
2429 }
2430  
2431 if (
2432 ($zoneObj->visibleAreaMax && $areaCount < $zoneObj->visibleAreaMax)
2433 || (! $zoneObj->multipleLayerFlag && $areaCount < $zoneObj->areaMax)
2434 ) {
2435 # Window allowed; use this zone
2436 return $zoneObj;
2437 }
2438 }
2439 }
2440 }
2441  
2442 # 3. If it's an unreserved zone and any of its layers aren't full, use it
2443 foreach my $zoneObj (@list) {
2444  
2445 my $areaCount;
2446  
2447 if (! $zoneObj->reservedFlag && ! $zoneObj->owner) {
2448  
2449 # This is a non-reserved zone. If the zone has a maximum number of areas, is there
2450 # room for another area in the zone?
2451 $areaCount = $zoneObj->ivPairs('areaHash');
2452 if (
2453 ($zoneObj->areaMax && $areaCount < $zoneObj->areaMax)
2454 || (! $zoneObj->areaMax)
2455 ) {
2456 # Window allowed; use this zone
2457 return $zoneObj;
2458 }
2459 }
2460 }
2461  
2462 # No zone found
2463 return undef;
2464 }
2465  
2466 sub chooseWinSize {
2467  
2468 # Called by $self->createGridWin after choosing the zone into which a window will be placed,
2469 # returns the size the window should have
2470 # Also called by GA::Obj::WorkspaceGrid->applyZonemap
2471 #
2472 # Expected arguments
2473 # $winType - The window type; one of the 'grid' window types specified by
2474 # GA::Client->constGridWinTypeHash
2475 # $workspaceGridObj
2476 # - The GA::Obj::WorkspaceGrid object which will receive the new window
2477 # $zoneObj - Blessed reference to the GA::Obj::Zone object, handling the zone in which
2478 # the window will be placed
2479 #
2480 # Optional arguments
2481 # $zoneSpecifiedFlag
2482 # - Set to TRUE if the function that called $self->createGridWin specified a
2483 # zone to use, otherwise set to FALSE. Set to 'undef' when called by
2484 # any other function
2485 # $widthPixels, $heightPixels
2486 # - The size of the window specified by the function that called
2487 # $self->createGridWin. Both set to 'undef' if no size was specified
2488 #
2489 # Return values
2490 # An empty list on improper arguments
2491 # Otherwise, returns the list ($widthPixels, $heightPixels)
2492  
2493 my (
2494 $self, $winType, $workspaceGridObj, $zoneObj, $zoneSpecifiedFlag, $widthPixels,
2495 $heightPixels, $check,
2496 ) = @_;
2497  
2498 # Local variables
2499 my (
2500 $blockSize,
2501 @emptyList,
2502 );
2503  
2504 # Check for improper arguments
2505 if (
2506 ! defined $winType || ! defined $workspaceGridObj || ! defined $zoneObj
2507 || defined $check
2508 ) {
2509 $axmud::CLIENT->writeImproper($self->_objClass . '->chooseWinSize', @_);
2510 return @emptyList;
2511 }
2512  
2513 # Import the standard gridblock size (in pixels) for convenience
2514 $blockSize = $axmud::CLIENT->gridBlockSize;
2515  
2516 # On the original call to $self->createGridWin, if a zone number wasn't specified and one
2517 # (but not both) of $widthPixels / $heightPixels weren't specified, use default values for
2518 # the width and height
2519 # Exception: if this window is a 'main' window and it's the first window ever created,
2520 # assume that the Axmud is still setting up. If there is more than one zone, set the
2521 # 'main' window to fill the zone. If there is only one zone, use the default 'main' window
2522 # size
2523 if (
2524 ! $zoneSpecifiedFlag
2525 && (
2526 (defined $widthPixels && ! defined $heightPixels)
2527 || (defined $heightPixels && ! defined $widthPixels)
2528 )
2529 ) {
2530 if ($winType eq 'main' && ! $axmud::CLIENT->desktopObj->gridWinHash) {
2531  
2532 # It's the 'main' window, which is almost always in zone 1
2533 if ($workspaceGridObj->zoneCount == 1) {
2534  
2535 $widthPixels = $axmud::CLIENT->customMainWinWidth;
2536 $heightPixels = $axmud::CLIENT->customMainWinHeight;
2537  
2538 } else {
2539  
2540 $widthPixels = ($zoneObj->widthBlocks * $blockSize);
2541 $heightPixels = ($zoneObj->heightBlocks * $blockSize);
2542 }
2543  
2544 } else {
2545  
2546 if (! defined $widthPixels) {
2547  
2548 $widthPixels = $axmud::CLIENT->customGridWinWidth;
2549  
2550 } elsif (! defined $heightPixels) {
2551  
2552 $heightPixels = $axmud::CLIENT->customGridWinHeight;
2553 }
2554 }
2555  
2556 # In a positioning free-for-all (zonemap 'single'), let a 'main' window use its own default
2557 # size
2558 } elsif ($winType eq 'main' && $workspaceGridObj->zonemap eq 'single') {
2559  
2560 $widthPixels = $axmud::CLIENT->customMainWinWidth;
2561 $heightPixels = $axmud::CLIENT->customMainWinHeight;
2562  
2563 # Most of the time, use the zone's default width and height if those defaults are specified.
2564 # Take into account any adjustment to the zone's default width and height that
2565 # $zoneObj->adjustMultipleWin might have made
2566 # If the zone's default values aren't specified, use the global default values
2567 } else {
2568  
2569 if (! defined $widthPixels) {
2570  
2571 if ($zoneObj->defaultAreaWidth) {
2572  
2573 $widthPixels
2574 = ($zoneObj->defaultAreaWidth + $zoneObj->widthAdjustBlocks) * $blockSize;
2575  
2576 } else {
2577  
2578 $widthPixels = $axmud::CLIENT->customGridWinWidth;
2579 }
2580 }
2581  
2582 if (! defined $heightPixels) {
2583  
2584 if ($zoneObj->defaultAreaHeight) {
2585  
2586 $heightPixels
2587 = ($zoneObj->defaultAreaHeight + $zoneObj->heightAdjustBlocks) * $blockSize;
2588  
2589 } else {
2590  
2591 $heightPixels = $axmud::CLIENT->customGridWinHeight;
2592 }
2593 }
2594 }
2595  
2596 return ($widthPixels, $heightPixels);
2597 }
2598  
2599 sub chooseWinPosn {
2600  
2601 # Called by $self->createGridWin after the window has been given a zone and a provisional
2602 # size
2603 # Sets the exact position of the window on the workspace, taking into account several
2604 # different factors
2605 #
2606 # Expected arguments
2607 # $complainant - The object to which error messages should be sent: either the
2608 # controlling GA::Session (if one was specified), or GA::Client
2609 # (if not)
2610 # $winType - The window type; one of the 'grid' window types specified by
2611 # GA::Client->constGridWinTypeHash
2612 # $zoneSpecifiedFlag
2613 # - Set to TRUE if the function that called $self->createGridWin specified
2614 # a zone to use, otherwise set to FALSE
2615 # $workspaceGridObj
2616 # - The GA::Obj::WorkspaceGrid object which will receive the new window
2617 # $widthPixels, $heightPixels
2618 # - The provisional size of the window (which may be changed by this
2619 # function)
2620 #
2621 # Optional arguments
2622 # $zoneObj - The GA::Obj::Zone, if already known (otherwise 'undef')
2623 # $layer - The layer in which to place the window specified by the function which
2624 # called $self->createGridWin. If no size was specified, set to
2625 # 'undef'
2626 # $xPosPixels, $yPosPixels
2627 # - The size of the window specified by the function which called
2628 # $self->createGridWin. If no size was specified, both set to 'undef'
2629 #
2630 # Return values
2631 # An empty list of improper arguments or if there is no room in this zone for the window
2632 # Otherwise, returns the window's size, position and layer in a list:
2633 # ($layer, $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $xPosPixels,
2634 # $yPosPixels, $widthPixels, $heightPixels)
2635  
2636 my (
2637 $self, $complainant, $winType, $zoneSpecifiedFlag, $workspaceGridObj, $widthPixels,
2638 $heightPixels, $zoneObj, $layer, $xPosPixels, $yPosPixels, $check
2639 ) = @_;
2640  
2641 # Local variables
2642 my (
2643 $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $successFlag,
2644 @emptyList,
2645 );
2646  
2647 # Check for improper arguments
2648 if (
2649 ! defined $complainant || ! defined $winType || ! defined $zoneSpecifiedFlag
2650 || ! defined $workspaceGridObj || ! defined $widthPixels || ! defined $heightPixels
2651 || defined $check
2652 ) {
2653 $axmud::CLIENT->writeImproper($self->_objClass . '->chooseWinPosn', @_);
2654 return @emptyList;
2655 }
2656  
2657 # If $zone was specified when this function was called, and either or both or $xPosPixels /
2658 # $yPosPixels were specified, this function must ignore them
2659 if ($zoneSpecifiedFlag) {
2660  
2661 $xPosPixels = undef;
2662 $yPosPixels = undef;
2663 }
2664  
2665 # Before we decide where exactly in the zone to put the window, we give it a provisional
2666 # position in one of the zone's corners (by default the top-left corner). If that region
2667 # of the zone happens to be free, the window will be put there permanently; otherwise it
2668 # will be moved to some other free part of the zone later in this function
2669 # However, if either or both of $xPosPixels / $yPosPixels are still specified, we use one
2670 # or both of those values as the provisional position instead
2671 ($widthBlocks, $heightBlocks, $xPosBlocks, $yPosBlocks)
2672 = $zoneObj->findProvWinPosn($widthPixels, $heightPixels, $xPosPixels, $yPosPixels);
2673  
2674 # If $layer wasn't specified, use the default layer provisionally
2675 if (! defined $layer) {
2676  
2677 $layer = $workspaceGridObj->defaultLayer;
2678  
2679 # Otherwise, check that the specified layer is valid (and use the default one if
2680 # not)
2681 } elsif (! $axmud::CLIENT->intCheck($layer, 0, ($workspaceGridObj->maxLayers - 1))) {
2682  
2683 $complainant->writeWarning(
2684 'The specified layer #' . $layer . ' is not valid, using the default layer #'
2685 . $workspaceGridObj->defaultLayer,
2686 $self->_objClass . '->chooseWinPosn',
2687 );
2688  
2689 $layer = $workspaceGridObj->defaultLayer;
2690 }
2691  
2692 # Place the window at some position within the zone (hopefully at the provisional one, on
2693 # the default layer)
2694 ($successFlag, $layer, $xPosBlocks, $yPosBlocks)
2695 = $zoneObj->placeWin($layer, $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks);
2696  
2697 if (! $successFlag) {
2698  
2699 # There is no room for the window in this zone
2700 $complainant->writeError(
2701 'Couldn\'t find room for the \'' . $winType . '\' window anywhere in zone #'
2702 . $zoneObj->number,
2703 $self->_objClass . '->chooseWinPosn',
2704 );
2705  
2706 return @emptyList;
2707 }
2708  
2709 # If the window position on the grid puts it rather close to the edge (or edges) of the
2710 # zone, and if the gaps between the window and the zone's edge are empty, adjust the size
2711 # of the window to fill the gap (this prevents small areas of the zone from always being
2712 # empty: makes the workspace look nice). If the maximum allowable gap size is 0, don't
2713 # fill gaps at all
2714 if ($axmud::CLIENT->gridGapMaxSize) {
2715  
2716 ($xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks)
2717 = $zoneObj->adjustSingleWin(
2718 $layer,
2719 $xPosBlocks,
2720 $yPosBlocks,
2721 $widthBlocks,
2722 $heightBlocks,
2723 );
2724 }
2725  
2726 # Now that we have the window's final size and position in the zone, reset the variables
2727 # that show the window's size and position, in pixels, on the workspace
2728 ($xPosPixels, $yPosPixels) = $zoneObj->getInternalGridPosn($xPosBlocks, $yPosBlocks);
2729  
2730 $widthPixels = $widthBlocks * $axmud::CLIENT->gridBlockSize;
2731 $heightPixels = $heightBlocks * $axmud::CLIENT->gridBlockSize;
2732  
2733 # If the desktop theme uses window controls, we have to take them into account, changing the
2734 # size of the window accordingly
2735 ($xPosPixels, $yPosPixels, $widthPixels, $heightPixels)
2736 = $workspaceGridObj->fineTuneWinSize(
2737 $winType,
2738 $xPosPixels,
2739 $yPosPixels,
2740 $widthPixels,
2741 $heightPixels
2742 );
2743  
2744 return (
2745 $layer, $xPosBlocks, $yPosBlocks, $widthBlocks, $heightBlocks, $xPosPixels, $yPosPixels,
2746 $widthPixels, $heightPixels,
2747 );
2748 }
2749  
2750 sub getWinmap {
2751  
2752 # Called by $self->createGridWin or $self->createSimpleGridWin
2753 # If a winmap name was specified as an argument to those functions, selects the
2754 # corresponding winmap object (if it exists)
2755 # Otherwise selects a default winmap object, or 'undef' for 'map', 'fixed' and 'external'
2756 # windows (which don't use winmaps)
2757 # 'main' windows must contain at least one winzone which creates a pane object
2758 # (GA::Table::Pane). For 'main' windows, if the selected winmap doesn't contain a pane
2759 # object, a standard winmap is used instead
2760 #
2761 # Expected arguments
2762 # $winType - The window type; one of the 'grid' window types specified by
2763 # GA::Client->constGridWinTypeHash
2764 #
2765 # Optional arguments
2766 # $winmapName - The winmap name specified as an argument in the calls to
2767 # $self->createGridWin or $self->createSimpleGridWin (may be 'undef')
2768 # $zoneObj - When called by $self->createGridWin, the GA::Obj::Zone into which the
2769 # window will be moved. When called by $self->createSimpleGridWin,
2770 # 'undef'
2771 #
2772 # Return values
2773 # 'undef' on improper arguments or if no winmap object is created
2774 # Otherwise, returns a winmap object (GA::Obj::Winmap)
2775  
2776 my ($self, $winType, $winmapName, $zoneObj, $check) = @_;
2777  
2778 # Local variables
2779 my ($winmapObj, $flag);
2780  
2781 # Check for improper arguments
2782 if (defined $check) {
2783  
2784 return $axmud::CLIENT->writeImproper($self->_objClass . '->getWinmap', @_);
2785 }
2786  
2787 # Convert a winmap name to a winmap object. If no winmap was specified, use a default one
2788 if (! defined $winType || defined $winmapName) {
2789  
2790 $winmapObj = $axmud::CLIENT->ivShow('winmapHash', $winmapName);
2791 }
2792  
2793 if (! $winmapObj) {
2794  
2795 # No winmap name specified, or winmap doesn't exist, so use a default winmap
2796 if ($winType eq 'main') {
2797  
2798 if ($self->gridEnableFlag && $axmud::CLIENT->activateGridFlag) {
2799  
2800 if ($zoneObj && defined $zoneObj->defaultEnabledWinmap) {
2801  
2802 $winmapObj
2803 = $axmud::CLIENT->ivShow('winmapHash', $zoneObj->defaultEnabledWinmap);
2804 }
2805  
2806 if (! $winmapObj) {
2807  
2808 $winmapObj = $axmud::CLIENT->ivShow(
2809 'winmapHash',
2810 $axmud::CLIENT->defaultEnabledWinmap,
2811 );
2812 }
2813  
2814 } else {
2815  
2816 if ($zoneObj && defined $zoneObj->defaultDisabledWinmap) {
2817  
2818 $winmapObj
2819 = $axmud::CLIENT->ivShow('winmapHash', $zoneObj->defaultDisabledWinmap);
2820 }
2821  
2822 if (! $winmapObj) {
2823  
2824 $winmapObj = $axmud::CLIENT->ivShow(
2825 'winmapHash',
2826 $axmud::CLIENT->defaultDisabledWinmap,
2827 );
2828 }
2829 }
2830  
2831 } elsif ($winType eq 'protocol' || $winType eq 'custom') {
2832  
2833 if ($zoneObj && defined $zoneObj->defaultInternalWinmap) {
2834  
2835 $winmapObj
2836 = $axmud::CLIENT->ivShow('winmapHash', $zoneObj->defaultInternalWinmap);
2837 }
2838  
2839 if (! $winmapObj) {
2840  
2841 $winmapObj = $axmud::CLIENT->ivShow(
2842 'winmapHash',
2843 $axmud::CLIENT->defaultInternalWinmap,
2844 );
2845 }
2846 }
2847 }
2848  
2849 # For 'main' windows, check we're going to get at least one pane object
2850 # (Exception: 'main_wait' deliberately contains no pane objects)
2851 if (
2852 $winType eq 'main'
2853 && (! $winmapName || $winmapName ne 'main_wait')
2854 ) {
2855 OUTER: foreach my $winzoneObj ($winmapObj->ivValues('zoneHash')) {
2856  
2857 if ($winzoneObj->packageName eq 'Games::Axmud::Table::Pane') {
2858  
2859 $flag = TRUE;
2860 last OUTER;
2861 }
2862 }
2863  
2864 if (! $flag) {
2865  
2866 # Use a standard winmap instead
2867 if ($self->gridEnableFlag && $axmud::CLIENT->activateGridFlag) {
2868  
2869 $winmapObj = $axmud::CLIENT->ivShow(
2870 'winmapHash',
2871 $axmud::CLIENT->constDefaultEnabledWinmap,
2872 );
2873  
2874 } else {
2875  
2876 $winmapObj = $axmud::CLIENT->ivShow(
2877 'winmapHash',
2878 $axmud::CLIENT->constDefaultDisabledWinmap,
2879 );
2880 }
2881 }
2882 }
2883  
2884 # Operation complete
2885 return $winmapObj;
2886 }
2887  
2888 ##################
2889 # Accessors - set
2890  
2891 sub set_defaultZonemap {
2892  
2893 # Called by GA::Cmd::ResetGrid->do, GA::Obj::File->extractData and
2894 # GA::WizWin::Setup->saveChanges
2895  
2896 my ($self, $obj, $check) = @_;
2897  
2898 # Check for improper arguments
2899 if (! defined $obj || defined $check) {
2900  
2901 return $axmud::CLIENT->writeImproper($self->_objClass . '->set_defaultZonemap', @_);
2902 }
2903  
2904 # Temporary zonemaps (created by MXP) can't be used as a workspace's default zonemap
2905 if ($obj->tempFlag) {
2906  
2907 return undef;
2908  
2909 } else {
2910  
2911 $self->ivPoke('defaultZonemap', $obj->name);
2912 }
2913  
2914 return 1;
2915 }
2916  
2917 sub reset_defaultZonemap {
2918  
2919 # Called by GA::WizWin::Setup->saveChanges
2920  
2921 my ($self, $check) = @_;
2922  
2923 # Check for improper arguments
2924 if (defined $check) {
2925  
2926 return $axmud::CLIENT->writeImproper($self->_objClass . '->reset_defaultZonemap', @_);
2927 }
2928  
2929 $self->ivPoke('defaultZonemap', undef);
2930  
2931 return 1;
2932 }
2933  
2934 ##################
2935 # Accessors - get
2936  
2937 sub number
2938 { $_[0]->{number} }
2939 sub systemNum
2940 { $_[0]->{systemNum} }
2941  
2942 sub currentWidth
2943 { $_[0]->{currentWidth} }
2944 sub currentHeight
2945 { $_[0]->{currentHeight} }
2946 sub panelLeftSize
2947 { $_[0]->{panelLeftSize} }
2948 sub panelRightSize
2949 { $_[0]->{panelRightSize} }
2950 sub panelTopSize
2951 { $_[0]->{panelTopSize} }
2952 sub panelBottomSize
2953 { $_[0]->{panelBottomSize} }
2954 sub controlsLeftSize
2955 { $_[0]->{controlsLeftSize} }
2956 sub controlsRightSize
2957 { $_[0]->{controlsRightSize} }
2958 sub controlsTopSize
2959 { $_[0]->{controlsTopSize} }
2960 sub controlsBottomSize
2961 { $_[0]->{controlsBottomSize} }
2962  
2963 sub gridHash
2964 { my $self = shift; return %{$self->{gridHash}}; }
2965 sub gridEnableFlag
2966 { $_[0]->{gridEnableFlag} }
2967 sub defaultZonemap
2968 { $_[0]->{defaultZonemap} }
2969 }
2970  
2971 # Package must return a true value
2972 1