Archive for June, 2007

“Secrets” to using GtkStatusIcon

Monday, June 25th, 2007

I finally found some time over last weekend to really sit down and try to explore and understand GtkStatusIcon — the new widget introduced in gtk+2.10 that allows us to develop system tray application using PHP-GTK2.

I was misled heavily by a paragraph in the official referenece manual of GTK+. It says: “Note that a GtkStatusIcon is not a widget, but just a GObject. Making it a widget would be impractical, since the system tray on Win32 doesn’t allow to embed arbitrary widgets.”

This led me into thinking that it must be a complicated process to establish the link between a system tray icon and the main PHP-GTK2 application.

And how do I pass control back and forth between the main PHP-GTK2 application and the system tray?

Anyway, after much trials and errors, I realized that I was looking in the totally wrong direction!

So, let me share these undocumented “secrets” about GtkStatusIcon with you:

  1. There’s only ONE main loop running, and that’s the main loop of your PHP-GTK2 application. NOT the GtkStatusIcon.
  2. Just think of GtkStatusIcon as a GtkButton.
  3. This “button” does not reside within your GtkWindow. It appears in the system tray, and is independent of your GtkWindow.
  4. The button can receive left click and right click with the signals ‘activate’ and ‘popup-menu’ respectively.

To understand what the above four lines mean, take a look at the sample code below and read the explanation.

How to set up an application to run in the system tray - Part 2 - display GTK window on left click?

Once you understand the “secrets” above, I think you will be extremely busy for the next couple of days devevloping your own system tray applications.

Have lots of fun with GtkStatusIcon!

/kksou

http://www.kksou.com/php-gtk2
The place for php-gtk2 sample codes

p.s.1 I will be coming out with more sample codes to show you how to use this exciting GtkStatusIcon. In particular, how to handle right mouse click so that you can place some of the commonly-used functions in a popup menu when the user clicks on the right mouse button.

p.s.2 I’m NOT saying that a GtkStatusIcon IS a GtkButton. It’s NOT. But it helps one to understand and use it by viewing it as a GtkButton.

Display svg image

Wednesday, June 20th, 2007

Want to display an SVG (Scalable Vector Graphics) image in php-gtk2?

Thanks to Elizabeth Smith for sharing with us how to make SVG work in php-gtk2. Please refer to her post.

I’ve written a step-by-step instruction here together with a sample code:

How to display svg image file?

And here’s the sample output.

/kksou

http://www.kksou.com/php-gtk2
The place for php-gtk2 sample codes

Display math and greek letters in php-gtk2 on winxp

Wednesday, June 20th, 2007

This looks easy, doesn’t it? Just use modify_font() and set it to use window’s Symbol font.

I thought it was easy too when I saw pbm’s post titled “Symbol font in PHP-GTK2″.

I ended up spending two nights playing around with unicode, utf-8 and OpenSymbol font.

So here are the results of my testing — 4 examples to teach you how to display math and greek letters in php-gtk2, and how to enter these unicode characters in your text editor.

  1. How to display math and greek characters using symbol font - Part 1? 06/19/2007
  2. How to display math and greek characters using symbol font - Part 2 - input unicode characters? 06/19/2007
  3. How to display math and greek characters using symbol font - Part 3 - unicode characters in plain ANSI text file? 06/19/2007
  4. How to display math and greek characters using symbol font - Part 4 - uuencode unicode characters? 06/19/2007

/kksou

http://www.kksou.com/php-gtk2
The place for php-gtk2 sample codes

Display arrays of buttons of GtkButtonBox

Friday, June 15th, 2007

If you need to displays rows of buttons at the top or bottom of your application, you might want to give GtkHButtonBox a try.

No doubt you can achieve exactly the same effect using just plain old GtkHBox and GtkButtons. I have been doing this for a long time! You have controls over all the fine details — the size of the buttons, the location of the buttons, the spacing between the button, the layout of the buttons, etc.

However, after playing with GtkButtonBox for some time, I thought it’s quite a fun and useful widget after all.

In case you haven’t tried this widget before, here are three examples to get you started:

  1. How to display arrays of buttons using GtkButtonBox - Part 1 - with different GtkButtonBoxStyle? 06/14/2007
  2. How to display arrays of buttons using GtkButtonBox - Part 2 - set spacing between buttons? 06/15/2007
  3. How to display arrays of buttons using GtkButtonBox - Part 3 - two button groups? 06/15/2007

/kksou

http://www.kksou.com/php-gtk2
The place for php-gtk2 sample codes

One advantage of GtkTreeView over GtkSheet

Wednesday, June 13th, 2007

Have written quite a number of examples on GtkSheet these few days. The latest one is:

How to use GtkSheet - Part 17 - sort by clicking column header?

Interestingly, this example highlights one advantage of GtkTreeView over GtkSheet.

GtkTreeView not only has built-in support for sorting. It has also GtkTreeModelSort and GtkTreeModelFilter, allowing you to have a base model and multiple sort/filter models that present different views over the base model. These “virtual” models do not hold any data itself. Any change in the base model is propagated through all the virtual models. Below are some examples on GtkTreeModelSort and GtkTreeModelFilter:

/kksou

http://www.kksou.com/php-gtk2
The place for php-gtk2 sample codes

Adding radio buttons to GtkSheet

Sunday, June 10th, 2007

If you have tried the previous 10+ examples on GtkSheet, you might think that what’s the big deal? I can do all these using GtkTreeView.

While GtkTreeView is indeed powerful and useful, the problem is that we are limited by the type of cell renders available to us. Currently it has only four: GtkCellRendererText, GtkCellRendererToggle, GtkCellRendererPixbuf and GtkCellRendererProgress. While it is possible in PyGTK to create custom cell renderers, creating a custom cell renderer is still not available to PHP-GTK2.

So, currently it’s not possible to include many other standard GTK widgets, e.g. radio buttons, in a GtkTreeView. But you can easily do so using a GtkSheet, as the following example illustrates:

How to use GtkSheet - Part 14 - setup radio buttons in cell?

/kksou

http://www.kksou.com/php-gtk2
The place for php-gtk2 sample codes

Adding combobox and checkbox to GtkSheet

Sunday, June 10th, 2007

Added three more examples to show how to add combobox (or pulldown menu) and checkbox to GtkSheet:

/kksou

http://www.kksou.com/php-gtk2
The place for php-gtk2 sample codes

GtkSheet::attach()

Sunday, June 10th, 2007

Have just added three examples that illustrates the use of GtkSheet::attach().

As mentioned in the previous post, GtkSheet::attach() is extremely useful because we can attach almost any standard GTK widget to a cell. For example, in the examples above, we attach GtkEventBox so that we can set the alternate row colors. To display images in cells, we just load the image into GtkImage and attach the GtkImage to the cell.

By the way, if you are using standard attach options, you can use the method GtkSheet::attach_default() instead of GtkSheet::attach(). For details and explanations, please refer to How to use GtkSheet - Part 9 - using attach with justification?

/kksou

http://www.kksou.com/php-gtk2
The place for php-gtk2 sample codes

GtkSheet - progress update

Wednesday, June 6th, 2007

Added three more examples over the past few days:

I’ve actually tried a lot more stuff on GtkSheet. If you’re interested, below are some of the more interesting (or frustrating) ones…

Have wanted to show an example that illustrates alternate color rows. However, the method GtkSheet::range_set_background() is not imported into php-gtk2 yet. Please see http://www.kksou.com/php-gtk2/forum/viewtopic.php?p=152#152 for details. It’s still possible to display alternate color rows without using this method. Will try to come out with a sample code for this later.

Have also tried GtkSheet::new_with_custom_entry(). By default, a GtkSheet uses GtkItemEntry for the cell entry. with this method, you can change it to use a standard GtkEntry or any GtkEntry subclass. Looks easy, but took me two days to figure out how what’s that last parameter GtkType!!! It always makes me wonder why do they want to make it so difficult, when it’s so straightforward and powerful!

I’ll definitely come out with the sample code to show you how to use GtkSheet::new_with_custom_entry(). But before that, I’m still trying to understand what’s the key difference between using a GtkItemEntry for the cell, compared with using a standard GtkEntry. I think GtkItemEntry is a subclass of GtkEntry. So is there any advantage to use a GtkEntry? Anybody got any clue?

I also got stuck for three days trying to understand the method GtkSheet::attach(). This is an extremely powerful method! With this method, you can attach almost any standard widget to a cell, similar to GtkTable::attach(). However, what puzzled me is that while you will find no problem attaching a widget to a cell, it seems that the underlying GtkItemEntry is active too! So when you use a mouse, you activate the attached widget. But when you use the keyboard, you will see the underlying GtkItemEntry. It’s confusing and messy. I still don’t know whether this is a bug or a feature, and how to “deactivate” the underlying GtkItemEntry.

Any expert out there who can give me some pointers?

/kksou

http://www.kksou.com/php-gtk2
The place for php-gtk2 sample codes

Setting values of GtkSheet cells

Monday, June 4th, 2007

Have added two more sample codes on GtkSheet (Part 3 and 4):

By the way, Elizabeth Smith has given some pointers on the use of GtkHTML here:
http://www.kksou.com/php-gtk2/forum/viewtopic.php?p=149#149

Will try out this weekend and come out with some sample codes.

You might want to try it out first!

/kksou

http://www.kksou.com/php-gtk2
The place for php-gtk2 sample codes