diamondx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /***********************************************************************
  2. DiamondX plugin
  3. Example XEmbed-aware Mozilla browser plugin by Adobe.
  4. Copyright (c) 2007 Adobe Systems Incorporated
  5. Permission is hereby granted, free of charge, to any person obtaining a
  6. copy of this software and associated documentation files (the "Software"),
  7. to deal in the Software without restriction, including without limitation
  8. the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. and/or sell copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  16. THE AUTHORS OR
  17. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  19. OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. SOFTWARE. *
  21. ***********************************************************************/
  22. #include "npapi.h"
  23. #include "diamondx.h"
  24. static NPWindow windowlessWindow;
  25. static void ContextMenuCallback1(PluginInstance *plugin)
  26. {
  27. printf(" first menu item clicked\n");
  28. }
  29. static void ContextMenuCallback2(PluginInstance *plugin)
  30. {
  31. printf(" second menu item clicked\n");
  32. }
  33. static gboolean CanvasEventCallback(GtkWidget *widget, GdkEvent *event, gpointer user_data)
  34. {
  35. gboolean handled = 0;
  36. PluginInstance *plugin = (PluginInstance *)user_data;
  37. GdkEventKey *key;
  38. GdkEventFocus *focus;
  39. GdkEventMotion *motion;
  40. GdkEventButton *button;
  41. GdkEventExpose *expose;
  42. /* only honor expose events if a dialog is up */
  43. if (plugin->dialog_up && event->type != GDK_EXPOSE)
  44. return 1;
  45. switch(event->type) {
  46. case GDK_EXPOSE:
  47. expose = (GdkEventExpose *)event;
  48. printf(" (canvas): expose event (x, y, w, h) = (%d, %d, %d, %d)\n",
  49. expose->area.x, expose->area.y, expose->area.width, expose->area.height);
  50. gdk_draw_rgb_image (
  51. plugin->canvas->window,
  52. plugin->canvas->style->fg_gc[GTK_STATE_NORMAL],
  53. expose->area.x,
  54. expose->area.y,
  55. expose->area.width,
  56. expose->area.height,
  57. GDK_RGB_DITHER_MAX,
  58. plugin->four_quads + (expose->area.y * plugin->width + expose->area.x) * 3,
  59. plugin->width * 3);
  60. handled = 1;
  61. break;
  62. case GDK_FOCUS_CHANGE:
  63. focus = (GdkEventFocus *)event;
  64. printf(" (canvas): focus change event (%s)\n", (focus->in) ? "in" : "out");
  65. handled = 1;
  66. break;
  67. case GDK_ENTER_NOTIFY:
  68. printf(" (canvas): enter event\n");
  69. handled = 1;
  70. break;
  71. case GDK_LEAVE_NOTIFY:
  72. printf(" (canvas): leave event\n");
  73. handled = 1;
  74. break;
  75. case GDK_MOTION_NOTIFY:
  76. motion = (GdkEventMotion *)event;
  77. printf(" (canvas): mouse motion -> (%f, %f)\n", motion->x, motion->y);
  78. handled = 1;
  79. break;
  80. case GDK_BUTTON_PRESS:
  81. button = (GdkEventButton *)event;
  82. printf(" (canvas): button %d pressed @ (%f, %f)\n", button->button, button->x, button->y);
  83. if (button->button == 1)
  84. {
  85. /* take the focus into our own hands */
  86. gtk_widget_grab_focus(plugin->canvas);
  87. /* handle first quadrant */
  88. if (((uint32)button->x <= (plugin->width / 2)) &&
  89. ((uint32)button->y <= (plugin->height / 2)))
  90. {
  91. printf(" first quadrant clicked\n");
  92. /* make a generic modal dialog */
  93. GtkWidget *dialog = gtk_dialog_new_with_buttons(
  94. "Hey!",
  95. GTK_WINDOW(plugin->container),
  96. GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
  97. GTK_STOCK_OK,
  98. GTK_RESPONSE_OK,
  99. NULL);
  100. plugin->dialog_up = 1;
  101. gtk_dialog_run(GTK_DIALOG(dialog));
  102. gtk_widget_destroy(GTK_WIDGET(dialog));
  103. plugin->dialog_up = 0;
  104. }
  105. else
  106. if (((uint32)button->x > (plugin->width / 2)) &&
  107. ((uint32)button->y <= (plugin->height / 2)))
  108. {
  109. printf(" second quadrant clicked\n");
  110. /* make a GTK file selection dialog */
  111. GtkWidget *dialog = gtk_file_chooser_dialog_new(
  112. "Open a file...",
  113. GTK_WINDOW(plugin->container),
  114. GTK_FILE_CHOOSER_ACTION_OPEN,
  115. GTK_STOCK_OK,
  116. GTK_RESPONSE_OK,
  117. GTK_STOCK_CANCEL,
  118. GTK_RESPONSE_CANCEL,
  119. NULL);
  120. gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), 1);
  121. plugin->dialog_up = 1;
  122. gtk_dialog_run(GTK_DIALOG(dialog));
  123. gtk_widget_destroy(GTK_WIDGET(dialog));
  124. plugin->dialog_up = 0;
  125. }
  126. else
  127. if (((uint32)button->x <= (plugin->width / 2)) &&
  128. ((uint32)button->y > (plugin->height / 2)))
  129. {
  130. printf(" third quadrant clicked\n");
  131. }
  132. else
  133. {
  134. printf(" fourth quadrant clicked\n");
  135. }
  136. }
  137. else
  138. if (button->button == 3)
  139. {
  140. /* context menu */
  141. GtkWidget *menu;
  142. GtkWidget *menu_item;
  143. menu = gtk_menu_new();
  144. menu_item = gtk_check_menu_item_new_with_label ("Hey, is this thing on?");
  145. gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item), 1);
  146. gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
  147. g_signal_connect_swapped(G_OBJECT(menu_item), "activate",
  148. G_CALLBACK(ContextMenuCallback1), plugin);
  149. gtk_widget_show (menu_item);
  150. menu_item = gtk_separator_menu_item_new();
  151. gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
  152. gtk_widget_show (menu_item);
  153. menu_item = gtk_menu_item_new_with_label ("You suck! Get off the stage!");
  154. gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
  155. g_signal_connect_swapped(G_OBJECT(menu_item), "activate",
  156. G_CALLBACK(ContextMenuCallback2), plugin);
  157. gtk_widget_show (menu_item);
  158. gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
  159. 0, gtk_get_current_event_time());
  160. }
  161. handled = 1;
  162. break;
  163. case GDK_BUTTON_RELEASE:
  164. button = (GdkEventButton *)event;
  165. printf(" (canvas): button %d released @ (%f, %f)\n", button->button, button->x, button->y);
  166. handled = 1;
  167. break;
  168. case GDK_KEY_PRESS:
  169. case GDK_KEY_RELEASE:
  170. key = (GdkEventKey *)event;
  171. printf(" (canvas): key %s, code = %d (0x%X)\n",
  172. (event->type == GDK_KEY_PRESS) ? "pressed" : "released", key->keyval, key->keyval);
  173. handled = 1;
  174. break;
  175. case GDK_SCROLL:
  176. printf(" (canvas): scroll event\n");
  177. handled = 0;
  178. break;
  179. default:
  180. printf(" (canvas): unhandled event %d\n", event->type);
  181. break;
  182. }
  183. return handled;
  184. }
  185. NPError DiamondXSetWindow(NPP instance, NPWindow* window)
  186. {
  187. PluginInstance* This;
  188. NPSetWindowCallbackStruct *ws_info;
  189. int quads, x, y;
  190. guchar *row, *index;
  191. guchar r1, g1, b1;
  192. guchar r2, g2, b2;
  193. int split;
  194. int half_width, half_height;
  195. gboolean positive_triangle;
  196. int xembedSupported = 0;
  197. if (instance == NULL)
  198. return NPERR_INVALID_INSTANCE_ERROR;
  199. This = (PluginInstance*) instance->pdata;
  200. if (This == NULL)
  201. return NPERR_INVALID_INSTANCE_ERROR;
  202. ws_info = (NPSetWindowCallbackStruct *)window->ws_info;
  203. /* Mozilla likes to re-run its greatest hits */
  204. if ((window == This->window) &&
  205. (window->x == This->x) &&
  206. (window->y == This->y) &&
  207. (window->width == This->width) &&
  208. (window->height == This->height)) {
  209. printf(" (window re-run; returning)\n");
  210. return NPERR_NO_ERROR;
  211. }
  212. NPN_GetValue(instance, NPNVSupportsXEmbedBool, &xembedSupported);
  213. if (!xembedSupported)
  214. {
  215. printf("DiamondX: XEmbed not supported\n");
  216. return NPERR_GENERIC_ERROR;
  217. }
  218. This->window = window;
  219. This->x = window->x;
  220. This->y = window->y;
  221. This->width = window->width;
  222. This->height = window->height;
  223. half_width = This->width / 2;
  224. half_height = This->height / 2;
  225. /* the following conditional block is for handling the experimental
  226. * windowless mode */
  227. if (This->windowless)
  228. {
  229. Display *disp = This->window->window;
  230. Drawable drawable = (Drawable)This->window->ws_info;
  231. GC gc = DefaultGC(disp, DefaultScreen(disp));
  232. if (!disp || ! drawable)
  233. return NPERR_GENERIC_ERROR;
  234. if (!This->ximage)
  235. {
  236. This->ximage = XCreateImage(
  237. disp, /* actually an X display */
  238. DefaultVisual(disp, DefaultScreen(disp)), /* default visual */
  239. 32, /* image depth */
  240. ZPixmap,
  241. 0,
  242. NULL,
  243. This->width,
  244. This->height,
  245. BitmapPad(disp), /* scanline pad */
  246. 0
  247. );
  248. if (This->ximage)
  249. {
  250. This->ximage->data = NPN_MemAlloc(This->ximage->height * This->ximage->bytes_per_line);
  251. if (!This->ximage->data)
  252. {
  253. XDestroyImage(This->ximage);
  254. This->ximage = NULL;
  255. }
  256. }
  257. }
  258. XPutImage(
  259. disp,
  260. drawable,
  261. gc,
  262. This->ximage,
  263. 0, /* source x */
  264. 0, /* source y */
  265. 0, /* dest x */
  266. 0, /* dest y */
  267. This->width,
  268. This->height);
  269. return NPERR_NO_ERROR;
  270. }
  271. This->four_quads = (guchar *)NPN_MemAlloc(This->width * This->height * 3);
  272. if (!This->four_quads)
  273. return NPERR_GENERIC_ERROR;
  274. /* draw the diamond */
  275. for (quads = 0; quads < 4; quads++) {
  276. switch (quads) {
  277. case 0:
  278. row = This->four_quads;
  279. r1 = 0xFF; g1 = 0x00; b1 = 0x00;
  280. r2 = 0xFF; g2 = 0xFF; b2 = 0xFF;
  281. positive_triangle = TRUE;
  282. break;
  283. case 1:
  284. row = This->four_quads + (This->width / 2) * 3;
  285. r1 = 0xFF; g1 = 0xFF; b1 = 0xFF;
  286. r2 = 0x00; g2 = 0xFF; b2 = 0x00;
  287. positive_triangle = FALSE;
  288. break;
  289. case 2:
  290. row = This->four_quads + (This->height / 2) * (This->width * 3);
  291. r1 = 0x00; g1 = 0x00; b1 = 0xFF;
  292. r2 = 0xFF; g2 = 0xFF; b2 = 0xFF;
  293. positive_triangle = FALSE;
  294. break;
  295. case 3:
  296. row = This->four_quads + ((This->height / 2) * (This->width * 3)) + ((This->width / 2) * 3);
  297. r1 = 0xFF; g1 = 0xFF; b1 = 0xFF;
  298. r2 = 0x80; g2 = 0x80; b2 = 0x80;
  299. positive_triangle = TRUE;
  300. break;
  301. }
  302. for (y = 0; y < half_height; y++)
  303. {
  304. index = row;
  305. if (positive_triangle)
  306. split = (half_height - y) * half_width / half_height;
  307. else
  308. split = y * half_width / half_height;
  309. for (x = 0; x < half_width; x++)
  310. {
  311. if (x >= split)
  312. {
  313. *index++ = r1;
  314. *index++ = g1;
  315. *index++ = b1;
  316. }
  317. else
  318. {
  319. *index++ = r2;
  320. *index++ = g2;
  321. *index++ = b2;
  322. }
  323. }
  324. row += This->width * 3;
  325. }
  326. }
  327. /* create a GtkPlug container and plot a drawing canvas inside */
  328. This->container = gtk_plug_new((GdkNativeWindow)window->window);
  329. This->canvas = gtk_drawing_area_new();
  330. /* make sure the canvas is capable of receiving focus */
  331. GTK_WIDGET_SET_FLAGS (GTK_WIDGET(This->canvas), GTK_CAN_FOCUS);
  332. /* all the events that our canvas wants to receive */
  333. gtk_widget_add_events(
  334. This->canvas,
  335. GDK_BUTTON_PRESS_MASK |
  336. GDK_BUTTON_RELEASE_MASK |
  337. GDK_KEY_PRESS_MASK |
  338. GDK_KEY_RELEASE_MASK |
  339. GDK_POINTER_MOTION_MASK |
  340. GDK_SCROLL_MASK |
  341. GDK_EXPOSURE_MASK |
  342. GDK_VISIBILITY_NOTIFY_MASK |
  343. GDK_ENTER_NOTIFY_MASK |
  344. GDK_LEAVE_NOTIFY_MASK |
  345. GDK_FOCUS_CHANGE_MASK
  346. );
  347. g_signal_connect(G_OBJECT(This->canvas), "event", G_CALLBACK(CanvasEventCallback), This);
  348. gtk_widget_show(This->canvas);
  349. gtk_container_add(GTK_CONTAINER(This->container), This->canvas);
  350. gtk_widget_show(This->container);
  351. gdk_draw_rgb_image (This->canvas->window, This->canvas->style->fg_gc[GTK_STATE_NORMAL],
  352. 0, 0, This->width, This->height,
  353. GDK_RGB_DITHER_MAX, This->four_quads, This->width * 3);
  354. return NPERR_NO_ERROR;
  355. }
  356. int16 DiamondXHandleEvent(NPP instance, void* event)
  357. {
  358. XGraphicsExposeEvent exposeEvent;
  359. XEvent *nsEvent;
  360. nsEvent = (XEvent *)event;
  361. exposeEvent = nsEvent->xgraphicsexpose;
  362. printf(" event: x, y, w, h = %d, %d, %d, %d; display @ %p, window/drawable = %d\n",
  363. exposeEvent.x,
  364. exposeEvent.y,
  365. exposeEvent.width,
  366. exposeEvent.height,
  367. exposeEvent.display,
  368. exposeEvent.drawable);
  369. windowlessWindow.window = exposeEvent.display;
  370. windowlessWindow.x = exposeEvent.x;
  371. windowlessWindow.y = exposeEvent.y;
  372. windowlessWindow.width = exposeEvent.width;
  373. windowlessWindow.height = exposeEvent.height;
  374. windowlessWindow.ws_info = (void *)exposeEvent.drawable;
  375. NPP_SetWindow(instance, &windowlessWindow);
  376. return 0;
  377. }