Skip to content

Commit 424f9ff

Browse files
committed
less coupling between controller and windows in example pokedit
1 parent 1e22980 commit 424f9ff

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

examples/pokedit.winxed

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,8 @@ class PokeditWindow : TopLevelWindow
400400

401401
self.SetWMProtocols( [ WM_DELETE_WINDOW_s, WM_TAKE_FOCUS_s ] );
402402

403-
self.OnDestroy += eventhandler(self, "ondestroy");
404403
self.OnConfigure += eventhandler(self, "onconfigure");
404+
self.OnMap += eventhandler(self, "onmap");
405405
self.OnClientMessage += eventhandler(self, "onclientmessage");
406406

407407
var menubar = new MenuBar(display, menufont);
@@ -420,10 +420,6 @@ class PokeditWindow : TopLevelWindow
420420
menubarw.Map();
421421
//textwindow.Map();
422422
}
423-
function ondestroy(event)
424-
{
425-
self.controller.pokeditclosed(self);
426-
}
427423
function onconfigure(event)
428424
{
429425
int width = event.width();
@@ -436,6 +432,11 @@ class PokeditWindow : TopLevelWindow
436432
self.width = width;
437433
self.height = height;
438434
}
435+
function onmap(event)
436+
{
437+
if (self.textwindow != null)
438+
self.textwindow.Map();
439+
}
439440
function onclientmessage(event)
440441
{
441442
string type = self.GetAtomName(event.message_type());
@@ -489,15 +490,16 @@ class PokeditWindow : TopLevelWindow
489490
self.menubarw = null;
490491
menubarw.Destroy();
491492
}
493+
// If the text window is still here, destroy it.
494+
// Its OnDentroy handler will call this method again.
495+
// In the following call, destroy self.
492496
var textwindow = self.textwindow;
493497
if (textwindow != null) {
494498
self.textwindow = null;
495499
self.controller.pushaction(method_fun(textwindow, "close"));
496500
}
497-
else {
498-
cry("destroy");
501+
else
499502
self.Destroy();
500-
}
501503
}
502504
}
503505

@@ -509,6 +511,19 @@ class PokeditController : Controller
509511
{
510512
self.Controller();
511513
}
514+
function pokeditnew()
515+
{
516+
var pokedit = new PokeditWindow(self);
517+
pokedit.OnDestroy += function (event) { self.pokeditclosed(pokedit); };
518+
pokedit.Map();
519+
}
520+
function pokeditopen(string filename)
521+
{
522+
var pokedit = new PokeditWindow(self);
523+
pokedit.openfile(filename);
524+
pokedit.OnDestroy += function (event) { self.pokeditclosed(pokedit); };
525+
pokedit.Map();
526+
}
512527
function pokeditclosed(window)
513528
{
514529
self.pushaction(method_fun(self, "Quit"));
@@ -520,11 +535,10 @@ class PokeditController : Controller
520535
function main [main] (args)
521536
{
522537
var controller = new PokeditController();
523-
var pokedit = new PokeditWindow(controller);
524538
if (elements(args) > 1)
525-
pokedit.openfile(args[1]);
526-
pokedit.textwindow.Map();
527-
pokedit.Map();
539+
controller.pokeditopen(args[1]);
540+
else
541+
controller.pokeditnew();
528542
controller.MainLoop();
529543
controller.Close();
530544
}

0 commit comments

Comments
 (0)