GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Hiding to tray is driving me crazy

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
Kazaam



Joined: 18 Aug 2008
Posts: 3

PostPosted: Mon Aug 18, 2008 8:57 am    Post subject: Hiding to tray is driving me crazy Reply with quote

Hi,
I try the first time to use a tray-icon. I want that as soon as the window gets minimized (ICONIFIED) it shall go to the tray. If I left click the tray-icon the window should come back again to the screen. But this doesn't work as I want it.

First my code:
Code: (C)
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
45
46
47

#include <gtk/gtk.h>

static void trayIconActivated(GObject *trayIcon, gpointer data);
static gboolean window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer user_data);

int main(int argc, char *argv[])
{
    gtk_init (&argc, &argv);

    GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request (window, 200, 200);

    GtkStatusIcon *trayIcon  = gtk_status_icon_new_from_icon_name(GTK_STOCK_MEDIA_STOP);
    gtk_status_icon_set_tooltip (trayIcon, "My trayicon test");
    gtk_status_icon_set_visible(trayIcon, FALSE);
    g_signal_connect(GTK_STATUS_ICON (trayIcon), "activate", GTK_SIGNAL_FUNC (trayIconActivated), window);
   

    g_signal_connect (G_OBJECT (window), "window-state-event", G_CALLBACK (window_state_event), trayIcon);

   
    gtk_widget_show(window);
    gtk_main ();
    return 0;
}

static void trayIconActivated(GObject *trayIcon, gpointer window)
{
    gtk_widget_show(GTK_WIDGET(window));
    gtk_window_deiconify(GTK_WINDOW(window));
}

static gboolean window_state_event (GtkWidget *widget, GdkEventWindowState *event, gpointer trayIcon)
{
    if(event->changed_mask == GDK_WINDOW_STATE_ICONIFIED && (event->new_window_state == GDK_WINDOW_STATE_ICONIFIED || event->new_window_state == (GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_MAXIMIZED)))
    {
        gtk_widget_hide (GTK_WIDGET(widget));
        gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), TRUE);
    }
    else if(event->changed_mask == GDK_WINDOW_STATE_WITHDRAWN && (event->new_window_state == GDK_WINDOW_STATE_ICONIFIED || event->new_window_state == (GDK_WINDOW_STATE_ICONIFIED | GDK_WINDOW_STATE_MAXIMIZED)))
    {
        gtk_status_icon_set_visible(GTK_STATUS_ICON(trayIcon), FALSE);
    }
    return TRUE;
}

You can compile the example with: gcc test.c -o test `pkg-config --cflags --libs gtk+-2.0`

So hiding is no problem. If I minimize the window it gets ICONIFIED and window_state_event reacts by hiding the window and showing the tray-icon.

But If I now left-click on the tray icon the window seems to appear for a milli-second in the applicationbar but minimized and so window_state_event directly makes it disappear again.

My suspision is:
Code: (C)
1
2
3
4
5
6
7

static void trayIconActivated(GObject *trayIcon, gpointer window)
{
    gtk_widget_show(GTK_WIDGET(window));
    gtk_window_deiconify(GTK_WINDOW(window));
}

gtk_widget_show just makes the window show ICONIFIED and even before I can change this with gtk_window_deiconify the window_state_event makes the window disappear again.


Any idea how to solve this problem or where the bug in my code is?
Back to top
dreblen
Never Seen the Sunlight


Joined: 14 Jun 2007
Posts: 526
Location: Falun, WI USA

PostPosted: Mon Aug 18, 2008 4:07 pm    Post subject: Reply with quote

well, on windows it works okay, and works perfectly if you change:
Code: (C)
1
2
3
4
5
static void trayIconActivated(GObject *trayIcon, gpointer window)
{
    gtk_widget_show(GTK_WIDGET(window));
    gtk_window_deiconify(GTK_WINDOW(window));
}

to
Code: (C)
1
2
3
4
5
static void trayIconActivated(GObject *trayIcon, gpointer window)
{
    gtk_window_deiconify(GTK_WINDOW(window));
    gtk_widget_show(GTK_WIDGET(window));
}

does changing this make it work for you?
Back to top
Kazaam



Joined: 18 Aug 2008
Posts: 3

PostPosted: Mon Aug 18, 2008 5:08 pm    Post subject: Reply with quote

No I also already tried it but I have the exact same behavior like before :(

It seems I can't deiconify a withdrawn window or widget.show changes this again.

OS: linux


But:
Code: (C)
1
2
3
4
5
6
7
8

static void trayIconActivated(GObject *trayIcon, gpointer window)
{
   gtk_window_deiconify(GTK_WINDOW(window));
   gtk_widget_show(window);
   gtk_window_deiconify(GTK_WINDOW(window));
}

makes it work 50% , so every second time I wanna show the window again it works and the other time it is just as I described above.
Back to top
dreblen
Never Seen the Sunlight


Joined: 14 Jun 2007
Posts: 526
Location: Falun, WI USA

PostPosted: Tue Aug 19, 2008 6:35 pm    Post subject: Reply with quote

Well, using my version (with gtk_widget_show second) on kubuntu 8.04 it works fine.
which distribution do you use?
Back to top
ramesh
GTK+ Guru


Joined: 16 Nov 2006
Posts: 173
Location: INDIA

PostPosted: Wed Aug 20, 2008 4:53 am    Post subject: Reply with quote

a small question from my side.
how can we show the trayicon at the time of showing the window.
and if we left click (single click) the mouse button on the tray icon the window should be appear on the screen, and again if we left click(single click) it should minimised in tray icon

is it possible
Back to top
Kazaam



Joined: 18 Aug 2008
Posts: 3

PostPosted: Wed Aug 20, 2008 8:24 am    Post subject: Reply with quote

dreblen wrote:
Well, using my version (with gtk_widget_show second) on kubuntu 8.04 it works fine.
which distribution do you use?


Thank you dreblen! I could narrow the problem: I tested it always with ubuntu 8.04 and centos 4.6 both with fluxbox running.

Now I tested it with gnome and it works just fine. But this is really confusing because the tray-con of every other application I use like xchat, pidgin, vidalia, sylpheed, wicd, ... works just fine with fluxbox. Any idea what could be the reason of this?

@ramesh: yes take a look at my example code and you will find all necessary function in there.
Back to top
ramesh
GTK+ Guru


Joined: 16 Nov 2006
Posts: 173
Location: INDIA

PostPosted: Thu Aug 21, 2008 11:41 am    Post subject: Reply with quote

Kazaam wrote:

@ramesh: yes take a look at my example code and you will find all necessary function in there.


hi
no i am not getting that requirement.
and if we close the window the trayicon still present its ok, but when we activate the tray icon it is showing some errors.(not showing window).
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP