aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/AutoModalDialog.cpp
blob: 03473085059abd4ff4a4f596bea350d31a2ccaed (plain) (blame)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* 
 *  Copyright © 2006, 2008 Christian Persch
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 2.1, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *  Some code taken from mozilla/embedding/components/windowwatcher/src/nsPromptService.cpp
 *  which was under MPL/LGPL/GPL tri-licence and is here being used under the licence above.
 *  Original notice:
 *
 *   The Original Code is mozilla.org code.
 *
 *   The Initial Developer of the Original Code is
 *   Netscape Communications Corporation.
 *   Portions created by the Initial Developer are Copyright (C) 2001
 *   the Initial Developer. All Rights Reserved.
 */

#include <xpcom-config.h>
#include <config.h>

#ifdef HAVE_GECKO_1_9
#include <nsIDOMDocumentEvent.h>
#include <nsIDOMDocument.h>
#include <nsIDOMEvent.h>
#include <nsIDOMEventTarget.h>
#include <nsIPrivateDOMEvent.h>
#include <nsPIDOMWindow.h>
#include <nsServiceManagerUtils.h>
#include <nsThreadUtils.h>
#endif /* HAVE_GECKO_1_9 */

#include <gtk/gtkdialog.h>

#include "EphyUtils.h"

#include "AutoModalDialog.h"

AutoModalDialog::AutoModalDialog (nsIDOMWindow *aWindow,
                                  PRBool aNotifyDOM)
  : mWindow (aWindow),
    mStack (),
    mModalState (aWindow),
#ifdef HAVE_GECKO_1_9
    mDefaultEnabled (DispatchEvent ("DOMWillOpenModalDialog", aNotifyDOM)),
#else
    mDefaultEnabled (PR_TRUE),
#endif
    mContextPushed (NS_SUCCEEDED (mStack.Init ()))
{
}

AutoModalDialog::~AutoModalDialog ()
{
#ifdef HAVE_GECKO_1_9
  if (mDefaultEnabled) {
    DispatchEvent ("DOMModalDialogClosed", PR_TRUE);
  }
#endif /* HAVE_GECKO_1_9 */
}

GtkWindow *
AutoModalDialog::GetParent ()
{
  return GTK_WINDOW (EphyUtils::FindGtkParent (mWindow));
}

int
AutoModalDialog::Run (GtkDialog *aDialog)
{
  NS_ASSERTION (ShouldShow(), "Calling ::Run on a prevented dialogue!");

  nsCOMPtr<nsPIDOMWindow> pWindow (do_QueryInterface (mWindow));

  // Reset popup state while opening a modal dialog, and firing
  // events about the dialog, to prevent the current state from
  // being active the whole time a modal dialog is open.
  nsAutoPopupStatePusher popupStatePusher (pWindow, openAbused);
  
#if 1
  return gtk_dialog_run (aDialog);
#else
  /* Do NOT use gtk_dialog_run here, since it blocks the network thread!
   * See https://bugzilla.mozilla.org/show_bug.cgi?id=338225
   */
  
  g_object_ref_sink (aDialog);
  mResponse = GTK_RESPONSE_DELETE_EVENT;

  gulong responseHandler = g_signal_connect (aDialog, "response",
                                             G_CALLBACK (ResponseCallback),
                                             reinterpret_cast<void*>(this));
  gulong deleteHandler = g_signal_connect (aDialog, "delete-event",
                                           G_CALLBACK (DeleteCallback), NULL);

  gtk_window_present (GTK_WINDOW (aDialog));

  nsCOMPtr<nsIThread> thread (do_GetCurrentThread ());
  NS_ASSERTION (thread, "No UI thread?");
  
  mContinueModalLoop = PR_TRUE;
  while (mContinueModalLoop) {
    if (!NS_ProcessNextEvent (thread))
      break;
  }

  g_signal_handler_disconnect (aDialog, responseHandler);
  g_signal_handler_disconnect (aDialog, deleteHandler);
  g_object_unref (aDialog);

  return mResponse;
#endif
}

#ifdef HAVE_GECKO_1_9

PRBool
AutoModalDialog::DispatchEvent (const char *aEventName,
                                PRBool aDoNotify)
{
  if (!mWindow || !aDoNotify) {
    return PR_TRUE;
  }

  nsCOMPtr<nsIDOMDocument> domdoc;
  mWindow->GetDocument (getter_AddRefs (domdoc));

  nsCOMPtr<nsIDOMDocumentEvent> docevent (do_QueryInterface (domdoc));
  nsCOMPtr<nsIDOMEvent> event;

  PRBool defaultActionEnabled = PR_TRUE;

  if (docevent) {
    docevent->CreateEvent (NS_LITERAL_STRING ("Events"), getter_AddRefs (event));

    nsCOMPtr<nsIPrivateDOMEvent> privateEvent (do_QueryInterface (event));
    if (privateEvent) {
      event->InitEvent (NS_ConvertASCIItoUTF16 (aEventName), PR_TRUE, PR_TRUE);

      privateEvent->SetTrusted(PR_TRUE);

      nsCOMPtr<nsIDOMEventTarget> target (do_QueryInterface (mWindow));

      target->DispatchEvent (event, &defaultActionEnabled);
    }
  }

  return defaultActionEnabled;
}

/* static */ void PR_CALLBACK
AutoModalDialog::ResponseCallback (GtkWidget *aDialog,
                                   int aResponse,
                                   void *aData)
{
  AutoModalDialog *obj = reinterpret_cast<AutoModalDialog*>(aData);

  gtk_widget_hide (aDialog);
  obj->mResponse = aResponse;
  obj->mContinueModalLoop = PR_FALSE;
}

/* static */ gboolean PR_CALLBACK
AutoModalDialog::DeleteCallback (GtkWidget *aDialog,
                                 void *aEvent,
                                 void *aData)
{
  gtk_dialog_response (GTK_DIALOG (aDialog), GTK_RESPONSE_DELETE_EVENT);
  return TRUE;
}

#endif /* HAVE_GECKO_1_9 */