PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 522: How to grab focus on GtkEntry in a GtkNotebook page - Part 1 - highlight all text on focus?
Written by kksou   
Monday, 08 September 2008
Problem

This is in response to Tropico's post titled "Finding first gtkentry widget into GtkNotebook page".

He would like to grab_focus on the first GtkEntry widget of one page of the notebook when the page becomes active as shown below:

How to grab focus on GtkEntry in a GtkNotebook page - Part 1 - highlight all text on focus?


Solution
  • We make use of the technique as described in How to know which tab of GtkNotebook is being clicked on? to know when a new page is active.
  • Once we know which tab the user has clicked, we can just grab focus on the GtkEntry associated with that tab.
  • In this example, I have used a global variable to store the GtkEntry's. If you're using Glade, you can easily use $glade->get_widget(entry_name) to retrieve the pointer to the GtkEntry. If you're using OOP or classes, you can neatly encapsulate this as one of the members within the class.

Sample Code
1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
29   
30   
31   
32   
33   
34   
35   
36   
37   
38   
39   
40   
41   
42   
43   
44   
<?php
$window = new GtkWindow();
$window->set_size_request(400, 240);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("   Grab focus on GtkEntry in a GtkNotebook page\n".
"Part 1 - whole text in GtkEntry is selected on focus");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$vbox->pack_start($title, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

// setup notebook
$notebook = new GtkNotebook(); // note 1
$vbox->pack_start($notebook);

// add a vbox into each of the notebook page
for ($i=1; $i<=3; ++$i) {
    // add a vbox to each page
    $page_vbox[$i] = new GtkVBox();
    $tab_label = "Tab $i";
    add_new_tab($notebook, $page_vbox[$i], $tab_label); // note 2

    $hbox = new GtkHBox();
    $page_vbox[$i]->pack_start($hbox, 0);

    // add the first alignment containing a label
    $alignment1 = new GtkAlignment(0, 0.5, 0, 0); // note 3
    $alignment1->add(new GtkLabel("Label $i: "));
    $hbox->pack_start($alignment1, 0);

    // add the second alignment containing a GtkEntry
    $alignment2 = new GtkAlignment(0, 0.5, 0, 0); // note 4
    $entry[$tab_label] = new GtkEntry(); // note 5
    $alignment2->add($entry[$tab_label]);
    $entry[$tab_label]->set_text("this is entry #$i");
    $hbox->pack_start($alignment2, 0);
}

$window->show_all();
Gtk::main();
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation

The code above is based on How to know which tab of GtkNotebook is being clicked on?

What's new here:

  1. Create a new notebook.
  2. Create 3 tabs and stuff a vbox into each page.
  3. This is the first alignment that will hold the entry label.
  4. This is the second alignment that will hold the entry.
  5. This is the GtkEntry that will be focused on. Note how we store this in an associative array with the name of the tab as key.
  6. Use has clicked a new tab. Grab focus on the GtkEntry!

Related Links

User reviews

There are no user reviews yet.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved