CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Anyone familiar with GTK signals?

    When working with GTK signals, a callback function looks something like this (let's take the "delete-event" signal as an example):-

    Code:
    static gboolean
    delete_event(GtkWidget* window, GdkEvent* event, gointer data)
    {
          gboolean answer =  /* ask the user a question here */;
    
          if (answer)
             return false;
          else
             return true;
    }
    and the actual signal would typically be connected to its callback function like this:-

    Code:
          g_signal_connect(G_OBJECT (some_window), "delete-event", G_CALLBACK (delete_event), NULL);
    Is it safe to connect a callback function that's already been connected earlier? - e.g.

    Code:
          g_signal_connect(G_OBJECT (some_window), "delete-event", G_CALLBACK (delete_event), NULL);
    
          // somewhere else in the program....
          g_signal_connect(G_OBJECT (some_window), "delete-event", G_CALLBACK (delete_event), NULL);
    If it isn't safe, is there a way to tell whether or not a callback function is already servicing a particular signal?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Anyone familiar with GTK signals?

    Hmm. Good question, I'm not sure. When I did this I put all of my g_signal_connect() calls in object constructors so it wasn't a problem.

    I can suggest a test, however----try it, and see whether or not your function gets called twice. That's the worst I would expect to happen, if anything.

    If memory serves, the callback sequence is essentially a stack; the top gets called first, then the next, and so on. The return value of any callback determines whether or not the next one in the stack is called, so be careful---you don't want to accidentally disable built-in GTK callbacks.

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Anyone familiar with GTK signals?

    Thanks Lindley. I traced my way through the source code and it looks like you're probably right. New response functions get add at the end of some sort of container - but without any checks to see if they've already been added.

    I've changed strategy slightly to use libsigc++. I found that it offers an object called sigc::connection. This can be used to check whether a particular connection is already activated, so it's exactly what I need in this case. .
    "A problem well stated is a problem half solved.” - Charles F. Kettering

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured