File: gui_window_manager.c

package info (click to toggle)
crystal-facet-uml 1.27.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,216 kB
  • sloc: ansic: 86,801; xml: 2,235; cpp: 60; makefile: 54; sh: 7
file content (154 lines) | stat: -rw-r--r-- 4,983 bytes parent folder | download
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
/* File: gui_window_manager.c; Copyright and License: see below */

#include "gui_window_manager.h"
#include "trace.h"
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdbool.h>

void gui_window_manager_init ( gui_window_manager_t *this_, ctrl_controller_t *controller, data_database_t *database )
{
    TRACE_BEGIN();

    gui_resources_init( &((*this_).gui_resources) );
    data_database_reader_init( &((*this_).db_reader), database );
    (*this_).controller = controller;
    (*this_).database = database;
    observer_init( &((*this_).window_close_observer), this_, (void (*)(void*,void*)) &gui_window_manager_close_main_window, "gui_window_manager_close_main_window()" );
    observer_init( &((*this_).window_open_observer), this_, (void (*)(void*,void*)) &gui_window_manager_open_main_window2, "gui_window_manager_open_main_window2()" );
    for( int index = 0; index < GUI_WINDOW_MANAGER_MAX_MAIN_WINDOWS; index ++ )
    {
        (*this_).main_window_active[index] = false;
    }

    TRACE_END();
}

void gui_window_manager_destroy( gui_window_manager_t *this_ )
{
    TRACE_BEGIN();

    for( int index = 0; index < GUI_WINDOW_MANAGER_MAX_MAIN_WINDOWS; index ++ )
    {
        if ( (*this_).main_window_active[index] )
        {
            gui_main_window_destroy( &((*this_).main_window[index]) );
            (*this_).main_window_active[index] = false;
        }
    }
    observer_destroy( &((*this_).window_close_observer) );
    data_database_reader_destroy( &((*this_).db_reader) );
    gui_resources_destroy( &((*this_).gui_resources) );

    TRACE_END();
}

gui_main_window_t *gui_window_manager_open_main_window( gui_window_manager_t *this_ )
{
    TRACE_BEGIN();
    gui_main_window_t *result;

    int pos = -1;
    for( int index = 0; index < GUI_WINDOW_MANAGER_MAX_MAIN_WINDOWS; index ++ )
    {
        if ( ! (*this_).main_window_active[index] )
        {
            pos = index;
        }
    }

    if ( -1 != pos )
    {
        gui_main_window_init( &((*this_).main_window[pos]),
                              (*this_).controller,
                              (*this_).database,
                              &((*this_).db_reader),
                              &((*this_).gui_resources),
                              &((*this_).window_close_observer),
                              &((*this_).window_open_observer)
                            );
        (*this_).main_window_active[pos] = true;
        TRACE_INFO_INT( "main_window[] index:", pos );
        result = &((*this_).main_window[pos]);
    }
    else
    {
        TSLOG_ERROR_INT( "Maximum number of windows already open.", GUI_WINDOW_MANAGER_MAX_MAIN_WINDOWS );
        result = NULL;
    }

    TRACE_END();
    return result;
}

void gui_window_manager_close_main_window( gui_window_manager_t *this_, gui_main_window_t *main_window )
{
    TRACE_BEGIN();
    int count_active = 0;
    int count_closed = 0;

    for( int index = 0; index < GUI_WINDOW_MANAGER_MAX_MAIN_WINDOWS; index ++ )
    {
        if ( (*this_).main_window_active[index] )
        {
            if ( main_window == &((*this_).main_window[index]) )
            {
                gui_main_window_destroy( &((*this_).main_window[index]) );
                (*this_).main_window_active[index] = false;
                TRACE_INFO_INT( "main_window[] index:", index );
                count_closed ++;
            }
            else
            {
                count_active ++;
            }
        }
    }

    if ( count_closed == 0 )
    {
        TSLOG_ERROR( "no window of given address found" );
    }
    if ( count_active == 0 )
    {
        gtk_main_quit();
    }

    TRACE_END();
}

void gui_window_manager_open_main_window2( gui_window_manager_t *this_, gui_simple_message_to_user_t *message_to_user )
{
    TRACE_BEGIN();

    gui_main_window_t *new_win;
    new_win = gui_window_manager_open_main_window( this_ );

    if ( NULL == new_win )
    {
        gui_simple_message_to_user_show_message_with_quantity( message_to_user,
                                                               GUI_SIMPLE_MESSAGE_TYPE_WARNING,
                                                               GUI_SIMPLE_MESSAGE_CONTENT_MAX_WINDOWS_ALREADY_OPEN,
                                                               GUI_WINDOW_MANAGER_MAX_MAIN_WINDOWS
                                                             );
    }

    TRACE_END();
}


/*
Copyright 2016-2021 Andreas Warnke

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/