~indicator-applet-developers/unity-gtk-module/trunk.14.04

« back to all changes in this revision

Viewing changes to lib/unity-gtk-action-group.c

  • Committer: William Hua
  • Date: 2014-05-07 19:55:08 UTC
  • Revision ID: william.hua@canonical.com-20140507195508-ydpp7jj3pgjwm6t3
Add submenu actions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
272
272
 
273
273
      if (action != NULL)
274
274
        {
275
 
          if (action->items_by_name != NULL)
 
275
          if (g_strcmp0 (name, action->name) == 0)
276
276
            {
277
 
              if (value != NULL)
278
 
                {
279
 
                  const gchar *name;
280
 
                  UnityGtkMenuItem *item;
281
 
 
282
 
                  g_return_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING));
283
 
 
284
 
                  name = g_variant_get_string (value, NULL);
285
 
                  item = g_hash_table_lookup (action->items_by_name, name);
286
 
 
287
 
                  if (item == NULL || !unity_gtk_menu_item_is_check (item))
288
 
                    {
289
 
                      g_warn_if_reached ();
290
 
 
291
 
                      value = NULL;
292
 
                    }
293
 
                  else
294
 
                    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item->menu_item), TRUE);
295
 
                }
296
 
 
297
 
              if (value == NULL)
298
 
                {
299
 
                  GHashTableIter iter;
300
 
                  gpointer value;
301
 
 
302
 
                  g_hash_table_iter_init (&iter, action->items_by_name);
303
 
                  while (g_hash_table_iter_next (&iter, NULL, &value))
304
 
                    {
305
 
                      UnityGtkMenuItem *item = value;
306
 
 
307
 
                      if (unity_gtk_menu_item_is_check (item))
308
 
                        gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item->menu_item), FALSE);
309
 
                    }
310
 
                }
 
277
              if (action->items_by_name != NULL)
 
278
                {
 
279
                  if (value != NULL)
 
280
                    {
 
281
                      const gchar *name;
 
282
                      UnityGtkMenuItem *item;
 
283
 
 
284
                      g_return_if_fail (g_variant_is_of_type (value, G_VARIANT_TYPE_STRING));
 
285
 
 
286
                      name = g_variant_get_string (value, NULL);
 
287
                      item = g_hash_table_lookup (action->items_by_name, name);
 
288
 
 
289
                      if (item == NULL || !unity_gtk_menu_item_is_check (item))
 
290
                        {
 
291
                          g_warn_if_reached ();
 
292
 
 
293
                          value = NULL;
 
294
                        }
 
295
                      else
 
296
                        gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item->menu_item), TRUE);
 
297
                    }
 
298
 
 
299
                  if (value == NULL)
 
300
                    {
 
301
                      GHashTableIter iter;
 
302
                      gpointer value;
 
303
 
 
304
                      g_hash_table_iter_init (&iter, action->items_by_name);
 
305
                      while (g_hash_table_iter_next (&iter, NULL, &value))
 
306
                        {
 
307
                          UnityGtkMenuItem *item = value;
 
308
 
 
309
                          if (unity_gtk_menu_item_is_check (item))
 
310
                            gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item->menu_item), FALSE);
 
311
                        }
 
312
                    }
 
313
                }
 
314
              else if (action->item != NULL && unity_gtk_menu_item_is_check (action->item))
 
315
                {
 
316
                  g_return_if_fail (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN));
 
317
 
 
318
                  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (action->item->menu_item), g_variant_get_boolean (value));
 
319
                }
 
320
              else
 
321
                g_warn_if_fail (value == NULL);
 
322
 
 
323
              return;
311
324
            }
312
 
          else if (action->item != NULL && unity_gtk_menu_item_is_check (action->item))
 
325
          else if (g_strcmp0 (name, action->subname) == 0)
313
326
            {
 
327
              GtkWidget *submenu;
 
328
 
314
329
              g_return_if_fail (value != NULL && g_variant_is_of_type (value, G_VARIANT_TYPE_BOOLEAN));
315
 
 
316
 
              gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (action->item->menu_item), g_variant_get_boolean (value));
 
330
              g_return_if_fail (action->item != NULL && action->item->menu_item != NULL);
 
331
 
 
332
              submenu = gtk_menu_item_get_submenu (action->item->menu_item);
 
333
 
 
334
              g_return_if_fail (submenu != NULL);
 
335
 
 
336
              g_signal_emit_by_name (submenu, g_variant_get_boolean (value) ? "show" : "hide");
 
337
 
 
338
              return;
317
339
            }
318
340
          else
319
 
            g_warn_if_fail (value == NULL);
320
 
 
321
 
          return;
 
341
            g_warn_if_reached ();
322
342
        }
 
343
      else
 
344
        g_warn_if_reached ();
323
345
    }
324
346
  else
325
347
    g_warn_if_reached ();
359
381
 
360
382
      if (action != NULL)
361
383
        {
362
 
          if (action->items_by_name != NULL)
363
 
            {
364
 
              const gchar *name;
365
 
              UnityGtkMenuItem *item;
366
 
 
367
 
              g_return_if_fail (parameter != NULL && g_variant_is_of_type (parameter, G_VARIANT_TYPE_STRING));
368
 
 
369
 
              name = g_variant_get_string (parameter, NULL);
370
 
              item = g_hash_table_lookup (action->items_by_name, name);
371
 
 
372
 
              if (item != NULL)
373
 
                unity_gtk_menu_item_activate (item);
374
 
 
375
 
              g_action_group_action_state_changed (G_ACTION_GROUP (group), action->name, parameter);
376
 
            }
377
 
          else if (action->item != NULL)
378
 
            {
379
 
              if (unity_gtk_menu_item_get_draw_as_radio (action->item))
380
 
                g_warn_if_fail (g_variant_is_of_type (parameter, G_VARIANT_TYPE_STRING));
381
 
              else
382
 
                g_warn_if_fail (parameter == NULL);
383
 
 
384
 
              unity_gtk_menu_item_activate (action->item);
385
 
            }
386
 
 
387
 
          return;
 
384
          if (g_strcmp0 (name, action->name) == 0)
 
385
            {
 
386
              if (action->items_by_name != NULL)
 
387
                {
 
388
                  const gchar *name;
 
389
                  UnityGtkMenuItem *item;
 
390
 
 
391
                  g_return_if_fail (parameter != NULL && g_variant_is_of_type (parameter, G_VARIANT_TYPE_STRING));
 
392
 
 
393
                  name = g_variant_get_string (parameter, NULL);
 
394
                  item = g_hash_table_lookup (action->items_by_name, name);
 
395
 
 
396
                  if (item != NULL)
 
397
                    unity_gtk_menu_item_activate (item);
 
398
 
 
399
                  g_action_group_action_state_changed (G_ACTION_GROUP (group), action->name, parameter);
 
400
                }
 
401
              else if (action->item != NULL)
 
402
                {
 
403
                  if (unity_gtk_menu_item_get_draw_as_radio (action->item))
 
404
                    g_warn_if_fail (g_variant_is_of_type (parameter, G_VARIANT_TYPE_STRING));
 
405
                  else
 
406
                    g_warn_if_fail (parameter == NULL);
 
407
 
 
408
                  unity_gtk_menu_item_activate (action->item);
 
409
                }
 
410
 
 
411
              return;
 
412
            }
 
413
          else
 
414
            g_warn_if_reached ();
388
415
        }
 
416
      else
 
417
        g_warn_if_reached ();
389
418
    }
390
419
  else
391
420
    g_warn_if_reached ();
419
448
 
420
449
      if (action != NULL)
421
450
        {
422
 
          if (enabled != NULL)
 
451
          if (g_strcmp0 (name, action->name) == 0)
423
452
            {
424
 
              if (action->items_by_name != NULL)
425
 
                {
426
 
                  GHashTableIter iter;
427
 
                  gpointer value;
428
 
 
429
 
                  *enabled = FALSE;
430
 
 
431
 
                  g_hash_table_iter_init (&iter, action->items_by_name);
432
 
                  while (!*enabled && g_hash_table_iter_next (&iter, NULL, &value))
433
 
                    *enabled = unity_gtk_menu_item_is_sensitive (value);
434
 
                }
435
 
              else
436
 
                *enabled = action->item != NULL && unity_gtk_menu_item_is_sensitive (action->item);
 
453
              if (enabled != NULL)
 
454
                {
 
455
                  if (action->items_by_name != NULL)
 
456
                    {
 
457
                      GHashTableIter iter;
 
458
                      gpointer value;
 
459
 
 
460
                      *enabled = FALSE;
 
461
 
 
462
                      g_hash_table_iter_init (&iter, action->items_by_name);
 
463
                      while (!*enabled && g_hash_table_iter_next (&iter, NULL, &value))
 
464
                        *enabled = unity_gtk_menu_item_is_sensitive (value);
 
465
                    }
 
466
                  else
 
467
                    *enabled = action->item != NULL && unity_gtk_menu_item_is_sensitive (action->item);
 
468
                }
 
469
 
 
470
              if (parameter_type != NULL)
 
471
                {
 
472
                  if (action->items_by_name != NULL || (action->item != NULL && unity_gtk_menu_item_get_draw_as_radio (action->item)))
 
473
                    *parameter_type = G_VARIANT_TYPE_STRING;
 
474
                  else
 
475
                    *parameter_type = NULL;
 
476
                }
 
477
 
 
478
              if (state_type != NULL)
 
479
                {
 
480
                  if (action->items_by_name != NULL || (action->item != NULL && unity_gtk_menu_item_get_draw_as_radio (action->item)))
 
481
                    *state_type = G_VARIANT_TYPE_STRING;
 
482
                  else if (action->item != NULL && unity_gtk_menu_item_is_check (action->item))
 
483
                    *state_type = G_VARIANT_TYPE_BOOLEAN;
 
484
                  else
 
485
                    *state_type = NULL;
 
486
                }
 
487
 
 
488
              if (state_hint != NULL)
 
489
                {
 
490
                  if (action->items_by_name != NULL)
 
491
                    {
 
492
                      GVariantBuilder builder;
 
493
                      GHashTableIter iter;
 
494
                      gpointer key;
 
495
 
 
496
                      g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
 
497
 
 
498
                      g_hash_table_iter_init (&iter, action->items_by_name);
 
499
                      while (g_hash_table_iter_next (&iter, &key, NULL))
 
500
                        g_variant_builder_add (&builder, "s", key);
 
501
 
 
502
                      *state_hint = g_variant_ref_sink (g_variant_builder_end (&builder));
 
503
                    }
 
504
                  else if (action->item != NULL && unity_gtk_menu_item_is_check (action->item))
 
505
                    {
 
506
                      GVariantBuilder builder;
 
507
 
 
508
                      if (unity_gtk_menu_item_get_draw_as_radio (action->item))
 
509
                        {
 
510
                          g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
 
511
                          g_variant_builder_add (&builder, "s", action->name);
 
512
                          *state_hint = g_variant_ref_sink (g_variant_builder_end (&builder));
 
513
                        }
 
514
                      else
 
515
                        {
 
516
                          g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
 
517
                          g_variant_builder_add (&builder, "b", FALSE);
 
518
                          g_variant_builder_add (&builder, "b", TRUE);
 
519
                          *state_hint = g_variant_ref_sink (g_variant_builder_end (&builder));
 
520
                        }
 
521
                    }
 
522
                  else
 
523
                    *state_hint = NULL;
 
524
                }
 
525
 
 
526
              if (state != NULL)
 
527
                {
 
528
                  if (action->items_by_name != NULL)
 
529
                    {
 
530
                      GHashTableIter iter;
 
531
                      gpointer key;
 
532
                      gpointer value;
 
533
 
 
534
                      *state = NULL;
 
535
 
 
536
                      g_hash_table_iter_init (&iter, action->items_by_name);
 
537
                      while (*state == NULL && g_hash_table_iter_next (&iter, &key, &value))
 
538
                        if (unity_gtk_menu_item_is_active (value))
 
539
                          *state = g_variant_ref_sink (g_variant_new_string (key));
 
540
                    }
 
541
                  else if (action->item != NULL && unity_gtk_menu_item_is_check (action->item))
 
542
                    {
 
543
                      if (unity_gtk_menu_item_get_draw_as_radio (action->item))
 
544
                        {
 
545
                          if (unity_gtk_menu_item_is_active (action->item))
 
546
                            *state = g_variant_ref_sink (g_variant_new_string (action->name));
 
547
                          else
 
548
                            *state = g_variant_ref_sink (g_variant_new_string (""));
 
549
                        }
 
550
                      else
 
551
                        *state = g_variant_ref_sink (g_variant_new_boolean (unity_gtk_menu_item_is_active (action->item)));
 
552
                    }
 
553
                  else
 
554
                    *state = NULL;
 
555
                }
 
556
 
 
557
              return TRUE;
437
558
            }
 
559
          else if (g_strcmp0 (name, action->subname) == 0)
 
560
            {
 
561
              if (enabled != NULL)
 
562
                *enabled = TRUE;
438
563
 
439
 
          if (parameter_type != NULL)
440
 
            {
441
 
              if (action->items_by_name != NULL || (action->item != NULL && unity_gtk_menu_item_get_draw_as_radio (action->item)))
442
 
                *parameter_type = G_VARIANT_TYPE_STRING;
443
 
              else
 
564
              if (parameter_type != NULL)
444
565
                *parameter_type = NULL;
445
 
            }
446
566
 
447
 
          if (state_type != NULL)
448
 
            {
449
 
              if (action->items_by_name != NULL || (action->item != NULL && unity_gtk_menu_item_get_draw_as_radio (action->item)))
450
 
                *state_type = G_VARIANT_TYPE_STRING;
451
 
              else if (action->item != NULL && unity_gtk_menu_item_is_check (action->item))
 
567
              if (state_type != NULL)
452
568
                *state_type = G_VARIANT_TYPE_BOOLEAN;
453
 
              else
454
 
                *state_type = NULL;
455
 
            }
456
569
 
457
 
          if (state_hint != NULL)
458
 
            {
459
 
              if (action->items_by_name != NULL)
 
570
              if (state_hint != NULL)
460
571
                {
461
572
                  GVariantBuilder builder;
462
 
                  GHashTableIter iter;
463
 
                  gpointer key;
464
 
 
465
 
                  g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
466
 
 
467
 
                  g_hash_table_iter_init (&iter, action->items_by_name);
468
 
                  while (g_hash_table_iter_next (&iter, &key, NULL))
469
 
                    g_variant_builder_add (&builder, "s", key);
470
 
 
 
573
 
 
574
                  g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
 
575
                  g_variant_builder_add (&builder, "b", FALSE);
 
576
                  g_variant_builder_add (&builder, "b", TRUE);
471
577
                  *state_hint = g_variant_ref_sink (g_variant_builder_end (&builder));
472
578
                }
473
 
              else if (action->item != NULL && unity_gtk_menu_item_is_check (action->item))
474
 
                {
475
 
                  GVariantBuilder builder;
476
 
 
477
 
                  if (unity_gtk_menu_item_get_draw_as_radio (action->item))
478
 
                    {
479
 
                      g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
480
 
                      g_variant_builder_add (&builder, "s", action->name);
481
 
                      *state_hint = g_variant_ref_sink (g_variant_builder_end (&builder));
482
 
                    }
483
 
                  else
484
 
                    {
485
 
                      g_variant_builder_init (&builder, G_VARIANT_TYPE_TUPLE);
486
 
                      g_variant_builder_add (&builder, "b", FALSE);
487
 
                      g_variant_builder_add (&builder, "b", TRUE);
488
 
                      *state_hint = g_variant_ref_sink (g_variant_builder_end (&builder));
489
 
                    }
490
 
                }
491
 
              else
492
 
                *state_hint = NULL;
493
 
            }
494
 
 
495
 
          if (state != NULL)
496
 
            {
497
 
              if (action->items_by_name != NULL)
498
 
                {
499
 
                  GHashTableIter iter;
500
 
                  gpointer key;
501
 
                  gpointer value;
502
 
 
503
 
                  *state = NULL;
504
 
 
505
 
                  g_hash_table_iter_init (&iter, action->items_by_name);
506
 
                  while (*state == NULL && g_hash_table_iter_next (&iter, &key, &value))
507
 
                    if (unity_gtk_menu_item_is_active (value))
508
 
                      *state = g_variant_ref_sink (g_variant_new_string (key));
509
 
                }
510
 
              else if (action->item != NULL && unity_gtk_menu_item_is_check (action->item))
511
 
                {
512
 
                  if (unity_gtk_menu_item_get_draw_as_radio (action->item))
513
 
                    {
514
 
                      if (unity_gtk_menu_item_is_active (action->item))
515
 
                        *state = g_variant_ref_sink (g_variant_new_string (action->name));
516
 
                      else
517
 
                        *state = g_variant_ref_sink (g_variant_new_string (""));
518
 
                    }
519
 
                  else
520
 
                    *state = g_variant_ref_sink (g_variant_new_boolean (unity_gtk_menu_item_is_active (action->item)));
521
 
                }
522
 
              else
523
 
                *state = NULL;
524
 
            }
525
 
 
526
 
          return TRUE;
 
579
 
 
580
              if (state != NULL)
 
581
                *state = g_variant_ref_sink (g_variant_new_boolean (TRUE));
 
582
 
 
583
              return TRUE;
 
584
            }
 
585
          else
 
586
            g_warn_if_reached ();
527
587
        }
528
588
    }
529
589
  else
865
925
            g_warn_if_reached ();
866
926
 
867
927
          g_action_group_action_added (G_ACTION_GROUP (group), new_action->name);
 
928
 
 
929
          /* Add a new submenu action so we can detect opening and closing. */
 
930
          if (item->menu_item != NULL && gtk_menu_item_get_submenu (item->menu_item) != NULL)
 
931
            {
 
932
              gchar *subname = unity_gtk_action_group_get_action_name (group, item);
 
933
              unity_gtk_action_set_subname (new_action, subname);
 
934
              g_free (subname);
 
935
 
 
936
              if (group->actions_by_name != NULL)
 
937
                g_hash_table_insert (group->actions_by_name, new_action->subname, g_object_ref (new_action));
 
938
              else
 
939
                g_warn_if_reached ();
 
940
 
 
941
              g_action_group_action_added (G_ACTION_GROUP (group), new_action->subname);
 
942
              g_print ("add '%s'\n", new_action->subname);
 
943
            }
868
944
        }
869
945
    }
870
946
}
908
984
 
909
985
                  if (g_hash_table_size (action->items_by_name) == 0)
910
986
                    {
 
987
                      /* Remove the submenu action used to detect opening and closing. */
 
988
                      if (action->subname != NULL)
 
989
                        {
 
990
                          if (group->actions_by_name != NULL)
 
991
                            g_hash_table_remove (group->actions_by_name, action->subname);
 
992
                          else
 
993
                            g_warn_if_reached ();
 
994
 
 
995
                          g_action_group_action_removed (G_ACTION_GROUP (group), action->subname);
 
996
                          g_print ("remove '%s'\n", action->subname);
 
997
                        }
 
998
 
911
999
                      if (group->actions_by_name != NULL)
912
1000
                        g_hash_table_remove (group->actions_by_name, action->name);
913
1001
                      else
924
1012
        }
925
1013
      else
926
1014
        {
 
1015
          /* Remove the submenu action used to detect opening and closing. */
 
1016
          if (action->subname != NULL)
 
1017
            {
 
1018
              if (group->actions_by_name != NULL)
 
1019
                g_hash_table_remove (group->actions_by_name, action->subname);
 
1020
              else
 
1021
                g_warn_if_reached ();
 
1022
 
 
1023
              g_action_group_action_removed (G_ACTION_GROUP (group), action->subname);
 
1024
              g_print ("remove '%s'\n", action->subname);
 
1025
            }
 
1026
 
927
1027
          if (group->actions_by_name != NULL)
928
1028
            g_hash_table_remove (group->actions_by_name, action->name);
929
1029
          else