blib/lib/Games/Axmud/Win/External.pm untested

filter: Statementstmt 0.0% 0 / 87 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 184 CC 21 SCAR 30.6
CC
21
Cov
0%
winDisengage
56.0
winDestroy
30.0
new
12.0
CRAP 21.4
1 # Copyright (C) 2011-2024 A S Lewis
2 #
3 # This program is free software: you can redistribute it and/or modify it under the terms of the GNU
4 # General Public License as published by the Free Software Foundation, either version 3 of the
5 # License, or (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License along with this program. If not,
12 # see <http://www.gnu.org/licenses/>.
13 #
14 #
15 # Games::Axmud::Win::External
16 # Object handling 'external' windows (any window that can be placed on the workspace grid, but which
17 # is not created/controlled by Axmud). Doesn't include 'free' windows
18  
19 { package Games::Axmud::Win::External;
20  
21 use strict;
22 use warnings;
23 # use diagnostics;
24  
25 use Glib qw(TRUE FALSE);
26  
27 our @ISA = qw(
28 Games::Axmud::Generic::GridWin Games::Axmud::Generic::Win Games::Axmud
29 );
30  
31 ##################
32 # Constructors
33  
34 sub new {
35  
36 # Called by GA::Obj::Workspace->createGridWin and ->createSimpleGridWin
37 # Creates an 'external' 'grid' window (any window that can be placed on the workspace grid,
38 # but which is not created/controlled by Axmud)
39 #
40 # Expected arguments
41 # $number - Unique number for this window object
42 # $winType - The window type, must be 'external'
43 # $winName - The 'external' window's name (e.g. 'Notepad')
44 # $workspaceObj
45 # - The GA::Obj::Workspace object for the workspace in which this window is
46 # created
47 #
48 # Optional arguments
49 # $owner - The owner, if known. Can be any blessed reference, typically it's an
50 # GA::Session or a task (inheriting from GA::Generic::Task); could also
51 # be GA::Client
52 # $session - The owner's session. If $owner is a GA::Session, that session. If it's
53 # something else (like a task), the task's session. If $owner is 'undef',
54 # so is $session
55 # $workspaceGridObj
56 # - The GA::Obj::WorkspaceGrid object into whose grid this window has been
57 # placed. 'undef' in $workspaceObj->gridEnableFlag = FALSE
58 # $areaObj - The GA::Obj::Area (a region of a workspace grid zone) which handles this
59 # window. 'undef' in $workspaceObj->gridEnableFlag = FALSE
60 # $winmap - On calls to GA::Win::Internal->new, this argument describes the ->name
61 # of an a GA::Obj::Winmap object. 'external' windows don't use winmaps
62 # so, if specified, this argument is ignored
63 #
64 # Return values
65 # 'undef' on improper arguments
66 # Blessed reference to the newly-created object on success
67  
68 my (
69 $class, $number, $winType, $winName, $workspaceObj, $owner, $session, $workspaceGridObj,
70 $areaObj, $winmap, $check,
71 ) = @_;
72  
73 # Check for improper arguments
74 if (
75 ! defined $class || ! defined $number || ! defined $winType || ! defined $winName
76 || ! defined $workspaceObj || defined $check
77 ) {
78 return $axmud::CLIENT->writeImproper($class . '->new', @_);
79 }
80  
81 # Check that the $winType is valid
82 if ($winType ne 'external') {
83  
84 return $axmud::CLIENT->writeError(
85 'Internal window error: invalid \'external\' window type \'' . $winType . '\'',
86 $class . '->new',
87 );
88 }
89  
90 # Setup
91 my $self = {
92 _objName => 'external_win_' . $number,
93 _objClass => $class,
94 _parentFile => undef, # No parent file object
95 _parentWorld => undef, # No parent file object
96 _privFlag => TRUE, # All IVs are private
97  
98 # Standard window object IVs
99 # --------------------------
100  
101 # Unique number for this window object
102 number => $number,
103 # The window category - 'grid' or 'free'
104 winCategory => 'grid',
105 # The window type, must be 'external'
106 winType => $winType,
107 # The external window's name (e.g. 'Notepad')
108 winName => $winName,
109 # The GA::Obj::Workspace object for the workspace in which this window is created
110 workspaceObj => $workspaceObj,
111 # The owner, if known ('undef' if not). If set, can be any blessed reference, typically
112 # it's a GA::Session or a task (inheriting from GA::Generic::Task). When there are
113 # no sessions running but there's a single 'main' window open, the owner is the
114 # GA::Client. When the window closes, the owner is informed via a call to its
115 # ->del_winObj function
116 owner => $owner,
117 # The owner's session ('undef' if not). If ->owner is a GA::Session, that session. If
118 # it's something else (like a task), the task's sesssion. If ->owner is 'undef', so is
119 # ->session
120 session => $session,
121 # When GA::Session->pseudoCmd is called to execute a client command, the mode in which
122 # it should be called (usually 'win_error' or 'win_only', which causes errors to be
123 # displayed in a 'dialogue' window)
124 pseudoCmdMode => 'win_error',
125  
126 # The window widget. For most window objects, the Gtk3::Window. For pseudo-windows, the
127 # parent 'main' window's Gtk3::Window
128 # The code should use this IV when it wants to do something to the window itself
129 # (minimise it, make it active, etc)
130 winWidget => undef,
131 # The window container. For most window objects, the Gtk3::Window. For pseudo-windows,
132 # the parent GA::Table::PseudoWin table object
133 # The code should use this IV when it wants to add, modify or remove widgets inside the
134 # window itself
135 winBox => undef,
136 # Flag set to TRUE if the window actually exists (after a call to $self->winEnable),
137 # FALSE if not
138 enabledFlag => FALSE,
139 # Flag set to TRUE if the Gtk3 window itself is visible (after a call to
140 # $self->setVisible), FALSE if it is not visible (after a call to $self->setInvisible)
141 visibleFlag => TRUE,
142 # Registry hash of 'free' windows for which this window is the parent (always empty,
143 # because 'external' windows can be a parent window to a 'free' window)
144 childFreeWinHash => {},
145 # When a child 'free' window (excluding 'dialogue' windows) is destroyed, this parent
146 # window is informed via a call to $self->del_childFreeWin
147 # When the child is destroyed, this window might want to call some of its own functions
148 # to update various widgets and/or IVs, in which case this window adds an entry to
149 # this hash; a hash in the form
150 # $childDestroyHash{unique_number} = list_reference
151 # ...where 'unique_number' is the child window's ->number, and 'list_reference' is a
152 # reference to a list in groups of 2, in the form
153 # (sub_name, argument_list_ref, sub_name, argument_list_ref...)
154 childDestroyHash => {},
155  
156 # The container widget into which all other widgets are packed (not required for an
157 # 'external' window)
158 packingBox => undef,
159  
160 # Standard IVs for 'grid' windows
161  
162 # The GA::Obj::WorkspaceGrid object into whose grid this window has been placed. 'undef'
163 # in $workspaceObj->gridEnableFlag = FALSE
164 workspaceGridObj => $workspaceGridObj,
165 # The GA::Obj::Area object for this window. An area object is a part of a zone's
166 # internal grid, handling a single window (this one). Set to 'undef' in
167 # $workspaceObj->gridEnableFlag = FALSE
168 areaObj => $areaObj,
169 # For pseudo-windows (in which a window object is created, but its widgets are drawn
170 # inside a GA::Table::PseudoWin table object), the table object created. Always
171 # 'undef' for 'external' windows which can't be pseudo-windows
172 pseudoWinTableObj => undef,
173 # The name of the GA::Obj::Winmap object that specifies the Gtk3::Window's layout when
174 # it is first created. Always 'undef' for 'external' windows
175 winmap => undef,
176  
177 # Standard IVs for 'external' windows
178  
179 # The X11::WMCtrl internal ID for this window
180 internalID => undef,
181 };
182  
183 # Bless the object into existence
184 bless $self, $class;
185  
186 return $self;
187 }
188  
189 ##################
190 # Methods
191  
192 sub winSetup {
193  
194 # Called by GA::Obj::Workspace->createGridWin or ->createSimpleGridWin
195 # The actual window already exists, but we still need to update IVs
196 #
197 # Expected arguments
198 # $internalID - The X11::WMCtrl internal ID for this window
199 #
200 # Return values
201 # 'undef' on improper arguments
202 # 1 on success
203  
204 my ($self, $internalID, $check) = @_;
205  
206 # Check for improper arguments
207 if (defined $check) {
208  
209 return $axmud::CLIENT->writeImproper($self->_objClass . '->winSetup', @_);
210 }
211  
212 # Update IVs
213 $self->ivPoke('internalID', $internalID);
214  
215 return 1;
216 }
217  
218 sub winEnable {
219  
220 # Called by GA::Obj::Workspace->createGridWin or ->createSimpleGridWin
221 # Used for consistency with 'internal' windows
222 #
223 # Expected arguments
224 # (none besides $self)
225 #
226 # Optional arguments
227 # $listRef - Reference to a list of functions for 'internal' windows. If specified,
228 # ignored
229 #
230 # Return values
231 # 'undef' on improper arguments
232 # 1 on success
233  
234 my ($self, $listRef, $check) = @_;
235  
236 # Check for improper arguments
237 if (defined $check) {
238  
239 return $axmud::CLIENT->writeImproper($self->_objClass . '->winEnable', @_);
240 }
241  
242 # Update IVs to be consistent with 'internal' windows
243 $self->ivPoke('enabledFlag', TRUE);
244  
245 return 1;
246 }
247  
248 sub winDisengage {
249  
250 # Called by GA::Cmd->BanishWindow->do
251 #
252 # Destroys the window object, but not the window itself, leaving the 'external' window free
253 # to pursue its own dreams
254 # Marks the area of the zone the window used to occupy as free, and available for other
255 # workspace grid windows
256 #
257 # Expected arguments
258 # (none besides $self)
259 #
260 # Return values
261 # 'undef' on improper arguments or if the window has already been disengaged in a previous
262 # call to this function
263 # 1 if the window is disengaged
264  
265 my ($self, $check) = @_;
266  
267 # Local variables
268 my ($zoneObj, $flag);
269  
270 # Check for improper arguments
271 if (defined $check) {
272  
273 return $axmud::CLIENT->writeImproper($self->_objClass . '->winDisengage', @_);
274 }
275  
276 if (! $self->internalID) {
277  
278 # Window already disengaged in a previous call to this function
279 return undef;
280 }
281  
282 # 'External' windows can't have child windows. But, the IV storing child windows exists, so
283 # in case someone decides to add some child windows anyway, close them
284 foreach my $winObj ($self->ivValues('childFreeWinHash')) {
285  
286 $winObj->winDestroy();
287 }
288  
289 # Inform the parent workspace grid object (if any)
290 if ($self->workspaceGridObj) {
291  
292 $self->workspaceGridObj->del_gridWin($self);
293 }
294  
295 # Inform the desktop object
296 $axmud::CLIENT->desktopObj->del_gridWin($self);
297  
298 # Look for other 'grid' windows (besides this one) handling the same 'external' window. If
299 # there are none, it's safe to minimise the 'external' window
300 if (defined $self->internalID) {
301  
302 OUTER: foreach my $winObj ($axmud::CLIENT->desktopObj->ivValues('gridWinHash')) {
303  
304 if (
305 $winObj->winType eq 'external'
306 && $self->internalID
307 && $winObj->internalID
308 && $self->internalID eq $winObj->internalID
309 ) {
310 $flag = TRUE;
311 last OUTER;
312 }
313 }
314  
315 if (! $flag) {
316  
317 # Minimise the window so that, visually, it appears to have been removed from
318 # Axmud's control
319 $self->minimise();
320 }
321 }
322  
323 # Operation complete
324 $self->ivUndef('internalID');
325  
326 return 1;
327 }
328  
329 sub winDestroy {
330  
331 # Called by ->signal_connects in $self->setDeleteEvent and ->setWindowClosedEvent
332 # Marks the area of the zone the window used to occupy as free, and available for other
333 # workspace grid windows
334 #
335 # Expected arguments
336 # (none besides $self)
337 #
338 # Return values
339 # 'undef' on improper arguments or if the window has already been disengaged in a previous
340 # call to this function
341 # 1 if the window is disengaged
342  
343 my ($self, $check) = @_;
344  
345 # Check for improper arguments
346 if (defined $check) {
347  
348 return $axmud::CLIENT->writeImproper($self->_objClass . '->winDestroy', @_);
349 }
350  
351 if (! $self->internalID) {
352  
353 # Window already disengaged in a previous call to this function
354 return undef;
355 }
356  
357 # 'External' windows can't have child windows. But, the IV storing child windows exists,
358 # so in case someone decides to add some child windows anyway, close them
359 foreach my $winObj ($self->ivValues('childFreeWinHash')) {
360  
361 $winObj->winDestroy();
362 }
363  
364 # Inform the parent workspace grid object (if any)
365 if ($self->workspaceGridObj) {
366  
367 $self->workspaceGridObj->del_gridWin($self);
368 }
369  
370 # Inform the desktop object
371 $axmud::CLIENT->desktopObj->del_gridWin($self);
372  
373 # Inform the ->owner, if there is one
374 if ($self->owner) {
375  
376 $self->owner->del_winObj($self);
377 }
378  
379 # Operation complete
380 $self->ivUndef('internalID');
381  
382 return 1;
383 }
384  
385 # sub winShowAll {} # Inherited from GA::Generic::Win
386  
387 # sub drawWidgets {} # Inherited from GA::Generic::Win
388  
389 # sub redrawWidgets {} # Inherited from GA::Generic::Win
390  
391 # ->signal_connects
392  
393 sub setDeleteEvent {
394  
395 # Called by $self->winEnable
396 # Does nothing (there is no way to detect that the 'external' window has closed)
397 #
398 # Expected arguments
399 # (none besides $self)
400 #
401 # Return values
402 # 'undef' on improper arguments
403 # 1 otherwise
404  
405 my ($self, $winBox, $check) = @_;
406  
407 # Check for improper arguments
408 if (defined $check) {
409  
410 return $axmud::CLIENT->writeImproper($self->_objClass . '->setDeleteEvent', @_);
411 }
412  
413 # (Do nothing)
414  
415 return 1;
416 }
417  
418 sub setWindowClosedEvent {
419  
420 # Called by $self->winEnable
421 # Does nothing (there is no way to detect that the 'external' window has closed)
422 #
423 # Expected arguments
424 # (none besides $self)
425 #
426 # Return values
427 # 'undef' on improper arguments
428 # 1 otherwise
429  
430 my ($self, $check) = @_;
431  
432 # Check for improper arguments
433 if (defined $check) {
434  
435 return $axmud::CLIENT->writeImproper($self->_objClass . '->setWindowClosedEvent', @_);
436 }
437  
438 # (Do nothing)
439  
440 return 1;
441 }
442  
443 # Other functions
444  
445 sub minimise {
446  
447 # Can be called by anything
448 # Minimises the 'external' window
449 #
450 # NB The author of X11::WMCtrl reports that 'Currently minimize() and unminimize() don't
451 # work. This appears to be a problem with wmctrl itself'
452 #
453 # Expected arguments
454 # (none besides $self)
455 #
456 # Return values
457 # 'undef' on improper arguments or if the window can't be minimised
458 # 1 otherwise
459  
460 my ($self, $check) = @_;
461  
462 # Check for improper arguments
463 if (defined $check) {
464  
465 return $axmud::CLIENT->writeImproper($self->_objClass . '->minimise', @_);
466 }
467  
468 if (! $self->internalID) {
469  
470 return undef;
471  
472 } else {
473  
474 # Unmaximise the window
475 $axmud::CLIENT->desktopObj->wmCtrlObj->wmctrl(
476 '-r',
477 $self->internalID,
478 '-i',
479 '-b',
480 'remove,maximised_vert,maximised_horz',
481 );
482  
483 # Then minimise it
484 $axmud::CLIENT->desktopObj->wmCtrlObj->wmctrl(
485 '-r',
486 $self->internalID,
487 '-i',
488 '-b',
489 'add,hidden',
490 );
491  
492 return 1
493 }
494 }
495  
496 sub unminimise {
497  
498 # Can be called by anything
499 # Unminimises the 'external' window
500 #
501 # NB The author of X11::WMCtrl reports that 'Currently minimize() and unminimize() don't
502 # work. This appears to be a problem with wmctrl itself'
503 #
504 # Expected arguments
505 # (none besides $self)
506 #
507 # Return values
508 # 'undef' on improper arguments or if the window can't be unminimised
509 # 1 otherwise
510  
511 my ($self, $check) = @_;
512  
513 # Check for improper arguments
514 if (defined $check) {
515  
516 return $axmud::CLIENT->writeImproper($self->_objClass . '->unminimise', @_);
517 }
518  
519 if (! $self->internalID) {
520  
521 return undef;
522  
523 } else {
524  
525 # Unminimise the window
526 $axmud::CLIENT->desktopObj->wmCtrlObj->wmctrl(
527 '-r',
528 $self->internalID,
529 '-i',
530 '-b',
531 'remove,hidden',
532 );
533  
534 return 1;
535 }
536 }
537  
538 ##################
539 # Accessors - set
540  
541 ##################
542 # Accessors - get
543  
544 sub internalID
545 { $_[0]->{internalID} }
546 }
547  
548 # Package must return a true value
549 1