Merging r42533 through r42601 from trunk into soc-2011-tomato
[blender.git] / source / blender / windowmanager / intern / wm.c
index fcf8951d796375005ec0b8fb8e46a5d8aa3fa3c4..c6c67e22bfd9d1b9b00a0a9ba42ca6f39c8dc43e 100644 (file)
@@ -1,6 +1,4 @@
-/**
- * $Id$
- *
+/*
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * ***** END GPL LICENSE BLOCK *****
  */
 
-#include "string.h"
+/** \file blender/windowmanager/intern/wm.c
+ *  \ingroup wm
+ */
+
+
+#include <string.h>
+#include <stddef.h>
+
+#include "BLO_sys_types.h"
 
 #include "DNA_windowmanager_types.h"
 
 #include "GHOST_C-api.h"
 
+#include "MEM_guardedalloc.h"
+
+#include "BLI_utildefines.h"
 #include "BLI_blenlib.h"
+#include "BLI_ghash.h"
 
 #include "BKE_blender.h"
 #include "BKE_context.h"
 #include "wm_draw.h"
 #include "wm.h"
 
-#include "MEM_guardedalloc.h"
-
 #include "ED_screen.h"
-#include "BPY_extern.h"
 
+#ifdef WITH_PYTHON
+#include "BPY_extern.h"
+#endif
 
 /* ****************************************************** */
 
@@ -64,7 +74,7 @@
 void WM_operator_free(wmOperator *op)
 {
 
-#ifndef DISABLE_PYTHON
+#ifdef WITH_PYTHON
        if(op->py_instance) {
                /* do this first incase there are any __del__ functions or
                 * similar that use properties */
@@ -122,14 +132,13 @@ void wm_operator_register(bContext *C, wmOperator *op)
        }
        
        /* so the console is redrawn */
-       WM_event_add_notifier(C, NC_SPACE|ND_SPACE_CONSOLE_REPORT, NULL);
+       WM_event_add_notifier(C, NC_SPACE|ND_SPACE_INFO_REPORT, NULL);
        WM_event_add_notifier(C, NC_WM|ND_HISTORY, NULL);
 }
 
 
-void WM_operator_stack_clear(bContext *C)
+void WM_operator_stack_clear(wmWindowManager *wm)
 {
-       wmWindowManager *wm= CTX_wm_manager(C);
        wmOperator *op;
        
        while((op= wm->operators.first)) {
@@ -137,21 +146,21 @@ void WM_operator_stack_clear(bContext *C)
                WM_operator_free(op);
        }
        
-       WM_event_add_notifier(C, NC_WM|ND_HISTORY, NULL);
+       WM_main_add_notifier(NC_WM|ND_HISTORY, NULL);
 }
 
 /* ****************************************** */
 
-static ListBase menutypes = {NULL, NULL}; /* global menutype list */
+static GHash *menutypes_hash= NULL;
 
 MenuType *WM_menutype_find(const char *idname, int quiet)
 {
        MenuType* mt;
 
        if (idname[0]) {
-               for(mt=menutypes.first; mt; mt=mt->next)
-                       if(strcmp(idname, mt->idname)==0)
-                               return mt;
+               mt= BLI_ghash_lookup(menutypes_hash, idname);
+               if(mt)
+                       return mt;
        }
 
        if(!quiet)
@@ -162,29 +171,35 @@ MenuType *WM_menutype_find(const char *idname, int quiet)
 
 int WM_menutype_add(MenuType* mt)
 {
-       BLI_addtail(&menutypes, mt);
+       BLI_ghash_insert(menutypes_hash, (void *)mt->idname, mt);
        return 1;
 }
 
 void WM_menutype_freelink(MenuType* mt)
 {
-       BLI_freelinkN(&menutypes, mt);
+       BLI_ghash_remove(menutypes_hash, mt->idname, NULL, (GHashValFreeFP)MEM_freeN);
 }
 
-void WM_menutype_free(void)
+/* called on initialize WM_init() */
+void WM_menutype_init(void)
 {
-       MenuType* mt= menutypes.first, *mt_next;
+       menutypes_hash= BLI_ghash_new(BLI_ghashutil_strhash, BLI_ghashutil_strcmp, "menutypes_hash gh");
+}
 
-       while(mt) {
-               mt_next= mt->next;
+void WM_menutype_free(void)
+{
+       GHashIterator *iter= BLI_ghashIterator_new(menutypes_hash);
 
-               if(mt->ext.free)
+       for( ; !BLI_ghashIterator_isDone(iter); BLI_ghashIterator_step(iter)) {
+               MenuType *mt= BLI_ghashIterator_getValue(iter);
+               if(mt->ext.free) {
                        mt->ext.free(mt->ext.data);
-
-               WM_menutype_freelink(mt);
-
-               mt= mt_next;
+               }
        }
+       BLI_ghashIterator_free(iter);
+
+       BLI_ghash_free(menutypes_hash, NULL, (GHashValFreeFP)MEM_freeN);
+       menutypes_hash= NULL;
 }
 
 /* ****************************************** */
@@ -193,14 +208,28 @@ void WM_keymap_init(bContext *C)
 {
        wmWindowManager *wm= CTX_wm_manager(C);
 
+       /* create standard key configs */
        if(!wm->defaultconf)
-               wm->defaultconf= WM_keyconfig_add(wm, "Blender");
+               wm->defaultconf= WM_keyconfig_new(wm, "Blender");
+       if(!wm->addonconf)
+               wm->addonconf= WM_keyconfig_new(wm, "Blender Addon");
+       if(!wm->userconf)
+               wm->userconf= WM_keyconfig_new(wm, "Blender User");
        
-       if(wm && CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
-               /* create default key config */
-               wm_window_keymap(wm->defaultconf);
-               ED_spacetypes_keymap(wm->defaultconf);
-               WM_keyconfig_userdef(wm);
+       /* initialize only after python init is done, for keymaps that
+          use python operators */
+       if(CTX_py_init_get(C) && (wm->initialized & WM_INIT_KEYMAP) == 0) {
+               /* create default key config, only initialize once,
+                  it's persistent across sessions */
+               if(!(wm->defaultconf->flag & KEYCONF_INIT_DEFAULT)) {
+                       wm_window_keymap(wm->defaultconf);
+                       ED_spacetypes_keymap(wm->defaultconf);
+
+                       wm->defaultconf->flag |= KEYCONF_INIT_DEFAULT;
+               }
+
+               WM_keyconfig_update_tag(NULL, NULL);
+               WM_keyconfig_update(wm);
 
                wm->initialized |= WM_INIT_KEYMAP;
        }
@@ -226,13 +255,14 @@ void WM_check(bContext *C)
                }
 
                /* case: no open windows at all, for old file reads */
-               wm_window_add_ghostwindows(wm);
+               wm_window_add_ghostwindows(C, wm);
+       }
 
-               /* case: fileread */
-               if((wm->initialized & WM_INIT_WINDOW) == 0) {
-                       ED_screens_initialize(wm);
-                       wm->initialized |= WM_INIT_WINDOW;
-               }
+       /* case: fileread */
+       /* note: this runs in bg mode to set the screen context cb */
+       if((wm->initialized & WM_INIT_WINDOW) == 0) {
+               ED_screens_initialize(wm);
+               wm->initialized |= WM_INIT_WINDOW;
        }
 }
 
@@ -269,7 +299,7 @@ void wm_add_default(bContext *C)
        win= wm_window_new(C);
        win->screen= screen;
        screen->winid= win->winid;
-       BLI_strncpy(win->screenname, screen->id.name+2, 21);
+       BLI_strncpy(win->screenname, screen->id.name+2, sizeof(win->screenname));
        
        wm->winactive= win;
        wm->file_saved= 1;