Page 1 of 1

Any GTK-PHP monsters out there?

Posted: Wed Jul 24, 2002 4:39 pm
by BDKR
OK, here's what's going on. I am working on learning GTK-PHP (duh) and decided I'd try to write a script that builds a GTK frontend for a movie player backend (M Player for Linux).

The problem is as such. I managed to create a (very) simple front end that manages to open a file selection dialog. From there I can select a file and actually have the movie player start playing that file. However, the problem comes in before the file starts playing. The file selection dialog doesn't go away. Instead, it sits on the screen and doesn't go away until the file is either finished or terminated.

Now that's NOT the correct behaviour. I want the file dialog to go away first, then the movie start playing. I even put in code that's supposed to accomplish this. Anyway, here's the code. I've been at it two days and am at a loss. If anyone else has an idea, tell me what you think.

Code: Select all

<?php

# Load the appropriate extension based on OS
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
  &#123; dl('php_gtk.dll'); &#125;
else
  &#123; dl('php_gtk.so'); &#125;
  
# Now setup the movie container
$main_feature = &new movieContainer;

# Create our window
$window = &new GtkWindow();
$window->set_title('PHat GeeT''s Movie Player');
$window->set_default_size(300,0);
$window->connect('destroy','destroy');
  
# This is the initial vertical box that we will pack
# the rest of the application into.
$vbox = &new GtkVBox();
$window->add($vbox);
$vbox->show();
  
# The first step to creating a menu
$menu = &new GtkMenu;
  
# Now let's start creating the individual menu options. 
# The first one is the Open command on the File list.
$menufile_open = &new GtkMenuItem("Open");
$menufile_open->connect("activate", "FileDialog");
  
# This is the quit dialog. There isn't going to be much here. 
$menufile_quit = &new GtkMenuItem("Quit");
$menufile_quit->connect("activate", "destroy");
  
# Add the items to the menu
$menu->append($menufile_open);
$menu->append($menufile_quit);
  
# Let's see if we can create a menu box as well. Perhaps 
# we should look at this as being a menu containe?
$menubar = &new GtkMenuBar();
$menubar->set_shadow_type(GTK_SHADOW_IN);
$vbox->add($menubar);

# This menuitem will be used as the label for our 
# drop down menu.
$label_file = &new GtkMenuItem("File");
$label_file->set_submenu($menu);

# Now make sure that the menubar has this item
$menubar->append($label_file);
  
# Show everything and start GTK::main() - THIS IS IMPORTANT :O)
$window->show_all();
Gtk::main();


#############################################################################################  
# Functions used by this program  
#############################################################################################  
# Used to kill the application
function destroy()
  &#123; Gtk::main_quit(); &#125;

# In the interest of choosing a file.
function FileDialog() 
  &#123;
  # The File selection box
  $fs = &new GtkFileSelection('Pick da'' Flick'); 
  $fs->hide_fileop_buttons();								// They can change file names elsewhere

  # Now for the OK button
  $ok_button = $fs->ok_button;
  $ok_button->connect_object('clicked', 'PlayMovie', $fs);	// The object is passed to the PlayMovie function to 
															// facilitate acting directly on it. 
  
  # And the cancel button
  $cancel_button = $fs->cancel_button;
  $cancel_button->connect_object('clicked', array($fs, 'destroy'));
  $fs->show();
  &#125;

# Now play the movie fool!
function PlayMovie(&$dialog)
  &#123;
  # Let's reference movieContainer
  global $main_feature;
  
  # Get the movie name
  $mov = $dialog->get_filename();
  $main_feature->setData($mov);

  # This is where the File Selection dialog should be destroyed. 
  $dialog->destroy();
  
  # And here, the movie should be played. However, this will be changed 
  # in time to provide more control over movie player while it's playing.
  system("mplayer $mov");
  &#125;

# Just a funkmaster container thrown together to store 
# the movie information for a short period. 
class movieContainer
  &#123;
  function setData($data)
	&#123; $this->info = $data; &#125;
	
  function getData()
	&#123; return $this->info; &#125;
  &#125;


?>
And remember, when it comes to this GTK stuff, I'm a nuubee. 8O

Anyways, if you can see where i've gone stupid, I'd love to know.

Later on,
BDKR

Posted: Fri Jul 26, 2002 2:21 am
by gnu2php
I tried out your code, but the file dialog closed just fine for me. :? I'm using Win98 and the newest GTK.

There was a problem, however, after the media player executed and then terminated--your PHP program froze up, and I had to press Ctrl+Alt+Del to exit the program. I did get around this problem by bypassing the system() function, and instead used a C spawn function (in a module I created).

Just a thought--

What if the system() function is somehow causing the dialog to remain? Try "commenting" it out like this:

Code: Select all

// system("mplayer $mov");

Posted: Fri Jul 26, 2002 9:08 am
by BDKR
Hey there,

Worked fine eh? I had a feeling it may be a platform issue (I'm on Linux) as the code is right in line with what is suggested in the manual for passing custom parameters.

As for the prog freezing, you are probably correct that it has something to do with the system() function, eventhough it doesn't freeze on my box. I actually inserted that function as an afterthought. If you note from the comments, I am planning on doing something a bit different in the interest of passing commands back to the player while it's playing.

Besides, the $dialog->destroy() method IS being called before the system() function. It would seem that a signal is being passed back to the file dialog box, but program execution is going forward without giving the file dialog box object a chance to destroy itself. Once the movie is finished, then it goes away.

:idea:

:!: Time Passess :!:

OK, I just did some testing as something above made me think that perhaps the PlayMovie function in Linux wasn't actually closing the dialog until it (the function itself) completed. The first thing I did was put the below code after the $dialog->destroy() method...

Code: Select all

while($dialog)
    &#123; /* do nothing */ &#125;
Or in other words, as long as the File Dialog is open, do nothing until it closes. So when I ran the prog, it froze up in that loop. It's as if the destroy() method was deferred until completion of the PlayMovie function. That's way out!

Well, I've got some things to try now as a result of what I found here. Thanx for the feedback as it got me thinking.

Later on,
BDKR