1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 /** \file
18  * \ingroup edinterface
19  */
20 
21 #include "BLI_listbase.h"
22 
23 #include "MEM_guardedalloc.h"
24 
25 #include "interface_intern.h"
26 
27 /* -------------------------------------------------------------------- */
28 /** \name Button Groups
29  * \{ */
30 
31 /**
32  * Every function that adds a set of buttons must create another group,
33  * then #ui_def_but adds buttons to the current group (the last).
34  */
ui_block_new_button_group(uiBlock * block,short flag)35 void ui_block_new_button_group(uiBlock *block, short flag)
36 {
37   /* Don't create a new group if there is a "lock" on new groups. */
38   if (!BLI_listbase_is_empty(&block->button_groups)) {
39     uiButtonGroup *last_button_group = block->button_groups.last;
40     if (last_button_group->flag & UI_BUTTON_GROUP_LOCK) {
41       return;
42     }
43   }
44 
45   uiButtonGroup *new_group = MEM_mallocN(sizeof(uiButtonGroup), __func__);
46   BLI_listbase_clear(&new_group->buttons);
47   new_group->flag = flag;
48   BLI_addtail(&block->button_groups, new_group);
49 }
50 
ui_button_group_add_but(uiBlock * block,uiBut * but)51 void ui_button_group_add_but(uiBlock *block, uiBut *but)
52 {
53   if (BLI_listbase_is_empty(&block->button_groups)) {
54     ui_block_new_button_group(block, 0);
55   }
56 
57   uiButtonGroup *current_button_group = block->button_groups.last;
58 
59   /* We can't use the button directly because adding it to
60    * this list would mess with its prev and next pointers. */
61   LinkData *button_link = BLI_genericNodeN(but);
62   BLI_addtail(&current_button_group->buttons, button_link);
63 }
64 
button_group_free(uiButtonGroup * button_group)65 static void button_group_free(uiButtonGroup *button_group)
66 {
67   BLI_freelistN(&button_group->buttons);
68   MEM_freeN(button_group);
69 }
70 
ui_block_free_button_groups(uiBlock * block)71 void ui_block_free_button_groups(uiBlock *block)
72 {
73   LISTBASE_FOREACH_MUTABLE (uiButtonGroup *, button_group, &block->button_groups) {
74     button_group_free(button_group);
75   }
76 }
77 
78 /* This function should be removed whenever #ui_layout_replace_but_ptr is removed. */
ui_button_group_replace_but_ptr(uiBlock * block,const void * old_but_ptr,uiBut * new_but)79 void ui_button_group_replace_but_ptr(uiBlock *block, const void *old_but_ptr, uiBut *new_but)
80 {
81   LISTBASE_FOREACH (uiButtonGroup *, button_group, &block->button_groups) {
82     LISTBASE_FOREACH (LinkData *, link, &button_group->buttons) {
83       if (link->data == old_but_ptr) {
84         link->data = new_but;
85         return;
86       }
87     }
88   }
89 }
90 
91 /** \} */
92